1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h>
| 1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h>
|
45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 269497 2014-08-04 01:16:20Z mav $");
| 45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 269622 2014-08-06 08:54:31Z mav $");
|
46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/types.h> 51#include <sys/kthread.h> 52#include <sys/bio.h> 53#include <sys/fcntl.h> 54#include <sys/lock.h> 55#include <sys/module.h> 56#include <sys/mutex.h> 57#include <sys/condvar.h> 58#include <sys/malloc.h> 59#include <sys/conf.h> 60#include <sys/ioccom.h> 61#include <sys/queue.h> 62#include <sys/sbuf.h> 63#include <sys/smp.h> 64#include <sys/endian.h> 65#include <sys/sysctl.h> 66 67#include <cam/cam.h> 68#include <cam/scsi/scsi_all.h> 69#include <cam/scsi/scsi_da.h> 70#include <cam/ctl/ctl_io.h> 71#include <cam/ctl/ctl.h> 72#include <cam/ctl/ctl_frontend.h> 73#include <cam/ctl/ctl_frontend_internal.h> 74#include <cam/ctl/ctl_util.h> 75#include <cam/ctl/ctl_backend.h> 76#include <cam/ctl/ctl_ioctl.h> 77#include <cam/ctl/ctl_ha.h> 78#include <cam/ctl/ctl_private.h> 79#include <cam/ctl/ctl_debug.h> 80#include <cam/ctl/ctl_scsi_all.h> 81#include <cam/ctl/ctl_error.h> 82 83struct ctl_softc *control_softc = NULL; 84 85/* 86 * Size and alignment macros needed for Copan-specific HA hardware. These 87 * can go away when the HA code is re-written, and uses busdma for any 88 * hardware. 89 */ 90#define CTL_ALIGN_8B(target, source, type) \ 91 if (((uint32_t)source & 0x7) != 0) \ 92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93 else \ 94 target = (type)source; 95 96#define CTL_SIZE_8B(target, size) \ 97 if ((size & 0x7) != 0) \ 98 target = size + (0x8 - (size & 0x7)); \ 99 else \ 100 target = size; 101 102#define CTL_ALIGN_8B_MARGIN 16 103 104/* 105 * Template mode pages. 106 */ 107 108/* 109 * Note that these are default values only. The actual values will be 110 * filled in when the user does a mode sense. 111 */ 112static struct copan_power_subpage power_page_default = { 113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 114 /*subpage*/ PWR_SUBPAGE_CODE, 115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 117 /*page_version*/ PWR_VERSION, 118 /* total_luns */ 26, 119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122 0, 0, 0, 0, 0, 0} 123}; 124 125static struct copan_power_subpage power_page_changeable = { 126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 127 /*subpage*/ PWR_SUBPAGE_CODE, 128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 130 /*page_version*/ 0, 131 /* total_luns */ 0, 132 /* max_active_luns*/ 0, 133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135 0, 0, 0, 0, 0, 0} 136}; 137 138static struct copan_aps_subpage aps_page_default = { 139 APS_PAGE_CODE | SMPH_SPF, //page_code 140 APS_SUBPAGE_CODE, //subpage 141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 143 APS_VERSION, //page_version 144 0, //lock_active 145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147 0, 0, 0, 0, 0} //reserved 148}; 149 150static struct copan_aps_subpage aps_page_changeable = { 151 APS_PAGE_CODE | SMPH_SPF, //page_code 152 APS_SUBPAGE_CODE, //subpage 153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 155 0, //page_version 156 0, //lock_active 157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159 0, 0, 0, 0, 0} //reserved 160}; 161 162static struct copan_debugconf_subpage debugconf_page_default = { 163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 164 DBGCNF_SUBPAGE_CODE, /* subpage */ 165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 167 DBGCNF_VERSION, /* page_version */ 168 {CTL_TIME_IO_DEFAULT_SECS>>8, 169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 170}; 171 172static struct copan_debugconf_subpage debugconf_page_changeable = { 173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 174 DBGCNF_SUBPAGE_CODE, /* subpage */ 175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 177 0, /* page_version */ 178 {0xff,0xff}, /* ctl_time_io_secs */ 179}; 180 181static struct scsi_format_page format_page_default = { 182 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 183 /*page_length*/sizeof(struct scsi_format_page) - 2, 184 /*tracks_per_zone*/ {0, 0}, 185 /*alt_sectors_per_zone*/ {0, 0}, 186 /*alt_tracks_per_zone*/ {0, 0}, 187 /*alt_tracks_per_lun*/ {0, 0}, 188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 190 /*bytes_per_sector*/ {0, 0}, 191 /*interleave*/ {0, 0}, 192 /*track_skew*/ {0, 0}, 193 /*cylinder_skew*/ {0, 0}, 194 /*flags*/ SFP_HSEC, 195 /*reserved*/ {0, 0, 0} 196}; 197 198static struct scsi_format_page format_page_changeable = { 199 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 200 /*page_length*/sizeof(struct scsi_format_page) - 2, 201 /*tracks_per_zone*/ {0, 0}, 202 /*alt_sectors_per_zone*/ {0, 0}, 203 /*alt_tracks_per_zone*/ {0, 0}, 204 /*alt_tracks_per_lun*/ {0, 0}, 205 /*sectors_per_track*/ {0, 0}, 206 /*bytes_per_sector*/ {0, 0}, 207 /*interleave*/ {0, 0}, 208 /*track_skew*/ {0, 0}, 209 /*cylinder_skew*/ {0, 0}, 210 /*flags*/ 0, 211 /*reserved*/ {0, 0, 0} 212}; 213 214static struct scsi_rigid_disk_page rigid_disk_page_default = { 215 /*page_code*/SMS_RIGID_DISK_PAGE, 216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 217 /*cylinders*/ {0, 0, 0}, 218 /*heads*/ CTL_DEFAULT_HEADS, 219 /*start_write_precomp*/ {0, 0, 0}, 220 /*start_reduced_current*/ {0, 0, 0}, 221 /*step_rate*/ {0, 0}, 222 /*landing_zone_cylinder*/ {0, 0, 0}, 223 /*rpl*/ SRDP_RPL_DISABLED, 224 /*rotational_offset*/ 0, 225 /*reserved1*/ 0, 226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 227 CTL_DEFAULT_ROTATION_RATE & 0xff}, 228 /*reserved2*/ {0, 0} 229}; 230 231static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 232 /*page_code*/SMS_RIGID_DISK_PAGE, 233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 234 /*cylinders*/ {0, 0, 0}, 235 /*heads*/ 0, 236 /*start_write_precomp*/ {0, 0, 0}, 237 /*start_reduced_current*/ {0, 0, 0}, 238 /*step_rate*/ {0, 0}, 239 /*landing_zone_cylinder*/ {0, 0, 0}, 240 /*rpl*/ 0, 241 /*rotational_offset*/ 0, 242 /*reserved1*/ 0, 243 /*rotation_rate*/ {0, 0}, 244 /*reserved2*/ {0, 0} 245}; 246 247static struct scsi_caching_page caching_page_default = { 248 /*page_code*/SMS_CACHING_PAGE, 249 /*page_length*/sizeof(struct scsi_caching_page) - 2, 250 /*flags1*/ SCP_DISC | SCP_WCE, 251 /*ret_priority*/ 0, 252 /*disable_pf_transfer_len*/ {0xff, 0xff}, 253 /*min_prefetch*/ {0, 0}, 254 /*max_prefetch*/ {0xff, 0xff}, 255 /*max_pf_ceiling*/ {0xff, 0xff}, 256 /*flags2*/ 0, 257 /*cache_segments*/ 0, 258 /*cache_seg_size*/ {0, 0}, 259 /*reserved*/ 0, 260 /*non_cache_seg_size*/ {0, 0, 0} 261}; 262 263static struct scsi_caching_page caching_page_changeable = { 264 /*page_code*/SMS_CACHING_PAGE, 265 /*page_length*/sizeof(struct scsi_caching_page) - 2, 266 /*flags1*/ 0, 267 /*ret_priority*/ 0, 268 /*disable_pf_transfer_len*/ {0, 0}, 269 /*min_prefetch*/ {0, 0}, 270 /*max_prefetch*/ {0, 0}, 271 /*max_pf_ceiling*/ {0, 0}, 272 /*flags2*/ 0, 273 /*cache_segments*/ 0, 274 /*cache_seg_size*/ {0, 0}, 275 /*reserved*/ 0, 276 /*non_cache_seg_size*/ {0, 0, 0} 277}; 278 279static struct scsi_control_page control_page_default = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE, 281 /*page_length*/sizeof(struct scsi_control_page) - 2, 282 /*rlec*/0, 283 /*queue_flags*/0, 284 /*eca_and_aen*/0, 285 /*flags4*/SCP_TAS, 286 /*aen_holdoff_period*/{0, 0}, 287 /*busy_timeout_period*/{0, 0}, 288 /*extended_selftest_completion_time*/{0, 0} 289}; 290 291static struct scsi_control_page control_page_changeable = { 292 /*page_code*/SMS_CONTROL_MODE_PAGE, 293 /*page_length*/sizeof(struct scsi_control_page) - 2, 294 /*rlec*/SCP_DSENSE, 295 /*queue_flags*/0, 296 /*eca_and_aen*/0, 297 /*flags4*/0, 298 /*aen_holdoff_period*/{0, 0}, 299 /*busy_timeout_period*/{0, 0}, 300 /*extended_selftest_completion_time*/{0, 0} 301}; 302 303 304/* 305 * XXX KDM move these into the softc. 306 */ 307static int rcv_sync_msg; 308static int persis_offset; 309static uint8_t ctl_pause_rtr; 310static int ctl_is_single = 1; 311static int index_to_aps_page; 312 313SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 314static int worker_threads = -1; 315SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 316 &worker_threads, 1, "Number of worker threads"); 317static int verbose = 0; 318SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 319 &verbose, 0, "Show SCSI errors returned to initiator"); 320 321/* 322 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
| 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/types.h> 51#include <sys/kthread.h> 52#include <sys/bio.h> 53#include <sys/fcntl.h> 54#include <sys/lock.h> 55#include <sys/module.h> 56#include <sys/mutex.h> 57#include <sys/condvar.h> 58#include <sys/malloc.h> 59#include <sys/conf.h> 60#include <sys/ioccom.h> 61#include <sys/queue.h> 62#include <sys/sbuf.h> 63#include <sys/smp.h> 64#include <sys/endian.h> 65#include <sys/sysctl.h> 66 67#include <cam/cam.h> 68#include <cam/scsi/scsi_all.h> 69#include <cam/scsi/scsi_da.h> 70#include <cam/ctl/ctl_io.h> 71#include <cam/ctl/ctl.h> 72#include <cam/ctl/ctl_frontend.h> 73#include <cam/ctl/ctl_frontend_internal.h> 74#include <cam/ctl/ctl_util.h> 75#include <cam/ctl/ctl_backend.h> 76#include <cam/ctl/ctl_ioctl.h> 77#include <cam/ctl/ctl_ha.h> 78#include <cam/ctl/ctl_private.h> 79#include <cam/ctl/ctl_debug.h> 80#include <cam/ctl/ctl_scsi_all.h> 81#include <cam/ctl/ctl_error.h> 82 83struct ctl_softc *control_softc = NULL; 84 85/* 86 * Size and alignment macros needed for Copan-specific HA hardware. These 87 * can go away when the HA code is re-written, and uses busdma for any 88 * hardware. 89 */ 90#define CTL_ALIGN_8B(target, source, type) \ 91 if (((uint32_t)source & 0x7) != 0) \ 92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93 else \ 94 target = (type)source; 95 96#define CTL_SIZE_8B(target, size) \ 97 if ((size & 0x7) != 0) \ 98 target = size + (0x8 - (size & 0x7)); \ 99 else \ 100 target = size; 101 102#define CTL_ALIGN_8B_MARGIN 16 103 104/* 105 * Template mode pages. 106 */ 107 108/* 109 * Note that these are default values only. The actual values will be 110 * filled in when the user does a mode sense. 111 */ 112static struct copan_power_subpage power_page_default = { 113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 114 /*subpage*/ PWR_SUBPAGE_CODE, 115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 117 /*page_version*/ PWR_VERSION, 118 /* total_luns */ 26, 119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122 0, 0, 0, 0, 0, 0} 123}; 124 125static struct copan_power_subpage power_page_changeable = { 126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 127 /*subpage*/ PWR_SUBPAGE_CODE, 128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 130 /*page_version*/ 0, 131 /* total_luns */ 0, 132 /* max_active_luns*/ 0, 133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135 0, 0, 0, 0, 0, 0} 136}; 137 138static struct copan_aps_subpage aps_page_default = { 139 APS_PAGE_CODE | SMPH_SPF, //page_code 140 APS_SUBPAGE_CODE, //subpage 141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 143 APS_VERSION, //page_version 144 0, //lock_active 145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147 0, 0, 0, 0, 0} //reserved 148}; 149 150static struct copan_aps_subpage aps_page_changeable = { 151 APS_PAGE_CODE | SMPH_SPF, //page_code 152 APS_SUBPAGE_CODE, //subpage 153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 155 0, //page_version 156 0, //lock_active 157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159 0, 0, 0, 0, 0} //reserved 160}; 161 162static struct copan_debugconf_subpage debugconf_page_default = { 163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 164 DBGCNF_SUBPAGE_CODE, /* subpage */ 165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 167 DBGCNF_VERSION, /* page_version */ 168 {CTL_TIME_IO_DEFAULT_SECS>>8, 169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 170}; 171 172static struct copan_debugconf_subpage debugconf_page_changeable = { 173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 174 DBGCNF_SUBPAGE_CODE, /* subpage */ 175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 177 0, /* page_version */ 178 {0xff,0xff}, /* ctl_time_io_secs */ 179}; 180 181static struct scsi_format_page format_page_default = { 182 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 183 /*page_length*/sizeof(struct scsi_format_page) - 2, 184 /*tracks_per_zone*/ {0, 0}, 185 /*alt_sectors_per_zone*/ {0, 0}, 186 /*alt_tracks_per_zone*/ {0, 0}, 187 /*alt_tracks_per_lun*/ {0, 0}, 188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 190 /*bytes_per_sector*/ {0, 0}, 191 /*interleave*/ {0, 0}, 192 /*track_skew*/ {0, 0}, 193 /*cylinder_skew*/ {0, 0}, 194 /*flags*/ SFP_HSEC, 195 /*reserved*/ {0, 0, 0} 196}; 197 198static struct scsi_format_page format_page_changeable = { 199 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 200 /*page_length*/sizeof(struct scsi_format_page) - 2, 201 /*tracks_per_zone*/ {0, 0}, 202 /*alt_sectors_per_zone*/ {0, 0}, 203 /*alt_tracks_per_zone*/ {0, 0}, 204 /*alt_tracks_per_lun*/ {0, 0}, 205 /*sectors_per_track*/ {0, 0}, 206 /*bytes_per_sector*/ {0, 0}, 207 /*interleave*/ {0, 0}, 208 /*track_skew*/ {0, 0}, 209 /*cylinder_skew*/ {0, 0}, 210 /*flags*/ 0, 211 /*reserved*/ {0, 0, 0} 212}; 213 214static struct scsi_rigid_disk_page rigid_disk_page_default = { 215 /*page_code*/SMS_RIGID_DISK_PAGE, 216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 217 /*cylinders*/ {0, 0, 0}, 218 /*heads*/ CTL_DEFAULT_HEADS, 219 /*start_write_precomp*/ {0, 0, 0}, 220 /*start_reduced_current*/ {0, 0, 0}, 221 /*step_rate*/ {0, 0}, 222 /*landing_zone_cylinder*/ {0, 0, 0}, 223 /*rpl*/ SRDP_RPL_DISABLED, 224 /*rotational_offset*/ 0, 225 /*reserved1*/ 0, 226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 227 CTL_DEFAULT_ROTATION_RATE & 0xff}, 228 /*reserved2*/ {0, 0} 229}; 230 231static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 232 /*page_code*/SMS_RIGID_DISK_PAGE, 233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 234 /*cylinders*/ {0, 0, 0}, 235 /*heads*/ 0, 236 /*start_write_precomp*/ {0, 0, 0}, 237 /*start_reduced_current*/ {0, 0, 0}, 238 /*step_rate*/ {0, 0}, 239 /*landing_zone_cylinder*/ {0, 0, 0}, 240 /*rpl*/ 0, 241 /*rotational_offset*/ 0, 242 /*reserved1*/ 0, 243 /*rotation_rate*/ {0, 0}, 244 /*reserved2*/ {0, 0} 245}; 246 247static struct scsi_caching_page caching_page_default = { 248 /*page_code*/SMS_CACHING_PAGE, 249 /*page_length*/sizeof(struct scsi_caching_page) - 2, 250 /*flags1*/ SCP_DISC | SCP_WCE, 251 /*ret_priority*/ 0, 252 /*disable_pf_transfer_len*/ {0xff, 0xff}, 253 /*min_prefetch*/ {0, 0}, 254 /*max_prefetch*/ {0xff, 0xff}, 255 /*max_pf_ceiling*/ {0xff, 0xff}, 256 /*flags2*/ 0, 257 /*cache_segments*/ 0, 258 /*cache_seg_size*/ {0, 0}, 259 /*reserved*/ 0, 260 /*non_cache_seg_size*/ {0, 0, 0} 261}; 262 263static struct scsi_caching_page caching_page_changeable = { 264 /*page_code*/SMS_CACHING_PAGE, 265 /*page_length*/sizeof(struct scsi_caching_page) - 2, 266 /*flags1*/ 0, 267 /*ret_priority*/ 0, 268 /*disable_pf_transfer_len*/ {0, 0}, 269 /*min_prefetch*/ {0, 0}, 270 /*max_prefetch*/ {0, 0}, 271 /*max_pf_ceiling*/ {0, 0}, 272 /*flags2*/ 0, 273 /*cache_segments*/ 0, 274 /*cache_seg_size*/ {0, 0}, 275 /*reserved*/ 0, 276 /*non_cache_seg_size*/ {0, 0, 0} 277}; 278 279static struct scsi_control_page control_page_default = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE, 281 /*page_length*/sizeof(struct scsi_control_page) - 2, 282 /*rlec*/0, 283 /*queue_flags*/0, 284 /*eca_and_aen*/0, 285 /*flags4*/SCP_TAS, 286 /*aen_holdoff_period*/{0, 0}, 287 /*busy_timeout_period*/{0, 0}, 288 /*extended_selftest_completion_time*/{0, 0} 289}; 290 291static struct scsi_control_page control_page_changeable = { 292 /*page_code*/SMS_CONTROL_MODE_PAGE, 293 /*page_length*/sizeof(struct scsi_control_page) - 2, 294 /*rlec*/SCP_DSENSE, 295 /*queue_flags*/0, 296 /*eca_and_aen*/0, 297 /*flags4*/0, 298 /*aen_holdoff_period*/{0, 0}, 299 /*busy_timeout_period*/{0, 0}, 300 /*extended_selftest_completion_time*/{0, 0} 301}; 302 303 304/* 305 * XXX KDM move these into the softc. 306 */ 307static int rcv_sync_msg; 308static int persis_offset; 309static uint8_t ctl_pause_rtr; 310static int ctl_is_single = 1; 311static int index_to_aps_page; 312 313SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 314static int worker_threads = -1; 315SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 316 &worker_threads, 1, "Number of worker threads"); 317static int verbose = 0; 318SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 319 &verbose, 0, "Show SCSI errors returned to initiator"); 320 321/* 322 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
|
323 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0) and 324 * Logical Block Provisioning (0xB2)
| 323 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 324 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
|
325 */
| 325 */
|
326#define SCSI_EVPD_NUM_SUPPORTED_PAGES 7
| 326#define SCSI_EVPD_NUM_SUPPORTED_PAGES 8
|
327 328static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 329 int param); 330static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 331static int ctl_init(void); 332void ctl_shutdown(void); 333static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 334static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 335static void ctl_ioctl_online(void *arg); 336static void ctl_ioctl_offline(void *arg); 337static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 338static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 339static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 340static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 341static int ctl_ioctl_submit_wait(union ctl_io *io); 342static void ctl_ioctl_datamove(union ctl_io *io); 343static void ctl_ioctl_done(union ctl_io *io); 344static void ctl_ioctl_hard_startstop_callback(void *arg, 345 struct cfi_metatask *metatask); 346static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 347static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 348 struct ctl_ooa *ooa_hdr, 349 struct ctl_ooa_entry *kern_entries); 350static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 351 struct thread *td); 352static uint32_t ctl_map_lun(int port_num, uint32_t lun); 353static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 354#ifdef unused 355static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 356 uint32_t targ_target, uint32_t targ_lun, 357 int can_wait); 358static void ctl_kfree_io(union ctl_io *io); 359#endif /* unused */ 360static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 361 struct ctl_be_lun *be_lun, struct ctl_id target_id); 362static int ctl_free_lun(struct ctl_lun *lun); 363static void ctl_create_lun(struct ctl_be_lun *be_lun); 364/** 365static void ctl_failover_change_pages(struct ctl_softc *softc, 366 struct ctl_scsiio *ctsio, int master); 367**/ 368 369static int ctl_do_mode_select(union ctl_io *io); 370static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 371 uint64_t res_key, uint64_t sa_res_key, 372 uint8_t type, uint32_t residx, 373 struct ctl_scsiio *ctsio, 374 struct scsi_per_res_out *cdb, 375 struct scsi_per_res_out_parms* param); 376static void ctl_pro_preempt_other(struct ctl_lun *lun, 377 union ctl_ha_msg *msg); 378static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 379static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 380static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 381static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 382static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 383 int alloc_len); 384static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 385 int alloc_len);
| 327 328static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 329 int param); 330static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 331static int ctl_init(void); 332void ctl_shutdown(void); 333static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 334static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 335static void ctl_ioctl_online(void *arg); 336static void ctl_ioctl_offline(void *arg); 337static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 338static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 339static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 340static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 341static int ctl_ioctl_submit_wait(union ctl_io *io); 342static void ctl_ioctl_datamove(union ctl_io *io); 343static void ctl_ioctl_done(union ctl_io *io); 344static void ctl_ioctl_hard_startstop_callback(void *arg, 345 struct cfi_metatask *metatask); 346static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 347static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 348 struct ctl_ooa *ooa_hdr, 349 struct ctl_ooa_entry *kern_entries); 350static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 351 struct thread *td); 352static uint32_t ctl_map_lun(int port_num, uint32_t lun); 353static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 354#ifdef unused 355static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 356 uint32_t targ_target, uint32_t targ_lun, 357 int can_wait); 358static void ctl_kfree_io(union ctl_io *io); 359#endif /* unused */ 360static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 361 struct ctl_be_lun *be_lun, struct ctl_id target_id); 362static int ctl_free_lun(struct ctl_lun *lun); 363static void ctl_create_lun(struct ctl_be_lun *be_lun); 364/** 365static void ctl_failover_change_pages(struct ctl_softc *softc, 366 struct ctl_scsiio *ctsio, int master); 367**/ 368 369static int ctl_do_mode_select(union ctl_io *io); 370static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 371 uint64_t res_key, uint64_t sa_res_key, 372 uint8_t type, uint32_t residx, 373 struct ctl_scsiio *ctsio, 374 struct scsi_per_res_out *cdb, 375 struct scsi_per_res_out_parms* param); 376static void ctl_pro_preempt_other(struct ctl_lun *lun, 377 union ctl_ha_msg *msg); 378static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 379static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 380static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 381static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 382static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 383 int alloc_len); 384static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 385 int alloc_len);
|
| 386static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
|
386static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 387static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 388static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 389static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 390static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 391static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 392 union ctl_io *ooa_io); 393static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 394 union ctl_io *starting_io); 395static int ctl_check_blocked(struct ctl_lun *lun); 396static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 397 struct ctl_lun *lun, 398 const struct ctl_cmd_entry *entry, 399 struct ctl_scsiio *ctsio); 400//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 401static void ctl_failover(void); 402static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 403 struct ctl_scsiio *ctsio); 404static int ctl_scsiio(struct ctl_scsiio *ctsio); 405 406static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 407static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 408 ctl_ua_type ua_type); 409static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 410 ctl_ua_type ua_type); 411static int ctl_abort_task(union ctl_io *io); 412static int ctl_abort_task_set(union ctl_io *io); 413static int ctl_i_t_nexus_reset(union ctl_io *io); 414static void ctl_run_task(union ctl_io *io); 415#ifdef CTL_IO_DELAY 416static void ctl_datamove_timer_wakeup(void *arg); 417static void ctl_done_timer_wakeup(void *arg); 418#endif /* CTL_IO_DELAY */ 419 420static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 421static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 422static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 423static void ctl_datamove_remote_write(union ctl_io *io); 424static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 425static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 426static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 427static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 428 ctl_ha_dt_cb callback); 429static void ctl_datamove_remote_read(union ctl_io *io); 430static void ctl_datamove_remote(union ctl_io *io); 431static int ctl_process_done(union ctl_io *io); 432static void ctl_lun_thread(void *arg); 433static void ctl_work_thread(void *arg); 434static void ctl_enqueue_incoming(union ctl_io *io); 435static void ctl_enqueue_rtr(union ctl_io *io); 436static void ctl_enqueue_done(union ctl_io *io); 437static void ctl_enqueue_isc(union ctl_io *io); 438static const struct ctl_cmd_entry * 439 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 440static const struct ctl_cmd_entry * 441 ctl_validate_command(struct ctl_scsiio *ctsio); 442static int ctl_cmd_applicable(uint8_t lun_type, 443 const struct ctl_cmd_entry *entry); 444 445/* 446 * Load the serialization table. This isn't very pretty, but is probably 447 * the easiest way to do it. 448 */ 449#include "ctl_ser_table.c" 450 451/* 452 * We only need to define open, close and ioctl routines for this driver. 453 */ 454static struct cdevsw ctl_cdevsw = { 455 .d_version = D_VERSION, 456 .d_flags = 0, 457 .d_open = ctl_open, 458 .d_close = ctl_close, 459 .d_ioctl = ctl_ioctl, 460 .d_name = "ctl", 461}; 462 463 464MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 465MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 466 467static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 468 469static moduledata_t ctl_moduledata = { 470 "ctl", 471 ctl_module_event_handler, 472 NULL 473}; 474 475DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 476MODULE_VERSION(ctl, 1); 477 478static struct ctl_frontend ioctl_frontend = 479{ 480 .name = "ioctl", 481}; 482 483static void 484ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 485 union ctl_ha_msg *msg_info) 486{ 487 struct ctl_scsiio *ctsio; 488 489 if (msg_info->hdr.original_sc == NULL) { 490 printf("%s: original_sc == NULL!\n", __func__); 491 /* XXX KDM now what? */ 492 return; 493 } 494 495 ctsio = &msg_info->hdr.original_sc->scsiio; 496 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 497 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 498 ctsio->io_hdr.status = msg_info->hdr.status; 499 ctsio->scsi_status = msg_info->scsi.scsi_status; 500 ctsio->sense_len = msg_info->scsi.sense_len; 501 ctsio->sense_residual = msg_info->scsi.sense_residual; 502 ctsio->residual = msg_info->scsi.residual; 503 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 504 sizeof(ctsio->sense_data)); 505 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 506 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 507 ctl_enqueue_isc((union ctl_io *)ctsio); 508} 509 510static void 511ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 512 union ctl_ha_msg *msg_info) 513{ 514 struct ctl_scsiio *ctsio; 515 516 if (msg_info->hdr.serializing_sc == NULL) { 517 printf("%s: serializing_sc == NULL!\n", __func__); 518 /* XXX KDM now what? */ 519 return; 520 } 521 522 ctsio = &msg_info->hdr.serializing_sc->scsiio; 523#if 0 524 /* 525 * Attempt to catch the situation where an I/O has 526 * been freed, and we're using it again. 527 */ 528 if (ctsio->io_hdr.io_type == 0xff) { 529 union ctl_io *tmp_io; 530 tmp_io = (union ctl_io *)ctsio; 531 printf("%s: %p use after free!\n", __func__, 532 ctsio); 533 printf("%s: type %d msg %d cdb %x iptl: " 534 "%d:%d:%d:%d tag 0x%04x " 535 "flag %#x status %x\n", 536 __func__, 537 tmp_io->io_hdr.io_type, 538 tmp_io->io_hdr.msg_type, 539 tmp_io->scsiio.cdb[0], 540 tmp_io->io_hdr.nexus.initid.id, 541 tmp_io->io_hdr.nexus.targ_port, 542 tmp_io->io_hdr.nexus.targ_target.id, 543 tmp_io->io_hdr.nexus.targ_lun, 544 (tmp_io->io_hdr.io_type == 545 CTL_IO_TASK) ? 546 tmp_io->taskio.tag_num : 547 tmp_io->scsiio.tag_num, 548 tmp_io->io_hdr.flags, 549 tmp_io->io_hdr.status); 550 } 551#endif 552 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 553 ctl_enqueue_isc((union ctl_io *)ctsio); 554} 555 556/* 557 * ISC (Inter Shelf Communication) event handler. Events from the HA 558 * subsystem come in here. 559 */ 560static void 561ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 562{ 563 struct ctl_softc *ctl_softc; 564 union ctl_io *io; 565 struct ctl_prio *presio; 566 ctl_ha_status isc_status; 567 568 ctl_softc = control_softc; 569 io = NULL; 570 571 572#if 0 573 printf("CTL: Isc Msg event %d\n", event); 574#endif 575 if (event == CTL_HA_EVT_MSG_RECV) { 576 union ctl_ha_msg msg_info; 577 578 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 579 sizeof(msg_info), /*wait*/ 0); 580#if 0 581 printf("CTL: msg_type %d\n", msg_info.msg_type); 582#endif 583 if (isc_status != 0) { 584 printf("Error receiving message, status = %d\n", 585 isc_status); 586 return; 587 } 588 589 switch (msg_info.hdr.msg_type) { 590 case CTL_MSG_SERIALIZE: 591#if 0 592 printf("Serialize\n"); 593#endif 594 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 595 if (io == NULL) { 596 printf("ctl_isc_event_handler: can't allocate " 597 "ctl_io!\n"); 598 /* Bad Juju */ 599 /* Need to set busy and send msg back */ 600 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 601 msg_info.hdr.status = CTL_SCSI_ERROR; 602 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 603 msg_info.scsi.sense_len = 0; 604 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 605 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 606 } 607 goto bailout; 608 } 609 ctl_zero_io(io); 610 // populate ctsio from msg_info 611 io->io_hdr.io_type = CTL_IO_SCSI; 612 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 613 io->io_hdr.original_sc = msg_info.hdr.original_sc; 614#if 0 615 printf("pOrig %x\n", (int)msg_info.original_sc); 616#endif 617 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 618 CTL_FLAG_IO_ACTIVE; 619 /* 620 * If we're in serialization-only mode, we don't 621 * want to go through full done processing. Thus 622 * the COPY flag. 623 * 624 * XXX KDM add another flag that is more specific. 625 */ 626 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 627 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 628 io->io_hdr.nexus = msg_info.hdr.nexus; 629#if 0 630 printf("targ %d, port %d, iid %d, lun %d\n", 631 io->io_hdr.nexus.targ_target.id, 632 io->io_hdr.nexus.targ_port, 633 io->io_hdr.nexus.initid.id, 634 io->io_hdr.nexus.targ_lun); 635#endif 636 io->scsiio.tag_num = msg_info.scsi.tag_num; 637 io->scsiio.tag_type = msg_info.scsi.tag_type; 638 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 639 CTL_MAX_CDBLEN); 640 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 641 const struct ctl_cmd_entry *entry; 642 643 entry = ctl_get_cmd_entry(&io->scsiio); 644 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 645 io->io_hdr.flags |= 646 entry->flags & CTL_FLAG_DATA_MASK; 647 } 648 ctl_enqueue_isc(io); 649 break; 650 651 /* Performed on the Originating SC, XFER mode only */ 652 case CTL_MSG_DATAMOVE: { 653 struct ctl_sg_entry *sgl; 654 int i, j; 655 656 io = msg_info.hdr.original_sc; 657 if (io == NULL) { 658 printf("%s: original_sc == NULL!\n", __func__); 659 /* XXX KDM do something here */ 660 break; 661 } 662 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 663 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 664 /* 665 * Keep track of this, we need to send it back over 666 * when the datamove is complete. 667 */ 668 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 669 670 if (msg_info.dt.sg_sequence == 0) { 671 /* 672 * XXX KDM we use the preallocated S/G list 673 * here, but we'll need to change this to 674 * dynamic allocation if we need larger S/G 675 * lists. 676 */ 677 if (msg_info.dt.kern_sg_entries > 678 sizeof(io->io_hdr.remote_sglist) / 679 sizeof(io->io_hdr.remote_sglist[0])) { 680 printf("%s: number of S/G entries " 681 "needed %u > allocated num %zd\n", 682 __func__, 683 msg_info.dt.kern_sg_entries, 684 sizeof(io->io_hdr.remote_sglist)/ 685 sizeof(io->io_hdr.remote_sglist[0])); 686 687 /* 688 * XXX KDM send a message back to 689 * the other side to shut down the 690 * DMA. The error will come back 691 * through via the normal channel. 692 */ 693 break; 694 } 695 sgl = io->io_hdr.remote_sglist; 696 memset(sgl, 0, 697 sizeof(io->io_hdr.remote_sglist)); 698 699 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 700 701 io->scsiio.kern_sg_entries = 702 msg_info.dt.kern_sg_entries; 703 io->scsiio.rem_sg_entries = 704 msg_info.dt.kern_sg_entries; 705 io->scsiio.kern_data_len = 706 msg_info.dt.kern_data_len; 707 io->scsiio.kern_total_len = 708 msg_info.dt.kern_total_len; 709 io->scsiio.kern_data_resid = 710 msg_info.dt.kern_data_resid; 711 io->scsiio.kern_rel_offset = 712 msg_info.dt.kern_rel_offset; 713 /* 714 * Clear out per-DMA flags. 715 */ 716 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 717 /* 718 * Add per-DMA flags that are set for this 719 * particular DMA request. 720 */ 721 io->io_hdr.flags |= msg_info.dt.flags & 722 CTL_FLAG_RDMA_MASK; 723 } else 724 sgl = (struct ctl_sg_entry *) 725 io->scsiio.kern_data_ptr; 726 727 for (i = msg_info.dt.sent_sg_entries, j = 0; 728 i < (msg_info.dt.sent_sg_entries + 729 msg_info.dt.cur_sg_entries); i++, j++) { 730 sgl[i].addr = msg_info.dt.sg_list[j].addr; 731 sgl[i].len = msg_info.dt.sg_list[j].len; 732 733#if 0 734 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 735 __func__, 736 msg_info.dt.sg_list[j].addr, 737 msg_info.dt.sg_list[j].len, 738 sgl[i].addr, sgl[i].len, j, i); 739#endif 740 } 741#if 0 742 memcpy(&sgl[msg_info.dt.sent_sg_entries], 743 msg_info.dt.sg_list, 744 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 745#endif 746 747 /* 748 * If this is the last piece of the I/O, we've got 749 * the full S/G list. Queue processing in the thread. 750 * Otherwise wait for the next piece. 751 */ 752 if (msg_info.dt.sg_last != 0) 753 ctl_enqueue_isc(io); 754 break; 755 } 756 /* Performed on the Serializing (primary) SC, XFER mode only */ 757 case CTL_MSG_DATAMOVE_DONE: { 758 if (msg_info.hdr.serializing_sc == NULL) { 759 printf("%s: serializing_sc == NULL!\n", 760 __func__); 761 /* XXX KDM now what? */ 762 break; 763 } 764 /* 765 * We grab the sense information here in case 766 * there was a failure, so we can return status 767 * back to the initiator. 768 */ 769 io = msg_info.hdr.serializing_sc; 770 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 771 io->io_hdr.status = msg_info.hdr.status; 772 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 773 io->scsiio.sense_len = msg_info.scsi.sense_len; 774 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 775 io->io_hdr.port_status = msg_info.scsi.fetd_status; 776 io->scsiio.residual = msg_info.scsi.residual; 777 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 778 sizeof(io->scsiio.sense_data)); 779 ctl_enqueue_isc(io); 780 break; 781 } 782 783 /* Preformed on Originating SC, SER_ONLY mode */ 784 case CTL_MSG_R2R: 785 io = msg_info.hdr.original_sc; 786 if (io == NULL) { 787 printf("%s: Major Bummer\n", __func__); 788 return; 789 } else { 790#if 0 791 printf("pOrig %x\n",(int) ctsio); 792#endif 793 } 794 io->io_hdr.msg_type = CTL_MSG_R2R; 795 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 796 ctl_enqueue_isc(io); 797 break; 798 799 /* 800 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 801 * mode. 802 * Performed on the Originating (i.e. secondary) SC in XFER 803 * mode 804 */ 805 case CTL_MSG_FINISH_IO: 806 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 807 ctl_isc_handler_finish_xfer(ctl_softc, 808 &msg_info); 809 else 810 ctl_isc_handler_finish_ser_only(ctl_softc, 811 &msg_info); 812 break; 813 814 /* Preformed on Originating SC */ 815 case CTL_MSG_BAD_JUJU: 816 io = msg_info.hdr.original_sc; 817 if (io == NULL) { 818 printf("%s: Bad JUJU!, original_sc is NULL!\n", 819 __func__); 820 break; 821 } 822 ctl_copy_sense_data(&msg_info, io); 823 /* 824 * IO should have already been cleaned up on other 825 * SC so clear this flag so we won't send a message 826 * back to finish the IO there. 827 */ 828 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 829 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 830 831 /* io = msg_info.hdr.serializing_sc; */ 832 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 833 ctl_enqueue_isc(io); 834 break; 835 836 /* Handle resets sent from the other side */ 837 case CTL_MSG_MANAGE_TASKS: { 838 struct ctl_taskio *taskio; 839 taskio = (struct ctl_taskio *)ctl_alloc_io( 840 (void *)ctl_softc->othersc_pool); 841 if (taskio == NULL) { 842 printf("ctl_isc_event_handler: can't allocate " 843 "ctl_io!\n"); 844 /* Bad Juju */ 845 /* should I just call the proper reset func 846 here??? */ 847 goto bailout; 848 } 849 ctl_zero_io((union ctl_io *)taskio); 850 taskio->io_hdr.io_type = CTL_IO_TASK; 851 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 852 taskio->io_hdr.nexus = msg_info.hdr.nexus; 853 taskio->task_action = msg_info.task.task_action; 854 taskio->tag_num = msg_info.task.tag_num; 855 taskio->tag_type = msg_info.task.tag_type; 856#ifdef CTL_TIME_IO 857 taskio->io_hdr.start_time = time_uptime; 858 getbintime(&taskio->io_hdr.start_bt); 859#if 0 860 cs_prof_gettime(&taskio->io_hdr.start_ticks); 861#endif 862#endif /* CTL_TIME_IO */ 863 ctl_run_task((union ctl_io *)taskio); 864 break; 865 } 866 /* Persistent Reserve action which needs attention */ 867 case CTL_MSG_PERS_ACTION: 868 presio = (struct ctl_prio *)ctl_alloc_io( 869 (void *)ctl_softc->othersc_pool); 870 if (presio == NULL) { 871 printf("ctl_isc_event_handler: can't allocate " 872 "ctl_io!\n"); 873 /* Bad Juju */ 874 /* Need to set busy and send msg back */ 875 goto bailout; 876 } 877 ctl_zero_io((union ctl_io *)presio); 878 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 879 presio->pr_msg = msg_info.pr; 880 ctl_enqueue_isc((union ctl_io *)presio); 881 break; 882 case CTL_MSG_SYNC_FE: 883 rcv_sync_msg = 1; 884 break; 885 case CTL_MSG_APS_LOCK: { 886 // It's quicker to execute this then to 887 // queue it. 888 struct ctl_lun *lun; 889 struct ctl_page_index *page_index; 890 struct copan_aps_subpage *current_sp; 891 uint32_t targ_lun; 892 893 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 894 lun = ctl_softc->ctl_luns[targ_lun]; 895 mtx_lock(&lun->lun_lock); 896 page_index = &lun->mode_pages.index[index_to_aps_page]; 897 current_sp = (struct copan_aps_subpage *) 898 (page_index->page_data + 899 (page_index->page_len * CTL_PAGE_CURRENT)); 900 901 current_sp->lock_active = msg_info.aps.lock_flag; 902 mtx_unlock(&lun->lun_lock); 903 break; 904 } 905 default: 906 printf("How did I get here?\n"); 907 } 908 } else if (event == CTL_HA_EVT_MSG_SENT) { 909 if (param != CTL_HA_STATUS_SUCCESS) { 910 printf("Bad status from ctl_ha_msg_send status %d\n", 911 param); 912 } 913 return; 914 } else if (event == CTL_HA_EVT_DISCONNECT) { 915 printf("CTL: Got a disconnect from Isc\n"); 916 return; 917 } else { 918 printf("ctl_isc_event_handler: Unknown event %d\n", event); 919 return; 920 } 921 922bailout: 923 return; 924} 925 926static void 927ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 928{ 929 struct scsi_sense_data *sense; 930 931 sense = &dest->scsiio.sense_data; 932 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 933 dest->scsiio.scsi_status = src->scsi.scsi_status; 934 dest->scsiio.sense_len = src->scsi.sense_len; 935 dest->io_hdr.status = src->hdr.status; 936} 937 938static int 939ctl_init(void) 940{ 941 struct ctl_softc *softc; 942 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 943 struct ctl_port *port; 944 uint8_t sc_id =0; 945 int i, error, retval; 946 //int isc_retval; 947 948 retval = 0; 949 ctl_pause_rtr = 0; 950 rcv_sync_msg = 0; 951 952 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 953 M_WAITOK | M_ZERO); 954 softc = control_softc; 955 956 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 957 "cam/ctl"); 958 959 softc->dev->si_drv1 = softc; 960 961 /* 962 * By default, return a "bad LUN" peripheral qualifier for unknown 963 * LUNs. The user can override this default using the tunable or 964 * sysctl. See the comment in ctl_inquiry_std() for more details. 965 */ 966 softc->inquiry_pq_no_lun = 1; 967 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 968 &softc->inquiry_pq_no_lun); 969 sysctl_ctx_init(&softc->sysctl_ctx); 970 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 971 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 972 CTLFLAG_RD, 0, "CAM Target Layer"); 973 974 if (softc->sysctl_tree == NULL) { 975 printf("%s: unable to allocate sysctl tree\n", __func__); 976 destroy_dev(softc->dev); 977 free(control_softc, M_DEVBUF); 978 control_softc = NULL; 979 return (ENOMEM); 980 } 981 982 SYSCTL_ADD_INT(&softc->sysctl_ctx, 983 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 984 "inquiry_pq_no_lun", CTLFLAG_RW, 985 &softc->inquiry_pq_no_lun, 0, 986 "Report no lun possible for invalid LUNs"); 987 988 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 989 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 990 softc->open_count = 0; 991 992 /* 993 * Default to actually sending a SYNCHRONIZE CACHE command down to 994 * the drive. 995 */ 996 softc->flags = CTL_FLAG_REAL_SYNC; 997 998 /* 999 * In Copan's HA scheme, the "master" and "slave" roles are 1000 * figured out through the slot the controller is in. Although it 1001 * is an active/active system, someone has to be in charge. 1002 */ 1003#ifdef NEEDTOPORT 1004 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1005#endif 1006 1007 if (sc_id == 0) { 1008 softc->flags |= CTL_FLAG_MASTER_SHELF; 1009 persis_offset = 0; 1010 } else 1011 persis_offset = CTL_MAX_INITIATORS; 1012 1013 /* 1014 * XXX KDM need to figure out where we want to get our target ID 1015 * and WWID. Is it different on each port? 1016 */ 1017 softc->target.id = 0; 1018 softc->target.wwid[0] = 0x12345678; 1019 softc->target.wwid[1] = 0x87654321; 1020 STAILQ_INIT(&softc->lun_list); 1021 STAILQ_INIT(&softc->pending_lun_queue); 1022 STAILQ_INIT(&softc->fe_list); 1023 STAILQ_INIT(&softc->port_list); 1024 STAILQ_INIT(&softc->be_list); 1025 STAILQ_INIT(&softc->io_pools); 1026 ctl_tpc_init(softc); 1027 1028 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1029 &internal_pool)!= 0){ 1030 printf("ctl: can't allocate %d entry internal pool, " 1031 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1032 return (ENOMEM); 1033 } 1034 1035 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1036 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1037 printf("ctl: can't allocate %d entry emergency pool, " 1038 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1039 ctl_pool_free(internal_pool); 1040 return (ENOMEM); 1041 } 1042 1043 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1044 &other_pool) != 0) 1045 { 1046 printf("ctl: can't allocate %d entry other SC pool, " 1047 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1048 ctl_pool_free(internal_pool); 1049 ctl_pool_free(emergency_pool); 1050 return (ENOMEM); 1051 } 1052 1053 softc->internal_pool = internal_pool; 1054 softc->emergency_pool = emergency_pool; 1055 softc->othersc_pool = other_pool; 1056 1057 if (worker_threads <= 0) 1058 worker_threads = max(1, mp_ncpus / 4); 1059 if (worker_threads > CTL_MAX_THREADS) 1060 worker_threads = CTL_MAX_THREADS; 1061 1062 for (i = 0; i < worker_threads; i++) { 1063 struct ctl_thread *thr = &softc->threads[i]; 1064 1065 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1066 thr->ctl_softc = softc; 1067 STAILQ_INIT(&thr->incoming_queue); 1068 STAILQ_INIT(&thr->rtr_queue); 1069 STAILQ_INIT(&thr->done_queue); 1070 STAILQ_INIT(&thr->isc_queue); 1071 1072 error = kproc_kthread_add(ctl_work_thread, thr, 1073 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1074 if (error != 0) { 1075 printf("error creating CTL work thread!\n"); 1076 ctl_pool_free(internal_pool); 1077 ctl_pool_free(emergency_pool); 1078 ctl_pool_free(other_pool); 1079 return (error); 1080 } 1081 } 1082 error = kproc_kthread_add(ctl_lun_thread, softc, 1083 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1084 if (error != 0) { 1085 printf("error creating CTL lun thread!\n"); 1086 ctl_pool_free(internal_pool); 1087 ctl_pool_free(emergency_pool); 1088 ctl_pool_free(other_pool); 1089 return (error); 1090 } 1091 if (bootverbose) 1092 printf("ctl: CAM Target Layer loaded\n"); 1093 1094 /* 1095 * Initialize the ioctl front end. 1096 */ 1097 ctl_frontend_register(&ioctl_frontend); 1098 port = &softc->ioctl_info.port; 1099 port->frontend = &ioctl_frontend; 1100 sprintf(softc->ioctl_info.port_name, "ioctl"); 1101 port->port_type = CTL_PORT_IOCTL; 1102 port->num_requested_ctl_io = 100; 1103 port->port_name = softc->ioctl_info.port_name; 1104 port->port_online = ctl_ioctl_online; 1105 port->port_offline = ctl_ioctl_offline; 1106 port->onoff_arg = &softc->ioctl_info; 1107 port->lun_enable = ctl_ioctl_lun_enable; 1108 port->lun_disable = ctl_ioctl_lun_disable; 1109 port->targ_lun_arg = &softc->ioctl_info; 1110 port->fe_datamove = ctl_ioctl_datamove; 1111 port->fe_done = ctl_ioctl_done; 1112 port->max_targets = 15; 1113 port->max_target_id = 15; 1114 1115 if (ctl_port_register(&softc->ioctl_info.port, 1116 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1117 printf("ctl: ioctl front end registration failed, will " 1118 "continue anyway\n"); 1119 } 1120 1121#ifdef CTL_IO_DELAY 1122 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1123 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1124 sizeof(struct callout), CTL_TIMER_BYTES); 1125 return (EINVAL); 1126 } 1127#endif /* CTL_IO_DELAY */ 1128 1129 return (0); 1130} 1131 1132void 1133ctl_shutdown(void) 1134{ 1135 struct ctl_softc *softc; 1136 struct ctl_lun *lun, *next_lun; 1137 struct ctl_io_pool *pool; 1138 1139 softc = (struct ctl_softc *)control_softc; 1140 1141 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1142 printf("ctl: ioctl front end deregistration failed\n"); 1143 1144 mtx_lock(&softc->ctl_lock); 1145 1146 /* 1147 * Free up each LUN. 1148 */ 1149 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1150 next_lun = STAILQ_NEXT(lun, links); 1151 ctl_free_lun(lun); 1152 } 1153 1154 mtx_unlock(&softc->ctl_lock); 1155 1156 ctl_frontend_deregister(&ioctl_frontend); 1157 1158 /* 1159 * This will rip the rug out from under any FETDs or anyone else 1160 * that has a pool allocated. Since we increment our module 1161 * refcount any time someone outside the main CTL module allocates 1162 * a pool, we shouldn't have any problems here. The user won't be 1163 * able to unload the CTL module until client modules have 1164 * successfully unloaded. 1165 */ 1166 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1167 ctl_pool_free(pool); 1168 1169#if 0 1170 ctl_shutdown_thread(softc->work_thread); 1171 mtx_destroy(&softc->queue_lock); 1172#endif 1173 1174 ctl_tpc_shutdown(softc); 1175 mtx_destroy(&softc->pool_lock); 1176 mtx_destroy(&softc->ctl_lock); 1177 1178 destroy_dev(softc->dev); 1179 1180 sysctl_ctx_free(&softc->sysctl_ctx); 1181 1182 free(control_softc, M_DEVBUF); 1183 control_softc = NULL; 1184 1185 if (bootverbose) 1186 printf("ctl: CAM Target Layer unloaded\n"); 1187} 1188 1189static int 1190ctl_module_event_handler(module_t mod, int what, void *arg) 1191{ 1192 1193 switch (what) { 1194 case MOD_LOAD: 1195 return (ctl_init()); 1196 case MOD_UNLOAD: 1197 return (EBUSY); 1198 default: 1199 return (EOPNOTSUPP); 1200 } 1201} 1202 1203/* 1204 * XXX KDM should we do some access checks here? Bump a reference count to 1205 * prevent a CTL module from being unloaded while someone has it open? 1206 */ 1207static int 1208ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1209{ 1210 return (0); 1211} 1212 1213static int 1214ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1215{ 1216 return (0); 1217} 1218 1219int 1220ctl_port_enable(ctl_port_type port_type) 1221{ 1222 struct ctl_softc *softc; 1223 struct ctl_port *port; 1224 1225 if (ctl_is_single == 0) { 1226 union ctl_ha_msg msg_info; 1227 int isc_retval; 1228 1229#if 0 1230 printf("%s: HA mode, synchronizing frontend enable\n", 1231 __func__); 1232#endif 1233 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1234 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1235 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1236 printf("Sync msg send error retval %d\n", isc_retval); 1237 } 1238 if (!rcv_sync_msg) { 1239 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1240 sizeof(msg_info), 1); 1241 } 1242#if 0 1243 printf("CTL:Frontend Enable\n"); 1244 } else { 1245 printf("%s: single mode, skipping frontend synchronization\n", 1246 __func__); 1247#endif 1248 } 1249 1250 softc = control_softc; 1251 1252 STAILQ_FOREACH(port, &softc->port_list, links) { 1253 if (port_type & port->port_type) 1254 { 1255#if 0 1256 printf("port %d\n", port->targ_port); 1257#endif 1258 ctl_port_online(port); 1259 } 1260 } 1261 1262 return (0); 1263} 1264 1265int 1266ctl_port_disable(ctl_port_type port_type) 1267{ 1268 struct ctl_softc *softc; 1269 struct ctl_port *port; 1270 1271 softc = control_softc; 1272 1273 STAILQ_FOREACH(port, &softc->port_list, links) { 1274 if (port_type & port->port_type) 1275 ctl_port_offline(port); 1276 } 1277 1278 return (0); 1279} 1280 1281/* 1282 * Returns 0 for success, 1 for failure. 1283 * Currently the only failure mode is if there aren't enough entries 1284 * allocated. So, in case of a failure, look at num_entries_dropped, 1285 * reallocate and try again. 1286 */ 1287int 1288ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1289 int *num_entries_filled, int *num_entries_dropped, 1290 ctl_port_type port_type, int no_virtual) 1291{ 1292 struct ctl_softc *softc; 1293 struct ctl_port *port; 1294 int entries_dropped, entries_filled; 1295 int retval; 1296 int i; 1297 1298 softc = control_softc; 1299 1300 retval = 0; 1301 entries_filled = 0; 1302 entries_dropped = 0; 1303 1304 i = 0; 1305 mtx_lock(&softc->ctl_lock); 1306 STAILQ_FOREACH(port, &softc->port_list, links) { 1307 struct ctl_port_entry *entry; 1308 1309 if ((port->port_type & port_type) == 0) 1310 continue; 1311 1312 if ((no_virtual != 0) 1313 && (port->virtual_port != 0)) 1314 continue; 1315 1316 if (entries_filled >= num_entries_alloced) { 1317 entries_dropped++; 1318 continue; 1319 } 1320 entry = &entries[i]; 1321 1322 entry->port_type = port->port_type; 1323 strlcpy(entry->port_name, port->port_name, 1324 sizeof(entry->port_name)); 1325 entry->physical_port = port->physical_port; 1326 entry->virtual_port = port->virtual_port; 1327 entry->wwnn = port->wwnn; 1328 entry->wwpn = port->wwpn; 1329 1330 i++; 1331 entries_filled++; 1332 } 1333 1334 mtx_unlock(&softc->ctl_lock); 1335 1336 if (entries_dropped > 0) 1337 retval = 1; 1338 1339 *num_entries_dropped = entries_dropped; 1340 *num_entries_filled = entries_filled; 1341 1342 return (retval); 1343} 1344 1345static void 1346ctl_ioctl_online(void *arg) 1347{ 1348 struct ctl_ioctl_info *ioctl_info; 1349 1350 ioctl_info = (struct ctl_ioctl_info *)arg; 1351 1352 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1353} 1354 1355static void 1356ctl_ioctl_offline(void *arg) 1357{ 1358 struct ctl_ioctl_info *ioctl_info; 1359 1360 ioctl_info = (struct ctl_ioctl_info *)arg; 1361 1362 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1363} 1364 1365/* 1366 * Remove an initiator by port number and initiator ID. 1367 * Returns 0 for success, -1 for failure. 1368 */ 1369int 1370ctl_remove_initiator(struct ctl_port *port, int iid) 1371{ 1372 struct ctl_softc *softc = control_softc; 1373 1374 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1375 1376 if (iid > CTL_MAX_INIT_PER_PORT) { 1377 printf("%s: initiator ID %u > maximun %u!\n", 1378 __func__, iid, CTL_MAX_INIT_PER_PORT); 1379 return (-1); 1380 } 1381 1382 mtx_lock(&softc->ctl_lock); 1383 port->wwpn_iid[iid].in_use--; 1384 port->wwpn_iid[iid].last_use = time_uptime; 1385 mtx_unlock(&softc->ctl_lock); 1386 1387 return (0); 1388} 1389 1390/* 1391 * Add an initiator to the initiator map. 1392 * Returns iid for success, < 0 for failure. 1393 */ 1394int 1395ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1396{ 1397 struct ctl_softc *softc = control_softc; 1398 time_t best_time; 1399 int i, best; 1400 1401 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1402 1403 if (iid >= CTL_MAX_INIT_PER_PORT) { 1404 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1405 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1406 free(name, M_CTL); 1407 return (-1); 1408 } 1409 1410 mtx_lock(&softc->ctl_lock); 1411 1412 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1413 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1414 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1415 iid = i; 1416 break; 1417 } 1418 if (name != NULL && port->wwpn_iid[i].name != NULL && 1419 strcmp(name, port->wwpn_iid[i].name) == 0) { 1420 iid = i; 1421 break; 1422 } 1423 } 1424 } 1425 1426 if (iid < 0) { 1427 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1428 if (port->wwpn_iid[i].in_use == 0 && 1429 port->wwpn_iid[i].wwpn == 0 && 1430 port->wwpn_iid[i].name == NULL) { 1431 iid = i; 1432 break; 1433 } 1434 } 1435 } 1436 1437 if (iid < 0) { 1438 best = -1; 1439 best_time = INT32_MAX; 1440 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1441 if (port->wwpn_iid[i].in_use == 0) { 1442 if (port->wwpn_iid[i].last_use < best_time) { 1443 best = i; 1444 best_time = port->wwpn_iid[i].last_use; 1445 } 1446 } 1447 } 1448 iid = best; 1449 } 1450 1451 if (iid < 0) { 1452 mtx_unlock(&softc->ctl_lock); 1453 free(name, M_CTL); 1454 return (-2); 1455 } 1456 1457 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1458 /* 1459 * This is not an error yet. 1460 */ 1461 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1462#if 0 1463 printf("%s: port %d iid %u WWPN %#jx arrived" 1464 " again\n", __func__, port->targ_port, 1465 iid, (uintmax_t)wwpn); 1466#endif 1467 goto take; 1468 } 1469 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1470 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1471#if 0 1472 printf("%s: port %d iid %u name '%s' arrived" 1473 " again\n", __func__, port->targ_port, 1474 iid, name); 1475#endif 1476 goto take; 1477 } 1478 1479 /* 1480 * This is an error, but what do we do about it? The 1481 * driver is telling us we have a new WWPN for this 1482 * initiator ID, so we pretty much need to use it. 1483 */ 1484 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1485 " but WWPN %#jx '%s' is still at that address\n", 1486 __func__, port->targ_port, iid, wwpn, name, 1487 (uintmax_t)port->wwpn_iid[iid].wwpn, 1488 port->wwpn_iid[iid].name); 1489 1490 /* 1491 * XXX KDM clear have_ca and ua_pending on each LUN for 1492 * this initiator. 1493 */ 1494 } 1495take: 1496 free(port->wwpn_iid[iid].name, M_CTL); 1497 port->wwpn_iid[iid].name = name; 1498 port->wwpn_iid[iid].wwpn = wwpn; 1499 port->wwpn_iid[iid].in_use++; 1500 mtx_unlock(&softc->ctl_lock); 1501 1502 return (iid); 1503} 1504 1505static int 1506ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1507{ 1508 int len; 1509 1510 switch (port->port_type) { 1511 case CTL_PORT_FC: 1512 { 1513 struct scsi_transportid_fcp *id = 1514 (struct scsi_transportid_fcp *)buf; 1515 if (port->wwpn_iid[iid].wwpn == 0) 1516 return (0); 1517 memset(id, 0, sizeof(*id)); 1518 id->format_protocol = SCSI_PROTO_FC; 1519 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1520 return (sizeof(*id)); 1521 } 1522 case CTL_PORT_ISCSI: 1523 { 1524 struct scsi_transportid_iscsi_port *id = 1525 (struct scsi_transportid_iscsi_port *)buf; 1526 if (port->wwpn_iid[iid].name == NULL) 1527 return (0); 1528 memset(id, 0, 256); 1529 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1530 SCSI_PROTO_ISCSI; 1531 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1532 len = roundup2(min(len, 252), 4); 1533 scsi_ulto2b(len, id->additional_length); 1534 return (sizeof(*id) + len); 1535 } 1536 case CTL_PORT_SAS: 1537 { 1538 struct scsi_transportid_sas *id = 1539 (struct scsi_transportid_sas *)buf; 1540 if (port->wwpn_iid[iid].wwpn == 0) 1541 return (0); 1542 memset(id, 0, sizeof(*id)); 1543 id->format_protocol = SCSI_PROTO_SAS; 1544 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1545 return (sizeof(*id)); 1546 } 1547 default: 1548 { 1549 struct scsi_transportid_spi *id = 1550 (struct scsi_transportid_spi *)buf; 1551 memset(id, 0, sizeof(*id)); 1552 id->format_protocol = SCSI_PROTO_SPI; 1553 scsi_ulto2b(iid, id->scsi_addr); 1554 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1555 return (sizeof(*id)); 1556 } 1557 } 1558} 1559 1560static int 1561ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1562{ 1563 return (0); 1564} 1565 1566static int 1567ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1568{ 1569 return (0); 1570} 1571 1572/* 1573 * Data movement routine for the CTL ioctl frontend port. 1574 */ 1575static int 1576ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1577{ 1578 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1579 struct ctl_sg_entry ext_entry, kern_entry; 1580 int ext_sglen, ext_sg_entries, kern_sg_entries; 1581 int ext_sg_start, ext_offset; 1582 int len_to_copy, len_copied; 1583 int kern_watermark, ext_watermark; 1584 int ext_sglist_malloced; 1585 int i, j; 1586 1587 ext_sglist_malloced = 0; 1588 ext_sg_start = 0; 1589 ext_offset = 0; 1590 1591 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1592 1593 /* 1594 * If this flag is set, fake the data transfer. 1595 */ 1596 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1597 ctsio->ext_data_filled = ctsio->ext_data_len; 1598 goto bailout; 1599 } 1600 1601 /* 1602 * To simplify things here, if we have a single buffer, stick it in 1603 * a S/G entry and just make it a single entry S/G list. 1604 */ 1605 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1606 int len_seen; 1607 1608 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1609 1610 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1611 M_WAITOK); 1612 ext_sglist_malloced = 1; 1613 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1614 ext_sglen) != 0) { 1615 ctl_set_internal_failure(ctsio, 1616 /*sks_valid*/ 0, 1617 /*retry_count*/ 0); 1618 goto bailout; 1619 } 1620 ext_sg_entries = ctsio->ext_sg_entries; 1621 len_seen = 0; 1622 for (i = 0; i < ext_sg_entries; i++) { 1623 if ((len_seen + ext_sglist[i].len) >= 1624 ctsio->ext_data_filled) { 1625 ext_sg_start = i; 1626 ext_offset = ctsio->ext_data_filled - len_seen; 1627 break; 1628 } 1629 len_seen += ext_sglist[i].len; 1630 } 1631 } else { 1632 ext_sglist = &ext_entry; 1633 ext_sglist->addr = ctsio->ext_data_ptr; 1634 ext_sglist->len = ctsio->ext_data_len; 1635 ext_sg_entries = 1; 1636 ext_sg_start = 0; 1637 ext_offset = ctsio->ext_data_filled; 1638 } 1639 1640 if (ctsio->kern_sg_entries > 0) { 1641 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1642 kern_sg_entries = ctsio->kern_sg_entries; 1643 } else { 1644 kern_sglist = &kern_entry; 1645 kern_sglist->addr = ctsio->kern_data_ptr; 1646 kern_sglist->len = ctsio->kern_data_len; 1647 kern_sg_entries = 1; 1648 } 1649 1650 1651 kern_watermark = 0; 1652 ext_watermark = ext_offset; 1653 len_copied = 0; 1654 for (i = ext_sg_start, j = 0; 1655 i < ext_sg_entries && j < kern_sg_entries;) { 1656 uint8_t *ext_ptr, *kern_ptr; 1657 1658 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1659 kern_sglist[j].len - kern_watermark); 1660 1661 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1662 ext_ptr = ext_ptr + ext_watermark; 1663 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1664 /* 1665 * XXX KDM fix this! 1666 */ 1667 panic("need to implement bus address support"); 1668#if 0 1669 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1670#endif 1671 } else 1672 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1673 kern_ptr = kern_ptr + kern_watermark; 1674 1675 kern_watermark += len_to_copy; 1676 ext_watermark += len_to_copy; 1677 1678 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1679 CTL_FLAG_DATA_IN) { 1680 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1681 "bytes to user\n", len_to_copy)); 1682 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1683 "to %p\n", kern_ptr, ext_ptr)); 1684 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1685 ctl_set_internal_failure(ctsio, 1686 /*sks_valid*/ 0, 1687 /*retry_count*/ 0); 1688 goto bailout; 1689 } 1690 } else { 1691 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1692 "bytes from user\n", len_to_copy)); 1693 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1694 "to %p\n", ext_ptr, kern_ptr)); 1695 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1696 ctl_set_internal_failure(ctsio, 1697 /*sks_valid*/ 0, 1698 /*retry_count*/0); 1699 goto bailout; 1700 } 1701 } 1702 1703 len_copied += len_to_copy; 1704 1705 if (ext_sglist[i].len == ext_watermark) { 1706 i++; 1707 ext_watermark = 0; 1708 } 1709 1710 if (kern_sglist[j].len == kern_watermark) { 1711 j++; 1712 kern_watermark = 0; 1713 } 1714 } 1715 1716 ctsio->ext_data_filled += len_copied; 1717 1718 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1719 "kern_sg_entries: %d\n", ext_sg_entries, 1720 kern_sg_entries)); 1721 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1722 "kern_data_len = %d\n", ctsio->ext_data_len, 1723 ctsio->kern_data_len)); 1724 1725 1726 /* XXX KDM set residual?? */ 1727bailout: 1728 1729 if (ext_sglist_malloced != 0) 1730 free(ext_sglist, M_CTL); 1731 1732 return (CTL_RETVAL_COMPLETE); 1733} 1734 1735/* 1736 * Serialize a command that went down the "wrong" side, and so was sent to 1737 * this controller for execution. The logic is a little different than the 1738 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1739 * sent back to the other side, but in the success case, we execute the 1740 * command on this side (XFER mode) or tell the other side to execute it 1741 * (SER_ONLY mode). 1742 */ 1743static int 1744ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1745{ 1746 struct ctl_softc *ctl_softc; 1747 union ctl_ha_msg msg_info; 1748 struct ctl_lun *lun; 1749 int retval = 0; 1750 uint32_t targ_lun; 1751 1752 ctl_softc = control_softc; 1753 1754 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1755 lun = ctl_softc->ctl_luns[targ_lun]; 1756 if (lun==NULL) 1757 { 1758 /* 1759 * Why isn't LUN defined? The other side wouldn't 1760 * send a cmd if the LUN is undefined. 1761 */ 1762 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1763 1764 /* "Logical unit not supported" */ 1765 ctl_set_sense_data(&msg_info.scsi.sense_data, 1766 lun, 1767 /*sense_format*/SSD_TYPE_NONE, 1768 /*current_error*/ 1, 1769 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1770 /*asc*/ 0x25, 1771 /*ascq*/ 0x00, 1772 SSD_ELEM_NONE); 1773 1774 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1775 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1776 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1777 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1778 msg_info.hdr.serializing_sc = NULL; 1779 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1780 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1781 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1782 } 1783 return(1); 1784 1785 } 1786 1787 mtx_lock(&lun->lun_lock); 1788 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1789 1790 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1791 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1792 ooa_links))) { 1793 case CTL_ACTION_BLOCK: 1794 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1795 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1796 blocked_links); 1797 break; 1798 case CTL_ACTION_PASS: 1799 case CTL_ACTION_SKIP: 1800 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1801 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1802 ctl_enqueue_rtr((union ctl_io *)ctsio); 1803 } else { 1804 1805 /* send msg back to other side */ 1806 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1807 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1808 msg_info.hdr.msg_type = CTL_MSG_R2R; 1809#if 0 1810 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1811#endif 1812 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1813 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1814 } 1815 } 1816 break; 1817 case CTL_ACTION_OVERLAP: 1818 /* OVERLAPPED COMMANDS ATTEMPTED */ 1819 ctl_set_sense_data(&msg_info.scsi.sense_data, 1820 lun, 1821 /*sense_format*/SSD_TYPE_NONE, 1822 /*current_error*/ 1, 1823 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1824 /*asc*/ 0x4E, 1825 /*ascq*/ 0x00, 1826 SSD_ELEM_NONE); 1827 1828 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1829 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1830 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1831 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1832 msg_info.hdr.serializing_sc = NULL; 1833 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1834#if 0 1835 printf("BAD JUJU:Major Bummer Overlap\n"); 1836#endif 1837 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1838 retval = 1; 1839 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1840 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1841 } 1842 break; 1843 case CTL_ACTION_OVERLAP_TAG: 1844 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1845 ctl_set_sense_data(&msg_info.scsi.sense_data, 1846 lun, 1847 /*sense_format*/SSD_TYPE_NONE, 1848 /*current_error*/ 1, 1849 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1850 /*asc*/ 0x4D, 1851 /*ascq*/ ctsio->tag_num & 0xff, 1852 SSD_ELEM_NONE); 1853 1854 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1855 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1856 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1857 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1858 msg_info.hdr.serializing_sc = NULL; 1859 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1860#if 0 1861 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1862#endif 1863 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1864 retval = 1; 1865 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1866 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1867 } 1868 break; 1869 case CTL_ACTION_ERROR: 1870 default: 1871 /* "Internal target failure" */ 1872 ctl_set_sense_data(&msg_info.scsi.sense_data, 1873 lun, 1874 /*sense_format*/SSD_TYPE_NONE, 1875 /*current_error*/ 1, 1876 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1877 /*asc*/ 0x44, 1878 /*ascq*/ 0x00, 1879 SSD_ELEM_NONE); 1880 1881 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1882 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1883 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1884 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1885 msg_info.hdr.serializing_sc = NULL; 1886 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1887#if 0 1888 printf("BAD JUJU:Major Bummer HW Error\n"); 1889#endif 1890 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1891 retval = 1; 1892 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1893 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1894 } 1895 break; 1896 } 1897 mtx_unlock(&lun->lun_lock); 1898 return (retval); 1899} 1900 1901static int 1902ctl_ioctl_submit_wait(union ctl_io *io) 1903{ 1904 struct ctl_fe_ioctl_params params; 1905 ctl_fe_ioctl_state last_state; 1906 int done, retval; 1907 1908 retval = 0; 1909 1910 bzero(¶ms, sizeof(params)); 1911 1912 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1913 cv_init(¶ms.sem, "ctlioccv"); 1914 params.state = CTL_IOCTL_INPROG; 1915 last_state = params.state; 1916 1917 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1918 1919 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1920 1921 /* This shouldn't happen */ 1922 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1923 return (retval); 1924 1925 done = 0; 1926 1927 do { 1928 mtx_lock(¶ms.ioctl_mtx); 1929 /* 1930 * Check the state here, and don't sleep if the state has 1931 * already changed (i.e. wakeup has already occured, but we 1932 * weren't waiting yet). 1933 */ 1934 if (params.state == last_state) { 1935 /* XXX KDM cv_wait_sig instead? */ 1936 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1937 } 1938 last_state = params.state; 1939 1940 switch (params.state) { 1941 case CTL_IOCTL_INPROG: 1942 /* Why did we wake up? */ 1943 /* XXX KDM error here? */ 1944 mtx_unlock(¶ms.ioctl_mtx); 1945 break; 1946 case CTL_IOCTL_DATAMOVE: 1947 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1948 1949 /* 1950 * change last_state back to INPROG to avoid 1951 * deadlock on subsequent data moves. 1952 */ 1953 params.state = last_state = CTL_IOCTL_INPROG; 1954 1955 mtx_unlock(¶ms.ioctl_mtx); 1956 ctl_ioctl_do_datamove(&io->scsiio); 1957 /* 1958 * Note that in some cases, most notably writes, 1959 * this will queue the I/O and call us back later. 1960 * In other cases, generally reads, this routine 1961 * will immediately call back and wake us up, 1962 * probably using our own context. 1963 */ 1964 io->scsiio.be_move_done(io); 1965 break; 1966 case CTL_IOCTL_DONE: 1967 mtx_unlock(¶ms.ioctl_mtx); 1968 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1969 done = 1; 1970 break; 1971 default: 1972 mtx_unlock(¶ms.ioctl_mtx); 1973 /* XXX KDM error here? */ 1974 break; 1975 } 1976 } while (done == 0); 1977 1978 mtx_destroy(¶ms.ioctl_mtx); 1979 cv_destroy(¶ms.sem); 1980 1981 return (CTL_RETVAL_COMPLETE); 1982} 1983 1984static void 1985ctl_ioctl_datamove(union ctl_io *io) 1986{ 1987 struct ctl_fe_ioctl_params *params; 1988 1989 params = (struct ctl_fe_ioctl_params *) 1990 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1991 1992 mtx_lock(¶ms->ioctl_mtx); 1993 params->state = CTL_IOCTL_DATAMOVE; 1994 cv_broadcast(¶ms->sem); 1995 mtx_unlock(¶ms->ioctl_mtx); 1996} 1997 1998static void 1999ctl_ioctl_done(union ctl_io *io) 2000{ 2001 struct ctl_fe_ioctl_params *params; 2002 2003 params = (struct ctl_fe_ioctl_params *) 2004 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2005 2006 mtx_lock(¶ms->ioctl_mtx); 2007 params->state = CTL_IOCTL_DONE; 2008 cv_broadcast(¶ms->sem); 2009 mtx_unlock(¶ms->ioctl_mtx); 2010} 2011 2012static void 2013ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2014{ 2015 struct ctl_fe_ioctl_startstop_info *sd_info; 2016 2017 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2018 2019 sd_info->hs_info.status = metatask->status; 2020 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2021 sd_info->hs_info.luns_complete = 2022 metatask->taskinfo.startstop.luns_complete; 2023 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2024 2025 cv_broadcast(&sd_info->sem); 2026} 2027 2028static void 2029ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2030{ 2031 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2032 2033 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2034 2035 mtx_lock(fe_bbr_info->lock); 2036 fe_bbr_info->bbr_info->status = metatask->status; 2037 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2038 fe_bbr_info->wakeup_done = 1; 2039 mtx_unlock(fe_bbr_info->lock); 2040 2041 cv_broadcast(&fe_bbr_info->sem); 2042} 2043 2044/* 2045 * Returns 0 for success, errno for failure. 2046 */ 2047static int 2048ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2049 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2050{ 2051 union ctl_io *io; 2052 int retval; 2053 2054 retval = 0; 2055 2056 mtx_lock(&lun->lun_lock); 2057 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2058 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2059 ooa_links)) { 2060 struct ctl_ooa_entry *entry; 2061 2062 /* 2063 * If we've got more than we can fit, just count the 2064 * remaining entries. 2065 */ 2066 if (*cur_fill_num >= ooa_hdr->alloc_num) 2067 continue; 2068 2069 entry = &kern_entries[*cur_fill_num]; 2070 2071 entry->tag_num = io->scsiio.tag_num; 2072 entry->lun_num = lun->lun; 2073#ifdef CTL_TIME_IO 2074 entry->start_bt = io->io_hdr.start_bt; 2075#endif 2076 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2077 entry->cdb_len = io->scsiio.cdb_len; 2078 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2079 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2080 2081 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2082 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2083 2084 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2085 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2086 2087 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2088 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2089 2090 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2091 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2092 } 2093 mtx_unlock(&lun->lun_lock); 2094 2095 return (retval); 2096} 2097 2098static void * 2099ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2100 size_t error_str_len) 2101{ 2102 void *kptr; 2103 2104 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2105 2106 if (copyin(user_addr, kptr, len) != 0) { 2107 snprintf(error_str, error_str_len, "Error copying %d bytes " 2108 "from user address %p to kernel address %p", len, 2109 user_addr, kptr); 2110 free(kptr, M_CTL); 2111 return (NULL); 2112 } 2113 2114 return (kptr); 2115} 2116 2117static void 2118ctl_free_args(int num_args, struct ctl_be_arg *args) 2119{ 2120 int i; 2121 2122 if (args == NULL) 2123 return; 2124 2125 for (i = 0; i < num_args; i++) { 2126 free(args[i].kname, M_CTL); 2127 free(args[i].kvalue, M_CTL); 2128 } 2129 2130 free(args, M_CTL); 2131} 2132 2133static struct ctl_be_arg * 2134ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2135 char *error_str, size_t error_str_len) 2136{ 2137 struct ctl_be_arg *args; 2138 int i; 2139 2140 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2141 error_str, error_str_len); 2142 2143 if (args == NULL) 2144 goto bailout; 2145 2146 for (i = 0; i < num_args; i++) { 2147 args[i].kname = NULL; 2148 args[i].kvalue = NULL; 2149 } 2150 2151 for (i = 0; i < num_args; i++) { 2152 uint8_t *tmpptr; 2153 2154 args[i].kname = ctl_copyin_alloc(args[i].name, 2155 args[i].namelen, error_str, error_str_len); 2156 if (args[i].kname == NULL) 2157 goto bailout; 2158 2159 if (args[i].kname[args[i].namelen - 1] != '\0') { 2160 snprintf(error_str, error_str_len, "Argument %d " 2161 "name is not NUL-terminated", i); 2162 goto bailout; 2163 } 2164 2165 if (args[i].flags & CTL_BEARG_RD) { 2166 tmpptr = ctl_copyin_alloc(args[i].value, 2167 args[i].vallen, error_str, error_str_len); 2168 if (tmpptr == NULL) 2169 goto bailout; 2170 if ((args[i].flags & CTL_BEARG_ASCII) 2171 && (tmpptr[args[i].vallen - 1] != '\0')) { 2172 snprintf(error_str, error_str_len, "Argument " 2173 "%d value is not NUL-terminated", i); 2174 goto bailout; 2175 } 2176 args[i].kvalue = tmpptr; 2177 } else { 2178 args[i].kvalue = malloc(args[i].vallen, 2179 M_CTL, M_WAITOK | M_ZERO); 2180 } 2181 } 2182 2183 return (args); 2184bailout: 2185 2186 ctl_free_args(num_args, args); 2187 2188 return (NULL); 2189} 2190 2191static void 2192ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2193{ 2194 int i; 2195 2196 for (i = 0; i < num_args; i++) { 2197 if (args[i].flags & CTL_BEARG_WR) 2198 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2199 } 2200} 2201 2202/* 2203 * Escape characters that are illegal or not recommended in XML. 2204 */ 2205int 2206ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2207{ 2208 int retval; 2209 2210 retval = 0; 2211 2212 for (; *str; str++) { 2213 switch (*str) { 2214 case '&': 2215 retval = sbuf_printf(sb, "&"); 2216 break; 2217 case '>': 2218 retval = sbuf_printf(sb, ">"); 2219 break; 2220 case '<': 2221 retval = sbuf_printf(sb, "<"); 2222 break; 2223 default: 2224 retval = sbuf_putc(sb, *str); 2225 break; 2226 } 2227 2228 if (retval != 0) 2229 break; 2230 2231 } 2232 2233 return (retval); 2234} 2235 2236static int 2237ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2238 struct thread *td) 2239{ 2240 struct ctl_softc *softc; 2241 int retval; 2242 2243 softc = control_softc; 2244 2245 retval = 0; 2246 2247 switch (cmd) { 2248 case CTL_IO: { 2249 union ctl_io *io; 2250 void *pool_tmp; 2251 2252 /* 2253 * If we haven't been "enabled", don't allow any SCSI I/O 2254 * to this FETD. 2255 */ 2256 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2257 retval = EPERM; 2258 break; 2259 } 2260 2261 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2262 if (io == NULL) { 2263 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2264 retval = ENOSPC; 2265 break; 2266 } 2267 2268 /* 2269 * Need to save the pool reference so it doesn't get 2270 * spammed by the user's ctl_io. 2271 */ 2272 pool_tmp = io->io_hdr.pool; 2273 2274 memcpy(io, (void *)addr, sizeof(*io)); 2275 2276 io->io_hdr.pool = pool_tmp; 2277 /* 2278 * No status yet, so make sure the status is set properly. 2279 */ 2280 io->io_hdr.status = CTL_STATUS_NONE; 2281 2282 /* 2283 * The user sets the initiator ID, target and LUN IDs. 2284 */ 2285 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2286 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2287 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2288 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2289 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2290 2291 retval = ctl_ioctl_submit_wait(io); 2292 2293 if (retval != 0) { 2294 ctl_free_io(io); 2295 break; 2296 } 2297 2298 memcpy((void *)addr, io, sizeof(*io)); 2299 2300 /* return this to our pool */ 2301 ctl_free_io(io); 2302 2303 break; 2304 } 2305 case CTL_ENABLE_PORT: 2306 case CTL_DISABLE_PORT: 2307 case CTL_SET_PORT_WWNS: { 2308 struct ctl_port *port; 2309 struct ctl_port_entry *entry; 2310 2311 entry = (struct ctl_port_entry *)addr; 2312 2313 mtx_lock(&softc->ctl_lock); 2314 STAILQ_FOREACH(port, &softc->port_list, links) { 2315 int action, done; 2316 2317 action = 0; 2318 done = 0; 2319 2320 if ((entry->port_type == CTL_PORT_NONE) 2321 && (entry->targ_port == port->targ_port)) { 2322 /* 2323 * If the user only wants to enable or 2324 * disable or set WWNs on a specific port, 2325 * do the operation and we're done. 2326 */ 2327 action = 1; 2328 done = 1; 2329 } else if (entry->port_type & port->port_type) { 2330 /* 2331 * Compare the user's type mask with the 2332 * particular frontend type to see if we 2333 * have a match. 2334 */ 2335 action = 1; 2336 done = 0; 2337 2338 /* 2339 * Make sure the user isn't trying to set 2340 * WWNs on multiple ports at the same time. 2341 */ 2342 if (cmd == CTL_SET_PORT_WWNS) { 2343 printf("%s: Can't set WWNs on " 2344 "multiple ports\n", __func__); 2345 retval = EINVAL; 2346 break; 2347 } 2348 } 2349 if (action != 0) { 2350 /* 2351 * XXX KDM we have to drop the lock here, 2352 * because the online/offline operations 2353 * can potentially block. We need to 2354 * reference count the frontends so they 2355 * can't go away, 2356 */ 2357 mtx_unlock(&softc->ctl_lock); 2358 2359 if (cmd == CTL_ENABLE_PORT) { 2360 struct ctl_lun *lun; 2361 2362 STAILQ_FOREACH(lun, &softc->lun_list, 2363 links) { 2364 port->lun_enable(port->targ_lun_arg, 2365 lun->target, 2366 lun->lun); 2367 } 2368 2369 ctl_port_online(port); 2370 } else if (cmd == CTL_DISABLE_PORT) { 2371 struct ctl_lun *lun; 2372 2373 ctl_port_offline(port); 2374 2375 STAILQ_FOREACH(lun, &softc->lun_list, 2376 links) { 2377 port->lun_disable( 2378 port->targ_lun_arg, 2379 lun->target, 2380 lun->lun); 2381 } 2382 } 2383 2384 mtx_lock(&softc->ctl_lock); 2385 2386 if (cmd == CTL_SET_PORT_WWNS) 2387 ctl_port_set_wwns(port, 2388 (entry->flags & CTL_PORT_WWNN_VALID) ? 2389 1 : 0, entry->wwnn, 2390 (entry->flags & CTL_PORT_WWPN_VALID) ? 2391 1 : 0, entry->wwpn); 2392 } 2393 if (done != 0) 2394 break; 2395 } 2396 mtx_unlock(&softc->ctl_lock); 2397 break; 2398 } 2399 case CTL_GET_PORT_LIST: { 2400 struct ctl_port *port; 2401 struct ctl_port_list *list; 2402 int i; 2403 2404 list = (struct ctl_port_list *)addr; 2405 2406 if (list->alloc_len != (list->alloc_num * 2407 sizeof(struct ctl_port_entry))) { 2408 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2409 "alloc_num %u * sizeof(struct ctl_port_entry) " 2410 "%zu\n", __func__, list->alloc_len, 2411 list->alloc_num, sizeof(struct ctl_port_entry)); 2412 retval = EINVAL; 2413 break; 2414 } 2415 list->fill_len = 0; 2416 list->fill_num = 0; 2417 list->dropped_num = 0; 2418 i = 0; 2419 mtx_lock(&softc->ctl_lock); 2420 STAILQ_FOREACH(port, &softc->port_list, links) { 2421 struct ctl_port_entry entry, *list_entry; 2422 2423 if (list->fill_num >= list->alloc_num) { 2424 list->dropped_num++; 2425 continue; 2426 } 2427 2428 entry.port_type = port->port_type; 2429 strlcpy(entry.port_name, port->port_name, 2430 sizeof(entry.port_name)); 2431 entry.targ_port = port->targ_port; 2432 entry.physical_port = port->physical_port; 2433 entry.virtual_port = port->virtual_port; 2434 entry.wwnn = port->wwnn; 2435 entry.wwpn = port->wwpn; 2436 if (port->status & CTL_PORT_STATUS_ONLINE) 2437 entry.online = 1; 2438 else 2439 entry.online = 0; 2440 2441 list_entry = &list->entries[i]; 2442 2443 retval = copyout(&entry, list_entry, sizeof(entry)); 2444 if (retval != 0) { 2445 printf("%s: CTL_GET_PORT_LIST: copyout " 2446 "returned %d\n", __func__, retval); 2447 break; 2448 } 2449 i++; 2450 list->fill_num++; 2451 list->fill_len += sizeof(entry); 2452 } 2453 mtx_unlock(&softc->ctl_lock); 2454 2455 /* 2456 * If this is non-zero, we had a copyout fault, so there's 2457 * probably no point in attempting to set the status inside 2458 * the structure. 2459 */ 2460 if (retval != 0) 2461 break; 2462 2463 if (list->dropped_num > 0) 2464 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2465 else 2466 list->status = CTL_PORT_LIST_OK; 2467 break; 2468 } 2469 case CTL_DUMP_OOA: { 2470 struct ctl_lun *lun; 2471 union ctl_io *io; 2472 char printbuf[128]; 2473 struct sbuf sb; 2474 2475 mtx_lock(&softc->ctl_lock); 2476 printf("Dumping OOA queues:\n"); 2477 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2478 mtx_lock(&lun->lun_lock); 2479 for (io = (union ctl_io *)TAILQ_FIRST( 2480 &lun->ooa_queue); io != NULL; 2481 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2482 ooa_links)) { 2483 sbuf_new(&sb, printbuf, sizeof(printbuf), 2484 SBUF_FIXEDLEN); 2485 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2486 (intmax_t)lun->lun, 2487 io->scsiio.tag_num, 2488 (io->io_hdr.flags & 2489 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2490 (io->io_hdr.flags & 2491 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2492 (io->io_hdr.flags & 2493 CTL_FLAG_ABORT) ? " ABORT" : "", 2494 (io->io_hdr.flags & 2495 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2496 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2497 sbuf_finish(&sb); 2498 printf("%s\n", sbuf_data(&sb)); 2499 } 2500 mtx_unlock(&lun->lun_lock); 2501 } 2502 printf("OOA queues dump done\n"); 2503 mtx_unlock(&softc->ctl_lock); 2504 break; 2505 } 2506 case CTL_GET_OOA: { 2507 struct ctl_lun *lun; 2508 struct ctl_ooa *ooa_hdr; 2509 struct ctl_ooa_entry *entries; 2510 uint32_t cur_fill_num; 2511 2512 ooa_hdr = (struct ctl_ooa *)addr; 2513 2514 if ((ooa_hdr->alloc_len == 0) 2515 || (ooa_hdr->alloc_num == 0)) { 2516 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2517 "must be non-zero\n", __func__, 2518 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2519 retval = EINVAL; 2520 break; 2521 } 2522 2523 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2524 sizeof(struct ctl_ooa_entry))) { 2525 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2526 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2527 __func__, ooa_hdr->alloc_len, 2528 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2529 retval = EINVAL; 2530 break; 2531 } 2532 2533 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2534 if (entries == NULL) { 2535 printf("%s: could not allocate %d bytes for OOA " 2536 "dump\n", __func__, ooa_hdr->alloc_len); 2537 retval = ENOMEM; 2538 break; 2539 } 2540 2541 mtx_lock(&softc->ctl_lock); 2542 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2543 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2544 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2545 mtx_unlock(&softc->ctl_lock); 2546 free(entries, M_CTL); 2547 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2548 __func__, (uintmax_t)ooa_hdr->lun_num); 2549 retval = EINVAL; 2550 break; 2551 } 2552 2553 cur_fill_num = 0; 2554 2555 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2556 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2557 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2558 ooa_hdr, entries); 2559 if (retval != 0) 2560 break; 2561 } 2562 if (retval != 0) { 2563 mtx_unlock(&softc->ctl_lock); 2564 free(entries, M_CTL); 2565 break; 2566 } 2567 } else { 2568 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2569 2570 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2571 entries); 2572 } 2573 mtx_unlock(&softc->ctl_lock); 2574 2575 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2576 ooa_hdr->fill_len = ooa_hdr->fill_num * 2577 sizeof(struct ctl_ooa_entry); 2578 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2579 if (retval != 0) { 2580 printf("%s: error copying out %d bytes for OOA dump\n", 2581 __func__, ooa_hdr->fill_len); 2582 } 2583 2584 getbintime(&ooa_hdr->cur_bt); 2585 2586 if (cur_fill_num > ooa_hdr->alloc_num) { 2587 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2588 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2589 } else { 2590 ooa_hdr->dropped_num = 0; 2591 ooa_hdr->status = CTL_OOA_OK; 2592 } 2593 2594 free(entries, M_CTL); 2595 break; 2596 } 2597 case CTL_CHECK_OOA: { 2598 union ctl_io *io; 2599 struct ctl_lun *lun; 2600 struct ctl_ooa_info *ooa_info; 2601 2602 2603 ooa_info = (struct ctl_ooa_info *)addr; 2604 2605 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2606 ooa_info->status = CTL_OOA_INVALID_LUN; 2607 break; 2608 } 2609 mtx_lock(&softc->ctl_lock); 2610 lun = softc->ctl_luns[ooa_info->lun_id]; 2611 if (lun == NULL) { 2612 mtx_unlock(&softc->ctl_lock); 2613 ooa_info->status = CTL_OOA_INVALID_LUN; 2614 break; 2615 } 2616 mtx_lock(&lun->lun_lock); 2617 mtx_unlock(&softc->ctl_lock); 2618 ooa_info->num_entries = 0; 2619 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2620 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2621 &io->io_hdr, ooa_links)) { 2622 ooa_info->num_entries++; 2623 } 2624 mtx_unlock(&lun->lun_lock); 2625 2626 ooa_info->status = CTL_OOA_SUCCESS; 2627 2628 break; 2629 } 2630 case CTL_HARD_START: 2631 case CTL_HARD_STOP: { 2632 struct ctl_fe_ioctl_startstop_info ss_info; 2633 struct cfi_metatask *metatask; 2634 struct mtx hs_mtx; 2635 2636 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2637 2638 cv_init(&ss_info.sem, "hard start/stop cv" ); 2639 2640 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2641 if (metatask == NULL) { 2642 retval = ENOMEM; 2643 mtx_destroy(&hs_mtx); 2644 break; 2645 } 2646 2647 if (cmd == CTL_HARD_START) 2648 metatask->tasktype = CFI_TASK_STARTUP; 2649 else 2650 metatask->tasktype = CFI_TASK_SHUTDOWN; 2651 2652 metatask->callback = ctl_ioctl_hard_startstop_callback; 2653 metatask->callback_arg = &ss_info; 2654 2655 cfi_action(metatask); 2656 2657 /* Wait for the callback */ 2658 mtx_lock(&hs_mtx); 2659 cv_wait_sig(&ss_info.sem, &hs_mtx); 2660 mtx_unlock(&hs_mtx); 2661 2662 /* 2663 * All information has been copied from the metatask by the 2664 * time cv_broadcast() is called, so we free the metatask here. 2665 */ 2666 cfi_free_metatask(metatask); 2667 2668 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2669 2670 mtx_destroy(&hs_mtx); 2671 break; 2672 } 2673 case CTL_BBRREAD: { 2674 struct ctl_bbrread_info *bbr_info; 2675 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2676 struct mtx bbr_mtx; 2677 struct cfi_metatask *metatask; 2678 2679 bbr_info = (struct ctl_bbrread_info *)addr; 2680 2681 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2682 2683 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2684 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2685 2686 fe_bbr_info.bbr_info = bbr_info; 2687 fe_bbr_info.lock = &bbr_mtx; 2688 2689 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2690 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2691 2692 if (metatask == NULL) { 2693 mtx_destroy(&bbr_mtx); 2694 cv_destroy(&fe_bbr_info.sem); 2695 retval = ENOMEM; 2696 break; 2697 } 2698 metatask->tasktype = CFI_TASK_BBRREAD; 2699 metatask->callback = ctl_ioctl_bbrread_callback; 2700 metatask->callback_arg = &fe_bbr_info; 2701 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2702 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2703 metatask->taskinfo.bbrread.len = bbr_info->len; 2704 2705 cfi_action(metatask); 2706 2707 mtx_lock(&bbr_mtx); 2708 while (fe_bbr_info.wakeup_done == 0) 2709 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2710 mtx_unlock(&bbr_mtx); 2711 2712 bbr_info->status = metatask->status; 2713 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2714 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2715 memcpy(&bbr_info->sense_data, 2716 &metatask->taskinfo.bbrread.sense_data, 2717 ctl_min(sizeof(bbr_info->sense_data), 2718 sizeof(metatask->taskinfo.bbrread.sense_data))); 2719 2720 cfi_free_metatask(metatask); 2721 2722 mtx_destroy(&bbr_mtx); 2723 cv_destroy(&fe_bbr_info.sem); 2724 2725 break; 2726 } 2727 case CTL_DELAY_IO: { 2728 struct ctl_io_delay_info *delay_info; 2729#ifdef CTL_IO_DELAY 2730 struct ctl_lun *lun; 2731#endif /* CTL_IO_DELAY */ 2732 2733 delay_info = (struct ctl_io_delay_info *)addr; 2734 2735#ifdef CTL_IO_DELAY 2736 mtx_lock(&softc->ctl_lock); 2737 2738 if ((delay_info->lun_id > CTL_MAX_LUNS) 2739 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2740 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2741 } else { 2742 lun = softc->ctl_luns[delay_info->lun_id]; 2743 mtx_lock(&lun->lun_lock); 2744 2745 delay_info->status = CTL_DELAY_STATUS_OK; 2746 2747 switch (delay_info->delay_type) { 2748 case CTL_DELAY_TYPE_CONT: 2749 break; 2750 case CTL_DELAY_TYPE_ONESHOT: 2751 break; 2752 default: 2753 delay_info->status = 2754 CTL_DELAY_STATUS_INVALID_TYPE; 2755 break; 2756 } 2757 2758 switch (delay_info->delay_loc) { 2759 case CTL_DELAY_LOC_DATAMOVE: 2760 lun->delay_info.datamove_type = 2761 delay_info->delay_type; 2762 lun->delay_info.datamove_delay = 2763 delay_info->delay_secs; 2764 break; 2765 case CTL_DELAY_LOC_DONE: 2766 lun->delay_info.done_type = 2767 delay_info->delay_type; 2768 lun->delay_info.done_delay = 2769 delay_info->delay_secs; 2770 break; 2771 default: 2772 delay_info->status = 2773 CTL_DELAY_STATUS_INVALID_LOC; 2774 break; 2775 } 2776 mtx_unlock(&lun->lun_lock); 2777 } 2778 2779 mtx_unlock(&softc->ctl_lock); 2780#else 2781 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2782#endif /* CTL_IO_DELAY */ 2783 break; 2784 } 2785 case CTL_REALSYNC_SET: { 2786 int *syncstate; 2787 2788 syncstate = (int *)addr; 2789 2790 mtx_lock(&softc->ctl_lock); 2791 switch (*syncstate) { 2792 case 0: 2793 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2794 break; 2795 case 1: 2796 softc->flags |= CTL_FLAG_REAL_SYNC; 2797 break; 2798 default: 2799 retval = EINVAL; 2800 break; 2801 } 2802 mtx_unlock(&softc->ctl_lock); 2803 break; 2804 } 2805 case CTL_REALSYNC_GET: { 2806 int *syncstate; 2807 2808 syncstate = (int*)addr; 2809 2810 mtx_lock(&softc->ctl_lock); 2811 if (softc->flags & CTL_FLAG_REAL_SYNC) 2812 *syncstate = 1; 2813 else 2814 *syncstate = 0; 2815 mtx_unlock(&softc->ctl_lock); 2816 2817 break; 2818 } 2819 case CTL_SETSYNC: 2820 case CTL_GETSYNC: { 2821 struct ctl_sync_info *sync_info; 2822 struct ctl_lun *lun; 2823 2824 sync_info = (struct ctl_sync_info *)addr; 2825 2826 mtx_lock(&softc->ctl_lock); 2827 lun = softc->ctl_luns[sync_info->lun_id]; 2828 if (lun == NULL) { 2829 mtx_unlock(&softc->ctl_lock); 2830 sync_info->status = CTL_GS_SYNC_NO_LUN; 2831 } 2832 /* 2833 * Get or set the sync interval. We're not bounds checking 2834 * in the set case, hopefully the user won't do something 2835 * silly. 2836 */ 2837 mtx_lock(&lun->lun_lock); 2838 mtx_unlock(&softc->ctl_lock); 2839 if (cmd == CTL_GETSYNC) 2840 sync_info->sync_interval = lun->sync_interval; 2841 else 2842 lun->sync_interval = sync_info->sync_interval; 2843 mtx_unlock(&lun->lun_lock); 2844 2845 sync_info->status = CTL_GS_SYNC_OK; 2846 2847 break; 2848 } 2849 case CTL_GETSTATS: { 2850 struct ctl_stats *stats; 2851 struct ctl_lun *lun; 2852 int i; 2853 2854 stats = (struct ctl_stats *)addr; 2855 2856 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2857 stats->alloc_len) { 2858 stats->status = CTL_SS_NEED_MORE_SPACE; 2859 stats->num_luns = softc->num_luns; 2860 break; 2861 } 2862 /* 2863 * XXX KDM no locking here. If the LUN list changes, 2864 * things can blow up. 2865 */ 2866 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2867 i++, lun = STAILQ_NEXT(lun, links)) { 2868 retval = copyout(&lun->stats, &stats->lun_stats[i], 2869 sizeof(lun->stats)); 2870 if (retval != 0) 2871 break; 2872 } 2873 stats->num_luns = softc->num_luns; 2874 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2875 softc->num_luns; 2876 stats->status = CTL_SS_OK; 2877#ifdef CTL_TIME_IO 2878 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2879#else 2880 stats->flags = CTL_STATS_FLAG_NONE; 2881#endif 2882 getnanouptime(&stats->timestamp); 2883 break; 2884 } 2885 case CTL_ERROR_INJECT: { 2886 struct ctl_error_desc *err_desc, *new_err_desc; 2887 struct ctl_lun *lun; 2888 2889 err_desc = (struct ctl_error_desc *)addr; 2890 2891 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2892 M_WAITOK | M_ZERO); 2893 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2894 2895 mtx_lock(&softc->ctl_lock); 2896 lun = softc->ctl_luns[err_desc->lun_id]; 2897 if (lun == NULL) { 2898 mtx_unlock(&softc->ctl_lock); 2899 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2900 __func__, (uintmax_t)err_desc->lun_id); 2901 retval = EINVAL; 2902 break; 2903 } 2904 mtx_lock(&lun->lun_lock); 2905 mtx_unlock(&softc->ctl_lock); 2906 2907 /* 2908 * We could do some checking here to verify the validity 2909 * of the request, but given the complexity of error 2910 * injection requests, the checking logic would be fairly 2911 * complex. 2912 * 2913 * For now, if the request is invalid, it just won't get 2914 * executed and might get deleted. 2915 */ 2916 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2917 2918 /* 2919 * XXX KDM check to make sure the serial number is unique, 2920 * in case we somehow manage to wrap. That shouldn't 2921 * happen for a very long time, but it's the right thing to 2922 * do. 2923 */ 2924 new_err_desc->serial = lun->error_serial; 2925 err_desc->serial = lun->error_serial; 2926 lun->error_serial++; 2927 2928 mtx_unlock(&lun->lun_lock); 2929 break; 2930 } 2931 case CTL_ERROR_INJECT_DELETE: { 2932 struct ctl_error_desc *delete_desc, *desc, *desc2; 2933 struct ctl_lun *lun; 2934 int delete_done; 2935 2936 delete_desc = (struct ctl_error_desc *)addr; 2937 delete_done = 0; 2938 2939 mtx_lock(&softc->ctl_lock); 2940 lun = softc->ctl_luns[delete_desc->lun_id]; 2941 if (lun == NULL) { 2942 mtx_unlock(&softc->ctl_lock); 2943 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2944 __func__, (uintmax_t)delete_desc->lun_id); 2945 retval = EINVAL; 2946 break; 2947 } 2948 mtx_lock(&lun->lun_lock); 2949 mtx_unlock(&softc->ctl_lock); 2950 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2951 if (desc->serial != delete_desc->serial) 2952 continue; 2953 2954 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2955 links); 2956 free(desc, M_CTL); 2957 delete_done = 1; 2958 } 2959 mtx_unlock(&lun->lun_lock); 2960 if (delete_done == 0) { 2961 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2962 "error serial %ju on LUN %u\n", __func__, 2963 delete_desc->serial, delete_desc->lun_id); 2964 retval = EINVAL; 2965 break; 2966 } 2967 break; 2968 } 2969 case CTL_DUMP_STRUCTS: { 2970 int i, j, k, idx; 2971 struct ctl_port *port; 2972 struct ctl_frontend *fe; 2973 2974 mtx_lock(&softc->ctl_lock); 2975 printf("CTL Persistent Reservation information start:\n"); 2976 for (i = 0; i < CTL_MAX_LUNS; i++) { 2977 struct ctl_lun *lun; 2978 2979 lun = softc->ctl_luns[i]; 2980 2981 if ((lun == NULL) 2982 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2983 continue; 2984 2985 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2986 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2987 idx = j * CTL_MAX_INIT_PER_PORT + k; 2988 if (lun->per_res[idx].registered == 0) 2989 continue; 2990 printf(" LUN %d port %d iid %d key " 2991 "%#jx\n", i, j, k, 2992 (uintmax_t)scsi_8btou64( 2993 lun->per_res[idx].res_key.key)); 2994 } 2995 } 2996 } 2997 printf("CTL Persistent Reservation information end\n"); 2998 printf("CTL Ports:\n"); 2999 STAILQ_FOREACH(port, &softc->port_list, links) { 3000 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3001 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3002 port->frontend->name, port->port_type, 3003 port->physical_port, port->virtual_port, 3004 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3005 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3006 if (port->wwpn_iid[j].in_use == 0 && 3007 port->wwpn_iid[j].wwpn == 0 && 3008 port->wwpn_iid[j].name == NULL) 3009 continue; 3010 3011 printf(" iid %u use %d WWPN %#jx '%s'\n", 3012 j, port->wwpn_iid[j].in_use, 3013 (uintmax_t)port->wwpn_iid[j].wwpn, 3014 port->wwpn_iid[j].name); 3015 } 3016 } 3017 printf("CTL Port information end\n"); 3018 mtx_unlock(&softc->ctl_lock); 3019 /* 3020 * XXX KDM calling this without a lock. We'd likely want 3021 * to drop the lock before calling the frontend's dump 3022 * routine anyway. 3023 */ 3024 printf("CTL Frontends:\n"); 3025 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3026 printf(" Frontend '%s'\n", fe->name); 3027 if (fe->fe_dump != NULL) 3028 fe->fe_dump(); 3029 } 3030 printf("CTL Frontend information end\n"); 3031 break; 3032 } 3033 case CTL_LUN_REQ: { 3034 struct ctl_lun_req *lun_req; 3035 struct ctl_backend_driver *backend; 3036 3037 lun_req = (struct ctl_lun_req *)addr; 3038 3039 backend = ctl_backend_find(lun_req->backend); 3040 if (backend == NULL) { 3041 lun_req->status = CTL_LUN_ERROR; 3042 snprintf(lun_req->error_str, 3043 sizeof(lun_req->error_str), 3044 "Backend \"%s\" not found.", 3045 lun_req->backend); 3046 break; 3047 } 3048 if (lun_req->num_be_args > 0) { 3049 lun_req->kern_be_args = ctl_copyin_args( 3050 lun_req->num_be_args, 3051 lun_req->be_args, 3052 lun_req->error_str, 3053 sizeof(lun_req->error_str)); 3054 if (lun_req->kern_be_args == NULL) { 3055 lun_req->status = CTL_LUN_ERROR; 3056 break; 3057 } 3058 } 3059 3060 retval = backend->ioctl(dev, cmd, addr, flag, td); 3061 3062 if (lun_req->num_be_args > 0) { 3063 ctl_copyout_args(lun_req->num_be_args, 3064 lun_req->kern_be_args); 3065 ctl_free_args(lun_req->num_be_args, 3066 lun_req->kern_be_args); 3067 } 3068 break; 3069 } 3070 case CTL_LUN_LIST: { 3071 struct sbuf *sb; 3072 struct ctl_lun *lun; 3073 struct ctl_lun_list *list; 3074 struct ctl_option *opt; 3075 3076 list = (struct ctl_lun_list *)addr; 3077 3078 /* 3079 * Allocate a fixed length sbuf here, based on the length 3080 * of the user's buffer. We could allocate an auto-extending 3081 * buffer, and then tell the user how much larger our 3082 * amount of data is than his buffer, but that presents 3083 * some problems: 3084 * 3085 * 1. The sbuf(9) routines use a blocking malloc, and so 3086 * we can't hold a lock while calling them with an 3087 * auto-extending buffer. 3088 * 3089 * 2. There is not currently a LUN reference counting 3090 * mechanism, outside of outstanding transactions on 3091 * the LUN's OOA queue. So a LUN could go away on us 3092 * while we're getting the LUN number, backend-specific 3093 * information, etc. Thus, given the way things 3094 * currently work, we need to hold the CTL lock while 3095 * grabbing LUN information. 3096 * 3097 * So, from the user's standpoint, the best thing to do is 3098 * allocate what he thinks is a reasonable buffer length, 3099 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3100 * double the buffer length and try again. (And repeat 3101 * that until he succeeds.) 3102 */ 3103 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3104 if (sb == NULL) { 3105 list->status = CTL_LUN_LIST_ERROR; 3106 snprintf(list->error_str, sizeof(list->error_str), 3107 "Unable to allocate %d bytes for LUN list", 3108 list->alloc_len); 3109 break; 3110 } 3111 3112 sbuf_printf(sb, "<ctllunlist>\n"); 3113 3114 mtx_lock(&softc->ctl_lock); 3115 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3116 mtx_lock(&lun->lun_lock); 3117 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3118 (uintmax_t)lun->lun); 3119 3120 /* 3121 * Bail out as soon as we see that we've overfilled 3122 * the buffer. 3123 */ 3124 if (retval != 0) 3125 break; 3126 3127 retval = sbuf_printf(sb, "\t<backend_type>%s" 3128 "</backend_type>\n", 3129 (lun->backend == NULL) ? "none" : 3130 lun->backend->name); 3131 3132 if (retval != 0) 3133 break; 3134 3135 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3136 lun->be_lun->lun_type); 3137 3138 if (retval != 0) 3139 break; 3140 3141 if (lun->backend == NULL) { 3142 retval = sbuf_printf(sb, "</lun>\n"); 3143 if (retval != 0) 3144 break; 3145 continue; 3146 } 3147 3148 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3149 (lun->be_lun->maxlba > 0) ? 3150 lun->be_lun->maxlba + 1 : 0); 3151 3152 if (retval != 0) 3153 break; 3154 3155 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3156 lun->be_lun->blocksize); 3157 3158 if (retval != 0) 3159 break; 3160 3161 retval = sbuf_printf(sb, "\t<serial_number>"); 3162 3163 if (retval != 0) 3164 break; 3165 3166 retval = ctl_sbuf_printf_esc(sb, 3167 lun->be_lun->serial_num); 3168 3169 if (retval != 0) 3170 break; 3171 3172 retval = sbuf_printf(sb, "</serial_number>\n"); 3173 3174 if (retval != 0) 3175 break; 3176 3177 retval = sbuf_printf(sb, "\t<device_id>"); 3178 3179 if (retval != 0) 3180 break; 3181 3182 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3183 3184 if (retval != 0) 3185 break; 3186 3187 retval = sbuf_printf(sb, "</device_id>\n"); 3188 3189 if (retval != 0) 3190 break; 3191 3192 if (lun->backend->lun_info != NULL) { 3193 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3194 if (retval != 0) 3195 break; 3196 } 3197 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3198 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3199 opt->name, opt->value, opt->name); 3200 if (retval != 0) 3201 break; 3202 } 3203 3204 retval = sbuf_printf(sb, "</lun>\n"); 3205 3206 if (retval != 0) 3207 break; 3208 mtx_unlock(&lun->lun_lock); 3209 } 3210 if (lun != NULL) 3211 mtx_unlock(&lun->lun_lock); 3212 mtx_unlock(&softc->ctl_lock); 3213 3214 if ((retval != 0) 3215 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3216 retval = 0; 3217 sbuf_delete(sb); 3218 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3219 snprintf(list->error_str, sizeof(list->error_str), 3220 "Out of space, %d bytes is too small", 3221 list->alloc_len); 3222 break; 3223 } 3224 3225 sbuf_finish(sb); 3226 3227 retval = copyout(sbuf_data(sb), list->lun_xml, 3228 sbuf_len(sb) + 1); 3229 3230 list->fill_len = sbuf_len(sb) + 1; 3231 list->status = CTL_LUN_LIST_OK; 3232 sbuf_delete(sb); 3233 break; 3234 } 3235 case CTL_ISCSI: { 3236 struct ctl_iscsi *ci; 3237 struct ctl_frontend *fe; 3238 3239 ci = (struct ctl_iscsi *)addr; 3240 3241 fe = ctl_frontend_find("iscsi"); 3242 if (fe == NULL) { 3243 ci->status = CTL_ISCSI_ERROR; 3244 snprintf(ci->error_str, sizeof(ci->error_str), 3245 "Frontend \"iscsi\" not found."); 3246 break; 3247 } 3248 3249 retval = fe->ioctl(dev, cmd, addr, flag, td); 3250 break; 3251 } 3252 case CTL_PORT_REQ: { 3253 struct ctl_req *req; 3254 struct ctl_frontend *fe; 3255 3256 req = (struct ctl_req *)addr; 3257 3258 fe = ctl_frontend_find(req->driver); 3259 if (fe == NULL) { 3260 req->status = CTL_LUN_ERROR; 3261 snprintf(req->error_str, sizeof(req->error_str), 3262 "Frontend \"%s\" not found.", req->driver); 3263 break; 3264 } 3265 if (req->num_args > 0) { 3266 req->kern_args = ctl_copyin_args(req->num_args, 3267 req->args, req->error_str, sizeof(req->error_str)); 3268 if (req->kern_args == NULL) { 3269 req->status = CTL_LUN_ERROR; 3270 break; 3271 } 3272 } 3273 3274 retval = fe->ioctl(dev, cmd, addr, flag, td); 3275 3276 if (req->num_args > 0) { 3277 ctl_copyout_args(req->num_args, req->kern_args); 3278 ctl_free_args(req->num_args, req->kern_args); 3279 } 3280 break; 3281 } 3282 case CTL_PORT_LIST: { 3283 struct sbuf *sb; 3284 struct ctl_port *port; 3285 struct ctl_lun_list *list; 3286 struct ctl_option *opt; 3287 3288 list = (struct ctl_lun_list *)addr; 3289 3290 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3291 if (sb == NULL) { 3292 list->status = CTL_LUN_LIST_ERROR; 3293 snprintf(list->error_str, sizeof(list->error_str), 3294 "Unable to allocate %d bytes for LUN list", 3295 list->alloc_len); 3296 break; 3297 } 3298 3299 sbuf_printf(sb, "<ctlportlist>\n"); 3300 3301 mtx_lock(&softc->ctl_lock); 3302 STAILQ_FOREACH(port, &softc->port_list, links) { 3303 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3304 (uintmax_t)port->targ_port); 3305 3306 /* 3307 * Bail out as soon as we see that we've overfilled 3308 * the buffer. 3309 */ 3310 if (retval != 0) 3311 break; 3312 3313 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3314 "</frontend_type>\n", port->frontend->name); 3315 if (retval != 0) 3316 break; 3317 3318 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3319 port->port_type); 3320 if (retval != 0) 3321 break; 3322 3323 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3324 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3325 if (retval != 0) 3326 break; 3327 3328 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3329 port->port_name); 3330 if (retval != 0) 3331 break; 3332 3333 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3334 port->physical_port); 3335 if (retval != 0) 3336 break; 3337 3338 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3339 port->virtual_port); 3340 if (retval != 0) 3341 break; 3342 3343 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3344 (uintmax_t)port->wwnn); 3345 if (retval != 0) 3346 break; 3347 3348 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3349 (uintmax_t)port->wwpn); 3350 if (retval != 0) 3351 break; 3352 3353 if (port->port_info != NULL) { 3354 retval = port->port_info(port->onoff_arg, sb); 3355 if (retval != 0) 3356 break; 3357 } 3358 STAILQ_FOREACH(opt, &port->options, links) { 3359 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3360 opt->name, opt->value, opt->name); 3361 if (retval != 0) 3362 break; 3363 } 3364 3365 retval = sbuf_printf(sb, "</targ_port>\n"); 3366 if (retval != 0) 3367 break; 3368 } 3369 mtx_unlock(&softc->ctl_lock); 3370 3371 if ((retval != 0) 3372 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3373 retval = 0; 3374 sbuf_delete(sb); 3375 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3376 snprintf(list->error_str, sizeof(list->error_str), 3377 "Out of space, %d bytes is too small", 3378 list->alloc_len); 3379 break; 3380 } 3381 3382 sbuf_finish(sb); 3383 3384 retval = copyout(sbuf_data(sb), list->lun_xml, 3385 sbuf_len(sb) + 1); 3386 3387 list->fill_len = sbuf_len(sb) + 1; 3388 list->status = CTL_LUN_LIST_OK; 3389 sbuf_delete(sb); 3390 break; 3391 } 3392 default: { 3393 /* XXX KDM should we fix this? */ 3394#if 0 3395 struct ctl_backend_driver *backend; 3396 unsigned int type; 3397 int found; 3398 3399 found = 0; 3400 3401 /* 3402 * We encode the backend type as the ioctl type for backend 3403 * ioctls. So parse it out here, and then search for a 3404 * backend of this type. 3405 */ 3406 type = _IOC_TYPE(cmd); 3407 3408 STAILQ_FOREACH(backend, &softc->be_list, links) { 3409 if (backend->type == type) { 3410 found = 1; 3411 break; 3412 } 3413 } 3414 if (found == 0) { 3415 printf("ctl: unknown ioctl command %#lx or backend " 3416 "%d\n", cmd, type); 3417 retval = EINVAL; 3418 break; 3419 } 3420 retval = backend->ioctl(dev, cmd, addr, flag, td); 3421#endif 3422 retval = ENOTTY; 3423 break; 3424 } 3425 } 3426 return (retval); 3427} 3428 3429uint32_t 3430ctl_get_initindex(struct ctl_nexus *nexus) 3431{ 3432 if (nexus->targ_port < CTL_MAX_PORTS) 3433 return (nexus->initid.id + 3434 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3435 else 3436 return (nexus->initid.id + 3437 ((nexus->targ_port - CTL_MAX_PORTS) * 3438 CTL_MAX_INIT_PER_PORT)); 3439} 3440 3441uint32_t 3442ctl_get_resindex(struct ctl_nexus *nexus) 3443{ 3444 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3445} 3446 3447uint32_t 3448ctl_port_idx(int port_num) 3449{ 3450 if (port_num < CTL_MAX_PORTS) 3451 return(port_num); 3452 else 3453 return(port_num - CTL_MAX_PORTS); 3454} 3455 3456static uint32_t 3457ctl_map_lun(int port_num, uint32_t lun_id) 3458{ 3459 struct ctl_port *port; 3460 3461 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3462 if (port == NULL) 3463 return (UINT32_MAX); 3464 if (port->lun_map == NULL) 3465 return (lun_id); 3466 return (port->lun_map(port->targ_lun_arg, lun_id)); 3467} 3468 3469static uint32_t 3470ctl_map_lun_back(int port_num, uint32_t lun_id) 3471{ 3472 struct ctl_port *port; 3473 uint32_t i; 3474 3475 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3476 if (port->lun_map == NULL) 3477 return (lun_id); 3478 for (i = 0; i < CTL_MAX_LUNS; i++) { 3479 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3480 return (i); 3481 } 3482 return (UINT32_MAX); 3483} 3484 3485/* 3486 * Note: This only works for bitmask sizes that are at least 32 bits, and 3487 * that are a power of 2. 3488 */ 3489int 3490ctl_ffz(uint32_t *mask, uint32_t size) 3491{ 3492 uint32_t num_chunks, num_pieces; 3493 int i, j; 3494 3495 num_chunks = (size >> 5); 3496 if (num_chunks == 0) 3497 num_chunks++; 3498 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3499 3500 for (i = 0; i < num_chunks; i++) { 3501 for (j = 0; j < num_pieces; j++) { 3502 if ((mask[i] & (1 << j)) == 0) 3503 return ((i << 5) + j); 3504 } 3505 } 3506 3507 return (-1); 3508} 3509 3510int 3511ctl_set_mask(uint32_t *mask, uint32_t bit) 3512{ 3513 uint32_t chunk, piece; 3514 3515 chunk = bit >> 5; 3516 piece = bit % (sizeof(uint32_t) * 8); 3517 3518 if ((mask[chunk] & (1 << piece)) != 0) 3519 return (-1); 3520 else 3521 mask[chunk] |= (1 << piece); 3522 3523 return (0); 3524} 3525 3526int 3527ctl_clear_mask(uint32_t *mask, uint32_t bit) 3528{ 3529 uint32_t chunk, piece; 3530 3531 chunk = bit >> 5; 3532 piece = bit % (sizeof(uint32_t) * 8); 3533 3534 if ((mask[chunk] & (1 << piece)) == 0) 3535 return (-1); 3536 else 3537 mask[chunk] &= ~(1 << piece); 3538 3539 return (0); 3540} 3541 3542int 3543ctl_is_set(uint32_t *mask, uint32_t bit) 3544{ 3545 uint32_t chunk, piece; 3546 3547 chunk = bit >> 5; 3548 piece = bit % (sizeof(uint32_t) * 8); 3549 3550 if ((mask[chunk] & (1 << piece)) == 0) 3551 return (0); 3552 else 3553 return (1); 3554} 3555 3556#ifdef unused 3557/* 3558 * The bus, target and lun are optional, they can be filled in later. 3559 * can_wait is used to determine whether we can wait on the malloc or not. 3560 */ 3561union ctl_io* 3562ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3563 uint32_t targ_lun, int can_wait) 3564{ 3565 union ctl_io *io; 3566 3567 if (can_wait) 3568 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3569 else 3570 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3571 3572 if (io != NULL) { 3573 io->io_hdr.io_type = io_type; 3574 io->io_hdr.targ_port = targ_port; 3575 /* 3576 * XXX KDM this needs to change/go away. We need to move 3577 * to a preallocated pool of ctl_scsiio structures. 3578 */ 3579 io->io_hdr.nexus.targ_target.id = targ_target; 3580 io->io_hdr.nexus.targ_lun = targ_lun; 3581 } 3582 3583 return (io); 3584} 3585 3586void 3587ctl_kfree_io(union ctl_io *io) 3588{ 3589 free(io, M_CTL); 3590} 3591#endif /* unused */ 3592 3593/* 3594 * ctl_softc, pool_type, total_ctl_io are passed in. 3595 * npool is passed out. 3596 */ 3597int 3598ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3599 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3600{ 3601 uint32_t i; 3602 union ctl_io *cur_io, *next_io; 3603 struct ctl_io_pool *pool; 3604 int retval; 3605 3606 retval = 0; 3607 3608 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3609 M_NOWAIT | M_ZERO); 3610 if (pool == NULL) { 3611 retval = ENOMEM; 3612 goto bailout; 3613 } 3614 3615 pool->type = pool_type; 3616 pool->ctl_softc = ctl_softc; 3617 3618 mtx_lock(&ctl_softc->pool_lock); 3619 pool->id = ctl_softc->cur_pool_id++; 3620 mtx_unlock(&ctl_softc->pool_lock); 3621 3622 pool->flags = CTL_POOL_FLAG_NONE; 3623 pool->refcount = 1; /* Reference for validity. */ 3624 STAILQ_INIT(&pool->free_queue); 3625 3626 /* 3627 * XXX KDM other options here: 3628 * - allocate a page at a time 3629 * - allocate one big chunk of memory. 3630 * Page allocation might work well, but would take a little more 3631 * tracking. 3632 */ 3633 for (i = 0; i < total_ctl_io; i++) { 3634 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3635 M_NOWAIT); 3636 if (cur_io == NULL) { 3637 retval = ENOMEM; 3638 break; 3639 } 3640 cur_io->io_hdr.pool = pool; 3641 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3642 pool->total_ctl_io++; 3643 pool->free_ctl_io++; 3644 } 3645 3646 if (retval != 0) { 3647 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3648 cur_io != NULL; cur_io = next_io) { 3649 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3650 links); 3651 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3652 ctl_io_hdr, links); 3653 free(cur_io, M_CTLIO); 3654 } 3655 3656 free(pool, M_CTL); 3657 goto bailout; 3658 } 3659 mtx_lock(&ctl_softc->pool_lock); 3660 ctl_softc->num_pools++; 3661 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3662 /* 3663 * Increment our usage count if this is an external consumer, so we 3664 * can't get unloaded until the external consumer (most likely a 3665 * FETD) unloads and frees his pool. 3666 * 3667 * XXX KDM will this increment the caller's module use count, or 3668 * mine? 3669 */ 3670#if 0 3671 if ((pool_type != CTL_POOL_EMERGENCY) 3672 && (pool_type != CTL_POOL_INTERNAL) 3673 && (pool_type != CTL_POOL_4OTHERSC)) 3674 MOD_INC_USE_COUNT; 3675#endif 3676 3677 mtx_unlock(&ctl_softc->pool_lock); 3678 3679 *npool = pool; 3680 3681bailout: 3682 3683 return (retval); 3684} 3685 3686static int 3687ctl_pool_acquire(struct ctl_io_pool *pool) 3688{ 3689 3690 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3691 3692 if (pool->flags & CTL_POOL_FLAG_INVALID) 3693 return (EINVAL); 3694 3695 pool->refcount++; 3696 3697 return (0); 3698} 3699 3700static void 3701ctl_pool_release(struct ctl_io_pool *pool) 3702{ 3703 struct ctl_softc *ctl_softc = pool->ctl_softc; 3704 union ctl_io *io; 3705 3706 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3707 3708 if (--pool->refcount != 0) 3709 return; 3710 3711 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3712 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3713 links); 3714 free(io, M_CTLIO); 3715 } 3716 3717 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3718 ctl_softc->num_pools--; 3719 3720 /* 3721 * XXX KDM will this decrement the caller's usage count or mine? 3722 */ 3723#if 0 3724 if ((pool->type != CTL_POOL_EMERGENCY) 3725 && (pool->type != CTL_POOL_INTERNAL) 3726 && (pool->type != CTL_POOL_4OTHERSC)) 3727 MOD_DEC_USE_COUNT; 3728#endif 3729 3730 free(pool, M_CTL); 3731} 3732 3733void 3734ctl_pool_free(struct ctl_io_pool *pool) 3735{ 3736 struct ctl_softc *ctl_softc; 3737 3738 if (pool == NULL) 3739 return; 3740 3741 ctl_softc = pool->ctl_softc; 3742 mtx_lock(&ctl_softc->pool_lock); 3743 pool->flags |= CTL_POOL_FLAG_INVALID; 3744 ctl_pool_release(pool); 3745 mtx_unlock(&ctl_softc->pool_lock); 3746} 3747 3748/* 3749 * This routine does not block (except for spinlocks of course). 3750 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3751 * possible. 3752 */ 3753union ctl_io * 3754ctl_alloc_io(void *pool_ref) 3755{ 3756 union ctl_io *io; 3757 struct ctl_softc *ctl_softc; 3758 struct ctl_io_pool *pool, *npool; 3759 struct ctl_io_pool *emergency_pool; 3760 3761 pool = (struct ctl_io_pool *)pool_ref; 3762 3763 if (pool == NULL) { 3764 printf("%s: pool is NULL\n", __func__); 3765 return (NULL); 3766 } 3767 3768 emergency_pool = NULL; 3769 3770 ctl_softc = pool->ctl_softc; 3771 3772 mtx_lock(&ctl_softc->pool_lock); 3773 /* 3774 * First, try to get the io structure from the user's pool. 3775 */ 3776 if (ctl_pool_acquire(pool) == 0) { 3777 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3778 if (io != NULL) { 3779 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3780 pool->total_allocated++; 3781 pool->free_ctl_io--; 3782 mtx_unlock(&ctl_softc->pool_lock); 3783 return (io); 3784 } else 3785 ctl_pool_release(pool); 3786 } 3787 /* 3788 * If he doesn't have any io structures left, search for an 3789 * emergency pool and grab one from there. 3790 */ 3791 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3792 if (npool->type != CTL_POOL_EMERGENCY) 3793 continue; 3794 3795 if (ctl_pool_acquire(npool) != 0) 3796 continue; 3797 3798 emergency_pool = npool; 3799 3800 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3801 if (io != NULL) { 3802 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3803 npool->total_allocated++; 3804 npool->free_ctl_io--; 3805 mtx_unlock(&ctl_softc->pool_lock); 3806 return (io); 3807 } else 3808 ctl_pool_release(npool); 3809 } 3810 3811 /* Drop the spinlock before we malloc */ 3812 mtx_unlock(&ctl_softc->pool_lock); 3813 3814 /* 3815 * The emergency pool (if it exists) didn't have one, so try an 3816 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3817 */ 3818 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3819 if (io != NULL) { 3820 /* 3821 * If the emergency pool exists but is empty, add this 3822 * ctl_io to its list when it gets freed. 3823 */ 3824 if (emergency_pool != NULL) { 3825 mtx_lock(&ctl_softc->pool_lock); 3826 if (ctl_pool_acquire(emergency_pool) == 0) { 3827 io->io_hdr.pool = emergency_pool; 3828 emergency_pool->total_ctl_io++; 3829 /* 3830 * Need to bump this, otherwise 3831 * total_allocated and total_freed won't 3832 * match when we no longer have anything 3833 * outstanding. 3834 */ 3835 emergency_pool->total_allocated++; 3836 } 3837 mtx_unlock(&ctl_softc->pool_lock); 3838 } else 3839 io->io_hdr.pool = NULL; 3840 } 3841 3842 return (io); 3843} 3844 3845void 3846ctl_free_io(union ctl_io *io) 3847{ 3848 if (io == NULL) 3849 return; 3850 3851 /* 3852 * If this ctl_io has a pool, return it to that pool. 3853 */ 3854 if (io->io_hdr.pool != NULL) { 3855 struct ctl_io_pool *pool; 3856 3857 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3858 mtx_lock(&pool->ctl_softc->pool_lock); 3859 io->io_hdr.io_type = 0xff; 3860 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3861 pool->total_freed++; 3862 pool->free_ctl_io++; 3863 ctl_pool_release(pool); 3864 mtx_unlock(&pool->ctl_softc->pool_lock); 3865 } else { 3866 /* 3867 * Otherwise, just free it. We probably malloced it and 3868 * the emergency pool wasn't available. 3869 */ 3870 free(io, M_CTLIO); 3871 } 3872 3873} 3874 3875void 3876ctl_zero_io(union ctl_io *io) 3877{ 3878 void *pool_ref; 3879 3880 if (io == NULL) 3881 return; 3882 3883 /* 3884 * May need to preserve linked list pointers at some point too. 3885 */ 3886 pool_ref = io->io_hdr.pool; 3887 3888 memset(io, 0, sizeof(*io)); 3889 3890 io->io_hdr.pool = pool_ref; 3891} 3892 3893/* 3894 * This routine is currently used for internal copies of ctl_ios that need 3895 * to persist for some reason after we've already returned status to the 3896 * FETD. (Thus the flag set.) 3897 * 3898 * XXX XXX 3899 * Note that this makes a blind copy of all fields in the ctl_io, except 3900 * for the pool reference. This includes any memory that has been 3901 * allocated! That memory will no longer be valid after done has been 3902 * called, so this would be VERY DANGEROUS for command that actually does 3903 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3904 * start and stop commands, which don't transfer any data, so this is not a 3905 * problem. If it is used for anything else, the caller would also need to 3906 * allocate data buffer space and this routine would need to be modified to 3907 * copy the data buffer(s) as well. 3908 */ 3909void 3910ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3911{ 3912 void *pool_ref; 3913 3914 if ((src == NULL) 3915 || (dest == NULL)) 3916 return; 3917 3918 /* 3919 * May need to preserve linked list pointers at some point too. 3920 */ 3921 pool_ref = dest->io_hdr.pool; 3922 3923 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3924 3925 dest->io_hdr.pool = pool_ref; 3926 /* 3927 * We need to know that this is an internal copy, and doesn't need 3928 * to get passed back to the FETD that allocated it. 3929 */ 3930 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3931} 3932 3933#ifdef NEEDTOPORT 3934static void 3935ctl_update_power_subpage(struct copan_power_subpage *page) 3936{ 3937 int num_luns, num_partitions, config_type; 3938 struct ctl_softc *softc; 3939 cs_BOOL_t aor_present, shelf_50pct_power; 3940 cs_raidset_personality_t rs_type; 3941 int max_active_luns; 3942 3943 softc = control_softc; 3944 3945 /* subtract out the processor LUN */ 3946 num_luns = softc->num_luns - 1; 3947 /* 3948 * Default to 7 LUNs active, which was the only number we allowed 3949 * in the past. 3950 */ 3951 max_active_luns = 7; 3952 3953 num_partitions = config_GetRsPartitionInfo(); 3954 config_type = config_GetConfigType(); 3955 shelf_50pct_power = config_GetShelfPowerMode(); 3956 aor_present = config_IsAorRsPresent(); 3957 3958 rs_type = ddb_GetRsRaidType(1); 3959 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3960 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3961 EPRINT(0, "Unsupported RS type %d!", rs_type); 3962 } 3963 3964 3965 page->total_luns = num_luns; 3966 3967 switch (config_type) { 3968 case 40: 3969 /* 3970 * In a 40 drive configuration, it doesn't matter what DC 3971 * cards we have, whether we have AOR enabled or not, 3972 * partitioning or not, or what type of RAIDset we have. 3973 * In that scenario, we can power up every LUN we present 3974 * to the user. 3975 */ 3976 max_active_luns = num_luns; 3977 3978 break; 3979 case 64: 3980 if (shelf_50pct_power == CS_FALSE) { 3981 /* 25% power */ 3982 if (aor_present == CS_TRUE) { 3983 if (rs_type == 3984 CS_RAIDSET_PERSONALITY_RAID5) { 3985 max_active_luns = 7; 3986 } else if (rs_type == 3987 CS_RAIDSET_PERSONALITY_RAID1){ 3988 max_active_luns = 14; 3989 } else { 3990 /* XXX KDM now what?? */ 3991 } 3992 } else { 3993 if (rs_type == 3994 CS_RAIDSET_PERSONALITY_RAID5) { 3995 max_active_luns = 8; 3996 } else if (rs_type == 3997 CS_RAIDSET_PERSONALITY_RAID1){ 3998 max_active_luns = 16; 3999 } else { 4000 /* XXX KDM now what?? */ 4001 } 4002 } 4003 } else { 4004 /* 50% power */ 4005 /* 4006 * With 50% power in a 64 drive configuration, we 4007 * can power all LUNs we present. 4008 */ 4009 max_active_luns = num_luns; 4010 } 4011 break; 4012 case 112: 4013 if (shelf_50pct_power == CS_FALSE) { 4014 /* 25% power */ 4015 if (aor_present == CS_TRUE) { 4016 if (rs_type == 4017 CS_RAIDSET_PERSONALITY_RAID5) { 4018 max_active_luns = 7; 4019 } else if (rs_type == 4020 CS_RAIDSET_PERSONALITY_RAID1){ 4021 max_active_luns = 14; 4022 } else { 4023 /* XXX KDM now what?? */ 4024 } 4025 } else { 4026 if (rs_type == 4027 CS_RAIDSET_PERSONALITY_RAID5) { 4028 max_active_luns = 8; 4029 } else if (rs_type == 4030 CS_RAIDSET_PERSONALITY_RAID1){ 4031 max_active_luns = 16; 4032 } else { 4033 /* XXX KDM now what?? */ 4034 } 4035 } 4036 } else { 4037 /* 50% power */ 4038 if (aor_present == CS_TRUE) { 4039 if (rs_type == 4040 CS_RAIDSET_PERSONALITY_RAID5) { 4041 max_active_luns = 14; 4042 } else if (rs_type == 4043 CS_RAIDSET_PERSONALITY_RAID1){ 4044 /* 4045 * We're assuming here that disk 4046 * caching is enabled, and so we're 4047 * able to power up half of each 4048 * LUN, and cache all writes. 4049 */ 4050 max_active_luns = num_luns; 4051 } else { 4052 /* XXX KDM now what?? */ 4053 } 4054 } else { 4055 if (rs_type == 4056 CS_RAIDSET_PERSONALITY_RAID5) { 4057 max_active_luns = 15; 4058 } else if (rs_type == 4059 CS_RAIDSET_PERSONALITY_RAID1){ 4060 max_active_luns = 30; 4061 } else { 4062 /* XXX KDM now what?? */ 4063 } 4064 } 4065 } 4066 break; 4067 default: 4068 /* 4069 * In this case, we have an unknown configuration, so we 4070 * just use the default from above. 4071 */ 4072 break; 4073 } 4074 4075 page->max_active_luns = max_active_luns; 4076#if 0 4077 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4078 page->total_luns, page->max_active_luns); 4079#endif 4080} 4081#endif /* NEEDTOPORT */ 4082 4083/* 4084 * This routine could be used in the future to load default and/or saved 4085 * mode page parameters for a particuar lun. 4086 */ 4087static int 4088ctl_init_page_index(struct ctl_lun *lun) 4089{ 4090 int i; 4091 struct ctl_page_index *page_index; 4092 struct ctl_softc *softc; 4093 4094 memcpy(&lun->mode_pages.index, page_index_template, 4095 sizeof(page_index_template)); 4096 4097 softc = lun->ctl_softc; 4098 4099 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4100 4101 page_index = &lun->mode_pages.index[i]; 4102 /* 4103 * If this is a disk-only mode page, there's no point in 4104 * setting it up. For some pages, we have to have some 4105 * basic information about the disk in order to calculate the 4106 * mode page data. 4107 */ 4108 if ((lun->be_lun->lun_type != T_DIRECT) 4109 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4110 continue; 4111 4112 switch (page_index->page_code & SMPH_PC_MASK) { 4113 case SMS_FORMAT_DEVICE_PAGE: { 4114 struct scsi_format_page *format_page; 4115 4116 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4117 panic("subpage is incorrect!"); 4118 4119 /* 4120 * Sectors per track are set above. Bytes per 4121 * sector need to be set here on a per-LUN basis. 4122 */ 4123 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4124 &format_page_default, 4125 sizeof(format_page_default)); 4126 memcpy(&lun->mode_pages.format_page[ 4127 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4128 sizeof(format_page_changeable)); 4129 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4130 &format_page_default, 4131 sizeof(format_page_default)); 4132 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4133 &format_page_default, 4134 sizeof(format_page_default)); 4135 4136 format_page = &lun->mode_pages.format_page[ 4137 CTL_PAGE_CURRENT]; 4138 scsi_ulto2b(lun->be_lun->blocksize, 4139 format_page->bytes_per_sector); 4140 4141 format_page = &lun->mode_pages.format_page[ 4142 CTL_PAGE_DEFAULT]; 4143 scsi_ulto2b(lun->be_lun->blocksize, 4144 format_page->bytes_per_sector); 4145 4146 format_page = &lun->mode_pages.format_page[ 4147 CTL_PAGE_SAVED]; 4148 scsi_ulto2b(lun->be_lun->blocksize, 4149 format_page->bytes_per_sector); 4150 4151 page_index->page_data = 4152 (uint8_t *)lun->mode_pages.format_page; 4153 break; 4154 } 4155 case SMS_RIGID_DISK_PAGE: { 4156 struct scsi_rigid_disk_page *rigid_disk_page; 4157 uint32_t sectors_per_cylinder; 4158 uint64_t cylinders; 4159#ifndef __XSCALE__ 4160 int shift; 4161#endif /* !__XSCALE__ */ 4162 4163 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4164 panic("invalid subpage value %d", 4165 page_index->subpage); 4166 4167 /* 4168 * Rotation rate and sectors per track are set 4169 * above. We calculate the cylinders here based on 4170 * capacity. Due to the number of heads and 4171 * sectors per track we're using, smaller arrays 4172 * may turn out to have 0 cylinders. Linux and 4173 * FreeBSD don't pay attention to these mode pages 4174 * to figure out capacity, but Solaris does. It 4175 * seems to deal with 0 cylinders just fine, and 4176 * works out a fake geometry based on the capacity. 4177 */ 4178 memcpy(&lun->mode_pages.rigid_disk_page[ 4179 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4180 sizeof(rigid_disk_page_default)); 4181 memcpy(&lun->mode_pages.rigid_disk_page[ 4182 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4183 sizeof(rigid_disk_page_changeable)); 4184 memcpy(&lun->mode_pages.rigid_disk_page[ 4185 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4186 sizeof(rigid_disk_page_default)); 4187 memcpy(&lun->mode_pages.rigid_disk_page[ 4188 CTL_PAGE_SAVED], &rigid_disk_page_default, 4189 sizeof(rigid_disk_page_default)); 4190 4191 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4192 CTL_DEFAULT_HEADS; 4193 4194 /* 4195 * The divide method here will be more accurate, 4196 * probably, but results in floating point being 4197 * used in the kernel on i386 (__udivdi3()). On the 4198 * XScale, though, __udivdi3() is implemented in 4199 * software. 4200 * 4201 * The shift method for cylinder calculation is 4202 * accurate if sectors_per_cylinder is a power of 4203 * 2. Otherwise it might be slightly off -- you 4204 * might have a bit of a truncation problem. 4205 */ 4206#ifdef __XSCALE__ 4207 cylinders = (lun->be_lun->maxlba + 1) / 4208 sectors_per_cylinder; 4209#else 4210 for (shift = 31; shift > 0; shift--) { 4211 if (sectors_per_cylinder & (1 << shift)) 4212 break; 4213 } 4214 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4215#endif 4216 4217 /* 4218 * We've basically got 3 bytes, or 24 bits for the 4219 * cylinder size in the mode page. If we're over, 4220 * just round down to 2^24. 4221 */ 4222 if (cylinders > 0xffffff) 4223 cylinders = 0xffffff; 4224 4225 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4226 CTL_PAGE_CURRENT]; 4227 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4228 4229 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4230 CTL_PAGE_DEFAULT]; 4231 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4232 4233 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4234 CTL_PAGE_SAVED]; 4235 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4236 4237 page_index->page_data = 4238 (uint8_t *)lun->mode_pages.rigid_disk_page; 4239 break; 4240 } 4241 case SMS_CACHING_PAGE: { 4242 4243 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4244 panic("invalid subpage value %d", 4245 page_index->subpage); 4246 /* 4247 * Defaults should be okay here, no calculations 4248 * needed. 4249 */ 4250 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4251 &caching_page_default, 4252 sizeof(caching_page_default)); 4253 memcpy(&lun->mode_pages.caching_page[ 4254 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4255 sizeof(caching_page_changeable)); 4256 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4257 &caching_page_default, 4258 sizeof(caching_page_default)); 4259 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4260 &caching_page_default, 4261 sizeof(caching_page_default)); 4262 page_index->page_data = 4263 (uint8_t *)lun->mode_pages.caching_page; 4264 break; 4265 } 4266 case SMS_CONTROL_MODE_PAGE: { 4267 4268 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4269 panic("invalid subpage value %d", 4270 page_index->subpage); 4271 4272 /* 4273 * Defaults should be okay here, no calculations 4274 * needed. 4275 */ 4276 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4277 &control_page_default, 4278 sizeof(control_page_default)); 4279 memcpy(&lun->mode_pages.control_page[ 4280 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4281 sizeof(control_page_changeable)); 4282 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4283 &control_page_default, 4284 sizeof(control_page_default)); 4285 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4286 &control_page_default, 4287 sizeof(control_page_default)); 4288 page_index->page_data = 4289 (uint8_t *)lun->mode_pages.control_page; 4290 break; 4291 4292 } 4293 case SMS_VENDOR_SPECIFIC_PAGE:{ 4294 switch (page_index->subpage) { 4295 case PWR_SUBPAGE_CODE: { 4296 struct copan_power_subpage *current_page, 4297 *saved_page; 4298 4299 memcpy(&lun->mode_pages.power_subpage[ 4300 CTL_PAGE_CURRENT], 4301 &power_page_default, 4302 sizeof(power_page_default)); 4303 memcpy(&lun->mode_pages.power_subpage[ 4304 CTL_PAGE_CHANGEABLE], 4305 &power_page_changeable, 4306 sizeof(power_page_changeable)); 4307 memcpy(&lun->mode_pages.power_subpage[ 4308 CTL_PAGE_DEFAULT], 4309 &power_page_default, 4310 sizeof(power_page_default)); 4311 memcpy(&lun->mode_pages.power_subpage[ 4312 CTL_PAGE_SAVED], 4313 &power_page_default, 4314 sizeof(power_page_default)); 4315 page_index->page_data = 4316 (uint8_t *)lun->mode_pages.power_subpage; 4317 4318 current_page = (struct copan_power_subpage *) 4319 (page_index->page_data + 4320 (page_index->page_len * 4321 CTL_PAGE_CURRENT)); 4322 saved_page = (struct copan_power_subpage *) 4323 (page_index->page_data + 4324 (page_index->page_len * 4325 CTL_PAGE_SAVED)); 4326 break; 4327 } 4328 case APS_SUBPAGE_CODE: { 4329 struct copan_aps_subpage *current_page, 4330 *saved_page; 4331 4332 // This gets set multiple times but 4333 // it should always be the same. It's 4334 // only done during init so who cares. 4335 index_to_aps_page = i; 4336 4337 memcpy(&lun->mode_pages.aps_subpage[ 4338 CTL_PAGE_CURRENT], 4339 &aps_page_default, 4340 sizeof(aps_page_default)); 4341 memcpy(&lun->mode_pages.aps_subpage[ 4342 CTL_PAGE_CHANGEABLE], 4343 &aps_page_changeable, 4344 sizeof(aps_page_changeable)); 4345 memcpy(&lun->mode_pages.aps_subpage[ 4346 CTL_PAGE_DEFAULT], 4347 &aps_page_default, 4348 sizeof(aps_page_default)); 4349 memcpy(&lun->mode_pages.aps_subpage[ 4350 CTL_PAGE_SAVED], 4351 &aps_page_default, 4352 sizeof(aps_page_default)); 4353 page_index->page_data = 4354 (uint8_t *)lun->mode_pages.aps_subpage; 4355 4356 current_page = (struct copan_aps_subpage *) 4357 (page_index->page_data + 4358 (page_index->page_len * 4359 CTL_PAGE_CURRENT)); 4360 saved_page = (struct copan_aps_subpage *) 4361 (page_index->page_data + 4362 (page_index->page_len * 4363 CTL_PAGE_SAVED)); 4364 break; 4365 } 4366 case DBGCNF_SUBPAGE_CODE: { 4367 struct copan_debugconf_subpage *current_page, 4368 *saved_page; 4369 4370 memcpy(&lun->mode_pages.debugconf_subpage[ 4371 CTL_PAGE_CURRENT], 4372 &debugconf_page_default, 4373 sizeof(debugconf_page_default)); 4374 memcpy(&lun->mode_pages.debugconf_subpage[ 4375 CTL_PAGE_CHANGEABLE], 4376 &debugconf_page_changeable, 4377 sizeof(debugconf_page_changeable)); 4378 memcpy(&lun->mode_pages.debugconf_subpage[ 4379 CTL_PAGE_DEFAULT], 4380 &debugconf_page_default, 4381 sizeof(debugconf_page_default)); 4382 memcpy(&lun->mode_pages.debugconf_subpage[ 4383 CTL_PAGE_SAVED], 4384 &debugconf_page_default, 4385 sizeof(debugconf_page_default)); 4386 page_index->page_data = 4387 (uint8_t *)lun->mode_pages.debugconf_subpage; 4388 4389 current_page = (struct copan_debugconf_subpage *) 4390 (page_index->page_data + 4391 (page_index->page_len * 4392 CTL_PAGE_CURRENT)); 4393 saved_page = (struct copan_debugconf_subpage *) 4394 (page_index->page_data + 4395 (page_index->page_len * 4396 CTL_PAGE_SAVED)); 4397 break; 4398 } 4399 default: 4400 panic("invalid subpage value %d", 4401 page_index->subpage); 4402 break; 4403 } 4404 break; 4405 } 4406 default: 4407 panic("invalid page value %d", 4408 page_index->page_code & SMPH_PC_MASK); 4409 break; 4410 } 4411 } 4412 4413 return (CTL_RETVAL_COMPLETE); 4414} 4415 4416/* 4417 * LUN allocation. 4418 * 4419 * Requirements: 4420 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4421 * wants us to allocate the LUN and he can block. 4422 * - ctl_softc is always set 4423 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4424 * 4425 * Returns 0 for success, non-zero (errno) for failure. 4426 */ 4427static int 4428ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4429 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4430{ 4431 struct ctl_lun *nlun, *lun; 4432 struct ctl_port *port; 4433 struct scsi_vpd_id_descriptor *desc; 4434 struct scsi_vpd_id_t10 *t10id; 4435 const char *eui, *naa, *scsiname, *vendor; 4436 int lun_number, i, lun_malloced; 4437 int devidlen, idlen1, idlen2 = 0, len; 4438 4439 if (be_lun == NULL) 4440 return (EINVAL); 4441 4442 /* 4443 * We currently only support Direct Access or Processor LUN types. 4444 */ 4445 switch (be_lun->lun_type) { 4446 case T_DIRECT: 4447 break; 4448 case T_PROCESSOR: 4449 break; 4450 case T_SEQUENTIAL: 4451 case T_CHANGER: 4452 default: 4453 be_lun->lun_config_status(be_lun->be_lun, 4454 CTL_LUN_CONFIG_FAILURE); 4455 break; 4456 } 4457 if (ctl_lun == NULL) { 4458 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4459 lun_malloced = 1; 4460 } else { 4461 lun_malloced = 0; 4462 lun = ctl_lun; 4463 } 4464 4465 memset(lun, 0, sizeof(*lun)); 4466 if (lun_malloced) 4467 lun->flags = CTL_LUN_MALLOCED; 4468 4469 /* Generate LUN ID. */ 4470 devidlen = max(CTL_DEVID_MIN_LEN, 4471 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4472 idlen1 = sizeof(*t10id) + devidlen; 4473 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4474 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4475 if (scsiname != NULL) { 4476 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4477 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4478 } 4479 eui = ctl_get_opt(&be_lun->options, "eui"); 4480 if (eui != NULL) { 4481 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4482 } 4483 naa = ctl_get_opt(&be_lun->options, "naa"); 4484 if (naa != NULL) { 4485 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4486 } 4487 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4488 M_CTL, M_WAITOK | M_ZERO); 4489 lun->lun_devid->len = len; 4490 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4491 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4492 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4493 desc->length = idlen1; 4494 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4495 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4496 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4497 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4498 } else { 4499 strncpy(t10id->vendor, vendor, 4500 min(sizeof(t10id->vendor), strlen(vendor))); 4501 } 4502 strncpy((char *)t10id->vendor_spec_id, 4503 (char *)be_lun->device_id, devidlen); 4504 if (scsiname != NULL) { 4505 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4506 desc->length); 4507 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4508 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4509 SVPD_ID_TYPE_SCSI_NAME; 4510 desc->length = idlen2; 4511 strlcpy(desc->identifier, scsiname, idlen2); 4512 } 4513 if (eui != NULL) { 4514 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4515 desc->length); 4516 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4517 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4518 SVPD_ID_TYPE_EUI64; 4519 desc->length = 8; 4520 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4521 } 4522 if (naa != NULL) { 4523 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4524 desc->length); 4525 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4526 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4527 SVPD_ID_TYPE_NAA; 4528 desc->length = 8; 4529 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4530 } 4531 4532 mtx_lock(&ctl_softc->ctl_lock); 4533 /* 4534 * See if the caller requested a particular LUN number. If so, see 4535 * if it is available. Otherwise, allocate the first available LUN. 4536 */ 4537 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4538 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4539 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4540 mtx_unlock(&ctl_softc->ctl_lock); 4541 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4542 printf("ctl: requested LUN ID %d is higher " 4543 "than CTL_MAX_LUNS - 1 (%d)\n", 4544 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4545 } else { 4546 /* 4547 * XXX KDM return an error, or just assign 4548 * another LUN ID in this case?? 4549 */ 4550 printf("ctl: requested LUN ID %d is already " 4551 "in use\n", be_lun->req_lun_id); 4552 } 4553 if (lun->flags & CTL_LUN_MALLOCED) 4554 free(lun, M_CTL); 4555 be_lun->lun_config_status(be_lun->be_lun, 4556 CTL_LUN_CONFIG_FAILURE); 4557 return (ENOSPC); 4558 } 4559 lun_number = be_lun->req_lun_id; 4560 } else { 4561 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4562 if (lun_number == -1) { 4563 mtx_unlock(&ctl_softc->ctl_lock); 4564 printf("ctl: can't allocate LUN on target %ju, out of " 4565 "LUNs\n", (uintmax_t)target_id.id); 4566 if (lun->flags & CTL_LUN_MALLOCED) 4567 free(lun, M_CTL); 4568 be_lun->lun_config_status(be_lun->be_lun, 4569 CTL_LUN_CONFIG_FAILURE); 4570 return (ENOSPC); 4571 } 4572 } 4573 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4574 4575 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4576 lun->target = target_id; 4577 lun->lun = lun_number; 4578 lun->be_lun = be_lun; 4579 /* 4580 * The processor LUN is always enabled. Disk LUNs come on line 4581 * disabled, and must be enabled by the backend. 4582 */ 4583 lun->flags |= CTL_LUN_DISABLED; 4584 lun->backend = be_lun->be; 4585 be_lun->ctl_lun = lun; 4586 be_lun->lun_id = lun_number; 4587 atomic_add_int(&be_lun->be->num_luns, 1); 4588 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4589 lun->flags |= CTL_LUN_STOPPED; 4590 4591 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4592 lun->flags |= CTL_LUN_INOPERABLE; 4593 4594 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4595 lun->flags |= CTL_LUN_PRIMARY_SC; 4596 4597 lun->ctl_softc = ctl_softc; 4598 TAILQ_INIT(&lun->ooa_queue); 4599 TAILQ_INIT(&lun->blocked_queue); 4600 STAILQ_INIT(&lun->error_list); 4601 ctl_tpc_lun_init(lun); 4602 4603 /* 4604 * Initialize the mode page index. 4605 */ 4606 ctl_init_page_index(lun); 4607 4608 /* 4609 * Set the poweron UA for all initiators on this LUN only. 4610 */ 4611 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4612 lun->pending_ua[i] = CTL_UA_POWERON; 4613 4614 /* 4615 * Now, before we insert this lun on the lun list, set the lun 4616 * inventory changed UA for all other luns. 4617 */ 4618 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4619 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4620 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4621 } 4622 } 4623 4624 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4625 4626 ctl_softc->ctl_luns[lun_number] = lun; 4627 4628 ctl_softc->num_luns++; 4629 4630 /* Setup statistics gathering */ 4631 lun->stats.device_type = be_lun->lun_type; 4632 lun->stats.lun_number = lun_number; 4633 if (lun->stats.device_type == T_DIRECT) 4634 lun->stats.blocksize = be_lun->blocksize; 4635 else 4636 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4637 for (i = 0;i < CTL_MAX_PORTS;i++) 4638 lun->stats.ports[i].targ_port = i; 4639 4640 mtx_unlock(&ctl_softc->ctl_lock); 4641 4642 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4643 4644 /* 4645 * Run through each registered FETD and bring it online if it isn't 4646 * already. Enable the target ID if it hasn't been enabled, and 4647 * enable this particular LUN. 4648 */ 4649 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4650 int retval; 4651 4652 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4653 if (retval != 0) { 4654 printf("ctl_alloc_lun: FETD %s port %d returned error " 4655 "%d for lun_enable on target %ju lun %d\n", 4656 port->port_name, port->targ_port, retval, 4657 (uintmax_t)target_id.id, lun_number); 4658 } else 4659 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4660 } 4661 return (0); 4662} 4663 4664/* 4665 * Delete a LUN. 4666 * Assumptions: 4667 * - LUN has already been marked invalid and any pending I/O has been taken 4668 * care of. 4669 */ 4670static int 4671ctl_free_lun(struct ctl_lun *lun) 4672{ 4673 struct ctl_softc *softc; 4674#if 0 4675 struct ctl_port *port; 4676#endif 4677 struct ctl_lun *nlun; 4678 int i; 4679 4680 softc = lun->ctl_softc; 4681 4682 mtx_assert(&softc->ctl_lock, MA_OWNED); 4683 4684 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4685 4686 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4687 4688 softc->ctl_luns[lun->lun] = NULL; 4689 4690 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4691 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4692 4693 softc->num_luns--; 4694 4695 /* 4696 * XXX KDM this scheme only works for a single target/multiple LUN 4697 * setup. It needs to be revamped for a multiple target scheme. 4698 * 4699 * XXX KDM this results in port->lun_disable() getting called twice, 4700 * once when ctl_disable_lun() is called, and a second time here. 4701 * We really need to re-think the LUN disable semantics. There 4702 * should probably be several steps/levels to LUN removal: 4703 * - disable 4704 * - invalidate 4705 * - free 4706 * 4707 * Right now we only have a disable method when communicating to 4708 * the front end ports, at least for individual LUNs. 4709 */ 4710#if 0 4711 STAILQ_FOREACH(port, &softc->port_list, links) { 4712 int retval; 4713 4714 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4715 lun->lun); 4716 if (retval != 0) { 4717 printf("ctl_free_lun: FETD %s port %d returned error " 4718 "%d for lun_disable on target %ju lun %jd\n", 4719 port->port_name, port->targ_port, retval, 4720 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4721 } 4722 4723 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4724 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4725 4726 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4727 if (retval != 0) { 4728 printf("ctl_free_lun: FETD %s port %d " 4729 "returned error %d for targ_disable on " 4730 "target %ju\n", port->port_name, 4731 port->targ_port, retval, 4732 (uintmax_t)lun->target.id); 4733 } else 4734 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4735 4736 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4737 continue; 4738 4739#if 0 4740 port->port_offline(port->onoff_arg); 4741 port->status &= ~CTL_PORT_STATUS_ONLINE; 4742#endif 4743 } 4744 } 4745#endif 4746 4747 /* 4748 * Tell the backend to free resources, if this LUN has a backend. 4749 */ 4750 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4751 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4752 4753 ctl_tpc_lun_shutdown(lun); 4754 mtx_destroy(&lun->lun_lock); 4755 free(lun->lun_devid, M_CTL); 4756 if (lun->flags & CTL_LUN_MALLOCED) 4757 free(lun, M_CTL); 4758 4759 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4760 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4761 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4762 } 4763 } 4764 4765 return (0); 4766} 4767 4768static void 4769ctl_create_lun(struct ctl_be_lun *be_lun) 4770{ 4771 struct ctl_softc *ctl_softc; 4772 4773 ctl_softc = control_softc; 4774 4775 /* 4776 * ctl_alloc_lun() should handle all potential failure cases. 4777 */ 4778 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4779} 4780 4781int 4782ctl_add_lun(struct ctl_be_lun *be_lun) 4783{ 4784 struct ctl_softc *ctl_softc = control_softc; 4785 4786 mtx_lock(&ctl_softc->ctl_lock); 4787 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4788 mtx_unlock(&ctl_softc->ctl_lock); 4789 wakeup(&ctl_softc->pending_lun_queue); 4790 4791 return (0); 4792} 4793 4794int 4795ctl_enable_lun(struct ctl_be_lun *be_lun) 4796{ 4797 struct ctl_softc *ctl_softc; 4798 struct ctl_port *port, *nport; 4799 struct ctl_lun *lun; 4800 int retval; 4801 4802 ctl_softc = control_softc; 4803 4804 lun = (struct ctl_lun *)be_lun->ctl_lun; 4805 4806 mtx_lock(&ctl_softc->ctl_lock); 4807 mtx_lock(&lun->lun_lock); 4808 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4809 /* 4810 * eh? Why did we get called if the LUN is already 4811 * enabled? 4812 */ 4813 mtx_unlock(&lun->lun_lock); 4814 mtx_unlock(&ctl_softc->ctl_lock); 4815 return (0); 4816 } 4817 lun->flags &= ~CTL_LUN_DISABLED; 4818 mtx_unlock(&lun->lun_lock); 4819 4820 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4821 nport = STAILQ_NEXT(port, links); 4822 4823 /* 4824 * Drop the lock while we call the FETD's enable routine. 4825 * This can lead to a callback into CTL (at least in the 4826 * case of the internal initiator frontend. 4827 */ 4828 mtx_unlock(&ctl_softc->ctl_lock); 4829 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4830 mtx_lock(&ctl_softc->ctl_lock); 4831 if (retval != 0) { 4832 printf("%s: FETD %s port %d returned error " 4833 "%d for lun_enable on target %ju lun %jd\n", 4834 __func__, port->port_name, port->targ_port, retval, 4835 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4836 } 4837#if 0 4838 else { 4839 /* NOTE: TODO: why does lun enable affect port status? */ 4840 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4841 } 4842#endif 4843 } 4844 4845 mtx_unlock(&ctl_softc->ctl_lock); 4846 4847 return (0); 4848} 4849 4850int 4851ctl_disable_lun(struct ctl_be_lun *be_lun) 4852{ 4853 struct ctl_softc *ctl_softc; 4854 struct ctl_port *port; 4855 struct ctl_lun *lun; 4856 int retval; 4857 4858 ctl_softc = control_softc; 4859 4860 lun = (struct ctl_lun *)be_lun->ctl_lun; 4861 4862 mtx_lock(&ctl_softc->ctl_lock); 4863 mtx_lock(&lun->lun_lock); 4864 if (lun->flags & CTL_LUN_DISABLED) { 4865 mtx_unlock(&lun->lun_lock); 4866 mtx_unlock(&ctl_softc->ctl_lock); 4867 return (0); 4868 } 4869 lun->flags |= CTL_LUN_DISABLED; 4870 mtx_unlock(&lun->lun_lock); 4871 4872 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4873 mtx_unlock(&ctl_softc->ctl_lock); 4874 /* 4875 * Drop the lock before we call the frontend's disable 4876 * routine, to avoid lock order reversals. 4877 * 4878 * XXX KDM what happens if the frontend list changes while 4879 * we're traversing it? It's unlikely, but should be handled. 4880 */ 4881 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4882 lun->lun); 4883 mtx_lock(&ctl_softc->ctl_lock); 4884 if (retval != 0) { 4885 printf("ctl_alloc_lun: FETD %s port %d returned error " 4886 "%d for lun_disable on target %ju lun %jd\n", 4887 port->port_name, port->targ_port, retval, 4888 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4889 } 4890 } 4891 4892 mtx_unlock(&ctl_softc->ctl_lock); 4893 4894 return (0); 4895} 4896 4897int 4898ctl_start_lun(struct ctl_be_lun *be_lun) 4899{ 4900 struct ctl_softc *ctl_softc; 4901 struct ctl_lun *lun; 4902 4903 ctl_softc = control_softc; 4904 4905 lun = (struct ctl_lun *)be_lun->ctl_lun; 4906 4907 mtx_lock(&lun->lun_lock); 4908 lun->flags &= ~CTL_LUN_STOPPED; 4909 mtx_unlock(&lun->lun_lock); 4910 4911 return (0); 4912} 4913 4914int 4915ctl_stop_lun(struct ctl_be_lun *be_lun) 4916{ 4917 struct ctl_softc *ctl_softc; 4918 struct ctl_lun *lun; 4919 4920 ctl_softc = control_softc; 4921 4922 lun = (struct ctl_lun *)be_lun->ctl_lun; 4923 4924 mtx_lock(&lun->lun_lock); 4925 lun->flags |= CTL_LUN_STOPPED; 4926 mtx_unlock(&lun->lun_lock); 4927 4928 return (0); 4929} 4930 4931int 4932ctl_lun_offline(struct ctl_be_lun *be_lun) 4933{ 4934 struct ctl_softc *ctl_softc; 4935 struct ctl_lun *lun; 4936 4937 ctl_softc = control_softc; 4938 4939 lun = (struct ctl_lun *)be_lun->ctl_lun; 4940 4941 mtx_lock(&lun->lun_lock); 4942 lun->flags |= CTL_LUN_OFFLINE; 4943 mtx_unlock(&lun->lun_lock); 4944 4945 return (0); 4946} 4947 4948int 4949ctl_lun_online(struct ctl_be_lun *be_lun) 4950{ 4951 struct ctl_softc *ctl_softc; 4952 struct ctl_lun *lun; 4953 4954 ctl_softc = control_softc; 4955 4956 lun = (struct ctl_lun *)be_lun->ctl_lun; 4957 4958 mtx_lock(&lun->lun_lock); 4959 lun->flags &= ~CTL_LUN_OFFLINE; 4960 mtx_unlock(&lun->lun_lock); 4961 4962 return (0); 4963} 4964 4965int 4966ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4967{ 4968 struct ctl_softc *ctl_softc; 4969 struct ctl_lun *lun; 4970 4971 ctl_softc = control_softc; 4972 4973 lun = (struct ctl_lun *)be_lun->ctl_lun; 4974 4975 mtx_lock(&lun->lun_lock); 4976 4977 /* 4978 * The LUN needs to be disabled before it can be marked invalid. 4979 */ 4980 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4981 mtx_unlock(&lun->lun_lock); 4982 return (-1); 4983 } 4984 /* 4985 * Mark the LUN invalid. 4986 */ 4987 lun->flags |= CTL_LUN_INVALID; 4988 4989 /* 4990 * If there is nothing in the OOA queue, go ahead and free the LUN. 4991 * If we have something in the OOA queue, we'll free it when the 4992 * last I/O completes. 4993 */ 4994 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4995 mtx_unlock(&lun->lun_lock); 4996 mtx_lock(&ctl_softc->ctl_lock); 4997 ctl_free_lun(lun); 4998 mtx_unlock(&ctl_softc->ctl_lock); 4999 } else 5000 mtx_unlock(&lun->lun_lock); 5001 5002 return (0); 5003} 5004 5005int 5006ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5007{ 5008 struct ctl_softc *ctl_softc; 5009 struct ctl_lun *lun; 5010 5011 ctl_softc = control_softc; 5012 lun = (struct ctl_lun *)be_lun->ctl_lun; 5013 5014 mtx_lock(&lun->lun_lock); 5015 lun->flags |= CTL_LUN_INOPERABLE; 5016 mtx_unlock(&lun->lun_lock); 5017 5018 return (0); 5019} 5020 5021int 5022ctl_lun_operable(struct ctl_be_lun *be_lun) 5023{ 5024 struct ctl_softc *ctl_softc; 5025 struct ctl_lun *lun; 5026 5027 ctl_softc = control_softc; 5028 lun = (struct ctl_lun *)be_lun->ctl_lun; 5029 5030 mtx_lock(&lun->lun_lock); 5031 lun->flags &= ~CTL_LUN_INOPERABLE; 5032 mtx_unlock(&lun->lun_lock); 5033 5034 return (0); 5035} 5036 5037int 5038ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5039 int lock) 5040{ 5041 struct ctl_softc *softc; 5042 struct ctl_lun *lun; 5043 struct copan_aps_subpage *current_sp; 5044 struct ctl_page_index *page_index; 5045 int i; 5046 5047 softc = control_softc; 5048 5049 mtx_lock(&softc->ctl_lock); 5050 5051 lun = (struct ctl_lun *)be_lun->ctl_lun; 5052 mtx_lock(&lun->lun_lock); 5053 5054 page_index = NULL; 5055 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5056 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5057 APS_PAGE_CODE) 5058 continue; 5059 5060 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5061 continue; 5062 page_index = &lun->mode_pages.index[i]; 5063 } 5064 5065 if (page_index == NULL) { 5066 mtx_unlock(&lun->lun_lock); 5067 mtx_unlock(&softc->ctl_lock); 5068 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5069 (uintmax_t)lun->lun); 5070 return (1); 5071 } 5072#if 0 5073 if ((softc->aps_locked_lun != 0) 5074 && (softc->aps_locked_lun != lun->lun)) { 5075 printf("%s: attempt to lock LUN %llu when %llu is already " 5076 "locked\n"); 5077 mtx_unlock(&lun->lun_lock); 5078 mtx_unlock(&softc->ctl_lock); 5079 return (1); 5080 } 5081#endif 5082 5083 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5084 (page_index->page_len * CTL_PAGE_CURRENT)); 5085 5086 if (lock != 0) { 5087 current_sp->lock_active = APS_LOCK_ACTIVE; 5088 softc->aps_locked_lun = lun->lun; 5089 } else { 5090 current_sp->lock_active = 0; 5091 softc->aps_locked_lun = 0; 5092 } 5093 5094 5095 /* 5096 * If we're in HA mode, try to send the lock message to the other 5097 * side. 5098 */ 5099 if (ctl_is_single == 0) { 5100 int isc_retval; 5101 union ctl_ha_msg lock_msg; 5102 5103 lock_msg.hdr.nexus = *nexus; 5104 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5105 if (lock != 0) 5106 lock_msg.aps.lock_flag = 1; 5107 else 5108 lock_msg.aps.lock_flag = 0; 5109 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5110 sizeof(lock_msg), 0); 5111 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5112 printf("%s: APS (lock=%d) error returned from " 5113 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5114 mtx_unlock(&lun->lun_lock); 5115 mtx_unlock(&softc->ctl_lock); 5116 return (1); 5117 } 5118 } 5119 5120 mtx_unlock(&lun->lun_lock); 5121 mtx_unlock(&softc->ctl_lock); 5122 5123 return (0); 5124} 5125 5126void 5127ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5128{ 5129 struct ctl_lun *lun; 5130 struct ctl_softc *softc; 5131 int i; 5132 5133 softc = control_softc; 5134 5135 lun = (struct ctl_lun *)be_lun->ctl_lun; 5136 5137 mtx_lock(&lun->lun_lock); 5138 5139 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5140 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5141 5142 mtx_unlock(&lun->lun_lock); 5143} 5144 5145/* 5146 * Backend "memory move is complete" callback for requests that never 5147 * make it down to say RAIDCore's configuration code. 5148 */ 5149int 5150ctl_config_move_done(union ctl_io *io) 5151{ 5152 int retval; 5153 5154 retval = CTL_RETVAL_COMPLETE; 5155 5156 5157 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5158 /* 5159 * XXX KDM this shouldn't happen, but what if it does? 5160 */ 5161 if (io->io_hdr.io_type != CTL_IO_SCSI) 5162 panic("I/O type isn't CTL_IO_SCSI!"); 5163 5164 if ((io->io_hdr.port_status == 0) 5165 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5166 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5167 io->io_hdr.status = CTL_SUCCESS; 5168 else if ((io->io_hdr.port_status != 0) 5169 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5170 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5171 /* 5172 * For hardware error sense keys, the sense key 5173 * specific value is defined to be a retry count, 5174 * but we use it to pass back an internal FETD 5175 * error code. XXX KDM Hopefully the FETD is only 5176 * using 16 bits for an error code, since that's 5177 * all the space we have in the sks field. 5178 */ 5179 ctl_set_internal_failure(&io->scsiio, 5180 /*sks_valid*/ 1, 5181 /*retry_count*/ 5182 io->io_hdr.port_status); 5183 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5184 free(io->scsiio.kern_data_ptr, M_CTL); 5185 ctl_done(io); 5186 goto bailout; 5187 } 5188 5189 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5190 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5191 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5192 /* 5193 * XXX KDM just assuming a single pointer here, and not a 5194 * S/G list. If we start using S/G lists for config data, 5195 * we'll need to know how to clean them up here as well. 5196 */ 5197 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5198 free(io->scsiio.kern_data_ptr, M_CTL); 5199 /* Hopefully the user has already set the status... */ 5200 ctl_done(io); 5201 } else { 5202 /* 5203 * XXX KDM now we need to continue data movement. Some 5204 * options: 5205 * - call ctl_scsiio() again? We don't do this for data 5206 * writes, because for those at least we know ahead of 5207 * time where the write will go and how long it is. For 5208 * config writes, though, that information is largely 5209 * contained within the write itself, thus we need to 5210 * parse out the data again. 5211 * 5212 * - Call some other function once the data is in? 5213 */ 5214 5215 /* 5216 * XXX KDM call ctl_scsiio() again for now, and check flag 5217 * bits to see whether we're allocated or not. 5218 */ 5219 retval = ctl_scsiio(&io->scsiio); 5220 } 5221bailout: 5222 return (retval); 5223} 5224 5225/* 5226 * This gets called by a backend driver when it is done with a 5227 * data_submit method. 5228 */ 5229void 5230ctl_data_submit_done(union ctl_io *io) 5231{ 5232 /* 5233 * If the IO_CONT flag is set, we need to call the supplied 5234 * function to continue processing the I/O, instead of completing 5235 * the I/O just yet. 5236 * 5237 * If there is an error, though, we don't want to keep processing. 5238 * Instead, just send status back to the initiator. 5239 */ 5240 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5241 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5242 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5243 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5244 io->scsiio.io_cont(io); 5245 return; 5246 } 5247 ctl_done(io); 5248} 5249 5250/* 5251 * This gets called by a backend driver when it is done with a 5252 * configuration write. 5253 */ 5254void 5255ctl_config_write_done(union ctl_io *io) 5256{ 5257 /* 5258 * If the IO_CONT flag is set, we need to call the supplied 5259 * function to continue processing the I/O, instead of completing 5260 * the I/O just yet. 5261 * 5262 * If there is an error, though, we don't want to keep processing. 5263 * Instead, just send status back to the initiator. 5264 */ 5265 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5266 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5267 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5268 io->scsiio.io_cont(io); 5269 return; 5270 } 5271 /* 5272 * Since a configuration write can be done for commands that actually 5273 * have data allocated, like write buffer, and commands that have 5274 * no data, like start/stop unit, we need to check here. 5275 */ 5276 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5277 free(io->scsiio.kern_data_ptr, M_CTL); 5278 ctl_done(io); 5279} 5280 5281/* 5282 * SCSI release command. 5283 */ 5284int 5285ctl_scsi_release(struct ctl_scsiio *ctsio) 5286{ 5287 int length, longid, thirdparty_id, resv_id; 5288 struct ctl_softc *ctl_softc; 5289 struct ctl_lun *lun; 5290 5291 length = 0; 5292 resv_id = 0; 5293 5294 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5295 5296 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5297 ctl_softc = control_softc; 5298 5299 switch (ctsio->cdb[0]) { 5300 case RELEASE_10: { 5301 struct scsi_release_10 *cdb; 5302 5303 cdb = (struct scsi_release_10 *)ctsio->cdb; 5304 5305 if (cdb->byte2 & SR10_LONGID) 5306 longid = 1; 5307 else 5308 thirdparty_id = cdb->thirdparty_id; 5309 5310 resv_id = cdb->resv_id; 5311 length = scsi_2btoul(cdb->length); 5312 break; 5313 } 5314 } 5315 5316 5317 /* 5318 * XXX KDM right now, we only support LUN reservation. We don't 5319 * support 3rd party reservations, or extent reservations, which 5320 * might actually need the parameter list. If we've gotten this 5321 * far, we've got a LUN reservation. Anything else got kicked out 5322 * above. So, according to SPC, ignore the length. 5323 */ 5324 length = 0; 5325 5326 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5327 && (length > 0)) { 5328 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5329 ctsio->kern_data_len = length; 5330 ctsio->kern_total_len = length; 5331 ctsio->kern_data_resid = 0; 5332 ctsio->kern_rel_offset = 0; 5333 ctsio->kern_sg_entries = 0; 5334 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5335 ctsio->be_move_done = ctl_config_move_done; 5336 ctl_datamove((union ctl_io *)ctsio); 5337 5338 return (CTL_RETVAL_COMPLETE); 5339 } 5340 5341 if (length > 0) 5342 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5343 5344 mtx_lock(&lun->lun_lock); 5345 5346 /* 5347 * According to SPC, it is not an error for an intiator to attempt 5348 * to release a reservation on a LUN that isn't reserved, or that 5349 * is reserved by another initiator. The reservation can only be 5350 * released, though, by the initiator who made it or by one of 5351 * several reset type events. 5352 */ 5353 if (lun->flags & CTL_LUN_RESERVED) { 5354 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5355 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5356 && (ctsio->io_hdr.nexus.targ_target.id == 5357 lun->rsv_nexus.targ_target.id)) { 5358 lun->flags &= ~CTL_LUN_RESERVED; 5359 } 5360 } 5361 5362 mtx_unlock(&lun->lun_lock); 5363 5364 ctsio->scsi_status = SCSI_STATUS_OK; 5365 ctsio->io_hdr.status = CTL_SUCCESS; 5366 5367 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5368 free(ctsio->kern_data_ptr, M_CTL); 5369 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5370 } 5371 5372 ctl_done((union ctl_io *)ctsio); 5373 return (CTL_RETVAL_COMPLETE); 5374} 5375 5376int 5377ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5378{ 5379 int extent, thirdparty, longid; 5380 int resv_id, length; 5381 uint64_t thirdparty_id; 5382 struct ctl_softc *ctl_softc; 5383 struct ctl_lun *lun; 5384 5385 extent = 0; 5386 thirdparty = 0; 5387 longid = 0; 5388 resv_id = 0; 5389 length = 0; 5390 thirdparty_id = 0; 5391 5392 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5393 5394 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5395 ctl_softc = control_softc; 5396 5397 switch (ctsio->cdb[0]) { 5398 case RESERVE_10: { 5399 struct scsi_reserve_10 *cdb; 5400 5401 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5402 5403 if (cdb->byte2 & SR10_LONGID) 5404 longid = 1; 5405 else 5406 thirdparty_id = cdb->thirdparty_id; 5407 5408 resv_id = cdb->resv_id; 5409 length = scsi_2btoul(cdb->length); 5410 break; 5411 } 5412 } 5413 5414 /* 5415 * XXX KDM right now, we only support LUN reservation. We don't 5416 * support 3rd party reservations, or extent reservations, which 5417 * might actually need the parameter list. If we've gotten this 5418 * far, we've got a LUN reservation. Anything else got kicked out 5419 * above. So, according to SPC, ignore the length. 5420 */ 5421 length = 0; 5422 5423 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5424 && (length > 0)) { 5425 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5426 ctsio->kern_data_len = length; 5427 ctsio->kern_total_len = length; 5428 ctsio->kern_data_resid = 0; 5429 ctsio->kern_rel_offset = 0; 5430 ctsio->kern_sg_entries = 0; 5431 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5432 ctsio->be_move_done = ctl_config_move_done; 5433 ctl_datamove((union ctl_io *)ctsio); 5434 5435 return (CTL_RETVAL_COMPLETE); 5436 } 5437 5438 if (length > 0) 5439 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5440 5441 mtx_lock(&lun->lun_lock); 5442 if (lun->flags & CTL_LUN_RESERVED) { 5443 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5444 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5445 || (ctsio->io_hdr.nexus.targ_target.id != 5446 lun->rsv_nexus.targ_target.id)) { 5447 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5448 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5449 goto bailout; 5450 } 5451 } 5452 5453 lun->flags |= CTL_LUN_RESERVED; 5454 lun->rsv_nexus = ctsio->io_hdr.nexus; 5455 5456 ctsio->scsi_status = SCSI_STATUS_OK; 5457 ctsio->io_hdr.status = CTL_SUCCESS; 5458 5459bailout: 5460 mtx_unlock(&lun->lun_lock); 5461 5462 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5463 free(ctsio->kern_data_ptr, M_CTL); 5464 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5465 } 5466 5467 ctl_done((union ctl_io *)ctsio); 5468 return (CTL_RETVAL_COMPLETE); 5469} 5470 5471int 5472ctl_start_stop(struct ctl_scsiio *ctsio) 5473{ 5474 struct scsi_start_stop_unit *cdb; 5475 struct ctl_lun *lun; 5476 struct ctl_softc *ctl_softc; 5477 int retval; 5478 5479 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5480 5481 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5482 ctl_softc = control_softc; 5483 retval = 0; 5484 5485 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5486 5487 /* 5488 * XXX KDM 5489 * We don't support the immediate bit on a stop unit. In order to 5490 * do that, we would need to code up a way to know that a stop is 5491 * pending, and hold off any new commands until it completes, one 5492 * way or another. Then we could accept or reject those commands 5493 * depending on its status. We would almost need to do the reverse 5494 * of what we do below for an immediate start -- return the copy of 5495 * the ctl_io to the FETD with status to send to the host (and to 5496 * free the copy!) and then free the original I/O once the stop 5497 * actually completes. That way, the OOA queue mechanism can work 5498 * to block commands that shouldn't proceed. Another alternative 5499 * would be to put the copy in the queue in place of the original, 5500 * and return the original back to the caller. That could be 5501 * slightly safer.. 5502 */ 5503 if ((cdb->byte2 & SSS_IMMED) 5504 && ((cdb->how & SSS_START) == 0)) { 5505 ctl_set_invalid_field(ctsio, 5506 /*sks_valid*/ 1, 5507 /*command*/ 1, 5508 /*field*/ 1, 5509 /*bit_valid*/ 1, 5510 /*bit*/ 0); 5511 ctl_done((union ctl_io *)ctsio); 5512 return (CTL_RETVAL_COMPLETE); 5513 } 5514 5515 if ((lun->flags & CTL_LUN_PR_RESERVED) 5516 && ((cdb->how & SSS_START)==0)) { 5517 uint32_t residx; 5518 5519 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5520 if (!lun->per_res[residx].registered 5521 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5522 5523 ctl_set_reservation_conflict(ctsio); 5524 ctl_done((union ctl_io *)ctsio); 5525 return (CTL_RETVAL_COMPLETE); 5526 } 5527 } 5528 5529 /* 5530 * If there is no backend on this device, we can't start or stop 5531 * it. In theory we shouldn't get any start/stop commands in the 5532 * first place at this level if the LUN doesn't have a backend. 5533 * That should get stopped by the command decode code. 5534 */ 5535 if (lun->backend == NULL) { 5536 ctl_set_invalid_opcode(ctsio); 5537 ctl_done((union ctl_io *)ctsio); 5538 return (CTL_RETVAL_COMPLETE); 5539 } 5540 5541 /* 5542 * XXX KDM Copan-specific offline behavior. 5543 * Figure out a reasonable way to port this? 5544 */ 5545#ifdef NEEDTOPORT 5546 mtx_lock(&lun->lun_lock); 5547 5548 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5549 && (lun->flags & CTL_LUN_OFFLINE)) { 5550 /* 5551 * If the LUN is offline, and the on/offline bit isn't set, 5552 * reject the start or stop. Otherwise, let it through. 5553 */ 5554 mtx_unlock(&lun->lun_lock); 5555 ctl_set_lun_not_ready(ctsio); 5556 ctl_done((union ctl_io *)ctsio); 5557 } else { 5558 mtx_unlock(&lun->lun_lock); 5559#endif /* NEEDTOPORT */ 5560 /* 5561 * This could be a start or a stop when we're online, 5562 * or a stop/offline or start/online. A start or stop when 5563 * we're offline is covered in the case above. 5564 */ 5565 /* 5566 * In the non-immediate case, we send the request to 5567 * the backend and return status to the user when 5568 * it is done. 5569 * 5570 * In the immediate case, we allocate a new ctl_io 5571 * to hold a copy of the request, and send that to 5572 * the backend. We then set good status on the 5573 * user's request and return it immediately. 5574 */ 5575 if (cdb->byte2 & SSS_IMMED) { 5576 union ctl_io *new_io; 5577 5578 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5579 if (new_io == NULL) { 5580 ctl_set_busy(ctsio); 5581 ctl_done((union ctl_io *)ctsio); 5582 } else { 5583 ctl_copy_io((union ctl_io *)ctsio, 5584 new_io); 5585 retval = lun->backend->config_write(new_io); 5586 ctl_set_success(ctsio); 5587 ctl_done((union ctl_io *)ctsio); 5588 } 5589 } else { 5590 retval = lun->backend->config_write( 5591 (union ctl_io *)ctsio); 5592 } 5593#ifdef NEEDTOPORT 5594 } 5595#endif 5596 return (retval); 5597} 5598 5599/* 5600 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5601 * we don't really do anything with the LBA and length fields if the user 5602 * passes them in. Instead we'll just flush out the cache for the entire 5603 * LUN. 5604 */ 5605int 5606ctl_sync_cache(struct ctl_scsiio *ctsio) 5607{ 5608 struct ctl_lun *lun; 5609 struct ctl_softc *ctl_softc; 5610 uint64_t starting_lba; 5611 uint32_t block_count; 5612 int retval; 5613 5614 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5615 5616 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5617 ctl_softc = control_softc; 5618 retval = 0; 5619 5620 switch (ctsio->cdb[0]) { 5621 case SYNCHRONIZE_CACHE: { 5622 struct scsi_sync_cache *cdb; 5623 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5624 5625 starting_lba = scsi_4btoul(cdb->begin_lba); 5626 block_count = scsi_2btoul(cdb->lb_count); 5627 break; 5628 } 5629 case SYNCHRONIZE_CACHE_16: { 5630 struct scsi_sync_cache_16 *cdb; 5631 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5632 5633 starting_lba = scsi_8btou64(cdb->begin_lba); 5634 block_count = scsi_4btoul(cdb->lb_count); 5635 break; 5636 } 5637 default: 5638 ctl_set_invalid_opcode(ctsio); 5639 ctl_done((union ctl_io *)ctsio); 5640 goto bailout; 5641 break; /* NOTREACHED */ 5642 } 5643 5644 /* 5645 * We check the LBA and length, but don't do anything with them. 5646 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5647 * get flushed. This check will just help satisfy anyone who wants 5648 * to see an error for an out of range LBA. 5649 */ 5650 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5651 ctl_set_lba_out_of_range(ctsio); 5652 ctl_done((union ctl_io *)ctsio); 5653 goto bailout; 5654 } 5655 5656 /* 5657 * If this LUN has no backend, we can't flush the cache anyway. 5658 */ 5659 if (lun->backend == NULL) { 5660 ctl_set_invalid_opcode(ctsio); 5661 ctl_done((union ctl_io *)ctsio); 5662 goto bailout; 5663 } 5664 5665 /* 5666 * Check to see whether we're configured to send the SYNCHRONIZE 5667 * CACHE command directly to the back end. 5668 */ 5669 mtx_lock(&lun->lun_lock); 5670 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5671 && (++(lun->sync_count) >= lun->sync_interval)) { 5672 lun->sync_count = 0; 5673 mtx_unlock(&lun->lun_lock); 5674 retval = lun->backend->config_write((union ctl_io *)ctsio); 5675 } else { 5676 mtx_unlock(&lun->lun_lock); 5677 ctl_set_success(ctsio); 5678 ctl_done((union ctl_io *)ctsio); 5679 } 5680 5681bailout: 5682 5683 return (retval); 5684} 5685 5686int 5687ctl_format(struct ctl_scsiio *ctsio) 5688{ 5689 struct scsi_format *cdb; 5690 struct ctl_lun *lun; 5691 struct ctl_softc *ctl_softc; 5692 int length, defect_list_len; 5693 5694 CTL_DEBUG_PRINT(("ctl_format\n")); 5695 5696 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5697 ctl_softc = control_softc; 5698 5699 cdb = (struct scsi_format *)ctsio->cdb; 5700 5701 length = 0; 5702 if (cdb->byte2 & SF_FMTDATA) { 5703 if (cdb->byte2 & SF_LONGLIST) 5704 length = sizeof(struct scsi_format_header_long); 5705 else 5706 length = sizeof(struct scsi_format_header_short); 5707 } 5708 5709 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5710 && (length > 0)) { 5711 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5712 ctsio->kern_data_len = length; 5713 ctsio->kern_total_len = length; 5714 ctsio->kern_data_resid = 0; 5715 ctsio->kern_rel_offset = 0; 5716 ctsio->kern_sg_entries = 0; 5717 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5718 ctsio->be_move_done = ctl_config_move_done; 5719 ctl_datamove((union ctl_io *)ctsio); 5720 5721 return (CTL_RETVAL_COMPLETE); 5722 } 5723 5724 defect_list_len = 0; 5725 5726 if (cdb->byte2 & SF_FMTDATA) { 5727 if (cdb->byte2 & SF_LONGLIST) { 5728 struct scsi_format_header_long *header; 5729 5730 header = (struct scsi_format_header_long *) 5731 ctsio->kern_data_ptr; 5732 5733 defect_list_len = scsi_4btoul(header->defect_list_len); 5734 if (defect_list_len != 0) { 5735 ctl_set_invalid_field(ctsio, 5736 /*sks_valid*/ 1, 5737 /*command*/ 0, 5738 /*field*/ 2, 5739 /*bit_valid*/ 0, 5740 /*bit*/ 0); 5741 goto bailout; 5742 } 5743 } else { 5744 struct scsi_format_header_short *header; 5745 5746 header = (struct scsi_format_header_short *) 5747 ctsio->kern_data_ptr; 5748 5749 defect_list_len = scsi_2btoul(header->defect_list_len); 5750 if (defect_list_len != 0) { 5751 ctl_set_invalid_field(ctsio, 5752 /*sks_valid*/ 1, 5753 /*command*/ 0, 5754 /*field*/ 2, 5755 /*bit_valid*/ 0, 5756 /*bit*/ 0); 5757 goto bailout; 5758 } 5759 } 5760 } 5761 5762 /* 5763 * The format command will clear out the "Medium format corrupted" 5764 * status if set by the configuration code. That status is really 5765 * just a way to notify the host that we have lost the media, and 5766 * get them to issue a command that will basically make them think 5767 * they're blowing away the media. 5768 */ 5769 mtx_lock(&lun->lun_lock); 5770 lun->flags &= ~CTL_LUN_INOPERABLE; 5771 mtx_unlock(&lun->lun_lock); 5772 5773 ctsio->scsi_status = SCSI_STATUS_OK; 5774 ctsio->io_hdr.status = CTL_SUCCESS; 5775bailout: 5776 5777 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5778 free(ctsio->kern_data_ptr, M_CTL); 5779 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5780 } 5781 5782 ctl_done((union ctl_io *)ctsio); 5783 return (CTL_RETVAL_COMPLETE); 5784} 5785 5786int 5787ctl_read_buffer(struct ctl_scsiio *ctsio) 5788{ 5789 struct scsi_read_buffer *cdb; 5790 struct ctl_lun *lun; 5791 int buffer_offset, len; 5792 static uint8_t descr[4]; 5793 static uint8_t echo_descr[4] = { 0 }; 5794 5795 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5796 5797 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5798 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5799 5800 if (lun->flags & CTL_LUN_PR_RESERVED) { 5801 uint32_t residx; 5802 5803 /* 5804 * XXX KDM need a lock here. 5805 */ 5806 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5807 if ((lun->res_type == SPR_TYPE_EX_AC 5808 && residx != lun->pr_res_idx) 5809 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5810 || lun->res_type == SPR_TYPE_EX_AC_AR) 5811 && !lun->per_res[residx].registered)) { 5812 ctl_set_reservation_conflict(ctsio); 5813 ctl_done((union ctl_io *)ctsio); 5814 return (CTL_RETVAL_COMPLETE); 5815 } 5816 } 5817 5818 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5819 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5820 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5821 ctl_set_invalid_field(ctsio, 5822 /*sks_valid*/ 1, 5823 /*command*/ 1, 5824 /*field*/ 1, 5825 /*bit_valid*/ 1, 5826 /*bit*/ 4); 5827 ctl_done((union ctl_io *)ctsio); 5828 return (CTL_RETVAL_COMPLETE); 5829 } 5830 5831 len = scsi_3btoul(cdb->length); 5832 buffer_offset = scsi_3btoul(cdb->offset); 5833 5834 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5835 ctl_set_invalid_field(ctsio, 5836 /*sks_valid*/ 1, 5837 /*command*/ 1, 5838 /*field*/ 6, 5839 /*bit_valid*/ 0, 5840 /*bit*/ 0); 5841 ctl_done((union ctl_io *)ctsio); 5842 return (CTL_RETVAL_COMPLETE); 5843 } 5844 5845 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5846 descr[0] = 0; 5847 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5848 ctsio->kern_data_ptr = descr; 5849 len = min(len, sizeof(descr)); 5850 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5851 ctsio->kern_data_ptr = echo_descr; 5852 len = min(len, sizeof(echo_descr)); 5853 } else 5854 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5855 ctsio->kern_data_len = len; 5856 ctsio->kern_total_len = len; 5857 ctsio->kern_data_resid = 0; 5858 ctsio->kern_rel_offset = 0; 5859 ctsio->kern_sg_entries = 0; 5860 ctsio->be_move_done = ctl_config_move_done; 5861 ctl_datamove((union ctl_io *)ctsio); 5862 5863 return (CTL_RETVAL_COMPLETE); 5864} 5865 5866int 5867ctl_write_buffer(struct ctl_scsiio *ctsio) 5868{ 5869 struct scsi_write_buffer *cdb; 5870 struct ctl_lun *lun; 5871 int buffer_offset, len; 5872 5873 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5874 5875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5876 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5877 5878 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5879 ctl_set_invalid_field(ctsio, 5880 /*sks_valid*/ 1, 5881 /*command*/ 1, 5882 /*field*/ 1, 5883 /*bit_valid*/ 1, 5884 /*bit*/ 4); 5885 ctl_done((union ctl_io *)ctsio); 5886 return (CTL_RETVAL_COMPLETE); 5887 } 5888 5889 len = scsi_3btoul(cdb->length); 5890 buffer_offset = scsi_3btoul(cdb->offset); 5891 5892 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5893 ctl_set_invalid_field(ctsio, 5894 /*sks_valid*/ 1, 5895 /*command*/ 1, 5896 /*field*/ 6, 5897 /*bit_valid*/ 0, 5898 /*bit*/ 0); 5899 ctl_done((union ctl_io *)ctsio); 5900 return (CTL_RETVAL_COMPLETE); 5901 } 5902 5903 /* 5904 * If we've got a kernel request that hasn't been malloced yet, 5905 * malloc it and tell the caller the data buffer is here. 5906 */ 5907 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5908 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5909 ctsio->kern_data_len = len; 5910 ctsio->kern_total_len = len; 5911 ctsio->kern_data_resid = 0; 5912 ctsio->kern_rel_offset = 0; 5913 ctsio->kern_sg_entries = 0; 5914 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5915 ctsio->be_move_done = ctl_config_move_done; 5916 ctl_datamove((union ctl_io *)ctsio); 5917 5918 return (CTL_RETVAL_COMPLETE); 5919 } 5920 5921 ctl_done((union ctl_io *)ctsio); 5922 5923 return (CTL_RETVAL_COMPLETE); 5924} 5925 5926int 5927ctl_write_same(struct ctl_scsiio *ctsio) 5928{ 5929 struct ctl_lun *lun; 5930 struct ctl_lba_len_flags *lbalen; 5931 uint64_t lba; 5932 uint32_t num_blocks; 5933 int len, retval; 5934 uint8_t byte2; 5935 5936 retval = CTL_RETVAL_COMPLETE; 5937 5938 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5939 5940 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5941 5942 switch (ctsio->cdb[0]) { 5943 case WRITE_SAME_10: { 5944 struct scsi_write_same_10 *cdb; 5945 5946 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5947 5948 lba = scsi_4btoul(cdb->addr); 5949 num_blocks = scsi_2btoul(cdb->length); 5950 byte2 = cdb->byte2; 5951 break; 5952 } 5953 case WRITE_SAME_16: { 5954 struct scsi_write_same_16 *cdb; 5955 5956 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5957 5958 lba = scsi_8btou64(cdb->addr); 5959 num_blocks = scsi_4btoul(cdb->length); 5960 byte2 = cdb->byte2; 5961 break; 5962 } 5963 default: 5964 /* 5965 * We got a command we don't support. This shouldn't 5966 * happen, commands should be filtered out above us. 5967 */ 5968 ctl_set_invalid_opcode(ctsio); 5969 ctl_done((union ctl_io *)ctsio); 5970 5971 return (CTL_RETVAL_COMPLETE); 5972 break; /* NOTREACHED */ 5973 } 5974 5975 /* 5976 * The first check is to make sure we're in bounds, the second 5977 * check is to catch wrap-around problems. If the lba + num blocks 5978 * is less than the lba, then we've wrapped around and the block 5979 * range is invalid anyway. 5980 */ 5981 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5982 || ((lba + num_blocks) < lba)) { 5983 ctl_set_lba_out_of_range(ctsio); 5984 ctl_done((union ctl_io *)ctsio); 5985 return (CTL_RETVAL_COMPLETE); 5986 } 5987 5988 /* Zero number of blocks means "to the last logical block" */ 5989 if (num_blocks == 0) { 5990 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5991 ctl_set_invalid_field(ctsio, 5992 /*sks_valid*/ 0, 5993 /*command*/ 1, 5994 /*field*/ 0, 5995 /*bit_valid*/ 0, 5996 /*bit*/ 0); 5997 ctl_done((union ctl_io *)ctsio); 5998 return (CTL_RETVAL_COMPLETE); 5999 } 6000 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6001 } 6002 6003 len = lun->be_lun->blocksize; 6004 6005 /* 6006 * If we've got a kernel request that hasn't been malloced yet, 6007 * malloc it and tell the caller the data buffer is here. 6008 */ 6009 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6010 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6011 ctsio->kern_data_len = len; 6012 ctsio->kern_total_len = len; 6013 ctsio->kern_data_resid = 0; 6014 ctsio->kern_rel_offset = 0; 6015 ctsio->kern_sg_entries = 0; 6016 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6017 ctsio->be_move_done = ctl_config_move_done; 6018 ctl_datamove((union ctl_io *)ctsio); 6019 6020 return (CTL_RETVAL_COMPLETE); 6021 } 6022 6023 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6024 lbalen->lba = lba; 6025 lbalen->len = num_blocks; 6026 lbalen->flags = byte2; 6027 retval = lun->backend->config_write((union ctl_io *)ctsio); 6028 6029 return (retval); 6030} 6031 6032int 6033ctl_unmap(struct ctl_scsiio *ctsio) 6034{ 6035 struct ctl_lun *lun; 6036 struct scsi_unmap *cdb; 6037 struct ctl_ptr_len_flags *ptrlen; 6038 struct scsi_unmap_header *hdr; 6039 struct scsi_unmap_desc *buf, *end; 6040 uint64_t lba; 6041 uint32_t num_blocks; 6042 int len, retval; 6043 uint8_t byte2; 6044 6045 retval = CTL_RETVAL_COMPLETE; 6046 6047 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6048 6049 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6050 cdb = (struct scsi_unmap *)ctsio->cdb; 6051 6052 len = scsi_2btoul(cdb->length); 6053 byte2 = cdb->byte2; 6054 6055 /* 6056 * If we've got a kernel request that hasn't been malloced yet, 6057 * malloc it and tell the caller the data buffer is here. 6058 */ 6059 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6060 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6061 ctsio->kern_data_len = len; 6062 ctsio->kern_total_len = len; 6063 ctsio->kern_data_resid = 0; 6064 ctsio->kern_rel_offset = 0; 6065 ctsio->kern_sg_entries = 0; 6066 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6067 ctsio->be_move_done = ctl_config_move_done; 6068 ctl_datamove((union ctl_io *)ctsio); 6069 6070 return (CTL_RETVAL_COMPLETE); 6071 } 6072 6073 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6074 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6075 if (len < sizeof (*hdr) || 6076 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6077 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6078 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6079 ctl_set_invalid_field(ctsio, 6080 /*sks_valid*/ 0, 6081 /*command*/ 0, 6082 /*field*/ 0, 6083 /*bit_valid*/ 0, 6084 /*bit*/ 0); 6085 ctl_done((union ctl_io *)ctsio); 6086 return (CTL_RETVAL_COMPLETE); 6087 } 6088 len = scsi_2btoul(hdr->desc_length); 6089 buf = (struct scsi_unmap_desc *)(hdr + 1); 6090 end = buf + len / sizeof(*buf); 6091 6092 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6093 ptrlen->ptr = (void *)buf; 6094 ptrlen->len = len; 6095 ptrlen->flags = byte2; 6096 6097 for (; buf < end; buf++) { 6098 lba = scsi_8btou64(buf->lba); 6099 num_blocks = scsi_4btoul(buf->length); 6100 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6101 || ((lba + num_blocks) < lba)) { 6102 ctl_set_lba_out_of_range(ctsio); 6103 ctl_done((union ctl_io *)ctsio); 6104 return (CTL_RETVAL_COMPLETE); 6105 } 6106 } 6107 6108 retval = lun->backend->config_write((union ctl_io *)ctsio); 6109 6110 return (retval); 6111} 6112 6113/* 6114 * Note that this function currently doesn't actually do anything inside 6115 * CTL to enforce things if the DQue bit is turned on. 6116 * 6117 * Also note that this function can't be used in the default case, because 6118 * the DQue bit isn't set in the changeable mask for the control mode page 6119 * anyway. This is just here as an example for how to implement a page 6120 * handler, and a placeholder in case we want to allow the user to turn 6121 * tagged queueing on and off. 6122 * 6123 * The D_SENSE bit handling is functional, however, and will turn 6124 * descriptor sense on and off for a given LUN. 6125 */ 6126int 6127ctl_control_page_handler(struct ctl_scsiio *ctsio, 6128 struct ctl_page_index *page_index, uint8_t *page_ptr) 6129{ 6130 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6131 struct ctl_lun *lun; 6132 struct ctl_softc *softc; 6133 int set_ua; 6134 uint32_t initidx; 6135 6136 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6137 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6138 set_ua = 0; 6139 6140 user_cp = (struct scsi_control_page *)page_ptr; 6141 current_cp = (struct scsi_control_page *) 6142 (page_index->page_data + (page_index->page_len * 6143 CTL_PAGE_CURRENT)); 6144 saved_cp = (struct scsi_control_page *) 6145 (page_index->page_data + (page_index->page_len * 6146 CTL_PAGE_SAVED)); 6147 6148 softc = control_softc; 6149 6150 mtx_lock(&lun->lun_lock); 6151 if (((current_cp->rlec & SCP_DSENSE) == 0) 6152 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6153 /* 6154 * Descriptor sense is currently turned off and the user 6155 * wants to turn it on. 6156 */ 6157 current_cp->rlec |= SCP_DSENSE; 6158 saved_cp->rlec |= SCP_DSENSE; 6159 lun->flags |= CTL_LUN_SENSE_DESC; 6160 set_ua = 1; 6161 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6162 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6163 /* 6164 * Descriptor sense is currently turned on, and the user 6165 * wants to turn it off. 6166 */ 6167 current_cp->rlec &= ~SCP_DSENSE; 6168 saved_cp->rlec &= ~SCP_DSENSE; 6169 lun->flags &= ~CTL_LUN_SENSE_DESC; 6170 set_ua = 1; 6171 } 6172 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6173 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6174#ifdef NEEDTOPORT 6175 csevent_log(CSC_CTL | CSC_SHELF_SW | 6176 CTL_UNTAG_TO_UNTAG, 6177 csevent_LogType_Trace, 6178 csevent_Severity_Information, 6179 csevent_AlertLevel_Green, 6180 csevent_FRU_Firmware, 6181 csevent_FRU_Unknown, 6182 "Received untagged to untagged transition"); 6183#endif /* NEEDTOPORT */ 6184 } else { 6185#ifdef NEEDTOPORT 6186 csevent_log(CSC_CTL | CSC_SHELF_SW | 6187 CTL_UNTAG_TO_TAG, 6188 csevent_LogType_ConfigChange, 6189 csevent_Severity_Information, 6190 csevent_AlertLevel_Green, 6191 csevent_FRU_Firmware, 6192 csevent_FRU_Unknown, 6193 "Received untagged to tagged " 6194 "queueing transition"); 6195#endif /* NEEDTOPORT */ 6196 6197 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6198 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6199 set_ua = 1; 6200 } 6201 } else { 6202 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6203#ifdef NEEDTOPORT 6204 csevent_log(CSC_CTL | CSC_SHELF_SW | 6205 CTL_TAG_TO_UNTAG, 6206 csevent_LogType_ConfigChange, 6207 csevent_Severity_Warning, 6208 csevent_AlertLevel_Yellow, 6209 csevent_FRU_Firmware, 6210 csevent_FRU_Unknown, 6211 "Received tagged queueing to untagged " 6212 "transition"); 6213#endif /* NEEDTOPORT */ 6214 6215 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6216 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6217 set_ua = 1; 6218 } else { 6219#ifdef NEEDTOPORT 6220 csevent_log(CSC_CTL | CSC_SHELF_SW | 6221 CTL_TAG_TO_TAG, 6222 csevent_LogType_Trace, 6223 csevent_Severity_Information, 6224 csevent_AlertLevel_Green, 6225 csevent_FRU_Firmware, 6226 csevent_FRU_Unknown, 6227 "Received tagged queueing to tagged " 6228 "queueing transition"); 6229#endif /* NEEDTOPORT */ 6230 } 6231 } 6232 if (set_ua != 0) { 6233 int i; 6234 /* 6235 * Let other initiators know that the mode 6236 * parameters for this LUN have changed. 6237 */ 6238 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6239 if (i == initidx) 6240 continue; 6241 6242 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6243 } 6244 } 6245 mtx_unlock(&lun->lun_lock); 6246 6247 return (0); 6248} 6249 6250int 6251ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6252 struct ctl_page_index *page_index, uint8_t *page_ptr) 6253{ 6254 return (0); 6255} 6256 6257int 6258ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6259 struct ctl_page_index *page_index, int pc) 6260{ 6261 struct copan_power_subpage *page; 6262 6263 page = (struct copan_power_subpage *)page_index->page_data + 6264 (page_index->page_len * pc); 6265 6266 switch (pc) { 6267 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6268 /* 6269 * We don't update the changable bits for this page. 6270 */ 6271 break; 6272 case SMS_PAGE_CTRL_CURRENT >> 6: 6273 case SMS_PAGE_CTRL_DEFAULT >> 6: 6274 case SMS_PAGE_CTRL_SAVED >> 6: 6275#ifdef NEEDTOPORT 6276 ctl_update_power_subpage(page); 6277#endif 6278 break; 6279 default: 6280#ifdef NEEDTOPORT 6281 EPRINT(0, "Invalid PC %d!!", pc); 6282#endif 6283 break; 6284 } 6285 return (0); 6286} 6287 6288 6289int 6290ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6291 struct ctl_page_index *page_index, uint8_t *page_ptr) 6292{ 6293 struct copan_aps_subpage *user_sp; 6294 struct copan_aps_subpage *current_sp; 6295 union ctl_modepage_info *modepage_info; 6296 struct ctl_softc *softc; 6297 struct ctl_lun *lun; 6298 int retval; 6299 6300 retval = CTL_RETVAL_COMPLETE; 6301 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6302 (page_index->page_len * CTL_PAGE_CURRENT)); 6303 softc = control_softc; 6304 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6305 6306 user_sp = (struct copan_aps_subpage *)page_ptr; 6307 6308 modepage_info = (union ctl_modepage_info *) 6309 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6310 6311 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6312 modepage_info->header.subpage = page_index->subpage; 6313 modepage_info->aps.lock_active = user_sp->lock_active; 6314 6315 mtx_lock(&softc->ctl_lock); 6316 6317 /* 6318 * If there is a request to lock the LUN and another LUN is locked 6319 * this is an error. If the requested LUN is already locked ignore 6320 * the request. If no LUN is locked attempt to lock it. 6321 * if there is a request to unlock the LUN and the LUN is currently 6322 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6323 * if another LUN is locked or no LUN is locked. 6324 */ 6325 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6326 if (softc->aps_locked_lun == lun->lun) { 6327 /* 6328 * This LUN is already locked, so we're done. 6329 */ 6330 retval = CTL_RETVAL_COMPLETE; 6331 } else if (softc->aps_locked_lun == 0) { 6332 /* 6333 * No one has the lock, pass the request to the 6334 * backend. 6335 */ 6336 retval = lun->backend->config_write( 6337 (union ctl_io *)ctsio); 6338 } else { 6339 /* 6340 * Someone else has the lock, throw out the request. 6341 */ 6342 ctl_set_already_locked(ctsio); 6343 free(ctsio->kern_data_ptr, M_CTL); 6344 ctl_done((union ctl_io *)ctsio); 6345 6346 /* 6347 * Set the return value so that ctl_do_mode_select() 6348 * won't try to complete the command. We already 6349 * completed it here. 6350 */ 6351 retval = CTL_RETVAL_ERROR; 6352 } 6353 } else if (softc->aps_locked_lun == lun->lun) { 6354 /* 6355 * This LUN is locked, so pass the unlock request to the 6356 * backend. 6357 */ 6358 retval = lun->backend->config_write((union ctl_io *)ctsio); 6359 } 6360 mtx_unlock(&softc->ctl_lock); 6361 6362 return (retval); 6363} 6364 6365int 6366ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6367 struct ctl_page_index *page_index, 6368 uint8_t *page_ptr) 6369{ 6370 uint8_t *c; 6371 int i; 6372 6373 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6374 ctl_time_io_secs = 6375 (c[0] << 8) | 6376 (c[1] << 0) | 6377 0; 6378 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6379 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6380 printf("page data:"); 6381 for (i=0; i<8; i++) 6382 printf(" %.2x",page_ptr[i]); 6383 printf("\n"); 6384 return (0); 6385} 6386 6387int 6388ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6389 struct ctl_page_index *page_index, 6390 int pc) 6391{ 6392 struct copan_debugconf_subpage *page; 6393 6394 page = (struct copan_debugconf_subpage *)page_index->page_data + 6395 (page_index->page_len * pc); 6396 6397 switch (pc) { 6398 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6399 case SMS_PAGE_CTRL_DEFAULT >> 6: 6400 case SMS_PAGE_CTRL_SAVED >> 6: 6401 /* 6402 * We don't update the changable or default bits for this page. 6403 */ 6404 break; 6405 case SMS_PAGE_CTRL_CURRENT >> 6: 6406 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6407 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6408 break; 6409 default: 6410#ifdef NEEDTOPORT 6411 EPRINT(0, "Invalid PC %d!!", pc); 6412#endif /* NEEDTOPORT */ 6413 break; 6414 } 6415 return (0); 6416} 6417 6418 6419static int 6420ctl_do_mode_select(union ctl_io *io) 6421{ 6422 struct scsi_mode_page_header *page_header; 6423 struct ctl_page_index *page_index; 6424 struct ctl_scsiio *ctsio; 6425 int control_dev, page_len; 6426 int page_len_offset, page_len_size; 6427 union ctl_modepage_info *modepage_info; 6428 struct ctl_lun *lun; 6429 int *len_left, *len_used; 6430 int retval, i; 6431 6432 ctsio = &io->scsiio; 6433 page_index = NULL; 6434 page_len = 0; 6435 retval = CTL_RETVAL_COMPLETE; 6436 6437 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6438 6439 if (lun->be_lun->lun_type != T_DIRECT) 6440 control_dev = 1; 6441 else 6442 control_dev = 0; 6443 6444 modepage_info = (union ctl_modepage_info *) 6445 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6446 len_left = &modepage_info->header.len_left; 6447 len_used = &modepage_info->header.len_used; 6448 6449do_next_page: 6450 6451 page_header = (struct scsi_mode_page_header *) 6452 (ctsio->kern_data_ptr + *len_used); 6453 6454 if (*len_left == 0) { 6455 free(ctsio->kern_data_ptr, M_CTL); 6456 ctl_set_success(ctsio); 6457 ctl_done((union ctl_io *)ctsio); 6458 return (CTL_RETVAL_COMPLETE); 6459 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6460 6461 free(ctsio->kern_data_ptr, M_CTL); 6462 ctl_set_param_len_error(ctsio); 6463 ctl_done((union ctl_io *)ctsio); 6464 return (CTL_RETVAL_COMPLETE); 6465 6466 } else if ((page_header->page_code & SMPH_SPF) 6467 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6468 6469 free(ctsio->kern_data_ptr, M_CTL); 6470 ctl_set_param_len_error(ctsio); 6471 ctl_done((union ctl_io *)ctsio); 6472 return (CTL_RETVAL_COMPLETE); 6473 } 6474 6475 6476 /* 6477 * XXX KDM should we do something with the block descriptor? 6478 */ 6479 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6480 6481 if ((control_dev != 0) 6482 && (lun->mode_pages.index[i].page_flags & 6483 CTL_PAGE_FLAG_DISK_ONLY)) 6484 continue; 6485 6486 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6487 (page_header->page_code & SMPH_PC_MASK)) 6488 continue; 6489 6490 /* 6491 * If neither page has a subpage code, then we've got a 6492 * match. 6493 */ 6494 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6495 && ((page_header->page_code & SMPH_SPF) == 0)) { 6496 page_index = &lun->mode_pages.index[i]; 6497 page_len = page_header->page_length; 6498 break; 6499 } 6500 6501 /* 6502 * If both pages have subpages, then the subpage numbers 6503 * have to match. 6504 */ 6505 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6506 && (page_header->page_code & SMPH_SPF)) { 6507 struct scsi_mode_page_header_sp *sph; 6508 6509 sph = (struct scsi_mode_page_header_sp *)page_header; 6510 6511 if (lun->mode_pages.index[i].subpage == 6512 sph->subpage) { 6513 page_index = &lun->mode_pages.index[i]; 6514 page_len = scsi_2btoul(sph->page_length); 6515 break; 6516 } 6517 } 6518 } 6519 6520 /* 6521 * If we couldn't find the page, or if we don't have a mode select 6522 * handler for it, send back an error to the user. 6523 */ 6524 if ((page_index == NULL) 6525 || (page_index->select_handler == NULL)) { 6526 ctl_set_invalid_field(ctsio, 6527 /*sks_valid*/ 1, 6528 /*command*/ 0, 6529 /*field*/ *len_used, 6530 /*bit_valid*/ 0, 6531 /*bit*/ 0); 6532 free(ctsio->kern_data_ptr, M_CTL); 6533 ctl_done((union ctl_io *)ctsio); 6534 return (CTL_RETVAL_COMPLETE); 6535 } 6536 6537 if (page_index->page_code & SMPH_SPF) { 6538 page_len_offset = 2; 6539 page_len_size = 2; 6540 } else { 6541 page_len_size = 1; 6542 page_len_offset = 1; 6543 } 6544 6545 /* 6546 * If the length the initiator gives us isn't the one we specify in 6547 * the mode page header, or if they didn't specify enough data in 6548 * the CDB to avoid truncating this page, kick out the request. 6549 */ 6550 if ((page_len != (page_index->page_len - page_len_offset - 6551 page_len_size)) 6552 || (*len_left < page_index->page_len)) { 6553 6554 6555 ctl_set_invalid_field(ctsio, 6556 /*sks_valid*/ 1, 6557 /*command*/ 0, 6558 /*field*/ *len_used + page_len_offset, 6559 /*bit_valid*/ 0, 6560 /*bit*/ 0); 6561 free(ctsio->kern_data_ptr, M_CTL); 6562 ctl_done((union ctl_io *)ctsio); 6563 return (CTL_RETVAL_COMPLETE); 6564 } 6565 6566 /* 6567 * Run through the mode page, checking to make sure that the bits 6568 * the user changed are actually legal for him to change. 6569 */ 6570 for (i = 0; i < page_index->page_len; i++) { 6571 uint8_t *user_byte, *change_mask, *current_byte; 6572 int bad_bit; 6573 int j; 6574 6575 user_byte = (uint8_t *)page_header + i; 6576 change_mask = page_index->page_data + 6577 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6578 current_byte = page_index->page_data + 6579 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6580 6581 /* 6582 * Check to see whether the user set any bits in this byte 6583 * that he is not allowed to set. 6584 */ 6585 if ((*user_byte & ~(*change_mask)) == 6586 (*current_byte & ~(*change_mask))) 6587 continue; 6588 6589 /* 6590 * Go through bit by bit to determine which one is illegal. 6591 */ 6592 bad_bit = 0; 6593 for (j = 7; j >= 0; j--) { 6594 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6595 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6596 bad_bit = i; 6597 break; 6598 } 6599 } 6600 ctl_set_invalid_field(ctsio, 6601 /*sks_valid*/ 1, 6602 /*command*/ 0, 6603 /*field*/ *len_used + i, 6604 /*bit_valid*/ 1, 6605 /*bit*/ bad_bit); 6606 free(ctsio->kern_data_ptr, M_CTL); 6607 ctl_done((union ctl_io *)ctsio); 6608 return (CTL_RETVAL_COMPLETE); 6609 } 6610 6611 /* 6612 * Decrement these before we call the page handler, since we may 6613 * end up getting called back one way or another before the handler 6614 * returns to this context. 6615 */ 6616 *len_left -= page_index->page_len; 6617 *len_used += page_index->page_len; 6618 6619 retval = page_index->select_handler(ctsio, page_index, 6620 (uint8_t *)page_header); 6621 6622 /* 6623 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6624 * wait until this queued command completes to finish processing 6625 * the mode page. If it returns anything other than 6626 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6627 * already set the sense information, freed the data pointer, and 6628 * completed the io for us. 6629 */ 6630 if (retval != CTL_RETVAL_COMPLETE) 6631 goto bailout_no_done; 6632 6633 /* 6634 * If the initiator sent us more than one page, parse the next one. 6635 */ 6636 if (*len_left > 0) 6637 goto do_next_page; 6638 6639 ctl_set_success(ctsio); 6640 free(ctsio->kern_data_ptr, M_CTL); 6641 ctl_done((union ctl_io *)ctsio); 6642 6643bailout_no_done: 6644 6645 return (CTL_RETVAL_COMPLETE); 6646 6647} 6648 6649int 6650ctl_mode_select(struct ctl_scsiio *ctsio) 6651{ 6652 int param_len, pf, sp; 6653 int header_size, bd_len; 6654 int len_left, len_used; 6655 struct ctl_page_index *page_index; 6656 struct ctl_lun *lun; 6657 int control_dev, page_len; 6658 union ctl_modepage_info *modepage_info; 6659 int retval; 6660 6661 pf = 0; 6662 sp = 0; 6663 page_len = 0; 6664 len_used = 0; 6665 len_left = 0; 6666 retval = 0; 6667 bd_len = 0; 6668 page_index = NULL; 6669 6670 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6671 6672 if (lun->be_lun->lun_type != T_DIRECT) 6673 control_dev = 1; 6674 else 6675 control_dev = 0; 6676 6677 switch (ctsio->cdb[0]) { 6678 case MODE_SELECT_6: { 6679 struct scsi_mode_select_6 *cdb; 6680 6681 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6682 6683 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6684 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6685 6686 param_len = cdb->length; 6687 header_size = sizeof(struct scsi_mode_header_6); 6688 break; 6689 } 6690 case MODE_SELECT_10: { 6691 struct scsi_mode_select_10 *cdb; 6692 6693 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6694 6695 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6696 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6697 6698 param_len = scsi_2btoul(cdb->length); 6699 header_size = sizeof(struct scsi_mode_header_10); 6700 break; 6701 } 6702 default: 6703 ctl_set_invalid_opcode(ctsio); 6704 ctl_done((union ctl_io *)ctsio); 6705 return (CTL_RETVAL_COMPLETE); 6706 break; /* NOTREACHED */ 6707 } 6708 6709 /* 6710 * From SPC-3: 6711 * "A parameter list length of zero indicates that the Data-Out Buffer 6712 * shall be empty. This condition shall not be considered as an error." 6713 */ 6714 if (param_len == 0) { 6715 ctl_set_success(ctsio); 6716 ctl_done((union ctl_io *)ctsio); 6717 return (CTL_RETVAL_COMPLETE); 6718 } 6719 6720 /* 6721 * Since we'll hit this the first time through, prior to 6722 * allocation, we don't need to free a data buffer here. 6723 */ 6724 if (param_len < header_size) { 6725 ctl_set_param_len_error(ctsio); 6726 ctl_done((union ctl_io *)ctsio); 6727 return (CTL_RETVAL_COMPLETE); 6728 } 6729 6730 /* 6731 * Allocate the data buffer and grab the user's data. In theory, 6732 * we shouldn't have to sanity check the parameter list length here 6733 * because the maximum size is 64K. We should be able to malloc 6734 * that much without too many problems. 6735 */ 6736 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6737 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6738 ctsio->kern_data_len = param_len; 6739 ctsio->kern_total_len = param_len; 6740 ctsio->kern_data_resid = 0; 6741 ctsio->kern_rel_offset = 0; 6742 ctsio->kern_sg_entries = 0; 6743 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6744 ctsio->be_move_done = ctl_config_move_done; 6745 ctl_datamove((union ctl_io *)ctsio); 6746 6747 return (CTL_RETVAL_COMPLETE); 6748 } 6749 6750 switch (ctsio->cdb[0]) { 6751 case MODE_SELECT_6: { 6752 struct scsi_mode_header_6 *mh6; 6753 6754 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6755 bd_len = mh6->blk_desc_len; 6756 break; 6757 } 6758 case MODE_SELECT_10: { 6759 struct scsi_mode_header_10 *mh10; 6760 6761 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6762 bd_len = scsi_2btoul(mh10->blk_desc_len); 6763 break; 6764 } 6765 default: 6766 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6767 break; 6768 } 6769 6770 if (param_len < (header_size + bd_len)) { 6771 free(ctsio->kern_data_ptr, M_CTL); 6772 ctl_set_param_len_error(ctsio); 6773 ctl_done((union ctl_io *)ctsio); 6774 return (CTL_RETVAL_COMPLETE); 6775 } 6776 6777 /* 6778 * Set the IO_CONT flag, so that if this I/O gets passed to 6779 * ctl_config_write_done(), it'll get passed back to 6780 * ctl_do_mode_select() for further processing, or completion if 6781 * we're all done. 6782 */ 6783 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6784 ctsio->io_cont = ctl_do_mode_select; 6785 6786 modepage_info = (union ctl_modepage_info *) 6787 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6788 6789 memset(modepage_info, 0, sizeof(*modepage_info)); 6790 6791 len_left = param_len - header_size - bd_len; 6792 len_used = header_size + bd_len; 6793 6794 modepage_info->header.len_left = len_left; 6795 modepage_info->header.len_used = len_used; 6796 6797 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6798} 6799 6800int 6801ctl_mode_sense(struct ctl_scsiio *ctsio) 6802{ 6803 struct ctl_lun *lun; 6804 int pc, page_code, dbd, llba, subpage; 6805 int alloc_len, page_len, header_len, total_len; 6806 struct scsi_mode_block_descr *block_desc; 6807 struct ctl_page_index *page_index; 6808 int control_dev; 6809 6810 dbd = 0; 6811 llba = 0; 6812 block_desc = NULL; 6813 page_index = NULL; 6814 6815 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6816 6817 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6818 6819 if (lun->be_lun->lun_type != T_DIRECT) 6820 control_dev = 1; 6821 else 6822 control_dev = 0; 6823 6824 if (lun->flags & CTL_LUN_PR_RESERVED) { 6825 uint32_t residx; 6826 6827 /* 6828 * XXX KDM need a lock here. 6829 */ 6830 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6831 if ((lun->res_type == SPR_TYPE_EX_AC 6832 && residx != lun->pr_res_idx) 6833 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6834 || lun->res_type == SPR_TYPE_EX_AC_AR) 6835 && !lun->per_res[residx].registered)) { 6836 ctl_set_reservation_conflict(ctsio); 6837 ctl_done((union ctl_io *)ctsio); 6838 return (CTL_RETVAL_COMPLETE); 6839 } 6840 } 6841 6842 switch (ctsio->cdb[0]) { 6843 case MODE_SENSE_6: { 6844 struct scsi_mode_sense_6 *cdb; 6845 6846 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6847 6848 header_len = sizeof(struct scsi_mode_hdr_6); 6849 if (cdb->byte2 & SMS_DBD) 6850 dbd = 1; 6851 else 6852 header_len += sizeof(struct scsi_mode_block_descr); 6853 6854 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6855 page_code = cdb->page & SMS_PAGE_CODE; 6856 subpage = cdb->subpage; 6857 alloc_len = cdb->length; 6858 break; 6859 } 6860 case MODE_SENSE_10: { 6861 struct scsi_mode_sense_10 *cdb; 6862 6863 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6864 6865 header_len = sizeof(struct scsi_mode_hdr_10); 6866 6867 if (cdb->byte2 & SMS_DBD) 6868 dbd = 1; 6869 else 6870 header_len += sizeof(struct scsi_mode_block_descr); 6871 if (cdb->byte2 & SMS10_LLBAA) 6872 llba = 1; 6873 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6874 page_code = cdb->page & SMS_PAGE_CODE; 6875 subpage = cdb->subpage; 6876 alloc_len = scsi_2btoul(cdb->length); 6877 break; 6878 } 6879 default: 6880 ctl_set_invalid_opcode(ctsio); 6881 ctl_done((union ctl_io *)ctsio); 6882 return (CTL_RETVAL_COMPLETE); 6883 break; /* NOTREACHED */ 6884 } 6885 6886 /* 6887 * We have to make a first pass through to calculate the size of 6888 * the pages that match the user's query. Then we allocate enough 6889 * memory to hold it, and actually copy the data into the buffer. 6890 */ 6891 switch (page_code) { 6892 case SMS_ALL_PAGES_PAGE: { 6893 int i; 6894 6895 page_len = 0; 6896 6897 /* 6898 * At the moment, values other than 0 and 0xff here are 6899 * reserved according to SPC-3. 6900 */ 6901 if ((subpage != SMS_SUBPAGE_PAGE_0) 6902 && (subpage != SMS_SUBPAGE_ALL)) { 6903 ctl_set_invalid_field(ctsio, 6904 /*sks_valid*/ 1, 6905 /*command*/ 1, 6906 /*field*/ 3, 6907 /*bit_valid*/ 0, 6908 /*bit*/ 0); 6909 ctl_done((union ctl_io *)ctsio); 6910 return (CTL_RETVAL_COMPLETE); 6911 } 6912 6913 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6914 if ((control_dev != 0) 6915 && (lun->mode_pages.index[i].page_flags & 6916 CTL_PAGE_FLAG_DISK_ONLY)) 6917 continue; 6918 6919 /* 6920 * We don't use this subpage if the user didn't 6921 * request all subpages. 6922 */ 6923 if ((lun->mode_pages.index[i].subpage != 0) 6924 && (subpage == SMS_SUBPAGE_PAGE_0)) 6925 continue; 6926 6927#if 0 6928 printf("found page %#x len %d\n", 6929 lun->mode_pages.index[i].page_code & 6930 SMPH_PC_MASK, 6931 lun->mode_pages.index[i].page_len); 6932#endif 6933 page_len += lun->mode_pages.index[i].page_len; 6934 } 6935 break; 6936 } 6937 default: { 6938 int i; 6939 6940 page_len = 0; 6941 6942 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6943 /* Look for the right page code */ 6944 if ((lun->mode_pages.index[i].page_code & 6945 SMPH_PC_MASK) != page_code) 6946 continue; 6947 6948 /* Look for the right subpage or the subpage wildcard*/ 6949 if ((lun->mode_pages.index[i].subpage != subpage) 6950 && (subpage != SMS_SUBPAGE_ALL)) 6951 continue; 6952 6953 /* Make sure the page is supported for this dev type */ 6954 if ((control_dev != 0) 6955 && (lun->mode_pages.index[i].page_flags & 6956 CTL_PAGE_FLAG_DISK_ONLY)) 6957 continue; 6958 6959#if 0 6960 printf("found page %#x len %d\n", 6961 lun->mode_pages.index[i].page_code & 6962 SMPH_PC_MASK, 6963 lun->mode_pages.index[i].page_len); 6964#endif 6965 6966 page_len += lun->mode_pages.index[i].page_len; 6967 } 6968 6969 if (page_len == 0) { 6970 ctl_set_invalid_field(ctsio, 6971 /*sks_valid*/ 1, 6972 /*command*/ 1, 6973 /*field*/ 2, 6974 /*bit_valid*/ 1, 6975 /*bit*/ 5); 6976 ctl_done((union ctl_io *)ctsio); 6977 return (CTL_RETVAL_COMPLETE); 6978 } 6979 break; 6980 } 6981 } 6982 6983 total_len = header_len + page_len; 6984#if 0 6985 printf("header_len = %d, page_len = %d, total_len = %d\n", 6986 header_len, page_len, total_len); 6987#endif 6988 6989 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6990 ctsio->kern_sg_entries = 0; 6991 ctsio->kern_data_resid = 0; 6992 ctsio->kern_rel_offset = 0; 6993 if (total_len < alloc_len) { 6994 ctsio->residual = alloc_len - total_len; 6995 ctsio->kern_data_len = total_len; 6996 ctsio->kern_total_len = total_len; 6997 } else { 6998 ctsio->residual = 0; 6999 ctsio->kern_data_len = alloc_len; 7000 ctsio->kern_total_len = alloc_len; 7001 } 7002 7003 switch (ctsio->cdb[0]) { 7004 case MODE_SENSE_6: { 7005 struct scsi_mode_hdr_6 *header; 7006 7007 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7008 7009 header->datalen = ctl_min(total_len - 1, 254); 7010 7011 if (dbd) 7012 header->block_descr_len = 0; 7013 else 7014 header->block_descr_len = 7015 sizeof(struct scsi_mode_block_descr); 7016 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7017 break; 7018 } 7019 case MODE_SENSE_10: { 7020 struct scsi_mode_hdr_10 *header; 7021 int datalen; 7022 7023 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7024 7025 datalen = ctl_min(total_len - 2, 65533); 7026 scsi_ulto2b(datalen, header->datalen); 7027 if (dbd) 7028 scsi_ulto2b(0, header->block_descr_len); 7029 else 7030 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7031 header->block_descr_len); 7032 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7033 break; 7034 } 7035 default: 7036 panic("invalid CDB type %#x", ctsio->cdb[0]); 7037 break; /* NOTREACHED */ 7038 } 7039 7040 /* 7041 * If we've got a disk, use its blocksize in the block 7042 * descriptor. Otherwise, just set it to 0. 7043 */ 7044 if (dbd == 0) { 7045 if (control_dev != 0) 7046 scsi_ulto3b(lun->be_lun->blocksize, 7047 block_desc->block_len); 7048 else 7049 scsi_ulto3b(0, block_desc->block_len); 7050 } 7051 7052 switch (page_code) { 7053 case SMS_ALL_PAGES_PAGE: { 7054 int i, data_used; 7055 7056 data_used = header_len; 7057 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7058 struct ctl_page_index *page_index; 7059 7060 page_index = &lun->mode_pages.index[i]; 7061 7062 if ((control_dev != 0) 7063 && (page_index->page_flags & 7064 CTL_PAGE_FLAG_DISK_ONLY)) 7065 continue; 7066 7067 /* 7068 * We don't use this subpage if the user didn't 7069 * request all subpages. We already checked (above) 7070 * to make sure the user only specified a subpage 7071 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7072 */ 7073 if ((page_index->subpage != 0) 7074 && (subpage == SMS_SUBPAGE_PAGE_0)) 7075 continue; 7076 7077 /* 7078 * Call the handler, if it exists, to update the 7079 * page to the latest values. 7080 */ 7081 if (page_index->sense_handler != NULL) 7082 page_index->sense_handler(ctsio, page_index,pc); 7083 7084 memcpy(ctsio->kern_data_ptr + data_used, 7085 page_index->page_data + 7086 (page_index->page_len * pc), 7087 page_index->page_len); 7088 data_used += page_index->page_len; 7089 } 7090 break; 7091 } 7092 default: { 7093 int i, data_used; 7094 7095 data_used = header_len; 7096 7097 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7098 struct ctl_page_index *page_index; 7099 7100 page_index = &lun->mode_pages.index[i]; 7101 7102 /* Look for the right page code */ 7103 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7104 continue; 7105 7106 /* Look for the right subpage or the subpage wildcard*/ 7107 if ((page_index->subpage != subpage) 7108 && (subpage != SMS_SUBPAGE_ALL)) 7109 continue; 7110 7111 /* Make sure the page is supported for this dev type */ 7112 if ((control_dev != 0) 7113 && (page_index->page_flags & 7114 CTL_PAGE_FLAG_DISK_ONLY)) 7115 continue; 7116 7117 /* 7118 * Call the handler, if it exists, to update the 7119 * page to the latest values. 7120 */ 7121 if (page_index->sense_handler != NULL) 7122 page_index->sense_handler(ctsio, page_index,pc); 7123 7124 memcpy(ctsio->kern_data_ptr + data_used, 7125 page_index->page_data + 7126 (page_index->page_len * pc), 7127 page_index->page_len); 7128 data_used += page_index->page_len; 7129 } 7130 break; 7131 } 7132 } 7133 7134 ctsio->scsi_status = SCSI_STATUS_OK; 7135 7136 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7137 ctsio->be_move_done = ctl_config_move_done; 7138 ctl_datamove((union ctl_io *)ctsio); 7139 7140 return (CTL_RETVAL_COMPLETE); 7141} 7142 7143int 7144ctl_read_capacity(struct ctl_scsiio *ctsio) 7145{ 7146 struct scsi_read_capacity *cdb; 7147 struct scsi_read_capacity_data *data; 7148 struct ctl_lun *lun; 7149 uint32_t lba; 7150 7151 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7152 7153 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7154 7155 lba = scsi_4btoul(cdb->addr); 7156 if (((cdb->pmi & SRC_PMI) == 0) 7157 && (lba != 0)) { 7158 ctl_set_invalid_field(/*ctsio*/ ctsio, 7159 /*sks_valid*/ 1, 7160 /*command*/ 1, 7161 /*field*/ 2, 7162 /*bit_valid*/ 0, 7163 /*bit*/ 0); 7164 ctl_done((union ctl_io *)ctsio); 7165 return (CTL_RETVAL_COMPLETE); 7166 } 7167 7168 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7169 7170 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7171 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7172 ctsio->residual = 0; 7173 ctsio->kern_data_len = sizeof(*data); 7174 ctsio->kern_total_len = sizeof(*data); 7175 ctsio->kern_data_resid = 0; 7176 ctsio->kern_rel_offset = 0; 7177 ctsio->kern_sg_entries = 0; 7178 7179 /* 7180 * If the maximum LBA is greater than 0xfffffffe, the user must 7181 * issue a SERVICE ACTION IN (16) command, with the read capacity 7182 * serivce action set. 7183 */ 7184 if (lun->be_lun->maxlba > 0xfffffffe) 7185 scsi_ulto4b(0xffffffff, data->addr); 7186 else 7187 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7188 7189 /* 7190 * XXX KDM this may not be 512 bytes... 7191 */ 7192 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7193 7194 ctsio->scsi_status = SCSI_STATUS_OK; 7195 7196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7197 ctsio->be_move_done = ctl_config_move_done; 7198 ctl_datamove((union ctl_io *)ctsio); 7199 7200 return (CTL_RETVAL_COMPLETE); 7201} 7202 7203int 7204ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7205{ 7206 struct scsi_read_capacity_16 *cdb; 7207 struct scsi_read_capacity_data_long *data; 7208 struct ctl_lun *lun; 7209 uint64_t lba; 7210 uint32_t alloc_len; 7211 7212 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7213 7214 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7215 7216 alloc_len = scsi_4btoul(cdb->alloc_len); 7217 lba = scsi_8btou64(cdb->addr); 7218 7219 if ((cdb->reladr & SRC16_PMI) 7220 && (lba != 0)) { 7221 ctl_set_invalid_field(/*ctsio*/ ctsio, 7222 /*sks_valid*/ 1, 7223 /*command*/ 1, 7224 /*field*/ 2, 7225 /*bit_valid*/ 0, 7226 /*bit*/ 0); 7227 ctl_done((union ctl_io *)ctsio); 7228 return (CTL_RETVAL_COMPLETE); 7229 } 7230 7231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7232 7233 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7234 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7235 7236 if (sizeof(*data) < alloc_len) { 7237 ctsio->residual = alloc_len - sizeof(*data); 7238 ctsio->kern_data_len = sizeof(*data); 7239 ctsio->kern_total_len = sizeof(*data); 7240 } else { 7241 ctsio->residual = 0; 7242 ctsio->kern_data_len = alloc_len; 7243 ctsio->kern_total_len = alloc_len; 7244 } 7245 ctsio->kern_data_resid = 0; 7246 ctsio->kern_rel_offset = 0; 7247 ctsio->kern_sg_entries = 0; 7248 7249 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7250 /* XXX KDM this may not be 512 bytes... */ 7251 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7252 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7253 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7254 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
| 387static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 388static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 389static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 390static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 391static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 392static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 393 union ctl_io *ooa_io); 394static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 395 union ctl_io *starting_io); 396static int ctl_check_blocked(struct ctl_lun *lun); 397static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 398 struct ctl_lun *lun, 399 const struct ctl_cmd_entry *entry, 400 struct ctl_scsiio *ctsio); 401//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 402static void ctl_failover(void); 403static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 404 struct ctl_scsiio *ctsio); 405static int ctl_scsiio(struct ctl_scsiio *ctsio); 406 407static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 408static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 409 ctl_ua_type ua_type); 410static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 411 ctl_ua_type ua_type); 412static int ctl_abort_task(union ctl_io *io); 413static int ctl_abort_task_set(union ctl_io *io); 414static int ctl_i_t_nexus_reset(union ctl_io *io); 415static void ctl_run_task(union ctl_io *io); 416#ifdef CTL_IO_DELAY 417static void ctl_datamove_timer_wakeup(void *arg); 418static void ctl_done_timer_wakeup(void *arg); 419#endif /* CTL_IO_DELAY */ 420 421static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 422static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 423static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 424static void ctl_datamove_remote_write(union ctl_io *io); 425static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 426static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 427static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 428static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 429 ctl_ha_dt_cb callback); 430static void ctl_datamove_remote_read(union ctl_io *io); 431static void ctl_datamove_remote(union ctl_io *io); 432static int ctl_process_done(union ctl_io *io); 433static void ctl_lun_thread(void *arg); 434static void ctl_work_thread(void *arg); 435static void ctl_enqueue_incoming(union ctl_io *io); 436static void ctl_enqueue_rtr(union ctl_io *io); 437static void ctl_enqueue_done(union ctl_io *io); 438static void ctl_enqueue_isc(union ctl_io *io); 439static const struct ctl_cmd_entry * 440 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 441static const struct ctl_cmd_entry * 442 ctl_validate_command(struct ctl_scsiio *ctsio); 443static int ctl_cmd_applicable(uint8_t lun_type, 444 const struct ctl_cmd_entry *entry); 445 446/* 447 * Load the serialization table. This isn't very pretty, but is probably 448 * the easiest way to do it. 449 */ 450#include "ctl_ser_table.c" 451 452/* 453 * We only need to define open, close and ioctl routines for this driver. 454 */ 455static struct cdevsw ctl_cdevsw = { 456 .d_version = D_VERSION, 457 .d_flags = 0, 458 .d_open = ctl_open, 459 .d_close = ctl_close, 460 .d_ioctl = ctl_ioctl, 461 .d_name = "ctl", 462}; 463 464 465MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 466MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 467 468static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 469 470static moduledata_t ctl_moduledata = { 471 "ctl", 472 ctl_module_event_handler, 473 NULL 474}; 475 476DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 477MODULE_VERSION(ctl, 1); 478 479static struct ctl_frontend ioctl_frontend = 480{ 481 .name = "ioctl", 482}; 483 484static void 485ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 486 union ctl_ha_msg *msg_info) 487{ 488 struct ctl_scsiio *ctsio; 489 490 if (msg_info->hdr.original_sc == NULL) { 491 printf("%s: original_sc == NULL!\n", __func__); 492 /* XXX KDM now what? */ 493 return; 494 } 495 496 ctsio = &msg_info->hdr.original_sc->scsiio; 497 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 498 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 499 ctsio->io_hdr.status = msg_info->hdr.status; 500 ctsio->scsi_status = msg_info->scsi.scsi_status; 501 ctsio->sense_len = msg_info->scsi.sense_len; 502 ctsio->sense_residual = msg_info->scsi.sense_residual; 503 ctsio->residual = msg_info->scsi.residual; 504 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 505 sizeof(ctsio->sense_data)); 506 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 507 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 508 ctl_enqueue_isc((union ctl_io *)ctsio); 509} 510 511static void 512ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 513 union ctl_ha_msg *msg_info) 514{ 515 struct ctl_scsiio *ctsio; 516 517 if (msg_info->hdr.serializing_sc == NULL) { 518 printf("%s: serializing_sc == NULL!\n", __func__); 519 /* XXX KDM now what? */ 520 return; 521 } 522 523 ctsio = &msg_info->hdr.serializing_sc->scsiio; 524#if 0 525 /* 526 * Attempt to catch the situation where an I/O has 527 * been freed, and we're using it again. 528 */ 529 if (ctsio->io_hdr.io_type == 0xff) { 530 union ctl_io *tmp_io; 531 tmp_io = (union ctl_io *)ctsio; 532 printf("%s: %p use after free!\n", __func__, 533 ctsio); 534 printf("%s: type %d msg %d cdb %x iptl: " 535 "%d:%d:%d:%d tag 0x%04x " 536 "flag %#x status %x\n", 537 __func__, 538 tmp_io->io_hdr.io_type, 539 tmp_io->io_hdr.msg_type, 540 tmp_io->scsiio.cdb[0], 541 tmp_io->io_hdr.nexus.initid.id, 542 tmp_io->io_hdr.nexus.targ_port, 543 tmp_io->io_hdr.nexus.targ_target.id, 544 tmp_io->io_hdr.nexus.targ_lun, 545 (tmp_io->io_hdr.io_type == 546 CTL_IO_TASK) ? 547 tmp_io->taskio.tag_num : 548 tmp_io->scsiio.tag_num, 549 tmp_io->io_hdr.flags, 550 tmp_io->io_hdr.status); 551 } 552#endif 553 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 554 ctl_enqueue_isc((union ctl_io *)ctsio); 555} 556 557/* 558 * ISC (Inter Shelf Communication) event handler. Events from the HA 559 * subsystem come in here. 560 */ 561static void 562ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 563{ 564 struct ctl_softc *ctl_softc; 565 union ctl_io *io; 566 struct ctl_prio *presio; 567 ctl_ha_status isc_status; 568 569 ctl_softc = control_softc; 570 io = NULL; 571 572 573#if 0 574 printf("CTL: Isc Msg event %d\n", event); 575#endif 576 if (event == CTL_HA_EVT_MSG_RECV) { 577 union ctl_ha_msg msg_info; 578 579 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 580 sizeof(msg_info), /*wait*/ 0); 581#if 0 582 printf("CTL: msg_type %d\n", msg_info.msg_type); 583#endif 584 if (isc_status != 0) { 585 printf("Error receiving message, status = %d\n", 586 isc_status); 587 return; 588 } 589 590 switch (msg_info.hdr.msg_type) { 591 case CTL_MSG_SERIALIZE: 592#if 0 593 printf("Serialize\n"); 594#endif 595 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 596 if (io == NULL) { 597 printf("ctl_isc_event_handler: can't allocate " 598 "ctl_io!\n"); 599 /* Bad Juju */ 600 /* Need to set busy and send msg back */ 601 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 602 msg_info.hdr.status = CTL_SCSI_ERROR; 603 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 604 msg_info.scsi.sense_len = 0; 605 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 606 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 607 } 608 goto bailout; 609 } 610 ctl_zero_io(io); 611 // populate ctsio from msg_info 612 io->io_hdr.io_type = CTL_IO_SCSI; 613 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 614 io->io_hdr.original_sc = msg_info.hdr.original_sc; 615#if 0 616 printf("pOrig %x\n", (int)msg_info.original_sc); 617#endif 618 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 619 CTL_FLAG_IO_ACTIVE; 620 /* 621 * If we're in serialization-only mode, we don't 622 * want to go through full done processing. Thus 623 * the COPY flag. 624 * 625 * XXX KDM add another flag that is more specific. 626 */ 627 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 628 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 629 io->io_hdr.nexus = msg_info.hdr.nexus; 630#if 0 631 printf("targ %d, port %d, iid %d, lun %d\n", 632 io->io_hdr.nexus.targ_target.id, 633 io->io_hdr.nexus.targ_port, 634 io->io_hdr.nexus.initid.id, 635 io->io_hdr.nexus.targ_lun); 636#endif 637 io->scsiio.tag_num = msg_info.scsi.tag_num; 638 io->scsiio.tag_type = msg_info.scsi.tag_type; 639 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 640 CTL_MAX_CDBLEN); 641 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 642 const struct ctl_cmd_entry *entry; 643 644 entry = ctl_get_cmd_entry(&io->scsiio); 645 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 646 io->io_hdr.flags |= 647 entry->flags & CTL_FLAG_DATA_MASK; 648 } 649 ctl_enqueue_isc(io); 650 break; 651 652 /* Performed on the Originating SC, XFER mode only */ 653 case CTL_MSG_DATAMOVE: { 654 struct ctl_sg_entry *sgl; 655 int i, j; 656 657 io = msg_info.hdr.original_sc; 658 if (io == NULL) { 659 printf("%s: original_sc == NULL!\n", __func__); 660 /* XXX KDM do something here */ 661 break; 662 } 663 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 664 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 665 /* 666 * Keep track of this, we need to send it back over 667 * when the datamove is complete. 668 */ 669 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 670 671 if (msg_info.dt.sg_sequence == 0) { 672 /* 673 * XXX KDM we use the preallocated S/G list 674 * here, but we'll need to change this to 675 * dynamic allocation if we need larger S/G 676 * lists. 677 */ 678 if (msg_info.dt.kern_sg_entries > 679 sizeof(io->io_hdr.remote_sglist) / 680 sizeof(io->io_hdr.remote_sglist[0])) { 681 printf("%s: number of S/G entries " 682 "needed %u > allocated num %zd\n", 683 __func__, 684 msg_info.dt.kern_sg_entries, 685 sizeof(io->io_hdr.remote_sglist)/ 686 sizeof(io->io_hdr.remote_sglist[0])); 687 688 /* 689 * XXX KDM send a message back to 690 * the other side to shut down the 691 * DMA. The error will come back 692 * through via the normal channel. 693 */ 694 break; 695 } 696 sgl = io->io_hdr.remote_sglist; 697 memset(sgl, 0, 698 sizeof(io->io_hdr.remote_sglist)); 699 700 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 701 702 io->scsiio.kern_sg_entries = 703 msg_info.dt.kern_sg_entries; 704 io->scsiio.rem_sg_entries = 705 msg_info.dt.kern_sg_entries; 706 io->scsiio.kern_data_len = 707 msg_info.dt.kern_data_len; 708 io->scsiio.kern_total_len = 709 msg_info.dt.kern_total_len; 710 io->scsiio.kern_data_resid = 711 msg_info.dt.kern_data_resid; 712 io->scsiio.kern_rel_offset = 713 msg_info.dt.kern_rel_offset; 714 /* 715 * Clear out per-DMA flags. 716 */ 717 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 718 /* 719 * Add per-DMA flags that are set for this 720 * particular DMA request. 721 */ 722 io->io_hdr.flags |= msg_info.dt.flags & 723 CTL_FLAG_RDMA_MASK; 724 } else 725 sgl = (struct ctl_sg_entry *) 726 io->scsiio.kern_data_ptr; 727 728 for (i = msg_info.dt.sent_sg_entries, j = 0; 729 i < (msg_info.dt.sent_sg_entries + 730 msg_info.dt.cur_sg_entries); i++, j++) { 731 sgl[i].addr = msg_info.dt.sg_list[j].addr; 732 sgl[i].len = msg_info.dt.sg_list[j].len; 733 734#if 0 735 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 736 __func__, 737 msg_info.dt.sg_list[j].addr, 738 msg_info.dt.sg_list[j].len, 739 sgl[i].addr, sgl[i].len, j, i); 740#endif 741 } 742#if 0 743 memcpy(&sgl[msg_info.dt.sent_sg_entries], 744 msg_info.dt.sg_list, 745 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 746#endif 747 748 /* 749 * If this is the last piece of the I/O, we've got 750 * the full S/G list. Queue processing in the thread. 751 * Otherwise wait for the next piece. 752 */ 753 if (msg_info.dt.sg_last != 0) 754 ctl_enqueue_isc(io); 755 break; 756 } 757 /* Performed on the Serializing (primary) SC, XFER mode only */ 758 case CTL_MSG_DATAMOVE_DONE: { 759 if (msg_info.hdr.serializing_sc == NULL) { 760 printf("%s: serializing_sc == NULL!\n", 761 __func__); 762 /* XXX KDM now what? */ 763 break; 764 } 765 /* 766 * We grab the sense information here in case 767 * there was a failure, so we can return status 768 * back to the initiator. 769 */ 770 io = msg_info.hdr.serializing_sc; 771 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 772 io->io_hdr.status = msg_info.hdr.status; 773 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 774 io->scsiio.sense_len = msg_info.scsi.sense_len; 775 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 776 io->io_hdr.port_status = msg_info.scsi.fetd_status; 777 io->scsiio.residual = msg_info.scsi.residual; 778 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 779 sizeof(io->scsiio.sense_data)); 780 ctl_enqueue_isc(io); 781 break; 782 } 783 784 /* Preformed on Originating SC, SER_ONLY mode */ 785 case CTL_MSG_R2R: 786 io = msg_info.hdr.original_sc; 787 if (io == NULL) { 788 printf("%s: Major Bummer\n", __func__); 789 return; 790 } else { 791#if 0 792 printf("pOrig %x\n",(int) ctsio); 793#endif 794 } 795 io->io_hdr.msg_type = CTL_MSG_R2R; 796 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 797 ctl_enqueue_isc(io); 798 break; 799 800 /* 801 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 802 * mode. 803 * Performed on the Originating (i.e. secondary) SC in XFER 804 * mode 805 */ 806 case CTL_MSG_FINISH_IO: 807 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 808 ctl_isc_handler_finish_xfer(ctl_softc, 809 &msg_info); 810 else 811 ctl_isc_handler_finish_ser_only(ctl_softc, 812 &msg_info); 813 break; 814 815 /* Preformed on Originating SC */ 816 case CTL_MSG_BAD_JUJU: 817 io = msg_info.hdr.original_sc; 818 if (io == NULL) { 819 printf("%s: Bad JUJU!, original_sc is NULL!\n", 820 __func__); 821 break; 822 } 823 ctl_copy_sense_data(&msg_info, io); 824 /* 825 * IO should have already been cleaned up on other 826 * SC so clear this flag so we won't send a message 827 * back to finish the IO there. 828 */ 829 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 830 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 831 832 /* io = msg_info.hdr.serializing_sc; */ 833 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 834 ctl_enqueue_isc(io); 835 break; 836 837 /* Handle resets sent from the other side */ 838 case CTL_MSG_MANAGE_TASKS: { 839 struct ctl_taskio *taskio; 840 taskio = (struct ctl_taskio *)ctl_alloc_io( 841 (void *)ctl_softc->othersc_pool); 842 if (taskio == NULL) { 843 printf("ctl_isc_event_handler: can't allocate " 844 "ctl_io!\n"); 845 /* Bad Juju */ 846 /* should I just call the proper reset func 847 here??? */ 848 goto bailout; 849 } 850 ctl_zero_io((union ctl_io *)taskio); 851 taskio->io_hdr.io_type = CTL_IO_TASK; 852 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 853 taskio->io_hdr.nexus = msg_info.hdr.nexus; 854 taskio->task_action = msg_info.task.task_action; 855 taskio->tag_num = msg_info.task.tag_num; 856 taskio->tag_type = msg_info.task.tag_type; 857#ifdef CTL_TIME_IO 858 taskio->io_hdr.start_time = time_uptime; 859 getbintime(&taskio->io_hdr.start_bt); 860#if 0 861 cs_prof_gettime(&taskio->io_hdr.start_ticks); 862#endif 863#endif /* CTL_TIME_IO */ 864 ctl_run_task((union ctl_io *)taskio); 865 break; 866 } 867 /* Persistent Reserve action which needs attention */ 868 case CTL_MSG_PERS_ACTION: 869 presio = (struct ctl_prio *)ctl_alloc_io( 870 (void *)ctl_softc->othersc_pool); 871 if (presio == NULL) { 872 printf("ctl_isc_event_handler: can't allocate " 873 "ctl_io!\n"); 874 /* Bad Juju */ 875 /* Need to set busy and send msg back */ 876 goto bailout; 877 } 878 ctl_zero_io((union ctl_io *)presio); 879 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 880 presio->pr_msg = msg_info.pr; 881 ctl_enqueue_isc((union ctl_io *)presio); 882 break; 883 case CTL_MSG_SYNC_FE: 884 rcv_sync_msg = 1; 885 break; 886 case CTL_MSG_APS_LOCK: { 887 // It's quicker to execute this then to 888 // queue it. 889 struct ctl_lun *lun; 890 struct ctl_page_index *page_index; 891 struct copan_aps_subpage *current_sp; 892 uint32_t targ_lun; 893 894 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 895 lun = ctl_softc->ctl_luns[targ_lun]; 896 mtx_lock(&lun->lun_lock); 897 page_index = &lun->mode_pages.index[index_to_aps_page]; 898 current_sp = (struct copan_aps_subpage *) 899 (page_index->page_data + 900 (page_index->page_len * CTL_PAGE_CURRENT)); 901 902 current_sp->lock_active = msg_info.aps.lock_flag; 903 mtx_unlock(&lun->lun_lock); 904 break; 905 } 906 default: 907 printf("How did I get here?\n"); 908 } 909 } else if (event == CTL_HA_EVT_MSG_SENT) { 910 if (param != CTL_HA_STATUS_SUCCESS) { 911 printf("Bad status from ctl_ha_msg_send status %d\n", 912 param); 913 } 914 return; 915 } else if (event == CTL_HA_EVT_DISCONNECT) { 916 printf("CTL: Got a disconnect from Isc\n"); 917 return; 918 } else { 919 printf("ctl_isc_event_handler: Unknown event %d\n", event); 920 return; 921 } 922 923bailout: 924 return; 925} 926 927static void 928ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 929{ 930 struct scsi_sense_data *sense; 931 932 sense = &dest->scsiio.sense_data; 933 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 934 dest->scsiio.scsi_status = src->scsi.scsi_status; 935 dest->scsiio.sense_len = src->scsi.sense_len; 936 dest->io_hdr.status = src->hdr.status; 937} 938 939static int 940ctl_init(void) 941{ 942 struct ctl_softc *softc; 943 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 944 struct ctl_port *port; 945 uint8_t sc_id =0; 946 int i, error, retval; 947 //int isc_retval; 948 949 retval = 0; 950 ctl_pause_rtr = 0; 951 rcv_sync_msg = 0; 952 953 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 954 M_WAITOK | M_ZERO); 955 softc = control_softc; 956 957 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 958 "cam/ctl"); 959 960 softc->dev->si_drv1 = softc; 961 962 /* 963 * By default, return a "bad LUN" peripheral qualifier for unknown 964 * LUNs. The user can override this default using the tunable or 965 * sysctl. See the comment in ctl_inquiry_std() for more details. 966 */ 967 softc->inquiry_pq_no_lun = 1; 968 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 969 &softc->inquiry_pq_no_lun); 970 sysctl_ctx_init(&softc->sysctl_ctx); 971 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 972 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 973 CTLFLAG_RD, 0, "CAM Target Layer"); 974 975 if (softc->sysctl_tree == NULL) { 976 printf("%s: unable to allocate sysctl tree\n", __func__); 977 destroy_dev(softc->dev); 978 free(control_softc, M_DEVBUF); 979 control_softc = NULL; 980 return (ENOMEM); 981 } 982 983 SYSCTL_ADD_INT(&softc->sysctl_ctx, 984 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 985 "inquiry_pq_no_lun", CTLFLAG_RW, 986 &softc->inquiry_pq_no_lun, 0, 987 "Report no lun possible for invalid LUNs"); 988 989 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 990 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 991 softc->open_count = 0; 992 993 /* 994 * Default to actually sending a SYNCHRONIZE CACHE command down to 995 * the drive. 996 */ 997 softc->flags = CTL_FLAG_REAL_SYNC; 998 999 /* 1000 * In Copan's HA scheme, the "master" and "slave" roles are 1001 * figured out through the slot the controller is in. Although it 1002 * is an active/active system, someone has to be in charge. 1003 */ 1004#ifdef NEEDTOPORT 1005 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1006#endif 1007 1008 if (sc_id == 0) { 1009 softc->flags |= CTL_FLAG_MASTER_SHELF; 1010 persis_offset = 0; 1011 } else 1012 persis_offset = CTL_MAX_INITIATORS; 1013 1014 /* 1015 * XXX KDM need to figure out where we want to get our target ID 1016 * and WWID. Is it different on each port? 1017 */ 1018 softc->target.id = 0; 1019 softc->target.wwid[0] = 0x12345678; 1020 softc->target.wwid[1] = 0x87654321; 1021 STAILQ_INIT(&softc->lun_list); 1022 STAILQ_INIT(&softc->pending_lun_queue); 1023 STAILQ_INIT(&softc->fe_list); 1024 STAILQ_INIT(&softc->port_list); 1025 STAILQ_INIT(&softc->be_list); 1026 STAILQ_INIT(&softc->io_pools); 1027 ctl_tpc_init(softc); 1028 1029 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1030 &internal_pool)!= 0){ 1031 printf("ctl: can't allocate %d entry internal pool, " 1032 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1033 return (ENOMEM); 1034 } 1035 1036 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1037 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1038 printf("ctl: can't allocate %d entry emergency pool, " 1039 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1040 ctl_pool_free(internal_pool); 1041 return (ENOMEM); 1042 } 1043 1044 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1045 &other_pool) != 0) 1046 { 1047 printf("ctl: can't allocate %d entry other SC pool, " 1048 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1049 ctl_pool_free(internal_pool); 1050 ctl_pool_free(emergency_pool); 1051 return (ENOMEM); 1052 } 1053 1054 softc->internal_pool = internal_pool; 1055 softc->emergency_pool = emergency_pool; 1056 softc->othersc_pool = other_pool; 1057 1058 if (worker_threads <= 0) 1059 worker_threads = max(1, mp_ncpus / 4); 1060 if (worker_threads > CTL_MAX_THREADS) 1061 worker_threads = CTL_MAX_THREADS; 1062 1063 for (i = 0; i < worker_threads; i++) { 1064 struct ctl_thread *thr = &softc->threads[i]; 1065 1066 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1067 thr->ctl_softc = softc; 1068 STAILQ_INIT(&thr->incoming_queue); 1069 STAILQ_INIT(&thr->rtr_queue); 1070 STAILQ_INIT(&thr->done_queue); 1071 STAILQ_INIT(&thr->isc_queue); 1072 1073 error = kproc_kthread_add(ctl_work_thread, thr, 1074 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1075 if (error != 0) { 1076 printf("error creating CTL work thread!\n"); 1077 ctl_pool_free(internal_pool); 1078 ctl_pool_free(emergency_pool); 1079 ctl_pool_free(other_pool); 1080 return (error); 1081 } 1082 } 1083 error = kproc_kthread_add(ctl_lun_thread, softc, 1084 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1085 if (error != 0) { 1086 printf("error creating CTL lun thread!\n"); 1087 ctl_pool_free(internal_pool); 1088 ctl_pool_free(emergency_pool); 1089 ctl_pool_free(other_pool); 1090 return (error); 1091 } 1092 if (bootverbose) 1093 printf("ctl: CAM Target Layer loaded\n"); 1094 1095 /* 1096 * Initialize the ioctl front end. 1097 */ 1098 ctl_frontend_register(&ioctl_frontend); 1099 port = &softc->ioctl_info.port; 1100 port->frontend = &ioctl_frontend; 1101 sprintf(softc->ioctl_info.port_name, "ioctl"); 1102 port->port_type = CTL_PORT_IOCTL; 1103 port->num_requested_ctl_io = 100; 1104 port->port_name = softc->ioctl_info.port_name; 1105 port->port_online = ctl_ioctl_online; 1106 port->port_offline = ctl_ioctl_offline; 1107 port->onoff_arg = &softc->ioctl_info; 1108 port->lun_enable = ctl_ioctl_lun_enable; 1109 port->lun_disable = ctl_ioctl_lun_disable; 1110 port->targ_lun_arg = &softc->ioctl_info; 1111 port->fe_datamove = ctl_ioctl_datamove; 1112 port->fe_done = ctl_ioctl_done; 1113 port->max_targets = 15; 1114 port->max_target_id = 15; 1115 1116 if (ctl_port_register(&softc->ioctl_info.port, 1117 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1118 printf("ctl: ioctl front end registration failed, will " 1119 "continue anyway\n"); 1120 } 1121 1122#ifdef CTL_IO_DELAY 1123 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1124 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1125 sizeof(struct callout), CTL_TIMER_BYTES); 1126 return (EINVAL); 1127 } 1128#endif /* CTL_IO_DELAY */ 1129 1130 return (0); 1131} 1132 1133void 1134ctl_shutdown(void) 1135{ 1136 struct ctl_softc *softc; 1137 struct ctl_lun *lun, *next_lun; 1138 struct ctl_io_pool *pool; 1139 1140 softc = (struct ctl_softc *)control_softc; 1141 1142 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1143 printf("ctl: ioctl front end deregistration failed\n"); 1144 1145 mtx_lock(&softc->ctl_lock); 1146 1147 /* 1148 * Free up each LUN. 1149 */ 1150 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1151 next_lun = STAILQ_NEXT(lun, links); 1152 ctl_free_lun(lun); 1153 } 1154 1155 mtx_unlock(&softc->ctl_lock); 1156 1157 ctl_frontend_deregister(&ioctl_frontend); 1158 1159 /* 1160 * This will rip the rug out from under any FETDs or anyone else 1161 * that has a pool allocated. Since we increment our module 1162 * refcount any time someone outside the main CTL module allocates 1163 * a pool, we shouldn't have any problems here. The user won't be 1164 * able to unload the CTL module until client modules have 1165 * successfully unloaded. 1166 */ 1167 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1168 ctl_pool_free(pool); 1169 1170#if 0 1171 ctl_shutdown_thread(softc->work_thread); 1172 mtx_destroy(&softc->queue_lock); 1173#endif 1174 1175 ctl_tpc_shutdown(softc); 1176 mtx_destroy(&softc->pool_lock); 1177 mtx_destroy(&softc->ctl_lock); 1178 1179 destroy_dev(softc->dev); 1180 1181 sysctl_ctx_free(&softc->sysctl_ctx); 1182 1183 free(control_softc, M_DEVBUF); 1184 control_softc = NULL; 1185 1186 if (bootverbose) 1187 printf("ctl: CAM Target Layer unloaded\n"); 1188} 1189 1190static int 1191ctl_module_event_handler(module_t mod, int what, void *arg) 1192{ 1193 1194 switch (what) { 1195 case MOD_LOAD: 1196 return (ctl_init()); 1197 case MOD_UNLOAD: 1198 return (EBUSY); 1199 default: 1200 return (EOPNOTSUPP); 1201 } 1202} 1203 1204/* 1205 * XXX KDM should we do some access checks here? Bump a reference count to 1206 * prevent a CTL module from being unloaded while someone has it open? 1207 */ 1208static int 1209ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1210{ 1211 return (0); 1212} 1213 1214static int 1215ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1216{ 1217 return (0); 1218} 1219 1220int 1221ctl_port_enable(ctl_port_type port_type) 1222{ 1223 struct ctl_softc *softc; 1224 struct ctl_port *port; 1225 1226 if (ctl_is_single == 0) { 1227 union ctl_ha_msg msg_info; 1228 int isc_retval; 1229 1230#if 0 1231 printf("%s: HA mode, synchronizing frontend enable\n", 1232 __func__); 1233#endif 1234 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1235 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1236 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1237 printf("Sync msg send error retval %d\n", isc_retval); 1238 } 1239 if (!rcv_sync_msg) { 1240 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1241 sizeof(msg_info), 1); 1242 } 1243#if 0 1244 printf("CTL:Frontend Enable\n"); 1245 } else { 1246 printf("%s: single mode, skipping frontend synchronization\n", 1247 __func__); 1248#endif 1249 } 1250 1251 softc = control_softc; 1252 1253 STAILQ_FOREACH(port, &softc->port_list, links) { 1254 if (port_type & port->port_type) 1255 { 1256#if 0 1257 printf("port %d\n", port->targ_port); 1258#endif 1259 ctl_port_online(port); 1260 } 1261 } 1262 1263 return (0); 1264} 1265 1266int 1267ctl_port_disable(ctl_port_type port_type) 1268{ 1269 struct ctl_softc *softc; 1270 struct ctl_port *port; 1271 1272 softc = control_softc; 1273 1274 STAILQ_FOREACH(port, &softc->port_list, links) { 1275 if (port_type & port->port_type) 1276 ctl_port_offline(port); 1277 } 1278 1279 return (0); 1280} 1281 1282/* 1283 * Returns 0 for success, 1 for failure. 1284 * Currently the only failure mode is if there aren't enough entries 1285 * allocated. So, in case of a failure, look at num_entries_dropped, 1286 * reallocate and try again. 1287 */ 1288int 1289ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1290 int *num_entries_filled, int *num_entries_dropped, 1291 ctl_port_type port_type, int no_virtual) 1292{ 1293 struct ctl_softc *softc; 1294 struct ctl_port *port; 1295 int entries_dropped, entries_filled; 1296 int retval; 1297 int i; 1298 1299 softc = control_softc; 1300 1301 retval = 0; 1302 entries_filled = 0; 1303 entries_dropped = 0; 1304 1305 i = 0; 1306 mtx_lock(&softc->ctl_lock); 1307 STAILQ_FOREACH(port, &softc->port_list, links) { 1308 struct ctl_port_entry *entry; 1309 1310 if ((port->port_type & port_type) == 0) 1311 continue; 1312 1313 if ((no_virtual != 0) 1314 && (port->virtual_port != 0)) 1315 continue; 1316 1317 if (entries_filled >= num_entries_alloced) { 1318 entries_dropped++; 1319 continue; 1320 } 1321 entry = &entries[i]; 1322 1323 entry->port_type = port->port_type; 1324 strlcpy(entry->port_name, port->port_name, 1325 sizeof(entry->port_name)); 1326 entry->physical_port = port->physical_port; 1327 entry->virtual_port = port->virtual_port; 1328 entry->wwnn = port->wwnn; 1329 entry->wwpn = port->wwpn; 1330 1331 i++; 1332 entries_filled++; 1333 } 1334 1335 mtx_unlock(&softc->ctl_lock); 1336 1337 if (entries_dropped > 0) 1338 retval = 1; 1339 1340 *num_entries_dropped = entries_dropped; 1341 *num_entries_filled = entries_filled; 1342 1343 return (retval); 1344} 1345 1346static void 1347ctl_ioctl_online(void *arg) 1348{ 1349 struct ctl_ioctl_info *ioctl_info; 1350 1351 ioctl_info = (struct ctl_ioctl_info *)arg; 1352 1353 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1354} 1355 1356static void 1357ctl_ioctl_offline(void *arg) 1358{ 1359 struct ctl_ioctl_info *ioctl_info; 1360 1361 ioctl_info = (struct ctl_ioctl_info *)arg; 1362 1363 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1364} 1365 1366/* 1367 * Remove an initiator by port number and initiator ID. 1368 * Returns 0 for success, -1 for failure. 1369 */ 1370int 1371ctl_remove_initiator(struct ctl_port *port, int iid) 1372{ 1373 struct ctl_softc *softc = control_softc; 1374 1375 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1376 1377 if (iid > CTL_MAX_INIT_PER_PORT) { 1378 printf("%s: initiator ID %u > maximun %u!\n", 1379 __func__, iid, CTL_MAX_INIT_PER_PORT); 1380 return (-1); 1381 } 1382 1383 mtx_lock(&softc->ctl_lock); 1384 port->wwpn_iid[iid].in_use--; 1385 port->wwpn_iid[iid].last_use = time_uptime; 1386 mtx_unlock(&softc->ctl_lock); 1387 1388 return (0); 1389} 1390 1391/* 1392 * Add an initiator to the initiator map. 1393 * Returns iid for success, < 0 for failure. 1394 */ 1395int 1396ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1397{ 1398 struct ctl_softc *softc = control_softc; 1399 time_t best_time; 1400 int i, best; 1401 1402 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1403 1404 if (iid >= CTL_MAX_INIT_PER_PORT) { 1405 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1406 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1407 free(name, M_CTL); 1408 return (-1); 1409 } 1410 1411 mtx_lock(&softc->ctl_lock); 1412 1413 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1414 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1415 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1416 iid = i; 1417 break; 1418 } 1419 if (name != NULL && port->wwpn_iid[i].name != NULL && 1420 strcmp(name, port->wwpn_iid[i].name) == 0) { 1421 iid = i; 1422 break; 1423 } 1424 } 1425 } 1426 1427 if (iid < 0) { 1428 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1429 if (port->wwpn_iid[i].in_use == 0 && 1430 port->wwpn_iid[i].wwpn == 0 && 1431 port->wwpn_iid[i].name == NULL) { 1432 iid = i; 1433 break; 1434 } 1435 } 1436 } 1437 1438 if (iid < 0) { 1439 best = -1; 1440 best_time = INT32_MAX; 1441 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1442 if (port->wwpn_iid[i].in_use == 0) { 1443 if (port->wwpn_iid[i].last_use < best_time) { 1444 best = i; 1445 best_time = port->wwpn_iid[i].last_use; 1446 } 1447 } 1448 } 1449 iid = best; 1450 } 1451 1452 if (iid < 0) { 1453 mtx_unlock(&softc->ctl_lock); 1454 free(name, M_CTL); 1455 return (-2); 1456 } 1457 1458 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1459 /* 1460 * This is not an error yet. 1461 */ 1462 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1463#if 0 1464 printf("%s: port %d iid %u WWPN %#jx arrived" 1465 " again\n", __func__, port->targ_port, 1466 iid, (uintmax_t)wwpn); 1467#endif 1468 goto take; 1469 } 1470 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1471 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1472#if 0 1473 printf("%s: port %d iid %u name '%s' arrived" 1474 " again\n", __func__, port->targ_port, 1475 iid, name); 1476#endif 1477 goto take; 1478 } 1479 1480 /* 1481 * This is an error, but what do we do about it? The 1482 * driver is telling us we have a new WWPN for this 1483 * initiator ID, so we pretty much need to use it. 1484 */ 1485 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1486 " but WWPN %#jx '%s' is still at that address\n", 1487 __func__, port->targ_port, iid, wwpn, name, 1488 (uintmax_t)port->wwpn_iid[iid].wwpn, 1489 port->wwpn_iid[iid].name); 1490 1491 /* 1492 * XXX KDM clear have_ca and ua_pending on each LUN for 1493 * this initiator. 1494 */ 1495 } 1496take: 1497 free(port->wwpn_iid[iid].name, M_CTL); 1498 port->wwpn_iid[iid].name = name; 1499 port->wwpn_iid[iid].wwpn = wwpn; 1500 port->wwpn_iid[iid].in_use++; 1501 mtx_unlock(&softc->ctl_lock); 1502 1503 return (iid); 1504} 1505 1506static int 1507ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1508{ 1509 int len; 1510 1511 switch (port->port_type) { 1512 case CTL_PORT_FC: 1513 { 1514 struct scsi_transportid_fcp *id = 1515 (struct scsi_transportid_fcp *)buf; 1516 if (port->wwpn_iid[iid].wwpn == 0) 1517 return (0); 1518 memset(id, 0, sizeof(*id)); 1519 id->format_protocol = SCSI_PROTO_FC; 1520 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1521 return (sizeof(*id)); 1522 } 1523 case CTL_PORT_ISCSI: 1524 { 1525 struct scsi_transportid_iscsi_port *id = 1526 (struct scsi_transportid_iscsi_port *)buf; 1527 if (port->wwpn_iid[iid].name == NULL) 1528 return (0); 1529 memset(id, 0, 256); 1530 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1531 SCSI_PROTO_ISCSI; 1532 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1533 len = roundup2(min(len, 252), 4); 1534 scsi_ulto2b(len, id->additional_length); 1535 return (sizeof(*id) + len); 1536 } 1537 case CTL_PORT_SAS: 1538 { 1539 struct scsi_transportid_sas *id = 1540 (struct scsi_transportid_sas *)buf; 1541 if (port->wwpn_iid[iid].wwpn == 0) 1542 return (0); 1543 memset(id, 0, sizeof(*id)); 1544 id->format_protocol = SCSI_PROTO_SAS; 1545 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1546 return (sizeof(*id)); 1547 } 1548 default: 1549 { 1550 struct scsi_transportid_spi *id = 1551 (struct scsi_transportid_spi *)buf; 1552 memset(id, 0, sizeof(*id)); 1553 id->format_protocol = SCSI_PROTO_SPI; 1554 scsi_ulto2b(iid, id->scsi_addr); 1555 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1556 return (sizeof(*id)); 1557 } 1558 } 1559} 1560 1561static int 1562ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1563{ 1564 return (0); 1565} 1566 1567static int 1568ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1569{ 1570 return (0); 1571} 1572 1573/* 1574 * Data movement routine for the CTL ioctl frontend port. 1575 */ 1576static int 1577ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1578{ 1579 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1580 struct ctl_sg_entry ext_entry, kern_entry; 1581 int ext_sglen, ext_sg_entries, kern_sg_entries; 1582 int ext_sg_start, ext_offset; 1583 int len_to_copy, len_copied; 1584 int kern_watermark, ext_watermark; 1585 int ext_sglist_malloced; 1586 int i, j; 1587 1588 ext_sglist_malloced = 0; 1589 ext_sg_start = 0; 1590 ext_offset = 0; 1591 1592 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1593 1594 /* 1595 * If this flag is set, fake the data transfer. 1596 */ 1597 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1598 ctsio->ext_data_filled = ctsio->ext_data_len; 1599 goto bailout; 1600 } 1601 1602 /* 1603 * To simplify things here, if we have a single buffer, stick it in 1604 * a S/G entry and just make it a single entry S/G list. 1605 */ 1606 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1607 int len_seen; 1608 1609 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1610 1611 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1612 M_WAITOK); 1613 ext_sglist_malloced = 1; 1614 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1615 ext_sglen) != 0) { 1616 ctl_set_internal_failure(ctsio, 1617 /*sks_valid*/ 0, 1618 /*retry_count*/ 0); 1619 goto bailout; 1620 } 1621 ext_sg_entries = ctsio->ext_sg_entries; 1622 len_seen = 0; 1623 for (i = 0; i < ext_sg_entries; i++) { 1624 if ((len_seen + ext_sglist[i].len) >= 1625 ctsio->ext_data_filled) { 1626 ext_sg_start = i; 1627 ext_offset = ctsio->ext_data_filled - len_seen; 1628 break; 1629 } 1630 len_seen += ext_sglist[i].len; 1631 } 1632 } else { 1633 ext_sglist = &ext_entry; 1634 ext_sglist->addr = ctsio->ext_data_ptr; 1635 ext_sglist->len = ctsio->ext_data_len; 1636 ext_sg_entries = 1; 1637 ext_sg_start = 0; 1638 ext_offset = ctsio->ext_data_filled; 1639 } 1640 1641 if (ctsio->kern_sg_entries > 0) { 1642 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1643 kern_sg_entries = ctsio->kern_sg_entries; 1644 } else { 1645 kern_sglist = &kern_entry; 1646 kern_sglist->addr = ctsio->kern_data_ptr; 1647 kern_sglist->len = ctsio->kern_data_len; 1648 kern_sg_entries = 1; 1649 } 1650 1651 1652 kern_watermark = 0; 1653 ext_watermark = ext_offset; 1654 len_copied = 0; 1655 for (i = ext_sg_start, j = 0; 1656 i < ext_sg_entries && j < kern_sg_entries;) { 1657 uint8_t *ext_ptr, *kern_ptr; 1658 1659 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1660 kern_sglist[j].len - kern_watermark); 1661 1662 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1663 ext_ptr = ext_ptr + ext_watermark; 1664 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1665 /* 1666 * XXX KDM fix this! 1667 */ 1668 panic("need to implement bus address support"); 1669#if 0 1670 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1671#endif 1672 } else 1673 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1674 kern_ptr = kern_ptr + kern_watermark; 1675 1676 kern_watermark += len_to_copy; 1677 ext_watermark += len_to_copy; 1678 1679 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1680 CTL_FLAG_DATA_IN) { 1681 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1682 "bytes to user\n", len_to_copy)); 1683 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1684 "to %p\n", kern_ptr, ext_ptr)); 1685 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1686 ctl_set_internal_failure(ctsio, 1687 /*sks_valid*/ 0, 1688 /*retry_count*/ 0); 1689 goto bailout; 1690 } 1691 } else { 1692 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1693 "bytes from user\n", len_to_copy)); 1694 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1695 "to %p\n", ext_ptr, kern_ptr)); 1696 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1697 ctl_set_internal_failure(ctsio, 1698 /*sks_valid*/ 0, 1699 /*retry_count*/0); 1700 goto bailout; 1701 } 1702 } 1703 1704 len_copied += len_to_copy; 1705 1706 if (ext_sglist[i].len == ext_watermark) { 1707 i++; 1708 ext_watermark = 0; 1709 } 1710 1711 if (kern_sglist[j].len == kern_watermark) { 1712 j++; 1713 kern_watermark = 0; 1714 } 1715 } 1716 1717 ctsio->ext_data_filled += len_copied; 1718 1719 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1720 "kern_sg_entries: %d\n", ext_sg_entries, 1721 kern_sg_entries)); 1722 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1723 "kern_data_len = %d\n", ctsio->ext_data_len, 1724 ctsio->kern_data_len)); 1725 1726 1727 /* XXX KDM set residual?? */ 1728bailout: 1729 1730 if (ext_sglist_malloced != 0) 1731 free(ext_sglist, M_CTL); 1732 1733 return (CTL_RETVAL_COMPLETE); 1734} 1735 1736/* 1737 * Serialize a command that went down the "wrong" side, and so was sent to 1738 * this controller for execution. The logic is a little different than the 1739 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1740 * sent back to the other side, but in the success case, we execute the 1741 * command on this side (XFER mode) or tell the other side to execute it 1742 * (SER_ONLY mode). 1743 */ 1744static int 1745ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1746{ 1747 struct ctl_softc *ctl_softc; 1748 union ctl_ha_msg msg_info; 1749 struct ctl_lun *lun; 1750 int retval = 0; 1751 uint32_t targ_lun; 1752 1753 ctl_softc = control_softc; 1754 1755 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1756 lun = ctl_softc->ctl_luns[targ_lun]; 1757 if (lun==NULL) 1758 { 1759 /* 1760 * Why isn't LUN defined? The other side wouldn't 1761 * send a cmd if the LUN is undefined. 1762 */ 1763 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1764 1765 /* "Logical unit not supported" */ 1766 ctl_set_sense_data(&msg_info.scsi.sense_data, 1767 lun, 1768 /*sense_format*/SSD_TYPE_NONE, 1769 /*current_error*/ 1, 1770 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1771 /*asc*/ 0x25, 1772 /*ascq*/ 0x00, 1773 SSD_ELEM_NONE); 1774 1775 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1776 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1777 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1778 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1779 msg_info.hdr.serializing_sc = NULL; 1780 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1781 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1782 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1783 } 1784 return(1); 1785 1786 } 1787 1788 mtx_lock(&lun->lun_lock); 1789 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1790 1791 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1792 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1793 ooa_links))) { 1794 case CTL_ACTION_BLOCK: 1795 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1796 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1797 blocked_links); 1798 break; 1799 case CTL_ACTION_PASS: 1800 case CTL_ACTION_SKIP: 1801 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1802 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1803 ctl_enqueue_rtr((union ctl_io *)ctsio); 1804 } else { 1805 1806 /* send msg back to other side */ 1807 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1808 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1809 msg_info.hdr.msg_type = CTL_MSG_R2R; 1810#if 0 1811 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1812#endif 1813 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1814 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1815 } 1816 } 1817 break; 1818 case CTL_ACTION_OVERLAP: 1819 /* OVERLAPPED COMMANDS ATTEMPTED */ 1820 ctl_set_sense_data(&msg_info.scsi.sense_data, 1821 lun, 1822 /*sense_format*/SSD_TYPE_NONE, 1823 /*current_error*/ 1, 1824 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1825 /*asc*/ 0x4E, 1826 /*ascq*/ 0x00, 1827 SSD_ELEM_NONE); 1828 1829 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1830 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1831 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1832 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1833 msg_info.hdr.serializing_sc = NULL; 1834 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1835#if 0 1836 printf("BAD JUJU:Major Bummer Overlap\n"); 1837#endif 1838 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1839 retval = 1; 1840 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1841 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1842 } 1843 break; 1844 case CTL_ACTION_OVERLAP_TAG: 1845 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1846 ctl_set_sense_data(&msg_info.scsi.sense_data, 1847 lun, 1848 /*sense_format*/SSD_TYPE_NONE, 1849 /*current_error*/ 1, 1850 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1851 /*asc*/ 0x4D, 1852 /*ascq*/ ctsio->tag_num & 0xff, 1853 SSD_ELEM_NONE); 1854 1855 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1856 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1857 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1858 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1859 msg_info.hdr.serializing_sc = NULL; 1860 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1861#if 0 1862 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1863#endif 1864 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1865 retval = 1; 1866 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1867 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1868 } 1869 break; 1870 case CTL_ACTION_ERROR: 1871 default: 1872 /* "Internal target failure" */ 1873 ctl_set_sense_data(&msg_info.scsi.sense_data, 1874 lun, 1875 /*sense_format*/SSD_TYPE_NONE, 1876 /*current_error*/ 1, 1877 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1878 /*asc*/ 0x44, 1879 /*ascq*/ 0x00, 1880 SSD_ELEM_NONE); 1881 1882 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1883 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1884 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1885 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1886 msg_info.hdr.serializing_sc = NULL; 1887 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1888#if 0 1889 printf("BAD JUJU:Major Bummer HW Error\n"); 1890#endif 1891 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1892 retval = 1; 1893 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1894 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1895 } 1896 break; 1897 } 1898 mtx_unlock(&lun->lun_lock); 1899 return (retval); 1900} 1901 1902static int 1903ctl_ioctl_submit_wait(union ctl_io *io) 1904{ 1905 struct ctl_fe_ioctl_params params; 1906 ctl_fe_ioctl_state last_state; 1907 int done, retval; 1908 1909 retval = 0; 1910 1911 bzero(¶ms, sizeof(params)); 1912 1913 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1914 cv_init(¶ms.sem, "ctlioccv"); 1915 params.state = CTL_IOCTL_INPROG; 1916 last_state = params.state; 1917 1918 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1919 1920 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1921 1922 /* This shouldn't happen */ 1923 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1924 return (retval); 1925 1926 done = 0; 1927 1928 do { 1929 mtx_lock(¶ms.ioctl_mtx); 1930 /* 1931 * Check the state here, and don't sleep if the state has 1932 * already changed (i.e. wakeup has already occured, but we 1933 * weren't waiting yet). 1934 */ 1935 if (params.state == last_state) { 1936 /* XXX KDM cv_wait_sig instead? */ 1937 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1938 } 1939 last_state = params.state; 1940 1941 switch (params.state) { 1942 case CTL_IOCTL_INPROG: 1943 /* Why did we wake up? */ 1944 /* XXX KDM error here? */ 1945 mtx_unlock(¶ms.ioctl_mtx); 1946 break; 1947 case CTL_IOCTL_DATAMOVE: 1948 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1949 1950 /* 1951 * change last_state back to INPROG to avoid 1952 * deadlock on subsequent data moves. 1953 */ 1954 params.state = last_state = CTL_IOCTL_INPROG; 1955 1956 mtx_unlock(¶ms.ioctl_mtx); 1957 ctl_ioctl_do_datamove(&io->scsiio); 1958 /* 1959 * Note that in some cases, most notably writes, 1960 * this will queue the I/O and call us back later. 1961 * In other cases, generally reads, this routine 1962 * will immediately call back and wake us up, 1963 * probably using our own context. 1964 */ 1965 io->scsiio.be_move_done(io); 1966 break; 1967 case CTL_IOCTL_DONE: 1968 mtx_unlock(¶ms.ioctl_mtx); 1969 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1970 done = 1; 1971 break; 1972 default: 1973 mtx_unlock(¶ms.ioctl_mtx); 1974 /* XXX KDM error here? */ 1975 break; 1976 } 1977 } while (done == 0); 1978 1979 mtx_destroy(¶ms.ioctl_mtx); 1980 cv_destroy(¶ms.sem); 1981 1982 return (CTL_RETVAL_COMPLETE); 1983} 1984 1985static void 1986ctl_ioctl_datamove(union ctl_io *io) 1987{ 1988 struct ctl_fe_ioctl_params *params; 1989 1990 params = (struct ctl_fe_ioctl_params *) 1991 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1992 1993 mtx_lock(¶ms->ioctl_mtx); 1994 params->state = CTL_IOCTL_DATAMOVE; 1995 cv_broadcast(¶ms->sem); 1996 mtx_unlock(¶ms->ioctl_mtx); 1997} 1998 1999static void 2000ctl_ioctl_done(union ctl_io *io) 2001{ 2002 struct ctl_fe_ioctl_params *params; 2003 2004 params = (struct ctl_fe_ioctl_params *) 2005 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2006 2007 mtx_lock(¶ms->ioctl_mtx); 2008 params->state = CTL_IOCTL_DONE; 2009 cv_broadcast(¶ms->sem); 2010 mtx_unlock(¶ms->ioctl_mtx); 2011} 2012 2013static void 2014ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2015{ 2016 struct ctl_fe_ioctl_startstop_info *sd_info; 2017 2018 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2019 2020 sd_info->hs_info.status = metatask->status; 2021 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2022 sd_info->hs_info.luns_complete = 2023 metatask->taskinfo.startstop.luns_complete; 2024 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2025 2026 cv_broadcast(&sd_info->sem); 2027} 2028 2029static void 2030ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2031{ 2032 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2033 2034 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2035 2036 mtx_lock(fe_bbr_info->lock); 2037 fe_bbr_info->bbr_info->status = metatask->status; 2038 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2039 fe_bbr_info->wakeup_done = 1; 2040 mtx_unlock(fe_bbr_info->lock); 2041 2042 cv_broadcast(&fe_bbr_info->sem); 2043} 2044 2045/* 2046 * Returns 0 for success, errno for failure. 2047 */ 2048static int 2049ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2050 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2051{ 2052 union ctl_io *io; 2053 int retval; 2054 2055 retval = 0; 2056 2057 mtx_lock(&lun->lun_lock); 2058 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2059 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2060 ooa_links)) { 2061 struct ctl_ooa_entry *entry; 2062 2063 /* 2064 * If we've got more than we can fit, just count the 2065 * remaining entries. 2066 */ 2067 if (*cur_fill_num >= ooa_hdr->alloc_num) 2068 continue; 2069 2070 entry = &kern_entries[*cur_fill_num]; 2071 2072 entry->tag_num = io->scsiio.tag_num; 2073 entry->lun_num = lun->lun; 2074#ifdef CTL_TIME_IO 2075 entry->start_bt = io->io_hdr.start_bt; 2076#endif 2077 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2078 entry->cdb_len = io->scsiio.cdb_len; 2079 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2080 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2081 2082 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2083 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2084 2085 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2086 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2087 2088 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2089 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2090 2091 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2092 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2093 } 2094 mtx_unlock(&lun->lun_lock); 2095 2096 return (retval); 2097} 2098 2099static void * 2100ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2101 size_t error_str_len) 2102{ 2103 void *kptr; 2104 2105 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2106 2107 if (copyin(user_addr, kptr, len) != 0) { 2108 snprintf(error_str, error_str_len, "Error copying %d bytes " 2109 "from user address %p to kernel address %p", len, 2110 user_addr, kptr); 2111 free(kptr, M_CTL); 2112 return (NULL); 2113 } 2114 2115 return (kptr); 2116} 2117 2118static void 2119ctl_free_args(int num_args, struct ctl_be_arg *args) 2120{ 2121 int i; 2122 2123 if (args == NULL) 2124 return; 2125 2126 for (i = 0; i < num_args; i++) { 2127 free(args[i].kname, M_CTL); 2128 free(args[i].kvalue, M_CTL); 2129 } 2130 2131 free(args, M_CTL); 2132} 2133 2134static struct ctl_be_arg * 2135ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2136 char *error_str, size_t error_str_len) 2137{ 2138 struct ctl_be_arg *args; 2139 int i; 2140 2141 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2142 error_str, error_str_len); 2143 2144 if (args == NULL) 2145 goto bailout; 2146 2147 for (i = 0; i < num_args; i++) { 2148 args[i].kname = NULL; 2149 args[i].kvalue = NULL; 2150 } 2151 2152 for (i = 0; i < num_args; i++) { 2153 uint8_t *tmpptr; 2154 2155 args[i].kname = ctl_copyin_alloc(args[i].name, 2156 args[i].namelen, error_str, error_str_len); 2157 if (args[i].kname == NULL) 2158 goto bailout; 2159 2160 if (args[i].kname[args[i].namelen - 1] != '\0') { 2161 snprintf(error_str, error_str_len, "Argument %d " 2162 "name is not NUL-terminated", i); 2163 goto bailout; 2164 } 2165 2166 if (args[i].flags & CTL_BEARG_RD) { 2167 tmpptr = ctl_copyin_alloc(args[i].value, 2168 args[i].vallen, error_str, error_str_len); 2169 if (tmpptr == NULL) 2170 goto bailout; 2171 if ((args[i].flags & CTL_BEARG_ASCII) 2172 && (tmpptr[args[i].vallen - 1] != '\0')) { 2173 snprintf(error_str, error_str_len, "Argument " 2174 "%d value is not NUL-terminated", i); 2175 goto bailout; 2176 } 2177 args[i].kvalue = tmpptr; 2178 } else { 2179 args[i].kvalue = malloc(args[i].vallen, 2180 M_CTL, M_WAITOK | M_ZERO); 2181 } 2182 } 2183 2184 return (args); 2185bailout: 2186 2187 ctl_free_args(num_args, args); 2188 2189 return (NULL); 2190} 2191 2192static void 2193ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2194{ 2195 int i; 2196 2197 for (i = 0; i < num_args; i++) { 2198 if (args[i].flags & CTL_BEARG_WR) 2199 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2200 } 2201} 2202 2203/* 2204 * Escape characters that are illegal or not recommended in XML. 2205 */ 2206int 2207ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2208{ 2209 int retval; 2210 2211 retval = 0; 2212 2213 for (; *str; str++) { 2214 switch (*str) { 2215 case '&': 2216 retval = sbuf_printf(sb, "&"); 2217 break; 2218 case '>': 2219 retval = sbuf_printf(sb, ">"); 2220 break; 2221 case '<': 2222 retval = sbuf_printf(sb, "<"); 2223 break; 2224 default: 2225 retval = sbuf_putc(sb, *str); 2226 break; 2227 } 2228 2229 if (retval != 0) 2230 break; 2231 2232 } 2233 2234 return (retval); 2235} 2236 2237static int 2238ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2239 struct thread *td) 2240{ 2241 struct ctl_softc *softc; 2242 int retval; 2243 2244 softc = control_softc; 2245 2246 retval = 0; 2247 2248 switch (cmd) { 2249 case CTL_IO: { 2250 union ctl_io *io; 2251 void *pool_tmp; 2252 2253 /* 2254 * If we haven't been "enabled", don't allow any SCSI I/O 2255 * to this FETD. 2256 */ 2257 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2258 retval = EPERM; 2259 break; 2260 } 2261 2262 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2263 if (io == NULL) { 2264 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2265 retval = ENOSPC; 2266 break; 2267 } 2268 2269 /* 2270 * Need to save the pool reference so it doesn't get 2271 * spammed by the user's ctl_io. 2272 */ 2273 pool_tmp = io->io_hdr.pool; 2274 2275 memcpy(io, (void *)addr, sizeof(*io)); 2276 2277 io->io_hdr.pool = pool_tmp; 2278 /* 2279 * No status yet, so make sure the status is set properly. 2280 */ 2281 io->io_hdr.status = CTL_STATUS_NONE; 2282 2283 /* 2284 * The user sets the initiator ID, target and LUN IDs. 2285 */ 2286 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2287 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2288 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2289 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2290 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2291 2292 retval = ctl_ioctl_submit_wait(io); 2293 2294 if (retval != 0) { 2295 ctl_free_io(io); 2296 break; 2297 } 2298 2299 memcpy((void *)addr, io, sizeof(*io)); 2300 2301 /* return this to our pool */ 2302 ctl_free_io(io); 2303 2304 break; 2305 } 2306 case CTL_ENABLE_PORT: 2307 case CTL_DISABLE_PORT: 2308 case CTL_SET_PORT_WWNS: { 2309 struct ctl_port *port; 2310 struct ctl_port_entry *entry; 2311 2312 entry = (struct ctl_port_entry *)addr; 2313 2314 mtx_lock(&softc->ctl_lock); 2315 STAILQ_FOREACH(port, &softc->port_list, links) { 2316 int action, done; 2317 2318 action = 0; 2319 done = 0; 2320 2321 if ((entry->port_type == CTL_PORT_NONE) 2322 && (entry->targ_port == port->targ_port)) { 2323 /* 2324 * If the user only wants to enable or 2325 * disable or set WWNs on a specific port, 2326 * do the operation and we're done. 2327 */ 2328 action = 1; 2329 done = 1; 2330 } else if (entry->port_type & port->port_type) { 2331 /* 2332 * Compare the user's type mask with the 2333 * particular frontend type to see if we 2334 * have a match. 2335 */ 2336 action = 1; 2337 done = 0; 2338 2339 /* 2340 * Make sure the user isn't trying to set 2341 * WWNs on multiple ports at the same time. 2342 */ 2343 if (cmd == CTL_SET_PORT_WWNS) { 2344 printf("%s: Can't set WWNs on " 2345 "multiple ports\n", __func__); 2346 retval = EINVAL; 2347 break; 2348 } 2349 } 2350 if (action != 0) { 2351 /* 2352 * XXX KDM we have to drop the lock here, 2353 * because the online/offline operations 2354 * can potentially block. We need to 2355 * reference count the frontends so they 2356 * can't go away, 2357 */ 2358 mtx_unlock(&softc->ctl_lock); 2359 2360 if (cmd == CTL_ENABLE_PORT) { 2361 struct ctl_lun *lun; 2362 2363 STAILQ_FOREACH(lun, &softc->lun_list, 2364 links) { 2365 port->lun_enable(port->targ_lun_arg, 2366 lun->target, 2367 lun->lun); 2368 } 2369 2370 ctl_port_online(port); 2371 } else if (cmd == CTL_DISABLE_PORT) { 2372 struct ctl_lun *lun; 2373 2374 ctl_port_offline(port); 2375 2376 STAILQ_FOREACH(lun, &softc->lun_list, 2377 links) { 2378 port->lun_disable( 2379 port->targ_lun_arg, 2380 lun->target, 2381 lun->lun); 2382 } 2383 } 2384 2385 mtx_lock(&softc->ctl_lock); 2386 2387 if (cmd == CTL_SET_PORT_WWNS) 2388 ctl_port_set_wwns(port, 2389 (entry->flags & CTL_PORT_WWNN_VALID) ? 2390 1 : 0, entry->wwnn, 2391 (entry->flags & CTL_PORT_WWPN_VALID) ? 2392 1 : 0, entry->wwpn); 2393 } 2394 if (done != 0) 2395 break; 2396 } 2397 mtx_unlock(&softc->ctl_lock); 2398 break; 2399 } 2400 case CTL_GET_PORT_LIST: { 2401 struct ctl_port *port; 2402 struct ctl_port_list *list; 2403 int i; 2404 2405 list = (struct ctl_port_list *)addr; 2406 2407 if (list->alloc_len != (list->alloc_num * 2408 sizeof(struct ctl_port_entry))) { 2409 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2410 "alloc_num %u * sizeof(struct ctl_port_entry) " 2411 "%zu\n", __func__, list->alloc_len, 2412 list->alloc_num, sizeof(struct ctl_port_entry)); 2413 retval = EINVAL; 2414 break; 2415 } 2416 list->fill_len = 0; 2417 list->fill_num = 0; 2418 list->dropped_num = 0; 2419 i = 0; 2420 mtx_lock(&softc->ctl_lock); 2421 STAILQ_FOREACH(port, &softc->port_list, links) { 2422 struct ctl_port_entry entry, *list_entry; 2423 2424 if (list->fill_num >= list->alloc_num) { 2425 list->dropped_num++; 2426 continue; 2427 } 2428 2429 entry.port_type = port->port_type; 2430 strlcpy(entry.port_name, port->port_name, 2431 sizeof(entry.port_name)); 2432 entry.targ_port = port->targ_port; 2433 entry.physical_port = port->physical_port; 2434 entry.virtual_port = port->virtual_port; 2435 entry.wwnn = port->wwnn; 2436 entry.wwpn = port->wwpn; 2437 if (port->status & CTL_PORT_STATUS_ONLINE) 2438 entry.online = 1; 2439 else 2440 entry.online = 0; 2441 2442 list_entry = &list->entries[i]; 2443 2444 retval = copyout(&entry, list_entry, sizeof(entry)); 2445 if (retval != 0) { 2446 printf("%s: CTL_GET_PORT_LIST: copyout " 2447 "returned %d\n", __func__, retval); 2448 break; 2449 } 2450 i++; 2451 list->fill_num++; 2452 list->fill_len += sizeof(entry); 2453 } 2454 mtx_unlock(&softc->ctl_lock); 2455 2456 /* 2457 * If this is non-zero, we had a copyout fault, so there's 2458 * probably no point in attempting to set the status inside 2459 * the structure. 2460 */ 2461 if (retval != 0) 2462 break; 2463 2464 if (list->dropped_num > 0) 2465 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2466 else 2467 list->status = CTL_PORT_LIST_OK; 2468 break; 2469 } 2470 case CTL_DUMP_OOA: { 2471 struct ctl_lun *lun; 2472 union ctl_io *io; 2473 char printbuf[128]; 2474 struct sbuf sb; 2475 2476 mtx_lock(&softc->ctl_lock); 2477 printf("Dumping OOA queues:\n"); 2478 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2479 mtx_lock(&lun->lun_lock); 2480 for (io = (union ctl_io *)TAILQ_FIRST( 2481 &lun->ooa_queue); io != NULL; 2482 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2483 ooa_links)) { 2484 sbuf_new(&sb, printbuf, sizeof(printbuf), 2485 SBUF_FIXEDLEN); 2486 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2487 (intmax_t)lun->lun, 2488 io->scsiio.tag_num, 2489 (io->io_hdr.flags & 2490 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2491 (io->io_hdr.flags & 2492 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2493 (io->io_hdr.flags & 2494 CTL_FLAG_ABORT) ? " ABORT" : "", 2495 (io->io_hdr.flags & 2496 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2497 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2498 sbuf_finish(&sb); 2499 printf("%s\n", sbuf_data(&sb)); 2500 } 2501 mtx_unlock(&lun->lun_lock); 2502 } 2503 printf("OOA queues dump done\n"); 2504 mtx_unlock(&softc->ctl_lock); 2505 break; 2506 } 2507 case CTL_GET_OOA: { 2508 struct ctl_lun *lun; 2509 struct ctl_ooa *ooa_hdr; 2510 struct ctl_ooa_entry *entries; 2511 uint32_t cur_fill_num; 2512 2513 ooa_hdr = (struct ctl_ooa *)addr; 2514 2515 if ((ooa_hdr->alloc_len == 0) 2516 || (ooa_hdr->alloc_num == 0)) { 2517 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2518 "must be non-zero\n", __func__, 2519 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2520 retval = EINVAL; 2521 break; 2522 } 2523 2524 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2525 sizeof(struct ctl_ooa_entry))) { 2526 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2527 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2528 __func__, ooa_hdr->alloc_len, 2529 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2530 retval = EINVAL; 2531 break; 2532 } 2533 2534 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2535 if (entries == NULL) { 2536 printf("%s: could not allocate %d bytes for OOA " 2537 "dump\n", __func__, ooa_hdr->alloc_len); 2538 retval = ENOMEM; 2539 break; 2540 } 2541 2542 mtx_lock(&softc->ctl_lock); 2543 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2544 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2545 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2546 mtx_unlock(&softc->ctl_lock); 2547 free(entries, M_CTL); 2548 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2549 __func__, (uintmax_t)ooa_hdr->lun_num); 2550 retval = EINVAL; 2551 break; 2552 } 2553 2554 cur_fill_num = 0; 2555 2556 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2557 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2558 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2559 ooa_hdr, entries); 2560 if (retval != 0) 2561 break; 2562 } 2563 if (retval != 0) { 2564 mtx_unlock(&softc->ctl_lock); 2565 free(entries, M_CTL); 2566 break; 2567 } 2568 } else { 2569 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2570 2571 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2572 entries); 2573 } 2574 mtx_unlock(&softc->ctl_lock); 2575 2576 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2577 ooa_hdr->fill_len = ooa_hdr->fill_num * 2578 sizeof(struct ctl_ooa_entry); 2579 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2580 if (retval != 0) { 2581 printf("%s: error copying out %d bytes for OOA dump\n", 2582 __func__, ooa_hdr->fill_len); 2583 } 2584 2585 getbintime(&ooa_hdr->cur_bt); 2586 2587 if (cur_fill_num > ooa_hdr->alloc_num) { 2588 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2589 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2590 } else { 2591 ooa_hdr->dropped_num = 0; 2592 ooa_hdr->status = CTL_OOA_OK; 2593 } 2594 2595 free(entries, M_CTL); 2596 break; 2597 } 2598 case CTL_CHECK_OOA: { 2599 union ctl_io *io; 2600 struct ctl_lun *lun; 2601 struct ctl_ooa_info *ooa_info; 2602 2603 2604 ooa_info = (struct ctl_ooa_info *)addr; 2605 2606 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2607 ooa_info->status = CTL_OOA_INVALID_LUN; 2608 break; 2609 } 2610 mtx_lock(&softc->ctl_lock); 2611 lun = softc->ctl_luns[ooa_info->lun_id]; 2612 if (lun == NULL) { 2613 mtx_unlock(&softc->ctl_lock); 2614 ooa_info->status = CTL_OOA_INVALID_LUN; 2615 break; 2616 } 2617 mtx_lock(&lun->lun_lock); 2618 mtx_unlock(&softc->ctl_lock); 2619 ooa_info->num_entries = 0; 2620 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2621 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2622 &io->io_hdr, ooa_links)) { 2623 ooa_info->num_entries++; 2624 } 2625 mtx_unlock(&lun->lun_lock); 2626 2627 ooa_info->status = CTL_OOA_SUCCESS; 2628 2629 break; 2630 } 2631 case CTL_HARD_START: 2632 case CTL_HARD_STOP: { 2633 struct ctl_fe_ioctl_startstop_info ss_info; 2634 struct cfi_metatask *metatask; 2635 struct mtx hs_mtx; 2636 2637 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2638 2639 cv_init(&ss_info.sem, "hard start/stop cv" ); 2640 2641 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2642 if (metatask == NULL) { 2643 retval = ENOMEM; 2644 mtx_destroy(&hs_mtx); 2645 break; 2646 } 2647 2648 if (cmd == CTL_HARD_START) 2649 metatask->tasktype = CFI_TASK_STARTUP; 2650 else 2651 metatask->tasktype = CFI_TASK_SHUTDOWN; 2652 2653 metatask->callback = ctl_ioctl_hard_startstop_callback; 2654 metatask->callback_arg = &ss_info; 2655 2656 cfi_action(metatask); 2657 2658 /* Wait for the callback */ 2659 mtx_lock(&hs_mtx); 2660 cv_wait_sig(&ss_info.sem, &hs_mtx); 2661 mtx_unlock(&hs_mtx); 2662 2663 /* 2664 * All information has been copied from the metatask by the 2665 * time cv_broadcast() is called, so we free the metatask here. 2666 */ 2667 cfi_free_metatask(metatask); 2668 2669 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2670 2671 mtx_destroy(&hs_mtx); 2672 break; 2673 } 2674 case CTL_BBRREAD: { 2675 struct ctl_bbrread_info *bbr_info; 2676 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2677 struct mtx bbr_mtx; 2678 struct cfi_metatask *metatask; 2679 2680 bbr_info = (struct ctl_bbrread_info *)addr; 2681 2682 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2683 2684 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2685 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2686 2687 fe_bbr_info.bbr_info = bbr_info; 2688 fe_bbr_info.lock = &bbr_mtx; 2689 2690 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2691 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2692 2693 if (metatask == NULL) { 2694 mtx_destroy(&bbr_mtx); 2695 cv_destroy(&fe_bbr_info.sem); 2696 retval = ENOMEM; 2697 break; 2698 } 2699 metatask->tasktype = CFI_TASK_BBRREAD; 2700 metatask->callback = ctl_ioctl_bbrread_callback; 2701 metatask->callback_arg = &fe_bbr_info; 2702 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2703 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2704 metatask->taskinfo.bbrread.len = bbr_info->len; 2705 2706 cfi_action(metatask); 2707 2708 mtx_lock(&bbr_mtx); 2709 while (fe_bbr_info.wakeup_done == 0) 2710 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2711 mtx_unlock(&bbr_mtx); 2712 2713 bbr_info->status = metatask->status; 2714 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2715 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2716 memcpy(&bbr_info->sense_data, 2717 &metatask->taskinfo.bbrread.sense_data, 2718 ctl_min(sizeof(bbr_info->sense_data), 2719 sizeof(metatask->taskinfo.bbrread.sense_data))); 2720 2721 cfi_free_metatask(metatask); 2722 2723 mtx_destroy(&bbr_mtx); 2724 cv_destroy(&fe_bbr_info.sem); 2725 2726 break; 2727 } 2728 case CTL_DELAY_IO: { 2729 struct ctl_io_delay_info *delay_info; 2730#ifdef CTL_IO_DELAY 2731 struct ctl_lun *lun; 2732#endif /* CTL_IO_DELAY */ 2733 2734 delay_info = (struct ctl_io_delay_info *)addr; 2735 2736#ifdef CTL_IO_DELAY 2737 mtx_lock(&softc->ctl_lock); 2738 2739 if ((delay_info->lun_id > CTL_MAX_LUNS) 2740 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2741 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2742 } else { 2743 lun = softc->ctl_luns[delay_info->lun_id]; 2744 mtx_lock(&lun->lun_lock); 2745 2746 delay_info->status = CTL_DELAY_STATUS_OK; 2747 2748 switch (delay_info->delay_type) { 2749 case CTL_DELAY_TYPE_CONT: 2750 break; 2751 case CTL_DELAY_TYPE_ONESHOT: 2752 break; 2753 default: 2754 delay_info->status = 2755 CTL_DELAY_STATUS_INVALID_TYPE; 2756 break; 2757 } 2758 2759 switch (delay_info->delay_loc) { 2760 case CTL_DELAY_LOC_DATAMOVE: 2761 lun->delay_info.datamove_type = 2762 delay_info->delay_type; 2763 lun->delay_info.datamove_delay = 2764 delay_info->delay_secs; 2765 break; 2766 case CTL_DELAY_LOC_DONE: 2767 lun->delay_info.done_type = 2768 delay_info->delay_type; 2769 lun->delay_info.done_delay = 2770 delay_info->delay_secs; 2771 break; 2772 default: 2773 delay_info->status = 2774 CTL_DELAY_STATUS_INVALID_LOC; 2775 break; 2776 } 2777 mtx_unlock(&lun->lun_lock); 2778 } 2779 2780 mtx_unlock(&softc->ctl_lock); 2781#else 2782 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2783#endif /* CTL_IO_DELAY */ 2784 break; 2785 } 2786 case CTL_REALSYNC_SET: { 2787 int *syncstate; 2788 2789 syncstate = (int *)addr; 2790 2791 mtx_lock(&softc->ctl_lock); 2792 switch (*syncstate) { 2793 case 0: 2794 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2795 break; 2796 case 1: 2797 softc->flags |= CTL_FLAG_REAL_SYNC; 2798 break; 2799 default: 2800 retval = EINVAL; 2801 break; 2802 } 2803 mtx_unlock(&softc->ctl_lock); 2804 break; 2805 } 2806 case CTL_REALSYNC_GET: { 2807 int *syncstate; 2808 2809 syncstate = (int*)addr; 2810 2811 mtx_lock(&softc->ctl_lock); 2812 if (softc->flags & CTL_FLAG_REAL_SYNC) 2813 *syncstate = 1; 2814 else 2815 *syncstate = 0; 2816 mtx_unlock(&softc->ctl_lock); 2817 2818 break; 2819 } 2820 case CTL_SETSYNC: 2821 case CTL_GETSYNC: { 2822 struct ctl_sync_info *sync_info; 2823 struct ctl_lun *lun; 2824 2825 sync_info = (struct ctl_sync_info *)addr; 2826 2827 mtx_lock(&softc->ctl_lock); 2828 lun = softc->ctl_luns[sync_info->lun_id]; 2829 if (lun == NULL) { 2830 mtx_unlock(&softc->ctl_lock); 2831 sync_info->status = CTL_GS_SYNC_NO_LUN; 2832 } 2833 /* 2834 * Get or set the sync interval. We're not bounds checking 2835 * in the set case, hopefully the user won't do something 2836 * silly. 2837 */ 2838 mtx_lock(&lun->lun_lock); 2839 mtx_unlock(&softc->ctl_lock); 2840 if (cmd == CTL_GETSYNC) 2841 sync_info->sync_interval = lun->sync_interval; 2842 else 2843 lun->sync_interval = sync_info->sync_interval; 2844 mtx_unlock(&lun->lun_lock); 2845 2846 sync_info->status = CTL_GS_SYNC_OK; 2847 2848 break; 2849 } 2850 case CTL_GETSTATS: { 2851 struct ctl_stats *stats; 2852 struct ctl_lun *lun; 2853 int i; 2854 2855 stats = (struct ctl_stats *)addr; 2856 2857 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2858 stats->alloc_len) { 2859 stats->status = CTL_SS_NEED_MORE_SPACE; 2860 stats->num_luns = softc->num_luns; 2861 break; 2862 } 2863 /* 2864 * XXX KDM no locking here. If the LUN list changes, 2865 * things can blow up. 2866 */ 2867 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2868 i++, lun = STAILQ_NEXT(lun, links)) { 2869 retval = copyout(&lun->stats, &stats->lun_stats[i], 2870 sizeof(lun->stats)); 2871 if (retval != 0) 2872 break; 2873 } 2874 stats->num_luns = softc->num_luns; 2875 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2876 softc->num_luns; 2877 stats->status = CTL_SS_OK; 2878#ifdef CTL_TIME_IO 2879 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2880#else 2881 stats->flags = CTL_STATS_FLAG_NONE; 2882#endif 2883 getnanouptime(&stats->timestamp); 2884 break; 2885 } 2886 case CTL_ERROR_INJECT: { 2887 struct ctl_error_desc *err_desc, *new_err_desc; 2888 struct ctl_lun *lun; 2889 2890 err_desc = (struct ctl_error_desc *)addr; 2891 2892 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2893 M_WAITOK | M_ZERO); 2894 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2895 2896 mtx_lock(&softc->ctl_lock); 2897 lun = softc->ctl_luns[err_desc->lun_id]; 2898 if (lun == NULL) { 2899 mtx_unlock(&softc->ctl_lock); 2900 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2901 __func__, (uintmax_t)err_desc->lun_id); 2902 retval = EINVAL; 2903 break; 2904 } 2905 mtx_lock(&lun->lun_lock); 2906 mtx_unlock(&softc->ctl_lock); 2907 2908 /* 2909 * We could do some checking here to verify the validity 2910 * of the request, but given the complexity of error 2911 * injection requests, the checking logic would be fairly 2912 * complex. 2913 * 2914 * For now, if the request is invalid, it just won't get 2915 * executed and might get deleted. 2916 */ 2917 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2918 2919 /* 2920 * XXX KDM check to make sure the serial number is unique, 2921 * in case we somehow manage to wrap. That shouldn't 2922 * happen for a very long time, but it's the right thing to 2923 * do. 2924 */ 2925 new_err_desc->serial = lun->error_serial; 2926 err_desc->serial = lun->error_serial; 2927 lun->error_serial++; 2928 2929 mtx_unlock(&lun->lun_lock); 2930 break; 2931 } 2932 case CTL_ERROR_INJECT_DELETE: { 2933 struct ctl_error_desc *delete_desc, *desc, *desc2; 2934 struct ctl_lun *lun; 2935 int delete_done; 2936 2937 delete_desc = (struct ctl_error_desc *)addr; 2938 delete_done = 0; 2939 2940 mtx_lock(&softc->ctl_lock); 2941 lun = softc->ctl_luns[delete_desc->lun_id]; 2942 if (lun == NULL) { 2943 mtx_unlock(&softc->ctl_lock); 2944 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2945 __func__, (uintmax_t)delete_desc->lun_id); 2946 retval = EINVAL; 2947 break; 2948 } 2949 mtx_lock(&lun->lun_lock); 2950 mtx_unlock(&softc->ctl_lock); 2951 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2952 if (desc->serial != delete_desc->serial) 2953 continue; 2954 2955 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2956 links); 2957 free(desc, M_CTL); 2958 delete_done = 1; 2959 } 2960 mtx_unlock(&lun->lun_lock); 2961 if (delete_done == 0) { 2962 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2963 "error serial %ju on LUN %u\n", __func__, 2964 delete_desc->serial, delete_desc->lun_id); 2965 retval = EINVAL; 2966 break; 2967 } 2968 break; 2969 } 2970 case CTL_DUMP_STRUCTS: { 2971 int i, j, k, idx; 2972 struct ctl_port *port; 2973 struct ctl_frontend *fe; 2974 2975 mtx_lock(&softc->ctl_lock); 2976 printf("CTL Persistent Reservation information start:\n"); 2977 for (i = 0; i < CTL_MAX_LUNS; i++) { 2978 struct ctl_lun *lun; 2979 2980 lun = softc->ctl_luns[i]; 2981 2982 if ((lun == NULL) 2983 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2984 continue; 2985 2986 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2987 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2988 idx = j * CTL_MAX_INIT_PER_PORT + k; 2989 if (lun->per_res[idx].registered == 0) 2990 continue; 2991 printf(" LUN %d port %d iid %d key " 2992 "%#jx\n", i, j, k, 2993 (uintmax_t)scsi_8btou64( 2994 lun->per_res[idx].res_key.key)); 2995 } 2996 } 2997 } 2998 printf("CTL Persistent Reservation information end\n"); 2999 printf("CTL Ports:\n"); 3000 STAILQ_FOREACH(port, &softc->port_list, links) { 3001 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3002 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3003 port->frontend->name, port->port_type, 3004 port->physical_port, port->virtual_port, 3005 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3006 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3007 if (port->wwpn_iid[j].in_use == 0 && 3008 port->wwpn_iid[j].wwpn == 0 && 3009 port->wwpn_iid[j].name == NULL) 3010 continue; 3011 3012 printf(" iid %u use %d WWPN %#jx '%s'\n", 3013 j, port->wwpn_iid[j].in_use, 3014 (uintmax_t)port->wwpn_iid[j].wwpn, 3015 port->wwpn_iid[j].name); 3016 } 3017 } 3018 printf("CTL Port information end\n"); 3019 mtx_unlock(&softc->ctl_lock); 3020 /* 3021 * XXX KDM calling this without a lock. We'd likely want 3022 * to drop the lock before calling the frontend's dump 3023 * routine anyway. 3024 */ 3025 printf("CTL Frontends:\n"); 3026 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3027 printf(" Frontend '%s'\n", fe->name); 3028 if (fe->fe_dump != NULL) 3029 fe->fe_dump(); 3030 } 3031 printf("CTL Frontend information end\n"); 3032 break; 3033 } 3034 case CTL_LUN_REQ: { 3035 struct ctl_lun_req *lun_req; 3036 struct ctl_backend_driver *backend; 3037 3038 lun_req = (struct ctl_lun_req *)addr; 3039 3040 backend = ctl_backend_find(lun_req->backend); 3041 if (backend == NULL) { 3042 lun_req->status = CTL_LUN_ERROR; 3043 snprintf(lun_req->error_str, 3044 sizeof(lun_req->error_str), 3045 "Backend \"%s\" not found.", 3046 lun_req->backend); 3047 break; 3048 } 3049 if (lun_req->num_be_args > 0) { 3050 lun_req->kern_be_args = ctl_copyin_args( 3051 lun_req->num_be_args, 3052 lun_req->be_args, 3053 lun_req->error_str, 3054 sizeof(lun_req->error_str)); 3055 if (lun_req->kern_be_args == NULL) { 3056 lun_req->status = CTL_LUN_ERROR; 3057 break; 3058 } 3059 } 3060 3061 retval = backend->ioctl(dev, cmd, addr, flag, td); 3062 3063 if (lun_req->num_be_args > 0) { 3064 ctl_copyout_args(lun_req->num_be_args, 3065 lun_req->kern_be_args); 3066 ctl_free_args(lun_req->num_be_args, 3067 lun_req->kern_be_args); 3068 } 3069 break; 3070 } 3071 case CTL_LUN_LIST: { 3072 struct sbuf *sb; 3073 struct ctl_lun *lun; 3074 struct ctl_lun_list *list; 3075 struct ctl_option *opt; 3076 3077 list = (struct ctl_lun_list *)addr; 3078 3079 /* 3080 * Allocate a fixed length sbuf here, based on the length 3081 * of the user's buffer. We could allocate an auto-extending 3082 * buffer, and then tell the user how much larger our 3083 * amount of data is than his buffer, but that presents 3084 * some problems: 3085 * 3086 * 1. The sbuf(9) routines use a blocking malloc, and so 3087 * we can't hold a lock while calling them with an 3088 * auto-extending buffer. 3089 * 3090 * 2. There is not currently a LUN reference counting 3091 * mechanism, outside of outstanding transactions on 3092 * the LUN's OOA queue. So a LUN could go away on us 3093 * while we're getting the LUN number, backend-specific 3094 * information, etc. Thus, given the way things 3095 * currently work, we need to hold the CTL lock while 3096 * grabbing LUN information. 3097 * 3098 * So, from the user's standpoint, the best thing to do is 3099 * allocate what he thinks is a reasonable buffer length, 3100 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3101 * double the buffer length and try again. (And repeat 3102 * that until he succeeds.) 3103 */ 3104 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3105 if (sb == NULL) { 3106 list->status = CTL_LUN_LIST_ERROR; 3107 snprintf(list->error_str, sizeof(list->error_str), 3108 "Unable to allocate %d bytes for LUN list", 3109 list->alloc_len); 3110 break; 3111 } 3112 3113 sbuf_printf(sb, "<ctllunlist>\n"); 3114 3115 mtx_lock(&softc->ctl_lock); 3116 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3117 mtx_lock(&lun->lun_lock); 3118 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3119 (uintmax_t)lun->lun); 3120 3121 /* 3122 * Bail out as soon as we see that we've overfilled 3123 * the buffer. 3124 */ 3125 if (retval != 0) 3126 break; 3127 3128 retval = sbuf_printf(sb, "\t<backend_type>%s" 3129 "</backend_type>\n", 3130 (lun->backend == NULL) ? "none" : 3131 lun->backend->name); 3132 3133 if (retval != 0) 3134 break; 3135 3136 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3137 lun->be_lun->lun_type); 3138 3139 if (retval != 0) 3140 break; 3141 3142 if (lun->backend == NULL) { 3143 retval = sbuf_printf(sb, "</lun>\n"); 3144 if (retval != 0) 3145 break; 3146 continue; 3147 } 3148 3149 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3150 (lun->be_lun->maxlba > 0) ? 3151 lun->be_lun->maxlba + 1 : 0); 3152 3153 if (retval != 0) 3154 break; 3155 3156 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3157 lun->be_lun->blocksize); 3158 3159 if (retval != 0) 3160 break; 3161 3162 retval = sbuf_printf(sb, "\t<serial_number>"); 3163 3164 if (retval != 0) 3165 break; 3166 3167 retval = ctl_sbuf_printf_esc(sb, 3168 lun->be_lun->serial_num); 3169 3170 if (retval != 0) 3171 break; 3172 3173 retval = sbuf_printf(sb, "</serial_number>\n"); 3174 3175 if (retval != 0) 3176 break; 3177 3178 retval = sbuf_printf(sb, "\t<device_id>"); 3179 3180 if (retval != 0) 3181 break; 3182 3183 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3184 3185 if (retval != 0) 3186 break; 3187 3188 retval = sbuf_printf(sb, "</device_id>\n"); 3189 3190 if (retval != 0) 3191 break; 3192 3193 if (lun->backend->lun_info != NULL) { 3194 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3195 if (retval != 0) 3196 break; 3197 } 3198 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3199 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3200 opt->name, opt->value, opt->name); 3201 if (retval != 0) 3202 break; 3203 } 3204 3205 retval = sbuf_printf(sb, "</lun>\n"); 3206 3207 if (retval != 0) 3208 break; 3209 mtx_unlock(&lun->lun_lock); 3210 } 3211 if (lun != NULL) 3212 mtx_unlock(&lun->lun_lock); 3213 mtx_unlock(&softc->ctl_lock); 3214 3215 if ((retval != 0) 3216 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3217 retval = 0; 3218 sbuf_delete(sb); 3219 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3220 snprintf(list->error_str, sizeof(list->error_str), 3221 "Out of space, %d bytes is too small", 3222 list->alloc_len); 3223 break; 3224 } 3225 3226 sbuf_finish(sb); 3227 3228 retval = copyout(sbuf_data(sb), list->lun_xml, 3229 sbuf_len(sb) + 1); 3230 3231 list->fill_len = sbuf_len(sb) + 1; 3232 list->status = CTL_LUN_LIST_OK; 3233 sbuf_delete(sb); 3234 break; 3235 } 3236 case CTL_ISCSI: { 3237 struct ctl_iscsi *ci; 3238 struct ctl_frontend *fe; 3239 3240 ci = (struct ctl_iscsi *)addr; 3241 3242 fe = ctl_frontend_find("iscsi"); 3243 if (fe == NULL) { 3244 ci->status = CTL_ISCSI_ERROR; 3245 snprintf(ci->error_str, sizeof(ci->error_str), 3246 "Frontend \"iscsi\" not found."); 3247 break; 3248 } 3249 3250 retval = fe->ioctl(dev, cmd, addr, flag, td); 3251 break; 3252 } 3253 case CTL_PORT_REQ: { 3254 struct ctl_req *req; 3255 struct ctl_frontend *fe; 3256 3257 req = (struct ctl_req *)addr; 3258 3259 fe = ctl_frontend_find(req->driver); 3260 if (fe == NULL) { 3261 req->status = CTL_LUN_ERROR; 3262 snprintf(req->error_str, sizeof(req->error_str), 3263 "Frontend \"%s\" not found.", req->driver); 3264 break; 3265 } 3266 if (req->num_args > 0) { 3267 req->kern_args = ctl_copyin_args(req->num_args, 3268 req->args, req->error_str, sizeof(req->error_str)); 3269 if (req->kern_args == NULL) { 3270 req->status = CTL_LUN_ERROR; 3271 break; 3272 } 3273 } 3274 3275 retval = fe->ioctl(dev, cmd, addr, flag, td); 3276 3277 if (req->num_args > 0) { 3278 ctl_copyout_args(req->num_args, req->kern_args); 3279 ctl_free_args(req->num_args, req->kern_args); 3280 } 3281 break; 3282 } 3283 case CTL_PORT_LIST: { 3284 struct sbuf *sb; 3285 struct ctl_port *port; 3286 struct ctl_lun_list *list; 3287 struct ctl_option *opt; 3288 3289 list = (struct ctl_lun_list *)addr; 3290 3291 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3292 if (sb == NULL) { 3293 list->status = CTL_LUN_LIST_ERROR; 3294 snprintf(list->error_str, sizeof(list->error_str), 3295 "Unable to allocate %d bytes for LUN list", 3296 list->alloc_len); 3297 break; 3298 } 3299 3300 sbuf_printf(sb, "<ctlportlist>\n"); 3301 3302 mtx_lock(&softc->ctl_lock); 3303 STAILQ_FOREACH(port, &softc->port_list, links) { 3304 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3305 (uintmax_t)port->targ_port); 3306 3307 /* 3308 * Bail out as soon as we see that we've overfilled 3309 * the buffer. 3310 */ 3311 if (retval != 0) 3312 break; 3313 3314 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3315 "</frontend_type>\n", port->frontend->name); 3316 if (retval != 0) 3317 break; 3318 3319 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3320 port->port_type); 3321 if (retval != 0) 3322 break; 3323 3324 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3325 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3326 if (retval != 0) 3327 break; 3328 3329 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3330 port->port_name); 3331 if (retval != 0) 3332 break; 3333 3334 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3335 port->physical_port); 3336 if (retval != 0) 3337 break; 3338 3339 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3340 port->virtual_port); 3341 if (retval != 0) 3342 break; 3343 3344 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3345 (uintmax_t)port->wwnn); 3346 if (retval != 0) 3347 break; 3348 3349 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3350 (uintmax_t)port->wwpn); 3351 if (retval != 0) 3352 break; 3353 3354 if (port->port_info != NULL) { 3355 retval = port->port_info(port->onoff_arg, sb); 3356 if (retval != 0) 3357 break; 3358 } 3359 STAILQ_FOREACH(opt, &port->options, links) { 3360 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3361 opt->name, opt->value, opt->name); 3362 if (retval != 0) 3363 break; 3364 } 3365 3366 retval = sbuf_printf(sb, "</targ_port>\n"); 3367 if (retval != 0) 3368 break; 3369 } 3370 mtx_unlock(&softc->ctl_lock); 3371 3372 if ((retval != 0) 3373 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3374 retval = 0; 3375 sbuf_delete(sb); 3376 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3377 snprintf(list->error_str, sizeof(list->error_str), 3378 "Out of space, %d bytes is too small", 3379 list->alloc_len); 3380 break; 3381 } 3382 3383 sbuf_finish(sb); 3384 3385 retval = copyout(sbuf_data(sb), list->lun_xml, 3386 sbuf_len(sb) + 1); 3387 3388 list->fill_len = sbuf_len(sb) + 1; 3389 list->status = CTL_LUN_LIST_OK; 3390 sbuf_delete(sb); 3391 break; 3392 } 3393 default: { 3394 /* XXX KDM should we fix this? */ 3395#if 0 3396 struct ctl_backend_driver *backend; 3397 unsigned int type; 3398 int found; 3399 3400 found = 0; 3401 3402 /* 3403 * We encode the backend type as the ioctl type for backend 3404 * ioctls. So parse it out here, and then search for a 3405 * backend of this type. 3406 */ 3407 type = _IOC_TYPE(cmd); 3408 3409 STAILQ_FOREACH(backend, &softc->be_list, links) { 3410 if (backend->type == type) { 3411 found = 1; 3412 break; 3413 } 3414 } 3415 if (found == 0) { 3416 printf("ctl: unknown ioctl command %#lx or backend " 3417 "%d\n", cmd, type); 3418 retval = EINVAL; 3419 break; 3420 } 3421 retval = backend->ioctl(dev, cmd, addr, flag, td); 3422#endif 3423 retval = ENOTTY; 3424 break; 3425 } 3426 } 3427 return (retval); 3428} 3429 3430uint32_t 3431ctl_get_initindex(struct ctl_nexus *nexus) 3432{ 3433 if (nexus->targ_port < CTL_MAX_PORTS) 3434 return (nexus->initid.id + 3435 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3436 else 3437 return (nexus->initid.id + 3438 ((nexus->targ_port - CTL_MAX_PORTS) * 3439 CTL_MAX_INIT_PER_PORT)); 3440} 3441 3442uint32_t 3443ctl_get_resindex(struct ctl_nexus *nexus) 3444{ 3445 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3446} 3447 3448uint32_t 3449ctl_port_idx(int port_num) 3450{ 3451 if (port_num < CTL_MAX_PORTS) 3452 return(port_num); 3453 else 3454 return(port_num - CTL_MAX_PORTS); 3455} 3456 3457static uint32_t 3458ctl_map_lun(int port_num, uint32_t lun_id) 3459{ 3460 struct ctl_port *port; 3461 3462 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3463 if (port == NULL) 3464 return (UINT32_MAX); 3465 if (port->lun_map == NULL) 3466 return (lun_id); 3467 return (port->lun_map(port->targ_lun_arg, lun_id)); 3468} 3469 3470static uint32_t 3471ctl_map_lun_back(int port_num, uint32_t lun_id) 3472{ 3473 struct ctl_port *port; 3474 uint32_t i; 3475 3476 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3477 if (port->lun_map == NULL) 3478 return (lun_id); 3479 for (i = 0; i < CTL_MAX_LUNS; i++) { 3480 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3481 return (i); 3482 } 3483 return (UINT32_MAX); 3484} 3485 3486/* 3487 * Note: This only works for bitmask sizes that are at least 32 bits, and 3488 * that are a power of 2. 3489 */ 3490int 3491ctl_ffz(uint32_t *mask, uint32_t size) 3492{ 3493 uint32_t num_chunks, num_pieces; 3494 int i, j; 3495 3496 num_chunks = (size >> 5); 3497 if (num_chunks == 0) 3498 num_chunks++; 3499 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3500 3501 for (i = 0; i < num_chunks; i++) { 3502 for (j = 0; j < num_pieces; j++) { 3503 if ((mask[i] & (1 << j)) == 0) 3504 return ((i << 5) + j); 3505 } 3506 } 3507 3508 return (-1); 3509} 3510 3511int 3512ctl_set_mask(uint32_t *mask, uint32_t bit) 3513{ 3514 uint32_t chunk, piece; 3515 3516 chunk = bit >> 5; 3517 piece = bit % (sizeof(uint32_t) * 8); 3518 3519 if ((mask[chunk] & (1 << piece)) != 0) 3520 return (-1); 3521 else 3522 mask[chunk] |= (1 << piece); 3523 3524 return (0); 3525} 3526 3527int 3528ctl_clear_mask(uint32_t *mask, uint32_t bit) 3529{ 3530 uint32_t chunk, piece; 3531 3532 chunk = bit >> 5; 3533 piece = bit % (sizeof(uint32_t) * 8); 3534 3535 if ((mask[chunk] & (1 << piece)) == 0) 3536 return (-1); 3537 else 3538 mask[chunk] &= ~(1 << piece); 3539 3540 return (0); 3541} 3542 3543int 3544ctl_is_set(uint32_t *mask, uint32_t bit) 3545{ 3546 uint32_t chunk, piece; 3547 3548 chunk = bit >> 5; 3549 piece = bit % (sizeof(uint32_t) * 8); 3550 3551 if ((mask[chunk] & (1 << piece)) == 0) 3552 return (0); 3553 else 3554 return (1); 3555} 3556 3557#ifdef unused 3558/* 3559 * The bus, target and lun are optional, they can be filled in later. 3560 * can_wait is used to determine whether we can wait on the malloc or not. 3561 */ 3562union ctl_io* 3563ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3564 uint32_t targ_lun, int can_wait) 3565{ 3566 union ctl_io *io; 3567 3568 if (can_wait) 3569 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3570 else 3571 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3572 3573 if (io != NULL) { 3574 io->io_hdr.io_type = io_type; 3575 io->io_hdr.targ_port = targ_port; 3576 /* 3577 * XXX KDM this needs to change/go away. We need to move 3578 * to a preallocated pool of ctl_scsiio structures. 3579 */ 3580 io->io_hdr.nexus.targ_target.id = targ_target; 3581 io->io_hdr.nexus.targ_lun = targ_lun; 3582 } 3583 3584 return (io); 3585} 3586 3587void 3588ctl_kfree_io(union ctl_io *io) 3589{ 3590 free(io, M_CTL); 3591} 3592#endif /* unused */ 3593 3594/* 3595 * ctl_softc, pool_type, total_ctl_io are passed in. 3596 * npool is passed out. 3597 */ 3598int 3599ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3600 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3601{ 3602 uint32_t i; 3603 union ctl_io *cur_io, *next_io; 3604 struct ctl_io_pool *pool; 3605 int retval; 3606 3607 retval = 0; 3608 3609 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3610 M_NOWAIT | M_ZERO); 3611 if (pool == NULL) { 3612 retval = ENOMEM; 3613 goto bailout; 3614 } 3615 3616 pool->type = pool_type; 3617 pool->ctl_softc = ctl_softc; 3618 3619 mtx_lock(&ctl_softc->pool_lock); 3620 pool->id = ctl_softc->cur_pool_id++; 3621 mtx_unlock(&ctl_softc->pool_lock); 3622 3623 pool->flags = CTL_POOL_FLAG_NONE; 3624 pool->refcount = 1; /* Reference for validity. */ 3625 STAILQ_INIT(&pool->free_queue); 3626 3627 /* 3628 * XXX KDM other options here: 3629 * - allocate a page at a time 3630 * - allocate one big chunk of memory. 3631 * Page allocation might work well, but would take a little more 3632 * tracking. 3633 */ 3634 for (i = 0; i < total_ctl_io; i++) { 3635 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3636 M_NOWAIT); 3637 if (cur_io == NULL) { 3638 retval = ENOMEM; 3639 break; 3640 } 3641 cur_io->io_hdr.pool = pool; 3642 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3643 pool->total_ctl_io++; 3644 pool->free_ctl_io++; 3645 } 3646 3647 if (retval != 0) { 3648 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3649 cur_io != NULL; cur_io = next_io) { 3650 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3651 links); 3652 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3653 ctl_io_hdr, links); 3654 free(cur_io, M_CTLIO); 3655 } 3656 3657 free(pool, M_CTL); 3658 goto bailout; 3659 } 3660 mtx_lock(&ctl_softc->pool_lock); 3661 ctl_softc->num_pools++; 3662 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3663 /* 3664 * Increment our usage count if this is an external consumer, so we 3665 * can't get unloaded until the external consumer (most likely a 3666 * FETD) unloads and frees his pool. 3667 * 3668 * XXX KDM will this increment the caller's module use count, or 3669 * mine? 3670 */ 3671#if 0 3672 if ((pool_type != CTL_POOL_EMERGENCY) 3673 && (pool_type != CTL_POOL_INTERNAL) 3674 && (pool_type != CTL_POOL_4OTHERSC)) 3675 MOD_INC_USE_COUNT; 3676#endif 3677 3678 mtx_unlock(&ctl_softc->pool_lock); 3679 3680 *npool = pool; 3681 3682bailout: 3683 3684 return (retval); 3685} 3686 3687static int 3688ctl_pool_acquire(struct ctl_io_pool *pool) 3689{ 3690 3691 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3692 3693 if (pool->flags & CTL_POOL_FLAG_INVALID) 3694 return (EINVAL); 3695 3696 pool->refcount++; 3697 3698 return (0); 3699} 3700 3701static void 3702ctl_pool_release(struct ctl_io_pool *pool) 3703{ 3704 struct ctl_softc *ctl_softc = pool->ctl_softc; 3705 union ctl_io *io; 3706 3707 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3708 3709 if (--pool->refcount != 0) 3710 return; 3711 3712 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3713 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3714 links); 3715 free(io, M_CTLIO); 3716 } 3717 3718 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3719 ctl_softc->num_pools--; 3720 3721 /* 3722 * XXX KDM will this decrement the caller's usage count or mine? 3723 */ 3724#if 0 3725 if ((pool->type != CTL_POOL_EMERGENCY) 3726 && (pool->type != CTL_POOL_INTERNAL) 3727 && (pool->type != CTL_POOL_4OTHERSC)) 3728 MOD_DEC_USE_COUNT; 3729#endif 3730 3731 free(pool, M_CTL); 3732} 3733 3734void 3735ctl_pool_free(struct ctl_io_pool *pool) 3736{ 3737 struct ctl_softc *ctl_softc; 3738 3739 if (pool == NULL) 3740 return; 3741 3742 ctl_softc = pool->ctl_softc; 3743 mtx_lock(&ctl_softc->pool_lock); 3744 pool->flags |= CTL_POOL_FLAG_INVALID; 3745 ctl_pool_release(pool); 3746 mtx_unlock(&ctl_softc->pool_lock); 3747} 3748 3749/* 3750 * This routine does not block (except for spinlocks of course). 3751 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3752 * possible. 3753 */ 3754union ctl_io * 3755ctl_alloc_io(void *pool_ref) 3756{ 3757 union ctl_io *io; 3758 struct ctl_softc *ctl_softc; 3759 struct ctl_io_pool *pool, *npool; 3760 struct ctl_io_pool *emergency_pool; 3761 3762 pool = (struct ctl_io_pool *)pool_ref; 3763 3764 if (pool == NULL) { 3765 printf("%s: pool is NULL\n", __func__); 3766 return (NULL); 3767 } 3768 3769 emergency_pool = NULL; 3770 3771 ctl_softc = pool->ctl_softc; 3772 3773 mtx_lock(&ctl_softc->pool_lock); 3774 /* 3775 * First, try to get the io structure from the user's pool. 3776 */ 3777 if (ctl_pool_acquire(pool) == 0) { 3778 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3779 if (io != NULL) { 3780 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3781 pool->total_allocated++; 3782 pool->free_ctl_io--; 3783 mtx_unlock(&ctl_softc->pool_lock); 3784 return (io); 3785 } else 3786 ctl_pool_release(pool); 3787 } 3788 /* 3789 * If he doesn't have any io structures left, search for an 3790 * emergency pool and grab one from there. 3791 */ 3792 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3793 if (npool->type != CTL_POOL_EMERGENCY) 3794 continue; 3795 3796 if (ctl_pool_acquire(npool) != 0) 3797 continue; 3798 3799 emergency_pool = npool; 3800 3801 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3802 if (io != NULL) { 3803 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3804 npool->total_allocated++; 3805 npool->free_ctl_io--; 3806 mtx_unlock(&ctl_softc->pool_lock); 3807 return (io); 3808 } else 3809 ctl_pool_release(npool); 3810 } 3811 3812 /* Drop the spinlock before we malloc */ 3813 mtx_unlock(&ctl_softc->pool_lock); 3814 3815 /* 3816 * The emergency pool (if it exists) didn't have one, so try an 3817 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3818 */ 3819 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3820 if (io != NULL) { 3821 /* 3822 * If the emergency pool exists but is empty, add this 3823 * ctl_io to its list when it gets freed. 3824 */ 3825 if (emergency_pool != NULL) { 3826 mtx_lock(&ctl_softc->pool_lock); 3827 if (ctl_pool_acquire(emergency_pool) == 0) { 3828 io->io_hdr.pool = emergency_pool; 3829 emergency_pool->total_ctl_io++; 3830 /* 3831 * Need to bump this, otherwise 3832 * total_allocated and total_freed won't 3833 * match when we no longer have anything 3834 * outstanding. 3835 */ 3836 emergency_pool->total_allocated++; 3837 } 3838 mtx_unlock(&ctl_softc->pool_lock); 3839 } else 3840 io->io_hdr.pool = NULL; 3841 } 3842 3843 return (io); 3844} 3845 3846void 3847ctl_free_io(union ctl_io *io) 3848{ 3849 if (io == NULL) 3850 return; 3851 3852 /* 3853 * If this ctl_io has a pool, return it to that pool. 3854 */ 3855 if (io->io_hdr.pool != NULL) { 3856 struct ctl_io_pool *pool; 3857 3858 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3859 mtx_lock(&pool->ctl_softc->pool_lock); 3860 io->io_hdr.io_type = 0xff; 3861 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3862 pool->total_freed++; 3863 pool->free_ctl_io++; 3864 ctl_pool_release(pool); 3865 mtx_unlock(&pool->ctl_softc->pool_lock); 3866 } else { 3867 /* 3868 * Otherwise, just free it. We probably malloced it and 3869 * the emergency pool wasn't available. 3870 */ 3871 free(io, M_CTLIO); 3872 } 3873 3874} 3875 3876void 3877ctl_zero_io(union ctl_io *io) 3878{ 3879 void *pool_ref; 3880 3881 if (io == NULL) 3882 return; 3883 3884 /* 3885 * May need to preserve linked list pointers at some point too. 3886 */ 3887 pool_ref = io->io_hdr.pool; 3888 3889 memset(io, 0, sizeof(*io)); 3890 3891 io->io_hdr.pool = pool_ref; 3892} 3893 3894/* 3895 * This routine is currently used for internal copies of ctl_ios that need 3896 * to persist for some reason after we've already returned status to the 3897 * FETD. (Thus the flag set.) 3898 * 3899 * XXX XXX 3900 * Note that this makes a blind copy of all fields in the ctl_io, except 3901 * for the pool reference. This includes any memory that has been 3902 * allocated! That memory will no longer be valid after done has been 3903 * called, so this would be VERY DANGEROUS for command that actually does 3904 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3905 * start and stop commands, which don't transfer any data, so this is not a 3906 * problem. If it is used for anything else, the caller would also need to 3907 * allocate data buffer space and this routine would need to be modified to 3908 * copy the data buffer(s) as well. 3909 */ 3910void 3911ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3912{ 3913 void *pool_ref; 3914 3915 if ((src == NULL) 3916 || (dest == NULL)) 3917 return; 3918 3919 /* 3920 * May need to preserve linked list pointers at some point too. 3921 */ 3922 pool_ref = dest->io_hdr.pool; 3923 3924 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3925 3926 dest->io_hdr.pool = pool_ref; 3927 /* 3928 * We need to know that this is an internal copy, and doesn't need 3929 * to get passed back to the FETD that allocated it. 3930 */ 3931 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3932} 3933 3934#ifdef NEEDTOPORT 3935static void 3936ctl_update_power_subpage(struct copan_power_subpage *page) 3937{ 3938 int num_luns, num_partitions, config_type; 3939 struct ctl_softc *softc; 3940 cs_BOOL_t aor_present, shelf_50pct_power; 3941 cs_raidset_personality_t rs_type; 3942 int max_active_luns; 3943 3944 softc = control_softc; 3945 3946 /* subtract out the processor LUN */ 3947 num_luns = softc->num_luns - 1; 3948 /* 3949 * Default to 7 LUNs active, which was the only number we allowed 3950 * in the past. 3951 */ 3952 max_active_luns = 7; 3953 3954 num_partitions = config_GetRsPartitionInfo(); 3955 config_type = config_GetConfigType(); 3956 shelf_50pct_power = config_GetShelfPowerMode(); 3957 aor_present = config_IsAorRsPresent(); 3958 3959 rs_type = ddb_GetRsRaidType(1); 3960 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3961 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3962 EPRINT(0, "Unsupported RS type %d!", rs_type); 3963 } 3964 3965 3966 page->total_luns = num_luns; 3967 3968 switch (config_type) { 3969 case 40: 3970 /* 3971 * In a 40 drive configuration, it doesn't matter what DC 3972 * cards we have, whether we have AOR enabled or not, 3973 * partitioning or not, or what type of RAIDset we have. 3974 * In that scenario, we can power up every LUN we present 3975 * to the user. 3976 */ 3977 max_active_luns = num_luns; 3978 3979 break; 3980 case 64: 3981 if (shelf_50pct_power == CS_FALSE) { 3982 /* 25% power */ 3983 if (aor_present == CS_TRUE) { 3984 if (rs_type == 3985 CS_RAIDSET_PERSONALITY_RAID5) { 3986 max_active_luns = 7; 3987 } else if (rs_type == 3988 CS_RAIDSET_PERSONALITY_RAID1){ 3989 max_active_luns = 14; 3990 } else { 3991 /* XXX KDM now what?? */ 3992 } 3993 } else { 3994 if (rs_type == 3995 CS_RAIDSET_PERSONALITY_RAID5) { 3996 max_active_luns = 8; 3997 } else if (rs_type == 3998 CS_RAIDSET_PERSONALITY_RAID1){ 3999 max_active_luns = 16; 4000 } else { 4001 /* XXX KDM now what?? */ 4002 } 4003 } 4004 } else { 4005 /* 50% power */ 4006 /* 4007 * With 50% power in a 64 drive configuration, we 4008 * can power all LUNs we present. 4009 */ 4010 max_active_luns = num_luns; 4011 } 4012 break; 4013 case 112: 4014 if (shelf_50pct_power == CS_FALSE) { 4015 /* 25% power */ 4016 if (aor_present == CS_TRUE) { 4017 if (rs_type == 4018 CS_RAIDSET_PERSONALITY_RAID5) { 4019 max_active_luns = 7; 4020 } else if (rs_type == 4021 CS_RAIDSET_PERSONALITY_RAID1){ 4022 max_active_luns = 14; 4023 } else { 4024 /* XXX KDM now what?? */ 4025 } 4026 } else { 4027 if (rs_type == 4028 CS_RAIDSET_PERSONALITY_RAID5) { 4029 max_active_luns = 8; 4030 } else if (rs_type == 4031 CS_RAIDSET_PERSONALITY_RAID1){ 4032 max_active_luns = 16; 4033 } else { 4034 /* XXX KDM now what?? */ 4035 } 4036 } 4037 } else { 4038 /* 50% power */ 4039 if (aor_present == CS_TRUE) { 4040 if (rs_type == 4041 CS_RAIDSET_PERSONALITY_RAID5) { 4042 max_active_luns = 14; 4043 } else if (rs_type == 4044 CS_RAIDSET_PERSONALITY_RAID1){ 4045 /* 4046 * We're assuming here that disk 4047 * caching is enabled, and so we're 4048 * able to power up half of each 4049 * LUN, and cache all writes. 4050 */ 4051 max_active_luns = num_luns; 4052 } else { 4053 /* XXX KDM now what?? */ 4054 } 4055 } else { 4056 if (rs_type == 4057 CS_RAIDSET_PERSONALITY_RAID5) { 4058 max_active_luns = 15; 4059 } else if (rs_type == 4060 CS_RAIDSET_PERSONALITY_RAID1){ 4061 max_active_luns = 30; 4062 } else { 4063 /* XXX KDM now what?? */ 4064 } 4065 } 4066 } 4067 break; 4068 default: 4069 /* 4070 * In this case, we have an unknown configuration, so we 4071 * just use the default from above. 4072 */ 4073 break; 4074 } 4075 4076 page->max_active_luns = max_active_luns; 4077#if 0 4078 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4079 page->total_luns, page->max_active_luns); 4080#endif 4081} 4082#endif /* NEEDTOPORT */ 4083 4084/* 4085 * This routine could be used in the future to load default and/or saved 4086 * mode page parameters for a particuar lun. 4087 */ 4088static int 4089ctl_init_page_index(struct ctl_lun *lun) 4090{ 4091 int i; 4092 struct ctl_page_index *page_index; 4093 struct ctl_softc *softc; 4094 4095 memcpy(&lun->mode_pages.index, page_index_template, 4096 sizeof(page_index_template)); 4097 4098 softc = lun->ctl_softc; 4099 4100 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4101 4102 page_index = &lun->mode_pages.index[i]; 4103 /* 4104 * If this is a disk-only mode page, there's no point in 4105 * setting it up. For some pages, we have to have some 4106 * basic information about the disk in order to calculate the 4107 * mode page data. 4108 */ 4109 if ((lun->be_lun->lun_type != T_DIRECT) 4110 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4111 continue; 4112 4113 switch (page_index->page_code & SMPH_PC_MASK) { 4114 case SMS_FORMAT_DEVICE_PAGE: { 4115 struct scsi_format_page *format_page; 4116 4117 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4118 panic("subpage is incorrect!"); 4119 4120 /* 4121 * Sectors per track are set above. Bytes per 4122 * sector need to be set here on a per-LUN basis. 4123 */ 4124 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4125 &format_page_default, 4126 sizeof(format_page_default)); 4127 memcpy(&lun->mode_pages.format_page[ 4128 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4129 sizeof(format_page_changeable)); 4130 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4131 &format_page_default, 4132 sizeof(format_page_default)); 4133 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4134 &format_page_default, 4135 sizeof(format_page_default)); 4136 4137 format_page = &lun->mode_pages.format_page[ 4138 CTL_PAGE_CURRENT]; 4139 scsi_ulto2b(lun->be_lun->blocksize, 4140 format_page->bytes_per_sector); 4141 4142 format_page = &lun->mode_pages.format_page[ 4143 CTL_PAGE_DEFAULT]; 4144 scsi_ulto2b(lun->be_lun->blocksize, 4145 format_page->bytes_per_sector); 4146 4147 format_page = &lun->mode_pages.format_page[ 4148 CTL_PAGE_SAVED]; 4149 scsi_ulto2b(lun->be_lun->blocksize, 4150 format_page->bytes_per_sector); 4151 4152 page_index->page_data = 4153 (uint8_t *)lun->mode_pages.format_page; 4154 break; 4155 } 4156 case SMS_RIGID_DISK_PAGE: { 4157 struct scsi_rigid_disk_page *rigid_disk_page; 4158 uint32_t sectors_per_cylinder; 4159 uint64_t cylinders; 4160#ifndef __XSCALE__ 4161 int shift; 4162#endif /* !__XSCALE__ */ 4163 4164 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4165 panic("invalid subpage value %d", 4166 page_index->subpage); 4167 4168 /* 4169 * Rotation rate and sectors per track are set 4170 * above. We calculate the cylinders here based on 4171 * capacity. Due to the number of heads and 4172 * sectors per track we're using, smaller arrays 4173 * may turn out to have 0 cylinders. Linux and 4174 * FreeBSD don't pay attention to these mode pages 4175 * to figure out capacity, but Solaris does. It 4176 * seems to deal with 0 cylinders just fine, and 4177 * works out a fake geometry based on the capacity. 4178 */ 4179 memcpy(&lun->mode_pages.rigid_disk_page[ 4180 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4181 sizeof(rigid_disk_page_default)); 4182 memcpy(&lun->mode_pages.rigid_disk_page[ 4183 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4184 sizeof(rigid_disk_page_changeable)); 4185 memcpy(&lun->mode_pages.rigid_disk_page[ 4186 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4187 sizeof(rigid_disk_page_default)); 4188 memcpy(&lun->mode_pages.rigid_disk_page[ 4189 CTL_PAGE_SAVED], &rigid_disk_page_default, 4190 sizeof(rigid_disk_page_default)); 4191 4192 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4193 CTL_DEFAULT_HEADS; 4194 4195 /* 4196 * The divide method here will be more accurate, 4197 * probably, but results in floating point being 4198 * used in the kernel on i386 (__udivdi3()). On the 4199 * XScale, though, __udivdi3() is implemented in 4200 * software. 4201 * 4202 * The shift method for cylinder calculation is 4203 * accurate if sectors_per_cylinder is a power of 4204 * 2. Otherwise it might be slightly off -- you 4205 * might have a bit of a truncation problem. 4206 */ 4207#ifdef __XSCALE__ 4208 cylinders = (lun->be_lun->maxlba + 1) / 4209 sectors_per_cylinder; 4210#else 4211 for (shift = 31; shift > 0; shift--) { 4212 if (sectors_per_cylinder & (1 << shift)) 4213 break; 4214 } 4215 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4216#endif 4217 4218 /* 4219 * We've basically got 3 bytes, or 24 bits for the 4220 * cylinder size in the mode page. If we're over, 4221 * just round down to 2^24. 4222 */ 4223 if (cylinders > 0xffffff) 4224 cylinders = 0xffffff; 4225 4226 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4227 CTL_PAGE_CURRENT]; 4228 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4229 4230 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4231 CTL_PAGE_DEFAULT]; 4232 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4233 4234 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4235 CTL_PAGE_SAVED]; 4236 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4237 4238 page_index->page_data = 4239 (uint8_t *)lun->mode_pages.rigid_disk_page; 4240 break; 4241 } 4242 case SMS_CACHING_PAGE: { 4243 4244 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4245 panic("invalid subpage value %d", 4246 page_index->subpage); 4247 /* 4248 * Defaults should be okay here, no calculations 4249 * needed. 4250 */ 4251 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4252 &caching_page_default, 4253 sizeof(caching_page_default)); 4254 memcpy(&lun->mode_pages.caching_page[ 4255 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4256 sizeof(caching_page_changeable)); 4257 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4258 &caching_page_default, 4259 sizeof(caching_page_default)); 4260 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4261 &caching_page_default, 4262 sizeof(caching_page_default)); 4263 page_index->page_data = 4264 (uint8_t *)lun->mode_pages.caching_page; 4265 break; 4266 } 4267 case SMS_CONTROL_MODE_PAGE: { 4268 4269 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4270 panic("invalid subpage value %d", 4271 page_index->subpage); 4272 4273 /* 4274 * Defaults should be okay here, no calculations 4275 * needed. 4276 */ 4277 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4278 &control_page_default, 4279 sizeof(control_page_default)); 4280 memcpy(&lun->mode_pages.control_page[ 4281 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4282 sizeof(control_page_changeable)); 4283 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4284 &control_page_default, 4285 sizeof(control_page_default)); 4286 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4287 &control_page_default, 4288 sizeof(control_page_default)); 4289 page_index->page_data = 4290 (uint8_t *)lun->mode_pages.control_page; 4291 break; 4292 4293 } 4294 case SMS_VENDOR_SPECIFIC_PAGE:{ 4295 switch (page_index->subpage) { 4296 case PWR_SUBPAGE_CODE: { 4297 struct copan_power_subpage *current_page, 4298 *saved_page; 4299 4300 memcpy(&lun->mode_pages.power_subpage[ 4301 CTL_PAGE_CURRENT], 4302 &power_page_default, 4303 sizeof(power_page_default)); 4304 memcpy(&lun->mode_pages.power_subpage[ 4305 CTL_PAGE_CHANGEABLE], 4306 &power_page_changeable, 4307 sizeof(power_page_changeable)); 4308 memcpy(&lun->mode_pages.power_subpage[ 4309 CTL_PAGE_DEFAULT], 4310 &power_page_default, 4311 sizeof(power_page_default)); 4312 memcpy(&lun->mode_pages.power_subpage[ 4313 CTL_PAGE_SAVED], 4314 &power_page_default, 4315 sizeof(power_page_default)); 4316 page_index->page_data = 4317 (uint8_t *)lun->mode_pages.power_subpage; 4318 4319 current_page = (struct copan_power_subpage *) 4320 (page_index->page_data + 4321 (page_index->page_len * 4322 CTL_PAGE_CURRENT)); 4323 saved_page = (struct copan_power_subpage *) 4324 (page_index->page_data + 4325 (page_index->page_len * 4326 CTL_PAGE_SAVED)); 4327 break; 4328 } 4329 case APS_SUBPAGE_CODE: { 4330 struct copan_aps_subpage *current_page, 4331 *saved_page; 4332 4333 // This gets set multiple times but 4334 // it should always be the same. It's 4335 // only done during init so who cares. 4336 index_to_aps_page = i; 4337 4338 memcpy(&lun->mode_pages.aps_subpage[ 4339 CTL_PAGE_CURRENT], 4340 &aps_page_default, 4341 sizeof(aps_page_default)); 4342 memcpy(&lun->mode_pages.aps_subpage[ 4343 CTL_PAGE_CHANGEABLE], 4344 &aps_page_changeable, 4345 sizeof(aps_page_changeable)); 4346 memcpy(&lun->mode_pages.aps_subpage[ 4347 CTL_PAGE_DEFAULT], 4348 &aps_page_default, 4349 sizeof(aps_page_default)); 4350 memcpy(&lun->mode_pages.aps_subpage[ 4351 CTL_PAGE_SAVED], 4352 &aps_page_default, 4353 sizeof(aps_page_default)); 4354 page_index->page_data = 4355 (uint8_t *)lun->mode_pages.aps_subpage; 4356 4357 current_page = (struct copan_aps_subpage *) 4358 (page_index->page_data + 4359 (page_index->page_len * 4360 CTL_PAGE_CURRENT)); 4361 saved_page = (struct copan_aps_subpage *) 4362 (page_index->page_data + 4363 (page_index->page_len * 4364 CTL_PAGE_SAVED)); 4365 break; 4366 } 4367 case DBGCNF_SUBPAGE_CODE: { 4368 struct copan_debugconf_subpage *current_page, 4369 *saved_page; 4370 4371 memcpy(&lun->mode_pages.debugconf_subpage[ 4372 CTL_PAGE_CURRENT], 4373 &debugconf_page_default, 4374 sizeof(debugconf_page_default)); 4375 memcpy(&lun->mode_pages.debugconf_subpage[ 4376 CTL_PAGE_CHANGEABLE], 4377 &debugconf_page_changeable, 4378 sizeof(debugconf_page_changeable)); 4379 memcpy(&lun->mode_pages.debugconf_subpage[ 4380 CTL_PAGE_DEFAULT], 4381 &debugconf_page_default, 4382 sizeof(debugconf_page_default)); 4383 memcpy(&lun->mode_pages.debugconf_subpage[ 4384 CTL_PAGE_SAVED], 4385 &debugconf_page_default, 4386 sizeof(debugconf_page_default)); 4387 page_index->page_data = 4388 (uint8_t *)lun->mode_pages.debugconf_subpage; 4389 4390 current_page = (struct copan_debugconf_subpage *) 4391 (page_index->page_data + 4392 (page_index->page_len * 4393 CTL_PAGE_CURRENT)); 4394 saved_page = (struct copan_debugconf_subpage *) 4395 (page_index->page_data + 4396 (page_index->page_len * 4397 CTL_PAGE_SAVED)); 4398 break; 4399 } 4400 default: 4401 panic("invalid subpage value %d", 4402 page_index->subpage); 4403 break; 4404 } 4405 break; 4406 } 4407 default: 4408 panic("invalid page value %d", 4409 page_index->page_code & SMPH_PC_MASK); 4410 break; 4411 } 4412 } 4413 4414 return (CTL_RETVAL_COMPLETE); 4415} 4416 4417/* 4418 * LUN allocation. 4419 * 4420 * Requirements: 4421 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4422 * wants us to allocate the LUN and he can block. 4423 * - ctl_softc is always set 4424 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4425 * 4426 * Returns 0 for success, non-zero (errno) for failure. 4427 */ 4428static int 4429ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4430 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4431{ 4432 struct ctl_lun *nlun, *lun; 4433 struct ctl_port *port; 4434 struct scsi_vpd_id_descriptor *desc; 4435 struct scsi_vpd_id_t10 *t10id; 4436 const char *eui, *naa, *scsiname, *vendor; 4437 int lun_number, i, lun_malloced; 4438 int devidlen, idlen1, idlen2 = 0, len; 4439 4440 if (be_lun == NULL) 4441 return (EINVAL); 4442 4443 /* 4444 * We currently only support Direct Access or Processor LUN types. 4445 */ 4446 switch (be_lun->lun_type) { 4447 case T_DIRECT: 4448 break; 4449 case T_PROCESSOR: 4450 break; 4451 case T_SEQUENTIAL: 4452 case T_CHANGER: 4453 default: 4454 be_lun->lun_config_status(be_lun->be_lun, 4455 CTL_LUN_CONFIG_FAILURE); 4456 break; 4457 } 4458 if (ctl_lun == NULL) { 4459 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4460 lun_malloced = 1; 4461 } else { 4462 lun_malloced = 0; 4463 lun = ctl_lun; 4464 } 4465 4466 memset(lun, 0, sizeof(*lun)); 4467 if (lun_malloced) 4468 lun->flags = CTL_LUN_MALLOCED; 4469 4470 /* Generate LUN ID. */ 4471 devidlen = max(CTL_DEVID_MIN_LEN, 4472 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4473 idlen1 = sizeof(*t10id) + devidlen; 4474 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4475 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4476 if (scsiname != NULL) { 4477 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4478 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4479 } 4480 eui = ctl_get_opt(&be_lun->options, "eui"); 4481 if (eui != NULL) { 4482 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4483 } 4484 naa = ctl_get_opt(&be_lun->options, "naa"); 4485 if (naa != NULL) { 4486 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4487 } 4488 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4489 M_CTL, M_WAITOK | M_ZERO); 4490 lun->lun_devid->len = len; 4491 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4492 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4493 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4494 desc->length = idlen1; 4495 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4496 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4497 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4498 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4499 } else { 4500 strncpy(t10id->vendor, vendor, 4501 min(sizeof(t10id->vendor), strlen(vendor))); 4502 } 4503 strncpy((char *)t10id->vendor_spec_id, 4504 (char *)be_lun->device_id, devidlen); 4505 if (scsiname != NULL) { 4506 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4507 desc->length); 4508 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4509 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4510 SVPD_ID_TYPE_SCSI_NAME; 4511 desc->length = idlen2; 4512 strlcpy(desc->identifier, scsiname, idlen2); 4513 } 4514 if (eui != NULL) { 4515 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4516 desc->length); 4517 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4518 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4519 SVPD_ID_TYPE_EUI64; 4520 desc->length = 8; 4521 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4522 } 4523 if (naa != NULL) { 4524 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4525 desc->length); 4526 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4527 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4528 SVPD_ID_TYPE_NAA; 4529 desc->length = 8; 4530 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4531 } 4532 4533 mtx_lock(&ctl_softc->ctl_lock); 4534 /* 4535 * See if the caller requested a particular LUN number. If so, see 4536 * if it is available. Otherwise, allocate the first available LUN. 4537 */ 4538 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4539 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4540 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4541 mtx_unlock(&ctl_softc->ctl_lock); 4542 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4543 printf("ctl: requested LUN ID %d is higher " 4544 "than CTL_MAX_LUNS - 1 (%d)\n", 4545 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4546 } else { 4547 /* 4548 * XXX KDM return an error, or just assign 4549 * another LUN ID in this case?? 4550 */ 4551 printf("ctl: requested LUN ID %d is already " 4552 "in use\n", be_lun->req_lun_id); 4553 } 4554 if (lun->flags & CTL_LUN_MALLOCED) 4555 free(lun, M_CTL); 4556 be_lun->lun_config_status(be_lun->be_lun, 4557 CTL_LUN_CONFIG_FAILURE); 4558 return (ENOSPC); 4559 } 4560 lun_number = be_lun->req_lun_id; 4561 } else { 4562 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4563 if (lun_number == -1) { 4564 mtx_unlock(&ctl_softc->ctl_lock); 4565 printf("ctl: can't allocate LUN on target %ju, out of " 4566 "LUNs\n", (uintmax_t)target_id.id); 4567 if (lun->flags & CTL_LUN_MALLOCED) 4568 free(lun, M_CTL); 4569 be_lun->lun_config_status(be_lun->be_lun, 4570 CTL_LUN_CONFIG_FAILURE); 4571 return (ENOSPC); 4572 } 4573 } 4574 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4575 4576 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4577 lun->target = target_id; 4578 lun->lun = lun_number; 4579 lun->be_lun = be_lun; 4580 /* 4581 * The processor LUN is always enabled. Disk LUNs come on line 4582 * disabled, and must be enabled by the backend. 4583 */ 4584 lun->flags |= CTL_LUN_DISABLED; 4585 lun->backend = be_lun->be; 4586 be_lun->ctl_lun = lun; 4587 be_lun->lun_id = lun_number; 4588 atomic_add_int(&be_lun->be->num_luns, 1); 4589 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4590 lun->flags |= CTL_LUN_STOPPED; 4591 4592 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4593 lun->flags |= CTL_LUN_INOPERABLE; 4594 4595 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4596 lun->flags |= CTL_LUN_PRIMARY_SC; 4597 4598 lun->ctl_softc = ctl_softc; 4599 TAILQ_INIT(&lun->ooa_queue); 4600 TAILQ_INIT(&lun->blocked_queue); 4601 STAILQ_INIT(&lun->error_list); 4602 ctl_tpc_lun_init(lun); 4603 4604 /* 4605 * Initialize the mode page index. 4606 */ 4607 ctl_init_page_index(lun); 4608 4609 /* 4610 * Set the poweron UA for all initiators on this LUN only. 4611 */ 4612 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4613 lun->pending_ua[i] = CTL_UA_POWERON; 4614 4615 /* 4616 * Now, before we insert this lun on the lun list, set the lun 4617 * inventory changed UA for all other luns. 4618 */ 4619 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4620 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4621 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4622 } 4623 } 4624 4625 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4626 4627 ctl_softc->ctl_luns[lun_number] = lun; 4628 4629 ctl_softc->num_luns++; 4630 4631 /* Setup statistics gathering */ 4632 lun->stats.device_type = be_lun->lun_type; 4633 lun->stats.lun_number = lun_number; 4634 if (lun->stats.device_type == T_DIRECT) 4635 lun->stats.blocksize = be_lun->blocksize; 4636 else 4637 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4638 for (i = 0;i < CTL_MAX_PORTS;i++) 4639 lun->stats.ports[i].targ_port = i; 4640 4641 mtx_unlock(&ctl_softc->ctl_lock); 4642 4643 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4644 4645 /* 4646 * Run through each registered FETD and bring it online if it isn't 4647 * already. Enable the target ID if it hasn't been enabled, and 4648 * enable this particular LUN. 4649 */ 4650 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4651 int retval; 4652 4653 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4654 if (retval != 0) { 4655 printf("ctl_alloc_lun: FETD %s port %d returned error " 4656 "%d for lun_enable on target %ju lun %d\n", 4657 port->port_name, port->targ_port, retval, 4658 (uintmax_t)target_id.id, lun_number); 4659 } else 4660 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4661 } 4662 return (0); 4663} 4664 4665/* 4666 * Delete a LUN. 4667 * Assumptions: 4668 * - LUN has already been marked invalid and any pending I/O has been taken 4669 * care of. 4670 */ 4671static int 4672ctl_free_lun(struct ctl_lun *lun) 4673{ 4674 struct ctl_softc *softc; 4675#if 0 4676 struct ctl_port *port; 4677#endif 4678 struct ctl_lun *nlun; 4679 int i; 4680 4681 softc = lun->ctl_softc; 4682 4683 mtx_assert(&softc->ctl_lock, MA_OWNED); 4684 4685 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4686 4687 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4688 4689 softc->ctl_luns[lun->lun] = NULL; 4690 4691 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4692 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4693 4694 softc->num_luns--; 4695 4696 /* 4697 * XXX KDM this scheme only works for a single target/multiple LUN 4698 * setup. It needs to be revamped for a multiple target scheme. 4699 * 4700 * XXX KDM this results in port->lun_disable() getting called twice, 4701 * once when ctl_disable_lun() is called, and a second time here. 4702 * We really need to re-think the LUN disable semantics. There 4703 * should probably be several steps/levels to LUN removal: 4704 * - disable 4705 * - invalidate 4706 * - free 4707 * 4708 * Right now we only have a disable method when communicating to 4709 * the front end ports, at least for individual LUNs. 4710 */ 4711#if 0 4712 STAILQ_FOREACH(port, &softc->port_list, links) { 4713 int retval; 4714 4715 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4716 lun->lun); 4717 if (retval != 0) { 4718 printf("ctl_free_lun: FETD %s port %d returned error " 4719 "%d for lun_disable on target %ju lun %jd\n", 4720 port->port_name, port->targ_port, retval, 4721 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4722 } 4723 4724 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4725 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4726 4727 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4728 if (retval != 0) { 4729 printf("ctl_free_lun: FETD %s port %d " 4730 "returned error %d for targ_disable on " 4731 "target %ju\n", port->port_name, 4732 port->targ_port, retval, 4733 (uintmax_t)lun->target.id); 4734 } else 4735 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4736 4737 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4738 continue; 4739 4740#if 0 4741 port->port_offline(port->onoff_arg); 4742 port->status &= ~CTL_PORT_STATUS_ONLINE; 4743#endif 4744 } 4745 } 4746#endif 4747 4748 /* 4749 * Tell the backend to free resources, if this LUN has a backend. 4750 */ 4751 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4752 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4753 4754 ctl_tpc_lun_shutdown(lun); 4755 mtx_destroy(&lun->lun_lock); 4756 free(lun->lun_devid, M_CTL); 4757 if (lun->flags & CTL_LUN_MALLOCED) 4758 free(lun, M_CTL); 4759 4760 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4761 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4762 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4763 } 4764 } 4765 4766 return (0); 4767} 4768 4769static void 4770ctl_create_lun(struct ctl_be_lun *be_lun) 4771{ 4772 struct ctl_softc *ctl_softc; 4773 4774 ctl_softc = control_softc; 4775 4776 /* 4777 * ctl_alloc_lun() should handle all potential failure cases. 4778 */ 4779 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4780} 4781 4782int 4783ctl_add_lun(struct ctl_be_lun *be_lun) 4784{ 4785 struct ctl_softc *ctl_softc = control_softc; 4786 4787 mtx_lock(&ctl_softc->ctl_lock); 4788 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4789 mtx_unlock(&ctl_softc->ctl_lock); 4790 wakeup(&ctl_softc->pending_lun_queue); 4791 4792 return (0); 4793} 4794 4795int 4796ctl_enable_lun(struct ctl_be_lun *be_lun) 4797{ 4798 struct ctl_softc *ctl_softc; 4799 struct ctl_port *port, *nport; 4800 struct ctl_lun *lun; 4801 int retval; 4802 4803 ctl_softc = control_softc; 4804 4805 lun = (struct ctl_lun *)be_lun->ctl_lun; 4806 4807 mtx_lock(&ctl_softc->ctl_lock); 4808 mtx_lock(&lun->lun_lock); 4809 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4810 /* 4811 * eh? Why did we get called if the LUN is already 4812 * enabled? 4813 */ 4814 mtx_unlock(&lun->lun_lock); 4815 mtx_unlock(&ctl_softc->ctl_lock); 4816 return (0); 4817 } 4818 lun->flags &= ~CTL_LUN_DISABLED; 4819 mtx_unlock(&lun->lun_lock); 4820 4821 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4822 nport = STAILQ_NEXT(port, links); 4823 4824 /* 4825 * Drop the lock while we call the FETD's enable routine. 4826 * This can lead to a callback into CTL (at least in the 4827 * case of the internal initiator frontend. 4828 */ 4829 mtx_unlock(&ctl_softc->ctl_lock); 4830 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4831 mtx_lock(&ctl_softc->ctl_lock); 4832 if (retval != 0) { 4833 printf("%s: FETD %s port %d returned error " 4834 "%d for lun_enable on target %ju lun %jd\n", 4835 __func__, port->port_name, port->targ_port, retval, 4836 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4837 } 4838#if 0 4839 else { 4840 /* NOTE: TODO: why does lun enable affect port status? */ 4841 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4842 } 4843#endif 4844 } 4845 4846 mtx_unlock(&ctl_softc->ctl_lock); 4847 4848 return (0); 4849} 4850 4851int 4852ctl_disable_lun(struct ctl_be_lun *be_lun) 4853{ 4854 struct ctl_softc *ctl_softc; 4855 struct ctl_port *port; 4856 struct ctl_lun *lun; 4857 int retval; 4858 4859 ctl_softc = control_softc; 4860 4861 lun = (struct ctl_lun *)be_lun->ctl_lun; 4862 4863 mtx_lock(&ctl_softc->ctl_lock); 4864 mtx_lock(&lun->lun_lock); 4865 if (lun->flags & CTL_LUN_DISABLED) { 4866 mtx_unlock(&lun->lun_lock); 4867 mtx_unlock(&ctl_softc->ctl_lock); 4868 return (0); 4869 } 4870 lun->flags |= CTL_LUN_DISABLED; 4871 mtx_unlock(&lun->lun_lock); 4872 4873 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4874 mtx_unlock(&ctl_softc->ctl_lock); 4875 /* 4876 * Drop the lock before we call the frontend's disable 4877 * routine, to avoid lock order reversals. 4878 * 4879 * XXX KDM what happens if the frontend list changes while 4880 * we're traversing it? It's unlikely, but should be handled. 4881 */ 4882 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4883 lun->lun); 4884 mtx_lock(&ctl_softc->ctl_lock); 4885 if (retval != 0) { 4886 printf("ctl_alloc_lun: FETD %s port %d returned error " 4887 "%d for lun_disable on target %ju lun %jd\n", 4888 port->port_name, port->targ_port, retval, 4889 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4890 } 4891 } 4892 4893 mtx_unlock(&ctl_softc->ctl_lock); 4894 4895 return (0); 4896} 4897 4898int 4899ctl_start_lun(struct ctl_be_lun *be_lun) 4900{ 4901 struct ctl_softc *ctl_softc; 4902 struct ctl_lun *lun; 4903 4904 ctl_softc = control_softc; 4905 4906 lun = (struct ctl_lun *)be_lun->ctl_lun; 4907 4908 mtx_lock(&lun->lun_lock); 4909 lun->flags &= ~CTL_LUN_STOPPED; 4910 mtx_unlock(&lun->lun_lock); 4911 4912 return (0); 4913} 4914 4915int 4916ctl_stop_lun(struct ctl_be_lun *be_lun) 4917{ 4918 struct ctl_softc *ctl_softc; 4919 struct ctl_lun *lun; 4920 4921 ctl_softc = control_softc; 4922 4923 lun = (struct ctl_lun *)be_lun->ctl_lun; 4924 4925 mtx_lock(&lun->lun_lock); 4926 lun->flags |= CTL_LUN_STOPPED; 4927 mtx_unlock(&lun->lun_lock); 4928 4929 return (0); 4930} 4931 4932int 4933ctl_lun_offline(struct ctl_be_lun *be_lun) 4934{ 4935 struct ctl_softc *ctl_softc; 4936 struct ctl_lun *lun; 4937 4938 ctl_softc = control_softc; 4939 4940 lun = (struct ctl_lun *)be_lun->ctl_lun; 4941 4942 mtx_lock(&lun->lun_lock); 4943 lun->flags |= CTL_LUN_OFFLINE; 4944 mtx_unlock(&lun->lun_lock); 4945 4946 return (0); 4947} 4948 4949int 4950ctl_lun_online(struct ctl_be_lun *be_lun) 4951{ 4952 struct ctl_softc *ctl_softc; 4953 struct ctl_lun *lun; 4954 4955 ctl_softc = control_softc; 4956 4957 lun = (struct ctl_lun *)be_lun->ctl_lun; 4958 4959 mtx_lock(&lun->lun_lock); 4960 lun->flags &= ~CTL_LUN_OFFLINE; 4961 mtx_unlock(&lun->lun_lock); 4962 4963 return (0); 4964} 4965 4966int 4967ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4968{ 4969 struct ctl_softc *ctl_softc; 4970 struct ctl_lun *lun; 4971 4972 ctl_softc = control_softc; 4973 4974 lun = (struct ctl_lun *)be_lun->ctl_lun; 4975 4976 mtx_lock(&lun->lun_lock); 4977 4978 /* 4979 * The LUN needs to be disabled before it can be marked invalid. 4980 */ 4981 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4982 mtx_unlock(&lun->lun_lock); 4983 return (-1); 4984 } 4985 /* 4986 * Mark the LUN invalid. 4987 */ 4988 lun->flags |= CTL_LUN_INVALID; 4989 4990 /* 4991 * If there is nothing in the OOA queue, go ahead and free the LUN. 4992 * If we have something in the OOA queue, we'll free it when the 4993 * last I/O completes. 4994 */ 4995 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4996 mtx_unlock(&lun->lun_lock); 4997 mtx_lock(&ctl_softc->ctl_lock); 4998 ctl_free_lun(lun); 4999 mtx_unlock(&ctl_softc->ctl_lock); 5000 } else 5001 mtx_unlock(&lun->lun_lock); 5002 5003 return (0); 5004} 5005 5006int 5007ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5008{ 5009 struct ctl_softc *ctl_softc; 5010 struct ctl_lun *lun; 5011 5012 ctl_softc = control_softc; 5013 lun = (struct ctl_lun *)be_lun->ctl_lun; 5014 5015 mtx_lock(&lun->lun_lock); 5016 lun->flags |= CTL_LUN_INOPERABLE; 5017 mtx_unlock(&lun->lun_lock); 5018 5019 return (0); 5020} 5021 5022int 5023ctl_lun_operable(struct ctl_be_lun *be_lun) 5024{ 5025 struct ctl_softc *ctl_softc; 5026 struct ctl_lun *lun; 5027 5028 ctl_softc = control_softc; 5029 lun = (struct ctl_lun *)be_lun->ctl_lun; 5030 5031 mtx_lock(&lun->lun_lock); 5032 lun->flags &= ~CTL_LUN_INOPERABLE; 5033 mtx_unlock(&lun->lun_lock); 5034 5035 return (0); 5036} 5037 5038int 5039ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5040 int lock) 5041{ 5042 struct ctl_softc *softc; 5043 struct ctl_lun *lun; 5044 struct copan_aps_subpage *current_sp; 5045 struct ctl_page_index *page_index; 5046 int i; 5047 5048 softc = control_softc; 5049 5050 mtx_lock(&softc->ctl_lock); 5051 5052 lun = (struct ctl_lun *)be_lun->ctl_lun; 5053 mtx_lock(&lun->lun_lock); 5054 5055 page_index = NULL; 5056 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5057 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5058 APS_PAGE_CODE) 5059 continue; 5060 5061 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5062 continue; 5063 page_index = &lun->mode_pages.index[i]; 5064 } 5065 5066 if (page_index == NULL) { 5067 mtx_unlock(&lun->lun_lock); 5068 mtx_unlock(&softc->ctl_lock); 5069 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5070 (uintmax_t)lun->lun); 5071 return (1); 5072 } 5073#if 0 5074 if ((softc->aps_locked_lun != 0) 5075 && (softc->aps_locked_lun != lun->lun)) { 5076 printf("%s: attempt to lock LUN %llu when %llu is already " 5077 "locked\n"); 5078 mtx_unlock(&lun->lun_lock); 5079 mtx_unlock(&softc->ctl_lock); 5080 return (1); 5081 } 5082#endif 5083 5084 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5085 (page_index->page_len * CTL_PAGE_CURRENT)); 5086 5087 if (lock != 0) { 5088 current_sp->lock_active = APS_LOCK_ACTIVE; 5089 softc->aps_locked_lun = lun->lun; 5090 } else { 5091 current_sp->lock_active = 0; 5092 softc->aps_locked_lun = 0; 5093 } 5094 5095 5096 /* 5097 * If we're in HA mode, try to send the lock message to the other 5098 * side. 5099 */ 5100 if (ctl_is_single == 0) { 5101 int isc_retval; 5102 union ctl_ha_msg lock_msg; 5103 5104 lock_msg.hdr.nexus = *nexus; 5105 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5106 if (lock != 0) 5107 lock_msg.aps.lock_flag = 1; 5108 else 5109 lock_msg.aps.lock_flag = 0; 5110 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5111 sizeof(lock_msg), 0); 5112 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5113 printf("%s: APS (lock=%d) error returned from " 5114 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5115 mtx_unlock(&lun->lun_lock); 5116 mtx_unlock(&softc->ctl_lock); 5117 return (1); 5118 } 5119 } 5120 5121 mtx_unlock(&lun->lun_lock); 5122 mtx_unlock(&softc->ctl_lock); 5123 5124 return (0); 5125} 5126 5127void 5128ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5129{ 5130 struct ctl_lun *lun; 5131 struct ctl_softc *softc; 5132 int i; 5133 5134 softc = control_softc; 5135 5136 lun = (struct ctl_lun *)be_lun->ctl_lun; 5137 5138 mtx_lock(&lun->lun_lock); 5139 5140 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5141 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5142 5143 mtx_unlock(&lun->lun_lock); 5144} 5145 5146/* 5147 * Backend "memory move is complete" callback for requests that never 5148 * make it down to say RAIDCore's configuration code. 5149 */ 5150int 5151ctl_config_move_done(union ctl_io *io) 5152{ 5153 int retval; 5154 5155 retval = CTL_RETVAL_COMPLETE; 5156 5157 5158 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5159 /* 5160 * XXX KDM this shouldn't happen, but what if it does? 5161 */ 5162 if (io->io_hdr.io_type != CTL_IO_SCSI) 5163 panic("I/O type isn't CTL_IO_SCSI!"); 5164 5165 if ((io->io_hdr.port_status == 0) 5166 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5167 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5168 io->io_hdr.status = CTL_SUCCESS; 5169 else if ((io->io_hdr.port_status != 0) 5170 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5171 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5172 /* 5173 * For hardware error sense keys, the sense key 5174 * specific value is defined to be a retry count, 5175 * but we use it to pass back an internal FETD 5176 * error code. XXX KDM Hopefully the FETD is only 5177 * using 16 bits for an error code, since that's 5178 * all the space we have in the sks field. 5179 */ 5180 ctl_set_internal_failure(&io->scsiio, 5181 /*sks_valid*/ 1, 5182 /*retry_count*/ 5183 io->io_hdr.port_status); 5184 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5185 free(io->scsiio.kern_data_ptr, M_CTL); 5186 ctl_done(io); 5187 goto bailout; 5188 } 5189 5190 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5191 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5192 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5193 /* 5194 * XXX KDM just assuming a single pointer here, and not a 5195 * S/G list. If we start using S/G lists for config data, 5196 * we'll need to know how to clean them up here as well. 5197 */ 5198 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5199 free(io->scsiio.kern_data_ptr, M_CTL); 5200 /* Hopefully the user has already set the status... */ 5201 ctl_done(io); 5202 } else { 5203 /* 5204 * XXX KDM now we need to continue data movement. Some 5205 * options: 5206 * - call ctl_scsiio() again? We don't do this for data 5207 * writes, because for those at least we know ahead of 5208 * time where the write will go and how long it is. For 5209 * config writes, though, that information is largely 5210 * contained within the write itself, thus we need to 5211 * parse out the data again. 5212 * 5213 * - Call some other function once the data is in? 5214 */ 5215 5216 /* 5217 * XXX KDM call ctl_scsiio() again for now, and check flag 5218 * bits to see whether we're allocated or not. 5219 */ 5220 retval = ctl_scsiio(&io->scsiio); 5221 } 5222bailout: 5223 return (retval); 5224} 5225 5226/* 5227 * This gets called by a backend driver when it is done with a 5228 * data_submit method. 5229 */ 5230void 5231ctl_data_submit_done(union ctl_io *io) 5232{ 5233 /* 5234 * If the IO_CONT flag is set, we need to call the supplied 5235 * function to continue processing the I/O, instead of completing 5236 * the I/O just yet. 5237 * 5238 * If there is an error, though, we don't want to keep processing. 5239 * Instead, just send status back to the initiator. 5240 */ 5241 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5242 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5243 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5244 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5245 io->scsiio.io_cont(io); 5246 return; 5247 } 5248 ctl_done(io); 5249} 5250 5251/* 5252 * This gets called by a backend driver when it is done with a 5253 * configuration write. 5254 */ 5255void 5256ctl_config_write_done(union ctl_io *io) 5257{ 5258 /* 5259 * If the IO_CONT flag is set, we need to call the supplied 5260 * function to continue processing the I/O, instead of completing 5261 * the I/O just yet. 5262 * 5263 * If there is an error, though, we don't want to keep processing. 5264 * Instead, just send status back to the initiator. 5265 */ 5266 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5267 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5268 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5269 io->scsiio.io_cont(io); 5270 return; 5271 } 5272 /* 5273 * Since a configuration write can be done for commands that actually 5274 * have data allocated, like write buffer, and commands that have 5275 * no data, like start/stop unit, we need to check here. 5276 */ 5277 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5278 free(io->scsiio.kern_data_ptr, M_CTL); 5279 ctl_done(io); 5280} 5281 5282/* 5283 * SCSI release command. 5284 */ 5285int 5286ctl_scsi_release(struct ctl_scsiio *ctsio) 5287{ 5288 int length, longid, thirdparty_id, resv_id; 5289 struct ctl_softc *ctl_softc; 5290 struct ctl_lun *lun; 5291 5292 length = 0; 5293 resv_id = 0; 5294 5295 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5296 5297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5298 ctl_softc = control_softc; 5299 5300 switch (ctsio->cdb[0]) { 5301 case RELEASE_10: { 5302 struct scsi_release_10 *cdb; 5303 5304 cdb = (struct scsi_release_10 *)ctsio->cdb; 5305 5306 if (cdb->byte2 & SR10_LONGID) 5307 longid = 1; 5308 else 5309 thirdparty_id = cdb->thirdparty_id; 5310 5311 resv_id = cdb->resv_id; 5312 length = scsi_2btoul(cdb->length); 5313 break; 5314 } 5315 } 5316 5317 5318 /* 5319 * XXX KDM right now, we only support LUN reservation. We don't 5320 * support 3rd party reservations, or extent reservations, which 5321 * might actually need the parameter list. If we've gotten this 5322 * far, we've got a LUN reservation. Anything else got kicked out 5323 * above. So, according to SPC, ignore the length. 5324 */ 5325 length = 0; 5326 5327 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5328 && (length > 0)) { 5329 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5330 ctsio->kern_data_len = length; 5331 ctsio->kern_total_len = length; 5332 ctsio->kern_data_resid = 0; 5333 ctsio->kern_rel_offset = 0; 5334 ctsio->kern_sg_entries = 0; 5335 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5336 ctsio->be_move_done = ctl_config_move_done; 5337 ctl_datamove((union ctl_io *)ctsio); 5338 5339 return (CTL_RETVAL_COMPLETE); 5340 } 5341 5342 if (length > 0) 5343 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5344 5345 mtx_lock(&lun->lun_lock); 5346 5347 /* 5348 * According to SPC, it is not an error for an intiator to attempt 5349 * to release a reservation on a LUN that isn't reserved, or that 5350 * is reserved by another initiator. The reservation can only be 5351 * released, though, by the initiator who made it or by one of 5352 * several reset type events. 5353 */ 5354 if (lun->flags & CTL_LUN_RESERVED) { 5355 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5356 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5357 && (ctsio->io_hdr.nexus.targ_target.id == 5358 lun->rsv_nexus.targ_target.id)) { 5359 lun->flags &= ~CTL_LUN_RESERVED; 5360 } 5361 } 5362 5363 mtx_unlock(&lun->lun_lock); 5364 5365 ctsio->scsi_status = SCSI_STATUS_OK; 5366 ctsio->io_hdr.status = CTL_SUCCESS; 5367 5368 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5369 free(ctsio->kern_data_ptr, M_CTL); 5370 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5371 } 5372 5373 ctl_done((union ctl_io *)ctsio); 5374 return (CTL_RETVAL_COMPLETE); 5375} 5376 5377int 5378ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5379{ 5380 int extent, thirdparty, longid; 5381 int resv_id, length; 5382 uint64_t thirdparty_id; 5383 struct ctl_softc *ctl_softc; 5384 struct ctl_lun *lun; 5385 5386 extent = 0; 5387 thirdparty = 0; 5388 longid = 0; 5389 resv_id = 0; 5390 length = 0; 5391 thirdparty_id = 0; 5392 5393 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5394 5395 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5396 ctl_softc = control_softc; 5397 5398 switch (ctsio->cdb[0]) { 5399 case RESERVE_10: { 5400 struct scsi_reserve_10 *cdb; 5401 5402 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5403 5404 if (cdb->byte2 & SR10_LONGID) 5405 longid = 1; 5406 else 5407 thirdparty_id = cdb->thirdparty_id; 5408 5409 resv_id = cdb->resv_id; 5410 length = scsi_2btoul(cdb->length); 5411 break; 5412 } 5413 } 5414 5415 /* 5416 * XXX KDM right now, we only support LUN reservation. We don't 5417 * support 3rd party reservations, or extent reservations, which 5418 * might actually need the parameter list. If we've gotten this 5419 * far, we've got a LUN reservation. Anything else got kicked out 5420 * above. So, according to SPC, ignore the length. 5421 */ 5422 length = 0; 5423 5424 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5425 && (length > 0)) { 5426 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5427 ctsio->kern_data_len = length; 5428 ctsio->kern_total_len = length; 5429 ctsio->kern_data_resid = 0; 5430 ctsio->kern_rel_offset = 0; 5431 ctsio->kern_sg_entries = 0; 5432 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5433 ctsio->be_move_done = ctl_config_move_done; 5434 ctl_datamove((union ctl_io *)ctsio); 5435 5436 return (CTL_RETVAL_COMPLETE); 5437 } 5438 5439 if (length > 0) 5440 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5441 5442 mtx_lock(&lun->lun_lock); 5443 if (lun->flags & CTL_LUN_RESERVED) { 5444 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5445 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5446 || (ctsio->io_hdr.nexus.targ_target.id != 5447 lun->rsv_nexus.targ_target.id)) { 5448 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5449 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5450 goto bailout; 5451 } 5452 } 5453 5454 lun->flags |= CTL_LUN_RESERVED; 5455 lun->rsv_nexus = ctsio->io_hdr.nexus; 5456 5457 ctsio->scsi_status = SCSI_STATUS_OK; 5458 ctsio->io_hdr.status = CTL_SUCCESS; 5459 5460bailout: 5461 mtx_unlock(&lun->lun_lock); 5462 5463 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5464 free(ctsio->kern_data_ptr, M_CTL); 5465 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5466 } 5467 5468 ctl_done((union ctl_io *)ctsio); 5469 return (CTL_RETVAL_COMPLETE); 5470} 5471 5472int 5473ctl_start_stop(struct ctl_scsiio *ctsio) 5474{ 5475 struct scsi_start_stop_unit *cdb; 5476 struct ctl_lun *lun; 5477 struct ctl_softc *ctl_softc; 5478 int retval; 5479 5480 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5481 5482 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5483 ctl_softc = control_softc; 5484 retval = 0; 5485 5486 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5487 5488 /* 5489 * XXX KDM 5490 * We don't support the immediate bit on a stop unit. In order to 5491 * do that, we would need to code up a way to know that a stop is 5492 * pending, and hold off any new commands until it completes, one 5493 * way or another. Then we could accept or reject those commands 5494 * depending on its status. We would almost need to do the reverse 5495 * of what we do below for an immediate start -- return the copy of 5496 * the ctl_io to the FETD with status to send to the host (and to 5497 * free the copy!) and then free the original I/O once the stop 5498 * actually completes. That way, the OOA queue mechanism can work 5499 * to block commands that shouldn't proceed. Another alternative 5500 * would be to put the copy in the queue in place of the original, 5501 * and return the original back to the caller. That could be 5502 * slightly safer.. 5503 */ 5504 if ((cdb->byte2 & SSS_IMMED) 5505 && ((cdb->how & SSS_START) == 0)) { 5506 ctl_set_invalid_field(ctsio, 5507 /*sks_valid*/ 1, 5508 /*command*/ 1, 5509 /*field*/ 1, 5510 /*bit_valid*/ 1, 5511 /*bit*/ 0); 5512 ctl_done((union ctl_io *)ctsio); 5513 return (CTL_RETVAL_COMPLETE); 5514 } 5515 5516 if ((lun->flags & CTL_LUN_PR_RESERVED) 5517 && ((cdb->how & SSS_START)==0)) { 5518 uint32_t residx; 5519 5520 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5521 if (!lun->per_res[residx].registered 5522 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5523 5524 ctl_set_reservation_conflict(ctsio); 5525 ctl_done((union ctl_io *)ctsio); 5526 return (CTL_RETVAL_COMPLETE); 5527 } 5528 } 5529 5530 /* 5531 * If there is no backend on this device, we can't start or stop 5532 * it. In theory we shouldn't get any start/stop commands in the 5533 * first place at this level if the LUN doesn't have a backend. 5534 * That should get stopped by the command decode code. 5535 */ 5536 if (lun->backend == NULL) { 5537 ctl_set_invalid_opcode(ctsio); 5538 ctl_done((union ctl_io *)ctsio); 5539 return (CTL_RETVAL_COMPLETE); 5540 } 5541 5542 /* 5543 * XXX KDM Copan-specific offline behavior. 5544 * Figure out a reasonable way to port this? 5545 */ 5546#ifdef NEEDTOPORT 5547 mtx_lock(&lun->lun_lock); 5548 5549 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5550 && (lun->flags & CTL_LUN_OFFLINE)) { 5551 /* 5552 * If the LUN is offline, and the on/offline bit isn't set, 5553 * reject the start or stop. Otherwise, let it through. 5554 */ 5555 mtx_unlock(&lun->lun_lock); 5556 ctl_set_lun_not_ready(ctsio); 5557 ctl_done((union ctl_io *)ctsio); 5558 } else { 5559 mtx_unlock(&lun->lun_lock); 5560#endif /* NEEDTOPORT */ 5561 /* 5562 * This could be a start or a stop when we're online, 5563 * or a stop/offline or start/online. A start or stop when 5564 * we're offline is covered in the case above. 5565 */ 5566 /* 5567 * In the non-immediate case, we send the request to 5568 * the backend and return status to the user when 5569 * it is done. 5570 * 5571 * In the immediate case, we allocate a new ctl_io 5572 * to hold a copy of the request, and send that to 5573 * the backend. We then set good status on the 5574 * user's request and return it immediately. 5575 */ 5576 if (cdb->byte2 & SSS_IMMED) { 5577 union ctl_io *new_io; 5578 5579 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5580 if (new_io == NULL) { 5581 ctl_set_busy(ctsio); 5582 ctl_done((union ctl_io *)ctsio); 5583 } else { 5584 ctl_copy_io((union ctl_io *)ctsio, 5585 new_io); 5586 retval = lun->backend->config_write(new_io); 5587 ctl_set_success(ctsio); 5588 ctl_done((union ctl_io *)ctsio); 5589 } 5590 } else { 5591 retval = lun->backend->config_write( 5592 (union ctl_io *)ctsio); 5593 } 5594#ifdef NEEDTOPORT 5595 } 5596#endif 5597 return (retval); 5598} 5599 5600/* 5601 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5602 * we don't really do anything with the LBA and length fields if the user 5603 * passes them in. Instead we'll just flush out the cache for the entire 5604 * LUN. 5605 */ 5606int 5607ctl_sync_cache(struct ctl_scsiio *ctsio) 5608{ 5609 struct ctl_lun *lun; 5610 struct ctl_softc *ctl_softc; 5611 uint64_t starting_lba; 5612 uint32_t block_count; 5613 int retval; 5614 5615 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5616 5617 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5618 ctl_softc = control_softc; 5619 retval = 0; 5620 5621 switch (ctsio->cdb[0]) { 5622 case SYNCHRONIZE_CACHE: { 5623 struct scsi_sync_cache *cdb; 5624 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5625 5626 starting_lba = scsi_4btoul(cdb->begin_lba); 5627 block_count = scsi_2btoul(cdb->lb_count); 5628 break; 5629 } 5630 case SYNCHRONIZE_CACHE_16: { 5631 struct scsi_sync_cache_16 *cdb; 5632 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5633 5634 starting_lba = scsi_8btou64(cdb->begin_lba); 5635 block_count = scsi_4btoul(cdb->lb_count); 5636 break; 5637 } 5638 default: 5639 ctl_set_invalid_opcode(ctsio); 5640 ctl_done((union ctl_io *)ctsio); 5641 goto bailout; 5642 break; /* NOTREACHED */ 5643 } 5644 5645 /* 5646 * We check the LBA and length, but don't do anything with them. 5647 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5648 * get flushed. This check will just help satisfy anyone who wants 5649 * to see an error for an out of range LBA. 5650 */ 5651 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5652 ctl_set_lba_out_of_range(ctsio); 5653 ctl_done((union ctl_io *)ctsio); 5654 goto bailout; 5655 } 5656 5657 /* 5658 * If this LUN has no backend, we can't flush the cache anyway. 5659 */ 5660 if (lun->backend == NULL) { 5661 ctl_set_invalid_opcode(ctsio); 5662 ctl_done((union ctl_io *)ctsio); 5663 goto bailout; 5664 } 5665 5666 /* 5667 * Check to see whether we're configured to send the SYNCHRONIZE 5668 * CACHE command directly to the back end. 5669 */ 5670 mtx_lock(&lun->lun_lock); 5671 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5672 && (++(lun->sync_count) >= lun->sync_interval)) { 5673 lun->sync_count = 0; 5674 mtx_unlock(&lun->lun_lock); 5675 retval = lun->backend->config_write((union ctl_io *)ctsio); 5676 } else { 5677 mtx_unlock(&lun->lun_lock); 5678 ctl_set_success(ctsio); 5679 ctl_done((union ctl_io *)ctsio); 5680 } 5681 5682bailout: 5683 5684 return (retval); 5685} 5686 5687int 5688ctl_format(struct ctl_scsiio *ctsio) 5689{ 5690 struct scsi_format *cdb; 5691 struct ctl_lun *lun; 5692 struct ctl_softc *ctl_softc; 5693 int length, defect_list_len; 5694 5695 CTL_DEBUG_PRINT(("ctl_format\n")); 5696 5697 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5698 ctl_softc = control_softc; 5699 5700 cdb = (struct scsi_format *)ctsio->cdb; 5701 5702 length = 0; 5703 if (cdb->byte2 & SF_FMTDATA) { 5704 if (cdb->byte2 & SF_LONGLIST) 5705 length = sizeof(struct scsi_format_header_long); 5706 else 5707 length = sizeof(struct scsi_format_header_short); 5708 } 5709 5710 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5711 && (length > 0)) { 5712 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5713 ctsio->kern_data_len = length; 5714 ctsio->kern_total_len = length; 5715 ctsio->kern_data_resid = 0; 5716 ctsio->kern_rel_offset = 0; 5717 ctsio->kern_sg_entries = 0; 5718 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5719 ctsio->be_move_done = ctl_config_move_done; 5720 ctl_datamove((union ctl_io *)ctsio); 5721 5722 return (CTL_RETVAL_COMPLETE); 5723 } 5724 5725 defect_list_len = 0; 5726 5727 if (cdb->byte2 & SF_FMTDATA) { 5728 if (cdb->byte2 & SF_LONGLIST) { 5729 struct scsi_format_header_long *header; 5730 5731 header = (struct scsi_format_header_long *) 5732 ctsio->kern_data_ptr; 5733 5734 defect_list_len = scsi_4btoul(header->defect_list_len); 5735 if (defect_list_len != 0) { 5736 ctl_set_invalid_field(ctsio, 5737 /*sks_valid*/ 1, 5738 /*command*/ 0, 5739 /*field*/ 2, 5740 /*bit_valid*/ 0, 5741 /*bit*/ 0); 5742 goto bailout; 5743 } 5744 } else { 5745 struct scsi_format_header_short *header; 5746 5747 header = (struct scsi_format_header_short *) 5748 ctsio->kern_data_ptr; 5749 5750 defect_list_len = scsi_2btoul(header->defect_list_len); 5751 if (defect_list_len != 0) { 5752 ctl_set_invalid_field(ctsio, 5753 /*sks_valid*/ 1, 5754 /*command*/ 0, 5755 /*field*/ 2, 5756 /*bit_valid*/ 0, 5757 /*bit*/ 0); 5758 goto bailout; 5759 } 5760 } 5761 } 5762 5763 /* 5764 * The format command will clear out the "Medium format corrupted" 5765 * status if set by the configuration code. That status is really 5766 * just a way to notify the host that we have lost the media, and 5767 * get them to issue a command that will basically make them think 5768 * they're blowing away the media. 5769 */ 5770 mtx_lock(&lun->lun_lock); 5771 lun->flags &= ~CTL_LUN_INOPERABLE; 5772 mtx_unlock(&lun->lun_lock); 5773 5774 ctsio->scsi_status = SCSI_STATUS_OK; 5775 ctsio->io_hdr.status = CTL_SUCCESS; 5776bailout: 5777 5778 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5779 free(ctsio->kern_data_ptr, M_CTL); 5780 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5781 } 5782 5783 ctl_done((union ctl_io *)ctsio); 5784 return (CTL_RETVAL_COMPLETE); 5785} 5786 5787int 5788ctl_read_buffer(struct ctl_scsiio *ctsio) 5789{ 5790 struct scsi_read_buffer *cdb; 5791 struct ctl_lun *lun; 5792 int buffer_offset, len; 5793 static uint8_t descr[4]; 5794 static uint8_t echo_descr[4] = { 0 }; 5795 5796 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5797 5798 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5799 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5800 5801 if (lun->flags & CTL_LUN_PR_RESERVED) { 5802 uint32_t residx; 5803 5804 /* 5805 * XXX KDM need a lock here. 5806 */ 5807 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5808 if ((lun->res_type == SPR_TYPE_EX_AC 5809 && residx != lun->pr_res_idx) 5810 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5811 || lun->res_type == SPR_TYPE_EX_AC_AR) 5812 && !lun->per_res[residx].registered)) { 5813 ctl_set_reservation_conflict(ctsio); 5814 ctl_done((union ctl_io *)ctsio); 5815 return (CTL_RETVAL_COMPLETE); 5816 } 5817 } 5818 5819 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5820 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5821 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5822 ctl_set_invalid_field(ctsio, 5823 /*sks_valid*/ 1, 5824 /*command*/ 1, 5825 /*field*/ 1, 5826 /*bit_valid*/ 1, 5827 /*bit*/ 4); 5828 ctl_done((union ctl_io *)ctsio); 5829 return (CTL_RETVAL_COMPLETE); 5830 } 5831 5832 len = scsi_3btoul(cdb->length); 5833 buffer_offset = scsi_3btoul(cdb->offset); 5834 5835 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5836 ctl_set_invalid_field(ctsio, 5837 /*sks_valid*/ 1, 5838 /*command*/ 1, 5839 /*field*/ 6, 5840 /*bit_valid*/ 0, 5841 /*bit*/ 0); 5842 ctl_done((union ctl_io *)ctsio); 5843 return (CTL_RETVAL_COMPLETE); 5844 } 5845 5846 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5847 descr[0] = 0; 5848 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5849 ctsio->kern_data_ptr = descr; 5850 len = min(len, sizeof(descr)); 5851 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5852 ctsio->kern_data_ptr = echo_descr; 5853 len = min(len, sizeof(echo_descr)); 5854 } else 5855 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5856 ctsio->kern_data_len = len; 5857 ctsio->kern_total_len = len; 5858 ctsio->kern_data_resid = 0; 5859 ctsio->kern_rel_offset = 0; 5860 ctsio->kern_sg_entries = 0; 5861 ctsio->be_move_done = ctl_config_move_done; 5862 ctl_datamove((union ctl_io *)ctsio); 5863 5864 return (CTL_RETVAL_COMPLETE); 5865} 5866 5867int 5868ctl_write_buffer(struct ctl_scsiio *ctsio) 5869{ 5870 struct scsi_write_buffer *cdb; 5871 struct ctl_lun *lun; 5872 int buffer_offset, len; 5873 5874 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5875 5876 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5877 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5878 5879 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5880 ctl_set_invalid_field(ctsio, 5881 /*sks_valid*/ 1, 5882 /*command*/ 1, 5883 /*field*/ 1, 5884 /*bit_valid*/ 1, 5885 /*bit*/ 4); 5886 ctl_done((union ctl_io *)ctsio); 5887 return (CTL_RETVAL_COMPLETE); 5888 } 5889 5890 len = scsi_3btoul(cdb->length); 5891 buffer_offset = scsi_3btoul(cdb->offset); 5892 5893 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5894 ctl_set_invalid_field(ctsio, 5895 /*sks_valid*/ 1, 5896 /*command*/ 1, 5897 /*field*/ 6, 5898 /*bit_valid*/ 0, 5899 /*bit*/ 0); 5900 ctl_done((union ctl_io *)ctsio); 5901 return (CTL_RETVAL_COMPLETE); 5902 } 5903 5904 /* 5905 * If we've got a kernel request that hasn't been malloced yet, 5906 * malloc it and tell the caller the data buffer is here. 5907 */ 5908 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5909 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5910 ctsio->kern_data_len = len; 5911 ctsio->kern_total_len = len; 5912 ctsio->kern_data_resid = 0; 5913 ctsio->kern_rel_offset = 0; 5914 ctsio->kern_sg_entries = 0; 5915 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5916 ctsio->be_move_done = ctl_config_move_done; 5917 ctl_datamove((union ctl_io *)ctsio); 5918 5919 return (CTL_RETVAL_COMPLETE); 5920 } 5921 5922 ctl_done((union ctl_io *)ctsio); 5923 5924 return (CTL_RETVAL_COMPLETE); 5925} 5926 5927int 5928ctl_write_same(struct ctl_scsiio *ctsio) 5929{ 5930 struct ctl_lun *lun; 5931 struct ctl_lba_len_flags *lbalen; 5932 uint64_t lba; 5933 uint32_t num_blocks; 5934 int len, retval; 5935 uint8_t byte2; 5936 5937 retval = CTL_RETVAL_COMPLETE; 5938 5939 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5940 5941 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5942 5943 switch (ctsio->cdb[0]) { 5944 case WRITE_SAME_10: { 5945 struct scsi_write_same_10 *cdb; 5946 5947 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5948 5949 lba = scsi_4btoul(cdb->addr); 5950 num_blocks = scsi_2btoul(cdb->length); 5951 byte2 = cdb->byte2; 5952 break; 5953 } 5954 case WRITE_SAME_16: { 5955 struct scsi_write_same_16 *cdb; 5956 5957 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5958 5959 lba = scsi_8btou64(cdb->addr); 5960 num_blocks = scsi_4btoul(cdb->length); 5961 byte2 = cdb->byte2; 5962 break; 5963 } 5964 default: 5965 /* 5966 * We got a command we don't support. This shouldn't 5967 * happen, commands should be filtered out above us. 5968 */ 5969 ctl_set_invalid_opcode(ctsio); 5970 ctl_done((union ctl_io *)ctsio); 5971 5972 return (CTL_RETVAL_COMPLETE); 5973 break; /* NOTREACHED */ 5974 } 5975 5976 /* 5977 * The first check is to make sure we're in bounds, the second 5978 * check is to catch wrap-around problems. If the lba + num blocks 5979 * is less than the lba, then we've wrapped around and the block 5980 * range is invalid anyway. 5981 */ 5982 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5983 || ((lba + num_blocks) < lba)) { 5984 ctl_set_lba_out_of_range(ctsio); 5985 ctl_done((union ctl_io *)ctsio); 5986 return (CTL_RETVAL_COMPLETE); 5987 } 5988 5989 /* Zero number of blocks means "to the last logical block" */ 5990 if (num_blocks == 0) { 5991 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5992 ctl_set_invalid_field(ctsio, 5993 /*sks_valid*/ 0, 5994 /*command*/ 1, 5995 /*field*/ 0, 5996 /*bit_valid*/ 0, 5997 /*bit*/ 0); 5998 ctl_done((union ctl_io *)ctsio); 5999 return (CTL_RETVAL_COMPLETE); 6000 } 6001 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6002 } 6003 6004 len = lun->be_lun->blocksize; 6005 6006 /* 6007 * If we've got a kernel request that hasn't been malloced yet, 6008 * malloc it and tell the caller the data buffer is here. 6009 */ 6010 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6011 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6012 ctsio->kern_data_len = len; 6013 ctsio->kern_total_len = len; 6014 ctsio->kern_data_resid = 0; 6015 ctsio->kern_rel_offset = 0; 6016 ctsio->kern_sg_entries = 0; 6017 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6018 ctsio->be_move_done = ctl_config_move_done; 6019 ctl_datamove((union ctl_io *)ctsio); 6020 6021 return (CTL_RETVAL_COMPLETE); 6022 } 6023 6024 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6025 lbalen->lba = lba; 6026 lbalen->len = num_blocks; 6027 lbalen->flags = byte2; 6028 retval = lun->backend->config_write((union ctl_io *)ctsio); 6029 6030 return (retval); 6031} 6032 6033int 6034ctl_unmap(struct ctl_scsiio *ctsio) 6035{ 6036 struct ctl_lun *lun; 6037 struct scsi_unmap *cdb; 6038 struct ctl_ptr_len_flags *ptrlen; 6039 struct scsi_unmap_header *hdr; 6040 struct scsi_unmap_desc *buf, *end; 6041 uint64_t lba; 6042 uint32_t num_blocks; 6043 int len, retval; 6044 uint8_t byte2; 6045 6046 retval = CTL_RETVAL_COMPLETE; 6047 6048 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6049 6050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6051 cdb = (struct scsi_unmap *)ctsio->cdb; 6052 6053 len = scsi_2btoul(cdb->length); 6054 byte2 = cdb->byte2; 6055 6056 /* 6057 * If we've got a kernel request that hasn't been malloced yet, 6058 * malloc it and tell the caller the data buffer is here. 6059 */ 6060 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6061 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6062 ctsio->kern_data_len = len; 6063 ctsio->kern_total_len = len; 6064 ctsio->kern_data_resid = 0; 6065 ctsio->kern_rel_offset = 0; 6066 ctsio->kern_sg_entries = 0; 6067 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6068 ctsio->be_move_done = ctl_config_move_done; 6069 ctl_datamove((union ctl_io *)ctsio); 6070 6071 return (CTL_RETVAL_COMPLETE); 6072 } 6073 6074 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6075 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6076 if (len < sizeof (*hdr) || 6077 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6078 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6079 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6080 ctl_set_invalid_field(ctsio, 6081 /*sks_valid*/ 0, 6082 /*command*/ 0, 6083 /*field*/ 0, 6084 /*bit_valid*/ 0, 6085 /*bit*/ 0); 6086 ctl_done((union ctl_io *)ctsio); 6087 return (CTL_RETVAL_COMPLETE); 6088 } 6089 len = scsi_2btoul(hdr->desc_length); 6090 buf = (struct scsi_unmap_desc *)(hdr + 1); 6091 end = buf + len / sizeof(*buf); 6092 6093 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6094 ptrlen->ptr = (void *)buf; 6095 ptrlen->len = len; 6096 ptrlen->flags = byte2; 6097 6098 for (; buf < end; buf++) { 6099 lba = scsi_8btou64(buf->lba); 6100 num_blocks = scsi_4btoul(buf->length); 6101 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6102 || ((lba + num_blocks) < lba)) { 6103 ctl_set_lba_out_of_range(ctsio); 6104 ctl_done((union ctl_io *)ctsio); 6105 return (CTL_RETVAL_COMPLETE); 6106 } 6107 } 6108 6109 retval = lun->backend->config_write((union ctl_io *)ctsio); 6110 6111 return (retval); 6112} 6113 6114/* 6115 * Note that this function currently doesn't actually do anything inside 6116 * CTL to enforce things if the DQue bit is turned on. 6117 * 6118 * Also note that this function can't be used in the default case, because 6119 * the DQue bit isn't set in the changeable mask for the control mode page 6120 * anyway. This is just here as an example for how to implement a page 6121 * handler, and a placeholder in case we want to allow the user to turn 6122 * tagged queueing on and off. 6123 * 6124 * The D_SENSE bit handling is functional, however, and will turn 6125 * descriptor sense on and off for a given LUN. 6126 */ 6127int 6128ctl_control_page_handler(struct ctl_scsiio *ctsio, 6129 struct ctl_page_index *page_index, uint8_t *page_ptr) 6130{ 6131 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6132 struct ctl_lun *lun; 6133 struct ctl_softc *softc; 6134 int set_ua; 6135 uint32_t initidx; 6136 6137 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6138 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6139 set_ua = 0; 6140 6141 user_cp = (struct scsi_control_page *)page_ptr; 6142 current_cp = (struct scsi_control_page *) 6143 (page_index->page_data + (page_index->page_len * 6144 CTL_PAGE_CURRENT)); 6145 saved_cp = (struct scsi_control_page *) 6146 (page_index->page_data + (page_index->page_len * 6147 CTL_PAGE_SAVED)); 6148 6149 softc = control_softc; 6150 6151 mtx_lock(&lun->lun_lock); 6152 if (((current_cp->rlec & SCP_DSENSE) == 0) 6153 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6154 /* 6155 * Descriptor sense is currently turned off and the user 6156 * wants to turn it on. 6157 */ 6158 current_cp->rlec |= SCP_DSENSE; 6159 saved_cp->rlec |= SCP_DSENSE; 6160 lun->flags |= CTL_LUN_SENSE_DESC; 6161 set_ua = 1; 6162 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6163 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6164 /* 6165 * Descriptor sense is currently turned on, and the user 6166 * wants to turn it off. 6167 */ 6168 current_cp->rlec &= ~SCP_DSENSE; 6169 saved_cp->rlec &= ~SCP_DSENSE; 6170 lun->flags &= ~CTL_LUN_SENSE_DESC; 6171 set_ua = 1; 6172 } 6173 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6174 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6175#ifdef NEEDTOPORT 6176 csevent_log(CSC_CTL | CSC_SHELF_SW | 6177 CTL_UNTAG_TO_UNTAG, 6178 csevent_LogType_Trace, 6179 csevent_Severity_Information, 6180 csevent_AlertLevel_Green, 6181 csevent_FRU_Firmware, 6182 csevent_FRU_Unknown, 6183 "Received untagged to untagged transition"); 6184#endif /* NEEDTOPORT */ 6185 } else { 6186#ifdef NEEDTOPORT 6187 csevent_log(CSC_CTL | CSC_SHELF_SW | 6188 CTL_UNTAG_TO_TAG, 6189 csevent_LogType_ConfigChange, 6190 csevent_Severity_Information, 6191 csevent_AlertLevel_Green, 6192 csevent_FRU_Firmware, 6193 csevent_FRU_Unknown, 6194 "Received untagged to tagged " 6195 "queueing transition"); 6196#endif /* NEEDTOPORT */ 6197 6198 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6199 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6200 set_ua = 1; 6201 } 6202 } else { 6203 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6204#ifdef NEEDTOPORT 6205 csevent_log(CSC_CTL | CSC_SHELF_SW | 6206 CTL_TAG_TO_UNTAG, 6207 csevent_LogType_ConfigChange, 6208 csevent_Severity_Warning, 6209 csevent_AlertLevel_Yellow, 6210 csevent_FRU_Firmware, 6211 csevent_FRU_Unknown, 6212 "Received tagged queueing to untagged " 6213 "transition"); 6214#endif /* NEEDTOPORT */ 6215 6216 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6217 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6218 set_ua = 1; 6219 } else { 6220#ifdef NEEDTOPORT 6221 csevent_log(CSC_CTL | CSC_SHELF_SW | 6222 CTL_TAG_TO_TAG, 6223 csevent_LogType_Trace, 6224 csevent_Severity_Information, 6225 csevent_AlertLevel_Green, 6226 csevent_FRU_Firmware, 6227 csevent_FRU_Unknown, 6228 "Received tagged queueing to tagged " 6229 "queueing transition"); 6230#endif /* NEEDTOPORT */ 6231 } 6232 } 6233 if (set_ua != 0) { 6234 int i; 6235 /* 6236 * Let other initiators know that the mode 6237 * parameters for this LUN have changed. 6238 */ 6239 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6240 if (i == initidx) 6241 continue; 6242 6243 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6244 } 6245 } 6246 mtx_unlock(&lun->lun_lock); 6247 6248 return (0); 6249} 6250 6251int 6252ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6253 struct ctl_page_index *page_index, uint8_t *page_ptr) 6254{ 6255 return (0); 6256} 6257 6258int 6259ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6260 struct ctl_page_index *page_index, int pc) 6261{ 6262 struct copan_power_subpage *page; 6263 6264 page = (struct copan_power_subpage *)page_index->page_data + 6265 (page_index->page_len * pc); 6266 6267 switch (pc) { 6268 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6269 /* 6270 * We don't update the changable bits for this page. 6271 */ 6272 break; 6273 case SMS_PAGE_CTRL_CURRENT >> 6: 6274 case SMS_PAGE_CTRL_DEFAULT >> 6: 6275 case SMS_PAGE_CTRL_SAVED >> 6: 6276#ifdef NEEDTOPORT 6277 ctl_update_power_subpage(page); 6278#endif 6279 break; 6280 default: 6281#ifdef NEEDTOPORT 6282 EPRINT(0, "Invalid PC %d!!", pc); 6283#endif 6284 break; 6285 } 6286 return (0); 6287} 6288 6289 6290int 6291ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6292 struct ctl_page_index *page_index, uint8_t *page_ptr) 6293{ 6294 struct copan_aps_subpage *user_sp; 6295 struct copan_aps_subpage *current_sp; 6296 union ctl_modepage_info *modepage_info; 6297 struct ctl_softc *softc; 6298 struct ctl_lun *lun; 6299 int retval; 6300 6301 retval = CTL_RETVAL_COMPLETE; 6302 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6303 (page_index->page_len * CTL_PAGE_CURRENT)); 6304 softc = control_softc; 6305 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6306 6307 user_sp = (struct copan_aps_subpage *)page_ptr; 6308 6309 modepage_info = (union ctl_modepage_info *) 6310 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6311 6312 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6313 modepage_info->header.subpage = page_index->subpage; 6314 modepage_info->aps.lock_active = user_sp->lock_active; 6315 6316 mtx_lock(&softc->ctl_lock); 6317 6318 /* 6319 * If there is a request to lock the LUN and another LUN is locked 6320 * this is an error. If the requested LUN is already locked ignore 6321 * the request. If no LUN is locked attempt to lock it. 6322 * if there is a request to unlock the LUN and the LUN is currently 6323 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6324 * if another LUN is locked or no LUN is locked. 6325 */ 6326 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6327 if (softc->aps_locked_lun == lun->lun) { 6328 /* 6329 * This LUN is already locked, so we're done. 6330 */ 6331 retval = CTL_RETVAL_COMPLETE; 6332 } else if (softc->aps_locked_lun == 0) { 6333 /* 6334 * No one has the lock, pass the request to the 6335 * backend. 6336 */ 6337 retval = lun->backend->config_write( 6338 (union ctl_io *)ctsio); 6339 } else { 6340 /* 6341 * Someone else has the lock, throw out the request. 6342 */ 6343 ctl_set_already_locked(ctsio); 6344 free(ctsio->kern_data_ptr, M_CTL); 6345 ctl_done((union ctl_io *)ctsio); 6346 6347 /* 6348 * Set the return value so that ctl_do_mode_select() 6349 * won't try to complete the command. We already 6350 * completed it here. 6351 */ 6352 retval = CTL_RETVAL_ERROR; 6353 } 6354 } else if (softc->aps_locked_lun == lun->lun) { 6355 /* 6356 * This LUN is locked, so pass the unlock request to the 6357 * backend. 6358 */ 6359 retval = lun->backend->config_write((union ctl_io *)ctsio); 6360 } 6361 mtx_unlock(&softc->ctl_lock); 6362 6363 return (retval); 6364} 6365 6366int 6367ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6368 struct ctl_page_index *page_index, 6369 uint8_t *page_ptr) 6370{ 6371 uint8_t *c; 6372 int i; 6373 6374 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6375 ctl_time_io_secs = 6376 (c[0] << 8) | 6377 (c[1] << 0) | 6378 0; 6379 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6380 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6381 printf("page data:"); 6382 for (i=0; i<8; i++) 6383 printf(" %.2x",page_ptr[i]); 6384 printf("\n"); 6385 return (0); 6386} 6387 6388int 6389ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6390 struct ctl_page_index *page_index, 6391 int pc) 6392{ 6393 struct copan_debugconf_subpage *page; 6394 6395 page = (struct copan_debugconf_subpage *)page_index->page_data + 6396 (page_index->page_len * pc); 6397 6398 switch (pc) { 6399 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6400 case SMS_PAGE_CTRL_DEFAULT >> 6: 6401 case SMS_PAGE_CTRL_SAVED >> 6: 6402 /* 6403 * We don't update the changable or default bits for this page. 6404 */ 6405 break; 6406 case SMS_PAGE_CTRL_CURRENT >> 6: 6407 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6408 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6409 break; 6410 default: 6411#ifdef NEEDTOPORT 6412 EPRINT(0, "Invalid PC %d!!", pc); 6413#endif /* NEEDTOPORT */ 6414 break; 6415 } 6416 return (0); 6417} 6418 6419 6420static int 6421ctl_do_mode_select(union ctl_io *io) 6422{ 6423 struct scsi_mode_page_header *page_header; 6424 struct ctl_page_index *page_index; 6425 struct ctl_scsiio *ctsio; 6426 int control_dev, page_len; 6427 int page_len_offset, page_len_size; 6428 union ctl_modepage_info *modepage_info; 6429 struct ctl_lun *lun; 6430 int *len_left, *len_used; 6431 int retval, i; 6432 6433 ctsio = &io->scsiio; 6434 page_index = NULL; 6435 page_len = 0; 6436 retval = CTL_RETVAL_COMPLETE; 6437 6438 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6439 6440 if (lun->be_lun->lun_type != T_DIRECT) 6441 control_dev = 1; 6442 else 6443 control_dev = 0; 6444 6445 modepage_info = (union ctl_modepage_info *) 6446 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6447 len_left = &modepage_info->header.len_left; 6448 len_used = &modepage_info->header.len_used; 6449 6450do_next_page: 6451 6452 page_header = (struct scsi_mode_page_header *) 6453 (ctsio->kern_data_ptr + *len_used); 6454 6455 if (*len_left == 0) { 6456 free(ctsio->kern_data_ptr, M_CTL); 6457 ctl_set_success(ctsio); 6458 ctl_done((union ctl_io *)ctsio); 6459 return (CTL_RETVAL_COMPLETE); 6460 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6461 6462 free(ctsio->kern_data_ptr, M_CTL); 6463 ctl_set_param_len_error(ctsio); 6464 ctl_done((union ctl_io *)ctsio); 6465 return (CTL_RETVAL_COMPLETE); 6466 6467 } else if ((page_header->page_code & SMPH_SPF) 6468 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6469 6470 free(ctsio->kern_data_ptr, M_CTL); 6471 ctl_set_param_len_error(ctsio); 6472 ctl_done((union ctl_io *)ctsio); 6473 return (CTL_RETVAL_COMPLETE); 6474 } 6475 6476 6477 /* 6478 * XXX KDM should we do something with the block descriptor? 6479 */ 6480 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6481 6482 if ((control_dev != 0) 6483 && (lun->mode_pages.index[i].page_flags & 6484 CTL_PAGE_FLAG_DISK_ONLY)) 6485 continue; 6486 6487 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6488 (page_header->page_code & SMPH_PC_MASK)) 6489 continue; 6490 6491 /* 6492 * If neither page has a subpage code, then we've got a 6493 * match. 6494 */ 6495 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6496 && ((page_header->page_code & SMPH_SPF) == 0)) { 6497 page_index = &lun->mode_pages.index[i]; 6498 page_len = page_header->page_length; 6499 break; 6500 } 6501 6502 /* 6503 * If both pages have subpages, then the subpage numbers 6504 * have to match. 6505 */ 6506 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6507 && (page_header->page_code & SMPH_SPF)) { 6508 struct scsi_mode_page_header_sp *sph; 6509 6510 sph = (struct scsi_mode_page_header_sp *)page_header; 6511 6512 if (lun->mode_pages.index[i].subpage == 6513 sph->subpage) { 6514 page_index = &lun->mode_pages.index[i]; 6515 page_len = scsi_2btoul(sph->page_length); 6516 break; 6517 } 6518 } 6519 } 6520 6521 /* 6522 * If we couldn't find the page, or if we don't have a mode select 6523 * handler for it, send back an error to the user. 6524 */ 6525 if ((page_index == NULL) 6526 || (page_index->select_handler == NULL)) { 6527 ctl_set_invalid_field(ctsio, 6528 /*sks_valid*/ 1, 6529 /*command*/ 0, 6530 /*field*/ *len_used, 6531 /*bit_valid*/ 0, 6532 /*bit*/ 0); 6533 free(ctsio->kern_data_ptr, M_CTL); 6534 ctl_done((union ctl_io *)ctsio); 6535 return (CTL_RETVAL_COMPLETE); 6536 } 6537 6538 if (page_index->page_code & SMPH_SPF) { 6539 page_len_offset = 2; 6540 page_len_size = 2; 6541 } else { 6542 page_len_size = 1; 6543 page_len_offset = 1; 6544 } 6545 6546 /* 6547 * If the length the initiator gives us isn't the one we specify in 6548 * the mode page header, or if they didn't specify enough data in 6549 * the CDB to avoid truncating this page, kick out the request. 6550 */ 6551 if ((page_len != (page_index->page_len - page_len_offset - 6552 page_len_size)) 6553 || (*len_left < page_index->page_len)) { 6554 6555 6556 ctl_set_invalid_field(ctsio, 6557 /*sks_valid*/ 1, 6558 /*command*/ 0, 6559 /*field*/ *len_used + page_len_offset, 6560 /*bit_valid*/ 0, 6561 /*bit*/ 0); 6562 free(ctsio->kern_data_ptr, M_CTL); 6563 ctl_done((union ctl_io *)ctsio); 6564 return (CTL_RETVAL_COMPLETE); 6565 } 6566 6567 /* 6568 * Run through the mode page, checking to make sure that the bits 6569 * the user changed are actually legal for him to change. 6570 */ 6571 for (i = 0; i < page_index->page_len; i++) { 6572 uint8_t *user_byte, *change_mask, *current_byte; 6573 int bad_bit; 6574 int j; 6575 6576 user_byte = (uint8_t *)page_header + i; 6577 change_mask = page_index->page_data + 6578 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6579 current_byte = page_index->page_data + 6580 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6581 6582 /* 6583 * Check to see whether the user set any bits in this byte 6584 * that he is not allowed to set. 6585 */ 6586 if ((*user_byte & ~(*change_mask)) == 6587 (*current_byte & ~(*change_mask))) 6588 continue; 6589 6590 /* 6591 * Go through bit by bit to determine which one is illegal. 6592 */ 6593 bad_bit = 0; 6594 for (j = 7; j >= 0; j--) { 6595 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6596 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6597 bad_bit = i; 6598 break; 6599 } 6600 } 6601 ctl_set_invalid_field(ctsio, 6602 /*sks_valid*/ 1, 6603 /*command*/ 0, 6604 /*field*/ *len_used + i, 6605 /*bit_valid*/ 1, 6606 /*bit*/ bad_bit); 6607 free(ctsio->kern_data_ptr, M_CTL); 6608 ctl_done((union ctl_io *)ctsio); 6609 return (CTL_RETVAL_COMPLETE); 6610 } 6611 6612 /* 6613 * Decrement these before we call the page handler, since we may 6614 * end up getting called back one way or another before the handler 6615 * returns to this context. 6616 */ 6617 *len_left -= page_index->page_len; 6618 *len_used += page_index->page_len; 6619 6620 retval = page_index->select_handler(ctsio, page_index, 6621 (uint8_t *)page_header); 6622 6623 /* 6624 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6625 * wait until this queued command completes to finish processing 6626 * the mode page. If it returns anything other than 6627 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6628 * already set the sense information, freed the data pointer, and 6629 * completed the io for us. 6630 */ 6631 if (retval != CTL_RETVAL_COMPLETE) 6632 goto bailout_no_done; 6633 6634 /* 6635 * If the initiator sent us more than one page, parse the next one. 6636 */ 6637 if (*len_left > 0) 6638 goto do_next_page; 6639 6640 ctl_set_success(ctsio); 6641 free(ctsio->kern_data_ptr, M_CTL); 6642 ctl_done((union ctl_io *)ctsio); 6643 6644bailout_no_done: 6645 6646 return (CTL_RETVAL_COMPLETE); 6647 6648} 6649 6650int 6651ctl_mode_select(struct ctl_scsiio *ctsio) 6652{ 6653 int param_len, pf, sp; 6654 int header_size, bd_len; 6655 int len_left, len_used; 6656 struct ctl_page_index *page_index; 6657 struct ctl_lun *lun; 6658 int control_dev, page_len; 6659 union ctl_modepage_info *modepage_info; 6660 int retval; 6661 6662 pf = 0; 6663 sp = 0; 6664 page_len = 0; 6665 len_used = 0; 6666 len_left = 0; 6667 retval = 0; 6668 bd_len = 0; 6669 page_index = NULL; 6670 6671 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6672 6673 if (lun->be_lun->lun_type != T_DIRECT) 6674 control_dev = 1; 6675 else 6676 control_dev = 0; 6677 6678 switch (ctsio->cdb[0]) { 6679 case MODE_SELECT_6: { 6680 struct scsi_mode_select_6 *cdb; 6681 6682 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6683 6684 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6685 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6686 6687 param_len = cdb->length; 6688 header_size = sizeof(struct scsi_mode_header_6); 6689 break; 6690 } 6691 case MODE_SELECT_10: { 6692 struct scsi_mode_select_10 *cdb; 6693 6694 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6695 6696 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6697 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6698 6699 param_len = scsi_2btoul(cdb->length); 6700 header_size = sizeof(struct scsi_mode_header_10); 6701 break; 6702 } 6703 default: 6704 ctl_set_invalid_opcode(ctsio); 6705 ctl_done((union ctl_io *)ctsio); 6706 return (CTL_RETVAL_COMPLETE); 6707 break; /* NOTREACHED */ 6708 } 6709 6710 /* 6711 * From SPC-3: 6712 * "A parameter list length of zero indicates that the Data-Out Buffer 6713 * shall be empty. This condition shall not be considered as an error." 6714 */ 6715 if (param_len == 0) { 6716 ctl_set_success(ctsio); 6717 ctl_done((union ctl_io *)ctsio); 6718 return (CTL_RETVAL_COMPLETE); 6719 } 6720 6721 /* 6722 * Since we'll hit this the first time through, prior to 6723 * allocation, we don't need to free a data buffer here. 6724 */ 6725 if (param_len < header_size) { 6726 ctl_set_param_len_error(ctsio); 6727 ctl_done((union ctl_io *)ctsio); 6728 return (CTL_RETVAL_COMPLETE); 6729 } 6730 6731 /* 6732 * Allocate the data buffer and grab the user's data. In theory, 6733 * we shouldn't have to sanity check the parameter list length here 6734 * because the maximum size is 64K. We should be able to malloc 6735 * that much without too many problems. 6736 */ 6737 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6738 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6739 ctsio->kern_data_len = param_len; 6740 ctsio->kern_total_len = param_len; 6741 ctsio->kern_data_resid = 0; 6742 ctsio->kern_rel_offset = 0; 6743 ctsio->kern_sg_entries = 0; 6744 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6745 ctsio->be_move_done = ctl_config_move_done; 6746 ctl_datamove((union ctl_io *)ctsio); 6747 6748 return (CTL_RETVAL_COMPLETE); 6749 } 6750 6751 switch (ctsio->cdb[0]) { 6752 case MODE_SELECT_6: { 6753 struct scsi_mode_header_6 *mh6; 6754 6755 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6756 bd_len = mh6->blk_desc_len; 6757 break; 6758 } 6759 case MODE_SELECT_10: { 6760 struct scsi_mode_header_10 *mh10; 6761 6762 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6763 bd_len = scsi_2btoul(mh10->blk_desc_len); 6764 break; 6765 } 6766 default: 6767 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6768 break; 6769 } 6770 6771 if (param_len < (header_size + bd_len)) { 6772 free(ctsio->kern_data_ptr, M_CTL); 6773 ctl_set_param_len_error(ctsio); 6774 ctl_done((union ctl_io *)ctsio); 6775 return (CTL_RETVAL_COMPLETE); 6776 } 6777 6778 /* 6779 * Set the IO_CONT flag, so that if this I/O gets passed to 6780 * ctl_config_write_done(), it'll get passed back to 6781 * ctl_do_mode_select() for further processing, or completion if 6782 * we're all done. 6783 */ 6784 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6785 ctsio->io_cont = ctl_do_mode_select; 6786 6787 modepage_info = (union ctl_modepage_info *) 6788 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6789 6790 memset(modepage_info, 0, sizeof(*modepage_info)); 6791 6792 len_left = param_len - header_size - bd_len; 6793 len_used = header_size + bd_len; 6794 6795 modepage_info->header.len_left = len_left; 6796 modepage_info->header.len_used = len_used; 6797 6798 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6799} 6800 6801int 6802ctl_mode_sense(struct ctl_scsiio *ctsio) 6803{ 6804 struct ctl_lun *lun; 6805 int pc, page_code, dbd, llba, subpage; 6806 int alloc_len, page_len, header_len, total_len; 6807 struct scsi_mode_block_descr *block_desc; 6808 struct ctl_page_index *page_index; 6809 int control_dev; 6810 6811 dbd = 0; 6812 llba = 0; 6813 block_desc = NULL; 6814 page_index = NULL; 6815 6816 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6817 6818 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6819 6820 if (lun->be_lun->lun_type != T_DIRECT) 6821 control_dev = 1; 6822 else 6823 control_dev = 0; 6824 6825 if (lun->flags & CTL_LUN_PR_RESERVED) { 6826 uint32_t residx; 6827 6828 /* 6829 * XXX KDM need a lock here. 6830 */ 6831 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6832 if ((lun->res_type == SPR_TYPE_EX_AC 6833 && residx != lun->pr_res_idx) 6834 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6835 || lun->res_type == SPR_TYPE_EX_AC_AR) 6836 && !lun->per_res[residx].registered)) { 6837 ctl_set_reservation_conflict(ctsio); 6838 ctl_done((union ctl_io *)ctsio); 6839 return (CTL_RETVAL_COMPLETE); 6840 } 6841 } 6842 6843 switch (ctsio->cdb[0]) { 6844 case MODE_SENSE_6: { 6845 struct scsi_mode_sense_6 *cdb; 6846 6847 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6848 6849 header_len = sizeof(struct scsi_mode_hdr_6); 6850 if (cdb->byte2 & SMS_DBD) 6851 dbd = 1; 6852 else 6853 header_len += sizeof(struct scsi_mode_block_descr); 6854 6855 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6856 page_code = cdb->page & SMS_PAGE_CODE; 6857 subpage = cdb->subpage; 6858 alloc_len = cdb->length; 6859 break; 6860 } 6861 case MODE_SENSE_10: { 6862 struct scsi_mode_sense_10 *cdb; 6863 6864 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6865 6866 header_len = sizeof(struct scsi_mode_hdr_10); 6867 6868 if (cdb->byte2 & SMS_DBD) 6869 dbd = 1; 6870 else 6871 header_len += sizeof(struct scsi_mode_block_descr); 6872 if (cdb->byte2 & SMS10_LLBAA) 6873 llba = 1; 6874 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6875 page_code = cdb->page & SMS_PAGE_CODE; 6876 subpage = cdb->subpage; 6877 alloc_len = scsi_2btoul(cdb->length); 6878 break; 6879 } 6880 default: 6881 ctl_set_invalid_opcode(ctsio); 6882 ctl_done((union ctl_io *)ctsio); 6883 return (CTL_RETVAL_COMPLETE); 6884 break; /* NOTREACHED */ 6885 } 6886 6887 /* 6888 * We have to make a first pass through to calculate the size of 6889 * the pages that match the user's query. Then we allocate enough 6890 * memory to hold it, and actually copy the data into the buffer. 6891 */ 6892 switch (page_code) { 6893 case SMS_ALL_PAGES_PAGE: { 6894 int i; 6895 6896 page_len = 0; 6897 6898 /* 6899 * At the moment, values other than 0 and 0xff here are 6900 * reserved according to SPC-3. 6901 */ 6902 if ((subpage != SMS_SUBPAGE_PAGE_0) 6903 && (subpage != SMS_SUBPAGE_ALL)) { 6904 ctl_set_invalid_field(ctsio, 6905 /*sks_valid*/ 1, 6906 /*command*/ 1, 6907 /*field*/ 3, 6908 /*bit_valid*/ 0, 6909 /*bit*/ 0); 6910 ctl_done((union ctl_io *)ctsio); 6911 return (CTL_RETVAL_COMPLETE); 6912 } 6913 6914 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6915 if ((control_dev != 0) 6916 && (lun->mode_pages.index[i].page_flags & 6917 CTL_PAGE_FLAG_DISK_ONLY)) 6918 continue; 6919 6920 /* 6921 * We don't use this subpage if the user didn't 6922 * request all subpages. 6923 */ 6924 if ((lun->mode_pages.index[i].subpage != 0) 6925 && (subpage == SMS_SUBPAGE_PAGE_0)) 6926 continue; 6927 6928#if 0 6929 printf("found page %#x len %d\n", 6930 lun->mode_pages.index[i].page_code & 6931 SMPH_PC_MASK, 6932 lun->mode_pages.index[i].page_len); 6933#endif 6934 page_len += lun->mode_pages.index[i].page_len; 6935 } 6936 break; 6937 } 6938 default: { 6939 int i; 6940 6941 page_len = 0; 6942 6943 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6944 /* Look for the right page code */ 6945 if ((lun->mode_pages.index[i].page_code & 6946 SMPH_PC_MASK) != page_code) 6947 continue; 6948 6949 /* Look for the right subpage or the subpage wildcard*/ 6950 if ((lun->mode_pages.index[i].subpage != subpage) 6951 && (subpage != SMS_SUBPAGE_ALL)) 6952 continue; 6953 6954 /* Make sure the page is supported for this dev type */ 6955 if ((control_dev != 0) 6956 && (lun->mode_pages.index[i].page_flags & 6957 CTL_PAGE_FLAG_DISK_ONLY)) 6958 continue; 6959 6960#if 0 6961 printf("found page %#x len %d\n", 6962 lun->mode_pages.index[i].page_code & 6963 SMPH_PC_MASK, 6964 lun->mode_pages.index[i].page_len); 6965#endif 6966 6967 page_len += lun->mode_pages.index[i].page_len; 6968 } 6969 6970 if (page_len == 0) { 6971 ctl_set_invalid_field(ctsio, 6972 /*sks_valid*/ 1, 6973 /*command*/ 1, 6974 /*field*/ 2, 6975 /*bit_valid*/ 1, 6976 /*bit*/ 5); 6977 ctl_done((union ctl_io *)ctsio); 6978 return (CTL_RETVAL_COMPLETE); 6979 } 6980 break; 6981 } 6982 } 6983 6984 total_len = header_len + page_len; 6985#if 0 6986 printf("header_len = %d, page_len = %d, total_len = %d\n", 6987 header_len, page_len, total_len); 6988#endif 6989 6990 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6991 ctsio->kern_sg_entries = 0; 6992 ctsio->kern_data_resid = 0; 6993 ctsio->kern_rel_offset = 0; 6994 if (total_len < alloc_len) { 6995 ctsio->residual = alloc_len - total_len; 6996 ctsio->kern_data_len = total_len; 6997 ctsio->kern_total_len = total_len; 6998 } else { 6999 ctsio->residual = 0; 7000 ctsio->kern_data_len = alloc_len; 7001 ctsio->kern_total_len = alloc_len; 7002 } 7003 7004 switch (ctsio->cdb[0]) { 7005 case MODE_SENSE_6: { 7006 struct scsi_mode_hdr_6 *header; 7007 7008 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7009 7010 header->datalen = ctl_min(total_len - 1, 254); 7011 7012 if (dbd) 7013 header->block_descr_len = 0; 7014 else 7015 header->block_descr_len = 7016 sizeof(struct scsi_mode_block_descr); 7017 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7018 break; 7019 } 7020 case MODE_SENSE_10: { 7021 struct scsi_mode_hdr_10 *header; 7022 int datalen; 7023 7024 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7025 7026 datalen = ctl_min(total_len - 2, 65533); 7027 scsi_ulto2b(datalen, header->datalen); 7028 if (dbd) 7029 scsi_ulto2b(0, header->block_descr_len); 7030 else 7031 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7032 header->block_descr_len); 7033 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7034 break; 7035 } 7036 default: 7037 panic("invalid CDB type %#x", ctsio->cdb[0]); 7038 break; /* NOTREACHED */ 7039 } 7040 7041 /* 7042 * If we've got a disk, use its blocksize in the block 7043 * descriptor. Otherwise, just set it to 0. 7044 */ 7045 if (dbd == 0) { 7046 if (control_dev != 0) 7047 scsi_ulto3b(lun->be_lun->blocksize, 7048 block_desc->block_len); 7049 else 7050 scsi_ulto3b(0, block_desc->block_len); 7051 } 7052 7053 switch (page_code) { 7054 case SMS_ALL_PAGES_PAGE: { 7055 int i, data_used; 7056 7057 data_used = header_len; 7058 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7059 struct ctl_page_index *page_index; 7060 7061 page_index = &lun->mode_pages.index[i]; 7062 7063 if ((control_dev != 0) 7064 && (page_index->page_flags & 7065 CTL_PAGE_FLAG_DISK_ONLY)) 7066 continue; 7067 7068 /* 7069 * We don't use this subpage if the user didn't 7070 * request all subpages. We already checked (above) 7071 * to make sure the user only specified a subpage 7072 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7073 */ 7074 if ((page_index->subpage != 0) 7075 && (subpage == SMS_SUBPAGE_PAGE_0)) 7076 continue; 7077 7078 /* 7079 * Call the handler, if it exists, to update the 7080 * page to the latest values. 7081 */ 7082 if (page_index->sense_handler != NULL) 7083 page_index->sense_handler(ctsio, page_index,pc); 7084 7085 memcpy(ctsio->kern_data_ptr + data_used, 7086 page_index->page_data + 7087 (page_index->page_len * pc), 7088 page_index->page_len); 7089 data_used += page_index->page_len; 7090 } 7091 break; 7092 } 7093 default: { 7094 int i, data_used; 7095 7096 data_used = header_len; 7097 7098 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7099 struct ctl_page_index *page_index; 7100 7101 page_index = &lun->mode_pages.index[i]; 7102 7103 /* Look for the right page code */ 7104 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7105 continue; 7106 7107 /* Look for the right subpage or the subpage wildcard*/ 7108 if ((page_index->subpage != subpage) 7109 && (subpage != SMS_SUBPAGE_ALL)) 7110 continue; 7111 7112 /* Make sure the page is supported for this dev type */ 7113 if ((control_dev != 0) 7114 && (page_index->page_flags & 7115 CTL_PAGE_FLAG_DISK_ONLY)) 7116 continue; 7117 7118 /* 7119 * Call the handler, if it exists, to update the 7120 * page to the latest values. 7121 */ 7122 if (page_index->sense_handler != NULL) 7123 page_index->sense_handler(ctsio, page_index,pc); 7124 7125 memcpy(ctsio->kern_data_ptr + data_used, 7126 page_index->page_data + 7127 (page_index->page_len * pc), 7128 page_index->page_len); 7129 data_used += page_index->page_len; 7130 } 7131 break; 7132 } 7133 } 7134 7135 ctsio->scsi_status = SCSI_STATUS_OK; 7136 7137 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7138 ctsio->be_move_done = ctl_config_move_done; 7139 ctl_datamove((union ctl_io *)ctsio); 7140 7141 return (CTL_RETVAL_COMPLETE); 7142} 7143 7144int 7145ctl_read_capacity(struct ctl_scsiio *ctsio) 7146{ 7147 struct scsi_read_capacity *cdb; 7148 struct scsi_read_capacity_data *data; 7149 struct ctl_lun *lun; 7150 uint32_t lba; 7151 7152 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7153 7154 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7155 7156 lba = scsi_4btoul(cdb->addr); 7157 if (((cdb->pmi & SRC_PMI) == 0) 7158 && (lba != 0)) { 7159 ctl_set_invalid_field(/*ctsio*/ ctsio, 7160 /*sks_valid*/ 1, 7161 /*command*/ 1, 7162 /*field*/ 2, 7163 /*bit_valid*/ 0, 7164 /*bit*/ 0); 7165 ctl_done((union ctl_io *)ctsio); 7166 return (CTL_RETVAL_COMPLETE); 7167 } 7168 7169 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7170 7171 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7172 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7173 ctsio->residual = 0; 7174 ctsio->kern_data_len = sizeof(*data); 7175 ctsio->kern_total_len = sizeof(*data); 7176 ctsio->kern_data_resid = 0; 7177 ctsio->kern_rel_offset = 0; 7178 ctsio->kern_sg_entries = 0; 7179 7180 /* 7181 * If the maximum LBA is greater than 0xfffffffe, the user must 7182 * issue a SERVICE ACTION IN (16) command, with the read capacity 7183 * serivce action set. 7184 */ 7185 if (lun->be_lun->maxlba > 0xfffffffe) 7186 scsi_ulto4b(0xffffffff, data->addr); 7187 else 7188 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7189 7190 /* 7191 * XXX KDM this may not be 512 bytes... 7192 */ 7193 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7194 7195 ctsio->scsi_status = SCSI_STATUS_OK; 7196 7197 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7198 ctsio->be_move_done = ctl_config_move_done; 7199 ctl_datamove((union ctl_io *)ctsio); 7200 7201 return (CTL_RETVAL_COMPLETE); 7202} 7203 7204int 7205ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7206{ 7207 struct scsi_read_capacity_16 *cdb; 7208 struct scsi_read_capacity_data_long *data; 7209 struct ctl_lun *lun; 7210 uint64_t lba; 7211 uint32_t alloc_len; 7212 7213 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7214 7215 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7216 7217 alloc_len = scsi_4btoul(cdb->alloc_len); 7218 lba = scsi_8btou64(cdb->addr); 7219 7220 if ((cdb->reladr & SRC16_PMI) 7221 && (lba != 0)) { 7222 ctl_set_invalid_field(/*ctsio*/ ctsio, 7223 /*sks_valid*/ 1, 7224 /*command*/ 1, 7225 /*field*/ 2, 7226 /*bit_valid*/ 0, 7227 /*bit*/ 0); 7228 ctl_done((union ctl_io *)ctsio); 7229 return (CTL_RETVAL_COMPLETE); 7230 } 7231 7232 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7233 7234 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7235 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7236 7237 if (sizeof(*data) < alloc_len) { 7238 ctsio->residual = alloc_len - sizeof(*data); 7239 ctsio->kern_data_len = sizeof(*data); 7240 ctsio->kern_total_len = sizeof(*data); 7241 } else { 7242 ctsio->residual = 0; 7243 ctsio->kern_data_len = alloc_len; 7244 ctsio->kern_total_len = alloc_len; 7245 } 7246 ctsio->kern_data_resid = 0; 7247 ctsio->kern_rel_offset = 0; 7248 ctsio->kern_sg_entries = 0; 7249 7250 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7251 /* XXX KDM this may not be 512 bytes... */ 7252 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7253 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7254 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7255 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
|
7255 data->lalba_lbp[0] |= SRC16_LBPME;
| 7256 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
|
7256 7257 ctsio->scsi_status = SCSI_STATUS_OK; 7258 7259 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7260 ctsio->be_move_done = ctl_config_move_done; 7261 ctl_datamove((union ctl_io *)ctsio); 7262 7263 return (CTL_RETVAL_COMPLETE); 7264} 7265 7266int 7267ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7268{ 7269 struct scsi_maintenance_in *cdb; 7270 int retval; 7271 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7272 int num_target_port_groups, num_target_ports, single; 7273 struct ctl_lun *lun; 7274 struct ctl_softc *softc; 7275 struct ctl_port *port; 7276 struct scsi_target_group_data *rtg_ptr; 7277 struct scsi_target_group_data_extended *rtg_ext_ptr; 7278 struct scsi_target_port_group_descriptor *tpg_desc; 7279 7280 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7281 7282 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7283 softc = control_softc; 7284 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7285 7286 retval = CTL_RETVAL_COMPLETE; 7287 7288 switch (cdb->byte2 & STG_PDF_MASK) { 7289 case STG_PDF_LENGTH: 7290 ext = 0; 7291 break; 7292 case STG_PDF_EXTENDED: 7293 ext = 1; 7294 break; 7295 default: 7296 ctl_set_invalid_field(/*ctsio*/ ctsio, 7297 /*sks_valid*/ 1, 7298 /*command*/ 1, 7299 /*field*/ 2, 7300 /*bit_valid*/ 1, 7301 /*bit*/ 5); 7302 ctl_done((union ctl_io *)ctsio); 7303 return(retval); 7304 } 7305 7306 single = ctl_is_single; 7307 if (single) 7308 num_target_port_groups = 1; 7309 else 7310 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7311 num_target_ports = 0; 7312 mtx_lock(&softc->ctl_lock); 7313 STAILQ_FOREACH(port, &softc->port_list, links) { 7314 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7315 continue; 7316 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7317 continue; 7318 num_target_ports++; 7319 } 7320 mtx_unlock(&softc->ctl_lock); 7321 7322 if (ext) 7323 total_len = sizeof(struct scsi_target_group_data_extended); 7324 else 7325 total_len = sizeof(struct scsi_target_group_data); 7326 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7327 num_target_port_groups + 7328 sizeof(struct scsi_target_port_descriptor) * 7329 num_target_ports * num_target_port_groups; 7330 7331 alloc_len = scsi_4btoul(cdb->length); 7332 7333 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7334 7335 ctsio->kern_sg_entries = 0; 7336 7337 if (total_len < alloc_len) { 7338 ctsio->residual = alloc_len - total_len; 7339 ctsio->kern_data_len = total_len; 7340 ctsio->kern_total_len = total_len; 7341 } else { 7342 ctsio->residual = 0; 7343 ctsio->kern_data_len = alloc_len; 7344 ctsio->kern_total_len = alloc_len; 7345 } 7346 ctsio->kern_data_resid = 0; 7347 ctsio->kern_rel_offset = 0; 7348 7349 if (ext) { 7350 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7351 ctsio->kern_data_ptr; 7352 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7353 rtg_ext_ptr->format_type = 0x10; 7354 rtg_ext_ptr->implicit_transition_time = 0; 7355 tpg_desc = &rtg_ext_ptr->groups[0]; 7356 } else { 7357 rtg_ptr = (struct scsi_target_group_data *) 7358 ctsio->kern_data_ptr; 7359 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7360 tpg_desc = &rtg_ptr->groups[0]; 7361 } 7362 7363 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7364 mtx_lock(&softc->ctl_lock); 7365 for (g = 0; g < num_target_port_groups; g++) { 7366 if (g == pg) 7367 tpg_desc->pref_state = TPG_PRIMARY | 7368 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7369 else 7370 tpg_desc->pref_state = 7371 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7372 tpg_desc->support = TPG_AO_SUP; 7373 if (!single) 7374 tpg_desc->support |= TPG_AN_SUP; 7375 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7376 tpg_desc->status = TPG_IMPLICIT; 7377 pc = 0; 7378 STAILQ_FOREACH(port, &softc->port_list, links) { 7379 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7380 continue; 7381 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7382 CTL_MAX_LUNS) 7383 continue; 7384 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7385 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7386 relative_target_port_identifier); 7387 pc++; 7388 } 7389 tpg_desc->target_port_count = pc; 7390 tpg_desc = (struct scsi_target_port_group_descriptor *) 7391 &tpg_desc->descriptors[pc]; 7392 } 7393 mtx_unlock(&softc->ctl_lock); 7394 7395 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7396 ctsio->be_move_done = ctl_config_move_done; 7397 7398 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7399 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7400 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7401 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7402 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7403 7404 ctl_datamove((union ctl_io *)ctsio); 7405 return(retval); 7406} 7407 7408int 7409ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7410{ 7411 struct ctl_lun *lun; 7412 struct scsi_report_supported_opcodes *cdb; 7413 const struct ctl_cmd_entry *entry, *sentry; 7414 struct scsi_report_supported_opcodes_all *all; 7415 struct scsi_report_supported_opcodes_descr *descr; 7416 struct scsi_report_supported_opcodes_one *one; 7417 int retval; 7418 int alloc_len, total_len; 7419 int opcode, service_action, i, j, num; 7420 7421 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7422 7423 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7424 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7425 7426 retval = CTL_RETVAL_COMPLETE; 7427 7428 opcode = cdb->requested_opcode; 7429 service_action = scsi_2btoul(cdb->requested_service_action); 7430 switch (cdb->options & RSO_OPTIONS_MASK) { 7431 case RSO_OPTIONS_ALL: 7432 num = 0; 7433 for (i = 0; i < 256; i++) { 7434 entry = &ctl_cmd_table[i]; 7435 if (entry->flags & CTL_CMD_FLAG_SA5) { 7436 for (j = 0; j < 32; j++) { 7437 sentry = &((const struct ctl_cmd_entry *) 7438 entry->execute)[j]; 7439 if (ctl_cmd_applicable( 7440 lun->be_lun->lun_type, sentry)) 7441 num++; 7442 } 7443 } else { 7444 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7445 entry)) 7446 num++; 7447 } 7448 } 7449 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7450 num * sizeof(struct scsi_report_supported_opcodes_descr); 7451 break; 7452 case RSO_OPTIONS_OC: 7453 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7454 ctl_set_invalid_field(/*ctsio*/ ctsio, 7455 /*sks_valid*/ 1, 7456 /*command*/ 1, 7457 /*field*/ 2, 7458 /*bit_valid*/ 1, 7459 /*bit*/ 2); 7460 ctl_done((union ctl_io *)ctsio); 7461 return (CTL_RETVAL_COMPLETE); 7462 } 7463 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7464 break; 7465 case RSO_OPTIONS_OC_SA: 7466 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7467 service_action >= 32) { 7468 ctl_set_invalid_field(/*ctsio*/ ctsio, 7469 /*sks_valid*/ 1, 7470 /*command*/ 1, 7471 /*field*/ 2, 7472 /*bit_valid*/ 1, 7473 /*bit*/ 2); 7474 ctl_done((union ctl_io *)ctsio); 7475 return (CTL_RETVAL_COMPLETE); 7476 } 7477 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7478 break; 7479 default: 7480 ctl_set_invalid_field(/*ctsio*/ ctsio, 7481 /*sks_valid*/ 1, 7482 /*command*/ 1, 7483 /*field*/ 2, 7484 /*bit_valid*/ 1, 7485 /*bit*/ 2); 7486 ctl_done((union ctl_io *)ctsio); 7487 return (CTL_RETVAL_COMPLETE); 7488 } 7489 7490 alloc_len = scsi_4btoul(cdb->length); 7491 7492 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7493 7494 ctsio->kern_sg_entries = 0; 7495 7496 if (total_len < alloc_len) { 7497 ctsio->residual = alloc_len - total_len; 7498 ctsio->kern_data_len = total_len; 7499 ctsio->kern_total_len = total_len; 7500 } else { 7501 ctsio->residual = 0; 7502 ctsio->kern_data_len = alloc_len; 7503 ctsio->kern_total_len = alloc_len; 7504 } 7505 ctsio->kern_data_resid = 0; 7506 ctsio->kern_rel_offset = 0; 7507 7508 switch (cdb->options & RSO_OPTIONS_MASK) { 7509 case RSO_OPTIONS_ALL: 7510 all = (struct scsi_report_supported_opcodes_all *) 7511 ctsio->kern_data_ptr; 7512 num = 0; 7513 for (i = 0; i < 256; i++) { 7514 entry = &ctl_cmd_table[i]; 7515 if (entry->flags & CTL_CMD_FLAG_SA5) { 7516 for (j = 0; j < 32; j++) { 7517 sentry = &((const struct ctl_cmd_entry *) 7518 entry->execute)[j]; 7519 if (!ctl_cmd_applicable( 7520 lun->be_lun->lun_type, sentry)) 7521 continue; 7522 descr = &all->descr[num++]; 7523 descr->opcode = i; 7524 scsi_ulto2b(j, descr->service_action); 7525 descr->flags = RSO_SERVACTV; 7526 scsi_ulto2b(sentry->length, 7527 descr->cdb_length); 7528 } 7529 } else { 7530 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7531 entry)) 7532 continue; 7533 descr = &all->descr[num++]; 7534 descr->opcode = i; 7535 scsi_ulto2b(0, descr->service_action); 7536 descr->flags = 0; 7537 scsi_ulto2b(entry->length, descr->cdb_length); 7538 } 7539 } 7540 scsi_ulto4b( 7541 num * sizeof(struct scsi_report_supported_opcodes_descr), 7542 all->length); 7543 break; 7544 case RSO_OPTIONS_OC: 7545 one = (struct scsi_report_supported_opcodes_one *) 7546 ctsio->kern_data_ptr; 7547 entry = &ctl_cmd_table[opcode]; 7548 goto fill_one; 7549 case RSO_OPTIONS_OC_SA: 7550 one = (struct scsi_report_supported_opcodes_one *) 7551 ctsio->kern_data_ptr; 7552 entry = &ctl_cmd_table[opcode]; 7553 entry = &((const struct ctl_cmd_entry *) 7554 entry->execute)[service_action]; 7555fill_one: 7556 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7557 one->support = 3; 7558 scsi_ulto2b(entry->length, one->cdb_length); 7559 one->cdb_usage[0] = opcode; 7560 memcpy(&one->cdb_usage[1], entry->usage, 7561 entry->length - 1); 7562 } else 7563 one->support = 1; 7564 break; 7565 } 7566 7567 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7568 ctsio->be_move_done = ctl_config_move_done; 7569 7570 ctl_datamove((union ctl_io *)ctsio); 7571 return(retval); 7572} 7573 7574int 7575ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7576{ 7577 struct ctl_lun *lun; 7578 struct scsi_report_supported_tmf *cdb; 7579 struct scsi_report_supported_tmf_data *data; 7580 int retval; 7581 int alloc_len, total_len; 7582 7583 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7584 7585 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7586 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7587 7588 retval = CTL_RETVAL_COMPLETE; 7589 7590 total_len = sizeof(struct scsi_report_supported_tmf_data); 7591 alloc_len = scsi_4btoul(cdb->length); 7592 7593 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7594 7595 ctsio->kern_sg_entries = 0; 7596 7597 if (total_len < alloc_len) { 7598 ctsio->residual = alloc_len - total_len; 7599 ctsio->kern_data_len = total_len; 7600 ctsio->kern_total_len = total_len; 7601 } else { 7602 ctsio->residual = 0; 7603 ctsio->kern_data_len = alloc_len; 7604 ctsio->kern_total_len = alloc_len; 7605 } 7606 ctsio->kern_data_resid = 0; 7607 ctsio->kern_rel_offset = 0; 7608 7609 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7610 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7611 data->byte2 |= RST_ITNRS; 7612 7613 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7614 ctsio->be_move_done = ctl_config_move_done; 7615 7616 ctl_datamove((union ctl_io *)ctsio); 7617 return (retval); 7618} 7619 7620int 7621ctl_report_timestamp(struct ctl_scsiio *ctsio) 7622{ 7623 struct ctl_lun *lun; 7624 struct scsi_report_timestamp *cdb; 7625 struct scsi_report_timestamp_data *data; 7626 struct timeval tv; 7627 int64_t timestamp; 7628 int retval; 7629 int alloc_len, total_len; 7630 7631 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7632 7633 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7634 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7635 7636 retval = CTL_RETVAL_COMPLETE; 7637 7638 total_len = sizeof(struct scsi_report_timestamp_data); 7639 alloc_len = scsi_4btoul(cdb->length); 7640 7641 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7642 7643 ctsio->kern_sg_entries = 0; 7644 7645 if (total_len < alloc_len) { 7646 ctsio->residual = alloc_len - total_len; 7647 ctsio->kern_data_len = total_len; 7648 ctsio->kern_total_len = total_len; 7649 } else { 7650 ctsio->residual = 0; 7651 ctsio->kern_data_len = alloc_len; 7652 ctsio->kern_total_len = alloc_len; 7653 } 7654 ctsio->kern_data_resid = 0; 7655 ctsio->kern_rel_offset = 0; 7656 7657 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7658 scsi_ulto2b(sizeof(*data) - 2, data->length); 7659 data->origin = RTS_ORIG_OUTSIDE; 7660 getmicrotime(&tv); 7661 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7662 scsi_ulto4b(timestamp >> 16, data->timestamp); 7663 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7664 7665 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7666 ctsio->be_move_done = ctl_config_move_done; 7667 7668 ctl_datamove((union ctl_io *)ctsio); 7669 return (retval); 7670} 7671 7672int 7673ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7674{ 7675 struct scsi_per_res_in *cdb; 7676 int alloc_len, total_len = 0; 7677 /* struct scsi_per_res_in_rsrv in_data; */ 7678 struct ctl_lun *lun; 7679 struct ctl_softc *softc; 7680 7681 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7682 7683 softc = control_softc; 7684 7685 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7686 7687 alloc_len = scsi_2btoul(cdb->length); 7688 7689 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7690 7691retry: 7692 mtx_lock(&lun->lun_lock); 7693 switch (cdb->action) { 7694 case SPRI_RK: /* read keys */ 7695 total_len = sizeof(struct scsi_per_res_in_keys) + 7696 lun->pr_key_count * 7697 sizeof(struct scsi_per_res_key); 7698 break; 7699 case SPRI_RR: /* read reservation */ 7700 if (lun->flags & CTL_LUN_PR_RESERVED) 7701 total_len = sizeof(struct scsi_per_res_in_rsrv); 7702 else 7703 total_len = sizeof(struct scsi_per_res_in_header); 7704 break; 7705 case SPRI_RC: /* report capabilities */ 7706 total_len = sizeof(struct scsi_per_res_cap); 7707 break; 7708 case SPRI_RS: /* read full status */ 7709 total_len = sizeof(struct scsi_per_res_in_header) + 7710 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7711 lun->pr_key_count; 7712 break; 7713 default: 7714 panic("Invalid PR type %x", cdb->action); 7715 } 7716 mtx_unlock(&lun->lun_lock); 7717 7718 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7719 7720 if (total_len < alloc_len) { 7721 ctsio->residual = alloc_len - total_len; 7722 ctsio->kern_data_len = total_len; 7723 ctsio->kern_total_len = total_len; 7724 } else { 7725 ctsio->residual = 0; 7726 ctsio->kern_data_len = alloc_len; 7727 ctsio->kern_total_len = alloc_len; 7728 } 7729 7730 ctsio->kern_data_resid = 0; 7731 ctsio->kern_rel_offset = 0; 7732 ctsio->kern_sg_entries = 0; 7733 7734 mtx_lock(&lun->lun_lock); 7735 switch (cdb->action) { 7736 case SPRI_RK: { // read keys 7737 struct scsi_per_res_in_keys *res_keys; 7738 int i, key_count; 7739 7740 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7741 7742 /* 7743 * We had to drop the lock to allocate our buffer, which 7744 * leaves time for someone to come in with another 7745 * persistent reservation. (That is unlikely, though, 7746 * since this should be the only persistent reservation 7747 * command active right now.) 7748 */ 7749 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7750 (lun->pr_key_count * 7751 sizeof(struct scsi_per_res_key)))){ 7752 mtx_unlock(&lun->lun_lock); 7753 free(ctsio->kern_data_ptr, M_CTL); 7754 printf("%s: reservation length changed, retrying\n", 7755 __func__); 7756 goto retry; 7757 } 7758 7759 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7760 7761 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7762 lun->pr_key_count, res_keys->header.length); 7763 7764 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7765 if (!lun->per_res[i].registered) 7766 continue; 7767 7768 /* 7769 * We used lun->pr_key_count to calculate the 7770 * size to allocate. If it turns out the number of 7771 * initiators with the registered flag set is 7772 * larger than that (i.e. they haven't been kept in 7773 * sync), we've got a problem. 7774 */ 7775 if (key_count >= lun->pr_key_count) { 7776#ifdef NEEDTOPORT 7777 csevent_log(CSC_CTL | CSC_SHELF_SW | 7778 CTL_PR_ERROR, 7779 csevent_LogType_Fault, 7780 csevent_AlertLevel_Yellow, 7781 csevent_FRU_ShelfController, 7782 csevent_FRU_Firmware, 7783 csevent_FRU_Unknown, 7784 "registered keys %d >= key " 7785 "count %d", key_count, 7786 lun->pr_key_count); 7787#endif 7788 key_count++; 7789 continue; 7790 } 7791 memcpy(res_keys->keys[key_count].key, 7792 lun->per_res[i].res_key.key, 7793 ctl_min(sizeof(res_keys->keys[key_count].key), 7794 sizeof(lun->per_res[i].res_key))); 7795 key_count++; 7796 } 7797 break; 7798 } 7799 case SPRI_RR: { // read reservation 7800 struct scsi_per_res_in_rsrv *res; 7801 int tmp_len, header_only; 7802 7803 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7804 7805 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7806 7807 if (lun->flags & CTL_LUN_PR_RESERVED) 7808 { 7809 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7810 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7811 res->header.length); 7812 header_only = 0; 7813 } else { 7814 tmp_len = sizeof(struct scsi_per_res_in_header); 7815 scsi_ulto4b(0, res->header.length); 7816 header_only = 1; 7817 } 7818 7819 /* 7820 * We had to drop the lock to allocate our buffer, which 7821 * leaves time for someone to come in with another 7822 * persistent reservation. (That is unlikely, though, 7823 * since this should be the only persistent reservation 7824 * command active right now.) 7825 */ 7826 if (tmp_len != total_len) { 7827 mtx_unlock(&lun->lun_lock); 7828 free(ctsio->kern_data_ptr, M_CTL); 7829 printf("%s: reservation status changed, retrying\n", 7830 __func__); 7831 goto retry; 7832 } 7833 7834 /* 7835 * No reservation held, so we're done. 7836 */ 7837 if (header_only != 0) 7838 break; 7839 7840 /* 7841 * If the registration is an All Registrants type, the key 7842 * is 0, since it doesn't really matter. 7843 */ 7844 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7845 memcpy(res->data.reservation, 7846 &lun->per_res[lun->pr_res_idx].res_key, 7847 sizeof(struct scsi_per_res_key)); 7848 } 7849 res->data.scopetype = lun->res_type; 7850 break; 7851 } 7852 case SPRI_RC: //report capabilities 7853 { 7854 struct scsi_per_res_cap *res_cap; 7855 uint16_t type_mask; 7856 7857 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7858 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7859 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7860 type_mask = SPRI_TM_WR_EX_AR | 7861 SPRI_TM_EX_AC_RO | 7862 SPRI_TM_WR_EX_RO | 7863 SPRI_TM_EX_AC | 7864 SPRI_TM_WR_EX | 7865 SPRI_TM_EX_AC_AR; 7866 scsi_ulto2b(type_mask, res_cap->type_mask); 7867 break; 7868 } 7869 case SPRI_RS: { // read full status 7870 struct scsi_per_res_in_full *res_status; 7871 struct scsi_per_res_in_full_desc *res_desc; 7872 struct ctl_port *port; 7873 int i, len; 7874 7875 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7876 7877 /* 7878 * We had to drop the lock to allocate our buffer, which 7879 * leaves time for someone to come in with another 7880 * persistent reservation. (That is unlikely, though, 7881 * since this should be the only persistent reservation 7882 * command active right now.) 7883 */ 7884 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7885 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7886 lun->pr_key_count)){ 7887 mtx_unlock(&lun->lun_lock); 7888 free(ctsio->kern_data_ptr, M_CTL); 7889 printf("%s: reservation length changed, retrying\n", 7890 __func__); 7891 goto retry; 7892 } 7893 7894 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7895 7896 res_desc = &res_status->desc[0]; 7897 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7898 if (!lun->per_res[i].registered) 7899 continue; 7900 7901 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7902 sizeof(res_desc->res_key)); 7903 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7904 (lun->pr_res_idx == i || 7905 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7906 res_desc->flags = SPRI_FULL_R_HOLDER; 7907 res_desc->scopetype = lun->res_type; 7908 } 7909 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7910 res_desc->rel_trgt_port_id); 7911 len = 0; 7912 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7913 if (port != NULL) 7914 len = ctl_create_iid(port, 7915 i % CTL_MAX_INIT_PER_PORT, 7916 res_desc->transport_id); 7917 scsi_ulto4b(len, res_desc->additional_length); 7918 res_desc = (struct scsi_per_res_in_full_desc *) 7919 &res_desc->transport_id[len]; 7920 } 7921 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7922 res_status->header.length); 7923 break; 7924 } 7925 default: 7926 /* 7927 * This is a bug, because we just checked for this above, 7928 * and should have returned an error. 7929 */ 7930 panic("Invalid PR type %x", cdb->action); 7931 break; /* NOTREACHED */ 7932 } 7933 mtx_unlock(&lun->lun_lock); 7934 7935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7936 ctsio->be_move_done = ctl_config_move_done; 7937 7938 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7939 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7940 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7941 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7942 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7943 7944 ctl_datamove((union ctl_io *)ctsio); 7945 7946 return (CTL_RETVAL_COMPLETE); 7947} 7948 7949/* 7950 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7951 * it should return. 7952 */ 7953static int 7954ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7955 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7956 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7957 struct scsi_per_res_out_parms* param) 7958{ 7959 union ctl_ha_msg persis_io; 7960 int retval, i; 7961 int isc_retval; 7962 7963 retval = 0; 7964 7965 mtx_lock(&lun->lun_lock); 7966 if (sa_res_key == 0) { 7967 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7968 /* validate scope and type */ 7969 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7970 SPR_LU_SCOPE) { 7971 mtx_unlock(&lun->lun_lock); 7972 ctl_set_invalid_field(/*ctsio*/ ctsio, 7973 /*sks_valid*/ 1, 7974 /*command*/ 1, 7975 /*field*/ 2, 7976 /*bit_valid*/ 1, 7977 /*bit*/ 4); 7978 ctl_done((union ctl_io *)ctsio); 7979 return (1); 7980 } 7981 7982 if (type>8 || type==2 || type==4 || type==0) { 7983 mtx_unlock(&lun->lun_lock); 7984 ctl_set_invalid_field(/*ctsio*/ ctsio, 7985 /*sks_valid*/ 1, 7986 /*command*/ 1, 7987 /*field*/ 2, 7988 /*bit_valid*/ 1, 7989 /*bit*/ 0); 7990 ctl_done((union ctl_io *)ctsio); 7991 return (1); 7992 } 7993 7994 /* temporarily unregister this nexus */ 7995 lun->per_res[residx].registered = 0; 7996 7997 /* 7998 * Unregister everybody else and build UA for 7999 * them 8000 */ 8001 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8002 if (lun->per_res[i].registered == 0) 8003 continue; 8004 8005 if (!persis_offset 8006 && i <CTL_MAX_INITIATORS) 8007 lun->pending_ua[i] |= 8008 CTL_UA_REG_PREEMPT; 8009 else if (persis_offset 8010 && i >= persis_offset) 8011 lun->pending_ua[i-persis_offset] |= 8012 CTL_UA_REG_PREEMPT; 8013 lun->per_res[i].registered = 0; 8014 memset(&lun->per_res[i].res_key, 0, 8015 sizeof(struct scsi_per_res_key)); 8016 } 8017 lun->per_res[residx].registered = 1; 8018 lun->pr_key_count = 1; 8019 lun->res_type = type; 8020 if (lun->res_type != SPR_TYPE_WR_EX_AR 8021 && lun->res_type != SPR_TYPE_EX_AC_AR) 8022 lun->pr_res_idx = residx; 8023 8024 /* send msg to other side */ 8025 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8026 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8027 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8028 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8029 persis_io.pr.pr_info.res_type = type; 8030 memcpy(persis_io.pr.pr_info.sa_res_key, 8031 param->serv_act_res_key, 8032 sizeof(param->serv_act_res_key)); 8033 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8034 &persis_io, sizeof(persis_io), 0)) > 8035 CTL_HA_STATUS_SUCCESS) { 8036 printf("CTL:Persis Out error returned " 8037 "from ctl_ha_msg_send %d\n", 8038 isc_retval); 8039 } 8040 } else { 8041 /* not all registrants */ 8042 mtx_unlock(&lun->lun_lock); 8043 free(ctsio->kern_data_ptr, M_CTL); 8044 ctl_set_invalid_field(ctsio, 8045 /*sks_valid*/ 1, 8046 /*command*/ 0, 8047 /*field*/ 8, 8048 /*bit_valid*/ 0, 8049 /*bit*/ 0); 8050 ctl_done((union ctl_io *)ctsio); 8051 return (1); 8052 } 8053 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8054 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8055 int found = 0; 8056 8057 if (res_key == sa_res_key) { 8058 /* special case */ 8059 /* 8060 * The spec implies this is not good but doesn't 8061 * say what to do. There are two choices either 8062 * generate a res conflict or check condition 8063 * with illegal field in parameter data. Since 8064 * that is what is done when the sa_res_key is 8065 * zero I'll take that approach since this has 8066 * to do with the sa_res_key. 8067 */ 8068 mtx_unlock(&lun->lun_lock); 8069 free(ctsio->kern_data_ptr, M_CTL); 8070 ctl_set_invalid_field(ctsio, 8071 /*sks_valid*/ 1, 8072 /*command*/ 0, 8073 /*field*/ 8, 8074 /*bit_valid*/ 0, 8075 /*bit*/ 0); 8076 ctl_done((union ctl_io *)ctsio); 8077 return (1); 8078 } 8079 8080 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8081 if (lun->per_res[i].registered 8082 && memcmp(param->serv_act_res_key, 8083 lun->per_res[i].res_key.key, 8084 sizeof(struct scsi_per_res_key)) != 0) 8085 continue; 8086 8087 found = 1; 8088 lun->per_res[i].registered = 0; 8089 memset(&lun->per_res[i].res_key, 0, 8090 sizeof(struct scsi_per_res_key)); 8091 lun->pr_key_count--; 8092 8093 if (!persis_offset && i < CTL_MAX_INITIATORS) 8094 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8095 else if (persis_offset && i >= persis_offset) 8096 lun->pending_ua[i-persis_offset] |= 8097 CTL_UA_REG_PREEMPT; 8098 } 8099 if (!found) { 8100 mtx_unlock(&lun->lun_lock); 8101 free(ctsio->kern_data_ptr, M_CTL); 8102 ctl_set_reservation_conflict(ctsio); 8103 ctl_done((union ctl_io *)ctsio); 8104 return (CTL_RETVAL_COMPLETE); 8105 } 8106 /* send msg to other side */ 8107 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8108 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8109 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8110 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8111 persis_io.pr.pr_info.res_type = type; 8112 memcpy(persis_io.pr.pr_info.sa_res_key, 8113 param->serv_act_res_key, 8114 sizeof(param->serv_act_res_key)); 8115 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8116 &persis_io, sizeof(persis_io), 0)) > 8117 CTL_HA_STATUS_SUCCESS) { 8118 printf("CTL:Persis Out error returned from " 8119 "ctl_ha_msg_send %d\n", isc_retval); 8120 } 8121 } else { 8122 /* Reserved but not all registrants */ 8123 /* sa_res_key is res holder */ 8124 if (memcmp(param->serv_act_res_key, 8125 lun->per_res[lun->pr_res_idx].res_key.key, 8126 sizeof(struct scsi_per_res_key)) == 0) { 8127 /* validate scope and type */ 8128 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8129 SPR_LU_SCOPE) { 8130 mtx_unlock(&lun->lun_lock); 8131 ctl_set_invalid_field(/*ctsio*/ ctsio, 8132 /*sks_valid*/ 1, 8133 /*command*/ 1, 8134 /*field*/ 2, 8135 /*bit_valid*/ 1, 8136 /*bit*/ 4); 8137 ctl_done((union ctl_io *)ctsio); 8138 return (1); 8139 } 8140 8141 if (type>8 || type==2 || type==4 || type==0) { 8142 mtx_unlock(&lun->lun_lock); 8143 ctl_set_invalid_field(/*ctsio*/ ctsio, 8144 /*sks_valid*/ 1, 8145 /*command*/ 1, 8146 /*field*/ 2, 8147 /*bit_valid*/ 1, 8148 /*bit*/ 0); 8149 ctl_done((union ctl_io *)ctsio); 8150 return (1); 8151 } 8152 8153 /* 8154 * Do the following: 8155 * if sa_res_key != res_key remove all 8156 * registrants w/sa_res_key and generate UA 8157 * for these registrants(Registrations 8158 * Preempted) if it wasn't an exclusive 8159 * reservation generate UA(Reservations 8160 * Preempted) for all other registered nexuses 8161 * if the type has changed. Establish the new 8162 * reservation and holder. If res_key and 8163 * sa_res_key are the same do the above 8164 * except don't unregister the res holder. 8165 */ 8166 8167 /* 8168 * Temporarily unregister so it won't get 8169 * removed or UA generated 8170 */ 8171 lun->per_res[residx].registered = 0; 8172 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8173 if (lun->per_res[i].registered == 0) 8174 continue; 8175 8176 if (memcmp(param->serv_act_res_key, 8177 lun->per_res[i].res_key.key, 8178 sizeof(struct scsi_per_res_key)) == 0) { 8179 lun->per_res[i].registered = 0; 8180 memset(&lun->per_res[i].res_key, 8181 0, 8182 sizeof(struct scsi_per_res_key)); 8183 lun->pr_key_count--; 8184 8185 if (!persis_offset 8186 && i < CTL_MAX_INITIATORS) 8187 lun->pending_ua[i] |= 8188 CTL_UA_REG_PREEMPT; 8189 else if (persis_offset 8190 && i >= persis_offset) 8191 lun->pending_ua[i-persis_offset] |= 8192 CTL_UA_REG_PREEMPT; 8193 } else if (type != lun->res_type 8194 && (lun->res_type == SPR_TYPE_WR_EX_RO 8195 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8196 if (!persis_offset 8197 && i < CTL_MAX_INITIATORS) 8198 lun->pending_ua[i] |= 8199 CTL_UA_RES_RELEASE; 8200 else if (persis_offset 8201 && i >= persis_offset) 8202 lun->pending_ua[ 8203 i-persis_offset] |= 8204 CTL_UA_RES_RELEASE; 8205 } 8206 } 8207 lun->per_res[residx].registered = 1; 8208 lun->res_type = type; 8209 if (lun->res_type != SPR_TYPE_WR_EX_AR 8210 && lun->res_type != SPR_TYPE_EX_AC_AR) 8211 lun->pr_res_idx = residx; 8212 else 8213 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8214 8215 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8216 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8217 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8218 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8219 persis_io.pr.pr_info.res_type = type; 8220 memcpy(persis_io.pr.pr_info.sa_res_key, 8221 param->serv_act_res_key, 8222 sizeof(param->serv_act_res_key)); 8223 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8224 &persis_io, sizeof(persis_io), 0)) > 8225 CTL_HA_STATUS_SUCCESS) { 8226 printf("CTL:Persis Out error returned " 8227 "from ctl_ha_msg_send %d\n", 8228 isc_retval); 8229 } 8230 } else { 8231 /* 8232 * sa_res_key is not the res holder just 8233 * remove registrants 8234 */ 8235 int found=0; 8236 8237 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8238 if (memcmp(param->serv_act_res_key, 8239 lun->per_res[i].res_key.key, 8240 sizeof(struct scsi_per_res_key)) != 0) 8241 continue; 8242 8243 found = 1; 8244 lun->per_res[i].registered = 0; 8245 memset(&lun->per_res[i].res_key, 0, 8246 sizeof(struct scsi_per_res_key)); 8247 lun->pr_key_count--; 8248 8249 if (!persis_offset 8250 && i < CTL_MAX_INITIATORS) 8251 lun->pending_ua[i] |= 8252 CTL_UA_REG_PREEMPT; 8253 else if (persis_offset 8254 && i >= persis_offset) 8255 lun->pending_ua[i-persis_offset] |= 8256 CTL_UA_REG_PREEMPT; 8257 } 8258 8259 if (!found) { 8260 mtx_unlock(&lun->lun_lock); 8261 free(ctsio->kern_data_ptr, M_CTL); 8262 ctl_set_reservation_conflict(ctsio); 8263 ctl_done((union ctl_io *)ctsio); 8264 return (1); 8265 } 8266 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8267 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8268 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8269 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8270 persis_io.pr.pr_info.res_type = type; 8271 memcpy(persis_io.pr.pr_info.sa_res_key, 8272 param->serv_act_res_key, 8273 sizeof(param->serv_act_res_key)); 8274 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8275 &persis_io, sizeof(persis_io), 0)) > 8276 CTL_HA_STATUS_SUCCESS) { 8277 printf("CTL:Persis Out error returned " 8278 "from ctl_ha_msg_send %d\n", 8279 isc_retval); 8280 } 8281 } 8282 } 8283 8284 lun->PRGeneration++; 8285 mtx_unlock(&lun->lun_lock); 8286 8287 return (retval); 8288} 8289 8290static void 8291ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8292{ 8293 int i; 8294 8295 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8296 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8297 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8298 msg->pr.pr_info.sa_res_key, 8299 sizeof(struct scsi_per_res_key)) != 0) { 8300 uint64_t sa_res_key; 8301 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8302 8303 if (sa_res_key == 0) { 8304 /* temporarily unregister this nexus */ 8305 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8306 8307 /* 8308 * Unregister everybody else and build UA for 8309 * them 8310 */ 8311 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8312 if (lun->per_res[i].registered == 0) 8313 continue; 8314 8315 if (!persis_offset 8316 && i < CTL_MAX_INITIATORS) 8317 lun->pending_ua[i] |= 8318 CTL_UA_REG_PREEMPT; 8319 else if (persis_offset && i >= persis_offset) 8320 lun->pending_ua[i - persis_offset] |= 8321 CTL_UA_REG_PREEMPT; 8322 lun->per_res[i].registered = 0; 8323 memset(&lun->per_res[i].res_key, 0, 8324 sizeof(struct scsi_per_res_key)); 8325 } 8326 8327 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8328 lun->pr_key_count = 1; 8329 lun->res_type = msg->pr.pr_info.res_type; 8330 if (lun->res_type != SPR_TYPE_WR_EX_AR 8331 && lun->res_type != SPR_TYPE_EX_AC_AR) 8332 lun->pr_res_idx = msg->pr.pr_info.residx; 8333 } else { 8334 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8335 if (memcmp(msg->pr.pr_info.sa_res_key, 8336 lun->per_res[i].res_key.key, 8337 sizeof(struct scsi_per_res_key)) != 0) 8338 continue; 8339 8340 lun->per_res[i].registered = 0; 8341 memset(&lun->per_res[i].res_key, 0, 8342 sizeof(struct scsi_per_res_key)); 8343 lun->pr_key_count--; 8344 8345 if (!persis_offset 8346 && i < persis_offset) 8347 lun->pending_ua[i] |= 8348 CTL_UA_REG_PREEMPT; 8349 else if (persis_offset 8350 && i >= persis_offset) 8351 lun->pending_ua[i - persis_offset] |= 8352 CTL_UA_REG_PREEMPT; 8353 } 8354 } 8355 } else { 8356 /* 8357 * Temporarily unregister so it won't get removed 8358 * or UA generated 8359 */ 8360 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8361 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8362 if (lun->per_res[i].registered == 0) 8363 continue; 8364 8365 if (memcmp(msg->pr.pr_info.sa_res_key, 8366 lun->per_res[i].res_key.key, 8367 sizeof(struct scsi_per_res_key)) == 0) { 8368 lun->per_res[i].registered = 0; 8369 memset(&lun->per_res[i].res_key, 0, 8370 sizeof(struct scsi_per_res_key)); 8371 lun->pr_key_count--; 8372 if (!persis_offset 8373 && i < CTL_MAX_INITIATORS) 8374 lun->pending_ua[i] |= 8375 CTL_UA_REG_PREEMPT; 8376 else if (persis_offset 8377 && i >= persis_offset) 8378 lun->pending_ua[i - persis_offset] |= 8379 CTL_UA_REG_PREEMPT; 8380 } else if (msg->pr.pr_info.res_type != lun->res_type 8381 && (lun->res_type == SPR_TYPE_WR_EX_RO 8382 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8383 if (!persis_offset 8384 && i < persis_offset) 8385 lun->pending_ua[i] |= 8386 CTL_UA_RES_RELEASE; 8387 else if (persis_offset 8388 && i >= persis_offset) 8389 lun->pending_ua[i - persis_offset] |= 8390 CTL_UA_RES_RELEASE; 8391 } 8392 } 8393 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8394 lun->res_type = msg->pr.pr_info.res_type; 8395 if (lun->res_type != SPR_TYPE_WR_EX_AR 8396 && lun->res_type != SPR_TYPE_EX_AC_AR) 8397 lun->pr_res_idx = msg->pr.pr_info.residx; 8398 else 8399 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8400 } 8401 lun->PRGeneration++; 8402 8403} 8404 8405 8406int 8407ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8408{ 8409 int retval; 8410 int isc_retval; 8411 u_int32_t param_len; 8412 struct scsi_per_res_out *cdb; 8413 struct ctl_lun *lun; 8414 struct scsi_per_res_out_parms* param; 8415 struct ctl_softc *softc; 8416 uint32_t residx; 8417 uint64_t res_key, sa_res_key; 8418 uint8_t type; 8419 union ctl_ha_msg persis_io; 8420 int i; 8421 8422 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8423 8424 retval = CTL_RETVAL_COMPLETE; 8425 8426 softc = control_softc; 8427 8428 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8429 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8430 8431 /* 8432 * We only support whole-LUN scope. The scope & type are ignored for 8433 * register, register and ignore existing key and clear. 8434 * We sometimes ignore scope and type on preempts too!! 8435 * Verify reservation type here as well. 8436 */ 8437 type = cdb->scope_type & SPR_TYPE_MASK; 8438 if ((cdb->action == SPRO_RESERVE) 8439 || (cdb->action == SPRO_RELEASE)) { 8440 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8441 ctl_set_invalid_field(/*ctsio*/ ctsio, 8442 /*sks_valid*/ 1, 8443 /*command*/ 1, 8444 /*field*/ 2, 8445 /*bit_valid*/ 1, 8446 /*bit*/ 4); 8447 ctl_done((union ctl_io *)ctsio); 8448 return (CTL_RETVAL_COMPLETE); 8449 } 8450 8451 if (type>8 || type==2 || type==4 || type==0) { 8452 ctl_set_invalid_field(/*ctsio*/ ctsio, 8453 /*sks_valid*/ 1, 8454 /*command*/ 1, 8455 /*field*/ 2, 8456 /*bit_valid*/ 1, 8457 /*bit*/ 0); 8458 ctl_done((union ctl_io *)ctsio); 8459 return (CTL_RETVAL_COMPLETE); 8460 } 8461 } 8462 8463 param_len = scsi_4btoul(cdb->length); 8464 8465 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8466 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8467 ctsio->kern_data_len = param_len; 8468 ctsio->kern_total_len = param_len; 8469 ctsio->kern_data_resid = 0; 8470 ctsio->kern_rel_offset = 0; 8471 ctsio->kern_sg_entries = 0; 8472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8473 ctsio->be_move_done = ctl_config_move_done; 8474 ctl_datamove((union ctl_io *)ctsio); 8475 8476 return (CTL_RETVAL_COMPLETE); 8477 } 8478 8479 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8480 8481 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8482 res_key = scsi_8btou64(param->res_key.key); 8483 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8484 8485 /* 8486 * Validate the reservation key here except for SPRO_REG_IGNO 8487 * This must be done for all other service actions 8488 */ 8489 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8490 mtx_lock(&lun->lun_lock); 8491 if (lun->per_res[residx].registered) { 8492 if (memcmp(param->res_key.key, 8493 lun->per_res[residx].res_key.key, 8494 ctl_min(sizeof(param->res_key), 8495 sizeof(lun->per_res[residx].res_key))) != 0) { 8496 /* 8497 * The current key passed in doesn't match 8498 * the one the initiator previously 8499 * registered. 8500 */ 8501 mtx_unlock(&lun->lun_lock); 8502 free(ctsio->kern_data_ptr, M_CTL); 8503 ctl_set_reservation_conflict(ctsio); 8504 ctl_done((union ctl_io *)ctsio); 8505 return (CTL_RETVAL_COMPLETE); 8506 } 8507 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8508 /* 8509 * We are not registered 8510 */ 8511 mtx_unlock(&lun->lun_lock); 8512 free(ctsio->kern_data_ptr, M_CTL); 8513 ctl_set_reservation_conflict(ctsio); 8514 ctl_done((union ctl_io *)ctsio); 8515 return (CTL_RETVAL_COMPLETE); 8516 } else if (res_key != 0) { 8517 /* 8518 * We are not registered and trying to register but 8519 * the register key isn't zero. 8520 */ 8521 mtx_unlock(&lun->lun_lock); 8522 free(ctsio->kern_data_ptr, M_CTL); 8523 ctl_set_reservation_conflict(ctsio); 8524 ctl_done((union ctl_io *)ctsio); 8525 return (CTL_RETVAL_COMPLETE); 8526 } 8527 mtx_unlock(&lun->lun_lock); 8528 } 8529 8530 switch (cdb->action & SPRO_ACTION_MASK) { 8531 case SPRO_REGISTER: 8532 case SPRO_REG_IGNO: { 8533 8534#if 0 8535 printf("Registration received\n"); 8536#endif 8537 8538 /* 8539 * We don't support any of these options, as we report in 8540 * the read capabilities request (see 8541 * ctl_persistent_reserve_in(), above). 8542 */ 8543 if ((param->flags & SPR_SPEC_I_PT) 8544 || (param->flags & SPR_ALL_TG_PT) 8545 || (param->flags & SPR_APTPL)) { 8546 int bit_ptr; 8547 8548 if (param->flags & SPR_APTPL) 8549 bit_ptr = 0; 8550 else if (param->flags & SPR_ALL_TG_PT) 8551 bit_ptr = 2; 8552 else /* SPR_SPEC_I_PT */ 8553 bit_ptr = 3; 8554 8555 free(ctsio->kern_data_ptr, M_CTL); 8556 ctl_set_invalid_field(ctsio, 8557 /*sks_valid*/ 1, 8558 /*command*/ 0, 8559 /*field*/ 20, 8560 /*bit_valid*/ 1, 8561 /*bit*/ bit_ptr); 8562 ctl_done((union ctl_io *)ctsio); 8563 return (CTL_RETVAL_COMPLETE); 8564 } 8565 8566 mtx_lock(&lun->lun_lock); 8567 8568 /* 8569 * The initiator wants to clear the 8570 * key/unregister. 8571 */ 8572 if (sa_res_key == 0) { 8573 if ((res_key == 0 8574 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8575 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8576 && !lun->per_res[residx].registered)) { 8577 mtx_unlock(&lun->lun_lock); 8578 goto done; 8579 } 8580 8581 lun->per_res[residx].registered = 0; 8582 memset(&lun->per_res[residx].res_key, 8583 0, sizeof(lun->per_res[residx].res_key)); 8584 lun->pr_key_count--; 8585 8586 if (residx == lun->pr_res_idx) { 8587 lun->flags &= ~CTL_LUN_PR_RESERVED; 8588 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8589 8590 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8591 || lun->res_type == SPR_TYPE_EX_AC_RO) 8592 && lun->pr_key_count) { 8593 /* 8594 * If the reservation is a registrants 8595 * only type we need to generate a UA 8596 * for other registered inits. The 8597 * sense code should be RESERVATIONS 8598 * RELEASED 8599 */ 8600 8601 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8602 if (lun->per_res[ 8603 i+persis_offset].registered 8604 == 0) 8605 continue; 8606 lun->pending_ua[i] |= 8607 CTL_UA_RES_RELEASE; 8608 } 8609 } 8610 lun->res_type = 0; 8611 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8612 if (lun->pr_key_count==0) { 8613 lun->flags &= ~CTL_LUN_PR_RESERVED; 8614 lun->res_type = 0; 8615 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8616 } 8617 } 8618 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8619 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8620 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8621 persis_io.pr.pr_info.residx = residx; 8622 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8623 &persis_io, sizeof(persis_io), 0 )) > 8624 CTL_HA_STATUS_SUCCESS) { 8625 printf("CTL:Persis Out error returned from " 8626 "ctl_ha_msg_send %d\n", isc_retval); 8627 } 8628 } else /* sa_res_key != 0 */ { 8629 8630 /* 8631 * If we aren't registered currently then increment 8632 * the key count and set the registered flag. 8633 */ 8634 if (!lun->per_res[residx].registered) { 8635 lun->pr_key_count++; 8636 lun->per_res[residx].registered = 1; 8637 } 8638 8639 memcpy(&lun->per_res[residx].res_key, 8640 param->serv_act_res_key, 8641 ctl_min(sizeof(param->serv_act_res_key), 8642 sizeof(lun->per_res[residx].res_key))); 8643 8644 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8645 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8646 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8647 persis_io.pr.pr_info.residx = residx; 8648 memcpy(persis_io.pr.pr_info.sa_res_key, 8649 param->serv_act_res_key, 8650 sizeof(param->serv_act_res_key)); 8651 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8652 &persis_io, sizeof(persis_io), 0)) > 8653 CTL_HA_STATUS_SUCCESS) { 8654 printf("CTL:Persis Out error returned from " 8655 "ctl_ha_msg_send %d\n", isc_retval); 8656 } 8657 } 8658 lun->PRGeneration++; 8659 mtx_unlock(&lun->lun_lock); 8660 8661 break; 8662 } 8663 case SPRO_RESERVE: 8664#if 0 8665 printf("Reserve executed type %d\n", type); 8666#endif 8667 mtx_lock(&lun->lun_lock); 8668 if (lun->flags & CTL_LUN_PR_RESERVED) { 8669 /* 8670 * if this isn't the reservation holder and it's 8671 * not a "all registrants" type or if the type is 8672 * different then we have a conflict 8673 */ 8674 if ((lun->pr_res_idx != residx 8675 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8676 || lun->res_type != type) { 8677 mtx_unlock(&lun->lun_lock); 8678 free(ctsio->kern_data_ptr, M_CTL); 8679 ctl_set_reservation_conflict(ctsio); 8680 ctl_done((union ctl_io *)ctsio); 8681 return (CTL_RETVAL_COMPLETE); 8682 } 8683 mtx_unlock(&lun->lun_lock); 8684 } else /* create a reservation */ { 8685 /* 8686 * If it's not an "all registrants" type record 8687 * reservation holder 8688 */ 8689 if (type != SPR_TYPE_WR_EX_AR 8690 && type != SPR_TYPE_EX_AC_AR) 8691 lun->pr_res_idx = residx; /* Res holder */ 8692 else 8693 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8694 8695 lun->flags |= CTL_LUN_PR_RESERVED; 8696 lun->res_type = type; 8697 8698 mtx_unlock(&lun->lun_lock); 8699 8700 /* send msg to other side */ 8701 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8702 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8703 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8704 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8705 persis_io.pr.pr_info.res_type = type; 8706 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8707 &persis_io, sizeof(persis_io), 0)) > 8708 CTL_HA_STATUS_SUCCESS) { 8709 printf("CTL:Persis Out error returned from " 8710 "ctl_ha_msg_send %d\n", isc_retval); 8711 } 8712 } 8713 break; 8714 8715 case SPRO_RELEASE: 8716 mtx_lock(&lun->lun_lock); 8717 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8718 /* No reservation exists return good status */ 8719 mtx_unlock(&lun->lun_lock); 8720 goto done; 8721 } 8722 /* 8723 * Is this nexus a reservation holder? 8724 */ 8725 if (lun->pr_res_idx != residx 8726 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8727 /* 8728 * not a res holder return good status but 8729 * do nothing 8730 */ 8731 mtx_unlock(&lun->lun_lock); 8732 goto done; 8733 } 8734 8735 if (lun->res_type != type) { 8736 mtx_unlock(&lun->lun_lock); 8737 free(ctsio->kern_data_ptr, M_CTL); 8738 ctl_set_illegal_pr_release(ctsio); 8739 ctl_done((union ctl_io *)ctsio); 8740 return (CTL_RETVAL_COMPLETE); 8741 } 8742 8743 /* okay to release */ 8744 lun->flags &= ~CTL_LUN_PR_RESERVED; 8745 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8746 lun->res_type = 0; 8747 8748 /* 8749 * if this isn't an exclusive access 8750 * res generate UA for all other 8751 * registrants. 8752 */ 8753 if (type != SPR_TYPE_EX_AC 8754 && type != SPR_TYPE_WR_EX) { 8755 /* 8756 * temporarily unregister so we don't generate UA 8757 */ 8758 lun->per_res[residx].registered = 0; 8759 8760 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8761 if (lun->per_res[i+persis_offset].registered 8762 == 0) 8763 continue; 8764 lun->pending_ua[i] |= 8765 CTL_UA_RES_RELEASE; 8766 } 8767 8768 lun->per_res[residx].registered = 1; 8769 } 8770 mtx_unlock(&lun->lun_lock); 8771 /* Send msg to other side */ 8772 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8773 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8774 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8775 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8776 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8777 printf("CTL:Persis Out error returned from " 8778 "ctl_ha_msg_send %d\n", isc_retval); 8779 } 8780 break; 8781 8782 case SPRO_CLEAR: 8783 /* send msg to other side */ 8784 8785 mtx_lock(&lun->lun_lock); 8786 lun->flags &= ~CTL_LUN_PR_RESERVED; 8787 lun->res_type = 0; 8788 lun->pr_key_count = 0; 8789 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8790 8791 8792 memset(&lun->per_res[residx].res_key, 8793 0, sizeof(lun->per_res[residx].res_key)); 8794 lun->per_res[residx].registered = 0; 8795 8796 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8797 if (lun->per_res[i].registered) { 8798 if (!persis_offset && i < CTL_MAX_INITIATORS) 8799 lun->pending_ua[i] |= 8800 CTL_UA_RES_PREEMPT; 8801 else if (persis_offset && i >= persis_offset) 8802 lun->pending_ua[i-persis_offset] |= 8803 CTL_UA_RES_PREEMPT; 8804 8805 memset(&lun->per_res[i].res_key, 8806 0, sizeof(struct scsi_per_res_key)); 8807 lun->per_res[i].registered = 0; 8808 } 8809 lun->PRGeneration++; 8810 mtx_unlock(&lun->lun_lock); 8811 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8812 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8813 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8814 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8815 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8816 printf("CTL:Persis Out error returned from " 8817 "ctl_ha_msg_send %d\n", isc_retval); 8818 } 8819 break; 8820 8821 case SPRO_PREEMPT: { 8822 int nretval; 8823 8824 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8825 residx, ctsio, cdb, param); 8826 if (nretval != 0) 8827 return (CTL_RETVAL_COMPLETE); 8828 break; 8829 } 8830 default: 8831 panic("Invalid PR type %x", cdb->action); 8832 } 8833 8834done: 8835 free(ctsio->kern_data_ptr, M_CTL); 8836 ctl_set_success(ctsio); 8837 ctl_done((union ctl_io *)ctsio); 8838 8839 return (retval); 8840} 8841 8842/* 8843 * This routine is for handling a message from the other SC pertaining to 8844 * persistent reserve out. All the error checking will have been done 8845 * so only perorming the action need be done here to keep the two 8846 * in sync. 8847 */ 8848static void 8849ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8850{ 8851 struct ctl_lun *lun; 8852 struct ctl_softc *softc; 8853 int i; 8854 uint32_t targ_lun; 8855 8856 softc = control_softc; 8857 8858 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8859 lun = softc->ctl_luns[targ_lun]; 8860 mtx_lock(&lun->lun_lock); 8861 switch(msg->pr.pr_info.action) { 8862 case CTL_PR_REG_KEY: 8863 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8864 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8865 lun->pr_key_count++; 8866 } 8867 lun->PRGeneration++; 8868 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8869 msg->pr.pr_info.sa_res_key, 8870 sizeof(struct scsi_per_res_key)); 8871 break; 8872 8873 case CTL_PR_UNREG_KEY: 8874 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8875 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8876 0, sizeof(struct scsi_per_res_key)); 8877 lun->pr_key_count--; 8878 8879 /* XXX Need to see if the reservation has been released */ 8880 /* if so do we need to generate UA? */ 8881 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8882 lun->flags &= ~CTL_LUN_PR_RESERVED; 8883 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8884 8885 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8886 || lun->res_type == SPR_TYPE_EX_AC_RO) 8887 && lun->pr_key_count) { 8888 /* 8889 * If the reservation is a registrants 8890 * only type we need to generate a UA 8891 * for other registered inits. The 8892 * sense code should be RESERVATIONS 8893 * RELEASED 8894 */ 8895 8896 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8897 if (lun->per_res[i+ 8898 persis_offset].registered == 0) 8899 continue; 8900 8901 lun->pending_ua[i] |= 8902 CTL_UA_RES_RELEASE; 8903 } 8904 } 8905 lun->res_type = 0; 8906 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8907 if (lun->pr_key_count==0) { 8908 lun->flags &= ~CTL_LUN_PR_RESERVED; 8909 lun->res_type = 0; 8910 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8911 } 8912 } 8913 lun->PRGeneration++; 8914 break; 8915 8916 case CTL_PR_RESERVE: 8917 lun->flags |= CTL_LUN_PR_RESERVED; 8918 lun->res_type = msg->pr.pr_info.res_type; 8919 lun->pr_res_idx = msg->pr.pr_info.residx; 8920 8921 break; 8922 8923 case CTL_PR_RELEASE: 8924 /* 8925 * if this isn't an exclusive access res generate UA for all 8926 * other registrants. 8927 */ 8928 if (lun->res_type != SPR_TYPE_EX_AC 8929 && lun->res_type != SPR_TYPE_WR_EX) { 8930 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8931 if (lun->per_res[i+persis_offset].registered) 8932 lun->pending_ua[i] |= 8933 CTL_UA_RES_RELEASE; 8934 } 8935 8936 lun->flags &= ~CTL_LUN_PR_RESERVED; 8937 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8938 lun->res_type = 0; 8939 break; 8940 8941 case CTL_PR_PREEMPT: 8942 ctl_pro_preempt_other(lun, msg); 8943 break; 8944 case CTL_PR_CLEAR: 8945 lun->flags &= ~CTL_LUN_PR_RESERVED; 8946 lun->res_type = 0; 8947 lun->pr_key_count = 0; 8948 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8949 8950 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8951 if (lun->per_res[i].registered == 0) 8952 continue; 8953 if (!persis_offset 8954 && i < CTL_MAX_INITIATORS) 8955 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8956 else if (persis_offset 8957 && i >= persis_offset) 8958 lun->pending_ua[i-persis_offset] |= 8959 CTL_UA_RES_PREEMPT; 8960 memset(&lun->per_res[i].res_key, 0, 8961 sizeof(struct scsi_per_res_key)); 8962 lun->per_res[i].registered = 0; 8963 } 8964 lun->PRGeneration++; 8965 break; 8966 } 8967 8968 mtx_unlock(&lun->lun_lock); 8969} 8970 8971int 8972ctl_read_write(struct ctl_scsiio *ctsio) 8973{ 8974 struct ctl_lun *lun; 8975 struct ctl_lba_len_flags *lbalen; 8976 uint64_t lba; 8977 uint32_t num_blocks; 8978 int fua, dpo; 8979 int retval; 8980 int isread; 8981 8982 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8983 8984 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8985 8986 fua = 0; 8987 dpo = 0; 8988 8989 retval = CTL_RETVAL_COMPLETE; 8990 8991 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8992 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8993 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8994 uint32_t residx; 8995 8996 /* 8997 * XXX KDM need a lock here. 8998 */ 8999 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9000 if ((lun->res_type == SPR_TYPE_EX_AC 9001 && residx != lun->pr_res_idx) 9002 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9003 || lun->res_type == SPR_TYPE_EX_AC_AR) 9004 && !lun->per_res[residx].registered)) { 9005 ctl_set_reservation_conflict(ctsio); 9006 ctl_done((union ctl_io *)ctsio); 9007 return (CTL_RETVAL_COMPLETE); 9008 } 9009 } 9010 9011 switch (ctsio->cdb[0]) { 9012 case READ_6: 9013 case WRITE_6: { 9014 struct scsi_rw_6 *cdb; 9015 9016 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9017 9018 lba = scsi_3btoul(cdb->addr); 9019 /* only 5 bits are valid in the most significant address byte */ 9020 lba &= 0x1fffff; 9021 num_blocks = cdb->length; 9022 /* 9023 * This is correct according to SBC-2. 9024 */ 9025 if (num_blocks == 0) 9026 num_blocks = 256; 9027 break; 9028 } 9029 case READ_10: 9030 case WRITE_10: { 9031 struct scsi_rw_10 *cdb; 9032 9033 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9034 9035 if (cdb->byte2 & SRW10_FUA) 9036 fua = 1; 9037 if (cdb->byte2 & SRW10_DPO) 9038 dpo = 1; 9039 9040 lba = scsi_4btoul(cdb->addr); 9041 num_blocks = scsi_2btoul(cdb->length); 9042 break; 9043 } 9044 case WRITE_VERIFY_10: { 9045 struct scsi_write_verify_10 *cdb; 9046 9047 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9048 9049 /* 9050 * XXX KDM we should do actual write verify support at some 9051 * point. This is obviously fake, we're just translating 9052 * things to a write. So we don't even bother checking the 9053 * BYTCHK field, since we don't do any verification. If 9054 * the user asks for it, we'll just pretend we did it. 9055 */ 9056 if (cdb->byte2 & SWV_DPO) 9057 dpo = 1; 9058 9059 lba = scsi_4btoul(cdb->addr); 9060 num_blocks = scsi_2btoul(cdb->length); 9061 break; 9062 } 9063 case READ_12: 9064 case WRITE_12: { 9065 struct scsi_rw_12 *cdb; 9066 9067 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9068 9069 if (cdb->byte2 & SRW12_FUA) 9070 fua = 1; 9071 if (cdb->byte2 & SRW12_DPO) 9072 dpo = 1; 9073 lba = scsi_4btoul(cdb->addr); 9074 num_blocks = scsi_4btoul(cdb->length); 9075 break; 9076 } 9077 case WRITE_VERIFY_12: { 9078 struct scsi_write_verify_12 *cdb; 9079 9080 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9081 9082 if (cdb->byte2 & SWV_DPO) 9083 dpo = 1; 9084 9085 lba = scsi_4btoul(cdb->addr); 9086 num_blocks = scsi_4btoul(cdb->length); 9087 9088 break; 9089 } 9090 case READ_16: 9091 case WRITE_16: { 9092 struct scsi_rw_16 *cdb; 9093 9094 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9095 9096 if (cdb->byte2 & SRW12_FUA) 9097 fua = 1; 9098 if (cdb->byte2 & SRW12_DPO) 9099 dpo = 1; 9100 9101 lba = scsi_8btou64(cdb->addr); 9102 num_blocks = scsi_4btoul(cdb->length); 9103 break; 9104 } 9105 case WRITE_VERIFY_16: { 9106 struct scsi_write_verify_16 *cdb; 9107 9108 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9109 9110 if (cdb->byte2 & SWV_DPO) 9111 dpo = 1; 9112 9113 lba = scsi_8btou64(cdb->addr); 9114 num_blocks = scsi_4btoul(cdb->length); 9115 break; 9116 } 9117 default: 9118 /* 9119 * We got a command we don't support. This shouldn't 9120 * happen, commands should be filtered out above us. 9121 */ 9122 ctl_set_invalid_opcode(ctsio); 9123 ctl_done((union ctl_io *)ctsio); 9124 9125 return (CTL_RETVAL_COMPLETE); 9126 break; /* NOTREACHED */ 9127 } 9128 9129 /* 9130 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9131 * interesting for us, but if RAIDCore is in write-back mode, 9132 * getting it to do write-through for a particular transaction may 9133 * not be possible. 9134 */ 9135 9136 /* 9137 * The first check is to make sure we're in bounds, the second 9138 * check is to catch wrap-around problems. If the lba + num blocks 9139 * is less than the lba, then we've wrapped around and the block 9140 * range is invalid anyway. 9141 */ 9142 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9143 || ((lba + num_blocks) < lba)) { 9144 ctl_set_lba_out_of_range(ctsio); 9145 ctl_done((union ctl_io *)ctsio); 9146 return (CTL_RETVAL_COMPLETE); 9147 } 9148 9149 /* 9150 * According to SBC-3, a transfer length of 0 is not an error. 9151 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9152 * translates to 256 blocks for those commands. 9153 */ 9154 if (num_blocks == 0) { 9155 ctl_set_success(ctsio); 9156 ctl_done((union ctl_io *)ctsio); 9157 return (CTL_RETVAL_COMPLETE); 9158 } 9159 9160 lbalen = (struct ctl_lba_len_flags *) 9161 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9162 lbalen->lba = lba; 9163 lbalen->len = num_blocks; 9164 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 9165 9166 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9167 ctsio->kern_rel_offset = 0; 9168 9169 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9170 9171 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9172 9173 return (retval); 9174} 9175 9176static int 9177ctl_cnw_cont(union ctl_io *io) 9178{ 9179 struct ctl_scsiio *ctsio; 9180 struct ctl_lun *lun; 9181 struct ctl_lba_len_flags *lbalen; 9182 int retval; 9183 9184 ctsio = &io->scsiio; 9185 ctsio->io_hdr.status = CTL_STATUS_NONE; 9186 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9187 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9188 lbalen = (struct ctl_lba_len_flags *) 9189 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9190 lbalen->flags = CTL_LLF_WRITE; 9191 9192 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9193 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9194 return (retval); 9195} 9196 9197int 9198ctl_cnw(struct ctl_scsiio *ctsio) 9199{ 9200 struct ctl_lun *lun; 9201 struct ctl_lba_len_flags *lbalen; 9202 uint64_t lba; 9203 uint32_t num_blocks; 9204 int fua, dpo; 9205 int retval; 9206 9207 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9208 9209 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9210 9211 fua = 0; 9212 dpo = 0; 9213 9214 retval = CTL_RETVAL_COMPLETE; 9215 9216 switch (ctsio->cdb[0]) { 9217 case COMPARE_AND_WRITE: { 9218 struct scsi_compare_and_write *cdb; 9219 9220 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9221 9222 if (cdb->byte2 & SRW10_FUA) 9223 fua = 1; 9224 if (cdb->byte2 & SRW10_DPO) 9225 dpo = 1; 9226 lba = scsi_8btou64(cdb->addr); 9227 num_blocks = cdb->length; 9228 break; 9229 } 9230 default: 9231 /* 9232 * We got a command we don't support. This shouldn't 9233 * happen, commands should be filtered out above us. 9234 */ 9235 ctl_set_invalid_opcode(ctsio); 9236 ctl_done((union ctl_io *)ctsio); 9237 9238 return (CTL_RETVAL_COMPLETE); 9239 break; /* NOTREACHED */ 9240 } 9241 9242 /* 9243 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9244 * interesting for us, but if RAIDCore is in write-back mode, 9245 * getting it to do write-through for a particular transaction may 9246 * not be possible. 9247 */ 9248 9249 /* 9250 * The first check is to make sure we're in bounds, the second 9251 * check is to catch wrap-around problems. If the lba + num blocks 9252 * is less than the lba, then we've wrapped around and the block 9253 * range is invalid anyway. 9254 */ 9255 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9256 || ((lba + num_blocks) < lba)) { 9257 ctl_set_lba_out_of_range(ctsio); 9258 ctl_done((union ctl_io *)ctsio); 9259 return (CTL_RETVAL_COMPLETE); 9260 } 9261 9262 /* 9263 * According to SBC-3, a transfer length of 0 is not an error. 9264 */ 9265 if (num_blocks == 0) { 9266 ctl_set_success(ctsio); 9267 ctl_done((union ctl_io *)ctsio); 9268 return (CTL_RETVAL_COMPLETE); 9269 } 9270 9271 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9272 ctsio->kern_rel_offset = 0; 9273 9274 /* 9275 * Set the IO_CONT flag, so that if this I/O gets passed to 9276 * ctl_data_submit_done(), it'll get passed back to 9277 * ctl_ctl_cnw_cont() for further processing. 9278 */ 9279 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9280 ctsio->io_cont = ctl_cnw_cont; 9281 9282 lbalen = (struct ctl_lba_len_flags *) 9283 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9284 lbalen->lba = lba; 9285 lbalen->len = num_blocks; 9286 lbalen->flags = CTL_LLF_COMPARE; 9287 9288 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9289 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9290 return (retval); 9291} 9292 9293int 9294ctl_verify(struct ctl_scsiio *ctsio) 9295{ 9296 struct ctl_lun *lun; 9297 struct ctl_lba_len_flags *lbalen; 9298 uint64_t lba; 9299 uint32_t num_blocks; 9300 int bytchk, dpo; 9301 int retval; 9302 9303 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9304 9305 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9306 9307 bytchk = 0; 9308 dpo = 0; 9309 retval = CTL_RETVAL_COMPLETE; 9310 9311 switch (ctsio->cdb[0]) { 9312 case VERIFY_10: { 9313 struct scsi_verify_10 *cdb; 9314 9315 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9316 if (cdb->byte2 & SVFY_BYTCHK) 9317 bytchk = 1; 9318 if (cdb->byte2 & SVFY_DPO) 9319 dpo = 1; 9320 lba = scsi_4btoul(cdb->addr); 9321 num_blocks = scsi_2btoul(cdb->length); 9322 break; 9323 } 9324 case VERIFY_12: { 9325 struct scsi_verify_12 *cdb; 9326 9327 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9328 if (cdb->byte2 & SVFY_BYTCHK) 9329 bytchk = 1; 9330 if (cdb->byte2 & SVFY_DPO) 9331 dpo = 1; 9332 lba = scsi_4btoul(cdb->addr); 9333 num_blocks = scsi_4btoul(cdb->length); 9334 break; 9335 } 9336 case VERIFY_16: { 9337 struct scsi_rw_16 *cdb; 9338 9339 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9340 if (cdb->byte2 & SVFY_BYTCHK) 9341 bytchk = 1; 9342 if (cdb->byte2 & SVFY_DPO) 9343 dpo = 1; 9344 lba = scsi_8btou64(cdb->addr); 9345 num_blocks = scsi_4btoul(cdb->length); 9346 break; 9347 } 9348 default: 9349 /* 9350 * We got a command we don't support. This shouldn't 9351 * happen, commands should be filtered out above us. 9352 */ 9353 ctl_set_invalid_opcode(ctsio); 9354 ctl_done((union ctl_io *)ctsio); 9355 return (CTL_RETVAL_COMPLETE); 9356 } 9357 9358 /* 9359 * The first check is to make sure we're in bounds, the second 9360 * check is to catch wrap-around problems. If the lba + num blocks 9361 * is less than the lba, then we've wrapped around and the block 9362 * range is invalid anyway. 9363 */ 9364 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9365 || ((lba + num_blocks) < lba)) { 9366 ctl_set_lba_out_of_range(ctsio); 9367 ctl_done((union ctl_io *)ctsio); 9368 return (CTL_RETVAL_COMPLETE); 9369 } 9370 9371 /* 9372 * According to SBC-3, a transfer length of 0 is not an error. 9373 */ 9374 if (num_blocks == 0) { 9375 ctl_set_success(ctsio); 9376 ctl_done((union ctl_io *)ctsio); 9377 return (CTL_RETVAL_COMPLETE); 9378 } 9379 9380 lbalen = (struct ctl_lba_len_flags *) 9381 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9382 lbalen->lba = lba; 9383 lbalen->len = num_blocks; 9384 if (bytchk) { 9385 lbalen->flags = CTL_LLF_COMPARE; 9386 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9387 } else { 9388 lbalen->flags = CTL_LLF_VERIFY; 9389 ctsio->kern_total_len = 0; 9390 } 9391 ctsio->kern_rel_offset = 0; 9392 9393 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9394 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9395 return (retval); 9396} 9397 9398int 9399ctl_report_luns(struct ctl_scsiio *ctsio) 9400{ 9401 struct scsi_report_luns *cdb; 9402 struct scsi_report_luns_data *lun_data; 9403 struct ctl_lun *lun, *request_lun; 9404 int num_luns, retval; 9405 uint32_t alloc_len, lun_datalen; 9406 int num_filled, well_known; 9407 uint32_t initidx, targ_lun_id, lun_id; 9408 9409 retval = CTL_RETVAL_COMPLETE; 9410 well_known = 0; 9411 9412 cdb = (struct scsi_report_luns *)ctsio->cdb; 9413 9414 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9415 9416 mtx_lock(&control_softc->ctl_lock); 9417 num_luns = control_softc->num_luns; 9418 mtx_unlock(&control_softc->ctl_lock); 9419 9420 switch (cdb->select_report) { 9421 case RPL_REPORT_DEFAULT: 9422 case RPL_REPORT_ALL: 9423 break; 9424 case RPL_REPORT_WELLKNOWN: 9425 well_known = 1; 9426 num_luns = 0; 9427 break; 9428 default: 9429 ctl_set_invalid_field(ctsio, 9430 /*sks_valid*/ 1, 9431 /*command*/ 1, 9432 /*field*/ 2, 9433 /*bit_valid*/ 0, 9434 /*bit*/ 0); 9435 ctl_done((union ctl_io *)ctsio); 9436 return (retval); 9437 break; /* NOTREACHED */ 9438 } 9439 9440 alloc_len = scsi_4btoul(cdb->length); 9441 /* 9442 * The initiator has to allocate at least 16 bytes for this request, 9443 * so he can at least get the header and the first LUN. Otherwise 9444 * we reject the request (per SPC-3 rev 14, section 6.21). 9445 */ 9446 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9447 sizeof(struct scsi_report_luns_lundata))) { 9448 ctl_set_invalid_field(ctsio, 9449 /*sks_valid*/ 1, 9450 /*command*/ 1, 9451 /*field*/ 6, 9452 /*bit_valid*/ 0, 9453 /*bit*/ 0); 9454 ctl_done((union ctl_io *)ctsio); 9455 return (retval); 9456 } 9457 9458 request_lun = (struct ctl_lun *) 9459 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9460 9461 lun_datalen = sizeof(*lun_data) + 9462 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9463 9464 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9465 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9466 ctsio->kern_sg_entries = 0; 9467 9468 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9469 9470 mtx_lock(&control_softc->ctl_lock); 9471 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9472 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9473 if (lun_id >= CTL_MAX_LUNS) 9474 continue; 9475 lun = control_softc->ctl_luns[lun_id]; 9476 if (lun == NULL) 9477 continue; 9478 9479 if (targ_lun_id <= 0xff) { 9480 /* 9481 * Peripheral addressing method, bus number 0. 9482 */ 9483 lun_data->luns[num_filled].lundata[0] = 9484 RPL_LUNDATA_ATYP_PERIPH; 9485 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9486 num_filled++; 9487 } else if (targ_lun_id <= 0x3fff) { 9488 /* 9489 * Flat addressing method. 9490 */ 9491 lun_data->luns[num_filled].lundata[0] = 9492 RPL_LUNDATA_ATYP_FLAT | 9493 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9494#ifdef OLDCTLHEADERS 9495 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9496 (targ_lun_id & SRLD_BUS_LUN_MASK); 9497#endif 9498 lun_data->luns[num_filled].lundata[1] = 9499#ifdef OLDCTLHEADERS 9500 targ_lun_id >> SRLD_BUS_LUN_BITS; 9501#endif 9502 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9503 num_filled++; 9504 } else { 9505 printf("ctl_report_luns: bogus LUN number %jd, " 9506 "skipping\n", (intmax_t)targ_lun_id); 9507 } 9508 /* 9509 * According to SPC-3, rev 14 section 6.21: 9510 * 9511 * "The execution of a REPORT LUNS command to any valid and 9512 * installed logical unit shall clear the REPORTED LUNS DATA 9513 * HAS CHANGED unit attention condition for all logical 9514 * units of that target with respect to the requesting 9515 * initiator. A valid and installed logical unit is one 9516 * having a PERIPHERAL QUALIFIER of 000b in the standard 9517 * INQUIRY data (see 6.4.2)." 9518 * 9519 * If request_lun is NULL, the LUN this report luns command 9520 * was issued to is either disabled or doesn't exist. In that 9521 * case, we shouldn't clear any pending lun change unit 9522 * attention. 9523 */ 9524 if (request_lun != NULL) { 9525 mtx_lock(&lun->lun_lock); 9526 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9527 mtx_unlock(&lun->lun_lock); 9528 } 9529 } 9530 mtx_unlock(&control_softc->ctl_lock); 9531 9532 /* 9533 * It's quite possible that we've returned fewer LUNs than we allocated 9534 * space for. Trim it. 9535 */ 9536 lun_datalen = sizeof(*lun_data) + 9537 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9538 9539 if (lun_datalen < alloc_len) { 9540 ctsio->residual = alloc_len - lun_datalen; 9541 ctsio->kern_data_len = lun_datalen; 9542 ctsio->kern_total_len = lun_datalen; 9543 } else { 9544 ctsio->residual = 0; 9545 ctsio->kern_data_len = alloc_len; 9546 ctsio->kern_total_len = alloc_len; 9547 } 9548 ctsio->kern_data_resid = 0; 9549 ctsio->kern_rel_offset = 0; 9550 ctsio->kern_sg_entries = 0; 9551 9552 /* 9553 * We set this to the actual data length, regardless of how much 9554 * space we actually have to return results. If the user looks at 9555 * this value, he'll know whether or not he allocated enough space 9556 * and reissue the command if necessary. We don't support well 9557 * known logical units, so if the user asks for that, return none. 9558 */ 9559 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9560 9561 /* 9562 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9563 * this request. 9564 */ 9565 ctsio->scsi_status = SCSI_STATUS_OK; 9566 9567 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9568 ctsio->be_move_done = ctl_config_move_done; 9569 ctl_datamove((union ctl_io *)ctsio); 9570 9571 return (retval); 9572} 9573 9574int 9575ctl_request_sense(struct ctl_scsiio *ctsio) 9576{ 9577 struct scsi_request_sense *cdb; 9578 struct scsi_sense_data *sense_ptr; 9579 struct ctl_lun *lun; 9580 uint32_t initidx; 9581 int have_error; 9582 scsi_sense_data_type sense_format; 9583 9584 cdb = (struct scsi_request_sense *)ctsio->cdb; 9585 9586 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9587 9588 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9589 9590 /* 9591 * Determine which sense format the user wants. 9592 */ 9593 if (cdb->byte2 & SRS_DESC) 9594 sense_format = SSD_TYPE_DESC; 9595 else 9596 sense_format = SSD_TYPE_FIXED; 9597 9598 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9599 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9600 ctsio->kern_sg_entries = 0; 9601 9602 /* 9603 * struct scsi_sense_data, which is currently set to 256 bytes, is 9604 * larger than the largest allowed value for the length field in the 9605 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9606 */ 9607 ctsio->residual = 0; 9608 ctsio->kern_data_len = cdb->length; 9609 ctsio->kern_total_len = cdb->length; 9610 9611 ctsio->kern_data_resid = 0; 9612 ctsio->kern_rel_offset = 0; 9613 ctsio->kern_sg_entries = 0; 9614 9615 /* 9616 * If we don't have a LUN, we don't have any pending sense. 9617 */ 9618 if (lun == NULL) 9619 goto no_sense; 9620 9621 have_error = 0; 9622 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9623 /* 9624 * Check for pending sense, and then for pending unit attentions. 9625 * Pending sense gets returned first, then pending unit attentions. 9626 */ 9627 mtx_lock(&lun->lun_lock); 9628#ifdef CTL_WITH_CA 9629 if (ctl_is_set(lun->have_ca, initidx)) { 9630 scsi_sense_data_type stored_format; 9631 9632 /* 9633 * Check to see which sense format was used for the stored 9634 * sense data. 9635 */ 9636 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9637 9638 /* 9639 * If the user requested a different sense format than the 9640 * one we stored, then we need to convert it to the other 9641 * format. If we're going from descriptor to fixed format 9642 * sense data, we may lose things in translation, depending 9643 * on what options were used. 9644 * 9645 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9646 * for some reason we'll just copy it out as-is. 9647 */ 9648 if ((stored_format == SSD_TYPE_FIXED) 9649 && (sense_format == SSD_TYPE_DESC)) 9650 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9651 &lun->pending_sense[initidx], 9652 (struct scsi_sense_data_desc *)sense_ptr); 9653 else if ((stored_format == SSD_TYPE_DESC) 9654 && (sense_format == SSD_TYPE_FIXED)) 9655 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9656 &lun->pending_sense[initidx], 9657 (struct scsi_sense_data_fixed *)sense_ptr); 9658 else 9659 memcpy(sense_ptr, &lun->pending_sense[initidx], 9660 ctl_min(sizeof(*sense_ptr), 9661 sizeof(lun->pending_sense[initidx]))); 9662 9663 ctl_clear_mask(lun->have_ca, initidx); 9664 have_error = 1; 9665 } else 9666#endif 9667 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9668 ctl_ua_type ua_type; 9669 9670 ua_type = ctl_build_ua(lun->pending_ua[initidx], 9671 sense_ptr, sense_format); 9672 if (ua_type != CTL_UA_NONE) { 9673 have_error = 1; 9674 /* We're reporting this UA, so clear it */ 9675 lun->pending_ua[initidx] &= ~ua_type; 9676 } 9677 } 9678 mtx_unlock(&lun->lun_lock); 9679 9680 /* 9681 * We already have a pending error, return it. 9682 */ 9683 if (have_error != 0) { 9684 /* 9685 * We report the SCSI status as OK, since the status of the 9686 * request sense command itself is OK. 9687 */ 9688 ctsio->scsi_status = SCSI_STATUS_OK; 9689 9690 /* 9691 * We report 0 for the sense length, because we aren't doing 9692 * autosense in this case. We're reporting sense as 9693 * parameter data. 9694 */ 9695 ctsio->sense_len = 0; 9696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9697 ctsio->be_move_done = ctl_config_move_done; 9698 ctl_datamove((union ctl_io *)ctsio); 9699 9700 return (CTL_RETVAL_COMPLETE); 9701 } 9702 9703no_sense: 9704 9705 /* 9706 * No sense information to report, so we report that everything is 9707 * okay. 9708 */ 9709 ctl_set_sense_data(sense_ptr, 9710 lun, 9711 sense_format, 9712 /*current_error*/ 1, 9713 /*sense_key*/ SSD_KEY_NO_SENSE, 9714 /*asc*/ 0x00, 9715 /*ascq*/ 0x00, 9716 SSD_ELEM_NONE); 9717 9718 ctsio->scsi_status = SCSI_STATUS_OK; 9719 9720 /* 9721 * We report 0 for the sense length, because we aren't doing 9722 * autosense in this case. We're reporting sense as parameter data. 9723 */ 9724 ctsio->sense_len = 0; 9725 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9726 ctsio->be_move_done = ctl_config_move_done; 9727 ctl_datamove((union ctl_io *)ctsio); 9728 9729 return (CTL_RETVAL_COMPLETE); 9730} 9731 9732int 9733ctl_tur(struct ctl_scsiio *ctsio) 9734{ 9735 struct ctl_lun *lun; 9736 9737 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9738 9739 CTL_DEBUG_PRINT(("ctl_tur\n")); 9740 9741 if (lun == NULL) 9742 return (EINVAL); 9743 9744 ctsio->scsi_status = SCSI_STATUS_OK; 9745 ctsio->io_hdr.status = CTL_SUCCESS; 9746 9747 ctl_done((union ctl_io *)ctsio); 9748 9749 return (CTL_RETVAL_COMPLETE); 9750} 9751 9752#ifdef notyet 9753static int 9754ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9755{ 9756 9757} 9758#endif 9759 9760static int 9761ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9762{ 9763 struct scsi_vpd_supported_pages *pages; 9764 int sup_page_size; 9765 struct ctl_lun *lun; 9766 9767 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9768 9769 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9770 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9771 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9772 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9773 ctsio->kern_sg_entries = 0; 9774 9775 if (sup_page_size < alloc_len) { 9776 ctsio->residual = alloc_len - sup_page_size; 9777 ctsio->kern_data_len = sup_page_size; 9778 ctsio->kern_total_len = sup_page_size; 9779 } else { 9780 ctsio->residual = 0; 9781 ctsio->kern_data_len = alloc_len; 9782 ctsio->kern_total_len = alloc_len; 9783 } 9784 ctsio->kern_data_resid = 0; 9785 ctsio->kern_rel_offset = 0; 9786 ctsio->kern_sg_entries = 0; 9787 9788 /* 9789 * The control device is always connected. The disk device, on the 9790 * other hand, may not be online all the time. Need to change this 9791 * to figure out whether the disk device is actually online or not. 9792 */ 9793 if (lun != NULL) 9794 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9795 lun->be_lun->lun_type; 9796 else 9797 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9798 9799 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9800 /* Supported VPD pages */ 9801 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9802 /* Serial Number */ 9803 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9804 /* Device Identification */ 9805 pages->page_list[2] = SVPD_DEVICE_ID; 9806 /* SCSI Ports */ 9807 pages->page_list[3] = SVPD_SCSI_PORTS; 9808 /* Third-party Copy */ 9809 pages->page_list[4] = SVPD_SCSI_TPC; 9810 /* Block limits */ 9811 pages->page_list[5] = SVPD_BLOCK_LIMITS;
| 7257 7258 ctsio->scsi_status = SCSI_STATUS_OK; 7259 7260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7261 ctsio->be_move_done = ctl_config_move_done; 7262 ctl_datamove((union ctl_io *)ctsio); 7263 7264 return (CTL_RETVAL_COMPLETE); 7265} 7266 7267int 7268ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7269{ 7270 struct scsi_maintenance_in *cdb; 7271 int retval; 7272 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7273 int num_target_port_groups, num_target_ports, single; 7274 struct ctl_lun *lun; 7275 struct ctl_softc *softc; 7276 struct ctl_port *port; 7277 struct scsi_target_group_data *rtg_ptr; 7278 struct scsi_target_group_data_extended *rtg_ext_ptr; 7279 struct scsi_target_port_group_descriptor *tpg_desc; 7280 7281 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7282 7283 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7284 softc = control_softc; 7285 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7286 7287 retval = CTL_RETVAL_COMPLETE; 7288 7289 switch (cdb->byte2 & STG_PDF_MASK) { 7290 case STG_PDF_LENGTH: 7291 ext = 0; 7292 break; 7293 case STG_PDF_EXTENDED: 7294 ext = 1; 7295 break; 7296 default: 7297 ctl_set_invalid_field(/*ctsio*/ ctsio, 7298 /*sks_valid*/ 1, 7299 /*command*/ 1, 7300 /*field*/ 2, 7301 /*bit_valid*/ 1, 7302 /*bit*/ 5); 7303 ctl_done((union ctl_io *)ctsio); 7304 return(retval); 7305 } 7306 7307 single = ctl_is_single; 7308 if (single) 7309 num_target_port_groups = 1; 7310 else 7311 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7312 num_target_ports = 0; 7313 mtx_lock(&softc->ctl_lock); 7314 STAILQ_FOREACH(port, &softc->port_list, links) { 7315 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7316 continue; 7317 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7318 continue; 7319 num_target_ports++; 7320 } 7321 mtx_unlock(&softc->ctl_lock); 7322 7323 if (ext) 7324 total_len = sizeof(struct scsi_target_group_data_extended); 7325 else 7326 total_len = sizeof(struct scsi_target_group_data); 7327 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7328 num_target_port_groups + 7329 sizeof(struct scsi_target_port_descriptor) * 7330 num_target_ports * num_target_port_groups; 7331 7332 alloc_len = scsi_4btoul(cdb->length); 7333 7334 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7335 7336 ctsio->kern_sg_entries = 0; 7337 7338 if (total_len < alloc_len) { 7339 ctsio->residual = alloc_len - total_len; 7340 ctsio->kern_data_len = total_len; 7341 ctsio->kern_total_len = total_len; 7342 } else { 7343 ctsio->residual = 0; 7344 ctsio->kern_data_len = alloc_len; 7345 ctsio->kern_total_len = alloc_len; 7346 } 7347 ctsio->kern_data_resid = 0; 7348 ctsio->kern_rel_offset = 0; 7349 7350 if (ext) { 7351 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7352 ctsio->kern_data_ptr; 7353 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7354 rtg_ext_ptr->format_type = 0x10; 7355 rtg_ext_ptr->implicit_transition_time = 0; 7356 tpg_desc = &rtg_ext_ptr->groups[0]; 7357 } else { 7358 rtg_ptr = (struct scsi_target_group_data *) 7359 ctsio->kern_data_ptr; 7360 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7361 tpg_desc = &rtg_ptr->groups[0]; 7362 } 7363 7364 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7365 mtx_lock(&softc->ctl_lock); 7366 for (g = 0; g < num_target_port_groups; g++) { 7367 if (g == pg) 7368 tpg_desc->pref_state = TPG_PRIMARY | 7369 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7370 else 7371 tpg_desc->pref_state = 7372 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7373 tpg_desc->support = TPG_AO_SUP; 7374 if (!single) 7375 tpg_desc->support |= TPG_AN_SUP; 7376 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7377 tpg_desc->status = TPG_IMPLICIT; 7378 pc = 0; 7379 STAILQ_FOREACH(port, &softc->port_list, links) { 7380 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7381 continue; 7382 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7383 CTL_MAX_LUNS) 7384 continue; 7385 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7386 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7387 relative_target_port_identifier); 7388 pc++; 7389 } 7390 tpg_desc->target_port_count = pc; 7391 tpg_desc = (struct scsi_target_port_group_descriptor *) 7392 &tpg_desc->descriptors[pc]; 7393 } 7394 mtx_unlock(&softc->ctl_lock); 7395 7396 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7397 ctsio->be_move_done = ctl_config_move_done; 7398 7399 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7400 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7401 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7402 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7403 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7404 7405 ctl_datamove((union ctl_io *)ctsio); 7406 return(retval); 7407} 7408 7409int 7410ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7411{ 7412 struct ctl_lun *lun; 7413 struct scsi_report_supported_opcodes *cdb; 7414 const struct ctl_cmd_entry *entry, *sentry; 7415 struct scsi_report_supported_opcodes_all *all; 7416 struct scsi_report_supported_opcodes_descr *descr; 7417 struct scsi_report_supported_opcodes_one *one; 7418 int retval; 7419 int alloc_len, total_len; 7420 int opcode, service_action, i, j, num; 7421 7422 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7423 7424 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7425 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7426 7427 retval = CTL_RETVAL_COMPLETE; 7428 7429 opcode = cdb->requested_opcode; 7430 service_action = scsi_2btoul(cdb->requested_service_action); 7431 switch (cdb->options & RSO_OPTIONS_MASK) { 7432 case RSO_OPTIONS_ALL: 7433 num = 0; 7434 for (i = 0; i < 256; i++) { 7435 entry = &ctl_cmd_table[i]; 7436 if (entry->flags & CTL_CMD_FLAG_SA5) { 7437 for (j = 0; j < 32; j++) { 7438 sentry = &((const struct ctl_cmd_entry *) 7439 entry->execute)[j]; 7440 if (ctl_cmd_applicable( 7441 lun->be_lun->lun_type, sentry)) 7442 num++; 7443 } 7444 } else { 7445 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7446 entry)) 7447 num++; 7448 } 7449 } 7450 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7451 num * sizeof(struct scsi_report_supported_opcodes_descr); 7452 break; 7453 case RSO_OPTIONS_OC: 7454 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7455 ctl_set_invalid_field(/*ctsio*/ ctsio, 7456 /*sks_valid*/ 1, 7457 /*command*/ 1, 7458 /*field*/ 2, 7459 /*bit_valid*/ 1, 7460 /*bit*/ 2); 7461 ctl_done((union ctl_io *)ctsio); 7462 return (CTL_RETVAL_COMPLETE); 7463 } 7464 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7465 break; 7466 case RSO_OPTIONS_OC_SA: 7467 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7468 service_action >= 32) { 7469 ctl_set_invalid_field(/*ctsio*/ ctsio, 7470 /*sks_valid*/ 1, 7471 /*command*/ 1, 7472 /*field*/ 2, 7473 /*bit_valid*/ 1, 7474 /*bit*/ 2); 7475 ctl_done((union ctl_io *)ctsio); 7476 return (CTL_RETVAL_COMPLETE); 7477 } 7478 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7479 break; 7480 default: 7481 ctl_set_invalid_field(/*ctsio*/ ctsio, 7482 /*sks_valid*/ 1, 7483 /*command*/ 1, 7484 /*field*/ 2, 7485 /*bit_valid*/ 1, 7486 /*bit*/ 2); 7487 ctl_done((union ctl_io *)ctsio); 7488 return (CTL_RETVAL_COMPLETE); 7489 } 7490 7491 alloc_len = scsi_4btoul(cdb->length); 7492 7493 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7494 7495 ctsio->kern_sg_entries = 0; 7496 7497 if (total_len < alloc_len) { 7498 ctsio->residual = alloc_len - total_len; 7499 ctsio->kern_data_len = total_len; 7500 ctsio->kern_total_len = total_len; 7501 } else { 7502 ctsio->residual = 0; 7503 ctsio->kern_data_len = alloc_len; 7504 ctsio->kern_total_len = alloc_len; 7505 } 7506 ctsio->kern_data_resid = 0; 7507 ctsio->kern_rel_offset = 0; 7508 7509 switch (cdb->options & RSO_OPTIONS_MASK) { 7510 case RSO_OPTIONS_ALL: 7511 all = (struct scsi_report_supported_opcodes_all *) 7512 ctsio->kern_data_ptr; 7513 num = 0; 7514 for (i = 0; i < 256; i++) { 7515 entry = &ctl_cmd_table[i]; 7516 if (entry->flags & CTL_CMD_FLAG_SA5) { 7517 for (j = 0; j < 32; j++) { 7518 sentry = &((const struct ctl_cmd_entry *) 7519 entry->execute)[j]; 7520 if (!ctl_cmd_applicable( 7521 lun->be_lun->lun_type, sentry)) 7522 continue; 7523 descr = &all->descr[num++]; 7524 descr->opcode = i; 7525 scsi_ulto2b(j, descr->service_action); 7526 descr->flags = RSO_SERVACTV; 7527 scsi_ulto2b(sentry->length, 7528 descr->cdb_length); 7529 } 7530 } else { 7531 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7532 entry)) 7533 continue; 7534 descr = &all->descr[num++]; 7535 descr->opcode = i; 7536 scsi_ulto2b(0, descr->service_action); 7537 descr->flags = 0; 7538 scsi_ulto2b(entry->length, descr->cdb_length); 7539 } 7540 } 7541 scsi_ulto4b( 7542 num * sizeof(struct scsi_report_supported_opcodes_descr), 7543 all->length); 7544 break; 7545 case RSO_OPTIONS_OC: 7546 one = (struct scsi_report_supported_opcodes_one *) 7547 ctsio->kern_data_ptr; 7548 entry = &ctl_cmd_table[opcode]; 7549 goto fill_one; 7550 case RSO_OPTIONS_OC_SA: 7551 one = (struct scsi_report_supported_opcodes_one *) 7552 ctsio->kern_data_ptr; 7553 entry = &ctl_cmd_table[opcode]; 7554 entry = &((const struct ctl_cmd_entry *) 7555 entry->execute)[service_action]; 7556fill_one: 7557 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7558 one->support = 3; 7559 scsi_ulto2b(entry->length, one->cdb_length); 7560 one->cdb_usage[0] = opcode; 7561 memcpy(&one->cdb_usage[1], entry->usage, 7562 entry->length - 1); 7563 } else 7564 one->support = 1; 7565 break; 7566 } 7567 7568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7569 ctsio->be_move_done = ctl_config_move_done; 7570 7571 ctl_datamove((union ctl_io *)ctsio); 7572 return(retval); 7573} 7574 7575int 7576ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7577{ 7578 struct ctl_lun *lun; 7579 struct scsi_report_supported_tmf *cdb; 7580 struct scsi_report_supported_tmf_data *data; 7581 int retval; 7582 int alloc_len, total_len; 7583 7584 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7585 7586 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7587 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7588 7589 retval = CTL_RETVAL_COMPLETE; 7590 7591 total_len = sizeof(struct scsi_report_supported_tmf_data); 7592 alloc_len = scsi_4btoul(cdb->length); 7593 7594 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7595 7596 ctsio->kern_sg_entries = 0; 7597 7598 if (total_len < alloc_len) { 7599 ctsio->residual = alloc_len - total_len; 7600 ctsio->kern_data_len = total_len; 7601 ctsio->kern_total_len = total_len; 7602 } else { 7603 ctsio->residual = 0; 7604 ctsio->kern_data_len = alloc_len; 7605 ctsio->kern_total_len = alloc_len; 7606 } 7607 ctsio->kern_data_resid = 0; 7608 ctsio->kern_rel_offset = 0; 7609 7610 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7611 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7612 data->byte2 |= RST_ITNRS; 7613 7614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7615 ctsio->be_move_done = ctl_config_move_done; 7616 7617 ctl_datamove((union ctl_io *)ctsio); 7618 return (retval); 7619} 7620 7621int 7622ctl_report_timestamp(struct ctl_scsiio *ctsio) 7623{ 7624 struct ctl_lun *lun; 7625 struct scsi_report_timestamp *cdb; 7626 struct scsi_report_timestamp_data *data; 7627 struct timeval tv; 7628 int64_t timestamp; 7629 int retval; 7630 int alloc_len, total_len; 7631 7632 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7633 7634 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7635 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7636 7637 retval = CTL_RETVAL_COMPLETE; 7638 7639 total_len = sizeof(struct scsi_report_timestamp_data); 7640 alloc_len = scsi_4btoul(cdb->length); 7641 7642 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7643 7644 ctsio->kern_sg_entries = 0; 7645 7646 if (total_len < alloc_len) { 7647 ctsio->residual = alloc_len - total_len; 7648 ctsio->kern_data_len = total_len; 7649 ctsio->kern_total_len = total_len; 7650 } else { 7651 ctsio->residual = 0; 7652 ctsio->kern_data_len = alloc_len; 7653 ctsio->kern_total_len = alloc_len; 7654 } 7655 ctsio->kern_data_resid = 0; 7656 ctsio->kern_rel_offset = 0; 7657 7658 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7659 scsi_ulto2b(sizeof(*data) - 2, data->length); 7660 data->origin = RTS_ORIG_OUTSIDE; 7661 getmicrotime(&tv); 7662 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7663 scsi_ulto4b(timestamp >> 16, data->timestamp); 7664 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7665 7666 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7667 ctsio->be_move_done = ctl_config_move_done; 7668 7669 ctl_datamove((union ctl_io *)ctsio); 7670 return (retval); 7671} 7672 7673int 7674ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7675{ 7676 struct scsi_per_res_in *cdb; 7677 int alloc_len, total_len = 0; 7678 /* struct scsi_per_res_in_rsrv in_data; */ 7679 struct ctl_lun *lun; 7680 struct ctl_softc *softc; 7681 7682 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7683 7684 softc = control_softc; 7685 7686 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7687 7688 alloc_len = scsi_2btoul(cdb->length); 7689 7690 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7691 7692retry: 7693 mtx_lock(&lun->lun_lock); 7694 switch (cdb->action) { 7695 case SPRI_RK: /* read keys */ 7696 total_len = sizeof(struct scsi_per_res_in_keys) + 7697 lun->pr_key_count * 7698 sizeof(struct scsi_per_res_key); 7699 break; 7700 case SPRI_RR: /* read reservation */ 7701 if (lun->flags & CTL_LUN_PR_RESERVED) 7702 total_len = sizeof(struct scsi_per_res_in_rsrv); 7703 else 7704 total_len = sizeof(struct scsi_per_res_in_header); 7705 break; 7706 case SPRI_RC: /* report capabilities */ 7707 total_len = sizeof(struct scsi_per_res_cap); 7708 break; 7709 case SPRI_RS: /* read full status */ 7710 total_len = sizeof(struct scsi_per_res_in_header) + 7711 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7712 lun->pr_key_count; 7713 break; 7714 default: 7715 panic("Invalid PR type %x", cdb->action); 7716 } 7717 mtx_unlock(&lun->lun_lock); 7718 7719 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7720 7721 if (total_len < alloc_len) { 7722 ctsio->residual = alloc_len - total_len; 7723 ctsio->kern_data_len = total_len; 7724 ctsio->kern_total_len = total_len; 7725 } else { 7726 ctsio->residual = 0; 7727 ctsio->kern_data_len = alloc_len; 7728 ctsio->kern_total_len = alloc_len; 7729 } 7730 7731 ctsio->kern_data_resid = 0; 7732 ctsio->kern_rel_offset = 0; 7733 ctsio->kern_sg_entries = 0; 7734 7735 mtx_lock(&lun->lun_lock); 7736 switch (cdb->action) { 7737 case SPRI_RK: { // read keys 7738 struct scsi_per_res_in_keys *res_keys; 7739 int i, key_count; 7740 7741 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7742 7743 /* 7744 * We had to drop the lock to allocate our buffer, which 7745 * leaves time for someone to come in with another 7746 * persistent reservation. (That is unlikely, though, 7747 * since this should be the only persistent reservation 7748 * command active right now.) 7749 */ 7750 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7751 (lun->pr_key_count * 7752 sizeof(struct scsi_per_res_key)))){ 7753 mtx_unlock(&lun->lun_lock); 7754 free(ctsio->kern_data_ptr, M_CTL); 7755 printf("%s: reservation length changed, retrying\n", 7756 __func__); 7757 goto retry; 7758 } 7759 7760 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7761 7762 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7763 lun->pr_key_count, res_keys->header.length); 7764 7765 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7766 if (!lun->per_res[i].registered) 7767 continue; 7768 7769 /* 7770 * We used lun->pr_key_count to calculate the 7771 * size to allocate. If it turns out the number of 7772 * initiators with the registered flag set is 7773 * larger than that (i.e. they haven't been kept in 7774 * sync), we've got a problem. 7775 */ 7776 if (key_count >= lun->pr_key_count) { 7777#ifdef NEEDTOPORT 7778 csevent_log(CSC_CTL | CSC_SHELF_SW | 7779 CTL_PR_ERROR, 7780 csevent_LogType_Fault, 7781 csevent_AlertLevel_Yellow, 7782 csevent_FRU_ShelfController, 7783 csevent_FRU_Firmware, 7784 csevent_FRU_Unknown, 7785 "registered keys %d >= key " 7786 "count %d", key_count, 7787 lun->pr_key_count); 7788#endif 7789 key_count++; 7790 continue; 7791 } 7792 memcpy(res_keys->keys[key_count].key, 7793 lun->per_res[i].res_key.key, 7794 ctl_min(sizeof(res_keys->keys[key_count].key), 7795 sizeof(lun->per_res[i].res_key))); 7796 key_count++; 7797 } 7798 break; 7799 } 7800 case SPRI_RR: { // read reservation 7801 struct scsi_per_res_in_rsrv *res; 7802 int tmp_len, header_only; 7803 7804 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7805 7806 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7807 7808 if (lun->flags & CTL_LUN_PR_RESERVED) 7809 { 7810 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7811 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7812 res->header.length); 7813 header_only = 0; 7814 } else { 7815 tmp_len = sizeof(struct scsi_per_res_in_header); 7816 scsi_ulto4b(0, res->header.length); 7817 header_only = 1; 7818 } 7819 7820 /* 7821 * We had to drop the lock to allocate our buffer, which 7822 * leaves time for someone to come in with another 7823 * persistent reservation. (That is unlikely, though, 7824 * since this should be the only persistent reservation 7825 * command active right now.) 7826 */ 7827 if (tmp_len != total_len) { 7828 mtx_unlock(&lun->lun_lock); 7829 free(ctsio->kern_data_ptr, M_CTL); 7830 printf("%s: reservation status changed, retrying\n", 7831 __func__); 7832 goto retry; 7833 } 7834 7835 /* 7836 * No reservation held, so we're done. 7837 */ 7838 if (header_only != 0) 7839 break; 7840 7841 /* 7842 * If the registration is an All Registrants type, the key 7843 * is 0, since it doesn't really matter. 7844 */ 7845 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7846 memcpy(res->data.reservation, 7847 &lun->per_res[lun->pr_res_idx].res_key, 7848 sizeof(struct scsi_per_res_key)); 7849 } 7850 res->data.scopetype = lun->res_type; 7851 break; 7852 } 7853 case SPRI_RC: //report capabilities 7854 { 7855 struct scsi_per_res_cap *res_cap; 7856 uint16_t type_mask; 7857 7858 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7859 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7860 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7861 type_mask = SPRI_TM_WR_EX_AR | 7862 SPRI_TM_EX_AC_RO | 7863 SPRI_TM_WR_EX_RO | 7864 SPRI_TM_EX_AC | 7865 SPRI_TM_WR_EX | 7866 SPRI_TM_EX_AC_AR; 7867 scsi_ulto2b(type_mask, res_cap->type_mask); 7868 break; 7869 } 7870 case SPRI_RS: { // read full status 7871 struct scsi_per_res_in_full *res_status; 7872 struct scsi_per_res_in_full_desc *res_desc; 7873 struct ctl_port *port; 7874 int i, len; 7875 7876 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7877 7878 /* 7879 * We had to drop the lock to allocate our buffer, which 7880 * leaves time for someone to come in with another 7881 * persistent reservation. (That is unlikely, though, 7882 * since this should be the only persistent reservation 7883 * command active right now.) 7884 */ 7885 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7886 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7887 lun->pr_key_count)){ 7888 mtx_unlock(&lun->lun_lock); 7889 free(ctsio->kern_data_ptr, M_CTL); 7890 printf("%s: reservation length changed, retrying\n", 7891 __func__); 7892 goto retry; 7893 } 7894 7895 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7896 7897 res_desc = &res_status->desc[0]; 7898 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7899 if (!lun->per_res[i].registered) 7900 continue; 7901 7902 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7903 sizeof(res_desc->res_key)); 7904 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7905 (lun->pr_res_idx == i || 7906 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7907 res_desc->flags = SPRI_FULL_R_HOLDER; 7908 res_desc->scopetype = lun->res_type; 7909 } 7910 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7911 res_desc->rel_trgt_port_id); 7912 len = 0; 7913 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7914 if (port != NULL) 7915 len = ctl_create_iid(port, 7916 i % CTL_MAX_INIT_PER_PORT, 7917 res_desc->transport_id); 7918 scsi_ulto4b(len, res_desc->additional_length); 7919 res_desc = (struct scsi_per_res_in_full_desc *) 7920 &res_desc->transport_id[len]; 7921 } 7922 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7923 res_status->header.length); 7924 break; 7925 } 7926 default: 7927 /* 7928 * This is a bug, because we just checked for this above, 7929 * and should have returned an error. 7930 */ 7931 panic("Invalid PR type %x", cdb->action); 7932 break; /* NOTREACHED */ 7933 } 7934 mtx_unlock(&lun->lun_lock); 7935 7936 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7937 ctsio->be_move_done = ctl_config_move_done; 7938 7939 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7940 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7941 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7942 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7943 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7944 7945 ctl_datamove((union ctl_io *)ctsio); 7946 7947 return (CTL_RETVAL_COMPLETE); 7948} 7949 7950/* 7951 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7952 * it should return. 7953 */ 7954static int 7955ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7956 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7957 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7958 struct scsi_per_res_out_parms* param) 7959{ 7960 union ctl_ha_msg persis_io; 7961 int retval, i; 7962 int isc_retval; 7963 7964 retval = 0; 7965 7966 mtx_lock(&lun->lun_lock); 7967 if (sa_res_key == 0) { 7968 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7969 /* validate scope and type */ 7970 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7971 SPR_LU_SCOPE) { 7972 mtx_unlock(&lun->lun_lock); 7973 ctl_set_invalid_field(/*ctsio*/ ctsio, 7974 /*sks_valid*/ 1, 7975 /*command*/ 1, 7976 /*field*/ 2, 7977 /*bit_valid*/ 1, 7978 /*bit*/ 4); 7979 ctl_done((union ctl_io *)ctsio); 7980 return (1); 7981 } 7982 7983 if (type>8 || type==2 || type==4 || type==0) { 7984 mtx_unlock(&lun->lun_lock); 7985 ctl_set_invalid_field(/*ctsio*/ ctsio, 7986 /*sks_valid*/ 1, 7987 /*command*/ 1, 7988 /*field*/ 2, 7989 /*bit_valid*/ 1, 7990 /*bit*/ 0); 7991 ctl_done((union ctl_io *)ctsio); 7992 return (1); 7993 } 7994 7995 /* temporarily unregister this nexus */ 7996 lun->per_res[residx].registered = 0; 7997 7998 /* 7999 * Unregister everybody else and build UA for 8000 * them 8001 */ 8002 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8003 if (lun->per_res[i].registered == 0) 8004 continue; 8005 8006 if (!persis_offset 8007 && i <CTL_MAX_INITIATORS) 8008 lun->pending_ua[i] |= 8009 CTL_UA_REG_PREEMPT; 8010 else if (persis_offset 8011 && i >= persis_offset) 8012 lun->pending_ua[i-persis_offset] |= 8013 CTL_UA_REG_PREEMPT; 8014 lun->per_res[i].registered = 0; 8015 memset(&lun->per_res[i].res_key, 0, 8016 sizeof(struct scsi_per_res_key)); 8017 } 8018 lun->per_res[residx].registered = 1; 8019 lun->pr_key_count = 1; 8020 lun->res_type = type; 8021 if (lun->res_type != SPR_TYPE_WR_EX_AR 8022 && lun->res_type != SPR_TYPE_EX_AC_AR) 8023 lun->pr_res_idx = residx; 8024 8025 /* send msg to other side */ 8026 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8027 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8028 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8029 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8030 persis_io.pr.pr_info.res_type = type; 8031 memcpy(persis_io.pr.pr_info.sa_res_key, 8032 param->serv_act_res_key, 8033 sizeof(param->serv_act_res_key)); 8034 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8035 &persis_io, sizeof(persis_io), 0)) > 8036 CTL_HA_STATUS_SUCCESS) { 8037 printf("CTL:Persis Out error returned " 8038 "from ctl_ha_msg_send %d\n", 8039 isc_retval); 8040 } 8041 } else { 8042 /* not all registrants */ 8043 mtx_unlock(&lun->lun_lock); 8044 free(ctsio->kern_data_ptr, M_CTL); 8045 ctl_set_invalid_field(ctsio, 8046 /*sks_valid*/ 1, 8047 /*command*/ 0, 8048 /*field*/ 8, 8049 /*bit_valid*/ 0, 8050 /*bit*/ 0); 8051 ctl_done((union ctl_io *)ctsio); 8052 return (1); 8053 } 8054 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8055 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8056 int found = 0; 8057 8058 if (res_key == sa_res_key) { 8059 /* special case */ 8060 /* 8061 * The spec implies this is not good but doesn't 8062 * say what to do. There are two choices either 8063 * generate a res conflict or check condition 8064 * with illegal field in parameter data. Since 8065 * that is what is done when the sa_res_key is 8066 * zero I'll take that approach since this has 8067 * to do with the sa_res_key. 8068 */ 8069 mtx_unlock(&lun->lun_lock); 8070 free(ctsio->kern_data_ptr, M_CTL); 8071 ctl_set_invalid_field(ctsio, 8072 /*sks_valid*/ 1, 8073 /*command*/ 0, 8074 /*field*/ 8, 8075 /*bit_valid*/ 0, 8076 /*bit*/ 0); 8077 ctl_done((union ctl_io *)ctsio); 8078 return (1); 8079 } 8080 8081 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8082 if (lun->per_res[i].registered 8083 && memcmp(param->serv_act_res_key, 8084 lun->per_res[i].res_key.key, 8085 sizeof(struct scsi_per_res_key)) != 0) 8086 continue; 8087 8088 found = 1; 8089 lun->per_res[i].registered = 0; 8090 memset(&lun->per_res[i].res_key, 0, 8091 sizeof(struct scsi_per_res_key)); 8092 lun->pr_key_count--; 8093 8094 if (!persis_offset && i < CTL_MAX_INITIATORS) 8095 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8096 else if (persis_offset && i >= persis_offset) 8097 lun->pending_ua[i-persis_offset] |= 8098 CTL_UA_REG_PREEMPT; 8099 } 8100 if (!found) { 8101 mtx_unlock(&lun->lun_lock); 8102 free(ctsio->kern_data_ptr, M_CTL); 8103 ctl_set_reservation_conflict(ctsio); 8104 ctl_done((union ctl_io *)ctsio); 8105 return (CTL_RETVAL_COMPLETE); 8106 } 8107 /* send msg to other side */ 8108 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8109 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8110 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8111 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8112 persis_io.pr.pr_info.res_type = type; 8113 memcpy(persis_io.pr.pr_info.sa_res_key, 8114 param->serv_act_res_key, 8115 sizeof(param->serv_act_res_key)); 8116 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8117 &persis_io, sizeof(persis_io), 0)) > 8118 CTL_HA_STATUS_SUCCESS) { 8119 printf("CTL:Persis Out error returned from " 8120 "ctl_ha_msg_send %d\n", isc_retval); 8121 } 8122 } else { 8123 /* Reserved but not all registrants */ 8124 /* sa_res_key is res holder */ 8125 if (memcmp(param->serv_act_res_key, 8126 lun->per_res[lun->pr_res_idx].res_key.key, 8127 sizeof(struct scsi_per_res_key)) == 0) { 8128 /* validate scope and type */ 8129 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8130 SPR_LU_SCOPE) { 8131 mtx_unlock(&lun->lun_lock); 8132 ctl_set_invalid_field(/*ctsio*/ ctsio, 8133 /*sks_valid*/ 1, 8134 /*command*/ 1, 8135 /*field*/ 2, 8136 /*bit_valid*/ 1, 8137 /*bit*/ 4); 8138 ctl_done((union ctl_io *)ctsio); 8139 return (1); 8140 } 8141 8142 if (type>8 || type==2 || type==4 || type==0) { 8143 mtx_unlock(&lun->lun_lock); 8144 ctl_set_invalid_field(/*ctsio*/ ctsio, 8145 /*sks_valid*/ 1, 8146 /*command*/ 1, 8147 /*field*/ 2, 8148 /*bit_valid*/ 1, 8149 /*bit*/ 0); 8150 ctl_done((union ctl_io *)ctsio); 8151 return (1); 8152 } 8153 8154 /* 8155 * Do the following: 8156 * if sa_res_key != res_key remove all 8157 * registrants w/sa_res_key and generate UA 8158 * for these registrants(Registrations 8159 * Preempted) if it wasn't an exclusive 8160 * reservation generate UA(Reservations 8161 * Preempted) for all other registered nexuses 8162 * if the type has changed. Establish the new 8163 * reservation and holder. If res_key and 8164 * sa_res_key are the same do the above 8165 * except don't unregister the res holder. 8166 */ 8167 8168 /* 8169 * Temporarily unregister so it won't get 8170 * removed or UA generated 8171 */ 8172 lun->per_res[residx].registered = 0; 8173 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8174 if (lun->per_res[i].registered == 0) 8175 continue; 8176 8177 if (memcmp(param->serv_act_res_key, 8178 lun->per_res[i].res_key.key, 8179 sizeof(struct scsi_per_res_key)) == 0) { 8180 lun->per_res[i].registered = 0; 8181 memset(&lun->per_res[i].res_key, 8182 0, 8183 sizeof(struct scsi_per_res_key)); 8184 lun->pr_key_count--; 8185 8186 if (!persis_offset 8187 && i < CTL_MAX_INITIATORS) 8188 lun->pending_ua[i] |= 8189 CTL_UA_REG_PREEMPT; 8190 else if (persis_offset 8191 && i >= persis_offset) 8192 lun->pending_ua[i-persis_offset] |= 8193 CTL_UA_REG_PREEMPT; 8194 } else if (type != lun->res_type 8195 && (lun->res_type == SPR_TYPE_WR_EX_RO 8196 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8197 if (!persis_offset 8198 && i < CTL_MAX_INITIATORS) 8199 lun->pending_ua[i] |= 8200 CTL_UA_RES_RELEASE; 8201 else if (persis_offset 8202 && i >= persis_offset) 8203 lun->pending_ua[ 8204 i-persis_offset] |= 8205 CTL_UA_RES_RELEASE; 8206 } 8207 } 8208 lun->per_res[residx].registered = 1; 8209 lun->res_type = type; 8210 if (lun->res_type != SPR_TYPE_WR_EX_AR 8211 && lun->res_type != SPR_TYPE_EX_AC_AR) 8212 lun->pr_res_idx = residx; 8213 else 8214 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8215 8216 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8217 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8218 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8219 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8220 persis_io.pr.pr_info.res_type = type; 8221 memcpy(persis_io.pr.pr_info.sa_res_key, 8222 param->serv_act_res_key, 8223 sizeof(param->serv_act_res_key)); 8224 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8225 &persis_io, sizeof(persis_io), 0)) > 8226 CTL_HA_STATUS_SUCCESS) { 8227 printf("CTL:Persis Out error returned " 8228 "from ctl_ha_msg_send %d\n", 8229 isc_retval); 8230 } 8231 } else { 8232 /* 8233 * sa_res_key is not the res holder just 8234 * remove registrants 8235 */ 8236 int found=0; 8237 8238 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8239 if (memcmp(param->serv_act_res_key, 8240 lun->per_res[i].res_key.key, 8241 sizeof(struct scsi_per_res_key)) != 0) 8242 continue; 8243 8244 found = 1; 8245 lun->per_res[i].registered = 0; 8246 memset(&lun->per_res[i].res_key, 0, 8247 sizeof(struct scsi_per_res_key)); 8248 lun->pr_key_count--; 8249 8250 if (!persis_offset 8251 && i < CTL_MAX_INITIATORS) 8252 lun->pending_ua[i] |= 8253 CTL_UA_REG_PREEMPT; 8254 else if (persis_offset 8255 && i >= persis_offset) 8256 lun->pending_ua[i-persis_offset] |= 8257 CTL_UA_REG_PREEMPT; 8258 } 8259 8260 if (!found) { 8261 mtx_unlock(&lun->lun_lock); 8262 free(ctsio->kern_data_ptr, M_CTL); 8263 ctl_set_reservation_conflict(ctsio); 8264 ctl_done((union ctl_io *)ctsio); 8265 return (1); 8266 } 8267 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8268 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8269 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8270 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8271 persis_io.pr.pr_info.res_type = type; 8272 memcpy(persis_io.pr.pr_info.sa_res_key, 8273 param->serv_act_res_key, 8274 sizeof(param->serv_act_res_key)); 8275 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8276 &persis_io, sizeof(persis_io), 0)) > 8277 CTL_HA_STATUS_SUCCESS) { 8278 printf("CTL:Persis Out error returned " 8279 "from ctl_ha_msg_send %d\n", 8280 isc_retval); 8281 } 8282 } 8283 } 8284 8285 lun->PRGeneration++; 8286 mtx_unlock(&lun->lun_lock); 8287 8288 return (retval); 8289} 8290 8291static void 8292ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8293{ 8294 int i; 8295 8296 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8297 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8298 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8299 msg->pr.pr_info.sa_res_key, 8300 sizeof(struct scsi_per_res_key)) != 0) { 8301 uint64_t sa_res_key; 8302 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8303 8304 if (sa_res_key == 0) { 8305 /* temporarily unregister this nexus */ 8306 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8307 8308 /* 8309 * Unregister everybody else and build UA for 8310 * them 8311 */ 8312 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8313 if (lun->per_res[i].registered == 0) 8314 continue; 8315 8316 if (!persis_offset 8317 && i < CTL_MAX_INITIATORS) 8318 lun->pending_ua[i] |= 8319 CTL_UA_REG_PREEMPT; 8320 else if (persis_offset && i >= persis_offset) 8321 lun->pending_ua[i - persis_offset] |= 8322 CTL_UA_REG_PREEMPT; 8323 lun->per_res[i].registered = 0; 8324 memset(&lun->per_res[i].res_key, 0, 8325 sizeof(struct scsi_per_res_key)); 8326 } 8327 8328 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8329 lun->pr_key_count = 1; 8330 lun->res_type = msg->pr.pr_info.res_type; 8331 if (lun->res_type != SPR_TYPE_WR_EX_AR 8332 && lun->res_type != SPR_TYPE_EX_AC_AR) 8333 lun->pr_res_idx = msg->pr.pr_info.residx; 8334 } else { 8335 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8336 if (memcmp(msg->pr.pr_info.sa_res_key, 8337 lun->per_res[i].res_key.key, 8338 sizeof(struct scsi_per_res_key)) != 0) 8339 continue; 8340 8341 lun->per_res[i].registered = 0; 8342 memset(&lun->per_res[i].res_key, 0, 8343 sizeof(struct scsi_per_res_key)); 8344 lun->pr_key_count--; 8345 8346 if (!persis_offset 8347 && i < persis_offset) 8348 lun->pending_ua[i] |= 8349 CTL_UA_REG_PREEMPT; 8350 else if (persis_offset 8351 && i >= persis_offset) 8352 lun->pending_ua[i - persis_offset] |= 8353 CTL_UA_REG_PREEMPT; 8354 } 8355 } 8356 } else { 8357 /* 8358 * Temporarily unregister so it won't get removed 8359 * or UA generated 8360 */ 8361 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8362 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8363 if (lun->per_res[i].registered == 0) 8364 continue; 8365 8366 if (memcmp(msg->pr.pr_info.sa_res_key, 8367 lun->per_res[i].res_key.key, 8368 sizeof(struct scsi_per_res_key)) == 0) { 8369 lun->per_res[i].registered = 0; 8370 memset(&lun->per_res[i].res_key, 0, 8371 sizeof(struct scsi_per_res_key)); 8372 lun->pr_key_count--; 8373 if (!persis_offset 8374 && i < CTL_MAX_INITIATORS) 8375 lun->pending_ua[i] |= 8376 CTL_UA_REG_PREEMPT; 8377 else if (persis_offset 8378 && i >= persis_offset) 8379 lun->pending_ua[i - persis_offset] |= 8380 CTL_UA_REG_PREEMPT; 8381 } else if (msg->pr.pr_info.res_type != lun->res_type 8382 && (lun->res_type == SPR_TYPE_WR_EX_RO 8383 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8384 if (!persis_offset 8385 && i < persis_offset) 8386 lun->pending_ua[i] |= 8387 CTL_UA_RES_RELEASE; 8388 else if (persis_offset 8389 && i >= persis_offset) 8390 lun->pending_ua[i - persis_offset] |= 8391 CTL_UA_RES_RELEASE; 8392 } 8393 } 8394 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8395 lun->res_type = msg->pr.pr_info.res_type; 8396 if (lun->res_type != SPR_TYPE_WR_EX_AR 8397 && lun->res_type != SPR_TYPE_EX_AC_AR) 8398 lun->pr_res_idx = msg->pr.pr_info.residx; 8399 else 8400 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8401 } 8402 lun->PRGeneration++; 8403 8404} 8405 8406 8407int 8408ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8409{ 8410 int retval; 8411 int isc_retval; 8412 u_int32_t param_len; 8413 struct scsi_per_res_out *cdb; 8414 struct ctl_lun *lun; 8415 struct scsi_per_res_out_parms* param; 8416 struct ctl_softc *softc; 8417 uint32_t residx; 8418 uint64_t res_key, sa_res_key; 8419 uint8_t type; 8420 union ctl_ha_msg persis_io; 8421 int i; 8422 8423 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8424 8425 retval = CTL_RETVAL_COMPLETE; 8426 8427 softc = control_softc; 8428 8429 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8430 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8431 8432 /* 8433 * We only support whole-LUN scope. The scope & type are ignored for 8434 * register, register and ignore existing key and clear. 8435 * We sometimes ignore scope and type on preempts too!! 8436 * Verify reservation type here as well. 8437 */ 8438 type = cdb->scope_type & SPR_TYPE_MASK; 8439 if ((cdb->action == SPRO_RESERVE) 8440 || (cdb->action == SPRO_RELEASE)) { 8441 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8442 ctl_set_invalid_field(/*ctsio*/ ctsio, 8443 /*sks_valid*/ 1, 8444 /*command*/ 1, 8445 /*field*/ 2, 8446 /*bit_valid*/ 1, 8447 /*bit*/ 4); 8448 ctl_done((union ctl_io *)ctsio); 8449 return (CTL_RETVAL_COMPLETE); 8450 } 8451 8452 if (type>8 || type==2 || type==4 || type==0) { 8453 ctl_set_invalid_field(/*ctsio*/ ctsio, 8454 /*sks_valid*/ 1, 8455 /*command*/ 1, 8456 /*field*/ 2, 8457 /*bit_valid*/ 1, 8458 /*bit*/ 0); 8459 ctl_done((union ctl_io *)ctsio); 8460 return (CTL_RETVAL_COMPLETE); 8461 } 8462 } 8463 8464 param_len = scsi_4btoul(cdb->length); 8465 8466 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8467 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8468 ctsio->kern_data_len = param_len; 8469 ctsio->kern_total_len = param_len; 8470 ctsio->kern_data_resid = 0; 8471 ctsio->kern_rel_offset = 0; 8472 ctsio->kern_sg_entries = 0; 8473 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8474 ctsio->be_move_done = ctl_config_move_done; 8475 ctl_datamove((union ctl_io *)ctsio); 8476 8477 return (CTL_RETVAL_COMPLETE); 8478 } 8479 8480 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8481 8482 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8483 res_key = scsi_8btou64(param->res_key.key); 8484 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8485 8486 /* 8487 * Validate the reservation key here except for SPRO_REG_IGNO 8488 * This must be done for all other service actions 8489 */ 8490 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8491 mtx_lock(&lun->lun_lock); 8492 if (lun->per_res[residx].registered) { 8493 if (memcmp(param->res_key.key, 8494 lun->per_res[residx].res_key.key, 8495 ctl_min(sizeof(param->res_key), 8496 sizeof(lun->per_res[residx].res_key))) != 0) { 8497 /* 8498 * The current key passed in doesn't match 8499 * the one the initiator previously 8500 * registered. 8501 */ 8502 mtx_unlock(&lun->lun_lock); 8503 free(ctsio->kern_data_ptr, M_CTL); 8504 ctl_set_reservation_conflict(ctsio); 8505 ctl_done((union ctl_io *)ctsio); 8506 return (CTL_RETVAL_COMPLETE); 8507 } 8508 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8509 /* 8510 * We are not registered 8511 */ 8512 mtx_unlock(&lun->lun_lock); 8513 free(ctsio->kern_data_ptr, M_CTL); 8514 ctl_set_reservation_conflict(ctsio); 8515 ctl_done((union ctl_io *)ctsio); 8516 return (CTL_RETVAL_COMPLETE); 8517 } else if (res_key != 0) { 8518 /* 8519 * We are not registered and trying to register but 8520 * the register key isn't zero. 8521 */ 8522 mtx_unlock(&lun->lun_lock); 8523 free(ctsio->kern_data_ptr, M_CTL); 8524 ctl_set_reservation_conflict(ctsio); 8525 ctl_done((union ctl_io *)ctsio); 8526 return (CTL_RETVAL_COMPLETE); 8527 } 8528 mtx_unlock(&lun->lun_lock); 8529 } 8530 8531 switch (cdb->action & SPRO_ACTION_MASK) { 8532 case SPRO_REGISTER: 8533 case SPRO_REG_IGNO: { 8534 8535#if 0 8536 printf("Registration received\n"); 8537#endif 8538 8539 /* 8540 * We don't support any of these options, as we report in 8541 * the read capabilities request (see 8542 * ctl_persistent_reserve_in(), above). 8543 */ 8544 if ((param->flags & SPR_SPEC_I_PT) 8545 || (param->flags & SPR_ALL_TG_PT) 8546 || (param->flags & SPR_APTPL)) { 8547 int bit_ptr; 8548 8549 if (param->flags & SPR_APTPL) 8550 bit_ptr = 0; 8551 else if (param->flags & SPR_ALL_TG_PT) 8552 bit_ptr = 2; 8553 else /* SPR_SPEC_I_PT */ 8554 bit_ptr = 3; 8555 8556 free(ctsio->kern_data_ptr, M_CTL); 8557 ctl_set_invalid_field(ctsio, 8558 /*sks_valid*/ 1, 8559 /*command*/ 0, 8560 /*field*/ 20, 8561 /*bit_valid*/ 1, 8562 /*bit*/ bit_ptr); 8563 ctl_done((union ctl_io *)ctsio); 8564 return (CTL_RETVAL_COMPLETE); 8565 } 8566 8567 mtx_lock(&lun->lun_lock); 8568 8569 /* 8570 * The initiator wants to clear the 8571 * key/unregister. 8572 */ 8573 if (sa_res_key == 0) { 8574 if ((res_key == 0 8575 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8576 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8577 && !lun->per_res[residx].registered)) { 8578 mtx_unlock(&lun->lun_lock); 8579 goto done; 8580 } 8581 8582 lun->per_res[residx].registered = 0; 8583 memset(&lun->per_res[residx].res_key, 8584 0, sizeof(lun->per_res[residx].res_key)); 8585 lun->pr_key_count--; 8586 8587 if (residx == lun->pr_res_idx) { 8588 lun->flags &= ~CTL_LUN_PR_RESERVED; 8589 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8590 8591 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8592 || lun->res_type == SPR_TYPE_EX_AC_RO) 8593 && lun->pr_key_count) { 8594 /* 8595 * If the reservation is a registrants 8596 * only type we need to generate a UA 8597 * for other registered inits. The 8598 * sense code should be RESERVATIONS 8599 * RELEASED 8600 */ 8601 8602 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8603 if (lun->per_res[ 8604 i+persis_offset].registered 8605 == 0) 8606 continue; 8607 lun->pending_ua[i] |= 8608 CTL_UA_RES_RELEASE; 8609 } 8610 } 8611 lun->res_type = 0; 8612 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8613 if (lun->pr_key_count==0) { 8614 lun->flags &= ~CTL_LUN_PR_RESERVED; 8615 lun->res_type = 0; 8616 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8617 } 8618 } 8619 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8620 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8621 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8622 persis_io.pr.pr_info.residx = residx; 8623 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8624 &persis_io, sizeof(persis_io), 0 )) > 8625 CTL_HA_STATUS_SUCCESS) { 8626 printf("CTL:Persis Out error returned from " 8627 "ctl_ha_msg_send %d\n", isc_retval); 8628 } 8629 } else /* sa_res_key != 0 */ { 8630 8631 /* 8632 * If we aren't registered currently then increment 8633 * the key count and set the registered flag. 8634 */ 8635 if (!lun->per_res[residx].registered) { 8636 lun->pr_key_count++; 8637 lun->per_res[residx].registered = 1; 8638 } 8639 8640 memcpy(&lun->per_res[residx].res_key, 8641 param->serv_act_res_key, 8642 ctl_min(sizeof(param->serv_act_res_key), 8643 sizeof(lun->per_res[residx].res_key))); 8644 8645 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8646 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8647 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8648 persis_io.pr.pr_info.residx = residx; 8649 memcpy(persis_io.pr.pr_info.sa_res_key, 8650 param->serv_act_res_key, 8651 sizeof(param->serv_act_res_key)); 8652 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8653 &persis_io, sizeof(persis_io), 0)) > 8654 CTL_HA_STATUS_SUCCESS) { 8655 printf("CTL:Persis Out error returned from " 8656 "ctl_ha_msg_send %d\n", isc_retval); 8657 } 8658 } 8659 lun->PRGeneration++; 8660 mtx_unlock(&lun->lun_lock); 8661 8662 break; 8663 } 8664 case SPRO_RESERVE: 8665#if 0 8666 printf("Reserve executed type %d\n", type); 8667#endif 8668 mtx_lock(&lun->lun_lock); 8669 if (lun->flags & CTL_LUN_PR_RESERVED) { 8670 /* 8671 * if this isn't the reservation holder and it's 8672 * not a "all registrants" type or if the type is 8673 * different then we have a conflict 8674 */ 8675 if ((lun->pr_res_idx != residx 8676 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8677 || lun->res_type != type) { 8678 mtx_unlock(&lun->lun_lock); 8679 free(ctsio->kern_data_ptr, M_CTL); 8680 ctl_set_reservation_conflict(ctsio); 8681 ctl_done((union ctl_io *)ctsio); 8682 return (CTL_RETVAL_COMPLETE); 8683 } 8684 mtx_unlock(&lun->lun_lock); 8685 } else /* create a reservation */ { 8686 /* 8687 * If it's not an "all registrants" type record 8688 * reservation holder 8689 */ 8690 if (type != SPR_TYPE_WR_EX_AR 8691 && type != SPR_TYPE_EX_AC_AR) 8692 lun->pr_res_idx = residx; /* Res holder */ 8693 else 8694 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8695 8696 lun->flags |= CTL_LUN_PR_RESERVED; 8697 lun->res_type = type; 8698 8699 mtx_unlock(&lun->lun_lock); 8700 8701 /* send msg to other side */ 8702 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8703 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8704 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8705 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8706 persis_io.pr.pr_info.res_type = type; 8707 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8708 &persis_io, sizeof(persis_io), 0)) > 8709 CTL_HA_STATUS_SUCCESS) { 8710 printf("CTL:Persis Out error returned from " 8711 "ctl_ha_msg_send %d\n", isc_retval); 8712 } 8713 } 8714 break; 8715 8716 case SPRO_RELEASE: 8717 mtx_lock(&lun->lun_lock); 8718 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8719 /* No reservation exists return good status */ 8720 mtx_unlock(&lun->lun_lock); 8721 goto done; 8722 } 8723 /* 8724 * Is this nexus a reservation holder? 8725 */ 8726 if (lun->pr_res_idx != residx 8727 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8728 /* 8729 * not a res holder return good status but 8730 * do nothing 8731 */ 8732 mtx_unlock(&lun->lun_lock); 8733 goto done; 8734 } 8735 8736 if (lun->res_type != type) { 8737 mtx_unlock(&lun->lun_lock); 8738 free(ctsio->kern_data_ptr, M_CTL); 8739 ctl_set_illegal_pr_release(ctsio); 8740 ctl_done((union ctl_io *)ctsio); 8741 return (CTL_RETVAL_COMPLETE); 8742 } 8743 8744 /* okay to release */ 8745 lun->flags &= ~CTL_LUN_PR_RESERVED; 8746 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8747 lun->res_type = 0; 8748 8749 /* 8750 * if this isn't an exclusive access 8751 * res generate UA for all other 8752 * registrants. 8753 */ 8754 if (type != SPR_TYPE_EX_AC 8755 && type != SPR_TYPE_WR_EX) { 8756 /* 8757 * temporarily unregister so we don't generate UA 8758 */ 8759 lun->per_res[residx].registered = 0; 8760 8761 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8762 if (lun->per_res[i+persis_offset].registered 8763 == 0) 8764 continue; 8765 lun->pending_ua[i] |= 8766 CTL_UA_RES_RELEASE; 8767 } 8768 8769 lun->per_res[residx].registered = 1; 8770 } 8771 mtx_unlock(&lun->lun_lock); 8772 /* Send msg to other side */ 8773 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8774 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8775 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8776 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8777 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8778 printf("CTL:Persis Out error returned from " 8779 "ctl_ha_msg_send %d\n", isc_retval); 8780 } 8781 break; 8782 8783 case SPRO_CLEAR: 8784 /* send msg to other side */ 8785 8786 mtx_lock(&lun->lun_lock); 8787 lun->flags &= ~CTL_LUN_PR_RESERVED; 8788 lun->res_type = 0; 8789 lun->pr_key_count = 0; 8790 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8791 8792 8793 memset(&lun->per_res[residx].res_key, 8794 0, sizeof(lun->per_res[residx].res_key)); 8795 lun->per_res[residx].registered = 0; 8796 8797 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8798 if (lun->per_res[i].registered) { 8799 if (!persis_offset && i < CTL_MAX_INITIATORS) 8800 lun->pending_ua[i] |= 8801 CTL_UA_RES_PREEMPT; 8802 else if (persis_offset && i >= persis_offset) 8803 lun->pending_ua[i-persis_offset] |= 8804 CTL_UA_RES_PREEMPT; 8805 8806 memset(&lun->per_res[i].res_key, 8807 0, sizeof(struct scsi_per_res_key)); 8808 lun->per_res[i].registered = 0; 8809 } 8810 lun->PRGeneration++; 8811 mtx_unlock(&lun->lun_lock); 8812 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8813 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8814 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8815 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8816 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8817 printf("CTL:Persis Out error returned from " 8818 "ctl_ha_msg_send %d\n", isc_retval); 8819 } 8820 break; 8821 8822 case SPRO_PREEMPT: { 8823 int nretval; 8824 8825 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8826 residx, ctsio, cdb, param); 8827 if (nretval != 0) 8828 return (CTL_RETVAL_COMPLETE); 8829 break; 8830 } 8831 default: 8832 panic("Invalid PR type %x", cdb->action); 8833 } 8834 8835done: 8836 free(ctsio->kern_data_ptr, M_CTL); 8837 ctl_set_success(ctsio); 8838 ctl_done((union ctl_io *)ctsio); 8839 8840 return (retval); 8841} 8842 8843/* 8844 * This routine is for handling a message from the other SC pertaining to 8845 * persistent reserve out. All the error checking will have been done 8846 * so only perorming the action need be done here to keep the two 8847 * in sync. 8848 */ 8849static void 8850ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8851{ 8852 struct ctl_lun *lun; 8853 struct ctl_softc *softc; 8854 int i; 8855 uint32_t targ_lun; 8856 8857 softc = control_softc; 8858 8859 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8860 lun = softc->ctl_luns[targ_lun]; 8861 mtx_lock(&lun->lun_lock); 8862 switch(msg->pr.pr_info.action) { 8863 case CTL_PR_REG_KEY: 8864 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8865 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8866 lun->pr_key_count++; 8867 } 8868 lun->PRGeneration++; 8869 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8870 msg->pr.pr_info.sa_res_key, 8871 sizeof(struct scsi_per_res_key)); 8872 break; 8873 8874 case CTL_PR_UNREG_KEY: 8875 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8876 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8877 0, sizeof(struct scsi_per_res_key)); 8878 lun->pr_key_count--; 8879 8880 /* XXX Need to see if the reservation has been released */ 8881 /* if so do we need to generate UA? */ 8882 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8883 lun->flags &= ~CTL_LUN_PR_RESERVED; 8884 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8885 8886 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8887 || lun->res_type == SPR_TYPE_EX_AC_RO) 8888 && lun->pr_key_count) { 8889 /* 8890 * If the reservation is a registrants 8891 * only type we need to generate a UA 8892 * for other registered inits. The 8893 * sense code should be RESERVATIONS 8894 * RELEASED 8895 */ 8896 8897 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8898 if (lun->per_res[i+ 8899 persis_offset].registered == 0) 8900 continue; 8901 8902 lun->pending_ua[i] |= 8903 CTL_UA_RES_RELEASE; 8904 } 8905 } 8906 lun->res_type = 0; 8907 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8908 if (lun->pr_key_count==0) { 8909 lun->flags &= ~CTL_LUN_PR_RESERVED; 8910 lun->res_type = 0; 8911 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8912 } 8913 } 8914 lun->PRGeneration++; 8915 break; 8916 8917 case CTL_PR_RESERVE: 8918 lun->flags |= CTL_LUN_PR_RESERVED; 8919 lun->res_type = msg->pr.pr_info.res_type; 8920 lun->pr_res_idx = msg->pr.pr_info.residx; 8921 8922 break; 8923 8924 case CTL_PR_RELEASE: 8925 /* 8926 * if this isn't an exclusive access res generate UA for all 8927 * other registrants. 8928 */ 8929 if (lun->res_type != SPR_TYPE_EX_AC 8930 && lun->res_type != SPR_TYPE_WR_EX) { 8931 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8932 if (lun->per_res[i+persis_offset].registered) 8933 lun->pending_ua[i] |= 8934 CTL_UA_RES_RELEASE; 8935 } 8936 8937 lun->flags &= ~CTL_LUN_PR_RESERVED; 8938 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8939 lun->res_type = 0; 8940 break; 8941 8942 case CTL_PR_PREEMPT: 8943 ctl_pro_preempt_other(lun, msg); 8944 break; 8945 case CTL_PR_CLEAR: 8946 lun->flags &= ~CTL_LUN_PR_RESERVED; 8947 lun->res_type = 0; 8948 lun->pr_key_count = 0; 8949 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8950 8951 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8952 if (lun->per_res[i].registered == 0) 8953 continue; 8954 if (!persis_offset 8955 && i < CTL_MAX_INITIATORS) 8956 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8957 else if (persis_offset 8958 && i >= persis_offset) 8959 lun->pending_ua[i-persis_offset] |= 8960 CTL_UA_RES_PREEMPT; 8961 memset(&lun->per_res[i].res_key, 0, 8962 sizeof(struct scsi_per_res_key)); 8963 lun->per_res[i].registered = 0; 8964 } 8965 lun->PRGeneration++; 8966 break; 8967 } 8968 8969 mtx_unlock(&lun->lun_lock); 8970} 8971 8972int 8973ctl_read_write(struct ctl_scsiio *ctsio) 8974{ 8975 struct ctl_lun *lun; 8976 struct ctl_lba_len_flags *lbalen; 8977 uint64_t lba; 8978 uint32_t num_blocks; 8979 int fua, dpo; 8980 int retval; 8981 int isread; 8982 8983 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8984 8985 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8986 8987 fua = 0; 8988 dpo = 0; 8989 8990 retval = CTL_RETVAL_COMPLETE; 8991 8992 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8993 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8994 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8995 uint32_t residx; 8996 8997 /* 8998 * XXX KDM need a lock here. 8999 */ 9000 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9001 if ((lun->res_type == SPR_TYPE_EX_AC 9002 && residx != lun->pr_res_idx) 9003 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9004 || lun->res_type == SPR_TYPE_EX_AC_AR) 9005 && !lun->per_res[residx].registered)) { 9006 ctl_set_reservation_conflict(ctsio); 9007 ctl_done((union ctl_io *)ctsio); 9008 return (CTL_RETVAL_COMPLETE); 9009 } 9010 } 9011 9012 switch (ctsio->cdb[0]) { 9013 case READ_6: 9014 case WRITE_6: { 9015 struct scsi_rw_6 *cdb; 9016 9017 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9018 9019 lba = scsi_3btoul(cdb->addr); 9020 /* only 5 bits are valid in the most significant address byte */ 9021 lba &= 0x1fffff; 9022 num_blocks = cdb->length; 9023 /* 9024 * This is correct according to SBC-2. 9025 */ 9026 if (num_blocks == 0) 9027 num_blocks = 256; 9028 break; 9029 } 9030 case READ_10: 9031 case WRITE_10: { 9032 struct scsi_rw_10 *cdb; 9033 9034 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9035 9036 if (cdb->byte2 & SRW10_FUA) 9037 fua = 1; 9038 if (cdb->byte2 & SRW10_DPO) 9039 dpo = 1; 9040 9041 lba = scsi_4btoul(cdb->addr); 9042 num_blocks = scsi_2btoul(cdb->length); 9043 break; 9044 } 9045 case WRITE_VERIFY_10: { 9046 struct scsi_write_verify_10 *cdb; 9047 9048 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9049 9050 /* 9051 * XXX KDM we should do actual write verify support at some 9052 * point. This is obviously fake, we're just translating 9053 * things to a write. So we don't even bother checking the 9054 * BYTCHK field, since we don't do any verification. If 9055 * the user asks for it, we'll just pretend we did it. 9056 */ 9057 if (cdb->byte2 & SWV_DPO) 9058 dpo = 1; 9059 9060 lba = scsi_4btoul(cdb->addr); 9061 num_blocks = scsi_2btoul(cdb->length); 9062 break; 9063 } 9064 case READ_12: 9065 case WRITE_12: { 9066 struct scsi_rw_12 *cdb; 9067 9068 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9069 9070 if (cdb->byte2 & SRW12_FUA) 9071 fua = 1; 9072 if (cdb->byte2 & SRW12_DPO) 9073 dpo = 1; 9074 lba = scsi_4btoul(cdb->addr); 9075 num_blocks = scsi_4btoul(cdb->length); 9076 break; 9077 } 9078 case WRITE_VERIFY_12: { 9079 struct scsi_write_verify_12 *cdb; 9080 9081 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9082 9083 if (cdb->byte2 & SWV_DPO) 9084 dpo = 1; 9085 9086 lba = scsi_4btoul(cdb->addr); 9087 num_blocks = scsi_4btoul(cdb->length); 9088 9089 break; 9090 } 9091 case READ_16: 9092 case WRITE_16: { 9093 struct scsi_rw_16 *cdb; 9094 9095 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9096 9097 if (cdb->byte2 & SRW12_FUA) 9098 fua = 1; 9099 if (cdb->byte2 & SRW12_DPO) 9100 dpo = 1; 9101 9102 lba = scsi_8btou64(cdb->addr); 9103 num_blocks = scsi_4btoul(cdb->length); 9104 break; 9105 } 9106 case WRITE_VERIFY_16: { 9107 struct scsi_write_verify_16 *cdb; 9108 9109 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9110 9111 if (cdb->byte2 & SWV_DPO) 9112 dpo = 1; 9113 9114 lba = scsi_8btou64(cdb->addr); 9115 num_blocks = scsi_4btoul(cdb->length); 9116 break; 9117 } 9118 default: 9119 /* 9120 * We got a command we don't support. This shouldn't 9121 * happen, commands should be filtered out above us. 9122 */ 9123 ctl_set_invalid_opcode(ctsio); 9124 ctl_done((union ctl_io *)ctsio); 9125 9126 return (CTL_RETVAL_COMPLETE); 9127 break; /* NOTREACHED */ 9128 } 9129 9130 /* 9131 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9132 * interesting for us, but if RAIDCore is in write-back mode, 9133 * getting it to do write-through for a particular transaction may 9134 * not be possible. 9135 */ 9136 9137 /* 9138 * The first check is to make sure we're in bounds, the second 9139 * check is to catch wrap-around problems. If the lba + num blocks 9140 * is less than the lba, then we've wrapped around and the block 9141 * range is invalid anyway. 9142 */ 9143 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9144 || ((lba + num_blocks) < lba)) { 9145 ctl_set_lba_out_of_range(ctsio); 9146 ctl_done((union ctl_io *)ctsio); 9147 return (CTL_RETVAL_COMPLETE); 9148 } 9149 9150 /* 9151 * According to SBC-3, a transfer length of 0 is not an error. 9152 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9153 * translates to 256 blocks for those commands. 9154 */ 9155 if (num_blocks == 0) { 9156 ctl_set_success(ctsio); 9157 ctl_done((union ctl_io *)ctsio); 9158 return (CTL_RETVAL_COMPLETE); 9159 } 9160 9161 lbalen = (struct ctl_lba_len_flags *) 9162 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9163 lbalen->lba = lba; 9164 lbalen->len = num_blocks; 9165 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 9166 9167 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9168 ctsio->kern_rel_offset = 0; 9169 9170 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9171 9172 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9173 9174 return (retval); 9175} 9176 9177static int 9178ctl_cnw_cont(union ctl_io *io) 9179{ 9180 struct ctl_scsiio *ctsio; 9181 struct ctl_lun *lun; 9182 struct ctl_lba_len_flags *lbalen; 9183 int retval; 9184 9185 ctsio = &io->scsiio; 9186 ctsio->io_hdr.status = CTL_STATUS_NONE; 9187 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9188 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9189 lbalen = (struct ctl_lba_len_flags *) 9190 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9191 lbalen->flags = CTL_LLF_WRITE; 9192 9193 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9194 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9195 return (retval); 9196} 9197 9198int 9199ctl_cnw(struct ctl_scsiio *ctsio) 9200{ 9201 struct ctl_lun *lun; 9202 struct ctl_lba_len_flags *lbalen; 9203 uint64_t lba; 9204 uint32_t num_blocks; 9205 int fua, dpo; 9206 int retval; 9207 9208 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9209 9210 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9211 9212 fua = 0; 9213 dpo = 0; 9214 9215 retval = CTL_RETVAL_COMPLETE; 9216 9217 switch (ctsio->cdb[0]) { 9218 case COMPARE_AND_WRITE: { 9219 struct scsi_compare_and_write *cdb; 9220 9221 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9222 9223 if (cdb->byte2 & SRW10_FUA) 9224 fua = 1; 9225 if (cdb->byte2 & SRW10_DPO) 9226 dpo = 1; 9227 lba = scsi_8btou64(cdb->addr); 9228 num_blocks = cdb->length; 9229 break; 9230 } 9231 default: 9232 /* 9233 * We got a command we don't support. This shouldn't 9234 * happen, commands should be filtered out above us. 9235 */ 9236 ctl_set_invalid_opcode(ctsio); 9237 ctl_done((union ctl_io *)ctsio); 9238 9239 return (CTL_RETVAL_COMPLETE); 9240 break; /* NOTREACHED */ 9241 } 9242 9243 /* 9244 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9245 * interesting for us, but if RAIDCore is in write-back mode, 9246 * getting it to do write-through for a particular transaction may 9247 * not be possible. 9248 */ 9249 9250 /* 9251 * The first check is to make sure we're in bounds, the second 9252 * check is to catch wrap-around problems. If the lba + num blocks 9253 * is less than the lba, then we've wrapped around and the block 9254 * range is invalid anyway. 9255 */ 9256 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9257 || ((lba + num_blocks) < lba)) { 9258 ctl_set_lba_out_of_range(ctsio); 9259 ctl_done((union ctl_io *)ctsio); 9260 return (CTL_RETVAL_COMPLETE); 9261 } 9262 9263 /* 9264 * According to SBC-3, a transfer length of 0 is not an error. 9265 */ 9266 if (num_blocks == 0) { 9267 ctl_set_success(ctsio); 9268 ctl_done((union ctl_io *)ctsio); 9269 return (CTL_RETVAL_COMPLETE); 9270 } 9271 9272 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9273 ctsio->kern_rel_offset = 0; 9274 9275 /* 9276 * Set the IO_CONT flag, so that if this I/O gets passed to 9277 * ctl_data_submit_done(), it'll get passed back to 9278 * ctl_ctl_cnw_cont() for further processing. 9279 */ 9280 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9281 ctsio->io_cont = ctl_cnw_cont; 9282 9283 lbalen = (struct ctl_lba_len_flags *) 9284 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9285 lbalen->lba = lba; 9286 lbalen->len = num_blocks; 9287 lbalen->flags = CTL_LLF_COMPARE; 9288 9289 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9290 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9291 return (retval); 9292} 9293 9294int 9295ctl_verify(struct ctl_scsiio *ctsio) 9296{ 9297 struct ctl_lun *lun; 9298 struct ctl_lba_len_flags *lbalen; 9299 uint64_t lba; 9300 uint32_t num_blocks; 9301 int bytchk, dpo; 9302 int retval; 9303 9304 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9305 9306 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9307 9308 bytchk = 0; 9309 dpo = 0; 9310 retval = CTL_RETVAL_COMPLETE; 9311 9312 switch (ctsio->cdb[0]) { 9313 case VERIFY_10: { 9314 struct scsi_verify_10 *cdb; 9315 9316 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9317 if (cdb->byte2 & SVFY_BYTCHK) 9318 bytchk = 1; 9319 if (cdb->byte2 & SVFY_DPO) 9320 dpo = 1; 9321 lba = scsi_4btoul(cdb->addr); 9322 num_blocks = scsi_2btoul(cdb->length); 9323 break; 9324 } 9325 case VERIFY_12: { 9326 struct scsi_verify_12 *cdb; 9327 9328 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9329 if (cdb->byte2 & SVFY_BYTCHK) 9330 bytchk = 1; 9331 if (cdb->byte2 & SVFY_DPO) 9332 dpo = 1; 9333 lba = scsi_4btoul(cdb->addr); 9334 num_blocks = scsi_4btoul(cdb->length); 9335 break; 9336 } 9337 case VERIFY_16: { 9338 struct scsi_rw_16 *cdb; 9339 9340 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9341 if (cdb->byte2 & SVFY_BYTCHK) 9342 bytchk = 1; 9343 if (cdb->byte2 & SVFY_DPO) 9344 dpo = 1; 9345 lba = scsi_8btou64(cdb->addr); 9346 num_blocks = scsi_4btoul(cdb->length); 9347 break; 9348 } 9349 default: 9350 /* 9351 * We got a command we don't support. This shouldn't 9352 * happen, commands should be filtered out above us. 9353 */ 9354 ctl_set_invalid_opcode(ctsio); 9355 ctl_done((union ctl_io *)ctsio); 9356 return (CTL_RETVAL_COMPLETE); 9357 } 9358 9359 /* 9360 * The first check is to make sure we're in bounds, the second 9361 * check is to catch wrap-around problems. If the lba + num blocks 9362 * is less than the lba, then we've wrapped around and the block 9363 * range is invalid anyway. 9364 */ 9365 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9366 || ((lba + num_blocks) < lba)) { 9367 ctl_set_lba_out_of_range(ctsio); 9368 ctl_done((union ctl_io *)ctsio); 9369 return (CTL_RETVAL_COMPLETE); 9370 } 9371 9372 /* 9373 * According to SBC-3, a transfer length of 0 is not an error. 9374 */ 9375 if (num_blocks == 0) { 9376 ctl_set_success(ctsio); 9377 ctl_done((union ctl_io *)ctsio); 9378 return (CTL_RETVAL_COMPLETE); 9379 } 9380 9381 lbalen = (struct ctl_lba_len_flags *) 9382 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9383 lbalen->lba = lba; 9384 lbalen->len = num_blocks; 9385 if (bytchk) { 9386 lbalen->flags = CTL_LLF_COMPARE; 9387 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9388 } else { 9389 lbalen->flags = CTL_LLF_VERIFY; 9390 ctsio->kern_total_len = 0; 9391 } 9392 ctsio->kern_rel_offset = 0; 9393 9394 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9395 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9396 return (retval); 9397} 9398 9399int 9400ctl_report_luns(struct ctl_scsiio *ctsio) 9401{ 9402 struct scsi_report_luns *cdb; 9403 struct scsi_report_luns_data *lun_data; 9404 struct ctl_lun *lun, *request_lun; 9405 int num_luns, retval; 9406 uint32_t alloc_len, lun_datalen; 9407 int num_filled, well_known; 9408 uint32_t initidx, targ_lun_id, lun_id; 9409 9410 retval = CTL_RETVAL_COMPLETE; 9411 well_known = 0; 9412 9413 cdb = (struct scsi_report_luns *)ctsio->cdb; 9414 9415 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9416 9417 mtx_lock(&control_softc->ctl_lock); 9418 num_luns = control_softc->num_luns; 9419 mtx_unlock(&control_softc->ctl_lock); 9420 9421 switch (cdb->select_report) { 9422 case RPL_REPORT_DEFAULT: 9423 case RPL_REPORT_ALL: 9424 break; 9425 case RPL_REPORT_WELLKNOWN: 9426 well_known = 1; 9427 num_luns = 0; 9428 break; 9429 default: 9430 ctl_set_invalid_field(ctsio, 9431 /*sks_valid*/ 1, 9432 /*command*/ 1, 9433 /*field*/ 2, 9434 /*bit_valid*/ 0, 9435 /*bit*/ 0); 9436 ctl_done((union ctl_io *)ctsio); 9437 return (retval); 9438 break; /* NOTREACHED */ 9439 } 9440 9441 alloc_len = scsi_4btoul(cdb->length); 9442 /* 9443 * The initiator has to allocate at least 16 bytes for this request, 9444 * so he can at least get the header and the first LUN. Otherwise 9445 * we reject the request (per SPC-3 rev 14, section 6.21). 9446 */ 9447 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9448 sizeof(struct scsi_report_luns_lundata))) { 9449 ctl_set_invalid_field(ctsio, 9450 /*sks_valid*/ 1, 9451 /*command*/ 1, 9452 /*field*/ 6, 9453 /*bit_valid*/ 0, 9454 /*bit*/ 0); 9455 ctl_done((union ctl_io *)ctsio); 9456 return (retval); 9457 } 9458 9459 request_lun = (struct ctl_lun *) 9460 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9461 9462 lun_datalen = sizeof(*lun_data) + 9463 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9464 9465 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9466 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9467 ctsio->kern_sg_entries = 0; 9468 9469 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9470 9471 mtx_lock(&control_softc->ctl_lock); 9472 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9473 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9474 if (lun_id >= CTL_MAX_LUNS) 9475 continue; 9476 lun = control_softc->ctl_luns[lun_id]; 9477 if (lun == NULL) 9478 continue; 9479 9480 if (targ_lun_id <= 0xff) { 9481 /* 9482 * Peripheral addressing method, bus number 0. 9483 */ 9484 lun_data->luns[num_filled].lundata[0] = 9485 RPL_LUNDATA_ATYP_PERIPH; 9486 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9487 num_filled++; 9488 } else if (targ_lun_id <= 0x3fff) { 9489 /* 9490 * Flat addressing method. 9491 */ 9492 lun_data->luns[num_filled].lundata[0] = 9493 RPL_LUNDATA_ATYP_FLAT | 9494 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9495#ifdef OLDCTLHEADERS 9496 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9497 (targ_lun_id & SRLD_BUS_LUN_MASK); 9498#endif 9499 lun_data->luns[num_filled].lundata[1] = 9500#ifdef OLDCTLHEADERS 9501 targ_lun_id >> SRLD_BUS_LUN_BITS; 9502#endif 9503 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9504 num_filled++; 9505 } else { 9506 printf("ctl_report_luns: bogus LUN number %jd, " 9507 "skipping\n", (intmax_t)targ_lun_id); 9508 } 9509 /* 9510 * According to SPC-3, rev 14 section 6.21: 9511 * 9512 * "The execution of a REPORT LUNS command to any valid and 9513 * installed logical unit shall clear the REPORTED LUNS DATA 9514 * HAS CHANGED unit attention condition for all logical 9515 * units of that target with respect to the requesting 9516 * initiator. A valid and installed logical unit is one 9517 * having a PERIPHERAL QUALIFIER of 000b in the standard 9518 * INQUIRY data (see 6.4.2)." 9519 * 9520 * If request_lun is NULL, the LUN this report luns command 9521 * was issued to is either disabled or doesn't exist. In that 9522 * case, we shouldn't clear any pending lun change unit 9523 * attention. 9524 */ 9525 if (request_lun != NULL) { 9526 mtx_lock(&lun->lun_lock); 9527 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9528 mtx_unlock(&lun->lun_lock); 9529 } 9530 } 9531 mtx_unlock(&control_softc->ctl_lock); 9532 9533 /* 9534 * It's quite possible that we've returned fewer LUNs than we allocated 9535 * space for. Trim it. 9536 */ 9537 lun_datalen = sizeof(*lun_data) + 9538 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9539 9540 if (lun_datalen < alloc_len) { 9541 ctsio->residual = alloc_len - lun_datalen; 9542 ctsio->kern_data_len = lun_datalen; 9543 ctsio->kern_total_len = lun_datalen; 9544 } else { 9545 ctsio->residual = 0; 9546 ctsio->kern_data_len = alloc_len; 9547 ctsio->kern_total_len = alloc_len; 9548 } 9549 ctsio->kern_data_resid = 0; 9550 ctsio->kern_rel_offset = 0; 9551 ctsio->kern_sg_entries = 0; 9552 9553 /* 9554 * We set this to the actual data length, regardless of how much 9555 * space we actually have to return results. If the user looks at 9556 * this value, he'll know whether or not he allocated enough space 9557 * and reissue the command if necessary. We don't support well 9558 * known logical units, so if the user asks for that, return none. 9559 */ 9560 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9561 9562 /* 9563 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9564 * this request. 9565 */ 9566 ctsio->scsi_status = SCSI_STATUS_OK; 9567 9568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9569 ctsio->be_move_done = ctl_config_move_done; 9570 ctl_datamove((union ctl_io *)ctsio); 9571 9572 return (retval); 9573} 9574 9575int 9576ctl_request_sense(struct ctl_scsiio *ctsio) 9577{ 9578 struct scsi_request_sense *cdb; 9579 struct scsi_sense_data *sense_ptr; 9580 struct ctl_lun *lun; 9581 uint32_t initidx; 9582 int have_error; 9583 scsi_sense_data_type sense_format; 9584 9585 cdb = (struct scsi_request_sense *)ctsio->cdb; 9586 9587 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9588 9589 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9590 9591 /* 9592 * Determine which sense format the user wants. 9593 */ 9594 if (cdb->byte2 & SRS_DESC) 9595 sense_format = SSD_TYPE_DESC; 9596 else 9597 sense_format = SSD_TYPE_FIXED; 9598 9599 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9600 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9601 ctsio->kern_sg_entries = 0; 9602 9603 /* 9604 * struct scsi_sense_data, which is currently set to 256 bytes, is 9605 * larger than the largest allowed value for the length field in the 9606 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9607 */ 9608 ctsio->residual = 0; 9609 ctsio->kern_data_len = cdb->length; 9610 ctsio->kern_total_len = cdb->length; 9611 9612 ctsio->kern_data_resid = 0; 9613 ctsio->kern_rel_offset = 0; 9614 ctsio->kern_sg_entries = 0; 9615 9616 /* 9617 * If we don't have a LUN, we don't have any pending sense. 9618 */ 9619 if (lun == NULL) 9620 goto no_sense; 9621 9622 have_error = 0; 9623 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9624 /* 9625 * Check for pending sense, and then for pending unit attentions. 9626 * Pending sense gets returned first, then pending unit attentions. 9627 */ 9628 mtx_lock(&lun->lun_lock); 9629#ifdef CTL_WITH_CA 9630 if (ctl_is_set(lun->have_ca, initidx)) { 9631 scsi_sense_data_type stored_format; 9632 9633 /* 9634 * Check to see which sense format was used for the stored 9635 * sense data. 9636 */ 9637 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9638 9639 /* 9640 * If the user requested a different sense format than the 9641 * one we stored, then we need to convert it to the other 9642 * format. If we're going from descriptor to fixed format 9643 * sense data, we may lose things in translation, depending 9644 * on what options were used. 9645 * 9646 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9647 * for some reason we'll just copy it out as-is. 9648 */ 9649 if ((stored_format == SSD_TYPE_FIXED) 9650 && (sense_format == SSD_TYPE_DESC)) 9651 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9652 &lun->pending_sense[initidx], 9653 (struct scsi_sense_data_desc *)sense_ptr); 9654 else if ((stored_format == SSD_TYPE_DESC) 9655 && (sense_format == SSD_TYPE_FIXED)) 9656 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9657 &lun->pending_sense[initidx], 9658 (struct scsi_sense_data_fixed *)sense_ptr); 9659 else 9660 memcpy(sense_ptr, &lun->pending_sense[initidx], 9661 ctl_min(sizeof(*sense_ptr), 9662 sizeof(lun->pending_sense[initidx]))); 9663 9664 ctl_clear_mask(lun->have_ca, initidx); 9665 have_error = 1; 9666 } else 9667#endif 9668 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9669 ctl_ua_type ua_type; 9670 9671 ua_type = ctl_build_ua(lun->pending_ua[initidx], 9672 sense_ptr, sense_format); 9673 if (ua_type != CTL_UA_NONE) { 9674 have_error = 1; 9675 /* We're reporting this UA, so clear it */ 9676 lun->pending_ua[initidx] &= ~ua_type; 9677 } 9678 } 9679 mtx_unlock(&lun->lun_lock); 9680 9681 /* 9682 * We already have a pending error, return it. 9683 */ 9684 if (have_error != 0) { 9685 /* 9686 * We report the SCSI status as OK, since the status of the 9687 * request sense command itself is OK. 9688 */ 9689 ctsio->scsi_status = SCSI_STATUS_OK; 9690 9691 /* 9692 * We report 0 for the sense length, because we aren't doing 9693 * autosense in this case. We're reporting sense as 9694 * parameter data. 9695 */ 9696 ctsio->sense_len = 0; 9697 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9698 ctsio->be_move_done = ctl_config_move_done; 9699 ctl_datamove((union ctl_io *)ctsio); 9700 9701 return (CTL_RETVAL_COMPLETE); 9702 } 9703 9704no_sense: 9705 9706 /* 9707 * No sense information to report, so we report that everything is 9708 * okay. 9709 */ 9710 ctl_set_sense_data(sense_ptr, 9711 lun, 9712 sense_format, 9713 /*current_error*/ 1, 9714 /*sense_key*/ SSD_KEY_NO_SENSE, 9715 /*asc*/ 0x00, 9716 /*ascq*/ 0x00, 9717 SSD_ELEM_NONE); 9718 9719 ctsio->scsi_status = SCSI_STATUS_OK; 9720 9721 /* 9722 * We report 0 for the sense length, because we aren't doing 9723 * autosense in this case. We're reporting sense as parameter data. 9724 */ 9725 ctsio->sense_len = 0; 9726 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9727 ctsio->be_move_done = ctl_config_move_done; 9728 ctl_datamove((union ctl_io *)ctsio); 9729 9730 return (CTL_RETVAL_COMPLETE); 9731} 9732 9733int 9734ctl_tur(struct ctl_scsiio *ctsio) 9735{ 9736 struct ctl_lun *lun; 9737 9738 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9739 9740 CTL_DEBUG_PRINT(("ctl_tur\n")); 9741 9742 if (lun == NULL) 9743 return (EINVAL); 9744 9745 ctsio->scsi_status = SCSI_STATUS_OK; 9746 ctsio->io_hdr.status = CTL_SUCCESS; 9747 9748 ctl_done((union ctl_io *)ctsio); 9749 9750 return (CTL_RETVAL_COMPLETE); 9751} 9752 9753#ifdef notyet 9754static int 9755ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9756{ 9757 9758} 9759#endif 9760 9761static int 9762ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9763{ 9764 struct scsi_vpd_supported_pages *pages; 9765 int sup_page_size; 9766 struct ctl_lun *lun; 9767 9768 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9769 9770 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9771 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9772 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9773 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9774 ctsio->kern_sg_entries = 0; 9775 9776 if (sup_page_size < alloc_len) { 9777 ctsio->residual = alloc_len - sup_page_size; 9778 ctsio->kern_data_len = sup_page_size; 9779 ctsio->kern_total_len = sup_page_size; 9780 } else { 9781 ctsio->residual = 0; 9782 ctsio->kern_data_len = alloc_len; 9783 ctsio->kern_total_len = alloc_len; 9784 } 9785 ctsio->kern_data_resid = 0; 9786 ctsio->kern_rel_offset = 0; 9787 ctsio->kern_sg_entries = 0; 9788 9789 /* 9790 * The control device is always connected. The disk device, on the 9791 * other hand, may not be online all the time. Need to change this 9792 * to figure out whether the disk device is actually online or not. 9793 */ 9794 if (lun != NULL) 9795 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9796 lun->be_lun->lun_type; 9797 else 9798 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9799 9800 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9801 /* Supported VPD pages */ 9802 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9803 /* Serial Number */ 9804 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9805 /* Device Identification */ 9806 pages->page_list[2] = SVPD_DEVICE_ID; 9807 /* SCSI Ports */ 9808 pages->page_list[3] = SVPD_SCSI_PORTS; 9809 /* Third-party Copy */ 9810 pages->page_list[4] = SVPD_SCSI_TPC; 9811 /* Block limits */ 9812 pages->page_list[5] = SVPD_BLOCK_LIMITS;
|
| 9813 /* Block Device Characteristics */ 9814 pages->page_list[6] = SVPD_BDC;
|
9812 /* Logical Block Provisioning */
| 9815 /* Logical Block Provisioning */
|
9813 pages->page_list[6] = SVPD_LBP;
| 9816 pages->page_list[7] = SVPD_LBP;
|
9814 9815 ctsio->scsi_status = SCSI_STATUS_OK; 9816 9817 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9818 ctsio->be_move_done = ctl_config_move_done; 9819 ctl_datamove((union ctl_io *)ctsio); 9820 9821 return (CTL_RETVAL_COMPLETE); 9822} 9823 9824static int 9825ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9826{ 9827 struct scsi_vpd_unit_serial_number *sn_ptr; 9828 struct ctl_lun *lun; 9829 9830 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9831 9832 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9833 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9834 ctsio->kern_sg_entries = 0; 9835 9836 if (sizeof(*sn_ptr) < alloc_len) { 9837 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9838 ctsio->kern_data_len = sizeof(*sn_ptr); 9839 ctsio->kern_total_len = sizeof(*sn_ptr); 9840 } else { 9841 ctsio->residual = 0; 9842 ctsio->kern_data_len = alloc_len; 9843 ctsio->kern_total_len = alloc_len; 9844 } 9845 ctsio->kern_data_resid = 0; 9846 ctsio->kern_rel_offset = 0; 9847 ctsio->kern_sg_entries = 0; 9848 9849 /* 9850 * The control device is always connected. The disk device, on the 9851 * other hand, may not be online all the time. Need to change this 9852 * to figure out whether the disk device is actually online or not. 9853 */ 9854 if (lun != NULL) 9855 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9856 lun->be_lun->lun_type; 9857 else 9858 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9859 9860 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9861 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9862 /* 9863 * If we don't have a LUN, we just leave the serial number as 9864 * all spaces. 9865 */ 9866 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9867 if (lun != NULL) { 9868 strncpy((char *)sn_ptr->serial_num, 9869 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9870 } 9871 ctsio->scsi_status = SCSI_STATUS_OK; 9872 9873 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9874 ctsio->be_move_done = ctl_config_move_done; 9875 ctl_datamove((union ctl_io *)ctsio); 9876 9877 return (CTL_RETVAL_COMPLETE); 9878} 9879 9880 9881static int 9882ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9883{ 9884 struct scsi_vpd_device_id *devid_ptr; 9885 struct scsi_vpd_id_descriptor *desc; 9886 struct ctl_softc *ctl_softc; 9887 struct ctl_lun *lun; 9888 struct ctl_port *port; 9889 int data_len; 9890 uint8_t proto; 9891 9892 ctl_softc = control_softc; 9893 9894 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9895 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9896 9897 data_len = sizeof(struct scsi_vpd_device_id) + 9898 sizeof(struct scsi_vpd_id_descriptor) + 9899 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9900 sizeof(struct scsi_vpd_id_descriptor) + 9901 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9902 if (lun && lun->lun_devid) 9903 data_len += lun->lun_devid->len; 9904 if (port->port_devid) 9905 data_len += port->port_devid->len; 9906 if (port->target_devid) 9907 data_len += port->target_devid->len; 9908 9909 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9910 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9911 ctsio->kern_sg_entries = 0; 9912 9913 if (data_len < alloc_len) { 9914 ctsio->residual = alloc_len - data_len; 9915 ctsio->kern_data_len = data_len; 9916 ctsio->kern_total_len = data_len; 9917 } else { 9918 ctsio->residual = 0; 9919 ctsio->kern_data_len = alloc_len; 9920 ctsio->kern_total_len = alloc_len; 9921 } 9922 ctsio->kern_data_resid = 0; 9923 ctsio->kern_rel_offset = 0; 9924 ctsio->kern_sg_entries = 0; 9925 9926 /* 9927 * The control device is always connected. The disk device, on the 9928 * other hand, may not be online all the time. 9929 */ 9930 if (lun != NULL) 9931 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9932 lun->be_lun->lun_type; 9933 else 9934 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9935 devid_ptr->page_code = SVPD_DEVICE_ID; 9936 scsi_ulto2b(data_len - 4, devid_ptr->length); 9937 9938 if (port->port_type == CTL_PORT_FC) 9939 proto = SCSI_PROTO_FC << 4; 9940 else if (port->port_type == CTL_PORT_ISCSI) 9941 proto = SCSI_PROTO_ISCSI << 4; 9942 else 9943 proto = SCSI_PROTO_SPI << 4; 9944 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9945 9946 /* 9947 * We're using a LUN association here. i.e., this device ID is a 9948 * per-LUN identifier. 9949 */ 9950 if (lun && lun->lun_devid) { 9951 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9952 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9953 lun->lun_devid->len); 9954 } 9955 9956 /* 9957 * This is for the WWPN which is a port association. 9958 */ 9959 if (port->port_devid) { 9960 memcpy(desc, port->port_devid->data, port->port_devid->len); 9961 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9962 port->port_devid->len); 9963 } 9964 9965 /* 9966 * This is for the Relative Target Port(type 4h) identifier 9967 */ 9968 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9969 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9970 SVPD_ID_TYPE_RELTARG; 9971 desc->length = 4; 9972 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9973 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9974 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9975 9976 /* 9977 * This is for the Target Port Group(type 5h) identifier 9978 */ 9979 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9980 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9981 SVPD_ID_TYPE_TPORTGRP; 9982 desc->length = 4; 9983 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9984 &desc->identifier[2]); 9985 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9986 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9987 9988 /* 9989 * This is for the Target identifier 9990 */ 9991 if (port->target_devid) { 9992 memcpy(desc, port->target_devid->data, port->target_devid->len); 9993 } 9994 9995 ctsio->scsi_status = SCSI_STATUS_OK; 9996 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9997 ctsio->be_move_done = ctl_config_move_done; 9998 ctl_datamove((union ctl_io *)ctsio); 9999 10000 return (CTL_RETVAL_COMPLETE); 10001} 10002 10003static int 10004ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10005{ 10006 struct ctl_softc *softc = control_softc; 10007 struct scsi_vpd_scsi_ports *sp; 10008 struct scsi_vpd_port_designation *pd; 10009 struct scsi_vpd_port_designation_cont *pdc; 10010 struct ctl_lun *lun; 10011 struct ctl_port *port; 10012 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10013 int num_target_port_groups, single; 10014 10015 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10016 10017 single = ctl_is_single; 10018 if (single) 10019 num_target_port_groups = 1; 10020 else 10021 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10022 num_target_ports = 0; 10023 iid_len = 0; 10024 id_len = 0; 10025 mtx_lock(&softc->ctl_lock); 10026 STAILQ_FOREACH(port, &softc->port_list, links) { 10027 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10028 continue; 10029 if (lun != NULL && 10030 ctl_map_lun_back(port->targ_port, lun->lun) >= 10031 CTL_MAX_LUNS) 10032 continue; 10033 num_target_ports++; 10034 if (port->init_devid) 10035 iid_len += port->init_devid->len; 10036 if (port->port_devid) 10037 id_len += port->port_devid->len; 10038 } 10039 mtx_unlock(&softc->ctl_lock); 10040 10041 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10042 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10043 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10044 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10045 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10046 ctsio->kern_sg_entries = 0; 10047 10048 if (data_len < alloc_len) { 10049 ctsio->residual = alloc_len - data_len; 10050 ctsio->kern_data_len = data_len; 10051 ctsio->kern_total_len = data_len; 10052 } else { 10053 ctsio->residual = 0; 10054 ctsio->kern_data_len = alloc_len; 10055 ctsio->kern_total_len = alloc_len; 10056 } 10057 ctsio->kern_data_resid = 0; 10058 ctsio->kern_rel_offset = 0; 10059 ctsio->kern_sg_entries = 0; 10060 10061 /* 10062 * The control device is always connected. The disk device, on the 10063 * other hand, may not be online all the time. Need to change this 10064 * to figure out whether the disk device is actually online or not. 10065 */ 10066 if (lun != NULL) 10067 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10068 lun->be_lun->lun_type; 10069 else 10070 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10071 10072 sp->page_code = SVPD_SCSI_PORTS; 10073 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10074 sp->page_length); 10075 pd = &sp->design[0]; 10076 10077 mtx_lock(&softc->ctl_lock); 10078 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10079 pg = 0; 10080 else 10081 pg = 1; 10082 for (g = 0; g < num_target_port_groups; g++) { 10083 STAILQ_FOREACH(port, &softc->port_list, links) { 10084 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10085 continue; 10086 if (lun != NULL && 10087 ctl_map_lun_back(port->targ_port, lun->lun) >= 10088 CTL_MAX_LUNS) 10089 continue; 10090 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10091 scsi_ulto2b(p, pd->relative_port_id); 10092 if (port->init_devid && g == pg) { 10093 iid_len = port->init_devid->len; 10094 memcpy(pd->initiator_transportid, 10095 port->init_devid->data, port->init_devid->len); 10096 } else 10097 iid_len = 0; 10098 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10099 pdc = (struct scsi_vpd_port_designation_cont *) 10100 (&pd->initiator_transportid[iid_len]); 10101 if (port->port_devid && g == pg) { 10102 id_len = port->port_devid->len; 10103 memcpy(pdc->target_port_descriptors, 10104 port->port_devid->data, port->port_devid->len); 10105 } else 10106 id_len = 0; 10107 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10108 pd = (struct scsi_vpd_port_designation *) 10109 ((uint8_t *)pdc->target_port_descriptors + id_len); 10110 } 10111 } 10112 mtx_unlock(&softc->ctl_lock); 10113 10114 ctsio->scsi_status = SCSI_STATUS_OK; 10115 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10116 ctsio->be_move_done = ctl_config_move_done; 10117 ctl_datamove((union ctl_io *)ctsio); 10118 10119 return (CTL_RETVAL_COMPLETE); 10120} 10121 10122static int 10123ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10124{ 10125 struct scsi_vpd_block_limits *bl_ptr; 10126 struct ctl_lun *lun; 10127 int bs; 10128 10129 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10130 10131 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10132 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10133 ctsio->kern_sg_entries = 0; 10134 10135 if (sizeof(*bl_ptr) < alloc_len) { 10136 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10137 ctsio->kern_data_len = sizeof(*bl_ptr); 10138 ctsio->kern_total_len = sizeof(*bl_ptr); 10139 } else { 10140 ctsio->residual = 0; 10141 ctsio->kern_data_len = alloc_len; 10142 ctsio->kern_total_len = alloc_len; 10143 } 10144 ctsio->kern_data_resid = 0; 10145 ctsio->kern_rel_offset = 0; 10146 ctsio->kern_sg_entries = 0; 10147 10148 /* 10149 * The control device is always connected. The disk device, on the 10150 * other hand, may not be online all the time. Need to change this 10151 * to figure out whether the disk device is actually online or not. 10152 */ 10153 if (lun != NULL) 10154 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10155 lun->be_lun->lun_type; 10156 else 10157 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10158 10159 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10160 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10161 bl_ptr->max_cmp_write_len = 0xff; 10162 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10163 if (lun != NULL) { 10164 bs = lun->be_lun->blocksize; 10165 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10166 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10167 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10168 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
| 9817 9818 ctsio->scsi_status = SCSI_STATUS_OK; 9819 9820 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9821 ctsio->be_move_done = ctl_config_move_done; 9822 ctl_datamove((union ctl_io *)ctsio); 9823 9824 return (CTL_RETVAL_COMPLETE); 9825} 9826 9827static int 9828ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9829{ 9830 struct scsi_vpd_unit_serial_number *sn_ptr; 9831 struct ctl_lun *lun; 9832 9833 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9834 9835 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9836 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9837 ctsio->kern_sg_entries = 0; 9838 9839 if (sizeof(*sn_ptr) < alloc_len) { 9840 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9841 ctsio->kern_data_len = sizeof(*sn_ptr); 9842 ctsio->kern_total_len = sizeof(*sn_ptr); 9843 } else { 9844 ctsio->residual = 0; 9845 ctsio->kern_data_len = alloc_len; 9846 ctsio->kern_total_len = alloc_len; 9847 } 9848 ctsio->kern_data_resid = 0; 9849 ctsio->kern_rel_offset = 0; 9850 ctsio->kern_sg_entries = 0; 9851 9852 /* 9853 * The control device is always connected. The disk device, on the 9854 * other hand, may not be online all the time. Need to change this 9855 * to figure out whether the disk device is actually online or not. 9856 */ 9857 if (lun != NULL) 9858 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9859 lun->be_lun->lun_type; 9860 else 9861 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9862 9863 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9864 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9865 /* 9866 * If we don't have a LUN, we just leave the serial number as 9867 * all spaces. 9868 */ 9869 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9870 if (lun != NULL) { 9871 strncpy((char *)sn_ptr->serial_num, 9872 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9873 } 9874 ctsio->scsi_status = SCSI_STATUS_OK; 9875 9876 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9877 ctsio->be_move_done = ctl_config_move_done; 9878 ctl_datamove((union ctl_io *)ctsio); 9879 9880 return (CTL_RETVAL_COMPLETE); 9881} 9882 9883 9884static int 9885ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9886{ 9887 struct scsi_vpd_device_id *devid_ptr; 9888 struct scsi_vpd_id_descriptor *desc; 9889 struct ctl_softc *ctl_softc; 9890 struct ctl_lun *lun; 9891 struct ctl_port *port; 9892 int data_len; 9893 uint8_t proto; 9894 9895 ctl_softc = control_softc; 9896 9897 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9898 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9899 9900 data_len = sizeof(struct scsi_vpd_device_id) + 9901 sizeof(struct scsi_vpd_id_descriptor) + 9902 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9903 sizeof(struct scsi_vpd_id_descriptor) + 9904 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9905 if (lun && lun->lun_devid) 9906 data_len += lun->lun_devid->len; 9907 if (port->port_devid) 9908 data_len += port->port_devid->len; 9909 if (port->target_devid) 9910 data_len += port->target_devid->len; 9911 9912 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9913 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9914 ctsio->kern_sg_entries = 0; 9915 9916 if (data_len < alloc_len) { 9917 ctsio->residual = alloc_len - data_len; 9918 ctsio->kern_data_len = data_len; 9919 ctsio->kern_total_len = data_len; 9920 } else { 9921 ctsio->residual = 0; 9922 ctsio->kern_data_len = alloc_len; 9923 ctsio->kern_total_len = alloc_len; 9924 } 9925 ctsio->kern_data_resid = 0; 9926 ctsio->kern_rel_offset = 0; 9927 ctsio->kern_sg_entries = 0; 9928 9929 /* 9930 * The control device is always connected. The disk device, on the 9931 * other hand, may not be online all the time. 9932 */ 9933 if (lun != NULL) 9934 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9935 lun->be_lun->lun_type; 9936 else 9937 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9938 devid_ptr->page_code = SVPD_DEVICE_ID; 9939 scsi_ulto2b(data_len - 4, devid_ptr->length); 9940 9941 if (port->port_type == CTL_PORT_FC) 9942 proto = SCSI_PROTO_FC << 4; 9943 else if (port->port_type == CTL_PORT_ISCSI) 9944 proto = SCSI_PROTO_ISCSI << 4; 9945 else 9946 proto = SCSI_PROTO_SPI << 4; 9947 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9948 9949 /* 9950 * We're using a LUN association here. i.e., this device ID is a 9951 * per-LUN identifier. 9952 */ 9953 if (lun && lun->lun_devid) { 9954 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9955 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9956 lun->lun_devid->len); 9957 } 9958 9959 /* 9960 * This is for the WWPN which is a port association. 9961 */ 9962 if (port->port_devid) { 9963 memcpy(desc, port->port_devid->data, port->port_devid->len); 9964 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9965 port->port_devid->len); 9966 } 9967 9968 /* 9969 * This is for the Relative Target Port(type 4h) identifier 9970 */ 9971 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9972 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9973 SVPD_ID_TYPE_RELTARG; 9974 desc->length = 4; 9975 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9976 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9977 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9978 9979 /* 9980 * This is for the Target Port Group(type 5h) identifier 9981 */ 9982 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9983 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9984 SVPD_ID_TYPE_TPORTGRP; 9985 desc->length = 4; 9986 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9987 &desc->identifier[2]); 9988 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9989 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9990 9991 /* 9992 * This is for the Target identifier 9993 */ 9994 if (port->target_devid) { 9995 memcpy(desc, port->target_devid->data, port->target_devid->len); 9996 } 9997 9998 ctsio->scsi_status = SCSI_STATUS_OK; 9999 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10000 ctsio->be_move_done = ctl_config_move_done; 10001 ctl_datamove((union ctl_io *)ctsio); 10002 10003 return (CTL_RETVAL_COMPLETE); 10004} 10005 10006static int 10007ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10008{ 10009 struct ctl_softc *softc = control_softc; 10010 struct scsi_vpd_scsi_ports *sp; 10011 struct scsi_vpd_port_designation *pd; 10012 struct scsi_vpd_port_designation_cont *pdc; 10013 struct ctl_lun *lun; 10014 struct ctl_port *port; 10015 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10016 int num_target_port_groups, single; 10017 10018 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10019 10020 single = ctl_is_single; 10021 if (single) 10022 num_target_port_groups = 1; 10023 else 10024 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10025 num_target_ports = 0; 10026 iid_len = 0; 10027 id_len = 0; 10028 mtx_lock(&softc->ctl_lock); 10029 STAILQ_FOREACH(port, &softc->port_list, links) { 10030 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10031 continue; 10032 if (lun != NULL && 10033 ctl_map_lun_back(port->targ_port, lun->lun) >= 10034 CTL_MAX_LUNS) 10035 continue; 10036 num_target_ports++; 10037 if (port->init_devid) 10038 iid_len += port->init_devid->len; 10039 if (port->port_devid) 10040 id_len += port->port_devid->len; 10041 } 10042 mtx_unlock(&softc->ctl_lock); 10043 10044 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10045 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10046 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10047 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10048 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10049 ctsio->kern_sg_entries = 0; 10050 10051 if (data_len < alloc_len) { 10052 ctsio->residual = alloc_len - data_len; 10053 ctsio->kern_data_len = data_len; 10054 ctsio->kern_total_len = data_len; 10055 } else { 10056 ctsio->residual = 0; 10057 ctsio->kern_data_len = alloc_len; 10058 ctsio->kern_total_len = alloc_len; 10059 } 10060 ctsio->kern_data_resid = 0; 10061 ctsio->kern_rel_offset = 0; 10062 ctsio->kern_sg_entries = 0; 10063 10064 /* 10065 * The control device is always connected. The disk device, on the 10066 * other hand, may not be online all the time. Need to change this 10067 * to figure out whether the disk device is actually online or not. 10068 */ 10069 if (lun != NULL) 10070 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10071 lun->be_lun->lun_type; 10072 else 10073 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10074 10075 sp->page_code = SVPD_SCSI_PORTS; 10076 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10077 sp->page_length); 10078 pd = &sp->design[0]; 10079 10080 mtx_lock(&softc->ctl_lock); 10081 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10082 pg = 0; 10083 else 10084 pg = 1; 10085 for (g = 0; g < num_target_port_groups; g++) { 10086 STAILQ_FOREACH(port, &softc->port_list, links) { 10087 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10088 continue; 10089 if (lun != NULL && 10090 ctl_map_lun_back(port->targ_port, lun->lun) >= 10091 CTL_MAX_LUNS) 10092 continue; 10093 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10094 scsi_ulto2b(p, pd->relative_port_id); 10095 if (port->init_devid && g == pg) { 10096 iid_len = port->init_devid->len; 10097 memcpy(pd->initiator_transportid, 10098 port->init_devid->data, port->init_devid->len); 10099 } else 10100 iid_len = 0; 10101 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10102 pdc = (struct scsi_vpd_port_designation_cont *) 10103 (&pd->initiator_transportid[iid_len]); 10104 if (port->port_devid && g == pg) { 10105 id_len = port->port_devid->len; 10106 memcpy(pdc->target_port_descriptors, 10107 port->port_devid->data, port->port_devid->len); 10108 } else 10109 id_len = 0; 10110 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10111 pd = (struct scsi_vpd_port_designation *) 10112 ((uint8_t *)pdc->target_port_descriptors + id_len); 10113 } 10114 } 10115 mtx_unlock(&softc->ctl_lock); 10116 10117 ctsio->scsi_status = SCSI_STATUS_OK; 10118 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10119 ctsio->be_move_done = ctl_config_move_done; 10120 ctl_datamove((union ctl_io *)ctsio); 10121 10122 return (CTL_RETVAL_COMPLETE); 10123} 10124 10125static int 10126ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10127{ 10128 struct scsi_vpd_block_limits *bl_ptr; 10129 struct ctl_lun *lun; 10130 int bs; 10131 10132 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10133 10134 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10135 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10136 ctsio->kern_sg_entries = 0; 10137 10138 if (sizeof(*bl_ptr) < alloc_len) { 10139 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10140 ctsio->kern_data_len = sizeof(*bl_ptr); 10141 ctsio->kern_total_len = sizeof(*bl_ptr); 10142 } else { 10143 ctsio->residual = 0; 10144 ctsio->kern_data_len = alloc_len; 10145 ctsio->kern_total_len = alloc_len; 10146 } 10147 ctsio->kern_data_resid = 0; 10148 ctsio->kern_rel_offset = 0; 10149 ctsio->kern_sg_entries = 0; 10150 10151 /* 10152 * The control device is always connected. The disk device, on the 10153 * other hand, may not be online all the time. Need to change this 10154 * to figure out whether the disk device is actually online or not. 10155 */ 10156 if (lun != NULL) 10157 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10158 lun->be_lun->lun_type; 10159 else 10160 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10161 10162 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10163 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10164 bl_ptr->max_cmp_write_len = 0xff; 10165 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10166 if (lun != NULL) { 10167 bs = lun->be_lun->blocksize; 10168 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10169 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10170 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10171 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
|
| 10172 if (lun->be_lun->pblockexp != 0) { 10173 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10174 bl_ptr->opt_unmap_grain); 10175 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10176 bl_ptr->unmap_grain_align); 10177 }
|
10169 } 10170 } 10171 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10172 10173 ctsio->scsi_status = SCSI_STATUS_OK; 10174 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10175 ctsio->be_move_done = ctl_config_move_done; 10176 ctl_datamove((union ctl_io *)ctsio); 10177 10178 return (CTL_RETVAL_COMPLETE); 10179} 10180 10181static int
| 10178 } 10179 } 10180 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10181 10182 ctsio->scsi_status = SCSI_STATUS_OK; 10183 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10184 ctsio->be_move_done = ctl_config_move_done; 10185 ctl_datamove((union ctl_io *)ctsio); 10186 10187 return (CTL_RETVAL_COMPLETE); 10188} 10189 10190static int
|
| 10191ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10192{ 10193 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10194 struct ctl_lun *lun; 10195 10196 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10197 10198 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10199 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10200 ctsio->kern_sg_entries = 0; 10201 10202 if (sizeof(*bdc_ptr) < alloc_len) { 10203 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10204 ctsio->kern_data_len = sizeof(*bdc_ptr); 10205 ctsio->kern_total_len = sizeof(*bdc_ptr); 10206 } else { 10207 ctsio->residual = 0; 10208 ctsio->kern_data_len = alloc_len; 10209 ctsio->kern_total_len = alloc_len; 10210 } 10211 ctsio->kern_data_resid = 0; 10212 ctsio->kern_rel_offset = 0; 10213 ctsio->kern_sg_entries = 0; 10214 10215 /* 10216 * The control device is always connected. The disk device, on the 10217 * other hand, may not be online all the time. Need to change this 10218 * to figure out whether the disk device is actually online or not. 10219 */ 10220 if (lun != NULL) 10221 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10222 lun->be_lun->lun_type; 10223 else 10224 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10225 bdc_ptr->page_code = SVPD_BDC; 10226 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10227 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10228 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10229 10230 ctsio->scsi_status = SCSI_STATUS_OK; 10231 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10232 ctsio->be_move_done = ctl_config_move_done; 10233 ctl_datamove((union ctl_io *)ctsio); 10234 10235 return (CTL_RETVAL_COMPLETE); 10236} 10237 10238static int
|
10182ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10183{ 10184 struct scsi_vpd_logical_block_prov *lbp_ptr; 10185 struct ctl_lun *lun; 10186 10187 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10188 10189 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10190 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10191 ctsio->kern_sg_entries = 0; 10192 10193 if (sizeof(*lbp_ptr) < alloc_len) { 10194 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10195 ctsio->kern_data_len = sizeof(*lbp_ptr); 10196 ctsio->kern_total_len = sizeof(*lbp_ptr); 10197 } else { 10198 ctsio->residual = 0; 10199 ctsio->kern_data_len = alloc_len; 10200 ctsio->kern_total_len = alloc_len; 10201 } 10202 ctsio->kern_data_resid = 0; 10203 ctsio->kern_rel_offset = 0; 10204 ctsio->kern_sg_entries = 0; 10205 10206 /* 10207 * The control device is always connected. The disk device, on the 10208 * other hand, may not be online all the time. Need to change this 10209 * to figure out whether the disk device is actually online or not. 10210 */ 10211 if (lun != NULL) 10212 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10213 lun->be_lun->lun_type; 10214 else 10215 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10216 10217 lbp_ptr->page_code = SVPD_LBP;
| 10239ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10240{ 10241 struct scsi_vpd_logical_block_prov *lbp_ptr; 10242 struct ctl_lun *lun; 10243 10244 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10245 10246 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10247 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10248 ctsio->kern_sg_entries = 0; 10249 10250 if (sizeof(*lbp_ptr) < alloc_len) { 10251 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10252 ctsio->kern_data_len = sizeof(*lbp_ptr); 10253 ctsio->kern_total_len = sizeof(*lbp_ptr); 10254 } else { 10255 ctsio->residual = 0; 10256 ctsio->kern_data_len = alloc_len; 10257 ctsio->kern_total_len = alloc_len; 10258 } 10259 ctsio->kern_data_resid = 0; 10260 ctsio->kern_rel_offset = 0; 10261 ctsio->kern_sg_entries = 0; 10262 10263 /* 10264 * The control device is always connected. The disk device, on the 10265 * other hand, may not be online all the time. Need to change this 10266 * to figure out whether the disk device is actually online or not. 10267 */ 10268 if (lun != NULL) 10269 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10270 lun->be_lun->lun_type; 10271 else 10272 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10273 10274 lbp_ptr->page_code = SVPD_LBP;
|
10218 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 10219 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
| 10275 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10276 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10277 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10278 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10279 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10280 }
|
10220 10221 ctsio->scsi_status = SCSI_STATUS_OK; 10222 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10223 ctsio->be_move_done = ctl_config_move_done; 10224 ctl_datamove((union ctl_io *)ctsio); 10225 10226 return (CTL_RETVAL_COMPLETE); 10227} 10228 10229static int 10230ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10231{ 10232 struct scsi_inquiry *cdb; 10233 struct ctl_lun *lun; 10234 int alloc_len, retval; 10235 10236 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10237 cdb = (struct scsi_inquiry *)ctsio->cdb; 10238 10239 retval = CTL_RETVAL_COMPLETE; 10240 10241 alloc_len = scsi_2btoul(cdb->length); 10242 10243 switch (cdb->page_code) { 10244 case SVPD_SUPPORTED_PAGES: 10245 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10246 break; 10247 case SVPD_UNIT_SERIAL_NUMBER: 10248 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10249 break; 10250 case SVPD_DEVICE_ID: 10251 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10252 break; 10253 case SVPD_SCSI_PORTS: 10254 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10255 break; 10256 case SVPD_SCSI_TPC: 10257 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10258 break; 10259 case SVPD_BLOCK_LIMITS: 10260 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10261 break;
| 10281 10282 ctsio->scsi_status = SCSI_STATUS_OK; 10283 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10284 ctsio->be_move_done = ctl_config_move_done; 10285 ctl_datamove((union ctl_io *)ctsio); 10286 10287 return (CTL_RETVAL_COMPLETE); 10288} 10289 10290static int 10291ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10292{ 10293 struct scsi_inquiry *cdb; 10294 struct ctl_lun *lun; 10295 int alloc_len, retval; 10296 10297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10298 cdb = (struct scsi_inquiry *)ctsio->cdb; 10299 10300 retval = CTL_RETVAL_COMPLETE; 10301 10302 alloc_len = scsi_2btoul(cdb->length); 10303 10304 switch (cdb->page_code) { 10305 case SVPD_SUPPORTED_PAGES: 10306 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10307 break; 10308 case SVPD_UNIT_SERIAL_NUMBER: 10309 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10310 break; 10311 case SVPD_DEVICE_ID: 10312 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10313 break; 10314 case SVPD_SCSI_PORTS: 10315 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10316 break; 10317 case SVPD_SCSI_TPC: 10318 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10319 break; 10320 case SVPD_BLOCK_LIMITS: 10321 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10322 break;
|
| 10323 case SVPD_BDC: 10324 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10325 break;
|
10262 case SVPD_LBP: 10263 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10264 break; 10265 default: 10266 ctl_set_invalid_field(ctsio, 10267 /*sks_valid*/ 1, 10268 /*command*/ 1, 10269 /*field*/ 2, 10270 /*bit_valid*/ 0, 10271 /*bit*/ 0); 10272 ctl_done((union ctl_io *)ctsio); 10273 retval = CTL_RETVAL_COMPLETE; 10274 break; 10275 } 10276 10277 return (retval); 10278} 10279 10280static int 10281ctl_inquiry_std(struct ctl_scsiio *ctsio) 10282{ 10283 struct scsi_inquiry_data *inq_ptr; 10284 struct scsi_inquiry *cdb; 10285 struct ctl_softc *ctl_softc; 10286 struct ctl_lun *lun; 10287 char *val; 10288 uint32_t alloc_len; 10289 ctl_port_type port_type; 10290 10291 ctl_softc = control_softc; 10292 10293 /* 10294 * Figure out whether we're talking to a Fibre Channel port or not. 10295 * We treat the ioctl front end, and any SCSI adapters, as packetized 10296 * SCSI front ends. 10297 */ 10298 port_type = ctl_softc->ctl_ports[ 10299 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10300 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10301 port_type = CTL_PORT_SCSI; 10302 10303 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10304 cdb = (struct scsi_inquiry *)ctsio->cdb; 10305 alloc_len = scsi_2btoul(cdb->length); 10306 10307 /* 10308 * We malloc the full inquiry data size here and fill it 10309 * in. If the user only asks for less, we'll give him 10310 * that much. 10311 */ 10312 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10313 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10314 ctsio->kern_sg_entries = 0; 10315 ctsio->kern_data_resid = 0; 10316 ctsio->kern_rel_offset = 0; 10317 10318 if (sizeof(*inq_ptr) < alloc_len) { 10319 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10320 ctsio->kern_data_len = sizeof(*inq_ptr); 10321 ctsio->kern_total_len = sizeof(*inq_ptr); 10322 } else { 10323 ctsio->residual = 0; 10324 ctsio->kern_data_len = alloc_len; 10325 ctsio->kern_total_len = alloc_len; 10326 } 10327 10328 /* 10329 * If we have a LUN configured, report it as connected. Otherwise, 10330 * report that it is offline or no device is supported, depending 10331 * on the value of inquiry_pq_no_lun. 10332 * 10333 * According to the spec (SPC-4 r34), the peripheral qualifier 10334 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10335 * 10336 * "A peripheral device having the specified peripheral device type 10337 * is not connected to this logical unit. However, the device 10338 * server is capable of supporting the specified peripheral device 10339 * type on this logical unit." 10340 * 10341 * According to the same spec, the peripheral qualifier 10342 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10343 * 10344 * "The device server is not capable of supporting a peripheral 10345 * device on this logical unit. For this peripheral qualifier the 10346 * peripheral device type shall be set to 1Fh. All other peripheral 10347 * device type values are reserved for this peripheral qualifier." 10348 * 10349 * Given the text, it would seem that we probably want to report that 10350 * the LUN is offline here. There is no LUN connected, but we can 10351 * support a LUN at the given LUN number. 10352 * 10353 * In the real world, though, it sounds like things are a little 10354 * different: 10355 * 10356 * - Linux, when presented with a LUN with the offline peripheral 10357 * qualifier, will create an sg driver instance for it. So when 10358 * you attach it to CTL, you wind up with a ton of sg driver 10359 * instances. (One for every LUN that Linux bothered to probe.) 10360 * Linux does this despite the fact that it issues a REPORT LUNs 10361 * to LUN 0 to get the inventory of supported LUNs. 10362 * 10363 * - There is other anecdotal evidence (from Emulex folks) about 10364 * arrays that use the offline peripheral qualifier for LUNs that 10365 * are on the "passive" path in an active/passive array. 10366 * 10367 * So the solution is provide a hopefully reasonable default 10368 * (return bad/no LUN) and allow the user to change the behavior 10369 * with a tunable/sysctl variable. 10370 */ 10371 if (lun != NULL) 10372 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10373 lun->be_lun->lun_type; 10374 else if (ctl_softc->inquiry_pq_no_lun == 0) 10375 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10376 else 10377 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10378 10379 /* RMB in byte 2 is 0 */ 10380 inq_ptr->version = SCSI_REV_SPC4; 10381 10382 /* 10383 * According to SAM-3, even if a device only supports a single 10384 * level of LUN addressing, it should still set the HISUP bit: 10385 * 10386 * 4.9.1 Logical unit numbers overview 10387 * 10388 * All logical unit number formats described in this standard are 10389 * hierarchical in structure even when only a single level in that 10390 * hierarchy is used. The HISUP bit shall be set to one in the 10391 * standard INQUIRY data (see SPC-2) when any logical unit number 10392 * format described in this standard is used. Non-hierarchical 10393 * formats are outside the scope of this standard. 10394 * 10395 * Therefore we set the HiSup bit here. 10396 * 10397 * The reponse format is 2, per SPC-3. 10398 */ 10399 inq_ptr->response_format = SID_HiSup | 2; 10400 10401 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 10402 CTL_DEBUG_PRINT(("additional_length = %d\n", 10403 inq_ptr->additional_length)); 10404 10405 inq_ptr->spc3_flags = SPC3_SID_3PC; 10406 if (!ctl_is_single) 10407 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT; 10408 /* 16 bit addressing */ 10409 if (port_type == CTL_PORT_SCSI) 10410 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10411 /* XXX set the SID_MultiP bit here if we're actually going to 10412 respond on multiple ports */ 10413 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10414 10415 /* 16 bit data bus, synchronous transfers */ 10416 if (port_type == CTL_PORT_SCSI) 10417 inq_ptr->flags = SID_WBus16 | SID_Sync; 10418 /* 10419 * XXX KDM do we want to support tagged queueing on the control 10420 * device at all? 10421 */ 10422 if ((lun == NULL) 10423 || (lun->be_lun->lun_type != T_PROCESSOR)) 10424 inq_ptr->flags |= SID_CmdQue; 10425 /* 10426 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10427 * We have 8 bytes for the vendor name, and 16 bytes for the device 10428 * name and 4 bytes for the revision. 10429 */ 10430 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10431 "vendor")) == NULL) { 10432 strcpy(inq_ptr->vendor, CTL_VENDOR); 10433 } else { 10434 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10435 strncpy(inq_ptr->vendor, val, 10436 min(sizeof(inq_ptr->vendor), strlen(val))); 10437 } 10438 if (lun == NULL) { 10439 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10440 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10441 switch (lun->be_lun->lun_type) { 10442 case T_DIRECT: 10443 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10444 break; 10445 case T_PROCESSOR: 10446 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10447 break; 10448 default: 10449 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10450 break; 10451 } 10452 } else { 10453 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10454 strncpy(inq_ptr->product, val, 10455 min(sizeof(inq_ptr->product), strlen(val))); 10456 } 10457 10458 /* 10459 * XXX make this a macro somewhere so it automatically gets 10460 * incremented when we make changes. 10461 */ 10462 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10463 "revision")) == NULL) { 10464 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10465 } else { 10466 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10467 strncpy(inq_ptr->revision, val, 10468 min(sizeof(inq_ptr->revision), strlen(val))); 10469 } 10470 10471 /* 10472 * For parallel SCSI, we support double transition and single 10473 * transition clocking. We also support QAS (Quick Arbitration 10474 * and Selection) and Information Unit transfers on both the 10475 * control and array devices. 10476 */ 10477 if (port_type == CTL_PORT_SCSI) 10478 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10479 SID_SPI_IUS; 10480 10481 /* SAM-5 (no version claimed) */ 10482 scsi_ulto2b(0x00A0, inq_ptr->version1); 10483 /* SPC-4 (no version claimed) */ 10484 scsi_ulto2b(0x0460, inq_ptr->version2); 10485 if (port_type == CTL_PORT_FC) { 10486 /* FCP-2 ANSI INCITS.350:2003 */ 10487 scsi_ulto2b(0x0917, inq_ptr->version3); 10488 } else if (port_type == CTL_PORT_SCSI) { 10489 /* SPI-4 ANSI INCITS.362:200x */ 10490 scsi_ulto2b(0x0B56, inq_ptr->version3); 10491 } else if (port_type == CTL_PORT_ISCSI) { 10492 /* iSCSI (no version claimed) */ 10493 scsi_ulto2b(0x0960, inq_ptr->version3); 10494 } else if (port_type == CTL_PORT_SAS) { 10495 /* SAS (no version claimed) */ 10496 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10497 } 10498 10499 if (lun == NULL) { 10500 /* SBC-3 (no version claimed) */ 10501 scsi_ulto2b(0x04C0, inq_ptr->version4); 10502 } else { 10503 switch (lun->be_lun->lun_type) { 10504 case T_DIRECT: 10505 /* SBC-3 (no version claimed) */ 10506 scsi_ulto2b(0x04C0, inq_ptr->version4); 10507 break; 10508 case T_PROCESSOR: 10509 default: 10510 break; 10511 } 10512 } 10513 10514 ctsio->scsi_status = SCSI_STATUS_OK; 10515 if (ctsio->kern_data_len > 0) { 10516 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10517 ctsio->be_move_done = ctl_config_move_done; 10518 ctl_datamove((union ctl_io *)ctsio); 10519 } else { 10520 ctsio->io_hdr.status = CTL_SUCCESS; 10521 ctl_done((union ctl_io *)ctsio); 10522 } 10523 10524 return (CTL_RETVAL_COMPLETE); 10525} 10526 10527int 10528ctl_inquiry(struct ctl_scsiio *ctsio) 10529{ 10530 struct scsi_inquiry *cdb; 10531 int retval; 10532 10533 cdb = (struct scsi_inquiry *)ctsio->cdb; 10534 10535 retval = 0; 10536 10537 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10538 10539 /* 10540 * Right now, we don't support the CmdDt inquiry information. 10541 * This would be nice to support in the future. When we do 10542 * support it, we should change this test so that it checks to make 10543 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10544 */ 10545#ifdef notyet 10546 if (((cdb->byte2 & SI_EVPD) 10547 && (cdb->byte2 & SI_CMDDT))) 10548#endif 10549 if (cdb->byte2 & SI_CMDDT) { 10550 /* 10551 * Point to the SI_CMDDT bit. We might change this 10552 * when we support SI_CMDDT, but since both bits would be 10553 * "wrong", this should probably just stay as-is then. 10554 */ 10555 ctl_set_invalid_field(ctsio, 10556 /*sks_valid*/ 1, 10557 /*command*/ 1, 10558 /*field*/ 1, 10559 /*bit_valid*/ 1, 10560 /*bit*/ 1); 10561 ctl_done((union ctl_io *)ctsio); 10562 return (CTL_RETVAL_COMPLETE); 10563 } 10564 if (cdb->byte2 & SI_EVPD) 10565 retval = ctl_inquiry_evpd(ctsio); 10566#ifdef notyet 10567 else if (cdb->byte2 & SI_CMDDT) 10568 retval = ctl_inquiry_cmddt(ctsio); 10569#endif 10570 else 10571 retval = ctl_inquiry_std(ctsio); 10572 10573 return (retval); 10574} 10575 10576/* 10577 * For known CDB types, parse the LBA and length. 10578 */ 10579static int 10580ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10581{ 10582 if (io->io_hdr.io_type != CTL_IO_SCSI) 10583 return (1); 10584 10585 switch (io->scsiio.cdb[0]) { 10586 case COMPARE_AND_WRITE: { 10587 struct scsi_compare_and_write *cdb; 10588 10589 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10590 10591 *lba = scsi_8btou64(cdb->addr); 10592 *len = cdb->length; 10593 break; 10594 } 10595 case READ_6: 10596 case WRITE_6: { 10597 struct scsi_rw_6 *cdb; 10598 10599 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10600 10601 *lba = scsi_3btoul(cdb->addr); 10602 /* only 5 bits are valid in the most significant address byte */ 10603 *lba &= 0x1fffff; 10604 *len = cdb->length; 10605 break; 10606 } 10607 case READ_10: 10608 case WRITE_10: { 10609 struct scsi_rw_10 *cdb; 10610 10611 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10612 10613 *lba = scsi_4btoul(cdb->addr); 10614 *len = scsi_2btoul(cdb->length); 10615 break; 10616 } 10617 case WRITE_VERIFY_10: { 10618 struct scsi_write_verify_10 *cdb; 10619 10620 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10621 10622 *lba = scsi_4btoul(cdb->addr); 10623 *len = scsi_2btoul(cdb->length); 10624 break; 10625 } 10626 case READ_12: 10627 case WRITE_12: { 10628 struct scsi_rw_12 *cdb; 10629 10630 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10631 10632 *lba = scsi_4btoul(cdb->addr); 10633 *len = scsi_4btoul(cdb->length); 10634 break; 10635 } 10636 case WRITE_VERIFY_12: { 10637 struct scsi_write_verify_12 *cdb; 10638 10639 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10640 10641 *lba = scsi_4btoul(cdb->addr); 10642 *len = scsi_4btoul(cdb->length); 10643 break; 10644 } 10645 case READ_16: 10646 case WRITE_16: { 10647 struct scsi_rw_16 *cdb; 10648 10649 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10650 10651 *lba = scsi_8btou64(cdb->addr); 10652 *len = scsi_4btoul(cdb->length); 10653 break; 10654 } 10655 case WRITE_VERIFY_16: { 10656 struct scsi_write_verify_16 *cdb; 10657 10658 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10659 10660 10661 *lba = scsi_8btou64(cdb->addr); 10662 *len = scsi_4btoul(cdb->length); 10663 break; 10664 } 10665 case WRITE_SAME_10: { 10666 struct scsi_write_same_10 *cdb; 10667 10668 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10669 10670 *lba = scsi_4btoul(cdb->addr); 10671 *len = scsi_2btoul(cdb->length); 10672 break; 10673 } 10674 case WRITE_SAME_16: { 10675 struct scsi_write_same_16 *cdb; 10676 10677 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10678 10679 *lba = scsi_8btou64(cdb->addr); 10680 *len = scsi_4btoul(cdb->length); 10681 break; 10682 } 10683 case VERIFY_10: { 10684 struct scsi_verify_10 *cdb; 10685 10686 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10687 10688 *lba = scsi_4btoul(cdb->addr); 10689 *len = scsi_2btoul(cdb->length); 10690 break; 10691 } 10692 case VERIFY_12: { 10693 struct scsi_verify_12 *cdb; 10694 10695 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10696 10697 *lba = scsi_4btoul(cdb->addr); 10698 *len = scsi_4btoul(cdb->length); 10699 break; 10700 } 10701 case VERIFY_16: { 10702 struct scsi_verify_16 *cdb; 10703 10704 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10705 10706 *lba = scsi_8btou64(cdb->addr); 10707 *len = scsi_4btoul(cdb->length); 10708 break; 10709 } 10710 default: 10711 return (1); 10712 break; /* NOTREACHED */ 10713 } 10714 10715 return (0); 10716} 10717 10718static ctl_action 10719ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10720{ 10721 uint64_t endlba1, endlba2; 10722 10723 endlba1 = lba1 + len1 - 1; 10724 endlba2 = lba2 + len2 - 1; 10725 10726 if ((endlba1 < lba2) 10727 || (endlba2 < lba1)) 10728 return (CTL_ACTION_PASS); 10729 else 10730 return (CTL_ACTION_BLOCK); 10731} 10732 10733static ctl_action 10734ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10735{ 10736 uint64_t lba1, lba2; 10737 uint32_t len1, len2; 10738 int retval; 10739 10740 retval = ctl_get_lba_len(io1, &lba1, &len1); 10741 if (retval != 0) 10742 return (CTL_ACTION_ERROR); 10743 10744 retval = ctl_get_lba_len(io2, &lba2, &len2); 10745 if (retval != 0) 10746 return (CTL_ACTION_ERROR); 10747 10748 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10749} 10750 10751static ctl_action 10752ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10753{ 10754 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10755 ctl_serialize_action *serialize_row; 10756 10757 /* 10758 * The initiator attempted multiple untagged commands at the same 10759 * time. Can't do that. 10760 */ 10761 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10762 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10763 && ((pending_io->io_hdr.nexus.targ_port == 10764 ooa_io->io_hdr.nexus.targ_port) 10765 && (pending_io->io_hdr.nexus.initid.id == 10766 ooa_io->io_hdr.nexus.initid.id)) 10767 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10768 return (CTL_ACTION_OVERLAP); 10769 10770 /* 10771 * The initiator attempted to send multiple tagged commands with 10772 * the same ID. (It's fine if different initiators have the same 10773 * tag ID.) 10774 * 10775 * Even if all of those conditions are true, we don't kill the I/O 10776 * if the command ahead of us has been aborted. We won't end up 10777 * sending it to the FETD, and it's perfectly legal to resend a 10778 * command with the same tag number as long as the previous 10779 * instance of this tag number has been aborted somehow. 10780 */ 10781 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10782 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10783 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10784 && ((pending_io->io_hdr.nexus.targ_port == 10785 ooa_io->io_hdr.nexus.targ_port) 10786 && (pending_io->io_hdr.nexus.initid.id == 10787 ooa_io->io_hdr.nexus.initid.id)) 10788 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10789 return (CTL_ACTION_OVERLAP_TAG); 10790 10791 /* 10792 * If we get a head of queue tag, SAM-3 says that we should 10793 * immediately execute it. 10794 * 10795 * What happens if this command would normally block for some other 10796 * reason? e.g. a request sense with a head of queue tag 10797 * immediately after a write. Normally that would block, but this 10798 * will result in its getting executed immediately... 10799 * 10800 * We currently return "pass" instead of "skip", so we'll end up 10801 * going through the rest of the queue to check for overlapped tags. 10802 * 10803 * XXX KDM check for other types of blockage first?? 10804 */ 10805 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10806 return (CTL_ACTION_PASS); 10807 10808 /* 10809 * Ordered tags have to block until all items ahead of them 10810 * have completed. If we get called with an ordered tag, we always 10811 * block, if something else is ahead of us in the queue. 10812 */ 10813 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10814 return (CTL_ACTION_BLOCK); 10815 10816 /* 10817 * Simple tags get blocked until all head of queue and ordered tags 10818 * ahead of them have completed. I'm lumping untagged commands in 10819 * with simple tags here. XXX KDM is that the right thing to do? 10820 */ 10821 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10822 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10823 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10824 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10825 return (CTL_ACTION_BLOCK); 10826 10827 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10828 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10829 10830 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10831 10832 switch (serialize_row[pending_entry->seridx]) { 10833 case CTL_SER_BLOCK: 10834 return (CTL_ACTION_BLOCK); 10835 break; /* NOTREACHED */ 10836 case CTL_SER_EXTENT: 10837 return (ctl_extent_check(pending_io, ooa_io)); 10838 break; /* NOTREACHED */ 10839 case CTL_SER_PASS: 10840 return (CTL_ACTION_PASS); 10841 break; /* NOTREACHED */ 10842 case CTL_SER_SKIP: 10843 return (CTL_ACTION_SKIP); 10844 break; 10845 default: 10846 panic("invalid serialization value %d", 10847 serialize_row[pending_entry->seridx]); 10848 break; /* NOTREACHED */ 10849 } 10850 10851 return (CTL_ACTION_ERROR); 10852} 10853 10854/* 10855 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10856 * Assumptions: 10857 * - pending_io is generally either incoming, or on the blocked queue 10858 * - starting I/O is the I/O we want to start the check with. 10859 */ 10860static ctl_action 10861ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10862 union ctl_io *starting_io) 10863{ 10864 union ctl_io *ooa_io; 10865 ctl_action action; 10866 10867 mtx_assert(&lun->lun_lock, MA_OWNED); 10868 10869 /* 10870 * Run back along the OOA queue, starting with the current 10871 * blocked I/O and going through every I/O before it on the 10872 * queue. If starting_io is NULL, we'll just end up returning 10873 * CTL_ACTION_PASS. 10874 */ 10875 for (ooa_io = starting_io; ooa_io != NULL; 10876 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10877 ooa_links)){ 10878 10879 /* 10880 * This routine just checks to see whether 10881 * cur_blocked is blocked by ooa_io, which is ahead 10882 * of it in the queue. It doesn't queue/dequeue 10883 * cur_blocked. 10884 */ 10885 action = ctl_check_for_blockage(pending_io, ooa_io); 10886 switch (action) { 10887 case CTL_ACTION_BLOCK: 10888 case CTL_ACTION_OVERLAP: 10889 case CTL_ACTION_OVERLAP_TAG: 10890 case CTL_ACTION_SKIP: 10891 case CTL_ACTION_ERROR: 10892 return (action); 10893 break; /* NOTREACHED */ 10894 case CTL_ACTION_PASS: 10895 break; 10896 default: 10897 panic("invalid action %d", action); 10898 break; /* NOTREACHED */ 10899 } 10900 } 10901 10902 return (CTL_ACTION_PASS); 10903} 10904 10905/* 10906 * Assumptions: 10907 * - An I/O has just completed, and has been removed from the per-LUN OOA 10908 * queue, so some items on the blocked queue may now be unblocked. 10909 */ 10910static int 10911ctl_check_blocked(struct ctl_lun *lun) 10912{ 10913 union ctl_io *cur_blocked, *next_blocked; 10914 10915 mtx_assert(&lun->lun_lock, MA_OWNED); 10916 10917 /* 10918 * Run forward from the head of the blocked queue, checking each 10919 * entry against the I/Os prior to it on the OOA queue to see if 10920 * there is still any blockage. 10921 * 10922 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10923 * with our removing a variable on it while it is traversing the 10924 * list. 10925 */ 10926 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10927 cur_blocked != NULL; cur_blocked = next_blocked) { 10928 union ctl_io *prev_ooa; 10929 ctl_action action; 10930 10931 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10932 blocked_links); 10933 10934 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10935 ctl_ooaq, ooa_links); 10936 10937 /* 10938 * If cur_blocked happens to be the first item in the OOA 10939 * queue now, prev_ooa will be NULL, and the action 10940 * returned will just be CTL_ACTION_PASS. 10941 */ 10942 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10943 10944 switch (action) { 10945 case CTL_ACTION_BLOCK: 10946 /* Nothing to do here, still blocked */ 10947 break; 10948 case CTL_ACTION_OVERLAP: 10949 case CTL_ACTION_OVERLAP_TAG: 10950 /* 10951 * This shouldn't happen! In theory we've already 10952 * checked this command for overlap... 10953 */ 10954 break; 10955 case CTL_ACTION_PASS: 10956 case CTL_ACTION_SKIP: { 10957 struct ctl_softc *softc; 10958 const struct ctl_cmd_entry *entry; 10959 uint32_t initidx; 10960 int isc_retval; 10961 10962 /* 10963 * The skip case shouldn't happen, this transaction 10964 * should have never made it onto the blocked queue. 10965 */ 10966 /* 10967 * This I/O is no longer blocked, we can remove it 10968 * from the blocked queue. Since this is a TAILQ 10969 * (doubly linked list), we can do O(1) removals 10970 * from any place on the list. 10971 */ 10972 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10973 blocked_links); 10974 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10975 10976 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10977 /* 10978 * Need to send IO back to original side to 10979 * run 10980 */ 10981 union ctl_ha_msg msg_info; 10982 10983 msg_info.hdr.original_sc = 10984 cur_blocked->io_hdr.original_sc; 10985 msg_info.hdr.serializing_sc = cur_blocked; 10986 msg_info.hdr.msg_type = CTL_MSG_R2R; 10987 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10988 &msg_info, sizeof(msg_info), 0)) > 10989 CTL_HA_STATUS_SUCCESS) { 10990 printf("CTL:Check Blocked error from " 10991 "ctl_ha_msg_send %d\n", 10992 isc_retval); 10993 } 10994 break; 10995 } 10996 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 10997 softc = control_softc; 10998 10999 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11000 11001 /* 11002 * Check this I/O for LUN state changes that may 11003 * have happened while this command was blocked. 11004 * The LUN state may have been changed by a command 11005 * ahead of us in the queue, so we need to re-check 11006 * for any states that can be caused by SCSI 11007 * commands. 11008 */ 11009 if (ctl_scsiio_lun_check(softc, lun, entry, 11010 &cur_blocked->scsiio) == 0) { 11011 cur_blocked->io_hdr.flags |= 11012 CTL_FLAG_IS_WAS_ON_RTR; 11013 ctl_enqueue_rtr(cur_blocked); 11014 } else 11015 ctl_done(cur_blocked); 11016 break; 11017 } 11018 default: 11019 /* 11020 * This probably shouldn't happen -- we shouldn't 11021 * get CTL_ACTION_ERROR, or anything else. 11022 */ 11023 break; 11024 } 11025 } 11026 11027 return (CTL_RETVAL_COMPLETE); 11028} 11029 11030/* 11031 * This routine (with one exception) checks LUN flags that can be set by 11032 * commands ahead of us in the OOA queue. These flags have to be checked 11033 * when a command initially comes in, and when we pull a command off the 11034 * blocked queue and are preparing to execute it. The reason we have to 11035 * check these flags for commands on the blocked queue is that the LUN 11036 * state may have been changed by a command ahead of us while we're on the 11037 * blocked queue. 11038 * 11039 * Ordering is somewhat important with these checks, so please pay 11040 * careful attention to the placement of any new checks. 11041 */ 11042static int 11043ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11044 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11045{ 11046 int retval; 11047 11048 retval = 0; 11049 11050 mtx_assert(&lun->lun_lock, MA_OWNED); 11051 11052 /* 11053 * If this shelf is a secondary shelf controller, we have to reject 11054 * any media access commands. 11055 */ 11056#if 0 11057 /* No longer needed for HA */ 11058 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11059 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11060 ctl_set_lun_standby(ctsio); 11061 retval = 1; 11062 goto bailout; 11063 } 11064#endif 11065 11066 /* 11067 * Check for a reservation conflict. If this command isn't allowed 11068 * even on reserved LUNs, and if this initiator isn't the one who 11069 * reserved us, reject the command with a reservation conflict. 11070 */ 11071 if ((lun->flags & CTL_LUN_RESERVED) 11072 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11073 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11074 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11075 || (ctsio->io_hdr.nexus.targ_target.id != 11076 lun->rsv_nexus.targ_target.id)) { 11077 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11078 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11079 retval = 1; 11080 goto bailout; 11081 } 11082 } 11083 11084 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11085 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11086 uint32_t residx; 11087 11088 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11089 /* 11090 * if we aren't registered or it's a res holder type 11091 * reservation and this isn't the res holder then set a 11092 * conflict. 11093 * NOTE: Commands which might be allowed on write exclusive 11094 * type reservations are checked in the particular command 11095 * for a conflict. Read and SSU are the only ones. 11096 */ 11097 if (!lun->per_res[residx].registered 11098 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11099 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11100 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11101 retval = 1; 11102 goto bailout; 11103 } 11104 11105 } 11106 11107 if ((lun->flags & CTL_LUN_OFFLINE) 11108 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11109 ctl_set_lun_not_ready(ctsio); 11110 retval = 1; 11111 goto bailout; 11112 } 11113 11114 /* 11115 * If the LUN is stopped, see if this particular command is allowed 11116 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11117 */ 11118 if ((lun->flags & CTL_LUN_STOPPED) 11119 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11120 /* "Logical unit not ready, initializing cmd. required" */ 11121 ctl_set_lun_stopped(ctsio); 11122 retval = 1; 11123 goto bailout; 11124 } 11125 11126 if ((lun->flags & CTL_LUN_INOPERABLE) 11127 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11128 /* "Medium format corrupted" */ 11129 ctl_set_medium_format_corrupted(ctsio); 11130 retval = 1; 11131 goto bailout; 11132 } 11133 11134bailout: 11135 return (retval); 11136 11137} 11138 11139static void 11140ctl_failover_io(union ctl_io *io, int have_lock) 11141{ 11142 ctl_set_busy(&io->scsiio); 11143 ctl_done(io); 11144} 11145 11146static void 11147ctl_failover(void) 11148{ 11149 struct ctl_lun *lun; 11150 struct ctl_softc *ctl_softc; 11151 union ctl_io *next_io, *pending_io; 11152 union ctl_io *io; 11153 int lun_idx; 11154 int i; 11155 11156 ctl_softc = control_softc; 11157 11158 mtx_lock(&ctl_softc->ctl_lock); 11159 /* 11160 * Remove any cmds from the other SC from the rtr queue. These 11161 * will obviously only be for LUNs for which we're the primary. 11162 * We can't send status or get/send data for these commands. 11163 * Since they haven't been executed yet, we can just remove them. 11164 * We'll either abort them or delete them below, depending on 11165 * which HA mode we're in. 11166 */ 11167#ifdef notyet 11168 mtx_lock(&ctl_softc->queue_lock); 11169 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11170 io != NULL; io = next_io) { 11171 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11172 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11173 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11174 ctl_io_hdr, links); 11175 } 11176 mtx_unlock(&ctl_softc->queue_lock); 11177#endif 11178 11179 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11180 lun = ctl_softc->ctl_luns[lun_idx]; 11181 if (lun==NULL) 11182 continue; 11183 11184 /* 11185 * Processor LUNs are primary on both sides. 11186 * XXX will this always be true? 11187 */ 11188 if (lun->be_lun->lun_type == T_PROCESSOR) 11189 continue; 11190 11191 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11192 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11193 printf("FAILOVER: primary lun %d\n", lun_idx); 11194 /* 11195 * Remove all commands from the other SC. First from the 11196 * blocked queue then from the ooa queue. Once we have 11197 * removed them. Call ctl_check_blocked to see if there 11198 * is anything that can run. 11199 */ 11200 for (io = (union ctl_io *)TAILQ_FIRST( 11201 &lun->blocked_queue); io != NULL; io = next_io) { 11202 11203 next_io = (union ctl_io *)TAILQ_NEXT( 11204 &io->io_hdr, blocked_links); 11205 11206 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11207 TAILQ_REMOVE(&lun->blocked_queue, 11208 &io->io_hdr,blocked_links); 11209 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11210 TAILQ_REMOVE(&lun->ooa_queue, 11211 &io->io_hdr, ooa_links); 11212 11213 ctl_free_io(io); 11214 } 11215 } 11216 11217 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11218 io != NULL; io = next_io) { 11219 11220 next_io = (union ctl_io *)TAILQ_NEXT( 11221 &io->io_hdr, ooa_links); 11222 11223 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11224 11225 TAILQ_REMOVE(&lun->ooa_queue, 11226 &io->io_hdr, 11227 ooa_links); 11228 11229 ctl_free_io(io); 11230 } 11231 } 11232 ctl_check_blocked(lun); 11233 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11234 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11235 11236 printf("FAILOVER: primary lun %d\n", lun_idx); 11237 /* 11238 * Abort all commands from the other SC. We can't 11239 * send status back for them now. These should get 11240 * cleaned up when they are completed or come out 11241 * for a datamove operation. 11242 */ 11243 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11244 io != NULL; io = next_io) { 11245 next_io = (union ctl_io *)TAILQ_NEXT( 11246 &io->io_hdr, ooa_links); 11247 11248 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11249 io->io_hdr.flags |= CTL_FLAG_ABORT; 11250 } 11251 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11252 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11253 11254 printf("FAILOVER: secondary lun %d\n", lun_idx); 11255 11256 lun->flags |= CTL_LUN_PRIMARY_SC; 11257 11258 /* 11259 * We send all I/O that was sent to this controller 11260 * and redirected to the other side back with 11261 * busy status, and have the initiator retry it. 11262 * Figuring out how much data has been transferred, 11263 * etc. and picking up where we left off would be 11264 * very tricky. 11265 * 11266 * XXX KDM need to remove I/O from the blocked 11267 * queue as well! 11268 */ 11269 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11270 &lun->ooa_queue); pending_io != NULL; 11271 pending_io = next_io) { 11272 11273 next_io = (union ctl_io *)TAILQ_NEXT( 11274 &pending_io->io_hdr, ooa_links); 11275 11276 pending_io->io_hdr.flags &= 11277 ~CTL_FLAG_SENT_2OTHER_SC; 11278 11279 if (pending_io->io_hdr.flags & 11280 CTL_FLAG_IO_ACTIVE) { 11281 pending_io->io_hdr.flags |= 11282 CTL_FLAG_FAILOVER; 11283 } else { 11284 ctl_set_busy(&pending_io->scsiio); 11285 ctl_done(pending_io); 11286 } 11287 } 11288 11289 /* 11290 * Build Unit Attention 11291 */ 11292 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11293 lun->pending_ua[i] |= 11294 CTL_UA_ASYM_ACC_CHANGE; 11295 } 11296 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11297 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11298 printf("FAILOVER: secondary lun %d\n", lun_idx); 11299 /* 11300 * if the first io on the OOA is not on the RtR queue 11301 * add it. 11302 */ 11303 lun->flags |= CTL_LUN_PRIMARY_SC; 11304 11305 pending_io = (union ctl_io *)TAILQ_FIRST( 11306 &lun->ooa_queue); 11307 if (pending_io==NULL) { 11308 printf("Nothing on OOA queue\n"); 11309 continue; 11310 } 11311 11312 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11313 if ((pending_io->io_hdr.flags & 11314 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11315 pending_io->io_hdr.flags |= 11316 CTL_FLAG_IS_WAS_ON_RTR; 11317 ctl_enqueue_rtr(pending_io); 11318 } 11319#if 0 11320 else 11321 { 11322 printf("Tag 0x%04x is running\n", 11323 pending_io->scsiio.tag_num); 11324 } 11325#endif 11326 11327 next_io = (union ctl_io *)TAILQ_NEXT( 11328 &pending_io->io_hdr, ooa_links); 11329 for (pending_io=next_io; pending_io != NULL; 11330 pending_io = next_io) { 11331 pending_io->io_hdr.flags &= 11332 ~CTL_FLAG_SENT_2OTHER_SC; 11333 next_io = (union ctl_io *)TAILQ_NEXT( 11334 &pending_io->io_hdr, ooa_links); 11335 if (pending_io->io_hdr.flags & 11336 CTL_FLAG_IS_WAS_ON_RTR) { 11337#if 0 11338 printf("Tag 0x%04x is running\n", 11339 pending_io->scsiio.tag_num); 11340#endif 11341 continue; 11342 } 11343 11344 switch (ctl_check_ooa(lun, pending_io, 11345 (union ctl_io *)TAILQ_PREV( 11346 &pending_io->io_hdr, ctl_ooaq, 11347 ooa_links))) { 11348 11349 case CTL_ACTION_BLOCK: 11350 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11351 &pending_io->io_hdr, 11352 blocked_links); 11353 pending_io->io_hdr.flags |= 11354 CTL_FLAG_BLOCKED; 11355 break; 11356 case CTL_ACTION_PASS: 11357 case CTL_ACTION_SKIP: 11358 pending_io->io_hdr.flags |= 11359 CTL_FLAG_IS_WAS_ON_RTR; 11360 ctl_enqueue_rtr(pending_io); 11361 break; 11362 case CTL_ACTION_OVERLAP: 11363 ctl_set_overlapped_cmd( 11364 (struct ctl_scsiio *)pending_io); 11365 ctl_done(pending_io); 11366 break; 11367 case CTL_ACTION_OVERLAP_TAG: 11368 ctl_set_overlapped_tag( 11369 (struct ctl_scsiio *)pending_io, 11370 pending_io->scsiio.tag_num & 0xff); 11371 ctl_done(pending_io); 11372 break; 11373 case CTL_ACTION_ERROR: 11374 default: 11375 ctl_set_internal_failure( 11376 (struct ctl_scsiio *)pending_io, 11377 0, // sks_valid 11378 0); //retry count 11379 ctl_done(pending_io); 11380 break; 11381 } 11382 } 11383 11384 /* 11385 * Build Unit Attention 11386 */ 11387 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11388 lun->pending_ua[i] |= 11389 CTL_UA_ASYM_ACC_CHANGE; 11390 } 11391 } else { 11392 panic("Unhandled HA mode failover, LUN flags = %#x, " 11393 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11394 } 11395 } 11396 ctl_pause_rtr = 0; 11397 mtx_unlock(&ctl_softc->ctl_lock); 11398} 11399 11400static int 11401ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11402{ 11403 struct ctl_lun *lun; 11404 const struct ctl_cmd_entry *entry; 11405 uint32_t initidx, targ_lun; 11406 int retval; 11407 11408 retval = 0; 11409 11410 lun = NULL; 11411 11412 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11413 if ((targ_lun < CTL_MAX_LUNS) 11414 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11415 lun = ctl_softc->ctl_luns[targ_lun]; 11416 /* 11417 * If the LUN is invalid, pretend that it doesn't exist. 11418 * It will go away as soon as all pending I/O has been 11419 * completed. 11420 */ 11421 if (lun->flags & CTL_LUN_DISABLED) { 11422 lun = NULL; 11423 } else { 11424 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11425 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11426 lun->be_lun; 11427 if (lun->be_lun->lun_type == T_PROCESSOR) { 11428 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11429 } 11430 11431 /* 11432 * Every I/O goes into the OOA queue for a 11433 * particular LUN, and stays there until completion. 11434 */ 11435 mtx_lock(&lun->lun_lock); 11436 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11437 ooa_links); 11438 } 11439 } else { 11440 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11441 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11442 } 11443 11444 /* Get command entry and return error if it is unsuppotyed. */ 11445 entry = ctl_validate_command(ctsio); 11446 if (entry == NULL) { 11447 if (lun) 11448 mtx_unlock(&lun->lun_lock); 11449 return (retval); 11450 } 11451 11452 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11453 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11454 11455 /* 11456 * Check to see whether we can send this command to LUNs that don't 11457 * exist. This should pretty much only be the case for inquiry 11458 * and request sense. Further checks, below, really require having 11459 * a LUN, so we can't really check the command anymore. Just put 11460 * it on the rtr queue. 11461 */ 11462 if (lun == NULL) { 11463 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11464 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11465 ctl_enqueue_rtr((union ctl_io *)ctsio); 11466 return (retval); 11467 } 11468 11469 ctl_set_unsupported_lun(ctsio); 11470 ctl_done((union ctl_io *)ctsio); 11471 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11472 return (retval); 11473 } else { 11474 /* 11475 * Make sure we support this particular command on this LUN. 11476 * e.g., we don't support writes to the control LUN. 11477 */ 11478 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11479 mtx_unlock(&lun->lun_lock); 11480 ctl_set_invalid_opcode(ctsio); 11481 ctl_done((union ctl_io *)ctsio); 11482 return (retval); 11483 } 11484 } 11485 11486 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11487 11488#ifdef CTL_WITH_CA 11489 /* 11490 * If we've got a request sense, it'll clear the contingent 11491 * allegiance condition. Otherwise, if we have a CA condition for 11492 * this initiator, clear it, because it sent down a command other 11493 * than request sense. 11494 */ 11495 if ((ctsio->cdb[0] != REQUEST_SENSE) 11496 && (ctl_is_set(lun->have_ca, initidx))) 11497 ctl_clear_mask(lun->have_ca, initidx); 11498#endif 11499 11500 /* 11501 * If the command has this flag set, it handles its own unit 11502 * attention reporting, we shouldn't do anything. Otherwise we 11503 * check for any pending unit attentions, and send them back to the 11504 * initiator. We only do this when a command initially comes in, 11505 * not when we pull it off the blocked queue. 11506 * 11507 * According to SAM-3, section 5.3.2, the order that things get 11508 * presented back to the host is basically unit attentions caused 11509 * by some sort of reset event, busy status, reservation conflicts 11510 * or task set full, and finally any other status. 11511 * 11512 * One issue here is that some of the unit attentions we report 11513 * don't fall into the "reset" category (e.g. "reported luns data 11514 * has changed"). So reporting it here, before the reservation 11515 * check, may be technically wrong. I guess the only thing to do 11516 * would be to check for and report the reset events here, and then 11517 * check for the other unit attention types after we check for a 11518 * reservation conflict. 11519 * 11520 * XXX KDM need to fix this 11521 */ 11522 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11523 ctl_ua_type ua_type; 11524 11525 ua_type = lun->pending_ua[initidx]; 11526 if (ua_type != CTL_UA_NONE) { 11527 scsi_sense_data_type sense_format; 11528 11529 if (lun != NULL) 11530 sense_format = (lun->flags & 11531 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11532 SSD_TYPE_FIXED; 11533 else 11534 sense_format = SSD_TYPE_FIXED; 11535 11536 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11537 sense_format); 11538 if (ua_type != CTL_UA_NONE) { 11539 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11540 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11541 CTL_AUTOSENSE; 11542 ctsio->sense_len = SSD_FULL_SIZE; 11543 lun->pending_ua[initidx] &= ~ua_type; 11544 mtx_unlock(&lun->lun_lock); 11545 ctl_done((union ctl_io *)ctsio); 11546 return (retval); 11547 } 11548 } 11549 } 11550 11551 11552 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11553 mtx_unlock(&lun->lun_lock); 11554 ctl_done((union ctl_io *)ctsio); 11555 return (retval); 11556 } 11557 11558 /* 11559 * XXX CHD this is where we want to send IO to other side if 11560 * this LUN is secondary on this SC. We will need to make a copy 11561 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11562 * the copy we send as FROM_OTHER. 11563 * We also need to stuff the address of the original IO so we can 11564 * find it easily. Something similar will need be done on the other 11565 * side so when we are done we can find the copy. 11566 */ 11567 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11568 union ctl_ha_msg msg_info; 11569 int isc_retval; 11570 11571 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11572 11573 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11574 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11575#if 0 11576 printf("1. ctsio %p\n", ctsio); 11577#endif 11578 msg_info.hdr.serializing_sc = NULL; 11579 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11580 msg_info.scsi.tag_num = ctsio->tag_num; 11581 msg_info.scsi.tag_type = ctsio->tag_type; 11582 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11583 11584 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11585 11586 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11587 (void *)&msg_info, sizeof(msg_info), 0)) > 11588 CTL_HA_STATUS_SUCCESS) { 11589 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11590 isc_retval); 11591 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11592 } else { 11593#if 0 11594 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11595#endif 11596 } 11597 11598 /* 11599 * XXX KDM this I/O is off the incoming queue, but hasn't 11600 * been inserted on any other queue. We may need to come 11601 * up with a holding queue while we wait for serialization 11602 * so that we have an idea of what we're waiting for from 11603 * the other side. 11604 */ 11605 mtx_unlock(&lun->lun_lock); 11606 return (retval); 11607 } 11608 11609 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11610 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11611 ctl_ooaq, ooa_links))) { 11612 case CTL_ACTION_BLOCK: 11613 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11614 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11615 blocked_links); 11616 mtx_unlock(&lun->lun_lock); 11617 return (retval); 11618 case CTL_ACTION_PASS: 11619 case CTL_ACTION_SKIP: 11620 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11621 mtx_unlock(&lun->lun_lock); 11622 ctl_enqueue_rtr((union ctl_io *)ctsio); 11623 break; 11624 case CTL_ACTION_OVERLAP: 11625 mtx_unlock(&lun->lun_lock); 11626 ctl_set_overlapped_cmd(ctsio); 11627 ctl_done((union ctl_io *)ctsio); 11628 break; 11629 case CTL_ACTION_OVERLAP_TAG: 11630 mtx_unlock(&lun->lun_lock); 11631 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11632 ctl_done((union ctl_io *)ctsio); 11633 break; 11634 case CTL_ACTION_ERROR: 11635 default: 11636 mtx_unlock(&lun->lun_lock); 11637 ctl_set_internal_failure(ctsio, 11638 /*sks_valid*/ 0, 11639 /*retry_count*/ 0); 11640 ctl_done((union ctl_io *)ctsio); 11641 break; 11642 } 11643 return (retval); 11644} 11645 11646const struct ctl_cmd_entry * 11647ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11648{ 11649 const struct ctl_cmd_entry *entry; 11650 int service_action; 11651 11652 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11653 if (entry->flags & CTL_CMD_FLAG_SA5) { 11654 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11655 entry = &((const struct ctl_cmd_entry *) 11656 entry->execute)[service_action]; 11657 } 11658 return (entry); 11659} 11660 11661const struct ctl_cmd_entry * 11662ctl_validate_command(struct ctl_scsiio *ctsio) 11663{ 11664 const struct ctl_cmd_entry *entry; 11665 int i; 11666 uint8_t diff; 11667 11668 entry = ctl_get_cmd_entry(ctsio); 11669 if (entry->execute == NULL) { 11670 ctl_set_invalid_opcode(ctsio); 11671 ctl_done((union ctl_io *)ctsio); 11672 return (NULL); 11673 } 11674 KASSERT(entry->length > 0, 11675 ("Not defined length for command 0x%02x/0x%02x", 11676 ctsio->cdb[0], ctsio->cdb[1])); 11677 for (i = 1; i < entry->length; i++) { 11678 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11679 if (diff == 0) 11680 continue; 11681 ctl_set_invalid_field(ctsio, 11682 /*sks_valid*/ 1, 11683 /*command*/ 1, 11684 /*field*/ i, 11685 /*bit_valid*/ 1, 11686 /*bit*/ fls(diff) - 1); 11687 ctl_done((union ctl_io *)ctsio); 11688 return (NULL); 11689 } 11690 return (entry); 11691} 11692 11693static int 11694ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11695{ 11696 11697 switch (lun_type) { 11698 case T_PROCESSOR: 11699 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11700 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11701 return (0); 11702 break; 11703 case T_DIRECT: 11704 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11705 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11706 return (0); 11707 break; 11708 default: 11709 return (0); 11710 } 11711 return (1); 11712} 11713 11714static int 11715ctl_scsiio(struct ctl_scsiio *ctsio) 11716{ 11717 int retval; 11718 const struct ctl_cmd_entry *entry; 11719 11720 retval = CTL_RETVAL_COMPLETE; 11721 11722 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11723 11724 entry = ctl_get_cmd_entry(ctsio); 11725 11726 /* 11727 * If this I/O has been aborted, just send it straight to 11728 * ctl_done() without executing it. 11729 */ 11730 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11731 ctl_done((union ctl_io *)ctsio); 11732 goto bailout; 11733 } 11734 11735 /* 11736 * All the checks should have been handled by ctl_scsiio_precheck(). 11737 * We should be clear now to just execute the I/O. 11738 */ 11739 retval = entry->execute(ctsio); 11740 11741bailout: 11742 return (retval); 11743} 11744 11745/* 11746 * Since we only implement one target right now, a bus reset simply resets 11747 * our single target. 11748 */ 11749static int 11750ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11751{ 11752 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11753} 11754 11755static int 11756ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11757 ctl_ua_type ua_type) 11758{ 11759 struct ctl_lun *lun; 11760 int retval; 11761 11762 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11763 union ctl_ha_msg msg_info; 11764 11765 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11766 msg_info.hdr.nexus = io->io_hdr.nexus; 11767 if (ua_type==CTL_UA_TARG_RESET) 11768 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11769 else 11770 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11771 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11772 msg_info.hdr.original_sc = NULL; 11773 msg_info.hdr.serializing_sc = NULL; 11774 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11775 (void *)&msg_info, sizeof(msg_info), 0)) { 11776 } 11777 } 11778 retval = 0; 11779 11780 mtx_lock(&ctl_softc->ctl_lock); 11781 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11782 retval += ctl_lun_reset(lun, io, ua_type); 11783 mtx_unlock(&ctl_softc->ctl_lock); 11784 11785 return (retval); 11786} 11787 11788/* 11789 * The LUN should always be set. The I/O is optional, and is used to 11790 * distinguish between I/Os sent by this initiator, and by other 11791 * initiators. We set unit attention for initiators other than this one. 11792 * SAM-3 is vague on this point. It does say that a unit attention should 11793 * be established for other initiators when a LUN is reset (see section 11794 * 5.7.3), but it doesn't specifically say that the unit attention should 11795 * be established for this particular initiator when a LUN is reset. Here 11796 * is the relevant text, from SAM-3 rev 8: 11797 * 11798 * 5.7.2 When a SCSI initiator port aborts its own tasks 11799 * 11800 * When a SCSI initiator port causes its own task(s) to be aborted, no 11801 * notification that the task(s) have been aborted shall be returned to 11802 * the SCSI initiator port other than the completion response for the 11803 * command or task management function action that caused the task(s) to 11804 * be aborted and notification(s) associated with related effects of the 11805 * action (e.g., a reset unit attention condition). 11806 * 11807 * XXX KDM for now, we're setting unit attention for all initiators. 11808 */ 11809static int 11810ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11811{ 11812 union ctl_io *xio; 11813#if 0 11814 uint32_t initindex; 11815#endif 11816 int i; 11817 11818 mtx_lock(&lun->lun_lock); 11819 /* 11820 * Run through the OOA queue and abort each I/O. 11821 */ 11822#if 0 11823 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11824#endif 11825 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11826 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11827 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11828 } 11829 11830 /* 11831 * This version sets unit attention for every 11832 */ 11833#if 0 11834 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11835 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11836 if (initindex == i) 11837 continue; 11838 lun->pending_ua[i] |= ua_type; 11839 } 11840#endif 11841 11842 /* 11843 * A reset (any kind, really) clears reservations established with 11844 * RESERVE/RELEASE. It does not clear reservations established 11845 * with PERSISTENT RESERVE OUT, but we don't support that at the 11846 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11847 * reservations made with the RESERVE/RELEASE commands, because 11848 * those commands are obsolete in SPC-3. 11849 */ 11850 lun->flags &= ~CTL_LUN_RESERVED; 11851 11852 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11853#ifdef CTL_WITH_CA 11854 ctl_clear_mask(lun->have_ca, i); 11855#endif 11856 lun->pending_ua[i] |= ua_type; 11857 } 11858 mtx_unlock(&lun->lun_lock); 11859 11860 return (0); 11861} 11862 11863static int 11864ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11865 int other_sc) 11866{ 11867 union ctl_io *xio; 11868 int found; 11869 11870 mtx_assert(&lun->lun_lock, MA_OWNED); 11871 11872 /* 11873 * Run through the OOA queue and attempt to find the given I/O. 11874 * The target port, initiator ID, tag type and tag number have to 11875 * match the values that we got from the initiator. If we have an 11876 * untagged command to abort, simply abort the first untagged command 11877 * we come to. We only allow one untagged command at a time of course. 11878 */ 11879 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11880 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11881 11882 if ((targ_port == UINT32_MAX || 11883 targ_port == xio->io_hdr.nexus.targ_port) && 11884 (init_id == UINT32_MAX || 11885 init_id == xio->io_hdr.nexus.initid.id)) { 11886 if (targ_port != xio->io_hdr.nexus.targ_port || 11887 init_id != xio->io_hdr.nexus.initid.id) 11888 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11889 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11890 found = 1; 11891 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11892 union ctl_ha_msg msg_info; 11893 11894 msg_info.hdr.nexus = xio->io_hdr.nexus; 11895 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11896 msg_info.task.tag_num = xio->scsiio.tag_num; 11897 msg_info.task.tag_type = xio->scsiio.tag_type; 11898 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11899 msg_info.hdr.original_sc = NULL; 11900 msg_info.hdr.serializing_sc = NULL; 11901 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11902 (void *)&msg_info, sizeof(msg_info), 0); 11903 } 11904 } 11905 } 11906 return (found); 11907} 11908 11909static int 11910ctl_abort_task_set(union ctl_io *io) 11911{ 11912 struct ctl_softc *softc = control_softc; 11913 struct ctl_lun *lun; 11914 uint32_t targ_lun; 11915 11916 /* 11917 * Look up the LUN. 11918 */ 11919 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11920 mtx_lock(&softc->ctl_lock); 11921 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11922 lun = softc->ctl_luns[targ_lun]; 11923 else { 11924 mtx_unlock(&softc->ctl_lock); 11925 return (1); 11926 } 11927 11928 mtx_lock(&lun->lun_lock); 11929 mtx_unlock(&softc->ctl_lock); 11930 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11931 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11932 io->io_hdr.nexus.initid.id, 11933 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11934 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11935 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11936 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11937 } 11938 mtx_unlock(&lun->lun_lock); 11939 return (0); 11940} 11941 11942static int 11943ctl_i_t_nexus_reset(union ctl_io *io) 11944{ 11945 struct ctl_softc *softc = control_softc; 11946 struct ctl_lun *lun; 11947 uint32_t initindex; 11948 11949 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11950 mtx_lock(&softc->ctl_lock); 11951 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11952 mtx_lock(&lun->lun_lock); 11953 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11954 io->io_hdr.nexus.initid.id, 11955 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11956#ifdef CTL_WITH_CA 11957 ctl_clear_mask(lun->have_ca, initindex); 11958#endif 11959 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 11960 mtx_unlock(&lun->lun_lock); 11961 } 11962 mtx_unlock(&softc->ctl_lock); 11963 return (0); 11964} 11965 11966static int 11967ctl_abort_task(union ctl_io *io) 11968{ 11969 union ctl_io *xio; 11970 struct ctl_lun *lun; 11971 struct ctl_softc *ctl_softc; 11972#if 0 11973 struct sbuf sb; 11974 char printbuf[128]; 11975#endif 11976 int found; 11977 uint32_t targ_lun; 11978 11979 ctl_softc = control_softc; 11980 found = 0; 11981 11982 /* 11983 * Look up the LUN. 11984 */ 11985 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11986 mtx_lock(&ctl_softc->ctl_lock); 11987 if ((targ_lun < CTL_MAX_LUNS) 11988 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11989 lun = ctl_softc->ctl_luns[targ_lun]; 11990 else { 11991 mtx_unlock(&ctl_softc->ctl_lock); 11992 return (1); 11993 } 11994 11995#if 0 11996 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11997 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11998#endif 11999 12000 mtx_lock(&lun->lun_lock); 12001 mtx_unlock(&ctl_softc->ctl_lock); 12002 /* 12003 * Run through the OOA queue and attempt to find the given I/O. 12004 * The target port, initiator ID, tag type and tag number have to 12005 * match the values that we got from the initiator. If we have an 12006 * untagged command to abort, simply abort the first untagged command 12007 * we come to. We only allow one untagged command at a time of course. 12008 */ 12009#if 0 12010 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12011#endif 12012 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12013 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12014#if 0 12015 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12016 12017 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12018 lun->lun, xio->scsiio.tag_num, 12019 xio->scsiio.tag_type, 12020 (xio->io_hdr.blocked_links.tqe_prev 12021 == NULL) ? "" : " BLOCKED", 12022 (xio->io_hdr.flags & 12023 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12024 (xio->io_hdr.flags & 12025 CTL_FLAG_ABORT) ? " ABORT" : "", 12026 (xio->io_hdr.flags & 12027 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12028 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12029 sbuf_finish(&sb); 12030 printf("%s\n", sbuf_data(&sb)); 12031#endif 12032 12033 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12034 && (xio->io_hdr.nexus.initid.id == 12035 io->io_hdr.nexus.initid.id)) { 12036 /* 12037 * If the abort says that the task is untagged, the 12038 * task in the queue must be untagged. Otherwise, 12039 * we just check to see whether the tag numbers 12040 * match. This is because the QLogic firmware 12041 * doesn't pass back the tag type in an abort 12042 * request. 12043 */ 12044#if 0 12045 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12046 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12047 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12048#endif 12049 /* 12050 * XXX KDM we've got problems with FC, because it 12051 * doesn't send down a tag type with aborts. So we 12052 * can only really go by the tag number... 12053 * This may cause problems with parallel SCSI. 12054 * Need to figure that out!! 12055 */ 12056 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12057 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12058 found = 1; 12059 if ((io->io_hdr.flags & 12060 CTL_FLAG_FROM_OTHER_SC) == 0 && 12061 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12062 union ctl_ha_msg msg_info; 12063 12064 io->io_hdr.flags |= 12065 CTL_FLAG_SENT_2OTHER_SC; 12066 msg_info.hdr.nexus = io->io_hdr.nexus; 12067 msg_info.task.task_action = 12068 CTL_TASK_ABORT_TASK; 12069 msg_info.task.tag_num = 12070 io->taskio.tag_num; 12071 msg_info.task.tag_type = 12072 io->taskio.tag_type; 12073 msg_info.hdr.msg_type = 12074 CTL_MSG_MANAGE_TASKS; 12075 msg_info.hdr.original_sc = NULL; 12076 msg_info.hdr.serializing_sc = NULL; 12077#if 0 12078 printf("Sent Abort to other side\n"); 12079#endif 12080 if (CTL_HA_STATUS_SUCCESS != 12081 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12082 (void *)&msg_info, 12083 sizeof(msg_info), 0)) { 12084 } 12085 } 12086#if 0 12087 printf("ctl_abort_task: found I/O to abort\n"); 12088#endif 12089 break; 12090 } 12091 } 12092 } 12093 mtx_unlock(&lun->lun_lock); 12094 12095 if (found == 0) { 12096 /* 12097 * This isn't really an error. It's entirely possible for 12098 * the abort and command completion to cross on the wire. 12099 * This is more of an informative/diagnostic error. 12100 */ 12101#if 0 12102 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12103 "%d:%d:%d:%d tag %d type %d\n", 12104 io->io_hdr.nexus.initid.id, 12105 io->io_hdr.nexus.targ_port, 12106 io->io_hdr.nexus.targ_target.id, 12107 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12108 io->taskio.tag_type); 12109#endif 12110 } 12111 return (0); 12112} 12113 12114static void 12115ctl_run_task(union ctl_io *io) 12116{ 12117 struct ctl_softc *ctl_softc = control_softc; 12118 int retval = 1; 12119 const char *task_desc; 12120 12121 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12122 12123 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12124 ("ctl_run_task: Unextected io_type %d\n", 12125 io->io_hdr.io_type)); 12126 12127 task_desc = ctl_scsi_task_string(&io->taskio); 12128 if (task_desc != NULL) { 12129#ifdef NEEDTOPORT 12130 csevent_log(CSC_CTL | CSC_SHELF_SW | 12131 CTL_TASK_REPORT, 12132 csevent_LogType_Trace, 12133 csevent_Severity_Information, 12134 csevent_AlertLevel_Green, 12135 csevent_FRU_Firmware, 12136 csevent_FRU_Unknown, 12137 "CTL: received task: %s",task_desc); 12138#endif 12139 } else { 12140#ifdef NEEDTOPORT 12141 csevent_log(CSC_CTL | CSC_SHELF_SW | 12142 CTL_TASK_REPORT, 12143 csevent_LogType_Trace, 12144 csevent_Severity_Information, 12145 csevent_AlertLevel_Green, 12146 csevent_FRU_Firmware, 12147 csevent_FRU_Unknown, 12148 "CTL: received unknown task " 12149 "type: %d (%#x)", 12150 io->taskio.task_action, 12151 io->taskio.task_action); 12152#endif 12153 } 12154 switch (io->taskio.task_action) { 12155 case CTL_TASK_ABORT_TASK: 12156 retval = ctl_abort_task(io); 12157 break; 12158 case CTL_TASK_ABORT_TASK_SET: 12159 case CTL_TASK_CLEAR_TASK_SET: 12160 retval = ctl_abort_task_set(io); 12161 break; 12162 case CTL_TASK_CLEAR_ACA: 12163 break; 12164 case CTL_TASK_I_T_NEXUS_RESET: 12165 retval = ctl_i_t_nexus_reset(io); 12166 break; 12167 case CTL_TASK_LUN_RESET: { 12168 struct ctl_lun *lun; 12169 uint32_t targ_lun; 12170 12171 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12172 mtx_lock(&ctl_softc->ctl_lock); 12173 if ((targ_lun < CTL_MAX_LUNS) 12174 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12175 lun = ctl_softc->ctl_luns[targ_lun]; 12176 else { 12177 mtx_unlock(&ctl_softc->ctl_lock); 12178 retval = 1; 12179 break; 12180 } 12181 12182 if (!(io->io_hdr.flags & 12183 CTL_FLAG_FROM_OTHER_SC)) { 12184 union ctl_ha_msg msg_info; 12185 12186 io->io_hdr.flags |= 12187 CTL_FLAG_SENT_2OTHER_SC; 12188 msg_info.hdr.msg_type = 12189 CTL_MSG_MANAGE_TASKS; 12190 msg_info.hdr.nexus = io->io_hdr.nexus; 12191 msg_info.task.task_action = 12192 CTL_TASK_LUN_RESET; 12193 msg_info.hdr.original_sc = NULL; 12194 msg_info.hdr.serializing_sc = NULL; 12195 if (CTL_HA_STATUS_SUCCESS != 12196 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12197 (void *)&msg_info, 12198 sizeof(msg_info), 0)) { 12199 } 12200 } 12201 12202 retval = ctl_lun_reset(lun, io, 12203 CTL_UA_LUN_RESET); 12204 mtx_unlock(&ctl_softc->ctl_lock); 12205 break; 12206 } 12207 case CTL_TASK_TARGET_RESET: 12208 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12209 break; 12210 case CTL_TASK_BUS_RESET: 12211 retval = ctl_bus_reset(ctl_softc, io); 12212 break; 12213 case CTL_TASK_PORT_LOGIN: 12214 break; 12215 case CTL_TASK_PORT_LOGOUT: 12216 break; 12217 default: 12218 printf("ctl_run_task: got unknown task management event %d\n", 12219 io->taskio.task_action); 12220 break; 12221 } 12222 if (retval == 0) 12223 io->io_hdr.status = CTL_SUCCESS; 12224 else 12225 io->io_hdr.status = CTL_ERROR; 12226 ctl_done(io); 12227} 12228 12229/* 12230 * For HA operation. Handle commands that come in from the other 12231 * controller. 12232 */ 12233static void 12234ctl_handle_isc(union ctl_io *io) 12235{ 12236 int free_io; 12237 struct ctl_lun *lun; 12238 struct ctl_softc *ctl_softc; 12239 uint32_t targ_lun; 12240 12241 ctl_softc = control_softc; 12242 12243 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12244 lun = ctl_softc->ctl_luns[targ_lun]; 12245 12246 switch (io->io_hdr.msg_type) { 12247 case CTL_MSG_SERIALIZE: 12248 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12249 break; 12250 case CTL_MSG_R2R: { 12251 const struct ctl_cmd_entry *entry; 12252 12253 /* 12254 * This is only used in SER_ONLY mode. 12255 */ 12256 free_io = 0; 12257 entry = ctl_get_cmd_entry(&io->scsiio); 12258 mtx_lock(&lun->lun_lock); 12259 if (ctl_scsiio_lun_check(ctl_softc, lun, 12260 entry, (struct ctl_scsiio *)io) != 0) { 12261 mtx_unlock(&lun->lun_lock); 12262 ctl_done(io); 12263 break; 12264 } 12265 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12266 mtx_unlock(&lun->lun_lock); 12267 ctl_enqueue_rtr(io); 12268 break; 12269 } 12270 case CTL_MSG_FINISH_IO: 12271 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12272 free_io = 0; 12273 ctl_done(io); 12274 } else { 12275 free_io = 1; 12276 mtx_lock(&lun->lun_lock); 12277 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12278 ooa_links); 12279 ctl_check_blocked(lun); 12280 mtx_unlock(&lun->lun_lock); 12281 } 12282 break; 12283 case CTL_MSG_PERS_ACTION: 12284 ctl_hndl_per_res_out_on_other_sc( 12285 (union ctl_ha_msg *)&io->presio.pr_msg); 12286 free_io = 1; 12287 break; 12288 case CTL_MSG_BAD_JUJU: 12289 free_io = 0; 12290 ctl_done(io); 12291 break; 12292 case CTL_MSG_DATAMOVE: 12293 /* Only used in XFER mode */ 12294 free_io = 0; 12295 ctl_datamove_remote(io); 12296 break; 12297 case CTL_MSG_DATAMOVE_DONE: 12298 /* Only used in XFER mode */ 12299 free_io = 0; 12300 io->scsiio.be_move_done(io); 12301 break; 12302 default: 12303 free_io = 1; 12304 printf("%s: Invalid message type %d\n", 12305 __func__, io->io_hdr.msg_type); 12306 break; 12307 } 12308 if (free_io) 12309 ctl_free_io(io); 12310 12311} 12312 12313 12314/* 12315 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12316 * there is no match. 12317 */ 12318static ctl_lun_error_pattern 12319ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12320{ 12321 const struct ctl_cmd_entry *entry; 12322 ctl_lun_error_pattern filtered_pattern, pattern; 12323 12324 pattern = desc->error_pattern; 12325 12326 /* 12327 * XXX KDM we need more data passed into this function to match a 12328 * custom pattern, and we actually need to implement custom pattern 12329 * matching. 12330 */ 12331 if (pattern & CTL_LUN_PAT_CMD) 12332 return (CTL_LUN_PAT_CMD); 12333 12334 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12335 return (CTL_LUN_PAT_ANY); 12336 12337 entry = ctl_get_cmd_entry(ctsio); 12338 12339 filtered_pattern = entry->pattern & pattern; 12340 12341 /* 12342 * If the user requested specific flags in the pattern (e.g. 12343 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12344 * flags. 12345 * 12346 * If the user did not specify any flags, it doesn't matter whether 12347 * or not the command supports the flags. 12348 */ 12349 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12350 (pattern & ~CTL_LUN_PAT_MASK)) 12351 return (CTL_LUN_PAT_NONE); 12352 12353 /* 12354 * If the user asked for a range check, see if the requested LBA 12355 * range overlaps with this command's LBA range. 12356 */ 12357 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12358 uint64_t lba1; 12359 uint32_t len1; 12360 ctl_action action; 12361 int retval; 12362 12363 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12364 if (retval != 0) 12365 return (CTL_LUN_PAT_NONE); 12366 12367 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12368 desc->lba_range.len); 12369 /* 12370 * A "pass" means that the LBA ranges don't overlap, so 12371 * this doesn't match the user's range criteria. 12372 */ 12373 if (action == CTL_ACTION_PASS) 12374 return (CTL_LUN_PAT_NONE); 12375 } 12376 12377 return (filtered_pattern); 12378} 12379 12380static void 12381ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12382{ 12383 struct ctl_error_desc *desc, *desc2; 12384 12385 mtx_assert(&lun->lun_lock, MA_OWNED); 12386 12387 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12388 ctl_lun_error_pattern pattern; 12389 /* 12390 * Check to see whether this particular command matches 12391 * the pattern in the descriptor. 12392 */ 12393 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12394 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12395 continue; 12396 12397 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12398 case CTL_LUN_INJ_ABORTED: 12399 ctl_set_aborted(&io->scsiio); 12400 break; 12401 case CTL_LUN_INJ_MEDIUM_ERR: 12402 ctl_set_medium_error(&io->scsiio); 12403 break; 12404 case CTL_LUN_INJ_UA: 12405 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12406 * OCCURRED */ 12407 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12408 break; 12409 case CTL_LUN_INJ_CUSTOM: 12410 /* 12411 * We're assuming the user knows what he is doing. 12412 * Just copy the sense information without doing 12413 * checks. 12414 */ 12415 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12416 ctl_min(sizeof(desc->custom_sense), 12417 sizeof(io->scsiio.sense_data))); 12418 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12419 io->scsiio.sense_len = SSD_FULL_SIZE; 12420 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12421 break; 12422 case CTL_LUN_INJ_NONE: 12423 default: 12424 /* 12425 * If this is an error injection type we don't know 12426 * about, clear the continuous flag (if it is set) 12427 * so it will get deleted below. 12428 */ 12429 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12430 break; 12431 } 12432 /* 12433 * By default, each error injection action is a one-shot 12434 */ 12435 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12436 continue; 12437 12438 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12439 12440 free(desc, M_CTL); 12441 } 12442} 12443 12444#ifdef CTL_IO_DELAY 12445static void 12446ctl_datamove_timer_wakeup(void *arg) 12447{ 12448 union ctl_io *io; 12449 12450 io = (union ctl_io *)arg; 12451 12452 ctl_datamove(io); 12453} 12454#endif /* CTL_IO_DELAY */ 12455 12456void 12457ctl_datamove(union ctl_io *io) 12458{ 12459 void (*fe_datamove)(union ctl_io *io); 12460 12461 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12462 12463 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12464 12465#ifdef CTL_TIME_IO 12466 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12467 char str[256]; 12468 char path_str[64]; 12469 struct sbuf sb; 12470 12471 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12472 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12473 12474 sbuf_cat(&sb, path_str); 12475 switch (io->io_hdr.io_type) { 12476 case CTL_IO_SCSI: 12477 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12478 sbuf_printf(&sb, "\n"); 12479 sbuf_cat(&sb, path_str); 12480 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12481 io->scsiio.tag_num, io->scsiio.tag_type); 12482 break; 12483 case CTL_IO_TASK: 12484 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12485 "Tag Type: %d\n", io->taskio.task_action, 12486 io->taskio.tag_num, io->taskio.tag_type); 12487 break; 12488 default: 12489 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12490 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12491 break; 12492 } 12493 sbuf_cat(&sb, path_str); 12494 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12495 (intmax_t)time_uptime - io->io_hdr.start_time); 12496 sbuf_finish(&sb); 12497 printf("%s", sbuf_data(&sb)); 12498 } 12499#endif /* CTL_TIME_IO */ 12500 12501#ifdef CTL_IO_DELAY 12502 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12503 struct ctl_lun *lun; 12504 12505 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12506 12507 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12508 } else { 12509 struct ctl_lun *lun; 12510 12511 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12512 if ((lun != NULL) 12513 && (lun->delay_info.datamove_delay > 0)) { 12514 struct callout *callout; 12515 12516 callout = (struct callout *)&io->io_hdr.timer_bytes; 12517 callout_init(callout, /*mpsafe*/ 1); 12518 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12519 callout_reset(callout, 12520 lun->delay_info.datamove_delay * hz, 12521 ctl_datamove_timer_wakeup, io); 12522 if (lun->delay_info.datamove_type == 12523 CTL_DELAY_TYPE_ONESHOT) 12524 lun->delay_info.datamove_delay = 0; 12525 return; 12526 } 12527 } 12528#endif 12529 12530 /* 12531 * This command has been aborted. Set the port status, so we fail 12532 * the data move. 12533 */ 12534 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12535 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12536 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12537 io->io_hdr.nexus.targ_port, 12538 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12539 io->io_hdr.nexus.targ_lun); 12540 io->io_hdr.port_status = 31337; 12541 /* 12542 * Note that the backend, in this case, will get the 12543 * callback in its context. In other cases it may get 12544 * called in the frontend's interrupt thread context. 12545 */ 12546 io->scsiio.be_move_done(io); 12547 return; 12548 } 12549 12550 /* 12551 * If we're in XFER mode and this I/O is from the other shelf 12552 * controller, we need to send the DMA to the other side to 12553 * actually transfer the data to/from the host. In serialize only 12554 * mode the transfer happens below CTL and ctl_datamove() is only 12555 * called on the machine that originally received the I/O. 12556 */ 12557 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12558 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12559 union ctl_ha_msg msg; 12560 uint32_t sg_entries_sent; 12561 int do_sg_copy; 12562 int i; 12563 12564 memset(&msg, 0, sizeof(msg)); 12565 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12566 msg.hdr.original_sc = io->io_hdr.original_sc; 12567 msg.hdr.serializing_sc = io; 12568 msg.hdr.nexus = io->io_hdr.nexus; 12569 msg.dt.flags = io->io_hdr.flags; 12570 /* 12571 * We convert everything into a S/G list here. We can't 12572 * pass by reference, only by value between controllers. 12573 * So we can't pass a pointer to the S/G list, only as many 12574 * S/G entries as we can fit in here. If it's possible for 12575 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12576 * then we need to break this up into multiple transfers. 12577 */ 12578 if (io->scsiio.kern_sg_entries == 0) { 12579 msg.dt.kern_sg_entries = 1; 12580 /* 12581 * If this is in cached memory, flush the cache 12582 * before we send the DMA request to the other 12583 * controller. We want to do this in either the 12584 * read or the write case. The read case is 12585 * straightforward. In the write case, we want to 12586 * make sure nothing is in the local cache that 12587 * could overwrite the DMAed data. 12588 */ 12589 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12590 /* 12591 * XXX KDM use bus_dmamap_sync() here. 12592 */ 12593 } 12594 12595 /* 12596 * Convert to a physical address if this is a 12597 * virtual address. 12598 */ 12599 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12600 msg.dt.sg_list[0].addr = 12601 io->scsiio.kern_data_ptr; 12602 } else { 12603 /* 12604 * XXX KDM use busdma here! 12605 */ 12606#if 0 12607 msg.dt.sg_list[0].addr = (void *) 12608 vtophys(io->scsiio.kern_data_ptr); 12609#endif 12610 } 12611 12612 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12613 do_sg_copy = 0; 12614 } else { 12615 struct ctl_sg_entry *sgl; 12616 12617 do_sg_copy = 1; 12618 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12619 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12620 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12621 /* 12622 * XXX KDM use bus_dmamap_sync() here. 12623 */ 12624 } 12625 } 12626 12627 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12628 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12629 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12630 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12631 msg.dt.sg_sequence = 0; 12632 12633 /* 12634 * Loop until we've sent all of the S/G entries. On the 12635 * other end, we'll recompose these S/G entries into one 12636 * contiguous list before passing it to the 12637 */ 12638 for (sg_entries_sent = 0; sg_entries_sent < 12639 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12640 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12641 sizeof(msg.dt.sg_list[0])), 12642 msg.dt.kern_sg_entries - sg_entries_sent); 12643 12644 if (do_sg_copy != 0) { 12645 struct ctl_sg_entry *sgl; 12646 int j; 12647 12648 sgl = (struct ctl_sg_entry *) 12649 io->scsiio.kern_data_ptr; 12650 /* 12651 * If this is in cached memory, flush the cache 12652 * before we send the DMA request to the other 12653 * controller. We want to do this in either 12654 * the * read or the write case. The read 12655 * case is straightforward. In the write 12656 * case, we want to make sure nothing is 12657 * in the local cache that could overwrite 12658 * the DMAed data. 12659 */ 12660 12661 for (i = sg_entries_sent, j = 0; 12662 i < msg.dt.cur_sg_entries; i++, j++) { 12663 if ((io->io_hdr.flags & 12664 CTL_FLAG_NO_DATASYNC) == 0) { 12665 /* 12666 * XXX KDM use bus_dmamap_sync() 12667 */ 12668 } 12669 if ((io->io_hdr.flags & 12670 CTL_FLAG_BUS_ADDR) == 0) { 12671 /* 12672 * XXX KDM use busdma. 12673 */ 12674#if 0 12675 msg.dt.sg_list[j].addr =(void *) 12676 vtophys(sgl[i].addr); 12677#endif 12678 } else { 12679 msg.dt.sg_list[j].addr = 12680 sgl[i].addr; 12681 } 12682 msg.dt.sg_list[j].len = sgl[i].len; 12683 } 12684 } 12685 12686 sg_entries_sent += msg.dt.cur_sg_entries; 12687 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12688 msg.dt.sg_last = 1; 12689 else 12690 msg.dt.sg_last = 0; 12691 12692 /* 12693 * XXX KDM drop and reacquire the lock here? 12694 */ 12695 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12696 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12697 /* 12698 * XXX do something here. 12699 */ 12700 } 12701 12702 msg.dt.sent_sg_entries = sg_entries_sent; 12703 } 12704 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12705 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12706 ctl_failover_io(io, /*have_lock*/ 0); 12707 12708 } else { 12709 12710 /* 12711 * Lookup the fe_datamove() function for this particular 12712 * front end. 12713 */ 12714 fe_datamove = 12715 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12716 12717 fe_datamove(io); 12718 } 12719} 12720 12721static void 12722ctl_send_datamove_done(union ctl_io *io, int have_lock) 12723{ 12724 union ctl_ha_msg msg; 12725 int isc_status; 12726 12727 memset(&msg, 0, sizeof(msg)); 12728 12729 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12730 msg.hdr.original_sc = io; 12731 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12732 msg.hdr.nexus = io->io_hdr.nexus; 12733 msg.hdr.status = io->io_hdr.status; 12734 msg.scsi.tag_num = io->scsiio.tag_num; 12735 msg.scsi.tag_type = io->scsiio.tag_type; 12736 msg.scsi.scsi_status = io->scsiio.scsi_status; 12737 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12738 sizeof(io->scsiio.sense_data)); 12739 msg.scsi.sense_len = io->scsiio.sense_len; 12740 msg.scsi.sense_residual = io->scsiio.sense_residual; 12741 msg.scsi.fetd_status = io->io_hdr.port_status; 12742 msg.scsi.residual = io->scsiio.residual; 12743 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12744 12745 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12746 ctl_failover_io(io, /*have_lock*/ have_lock); 12747 return; 12748 } 12749 12750 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12751 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12752 /* XXX do something if this fails */ 12753 } 12754 12755} 12756 12757/* 12758 * The DMA to the remote side is done, now we need to tell the other side 12759 * we're done so it can continue with its data movement. 12760 */ 12761static void 12762ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12763{ 12764 union ctl_io *io; 12765 12766 io = rq->context; 12767 12768 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12769 printf("%s: ISC DMA write failed with error %d", __func__, 12770 rq->ret); 12771 ctl_set_internal_failure(&io->scsiio, 12772 /*sks_valid*/ 1, 12773 /*retry_count*/ rq->ret); 12774 } 12775 12776 ctl_dt_req_free(rq); 12777 12778 /* 12779 * In this case, we had to malloc the memory locally. Free it. 12780 */ 12781 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12782 int i; 12783 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12784 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12785 } 12786 /* 12787 * The data is in local and remote memory, so now we need to send 12788 * status (good or back) back to the other side. 12789 */ 12790 ctl_send_datamove_done(io, /*have_lock*/ 0); 12791} 12792 12793/* 12794 * We've moved the data from the host/controller into local memory. Now we 12795 * need to push it over to the remote controller's memory. 12796 */ 12797static int 12798ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12799{ 12800 int retval; 12801 12802 retval = 0; 12803 12804 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12805 ctl_datamove_remote_write_cb); 12806 12807 return (retval); 12808} 12809 12810static void 12811ctl_datamove_remote_write(union ctl_io *io) 12812{ 12813 int retval; 12814 void (*fe_datamove)(union ctl_io *io); 12815 12816 /* 12817 * - Get the data from the host/HBA into local memory. 12818 * - DMA memory from the local controller to the remote controller. 12819 * - Send status back to the remote controller. 12820 */ 12821 12822 retval = ctl_datamove_remote_sgl_setup(io); 12823 if (retval != 0) 12824 return; 12825 12826 /* Switch the pointer over so the FETD knows what to do */ 12827 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12828 12829 /* 12830 * Use a custom move done callback, since we need to send completion 12831 * back to the other controller, not to the backend on this side. 12832 */ 12833 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12834 12835 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12836 12837 fe_datamove(io); 12838 12839 return; 12840 12841} 12842 12843static int 12844ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12845{ 12846#if 0 12847 char str[256]; 12848 char path_str[64]; 12849 struct sbuf sb; 12850#endif 12851 12852 /* 12853 * In this case, we had to malloc the memory locally. Free it. 12854 */ 12855 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12856 int i; 12857 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12858 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12859 } 12860 12861#if 0 12862 scsi_path_string(io, path_str, sizeof(path_str)); 12863 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12864 sbuf_cat(&sb, path_str); 12865 scsi_command_string(&io->scsiio, NULL, &sb); 12866 sbuf_printf(&sb, "\n"); 12867 sbuf_cat(&sb, path_str); 12868 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12869 io->scsiio.tag_num, io->scsiio.tag_type); 12870 sbuf_cat(&sb, path_str); 12871 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12872 io->io_hdr.flags, io->io_hdr.status); 12873 sbuf_finish(&sb); 12874 printk("%s", sbuf_data(&sb)); 12875#endif 12876 12877 12878 /* 12879 * The read is done, now we need to send status (good or bad) back 12880 * to the other side. 12881 */ 12882 ctl_send_datamove_done(io, /*have_lock*/ 0); 12883 12884 return (0); 12885} 12886 12887static void 12888ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12889{ 12890 union ctl_io *io; 12891 void (*fe_datamove)(union ctl_io *io); 12892 12893 io = rq->context; 12894 12895 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12896 printf("%s: ISC DMA read failed with error %d", __func__, 12897 rq->ret); 12898 ctl_set_internal_failure(&io->scsiio, 12899 /*sks_valid*/ 1, 12900 /*retry_count*/ rq->ret); 12901 } 12902 12903 ctl_dt_req_free(rq); 12904 12905 /* Switch the pointer over so the FETD knows what to do */ 12906 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12907 12908 /* 12909 * Use a custom move done callback, since we need to send completion 12910 * back to the other controller, not to the backend on this side. 12911 */ 12912 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12913 12914 /* XXX KDM add checks like the ones in ctl_datamove? */ 12915 12916 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12917 12918 fe_datamove(io); 12919} 12920 12921static int 12922ctl_datamove_remote_sgl_setup(union ctl_io *io) 12923{ 12924 struct ctl_sg_entry *local_sglist, *remote_sglist; 12925 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12926 struct ctl_softc *softc; 12927 int retval; 12928 int i; 12929 12930 retval = 0; 12931 softc = control_softc; 12932 12933 local_sglist = io->io_hdr.local_sglist; 12934 local_dma_sglist = io->io_hdr.local_dma_sglist; 12935 remote_sglist = io->io_hdr.remote_sglist; 12936 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12937 12938 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12939 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12940 local_sglist[i].len = remote_sglist[i].len; 12941 12942 /* 12943 * XXX Detect the situation where the RS-level I/O 12944 * redirector on the other side has already read the 12945 * data off of the AOR RS on this side, and 12946 * transferred it to remote (mirror) memory on the 12947 * other side. Since we already have the data in 12948 * memory here, we just need to use it. 12949 * 12950 * XXX KDM this can probably be removed once we 12951 * get the cache device code in and take the 12952 * current AOR implementation out. 12953 */ 12954#ifdef NEEDTOPORT 12955 if ((remote_sglist[i].addr >= 12956 (void *)vtophys(softc->mirr->addr)) 12957 && (remote_sglist[i].addr < 12958 ((void *)vtophys(softc->mirr->addr) + 12959 CacheMirrorOffset))) { 12960 local_sglist[i].addr = remote_sglist[i].addr - 12961 CacheMirrorOffset; 12962 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12963 CTL_FLAG_DATA_IN) 12964 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12965 } else { 12966 local_sglist[i].addr = remote_sglist[i].addr + 12967 CacheMirrorOffset; 12968 } 12969#endif 12970#if 0 12971 printf("%s: local %p, remote %p, len %d\n", 12972 __func__, local_sglist[i].addr, 12973 remote_sglist[i].addr, local_sglist[i].len); 12974#endif 12975 } 12976 } else { 12977 uint32_t len_to_go; 12978 12979 /* 12980 * In this case, we don't have automatically allocated 12981 * memory for this I/O on this controller. This typically 12982 * happens with internal CTL I/O -- e.g. inquiry, mode 12983 * sense, etc. Anything coming from RAIDCore will have 12984 * a mirror area available. 12985 */ 12986 len_to_go = io->scsiio.kern_data_len; 12987 12988 /* 12989 * Clear the no datasync flag, we have to use malloced 12990 * buffers. 12991 */ 12992 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12993 12994 /* 12995 * The difficult thing here is that the size of the various 12996 * S/G segments may be different than the size from the 12997 * remote controller. That'll make it harder when DMAing 12998 * the data back to the other side. 12999 */ 13000 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13001 sizeof(io->io_hdr.remote_sglist[0])) && 13002 (len_to_go > 0); i++) { 13003 local_sglist[i].len = ctl_min(len_to_go, 131072); 13004 CTL_SIZE_8B(local_dma_sglist[i].len, 13005 local_sglist[i].len); 13006 local_sglist[i].addr = 13007 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13008 13009 local_dma_sglist[i].addr = local_sglist[i].addr; 13010 13011 if (local_sglist[i].addr == NULL) { 13012 int j; 13013 13014 printf("malloc failed for %zd bytes!", 13015 local_dma_sglist[i].len); 13016 for (j = 0; j < i; j++) { 13017 free(local_sglist[j].addr, M_CTL); 13018 } 13019 ctl_set_internal_failure(&io->scsiio, 13020 /*sks_valid*/ 1, 13021 /*retry_count*/ 4857); 13022 retval = 1; 13023 goto bailout_error; 13024 13025 } 13026 /* XXX KDM do we need a sync here? */ 13027 13028 len_to_go -= local_sglist[i].len; 13029 } 13030 /* 13031 * Reset the number of S/G entries accordingly. The 13032 * original number of S/G entries is available in 13033 * rem_sg_entries. 13034 */ 13035 io->scsiio.kern_sg_entries = i; 13036 13037#if 0 13038 printf("%s: kern_sg_entries = %d\n", __func__, 13039 io->scsiio.kern_sg_entries); 13040 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13041 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13042 local_sglist[i].addr, local_sglist[i].len, 13043 local_dma_sglist[i].len); 13044#endif 13045 } 13046 13047 13048 return (retval); 13049 13050bailout_error: 13051 13052 ctl_send_datamove_done(io, /*have_lock*/ 0); 13053 13054 return (retval); 13055} 13056 13057static int 13058ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13059 ctl_ha_dt_cb callback) 13060{ 13061 struct ctl_ha_dt_req *rq; 13062 struct ctl_sg_entry *remote_sglist, *local_sglist; 13063 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13064 uint32_t local_used, remote_used, total_used; 13065 int retval; 13066 int i, j; 13067 13068 retval = 0; 13069 13070 rq = ctl_dt_req_alloc(); 13071 13072 /* 13073 * If we failed to allocate the request, and if the DMA didn't fail 13074 * anyway, set busy status. This is just a resource allocation 13075 * failure. 13076 */ 13077 if ((rq == NULL) 13078 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13079 ctl_set_busy(&io->scsiio); 13080 13081 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13082 13083 if (rq != NULL) 13084 ctl_dt_req_free(rq); 13085 13086 /* 13087 * The data move failed. We need to return status back 13088 * to the other controller. No point in trying to DMA 13089 * data to the remote controller. 13090 */ 13091 13092 ctl_send_datamove_done(io, /*have_lock*/ 0); 13093 13094 retval = 1; 13095 13096 goto bailout; 13097 } 13098 13099 local_sglist = io->io_hdr.local_sglist; 13100 local_dma_sglist = io->io_hdr.local_dma_sglist; 13101 remote_sglist = io->io_hdr.remote_sglist; 13102 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13103 local_used = 0; 13104 remote_used = 0; 13105 total_used = 0; 13106 13107 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13108 rq->ret = CTL_HA_STATUS_SUCCESS; 13109 rq->context = io; 13110 callback(rq); 13111 goto bailout; 13112 } 13113 13114 /* 13115 * Pull/push the data over the wire from/to the other controller. 13116 * This takes into account the possibility that the local and 13117 * remote sglists may not be identical in terms of the size of 13118 * the elements and the number of elements. 13119 * 13120 * One fundamental assumption here is that the length allocated for 13121 * both the local and remote sglists is identical. Otherwise, we've 13122 * essentially got a coding error of some sort. 13123 */ 13124 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13125 int isc_ret; 13126 uint32_t cur_len, dma_length; 13127 uint8_t *tmp_ptr; 13128 13129 rq->id = CTL_HA_DATA_CTL; 13130 rq->command = command; 13131 rq->context = io; 13132 13133 /* 13134 * Both pointers should be aligned. But it is possible 13135 * that the allocation length is not. They should both 13136 * also have enough slack left over at the end, though, 13137 * to round up to the next 8 byte boundary. 13138 */ 13139 cur_len = ctl_min(local_sglist[i].len - local_used, 13140 remote_sglist[j].len - remote_used); 13141 13142 /* 13143 * In this case, we have a size issue and need to decrease 13144 * the size, except in the case where we actually have less 13145 * than 8 bytes left. In that case, we need to increase 13146 * the DMA length to get the last bit. 13147 */ 13148 if ((cur_len & 0x7) != 0) { 13149 if (cur_len > 0x7) { 13150 cur_len = cur_len - (cur_len & 0x7); 13151 dma_length = cur_len; 13152 } else { 13153 CTL_SIZE_8B(dma_length, cur_len); 13154 } 13155 13156 } else 13157 dma_length = cur_len; 13158 13159 /* 13160 * If we had to allocate memory for this I/O, instead of using 13161 * the non-cached mirror memory, we'll need to flush the cache 13162 * before trying to DMA to the other controller. 13163 * 13164 * We could end up doing this multiple times for the same 13165 * segment if we have a larger local segment than remote 13166 * segment. That shouldn't be an issue. 13167 */ 13168 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13169 /* 13170 * XXX KDM use bus_dmamap_sync() here. 13171 */ 13172 } 13173 13174 rq->size = dma_length; 13175 13176 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13177 tmp_ptr += local_used; 13178 13179 /* Use physical addresses when talking to ISC hardware */ 13180 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13181 /* XXX KDM use busdma */ 13182#if 0 13183 rq->local = vtophys(tmp_ptr); 13184#endif 13185 } else 13186 rq->local = tmp_ptr; 13187 13188 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13189 tmp_ptr += remote_used; 13190 rq->remote = tmp_ptr; 13191 13192 rq->callback = NULL; 13193 13194 local_used += cur_len; 13195 if (local_used >= local_sglist[i].len) { 13196 i++; 13197 local_used = 0; 13198 } 13199 13200 remote_used += cur_len; 13201 if (remote_used >= remote_sglist[j].len) { 13202 j++; 13203 remote_used = 0; 13204 } 13205 total_used += cur_len; 13206 13207 if (total_used >= io->scsiio.kern_data_len) 13208 rq->callback = callback; 13209 13210 if ((rq->size & 0x7) != 0) { 13211 printf("%s: warning: size %d is not on 8b boundary\n", 13212 __func__, rq->size); 13213 } 13214 if (((uintptr_t)rq->local & 0x7) != 0) { 13215 printf("%s: warning: local %p not on 8b boundary\n", 13216 __func__, rq->local); 13217 } 13218 if (((uintptr_t)rq->remote & 0x7) != 0) { 13219 printf("%s: warning: remote %p not on 8b boundary\n", 13220 __func__, rq->local); 13221 } 13222#if 0 13223 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13224 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13225 rq->local, rq->remote, rq->size); 13226#endif 13227 13228 isc_ret = ctl_dt_single(rq); 13229 if (isc_ret == CTL_HA_STATUS_WAIT) 13230 continue; 13231 13232 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13233 rq->ret = CTL_HA_STATUS_SUCCESS; 13234 } else { 13235 rq->ret = isc_ret; 13236 } 13237 callback(rq); 13238 goto bailout; 13239 } 13240 13241bailout: 13242 return (retval); 13243 13244} 13245 13246static void 13247ctl_datamove_remote_read(union ctl_io *io) 13248{ 13249 int retval; 13250 int i; 13251 13252 /* 13253 * This will send an error to the other controller in the case of a 13254 * failure. 13255 */ 13256 retval = ctl_datamove_remote_sgl_setup(io); 13257 if (retval != 0) 13258 return; 13259 13260 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13261 ctl_datamove_remote_read_cb); 13262 if ((retval != 0) 13263 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13264 /* 13265 * Make sure we free memory if there was an error.. The 13266 * ctl_datamove_remote_xfer() function will send the 13267 * datamove done message, or call the callback with an 13268 * error if there is a problem. 13269 */ 13270 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13271 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13272 } 13273 13274 return; 13275} 13276 13277/* 13278 * Process a datamove request from the other controller. This is used for 13279 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13280 * first. Once that is complete, the data gets DMAed into the remote 13281 * controller's memory. For reads, we DMA from the remote controller's 13282 * memory into our memory first, and then move it out to the FETD. 13283 */ 13284static void 13285ctl_datamove_remote(union ctl_io *io) 13286{ 13287 struct ctl_softc *softc; 13288 13289 softc = control_softc; 13290 13291 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13292 13293 /* 13294 * Note that we look for an aborted I/O here, but don't do some of 13295 * the other checks that ctl_datamove() normally does. 13296 * We don't need to run the datamove delay code, since that should 13297 * have been done if need be on the other controller. 13298 */ 13299 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13300 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13301 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13302 io->io_hdr.nexus.targ_port, 13303 io->io_hdr.nexus.targ_target.id, 13304 io->io_hdr.nexus.targ_lun); 13305 io->io_hdr.port_status = 31338; 13306 ctl_send_datamove_done(io, /*have_lock*/ 0); 13307 return; 13308 } 13309 13310 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13311 ctl_datamove_remote_write(io); 13312 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13313 ctl_datamove_remote_read(io); 13314 } else { 13315 union ctl_ha_msg msg; 13316 struct scsi_sense_data *sense; 13317 uint8_t sks[3]; 13318 int retry_count; 13319 13320 memset(&msg, 0, sizeof(msg)); 13321 13322 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13323 msg.hdr.status = CTL_SCSI_ERROR; 13324 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13325 13326 retry_count = 4243; 13327 13328 sense = &msg.scsi.sense_data; 13329 sks[0] = SSD_SCS_VALID; 13330 sks[1] = (retry_count >> 8) & 0xff; 13331 sks[2] = retry_count & 0xff; 13332 13333 /* "Internal target failure" */ 13334 scsi_set_sense_data(sense, 13335 /*sense_format*/ SSD_TYPE_NONE, 13336 /*current_error*/ 1, 13337 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13338 /*asc*/ 0x44, 13339 /*ascq*/ 0x00, 13340 /*type*/ SSD_ELEM_SKS, 13341 /*size*/ sizeof(sks), 13342 /*data*/ sks, 13343 SSD_ELEM_NONE); 13344 13345 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13346 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13347 ctl_failover_io(io, /*have_lock*/ 1); 13348 return; 13349 } 13350 13351 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13352 CTL_HA_STATUS_SUCCESS) { 13353 /* XXX KDM what to do if this fails? */ 13354 } 13355 return; 13356 } 13357 13358} 13359 13360static int 13361ctl_process_done(union ctl_io *io) 13362{ 13363 struct ctl_lun *lun; 13364 struct ctl_softc *ctl_softc; 13365 void (*fe_done)(union ctl_io *io); 13366 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13367 13368 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13369 13370 fe_done = 13371 control_softc->ctl_ports[targ_port]->fe_done; 13372 13373#ifdef CTL_TIME_IO 13374 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13375 char str[256]; 13376 char path_str[64]; 13377 struct sbuf sb; 13378 13379 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13380 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13381 13382 sbuf_cat(&sb, path_str); 13383 switch (io->io_hdr.io_type) { 13384 case CTL_IO_SCSI: 13385 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13386 sbuf_printf(&sb, "\n"); 13387 sbuf_cat(&sb, path_str); 13388 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13389 io->scsiio.tag_num, io->scsiio.tag_type); 13390 break; 13391 case CTL_IO_TASK: 13392 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13393 "Tag Type: %d\n", io->taskio.task_action, 13394 io->taskio.tag_num, io->taskio.tag_type); 13395 break; 13396 default: 13397 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13398 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13399 break; 13400 } 13401 sbuf_cat(&sb, path_str); 13402 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13403 (intmax_t)time_uptime - io->io_hdr.start_time); 13404 sbuf_finish(&sb); 13405 printf("%s", sbuf_data(&sb)); 13406 } 13407#endif /* CTL_TIME_IO */ 13408 13409 switch (io->io_hdr.io_type) { 13410 case CTL_IO_SCSI: 13411 break; 13412 case CTL_IO_TASK: 13413 if (bootverbose || verbose > 0) 13414 ctl_io_error_print(io, NULL); 13415 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13416 ctl_free_io(io); 13417 else 13418 fe_done(io); 13419 return (CTL_RETVAL_COMPLETE); 13420 break; 13421 default: 13422 printf("ctl_process_done: invalid io type %d\n", 13423 io->io_hdr.io_type); 13424 panic("ctl_process_done: invalid io type %d\n", 13425 io->io_hdr.io_type); 13426 break; /* NOTREACHED */ 13427 } 13428 13429 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13430 if (lun == NULL) { 13431 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13432 io->io_hdr.nexus.targ_mapped_lun)); 13433 fe_done(io); 13434 goto bailout; 13435 } 13436 ctl_softc = lun->ctl_softc; 13437 13438 mtx_lock(&lun->lun_lock); 13439 13440 /* 13441 * Check to see if we have any errors to inject here. We only 13442 * inject errors for commands that don't already have errors set. 13443 */ 13444 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13445 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13446 ctl_inject_error(lun, io); 13447 13448 /* 13449 * XXX KDM how do we treat commands that aren't completed 13450 * successfully? 13451 * 13452 * XXX KDM should we also track I/O latency? 13453 */ 13454 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13455 io->io_hdr.io_type == CTL_IO_SCSI) { 13456#ifdef CTL_TIME_IO 13457 struct bintime cur_bt; 13458#endif 13459 int type; 13460 13461 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13462 CTL_FLAG_DATA_IN) 13463 type = CTL_STATS_READ; 13464 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13465 CTL_FLAG_DATA_OUT) 13466 type = CTL_STATS_WRITE; 13467 else 13468 type = CTL_STATS_NO_IO; 13469 13470 lun->stats.ports[targ_port].bytes[type] += 13471 io->scsiio.kern_total_len; 13472 lun->stats.ports[targ_port].operations[type]++; 13473#ifdef CTL_TIME_IO 13474 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13475 &io->io_hdr.dma_bt); 13476 lun->stats.ports[targ_port].num_dmas[type] += 13477 io->io_hdr.num_dmas; 13478 getbintime(&cur_bt); 13479 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13480 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13481#endif 13482 } 13483 13484 /* 13485 * Remove this from the OOA queue. 13486 */ 13487 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13488 13489 /* 13490 * Run through the blocked queue on this LUN and see if anything 13491 * has become unblocked, now that this transaction is done. 13492 */ 13493 ctl_check_blocked(lun); 13494 13495 /* 13496 * If the LUN has been invalidated, free it if there is nothing 13497 * left on its OOA queue. 13498 */ 13499 if ((lun->flags & CTL_LUN_INVALID) 13500 && TAILQ_EMPTY(&lun->ooa_queue)) { 13501 mtx_unlock(&lun->lun_lock); 13502 mtx_lock(&ctl_softc->ctl_lock); 13503 ctl_free_lun(lun); 13504 mtx_unlock(&ctl_softc->ctl_lock); 13505 } else 13506 mtx_unlock(&lun->lun_lock); 13507 13508 /* 13509 * If this command has been aborted, make sure we set the status 13510 * properly. The FETD is responsible for freeing the I/O and doing 13511 * whatever it needs to do to clean up its state. 13512 */ 13513 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13514 ctl_set_task_aborted(&io->scsiio); 13515 13516 /* 13517 * We print out status for every task management command. For SCSI 13518 * commands, we filter out any unit attention errors; they happen 13519 * on every boot, and would clutter up the log. Note: task 13520 * management commands aren't printed here, they are printed above, 13521 * since they should never even make it down here. 13522 */ 13523 switch (io->io_hdr.io_type) { 13524 case CTL_IO_SCSI: { 13525 int error_code, sense_key, asc, ascq; 13526 13527 sense_key = 0; 13528 13529 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13530 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13531 /* 13532 * Since this is just for printing, no need to 13533 * show errors here. 13534 */ 13535 scsi_extract_sense_len(&io->scsiio.sense_data, 13536 io->scsiio.sense_len, 13537 &error_code, 13538 &sense_key, 13539 &asc, 13540 &ascq, 13541 /*show_errors*/ 0); 13542 } 13543 13544 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13545 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13546 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13547 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13548 13549 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13550 ctl_softc->skipped_prints++; 13551 } else { 13552 uint32_t skipped_prints; 13553 13554 skipped_prints = ctl_softc->skipped_prints; 13555 13556 ctl_softc->skipped_prints = 0; 13557 ctl_softc->last_print_jiffies = time_uptime; 13558 13559 if (skipped_prints > 0) { 13560#ifdef NEEDTOPORT 13561 csevent_log(CSC_CTL | CSC_SHELF_SW | 13562 CTL_ERROR_REPORT, 13563 csevent_LogType_Trace, 13564 csevent_Severity_Information, 13565 csevent_AlertLevel_Green, 13566 csevent_FRU_Firmware, 13567 csevent_FRU_Unknown, 13568 "High CTL error volume, %d prints " 13569 "skipped", skipped_prints); 13570#endif 13571 } 13572 if (bootverbose || verbose > 0) 13573 ctl_io_error_print(io, NULL); 13574 } 13575 } 13576 break; 13577 } 13578 case CTL_IO_TASK: 13579 if (bootverbose || verbose > 0) 13580 ctl_io_error_print(io, NULL); 13581 break; 13582 default: 13583 break; 13584 } 13585 13586 /* 13587 * Tell the FETD or the other shelf controller we're done with this 13588 * command. Note that only SCSI commands get to this point. Task 13589 * management commands are completed above. 13590 * 13591 * We only send status to the other controller if we're in XFER 13592 * mode. In SER_ONLY mode, the I/O is done on the controller that 13593 * received the I/O (from CTL's perspective), and so the status is 13594 * generated there. 13595 * 13596 * XXX KDM if we hold the lock here, we could cause a deadlock 13597 * if the frontend comes back in in this context to queue 13598 * something. 13599 */ 13600 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13601 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13602 union ctl_ha_msg msg; 13603 13604 memset(&msg, 0, sizeof(msg)); 13605 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13606 msg.hdr.original_sc = io->io_hdr.original_sc; 13607 msg.hdr.nexus = io->io_hdr.nexus; 13608 msg.hdr.status = io->io_hdr.status; 13609 msg.scsi.scsi_status = io->scsiio.scsi_status; 13610 msg.scsi.tag_num = io->scsiio.tag_num; 13611 msg.scsi.tag_type = io->scsiio.tag_type; 13612 msg.scsi.sense_len = io->scsiio.sense_len; 13613 msg.scsi.sense_residual = io->scsiio.sense_residual; 13614 msg.scsi.residual = io->scsiio.residual; 13615 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13616 sizeof(io->scsiio.sense_data)); 13617 /* 13618 * We copy this whether or not this is an I/O-related 13619 * command. Otherwise, we'd have to go and check to see 13620 * whether it's a read/write command, and it really isn't 13621 * worth it. 13622 */ 13623 memcpy(&msg.scsi.lbalen, 13624 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13625 sizeof(msg.scsi.lbalen)); 13626 13627 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13628 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13629 /* XXX do something here */ 13630 } 13631 13632 ctl_free_io(io); 13633 } else 13634 fe_done(io); 13635 13636bailout: 13637 13638 return (CTL_RETVAL_COMPLETE); 13639} 13640 13641#ifdef CTL_WITH_CA 13642/* 13643 * Front end should call this if it doesn't do autosense. When the request 13644 * sense comes back in from the initiator, we'll dequeue this and send it. 13645 */ 13646int 13647ctl_queue_sense(union ctl_io *io) 13648{ 13649 struct ctl_lun *lun; 13650 struct ctl_softc *ctl_softc; 13651 uint32_t initidx, targ_lun; 13652 13653 ctl_softc = control_softc; 13654 13655 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13656 13657 /* 13658 * LUN lookup will likely move to the ctl_work_thread() once we 13659 * have our new queueing infrastructure (that doesn't put things on 13660 * a per-LUN queue initially). That is so that we can handle 13661 * things like an INQUIRY to a LUN that we don't have enabled. We 13662 * can't deal with that right now. 13663 */ 13664 mtx_lock(&ctl_softc->ctl_lock); 13665 13666 /* 13667 * If we don't have a LUN for this, just toss the sense 13668 * information. 13669 */ 13670 targ_lun = io->io_hdr.nexus.targ_lun; 13671 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13672 if ((targ_lun < CTL_MAX_LUNS) 13673 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13674 lun = ctl_softc->ctl_luns[targ_lun]; 13675 else 13676 goto bailout; 13677 13678 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13679 13680 mtx_lock(&lun->lun_lock); 13681 /* 13682 * Already have CA set for this LUN...toss the sense information. 13683 */ 13684 if (ctl_is_set(lun->have_ca, initidx)) { 13685 mtx_unlock(&lun->lun_lock); 13686 goto bailout; 13687 } 13688 13689 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13690 ctl_min(sizeof(lun->pending_sense[initidx]), 13691 sizeof(io->scsiio.sense_data))); 13692 ctl_set_mask(lun->have_ca, initidx); 13693 mtx_unlock(&lun->lun_lock); 13694 13695bailout: 13696 mtx_unlock(&ctl_softc->ctl_lock); 13697 13698 ctl_free_io(io); 13699 13700 return (CTL_RETVAL_COMPLETE); 13701} 13702#endif 13703 13704/* 13705 * Primary command inlet from frontend ports. All SCSI and task I/O 13706 * requests must go through this function. 13707 */ 13708int 13709ctl_queue(union ctl_io *io) 13710{ 13711 struct ctl_softc *ctl_softc; 13712 13713 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13714 13715 ctl_softc = control_softc; 13716 13717#ifdef CTL_TIME_IO 13718 io->io_hdr.start_time = time_uptime; 13719 getbintime(&io->io_hdr.start_bt); 13720#endif /* CTL_TIME_IO */ 13721 13722 /* Map FE-specific LUN ID into global one. */ 13723 io->io_hdr.nexus.targ_mapped_lun = 13724 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13725 13726 switch (io->io_hdr.io_type) { 13727 case CTL_IO_SCSI: 13728 case CTL_IO_TASK: 13729 ctl_enqueue_incoming(io); 13730 break; 13731 default: 13732 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13733 return (EINVAL); 13734 } 13735 13736 return (CTL_RETVAL_COMPLETE); 13737} 13738 13739#ifdef CTL_IO_DELAY 13740static void 13741ctl_done_timer_wakeup(void *arg) 13742{ 13743 union ctl_io *io; 13744 13745 io = (union ctl_io *)arg; 13746 ctl_done(io); 13747} 13748#endif /* CTL_IO_DELAY */ 13749 13750void 13751ctl_done(union ctl_io *io) 13752{ 13753 struct ctl_softc *ctl_softc; 13754 13755 ctl_softc = control_softc; 13756 13757 /* 13758 * Enable this to catch duplicate completion issues. 13759 */ 13760#if 0 13761 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13762 printf("%s: type %d msg %d cdb %x iptl: " 13763 "%d:%d:%d:%d tag 0x%04x " 13764 "flag %#x status %x\n", 13765 __func__, 13766 io->io_hdr.io_type, 13767 io->io_hdr.msg_type, 13768 io->scsiio.cdb[0], 13769 io->io_hdr.nexus.initid.id, 13770 io->io_hdr.nexus.targ_port, 13771 io->io_hdr.nexus.targ_target.id, 13772 io->io_hdr.nexus.targ_lun, 13773 (io->io_hdr.io_type == 13774 CTL_IO_TASK) ? 13775 io->taskio.tag_num : 13776 io->scsiio.tag_num, 13777 io->io_hdr.flags, 13778 io->io_hdr.status); 13779 } else 13780 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13781#endif 13782 13783 /* 13784 * This is an internal copy of an I/O, and should not go through 13785 * the normal done processing logic. 13786 */ 13787 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13788 return; 13789 13790 /* 13791 * We need to send a msg to the serializing shelf to finish the IO 13792 * as well. We don't send a finish message to the other shelf if 13793 * this is a task management command. Task management commands 13794 * aren't serialized in the OOA queue, but rather just executed on 13795 * both shelf controllers for commands that originated on that 13796 * controller. 13797 */ 13798 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13799 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13800 union ctl_ha_msg msg_io; 13801 13802 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13803 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13804 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13805 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13806 } 13807 /* continue on to finish IO */ 13808 } 13809#ifdef CTL_IO_DELAY 13810 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13811 struct ctl_lun *lun; 13812 13813 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13814 13815 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13816 } else { 13817 struct ctl_lun *lun; 13818 13819 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13820 13821 if ((lun != NULL) 13822 && (lun->delay_info.done_delay > 0)) { 13823 struct callout *callout; 13824 13825 callout = (struct callout *)&io->io_hdr.timer_bytes; 13826 callout_init(callout, /*mpsafe*/ 1); 13827 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13828 callout_reset(callout, 13829 lun->delay_info.done_delay * hz, 13830 ctl_done_timer_wakeup, io); 13831 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13832 lun->delay_info.done_delay = 0; 13833 return; 13834 } 13835 } 13836#endif /* CTL_IO_DELAY */ 13837 13838 ctl_enqueue_done(io); 13839} 13840 13841int 13842ctl_isc(struct ctl_scsiio *ctsio) 13843{ 13844 struct ctl_lun *lun; 13845 int retval; 13846 13847 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13848 13849 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13850 13851 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13852 13853 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13854 13855 return (retval); 13856} 13857 13858 13859static void 13860ctl_work_thread(void *arg) 13861{ 13862 struct ctl_thread *thr = (struct ctl_thread *)arg; 13863 struct ctl_softc *softc = thr->ctl_softc; 13864 union ctl_io *io; 13865 int retval; 13866 13867 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13868 13869 for (;;) { 13870 retval = 0; 13871 13872 /* 13873 * We handle the queues in this order: 13874 * - ISC 13875 * - done queue (to free up resources, unblock other commands) 13876 * - RtR queue 13877 * - incoming queue 13878 * 13879 * If those queues are empty, we break out of the loop and 13880 * go to sleep. 13881 */ 13882 mtx_lock(&thr->queue_lock); 13883 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13884 if (io != NULL) { 13885 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13886 mtx_unlock(&thr->queue_lock); 13887 ctl_handle_isc(io); 13888 continue; 13889 } 13890 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13891 if (io != NULL) { 13892 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13893 /* clear any blocked commands, call fe_done */ 13894 mtx_unlock(&thr->queue_lock); 13895 retval = ctl_process_done(io); 13896 continue; 13897 } 13898 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13899 if (io != NULL) { 13900 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13901 mtx_unlock(&thr->queue_lock); 13902 if (io->io_hdr.io_type == CTL_IO_TASK) 13903 ctl_run_task(io); 13904 else 13905 ctl_scsiio_precheck(softc, &io->scsiio); 13906 continue; 13907 } 13908 if (!ctl_pause_rtr) { 13909 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13910 if (io != NULL) { 13911 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13912 mtx_unlock(&thr->queue_lock); 13913 retval = ctl_scsiio(&io->scsiio); 13914 if (retval != CTL_RETVAL_COMPLETE) 13915 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13916 continue; 13917 } 13918 } 13919 13920 /* Sleep until we have something to do. */ 13921 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13922 } 13923} 13924 13925static void 13926ctl_lun_thread(void *arg) 13927{ 13928 struct ctl_softc *softc = (struct ctl_softc *)arg; 13929 struct ctl_be_lun *be_lun; 13930 int retval; 13931 13932 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13933 13934 for (;;) { 13935 retval = 0; 13936 mtx_lock(&softc->ctl_lock); 13937 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13938 if (be_lun != NULL) { 13939 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13940 mtx_unlock(&softc->ctl_lock); 13941 ctl_create_lun(be_lun); 13942 continue; 13943 } 13944 13945 /* Sleep until we have something to do. */ 13946 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13947 PDROP | PRIBIO, "-", 0); 13948 } 13949} 13950 13951static void 13952ctl_enqueue_incoming(union ctl_io *io) 13953{ 13954 struct ctl_softc *softc = control_softc; 13955 struct ctl_thread *thr; 13956 u_int idx; 13957 13958 idx = (io->io_hdr.nexus.targ_port * 127 + 13959 io->io_hdr.nexus.initid.id) % worker_threads; 13960 thr = &softc->threads[idx]; 13961 mtx_lock(&thr->queue_lock); 13962 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13963 mtx_unlock(&thr->queue_lock); 13964 wakeup(thr); 13965} 13966 13967static void 13968ctl_enqueue_rtr(union ctl_io *io) 13969{ 13970 struct ctl_softc *softc = control_softc; 13971 struct ctl_thread *thr; 13972 13973 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13974 mtx_lock(&thr->queue_lock); 13975 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13976 mtx_unlock(&thr->queue_lock); 13977 wakeup(thr); 13978} 13979 13980static void 13981ctl_enqueue_done(union ctl_io *io) 13982{ 13983 struct ctl_softc *softc = control_softc; 13984 struct ctl_thread *thr; 13985 13986 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13987 mtx_lock(&thr->queue_lock); 13988 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13989 mtx_unlock(&thr->queue_lock); 13990 wakeup(thr); 13991} 13992 13993static void 13994ctl_enqueue_isc(union ctl_io *io) 13995{ 13996 struct ctl_softc *softc = control_softc; 13997 struct ctl_thread *thr; 13998 13999 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14000 mtx_lock(&thr->queue_lock); 14001 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14002 mtx_unlock(&thr->queue_lock); 14003 wakeup(thr); 14004} 14005 14006/* Initialization and failover */ 14007 14008void 14009ctl_init_isc_msg(void) 14010{ 14011 printf("CTL: Still calling this thing\n"); 14012} 14013 14014/* 14015 * Init component 14016 * Initializes component into configuration defined by bootMode 14017 * (see hasc-sv.c) 14018 * returns hasc_Status: 14019 * OK 14020 * ERROR - fatal error 14021 */ 14022static ctl_ha_comp_status 14023ctl_isc_init(struct ctl_ha_component *c) 14024{ 14025 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14026 14027 c->status = ret; 14028 return ret; 14029} 14030 14031/* Start component 14032 * Starts component in state requested. If component starts successfully, 14033 * it must set its own state to the requestrd state 14034 * When requested state is HASC_STATE_HA, the component may refine it 14035 * by adding _SLAVE or _MASTER flags. 14036 * Currently allowed state transitions are: 14037 * UNKNOWN->HA - initial startup 14038 * UNKNOWN->SINGLE - initial startup when no parter detected 14039 * HA->SINGLE - failover 14040 * returns ctl_ha_comp_status: 14041 * OK - component successfully started in requested state 14042 * FAILED - could not start the requested state, failover may 14043 * be possible 14044 * ERROR - fatal error detected, no future startup possible 14045 */ 14046static ctl_ha_comp_status 14047ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14048{ 14049 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14050 14051 printf("%s: go\n", __func__); 14052 14053 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14054 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14055 ctl_is_single = 0; 14056 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14057 != CTL_HA_STATUS_SUCCESS) { 14058 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14059 ret = CTL_HA_COMP_STATUS_ERROR; 14060 } 14061 } else if (CTL_HA_STATE_IS_HA(c->state) 14062 && CTL_HA_STATE_IS_SINGLE(state)){ 14063 // HA->SINGLE transition 14064 ctl_failover(); 14065 ctl_is_single = 1; 14066 } else { 14067 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14068 c->state, state); 14069 ret = CTL_HA_COMP_STATUS_ERROR; 14070 } 14071 if (CTL_HA_STATE_IS_SINGLE(state)) 14072 ctl_is_single = 1; 14073 14074 c->state = state; 14075 c->status = ret; 14076 return ret; 14077} 14078 14079/* 14080 * Quiesce component 14081 * The component must clear any error conditions (set status to OK) and 14082 * prepare itself to another Start call 14083 * returns ctl_ha_comp_status: 14084 * OK 14085 * ERROR 14086 */ 14087static ctl_ha_comp_status 14088ctl_isc_quiesce(struct ctl_ha_component *c) 14089{ 14090 int ret = CTL_HA_COMP_STATUS_OK; 14091 14092 ctl_pause_rtr = 1; 14093 c->status = ret; 14094 return ret; 14095} 14096 14097struct ctl_ha_component ctl_ha_component_ctlisc = 14098{ 14099 .name = "CTL ISC", 14100 .state = CTL_HA_STATE_UNKNOWN, 14101 .init = ctl_isc_init, 14102 .start = ctl_isc_start, 14103 .quiesce = ctl_isc_quiesce 14104}; 14105 14106/* 14107 * vim: ts=8 14108 */
| 10326 case SVPD_LBP: 10327 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10328 break; 10329 default: 10330 ctl_set_invalid_field(ctsio, 10331 /*sks_valid*/ 1, 10332 /*command*/ 1, 10333 /*field*/ 2, 10334 /*bit_valid*/ 0, 10335 /*bit*/ 0); 10336 ctl_done((union ctl_io *)ctsio); 10337 retval = CTL_RETVAL_COMPLETE; 10338 break; 10339 } 10340 10341 return (retval); 10342} 10343 10344static int 10345ctl_inquiry_std(struct ctl_scsiio *ctsio) 10346{ 10347 struct scsi_inquiry_data *inq_ptr; 10348 struct scsi_inquiry *cdb; 10349 struct ctl_softc *ctl_softc; 10350 struct ctl_lun *lun; 10351 char *val; 10352 uint32_t alloc_len; 10353 ctl_port_type port_type; 10354 10355 ctl_softc = control_softc; 10356 10357 /* 10358 * Figure out whether we're talking to a Fibre Channel port or not. 10359 * We treat the ioctl front end, and any SCSI adapters, as packetized 10360 * SCSI front ends. 10361 */ 10362 port_type = ctl_softc->ctl_ports[ 10363 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10364 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10365 port_type = CTL_PORT_SCSI; 10366 10367 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10368 cdb = (struct scsi_inquiry *)ctsio->cdb; 10369 alloc_len = scsi_2btoul(cdb->length); 10370 10371 /* 10372 * We malloc the full inquiry data size here and fill it 10373 * in. If the user only asks for less, we'll give him 10374 * that much. 10375 */ 10376 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10377 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10378 ctsio->kern_sg_entries = 0; 10379 ctsio->kern_data_resid = 0; 10380 ctsio->kern_rel_offset = 0; 10381 10382 if (sizeof(*inq_ptr) < alloc_len) { 10383 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10384 ctsio->kern_data_len = sizeof(*inq_ptr); 10385 ctsio->kern_total_len = sizeof(*inq_ptr); 10386 } else { 10387 ctsio->residual = 0; 10388 ctsio->kern_data_len = alloc_len; 10389 ctsio->kern_total_len = alloc_len; 10390 } 10391 10392 /* 10393 * If we have a LUN configured, report it as connected. Otherwise, 10394 * report that it is offline or no device is supported, depending 10395 * on the value of inquiry_pq_no_lun. 10396 * 10397 * According to the spec (SPC-4 r34), the peripheral qualifier 10398 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10399 * 10400 * "A peripheral device having the specified peripheral device type 10401 * is not connected to this logical unit. However, the device 10402 * server is capable of supporting the specified peripheral device 10403 * type on this logical unit." 10404 * 10405 * According to the same spec, the peripheral qualifier 10406 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10407 * 10408 * "The device server is not capable of supporting a peripheral 10409 * device on this logical unit. For this peripheral qualifier the 10410 * peripheral device type shall be set to 1Fh. All other peripheral 10411 * device type values are reserved for this peripheral qualifier." 10412 * 10413 * Given the text, it would seem that we probably want to report that 10414 * the LUN is offline here. There is no LUN connected, but we can 10415 * support a LUN at the given LUN number. 10416 * 10417 * In the real world, though, it sounds like things are a little 10418 * different: 10419 * 10420 * - Linux, when presented with a LUN with the offline peripheral 10421 * qualifier, will create an sg driver instance for it. So when 10422 * you attach it to CTL, you wind up with a ton of sg driver 10423 * instances. (One for every LUN that Linux bothered to probe.) 10424 * Linux does this despite the fact that it issues a REPORT LUNs 10425 * to LUN 0 to get the inventory of supported LUNs. 10426 * 10427 * - There is other anecdotal evidence (from Emulex folks) about 10428 * arrays that use the offline peripheral qualifier for LUNs that 10429 * are on the "passive" path in an active/passive array. 10430 * 10431 * So the solution is provide a hopefully reasonable default 10432 * (return bad/no LUN) and allow the user to change the behavior 10433 * with a tunable/sysctl variable. 10434 */ 10435 if (lun != NULL) 10436 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10437 lun->be_lun->lun_type; 10438 else if (ctl_softc->inquiry_pq_no_lun == 0) 10439 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10440 else 10441 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10442 10443 /* RMB in byte 2 is 0 */ 10444 inq_ptr->version = SCSI_REV_SPC4; 10445 10446 /* 10447 * According to SAM-3, even if a device only supports a single 10448 * level of LUN addressing, it should still set the HISUP bit: 10449 * 10450 * 4.9.1 Logical unit numbers overview 10451 * 10452 * All logical unit number formats described in this standard are 10453 * hierarchical in structure even when only a single level in that 10454 * hierarchy is used. The HISUP bit shall be set to one in the 10455 * standard INQUIRY data (see SPC-2) when any logical unit number 10456 * format described in this standard is used. Non-hierarchical 10457 * formats are outside the scope of this standard. 10458 * 10459 * Therefore we set the HiSup bit here. 10460 * 10461 * The reponse format is 2, per SPC-3. 10462 */ 10463 inq_ptr->response_format = SID_HiSup | 2; 10464 10465 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 10466 CTL_DEBUG_PRINT(("additional_length = %d\n", 10467 inq_ptr->additional_length)); 10468 10469 inq_ptr->spc3_flags = SPC3_SID_3PC; 10470 if (!ctl_is_single) 10471 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT; 10472 /* 16 bit addressing */ 10473 if (port_type == CTL_PORT_SCSI) 10474 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10475 /* XXX set the SID_MultiP bit here if we're actually going to 10476 respond on multiple ports */ 10477 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10478 10479 /* 16 bit data bus, synchronous transfers */ 10480 if (port_type == CTL_PORT_SCSI) 10481 inq_ptr->flags = SID_WBus16 | SID_Sync; 10482 /* 10483 * XXX KDM do we want to support tagged queueing on the control 10484 * device at all? 10485 */ 10486 if ((lun == NULL) 10487 || (lun->be_lun->lun_type != T_PROCESSOR)) 10488 inq_ptr->flags |= SID_CmdQue; 10489 /* 10490 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10491 * We have 8 bytes for the vendor name, and 16 bytes for the device 10492 * name and 4 bytes for the revision. 10493 */ 10494 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10495 "vendor")) == NULL) { 10496 strcpy(inq_ptr->vendor, CTL_VENDOR); 10497 } else { 10498 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10499 strncpy(inq_ptr->vendor, val, 10500 min(sizeof(inq_ptr->vendor), strlen(val))); 10501 } 10502 if (lun == NULL) { 10503 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10504 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10505 switch (lun->be_lun->lun_type) { 10506 case T_DIRECT: 10507 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10508 break; 10509 case T_PROCESSOR: 10510 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10511 break; 10512 default: 10513 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10514 break; 10515 } 10516 } else { 10517 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10518 strncpy(inq_ptr->product, val, 10519 min(sizeof(inq_ptr->product), strlen(val))); 10520 } 10521 10522 /* 10523 * XXX make this a macro somewhere so it automatically gets 10524 * incremented when we make changes. 10525 */ 10526 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10527 "revision")) == NULL) { 10528 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10529 } else { 10530 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10531 strncpy(inq_ptr->revision, val, 10532 min(sizeof(inq_ptr->revision), strlen(val))); 10533 } 10534 10535 /* 10536 * For parallel SCSI, we support double transition and single 10537 * transition clocking. We also support QAS (Quick Arbitration 10538 * and Selection) and Information Unit transfers on both the 10539 * control and array devices. 10540 */ 10541 if (port_type == CTL_PORT_SCSI) 10542 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10543 SID_SPI_IUS; 10544 10545 /* SAM-5 (no version claimed) */ 10546 scsi_ulto2b(0x00A0, inq_ptr->version1); 10547 /* SPC-4 (no version claimed) */ 10548 scsi_ulto2b(0x0460, inq_ptr->version2); 10549 if (port_type == CTL_PORT_FC) { 10550 /* FCP-2 ANSI INCITS.350:2003 */ 10551 scsi_ulto2b(0x0917, inq_ptr->version3); 10552 } else if (port_type == CTL_PORT_SCSI) { 10553 /* SPI-4 ANSI INCITS.362:200x */ 10554 scsi_ulto2b(0x0B56, inq_ptr->version3); 10555 } else if (port_type == CTL_PORT_ISCSI) { 10556 /* iSCSI (no version claimed) */ 10557 scsi_ulto2b(0x0960, inq_ptr->version3); 10558 } else if (port_type == CTL_PORT_SAS) { 10559 /* SAS (no version claimed) */ 10560 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10561 } 10562 10563 if (lun == NULL) { 10564 /* SBC-3 (no version claimed) */ 10565 scsi_ulto2b(0x04C0, inq_ptr->version4); 10566 } else { 10567 switch (lun->be_lun->lun_type) { 10568 case T_DIRECT: 10569 /* SBC-3 (no version claimed) */ 10570 scsi_ulto2b(0x04C0, inq_ptr->version4); 10571 break; 10572 case T_PROCESSOR: 10573 default: 10574 break; 10575 } 10576 } 10577 10578 ctsio->scsi_status = SCSI_STATUS_OK; 10579 if (ctsio->kern_data_len > 0) { 10580 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10581 ctsio->be_move_done = ctl_config_move_done; 10582 ctl_datamove((union ctl_io *)ctsio); 10583 } else { 10584 ctsio->io_hdr.status = CTL_SUCCESS; 10585 ctl_done((union ctl_io *)ctsio); 10586 } 10587 10588 return (CTL_RETVAL_COMPLETE); 10589} 10590 10591int 10592ctl_inquiry(struct ctl_scsiio *ctsio) 10593{ 10594 struct scsi_inquiry *cdb; 10595 int retval; 10596 10597 cdb = (struct scsi_inquiry *)ctsio->cdb; 10598 10599 retval = 0; 10600 10601 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10602 10603 /* 10604 * Right now, we don't support the CmdDt inquiry information. 10605 * This would be nice to support in the future. When we do 10606 * support it, we should change this test so that it checks to make 10607 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10608 */ 10609#ifdef notyet 10610 if (((cdb->byte2 & SI_EVPD) 10611 && (cdb->byte2 & SI_CMDDT))) 10612#endif 10613 if (cdb->byte2 & SI_CMDDT) { 10614 /* 10615 * Point to the SI_CMDDT bit. We might change this 10616 * when we support SI_CMDDT, but since both bits would be 10617 * "wrong", this should probably just stay as-is then. 10618 */ 10619 ctl_set_invalid_field(ctsio, 10620 /*sks_valid*/ 1, 10621 /*command*/ 1, 10622 /*field*/ 1, 10623 /*bit_valid*/ 1, 10624 /*bit*/ 1); 10625 ctl_done((union ctl_io *)ctsio); 10626 return (CTL_RETVAL_COMPLETE); 10627 } 10628 if (cdb->byte2 & SI_EVPD) 10629 retval = ctl_inquiry_evpd(ctsio); 10630#ifdef notyet 10631 else if (cdb->byte2 & SI_CMDDT) 10632 retval = ctl_inquiry_cmddt(ctsio); 10633#endif 10634 else 10635 retval = ctl_inquiry_std(ctsio); 10636 10637 return (retval); 10638} 10639 10640/* 10641 * For known CDB types, parse the LBA and length. 10642 */ 10643static int 10644ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10645{ 10646 if (io->io_hdr.io_type != CTL_IO_SCSI) 10647 return (1); 10648 10649 switch (io->scsiio.cdb[0]) { 10650 case COMPARE_AND_WRITE: { 10651 struct scsi_compare_and_write *cdb; 10652 10653 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10654 10655 *lba = scsi_8btou64(cdb->addr); 10656 *len = cdb->length; 10657 break; 10658 } 10659 case READ_6: 10660 case WRITE_6: { 10661 struct scsi_rw_6 *cdb; 10662 10663 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10664 10665 *lba = scsi_3btoul(cdb->addr); 10666 /* only 5 bits are valid in the most significant address byte */ 10667 *lba &= 0x1fffff; 10668 *len = cdb->length; 10669 break; 10670 } 10671 case READ_10: 10672 case WRITE_10: { 10673 struct scsi_rw_10 *cdb; 10674 10675 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10676 10677 *lba = scsi_4btoul(cdb->addr); 10678 *len = scsi_2btoul(cdb->length); 10679 break; 10680 } 10681 case WRITE_VERIFY_10: { 10682 struct scsi_write_verify_10 *cdb; 10683 10684 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10685 10686 *lba = scsi_4btoul(cdb->addr); 10687 *len = scsi_2btoul(cdb->length); 10688 break; 10689 } 10690 case READ_12: 10691 case WRITE_12: { 10692 struct scsi_rw_12 *cdb; 10693 10694 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10695 10696 *lba = scsi_4btoul(cdb->addr); 10697 *len = scsi_4btoul(cdb->length); 10698 break; 10699 } 10700 case WRITE_VERIFY_12: { 10701 struct scsi_write_verify_12 *cdb; 10702 10703 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10704 10705 *lba = scsi_4btoul(cdb->addr); 10706 *len = scsi_4btoul(cdb->length); 10707 break; 10708 } 10709 case READ_16: 10710 case WRITE_16: { 10711 struct scsi_rw_16 *cdb; 10712 10713 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10714 10715 *lba = scsi_8btou64(cdb->addr); 10716 *len = scsi_4btoul(cdb->length); 10717 break; 10718 } 10719 case WRITE_VERIFY_16: { 10720 struct scsi_write_verify_16 *cdb; 10721 10722 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10723 10724 10725 *lba = scsi_8btou64(cdb->addr); 10726 *len = scsi_4btoul(cdb->length); 10727 break; 10728 } 10729 case WRITE_SAME_10: { 10730 struct scsi_write_same_10 *cdb; 10731 10732 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10733 10734 *lba = scsi_4btoul(cdb->addr); 10735 *len = scsi_2btoul(cdb->length); 10736 break; 10737 } 10738 case WRITE_SAME_16: { 10739 struct scsi_write_same_16 *cdb; 10740 10741 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10742 10743 *lba = scsi_8btou64(cdb->addr); 10744 *len = scsi_4btoul(cdb->length); 10745 break; 10746 } 10747 case VERIFY_10: { 10748 struct scsi_verify_10 *cdb; 10749 10750 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10751 10752 *lba = scsi_4btoul(cdb->addr); 10753 *len = scsi_2btoul(cdb->length); 10754 break; 10755 } 10756 case VERIFY_12: { 10757 struct scsi_verify_12 *cdb; 10758 10759 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10760 10761 *lba = scsi_4btoul(cdb->addr); 10762 *len = scsi_4btoul(cdb->length); 10763 break; 10764 } 10765 case VERIFY_16: { 10766 struct scsi_verify_16 *cdb; 10767 10768 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10769 10770 *lba = scsi_8btou64(cdb->addr); 10771 *len = scsi_4btoul(cdb->length); 10772 break; 10773 } 10774 default: 10775 return (1); 10776 break; /* NOTREACHED */ 10777 } 10778 10779 return (0); 10780} 10781 10782static ctl_action 10783ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10784{ 10785 uint64_t endlba1, endlba2; 10786 10787 endlba1 = lba1 + len1 - 1; 10788 endlba2 = lba2 + len2 - 1; 10789 10790 if ((endlba1 < lba2) 10791 || (endlba2 < lba1)) 10792 return (CTL_ACTION_PASS); 10793 else 10794 return (CTL_ACTION_BLOCK); 10795} 10796 10797static ctl_action 10798ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10799{ 10800 uint64_t lba1, lba2; 10801 uint32_t len1, len2; 10802 int retval; 10803 10804 retval = ctl_get_lba_len(io1, &lba1, &len1); 10805 if (retval != 0) 10806 return (CTL_ACTION_ERROR); 10807 10808 retval = ctl_get_lba_len(io2, &lba2, &len2); 10809 if (retval != 0) 10810 return (CTL_ACTION_ERROR); 10811 10812 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10813} 10814 10815static ctl_action 10816ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10817{ 10818 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10819 ctl_serialize_action *serialize_row; 10820 10821 /* 10822 * The initiator attempted multiple untagged commands at the same 10823 * time. Can't do that. 10824 */ 10825 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10826 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10827 && ((pending_io->io_hdr.nexus.targ_port == 10828 ooa_io->io_hdr.nexus.targ_port) 10829 && (pending_io->io_hdr.nexus.initid.id == 10830 ooa_io->io_hdr.nexus.initid.id)) 10831 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10832 return (CTL_ACTION_OVERLAP); 10833 10834 /* 10835 * The initiator attempted to send multiple tagged commands with 10836 * the same ID. (It's fine if different initiators have the same 10837 * tag ID.) 10838 * 10839 * Even if all of those conditions are true, we don't kill the I/O 10840 * if the command ahead of us has been aborted. We won't end up 10841 * sending it to the FETD, and it's perfectly legal to resend a 10842 * command with the same tag number as long as the previous 10843 * instance of this tag number has been aborted somehow. 10844 */ 10845 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10846 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10847 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10848 && ((pending_io->io_hdr.nexus.targ_port == 10849 ooa_io->io_hdr.nexus.targ_port) 10850 && (pending_io->io_hdr.nexus.initid.id == 10851 ooa_io->io_hdr.nexus.initid.id)) 10852 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10853 return (CTL_ACTION_OVERLAP_TAG); 10854 10855 /* 10856 * If we get a head of queue tag, SAM-3 says that we should 10857 * immediately execute it. 10858 * 10859 * What happens if this command would normally block for some other 10860 * reason? e.g. a request sense with a head of queue tag 10861 * immediately after a write. Normally that would block, but this 10862 * will result in its getting executed immediately... 10863 * 10864 * We currently return "pass" instead of "skip", so we'll end up 10865 * going through the rest of the queue to check for overlapped tags. 10866 * 10867 * XXX KDM check for other types of blockage first?? 10868 */ 10869 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10870 return (CTL_ACTION_PASS); 10871 10872 /* 10873 * Ordered tags have to block until all items ahead of them 10874 * have completed. If we get called with an ordered tag, we always 10875 * block, if something else is ahead of us in the queue. 10876 */ 10877 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10878 return (CTL_ACTION_BLOCK); 10879 10880 /* 10881 * Simple tags get blocked until all head of queue and ordered tags 10882 * ahead of them have completed. I'm lumping untagged commands in 10883 * with simple tags here. XXX KDM is that the right thing to do? 10884 */ 10885 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10886 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10887 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10888 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10889 return (CTL_ACTION_BLOCK); 10890 10891 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10892 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10893 10894 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10895 10896 switch (serialize_row[pending_entry->seridx]) { 10897 case CTL_SER_BLOCK: 10898 return (CTL_ACTION_BLOCK); 10899 break; /* NOTREACHED */ 10900 case CTL_SER_EXTENT: 10901 return (ctl_extent_check(pending_io, ooa_io)); 10902 break; /* NOTREACHED */ 10903 case CTL_SER_PASS: 10904 return (CTL_ACTION_PASS); 10905 break; /* NOTREACHED */ 10906 case CTL_SER_SKIP: 10907 return (CTL_ACTION_SKIP); 10908 break; 10909 default: 10910 panic("invalid serialization value %d", 10911 serialize_row[pending_entry->seridx]); 10912 break; /* NOTREACHED */ 10913 } 10914 10915 return (CTL_ACTION_ERROR); 10916} 10917 10918/* 10919 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10920 * Assumptions: 10921 * - pending_io is generally either incoming, or on the blocked queue 10922 * - starting I/O is the I/O we want to start the check with. 10923 */ 10924static ctl_action 10925ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10926 union ctl_io *starting_io) 10927{ 10928 union ctl_io *ooa_io; 10929 ctl_action action; 10930 10931 mtx_assert(&lun->lun_lock, MA_OWNED); 10932 10933 /* 10934 * Run back along the OOA queue, starting with the current 10935 * blocked I/O and going through every I/O before it on the 10936 * queue. If starting_io is NULL, we'll just end up returning 10937 * CTL_ACTION_PASS. 10938 */ 10939 for (ooa_io = starting_io; ooa_io != NULL; 10940 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10941 ooa_links)){ 10942 10943 /* 10944 * This routine just checks to see whether 10945 * cur_blocked is blocked by ooa_io, which is ahead 10946 * of it in the queue. It doesn't queue/dequeue 10947 * cur_blocked. 10948 */ 10949 action = ctl_check_for_blockage(pending_io, ooa_io); 10950 switch (action) { 10951 case CTL_ACTION_BLOCK: 10952 case CTL_ACTION_OVERLAP: 10953 case CTL_ACTION_OVERLAP_TAG: 10954 case CTL_ACTION_SKIP: 10955 case CTL_ACTION_ERROR: 10956 return (action); 10957 break; /* NOTREACHED */ 10958 case CTL_ACTION_PASS: 10959 break; 10960 default: 10961 panic("invalid action %d", action); 10962 break; /* NOTREACHED */ 10963 } 10964 } 10965 10966 return (CTL_ACTION_PASS); 10967} 10968 10969/* 10970 * Assumptions: 10971 * - An I/O has just completed, and has been removed from the per-LUN OOA 10972 * queue, so some items on the blocked queue may now be unblocked. 10973 */ 10974static int 10975ctl_check_blocked(struct ctl_lun *lun) 10976{ 10977 union ctl_io *cur_blocked, *next_blocked; 10978 10979 mtx_assert(&lun->lun_lock, MA_OWNED); 10980 10981 /* 10982 * Run forward from the head of the blocked queue, checking each 10983 * entry against the I/Os prior to it on the OOA queue to see if 10984 * there is still any blockage. 10985 * 10986 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10987 * with our removing a variable on it while it is traversing the 10988 * list. 10989 */ 10990 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10991 cur_blocked != NULL; cur_blocked = next_blocked) { 10992 union ctl_io *prev_ooa; 10993 ctl_action action; 10994 10995 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10996 blocked_links); 10997 10998 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10999 ctl_ooaq, ooa_links); 11000 11001 /* 11002 * If cur_blocked happens to be the first item in the OOA 11003 * queue now, prev_ooa will be NULL, and the action 11004 * returned will just be CTL_ACTION_PASS. 11005 */ 11006 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11007 11008 switch (action) { 11009 case CTL_ACTION_BLOCK: 11010 /* Nothing to do here, still blocked */ 11011 break; 11012 case CTL_ACTION_OVERLAP: 11013 case CTL_ACTION_OVERLAP_TAG: 11014 /* 11015 * This shouldn't happen! In theory we've already 11016 * checked this command for overlap... 11017 */ 11018 break; 11019 case CTL_ACTION_PASS: 11020 case CTL_ACTION_SKIP: { 11021 struct ctl_softc *softc; 11022 const struct ctl_cmd_entry *entry; 11023 uint32_t initidx; 11024 int isc_retval; 11025 11026 /* 11027 * The skip case shouldn't happen, this transaction 11028 * should have never made it onto the blocked queue. 11029 */ 11030 /* 11031 * This I/O is no longer blocked, we can remove it 11032 * from the blocked queue. Since this is a TAILQ 11033 * (doubly linked list), we can do O(1) removals 11034 * from any place on the list. 11035 */ 11036 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11037 blocked_links); 11038 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11039 11040 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11041 /* 11042 * Need to send IO back to original side to 11043 * run 11044 */ 11045 union ctl_ha_msg msg_info; 11046 11047 msg_info.hdr.original_sc = 11048 cur_blocked->io_hdr.original_sc; 11049 msg_info.hdr.serializing_sc = cur_blocked; 11050 msg_info.hdr.msg_type = CTL_MSG_R2R; 11051 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11052 &msg_info, sizeof(msg_info), 0)) > 11053 CTL_HA_STATUS_SUCCESS) { 11054 printf("CTL:Check Blocked error from " 11055 "ctl_ha_msg_send %d\n", 11056 isc_retval); 11057 } 11058 break; 11059 } 11060 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 11061 softc = control_softc; 11062 11063 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11064 11065 /* 11066 * Check this I/O for LUN state changes that may 11067 * have happened while this command was blocked. 11068 * The LUN state may have been changed by a command 11069 * ahead of us in the queue, so we need to re-check 11070 * for any states that can be caused by SCSI 11071 * commands. 11072 */ 11073 if (ctl_scsiio_lun_check(softc, lun, entry, 11074 &cur_blocked->scsiio) == 0) { 11075 cur_blocked->io_hdr.flags |= 11076 CTL_FLAG_IS_WAS_ON_RTR; 11077 ctl_enqueue_rtr(cur_blocked); 11078 } else 11079 ctl_done(cur_blocked); 11080 break; 11081 } 11082 default: 11083 /* 11084 * This probably shouldn't happen -- we shouldn't 11085 * get CTL_ACTION_ERROR, or anything else. 11086 */ 11087 break; 11088 } 11089 } 11090 11091 return (CTL_RETVAL_COMPLETE); 11092} 11093 11094/* 11095 * This routine (with one exception) checks LUN flags that can be set by 11096 * commands ahead of us in the OOA queue. These flags have to be checked 11097 * when a command initially comes in, and when we pull a command off the 11098 * blocked queue and are preparing to execute it. The reason we have to 11099 * check these flags for commands on the blocked queue is that the LUN 11100 * state may have been changed by a command ahead of us while we're on the 11101 * blocked queue. 11102 * 11103 * Ordering is somewhat important with these checks, so please pay 11104 * careful attention to the placement of any new checks. 11105 */ 11106static int 11107ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11108 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11109{ 11110 int retval; 11111 11112 retval = 0; 11113 11114 mtx_assert(&lun->lun_lock, MA_OWNED); 11115 11116 /* 11117 * If this shelf is a secondary shelf controller, we have to reject 11118 * any media access commands. 11119 */ 11120#if 0 11121 /* No longer needed for HA */ 11122 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11123 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11124 ctl_set_lun_standby(ctsio); 11125 retval = 1; 11126 goto bailout; 11127 } 11128#endif 11129 11130 /* 11131 * Check for a reservation conflict. If this command isn't allowed 11132 * even on reserved LUNs, and if this initiator isn't the one who 11133 * reserved us, reject the command with a reservation conflict. 11134 */ 11135 if ((lun->flags & CTL_LUN_RESERVED) 11136 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11137 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11138 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11139 || (ctsio->io_hdr.nexus.targ_target.id != 11140 lun->rsv_nexus.targ_target.id)) { 11141 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11142 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11143 retval = 1; 11144 goto bailout; 11145 } 11146 } 11147 11148 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11149 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11150 uint32_t residx; 11151 11152 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11153 /* 11154 * if we aren't registered or it's a res holder type 11155 * reservation and this isn't the res holder then set a 11156 * conflict. 11157 * NOTE: Commands which might be allowed on write exclusive 11158 * type reservations are checked in the particular command 11159 * for a conflict. Read and SSU are the only ones. 11160 */ 11161 if (!lun->per_res[residx].registered 11162 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11163 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11164 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11165 retval = 1; 11166 goto bailout; 11167 } 11168 11169 } 11170 11171 if ((lun->flags & CTL_LUN_OFFLINE) 11172 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11173 ctl_set_lun_not_ready(ctsio); 11174 retval = 1; 11175 goto bailout; 11176 } 11177 11178 /* 11179 * If the LUN is stopped, see if this particular command is allowed 11180 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11181 */ 11182 if ((lun->flags & CTL_LUN_STOPPED) 11183 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11184 /* "Logical unit not ready, initializing cmd. required" */ 11185 ctl_set_lun_stopped(ctsio); 11186 retval = 1; 11187 goto bailout; 11188 } 11189 11190 if ((lun->flags & CTL_LUN_INOPERABLE) 11191 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11192 /* "Medium format corrupted" */ 11193 ctl_set_medium_format_corrupted(ctsio); 11194 retval = 1; 11195 goto bailout; 11196 } 11197 11198bailout: 11199 return (retval); 11200 11201} 11202 11203static void 11204ctl_failover_io(union ctl_io *io, int have_lock) 11205{ 11206 ctl_set_busy(&io->scsiio); 11207 ctl_done(io); 11208} 11209 11210static void 11211ctl_failover(void) 11212{ 11213 struct ctl_lun *lun; 11214 struct ctl_softc *ctl_softc; 11215 union ctl_io *next_io, *pending_io; 11216 union ctl_io *io; 11217 int lun_idx; 11218 int i; 11219 11220 ctl_softc = control_softc; 11221 11222 mtx_lock(&ctl_softc->ctl_lock); 11223 /* 11224 * Remove any cmds from the other SC from the rtr queue. These 11225 * will obviously only be for LUNs for which we're the primary. 11226 * We can't send status or get/send data for these commands. 11227 * Since they haven't been executed yet, we can just remove them. 11228 * We'll either abort them or delete them below, depending on 11229 * which HA mode we're in. 11230 */ 11231#ifdef notyet 11232 mtx_lock(&ctl_softc->queue_lock); 11233 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11234 io != NULL; io = next_io) { 11235 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11236 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11237 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11238 ctl_io_hdr, links); 11239 } 11240 mtx_unlock(&ctl_softc->queue_lock); 11241#endif 11242 11243 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11244 lun = ctl_softc->ctl_luns[lun_idx]; 11245 if (lun==NULL) 11246 continue; 11247 11248 /* 11249 * Processor LUNs are primary on both sides. 11250 * XXX will this always be true? 11251 */ 11252 if (lun->be_lun->lun_type == T_PROCESSOR) 11253 continue; 11254 11255 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11256 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11257 printf("FAILOVER: primary lun %d\n", lun_idx); 11258 /* 11259 * Remove all commands from the other SC. First from the 11260 * blocked queue then from the ooa queue. Once we have 11261 * removed them. Call ctl_check_blocked to see if there 11262 * is anything that can run. 11263 */ 11264 for (io = (union ctl_io *)TAILQ_FIRST( 11265 &lun->blocked_queue); io != NULL; io = next_io) { 11266 11267 next_io = (union ctl_io *)TAILQ_NEXT( 11268 &io->io_hdr, blocked_links); 11269 11270 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11271 TAILQ_REMOVE(&lun->blocked_queue, 11272 &io->io_hdr,blocked_links); 11273 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11274 TAILQ_REMOVE(&lun->ooa_queue, 11275 &io->io_hdr, ooa_links); 11276 11277 ctl_free_io(io); 11278 } 11279 } 11280 11281 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11282 io != NULL; io = next_io) { 11283 11284 next_io = (union ctl_io *)TAILQ_NEXT( 11285 &io->io_hdr, ooa_links); 11286 11287 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11288 11289 TAILQ_REMOVE(&lun->ooa_queue, 11290 &io->io_hdr, 11291 ooa_links); 11292 11293 ctl_free_io(io); 11294 } 11295 } 11296 ctl_check_blocked(lun); 11297 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11298 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11299 11300 printf("FAILOVER: primary lun %d\n", lun_idx); 11301 /* 11302 * Abort all commands from the other SC. We can't 11303 * send status back for them now. These should get 11304 * cleaned up when they are completed or come out 11305 * for a datamove operation. 11306 */ 11307 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11308 io != NULL; io = next_io) { 11309 next_io = (union ctl_io *)TAILQ_NEXT( 11310 &io->io_hdr, ooa_links); 11311 11312 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11313 io->io_hdr.flags |= CTL_FLAG_ABORT; 11314 } 11315 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11316 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11317 11318 printf("FAILOVER: secondary lun %d\n", lun_idx); 11319 11320 lun->flags |= CTL_LUN_PRIMARY_SC; 11321 11322 /* 11323 * We send all I/O that was sent to this controller 11324 * and redirected to the other side back with 11325 * busy status, and have the initiator retry it. 11326 * Figuring out how much data has been transferred, 11327 * etc. and picking up where we left off would be 11328 * very tricky. 11329 * 11330 * XXX KDM need to remove I/O from the blocked 11331 * queue as well! 11332 */ 11333 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11334 &lun->ooa_queue); pending_io != NULL; 11335 pending_io = next_io) { 11336 11337 next_io = (union ctl_io *)TAILQ_NEXT( 11338 &pending_io->io_hdr, ooa_links); 11339 11340 pending_io->io_hdr.flags &= 11341 ~CTL_FLAG_SENT_2OTHER_SC; 11342 11343 if (pending_io->io_hdr.flags & 11344 CTL_FLAG_IO_ACTIVE) { 11345 pending_io->io_hdr.flags |= 11346 CTL_FLAG_FAILOVER; 11347 } else { 11348 ctl_set_busy(&pending_io->scsiio); 11349 ctl_done(pending_io); 11350 } 11351 } 11352 11353 /* 11354 * Build Unit Attention 11355 */ 11356 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11357 lun->pending_ua[i] |= 11358 CTL_UA_ASYM_ACC_CHANGE; 11359 } 11360 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11361 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11362 printf("FAILOVER: secondary lun %d\n", lun_idx); 11363 /* 11364 * if the first io on the OOA is not on the RtR queue 11365 * add it. 11366 */ 11367 lun->flags |= CTL_LUN_PRIMARY_SC; 11368 11369 pending_io = (union ctl_io *)TAILQ_FIRST( 11370 &lun->ooa_queue); 11371 if (pending_io==NULL) { 11372 printf("Nothing on OOA queue\n"); 11373 continue; 11374 } 11375 11376 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11377 if ((pending_io->io_hdr.flags & 11378 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11379 pending_io->io_hdr.flags |= 11380 CTL_FLAG_IS_WAS_ON_RTR; 11381 ctl_enqueue_rtr(pending_io); 11382 } 11383#if 0 11384 else 11385 { 11386 printf("Tag 0x%04x is running\n", 11387 pending_io->scsiio.tag_num); 11388 } 11389#endif 11390 11391 next_io = (union ctl_io *)TAILQ_NEXT( 11392 &pending_io->io_hdr, ooa_links); 11393 for (pending_io=next_io; pending_io != NULL; 11394 pending_io = next_io) { 11395 pending_io->io_hdr.flags &= 11396 ~CTL_FLAG_SENT_2OTHER_SC; 11397 next_io = (union ctl_io *)TAILQ_NEXT( 11398 &pending_io->io_hdr, ooa_links); 11399 if (pending_io->io_hdr.flags & 11400 CTL_FLAG_IS_WAS_ON_RTR) { 11401#if 0 11402 printf("Tag 0x%04x is running\n", 11403 pending_io->scsiio.tag_num); 11404#endif 11405 continue; 11406 } 11407 11408 switch (ctl_check_ooa(lun, pending_io, 11409 (union ctl_io *)TAILQ_PREV( 11410 &pending_io->io_hdr, ctl_ooaq, 11411 ooa_links))) { 11412 11413 case CTL_ACTION_BLOCK: 11414 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11415 &pending_io->io_hdr, 11416 blocked_links); 11417 pending_io->io_hdr.flags |= 11418 CTL_FLAG_BLOCKED; 11419 break; 11420 case CTL_ACTION_PASS: 11421 case CTL_ACTION_SKIP: 11422 pending_io->io_hdr.flags |= 11423 CTL_FLAG_IS_WAS_ON_RTR; 11424 ctl_enqueue_rtr(pending_io); 11425 break; 11426 case CTL_ACTION_OVERLAP: 11427 ctl_set_overlapped_cmd( 11428 (struct ctl_scsiio *)pending_io); 11429 ctl_done(pending_io); 11430 break; 11431 case CTL_ACTION_OVERLAP_TAG: 11432 ctl_set_overlapped_tag( 11433 (struct ctl_scsiio *)pending_io, 11434 pending_io->scsiio.tag_num & 0xff); 11435 ctl_done(pending_io); 11436 break; 11437 case CTL_ACTION_ERROR: 11438 default: 11439 ctl_set_internal_failure( 11440 (struct ctl_scsiio *)pending_io, 11441 0, // sks_valid 11442 0); //retry count 11443 ctl_done(pending_io); 11444 break; 11445 } 11446 } 11447 11448 /* 11449 * Build Unit Attention 11450 */ 11451 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11452 lun->pending_ua[i] |= 11453 CTL_UA_ASYM_ACC_CHANGE; 11454 } 11455 } else { 11456 panic("Unhandled HA mode failover, LUN flags = %#x, " 11457 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11458 } 11459 } 11460 ctl_pause_rtr = 0; 11461 mtx_unlock(&ctl_softc->ctl_lock); 11462} 11463 11464static int 11465ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11466{ 11467 struct ctl_lun *lun; 11468 const struct ctl_cmd_entry *entry; 11469 uint32_t initidx, targ_lun; 11470 int retval; 11471 11472 retval = 0; 11473 11474 lun = NULL; 11475 11476 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11477 if ((targ_lun < CTL_MAX_LUNS) 11478 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11479 lun = ctl_softc->ctl_luns[targ_lun]; 11480 /* 11481 * If the LUN is invalid, pretend that it doesn't exist. 11482 * It will go away as soon as all pending I/O has been 11483 * completed. 11484 */ 11485 if (lun->flags & CTL_LUN_DISABLED) { 11486 lun = NULL; 11487 } else { 11488 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11489 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11490 lun->be_lun; 11491 if (lun->be_lun->lun_type == T_PROCESSOR) { 11492 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11493 } 11494 11495 /* 11496 * Every I/O goes into the OOA queue for a 11497 * particular LUN, and stays there until completion. 11498 */ 11499 mtx_lock(&lun->lun_lock); 11500 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11501 ooa_links); 11502 } 11503 } else { 11504 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11505 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11506 } 11507 11508 /* Get command entry and return error if it is unsuppotyed. */ 11509 entry = ctl_validate_command(ctsio); 11510 if (entry == NULL) { 11511 if (lun) 11512 mtx_unlock(&lun->lun_lock); 11513 return (retval); 11514 } 11515 11516 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11517 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11518 11519 /* 11520 * Check to see whether we can send this command to LUNs that don't 11521 * exist. This should pretty much only be the case for inquiry 11522 * and request sense. Further checks, below, really require having 11523 * a LUN, so we can't really check the command anymore. Just put 11524 * it on the rtr queue. 11525 */ 11526 if (lun == NULL) { 11527 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11528 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11529 ctl_enqueue_rtr((union ctl_io *)ctsio); 11530 return (retval); 11531 } 11532 11533 ctl_set_unsupported_lun(ctsio); 11534 ctl_done((union ctl_io *)ctsio); 11535 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11536 return (retval); 11537 } else { 11538 /* 11539 * Make sure we support this particular command on this LUN. 11540 * e.g., we don't support writes to the control LUN. 11541 */ 11542 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11543 mtx_unlock(&lun->lun_lock); 11544 ctl_set_invalid_opcode(ctsio); 11545 ctl_done((union ctl_io *)ctsio); 11546 return (retval); 11547 } 11548 } 11549 11550 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11551 11552#ifdef CTL_WITH_CA 11553 /* 11554 * If we've got a request sense, it'll clear the contingent 11555 * allegiance condition. Otherwise, if we have a CA condition for 11556 * this initiator, clear it, because it sent down a command other 11557 * than request sense. 11558 */ 11559 if ((ctsio->cdb[0] != REQUEST_SENSE) 11560 && (ctl_is_set(lun->have_ca, initidx))) 11561 ctl_clear_mask(lun->have_ca, initidx); 11562#endif 11563 11564 /* 11565 * If the command has this flag set, it handles its own unit 11566 * attention reporting, we shouldn't do anything. Otherwise we 11567 * check for any pending unit attentions, and send them back to the 11568 * initiator. We only do this when a command initially comes in, 11569 * not when we pull it off the blocked queue. 11570 * 11571 * According to SAM-3, section 5.3.2, the order that things get 11572 * presented back to the host is basically unit attentions caused 11573 * by some sort of reset event, busy status, reservation conflicts 11574 * or task set full, and finally any other status. 11575 * 11576 * One issue here is that some of the unit attentions we report 11577 * don't fall into the "reset" category (e.g. "reported luns data 11578 * has changed"). So reporting it here, before the reservation 11579 * check, may be technically wrong. I guess the only thing to do 11580 * would be to check for and report the reset events here, and then 11581 * check for the other unit attention types after we check for a 11582 * reservation conflict. 11583 * 11584 * XXX KDM need to fix this 11585 */ 11586 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11587 ctl_ua_type ua_type; 11588 11589 ua_type = lun->pending_ua[initidx]; 11590 if (ua_type != CTL_UA_NONE) { 11591 scsi_sense_data_type sense_format; 11592 11593 if (lun != NULL) 11594 sense_format = (lun->flags & 11595 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11596 SSD_TYPE_FIXED; 11597 else 11598 sense_format = SSD_TYPE_FIXED; 11599 11600 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11601 sense_format); 11602 if (ua_type != CTL_UA_NONE) { 11603 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11604 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11605 CTL_AUTOSENSE; 11606 ctsio->sense_len = SSD_FULL_SIZE; 11607 lun->pending_ua[initidx] &= ~ua_type; 11608 mtx_unlock(&lun->lun_lock); 11609 ctl_done((union ctl_io *)ctsio); 11610 return (retval); 11611 } 11612 } 11613 } 11614 11615 11616 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11617 mtx_unlock(&lun->lun_lock); 11618 ctl_done((union ctl_io *)ctsio); 11619 return (retval); 11620 } 11621 11622 /* 11623 * XXX CHD this is where we want to send IO to other side if 11624 * this LUN is secondary on this SC. We will need to make a copy 11625 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11626 * the copy we send as FROM_OTHER. 11627 * We also need to stuff the address of the original IO so we can 11628 * find it easily. Something similar will need be done on the other 11629 * side so when we are done we can find the copy. 11630 */ 11631 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11632 union ctl_ha_msg msg_info; 11633 int isc_retval; 11634 11635 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11636 11637 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11638 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11639#if 0 11640 printf("1. ctsio %p\n", ctsio); 11641#endif 11642 msg_info.hdr.serializing_sc = NULL; 11643 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11644 msg_info.scsi.tag_num = ctsio->tag_num; 11645 msg_info.scsi.tag_type = ctsio->tag_type; 11646 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11647 11648 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11649 11650 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11651 (void *)&msg_info, sizeof(msg_info), 0)) > 11652 CTL_HA_STATUS_SUCCESS) { 11653 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11654 isc_retval); 11655 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11656 } else { 11657#if 0 11658 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11659#endif 11660 } 11661 11662 /* 11663 * XXX KDM this I/O is off the incoming queue, but hasn't 11664 * been inserted on any other queue. We may need to come 11665 * up with a holding queue while we wait for serialization 11666 * so that we have an idea of what we're waiting for from 11667 * the other side. 11668 */ 11669 mtx_unlock(&lun->lun_lock); 11670 return (retval); 11671 } 11672 11673 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11674 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11675 ctl_ooaq, ooa_links))) { 11676 case CTL_ACTION_BLOCK: 11677 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11678 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11679 blocked_links); 11680 mtx_unlock(&lun->lun_lock); 11681 return (retval); 11682 case CTL_ACTION_PASS: 11683 case CTL_ACTION_SKIP: 11684 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11685 mtx_unlock(&lun->lun_lock); 11686 ctl_enqueue_rtr((union ctl_io *)ctsio); 11687 break; 11688 case CTL_ACTION_OVERLAP: 11689 mtx_unlock(&lun->lun_lock); 11690 ctl_set_overlapped_cmd(ctsio); 11691 ctl_done((union ctl_io *)ctsio); 11692 break; 11693 case CTL_ACTION_OVERLAP_TAG: 11694 mtx_unlock(&lun->lun_lock); 11695 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11696 ctl_done((union ctl_io *)ctsio); 11697 break; 11698 case CTL_ACTION_ERROR: 11699 default: 11700 mtx_unlock(&lun->lun_lock); 11701 ctl_set_internal_failure(ctsio, 11702 /*sks_valid*/ 0, 11703 /*retry_count*/ 0); 11704 ctl_done((union ctl_io *)ctsio); 11705 break; 11706 } 11707 return (retval); 11708} 11709 11710const struct ctl_cmd_entry * 11711ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11712{ 11713 const struct ctl_cmd_entry *entry; 11714 int service_action; 11715 11716 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11717 if (entry->flags & CTL_CMD_FLAG_SA5) { 11718 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11719 entry = &((const struct ctl_cmd_entry *) 11720 entry->execute)[service_action]; 11721 } 11722 return (entry); 11723} 11724 11725const struct ctl_cmd_entry * 11726ctl_validate_command(struct ctl_scsiio *ctsio) 11727{ 11728 const struct ctl_cmd_entry *entry; 11729 int i; 11730 uint8_t diff; 11731 11732 entry = ctl_get_cmd_entry(ctsio); 11733 if (entry->execute == NULL) { 11734 ctl_set_invalid_opcode(ctsio); 11735 ctl_done((union ctl_io *)ctsio); 11736 return (NULL); 11737 } 11738 KASSERT(entry->length > 0, 11739 ("Not defined length for command 0x%02x/0x%02x", 11740 ctsio->cdb[0], ctsio->cdb[1])); 11741 for (i = 1; i < entry->length; i++) { 11742 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11743 if (diff == 0) 11744 continue; 11745 ctl_set_invalid_field(ctsio, 11746 /*sks_valid*/ 1, 11747 /*command*/ 1, 11748 /*field*/ i, 11749 /*bit_valid*/ 1, 11750 /*bit*/ fls(diff) - 1); 11751 ctl_done((union ctl_io *)ctsio); 11752 return (NULL); 11753 } 11754 return (entry); 11755} 11756 11757static int 11758ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11759{ 11760 11761 switch (lun_type) { 11762 case T_PROCESSOR: 11763 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11764 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11765 return (0); 11766 break; 11767 case T_DIRECT: 11768 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11769 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11770 return (0); 11771 break; 11772 default: 11773 return (0); 11774 } 11775 return (1); 11776} 11777 11778static int 11779ctl_scsiio(struct ctl_scsiio *ctsio) 11780{ 11781 int retval; 11782 const struct ctl_cmd_entry *entry; 11783 11784 retval = CTL_RETVAL_COMPLETE; 11785 11786 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11787 11788 entry = ctl_get_cmd_entry(ctsio); 11789 11790 /* 11791 * If this I/O has been aborted, just send it straight to 11792 * ctl_done() without executing it. 11793 */ 11794 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11795 ctl_done((union ctl_io *)ctsio); 11796 goto bailout; 11797 } 11798 11799 /* 11800 * All the checks should have been handled by ctl_scsiio_precheck(). 11801 * We should be clear now to just execute the I/O. 11802 */ 11803 retval = entry->execute(ctsio); 11804 11805bailout: 11806 return (retval); 11807} 11808 11809/* 11810 * Since we only implement one target right now, a bus reset simply resets 11811 * our single target. 11812 */ 11813static int 11814ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11815{ 11816 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11817} 11818 11819static int 11820ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11821 ctl_ua_type ua_type) 11822{ 11823 struct ctl_lun *lun; 11824 int retval; 11825 11826 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11827 union ctl_ha_msg msg_info; 11828 11829 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11830 msg_info.hdr.nexus = io->io_hdr.nexus; 11831 if (ua_type==CTL_UA_TARG_RESET) 11832 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11833 else 11834 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11835 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11836 msg_info.hdr.original_sc = NULL; 11837 msg_info.hdr.serializing_sc = NULL; 11838 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11839 (void *)&msg_info, sizeof(msg_info), 0)) { 11840 } 11841 } 11842 retval = 0; 11843 11844 mtx_lock(&ctl_softc->ctl_lock); 11845 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11846 retval += ctl_lun_reset(lun, io, ua_type); 11847 mtx_unlock(&ctl_softc->ctl_lock); 11848 11849 return (retval); 11850} 11851 11852/* 11853 * The LUN should always be set. The I/O is optional, and is used to 11854 * distinguish between I/Os sent by this initiator, and by other 11855 * initiators. We set unit attention for initiators other than this one. 11856 * SAM-3 is vague on this point. It does say that a unit attention should 11857 * be established for other initiators when a LUN is reset (see section 11858 * 5.7.3), but it doesn't specifically say that the unit attention should 11859 * be established for this particular initiator when a LUN is reset. Here 11860 * is the relevant text, from SAM-3 rev 8: 11861 * 11862 * 5.7.2 When a SCSI initiator port aborts its own tasks 11863 * 11864 * When a SCSI initiator port causes its own task(s) to be aborted, no 11865 * notification that the task(s) have been aborted shall be returned to 11866 * the SCSI initiator port other than the completion response for the 11867 * command or task management function action that caused the task(s) to 11868 * be aborted and notification(s) associated with related effects of the 11869 * action (e.g., a reset unit attention condition). 11870 * 11871 * XXX KDM for now, we're setting unit attention for all initiators. 11872 */ 11873static int 11874ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11875{ 11876 union ctl_io *xio; 11877#if 0 11878 uint32_t initindex; 11879#endif 11880 int i; 11881 11882 mtx_lock(&lun->lun_lock); 11883 /* 11884 * Run through the OOA queue and abort each I/O. 11885 */ 11886#if 0 11887 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11888#endif 11889 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11890 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11891 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11892 } 11893 11894 /* 11895 * This version sets unit attention for every 11896 */ 11897#if 0 11898 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11899 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11900 if (initindex == i) 11901 continue; 11902 lun->pending_ua[i] |= ua_type; 11903 } 11904#endif 11905 11906 /* 11907 * A reset (any kind, really) clears reservations established with 11908 * RESERVE/RELEASE. It does not clear reservations established 11909 * with PERSISTENT RESERVE OUT, but we don't support that at the 11910 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11911 * reservations made with the RESERVE/RELEASE commands, because 11912 * those commands are obsolete in SPC-3. 11913 */ 11914 lun->flags &= ~CTL_LUN_RESERVED; 11915 11916 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11917#ifdef CTL_WITH_CA 11918 ctl_clear_mask(lun->have_ca, i); 11919#endif 11920 lun->pending_ua[i] |= ua_type; 11921 } 11922 mtx_unlock(&lun->lun_lock); 11923 11924 return (0); 11925} 11926 11927static int 11928ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11929 int other_sc) 11930{ 11931 union ctl_io *xio; 11932 int found; 11933 11934 mtx_assert(&lun->lun_lock, MA_OWNED); 11935 11936 /* 11937 * Run through the OOA queue and attempt to find the given I/O. 11938 * The target port, initiator ID, tag type and tag number have to 11939 * match the values that we got from the initiator. If we have an 11940 * untagged command to abort, simply abort the first untagged command 11941 * we come to. We only allow one untagged command at a time of course. 11942 */ 11943 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11944 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11945 11946 if ((targ_port == UINT32_MAX || 11947 targ_port == xio->io_hdr.nexus.targ_port) && 11948 (init_id == UINT32_MAX || 11949 init_id == xio->io_hdr.nexus.initid.id)) { 11950 if (targ_port != xio->io_hdr.nexus.targ_port || 11951 init_id != xio->io_hdr.nexus.initid.id) 11952 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11953 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11954 found = 1; 11955 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11956 union ctl_ha_msg msg_info; 11957 11958 msg_info.hdr.nexus = xio->io_hdr.nexus; 11959 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11960 msg_info.task.tag_num = xio->scsiio.tag_num; 11961 msg_info.task.tag_type = xio->scsiio.tag_type; 11962 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11963 msg_info.hdr.original_sc = NULL; 11964 msg_info.hdr.serializing_sc = NULL; 11965 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11966 (void *)&msg_info, sizeof(msg_info), 0); 11967 } 11968 } 11969 } 11970 return (found); 11971} 11972 11973static int 11974ctl_abort_task_set(union ctl_io *io) 11975{ 11976 struct ctl_softc *softc = control_softc; 11977 struct ctl_lun *lun; 11978 uint32_t targ_lun; 11979 11980 /* 11981 * Look up the LUN. 11982 */ 11983 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11984 mtx_lock(&softc->ctl_lock); 11985 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11986 lun = softc->ctl_luns[targ_lun]; 11987 else { 11988 mtx_unlock(&softc->ctl_lock); 11989 return (1); 11990 } 11991 11992 mtx_lock(&lun->lun_lock); 11993 mtx_unlock(&softc->ctl_lock); 11994 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11995 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11996 io->io_hdr.nexus.initid.id, 11997 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11998 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11999 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12000 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12001 } 12002 mtx_unlock(&lun->lun_lock); 12003 return (0); 12004} 12005 12006static int 12007ctl_i_t_nexus_reset(union ctl_io *io) 12008{ 12009 struct ctl_softc *softc = control_softc; 12010 struct ctl_lun *lun; 12011 uint32_t initindex; 12012 12013 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12014 mtx_lock(&softc->ctl_lock); 12015 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12016 mtx_lock(&lun->lun_lock); 12017 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12018 io->io_hdr.nexus.initid.id, 12019 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12020#ifdef CTL_WITH_CA 12021 ctl_clear_mask(lun->have_ca, initindex); 12022#endif 12023 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12024 mtx_unlock(&lun->lun_lock); 12025 } 12026 mtx_unlock(&softc->ctl_lock); 12027 return (0); 12028} 12029 12030static int 12031ctl_abort_task(union ctl_io *io) 12032{ 12033 union ctl_io *xio; 12034 struct ctl_lun *lun; 12035 struct ctl_softc *ctl_softc; 12036#if 0 12037 struct sbuf sb; 12038 char printbuf[128]; 12039#endif 12040 int found; 12041 uint32_t targ_lun; 12042 12043 ctl_softc = control_softc; 12044 found = 0; 12045 12046 /* 12047 * Look up the LUN. 12048 */ 12049 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12050 mtx_lock(&ctl_softc->ctl_lock); 12051 if ((targ_lun < CTL_MAX_LUNS) 12052 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12053 lun = ctl_softc->ctl_luns[targ_lun]; 12054 else { 12055 mtx_unlock(&ctl_softc->ctl_lock); 12056 return (1); 12057 } 12058 12059#if 0 12060 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12061 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12062#endif 12063 12064 mtx_lock(&lun->lun_lock); 12065 mtx_unlock(&ctl_softc->ctl_lock); 12066 /* 12067 * Run through the OOA queue and attempt to find the given I/O. 12068 * The target port, initiator ID, tag type and tag number have to 12069 * match the values that we got from the initiator. If we have an 12070 * untagged command to abort, simply abort the first untagged command 12071 * we come to. We only allow one untagged command at a time of course. 12072 */ 12073#if 0 12074 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12075#endif 12076 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12077 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12078#if 0 12079 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12080 12081 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12082 lun->lun, xio->scsiio.tag_num, 12083 xio->scsiio.tag_type, 12084 (xio->io_hdr.blocked_links.tqe_prev 12085 == NULL) ? "" : " BLOCKED", 12086 (xio->io_hdr.flags & 12087 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12088 (xio->io_hdr.flags & 12089 CTL_FLAG_ABORT) ? " ABORT" : "", 12090 (xio->io_hdr.flags & 12091 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12092 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12093 sbuf_finish(&sb); 12094 printf("%s\n", sbuf_data(&sb)); 12095#endif 12096 12097 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12098 && (xio->io_hdr.nexus.initid.id == 12099 io->io_hdr.nexus.initid.id)) { 12100 /* 12101 * If the abort says that the task is untagged, the 12102 * task in the queue must be untagged. Otherwise, 12103 * we just check to see whether the tag numbers 12104 * match. This is because the QLogic firmware 12105 * doesn't pass back the tag type in an abort 12106 * request. 12107 */ 12108#if 0 12109 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12110 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12111 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12112#endif 12113 /* 12114 * XXX KDM we've got problems with FC, because it 12115 * doesn't send down a tag type with aborts. So we 12116 * can only really go by the tag number... 12117 * This may cause problems with parallel SCSI. 12118 * Need to figure that out!! 12119 */ 12120 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12121 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12122 found = 1; 12123 if ((io->io_hdr.flags & 12124 CTL_FLAG_FROM_OTHER_SC) == 0 && 12125 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12126 union ctl_ha_msg msg_info; 12127 12128 io->io_hdr.flags |= 12129 CTL_FLAG_SENT_2OTHER_SC; 12130 msg_info.hdr.nexus = io->io_hdr.nexus; 12131 msg_info.task.task_action = 12132 CTL_TASK_ABORT_TASK; 12133 msg_info.task.tag_num = 12134 io->taskio.tag_num; 12135 msg_info.task.tag_type = 12136 io->taskio.tag_type; 12137 msg_info.hdr.msg_type = 12138 CTL_MSG_MANAGE_TASKS; 12139 msg_info.hdr.original_sc = NULL; 12140 msg_info.hdr.serializing_sc = NULL; 12141#if 0 12142 printf("Sent Abort to other side\n"); 12143#endif 12144 if (CTL_HA_STATUS_SUCCESS != 12145 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12146 (void *)&msg_info, 12147 sizeof(msg_info), 0)) { 12148 } 12149 } 12150#if 0 12151 printf("ctl_abort_task: found I/O to abort\n"); 12152#endif 12153 break; 12154 } 12155 } 12156 } 12157 mtx_unlock(&lun->lun_lock); 12158 12159 if (found == 0) { 12160 /* 12161 * This isn't really an error. It's entirely possible for 12162 * the abort and command completion to cross on the wire. 12163 * This is more of an informative/diagnostic error. 12164 */ 12165#if 0 12166 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12167 "%d:%d:%d:%d tag %d type %d\n", 12168 io->io_hdr.nexus.initid.id, 12169 io->io_hdr.nexus.targ_port, 12170 io->io_hdr.nexus.targ_target.id, 12171 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12172 io->taskio.tag_type); 12173#endif 12174 } 12175 return (0); 12176} 12177 12178static void 12179ctl_run_task(union ctl_io *io) 12180{ 12181 struct ctl_softc *ctl_softc = control_softc; 12182 int retval = 1; 12183 const char *task_desc; 12184 12185 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12186 12187 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12188 ("ctl_run_task: Unextected io_type %d\n", 12189 io->io_hdr.io_type)); 12190 12191 task_desc = ctl_scsi_task_string(&io->taskio); 12192 if (task_desc != NULL) { 12193#ifdef NEEDTOPORT 12194 csevent_log(CSC_CTL | CSC_SHELF_SW | 12195 CTL_TASK_REPORT, 12196 csevent_LogType_Trace, 12197 csevent_Severity_Information, 12198 csevent_AlertLevel_Green, 12199 csevent_FRU_Firmware, 12200 csevent_FRU_Unknown, 12201 "CTL: received task: %s",task_desc); 12202#endif 12203 } else { 12204#ifdef NEEDTOPORT 12205 csevent_log(CSC_CTL | CSC_SHELF_SW | 12206 CTL_TASK_REPORT, 12207 csevent_LogType_Trace, 12208 csevent_Severity_Information, 12209 csevent_AlertLevel_Green, 12210 csevent_FRU_Firmware, 12211 csevent_FRU_Unknown, 12212 "CTL: received unknown task " 12213 "type: %d (%#x)", 12214 io->taskio.task_action, 12215 io->taskio.task_action); 12216#endif 12217 } 12218 switch (io->taskio.task_action) { 12219 case CTL_TASK_ABORT_TASK: 12220 retval = ctl_abort_task(io); 12221 break; 12222 case CTL_TASK_ABORT_TASK_SET: 12223 case CTL_TASK_CLEAR_TASK_SET: 12224 retval = ctl_abort_task_set(io); 12225 break; 12226 case CTL_TASK_CLEAR_ACA: 12227 break; 12228 case CTL_TASK_I_T_NEXUS_RESET: 12229 retval = ctl_i_t_nexus_reset(io); 12230 break; 12231 case CTL_TASK_LUN_RESET: { 12232 struct ctl_lun *lun; 12233 uint32_t targ_lun; 12234 12235 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12236 mtx_lock(&ctl_softc->ctl_lock); 12237 if ((targ_lun < CTL_MAX_LUNS) 12238 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12239 lun = ctl_softc->ctl_luns[targ_lun]; 12240 else { 12241 mtx_unlock(&ctl_softc->ctl_lock); 12242 retval = 1; 12243 break; 12244 } 12245 12246 if (!(io->io_hdr.flags & 12247 CTL_FLAG_FROM_OTHER_SC)) { 12248 union ctl_ha_msg msg_info; 12249 12250 io->io_hdr.flags |= 12251 CTL_FLAG_SENT_2OTHER_SC; 12252 msg_info.hdr.msg_type = 12253 CTL_MSG_MANAGE_TASKS; 12254 msg_info.hdr.nexus = io->io_hdr.nexus; 12255 msg_info.task.task_action = 12256 CTL_TASK_LUN_RESET; 12257 msg_info.hdr.original_sc = NULL; 12258 msg_info.hdr.serializing_sc = NULL; 12259 if (CTL_HA_STATUS_SUCCESS != 12260 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12261 (void *)&msg_info, 12262 sizeof(msg_info), 0)) { 12263 } 12264 } 12265 12266 retval = ctl_lun_reset(lun, io, 12267 CTL_UA_LUN_RESET); 12268 mtx_unlock(&ctl_softc->ctl_lock); 12269 break; 12270 } 12271 case CTL_TASK_TARGET_RESET: 12272 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12273 break; 12274 case CTL_TASK_BUS_RESET: 12275 retval = ctl_bus_reset(ctl_softc, io); 12276 break; 12277 case CTL_TASK_PORT_LOGIN: 12278 break; 12279 case CTL_TASK_PORT_LOGOUT: 12280 break; 12281 default: 12282 printf("ctl_run_task: got unknown task management event %d\n", 12283 io->taskio.task_action); 12284 break; 12285 } 12286 if (retval == 0) 12287 io->io_hdr.status = CTL_SUCCESS; 12288 else 12289 io->io_hdr.status = CTL_ERROR; 12290 ctl_done(io); 12291} 12292 12293/* 12294 * For HA operation. Handle commands that come in from the other 12295 * controller. 12296 */ 12297static void 12298ctl_handle_isc(union ctl_io *io) 12299{ 12300 int free_io; 12301 struct ctl_lun *lun; 12302 struct ctl_softc *ctl_softc; 12303 uint32_t targ_lun; 12304 12305 ctl_softc = control_softc; 12306 12307 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12308 lun = ctl_softc->ctl_luns[targ_lun]; 12309 12310 switch (io->io_hdr.msg_type) { 12311 case CTL_MSG_SERIALIZE: 12312 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12313 break; 12314 case CTL_MSG_R2R: { 12315 const struct ctl_cmd_entry *entry; 12316 12317 /* 12318 * This is only used in SER_ONLY mode. 12319 */ 12320 free_io = 0; 12321 entry = ctl_get_cmd_entry(&io->scsiio); 12322 mtx_lock(&lun->lun_lock); 12323 if (ctl_scsiio_lun_check(ctl_softc, lun, 12324 entry, (struct ctl_scsiio *)io) != 0) { 12325 mtx_unlock(&lun->lun_lock); 12326 ctl_done(io); 12327 break; 12328 } 12329 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12330 mtx_unlock(&lun->lun_lock); 12331 ctl_enqueue_rtr(io); 12332 break; 12333 } 12334 case CTL_MSG_FINISH_IO: 12335 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12336 free_io = 0; 12337 ctl_done(io); 12338 } else { 12339 free_io = 1; 12340 mtx_lock(&lun->lun_lock); 12341 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12342 ooa_links); 12343 ctl_check_blocked(lun); 12344 mtx_unlock(&lun->lun_lock); 12345 } 12346 break; 12347 case CTL_MSG_PERS_ACTION: 12348 ctl_hndl_per_res_out_on_other_sc( 12349 (union ctl_ha_msg *)&io->presio.pr_msg); 12350 free_io = 1; 12351 break; 12352 case CTL_MSG_BAD_JUJU: 12353 free_io = 0; 12354 ctl_done(io); 12355 break; 12356 case CTL_MSG_DATAMOVE: 12357 /* Only used in XFER mode */ 12358 free_io = 0; 12359 ctl_datamove_remote(io); 12360 break; 12361 case CTL_MSG_DATAMOVE_DONE: 12362 /* Only used in XFER mode */ 12363 free_io = 0; 12364 io->scsiio.be_move_done(io); 12365 break; 12366 default: 12367 free_io = 1; 12368 printf("%s: Invalid message type %d\n", 12369 __func__, io->io_hdr.msg_type); 12370 break; 12371 } 12372 if (free_io) 12373 ctl_free_io(io); 12374 12375} 12376 12377 12378/* 12379 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12380 * there is no match. 12381 */ 12382static ctl_lun_error_pattern 12383ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12384{ 12385 const struct ctl_cmd_entry *entry; 12386 ctl_lun_error_pattern filtered_pattern, pattern; 12387 12388 pattern = desc->error_pattern; 12389 12390 /* 12391 * XXX KDM we need more data passed into this function to match a 12392 * custom pattern, and we actually need to implement custom pattern 12393 * matching. 12394 */ 12395 if (pattern & CTL_LUN_PAT_CMD) 12396 return (CTL_LUN_PAT_CMD); 12397 12398 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12399 return (CTL_LUN_PAT_ANY); 12400 12401 entry = ctl_get_cmd_entry(ctsio); 12402 12403 filtered_pattern = entry->pattern & pattern; 12404 12405 /* 12406 * If the user requested specific flags in the pattern (e.g. 12407 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12408 * flags. 12409 * 12410 * If the user did not specify any flags, it doesn't matter whether 12411 * or not the command supports the flags. 12412 */ 12413 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12414 (pattern & ~CTL_LUN_PAT_MASK)) 12415 return (CTL_LUN_PAT_NONE); 12416 12417 /* 12418 * If the user asked for a range check, see if the requested LBA 12419 * range overlaps with this command's LBA range. 12420 */ 12421 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12422 uint64_t lba1; 12423 uint32_t len1; 12424 ctl_action action; 12425 int retval; 12426 12427 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12428 if (retval != 0) 12429 return (CTL_LUN_PAT_NONE); 12430 12431 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12432 desc->lba_range.len); 12433 /* 12434 * A "pass" means that the LBA ranges don't overlap, so 12435 * this doesn't match the user's range criteria. 12436 */ 12437 if (action == CTL_ACTION_PASS) 12438 return (CTL_LUN_PAT_NONE); 12439 } 12440 12441 return (filtered_pattern); 12442} 12443 12444static void 12445ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12446{ 12447 struct ctl_error_desc *desc, *desc2; 12448 12449 mtx_assert(&lun->lun_lock, MA_OWNED); 12450 12451 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12452 ctl_lun_error_pattern pattern; 12453 /* 12454 * Check to see whether this particular command matches 12455 * the pattern in the descriptor. 12456 */ 12457 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12458 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12459 continue; 12460 12461 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12462 case CTL_LUN_INJ_ABORTED: 12463 ctl_set_aborted(&io->scsiio); 12464 break; 12465 case CTL_LUN_INJ_MEDIUM_ERR: 12466 ctl_set_medium_error(&io->scsiio); 12467 break; 12468 case CTL_LUN_INJ_UA: 12469 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12470 * OCCURRED */ 12471 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12472 break; 12473 case CTL_LUN_INJ_CUSTOM: 12474 /* 12475 * We're assuming the user knows what he is doing. 12476 * Just copy the sense information without doing 12477 * checks. 12478 */ 12479 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12480 ctl_min(sizeof(desc->custom_sense), 12481 sizeof(io->scsiio.sense_data))); 12482 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12483 io->scsiio.sense_len = SSD_FULL_SIZE; 12484 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12485 break; 12486 case CTL_LUN_INJ_NONE: 12487 default: 12488 /* 12489 * If this is an error injection type we don't know 12490 * about, clear the continuous flag (if it is set) 12491 * so it will get deleted below. 12492 */ 12493 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12494 break; 12495 } 12496 /* 12497 * By default, each error injection action is a one-shot 12498 */ 12499 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12500 continue; 12501 12502 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12503 12504 free(desc, M_CTL); 12505 } 12506} 12507 12508#ifdef CTL_IO_DELAY 12509static void 12510ctl_datamove_timer_wakeup(void *arg) 12511{ 12512 union ctl_io *io; 12513 12514 io = (union ctl_io *)arg; 12515 12516 ctl_datamove(io); 12517} 12518#endif /* CTL_IO_DELAY */ 12519 12520void 12521ctl_datamove(union ctl_io *io) 12522{ 12523 void (*fe_datamove)(union ctl_io *io); 12524 12525 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12526 12527 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12528 12529#ifdef CTL_TIME_IO 12530 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12531 char str[256]; 12532 char path_str[64]; 12533 struct sbuf sb; 12534 12535 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12536 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12537 12538 sbuf_cat(&sb, path_str); 12539 switch (io->io_hdr.io_type) { 12540 case CTL_IO_SCSI: 12541 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12542 sbuf_printf(&sb, "\n"); 12543 sbuf_cat(&sb, path_str); 12544 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12545 io->scsiio.tag_num, io->scsiio.tag_type); 12546 break; 12547 case CTL_IO_TASK: 12548 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12549 "Tag Type: %d\n", io->taskio.task_action, 12550 io->taskio.tag_num, io->taskio.tag_type); 12551 break; 12552 default: 12553 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12554 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12555 break; 12556 } 12557 sbuf_cat(&sb, path_str); 12558 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12559 (intmax_t)time_uptime - io->io_hdr.start_time); 12560 sbuf_finish(&sb); 12561 printf("%s", sbuf_data(&sb)); 12562 } 12563#endif /* CTL_TIME_IO */ 12564 12565#ifdef CTL_IO_DELAY 12566 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12567 struct ctl_lun *lun; 12568 12569 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12570 12571 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12572 } else { 12573 struct ctl_lun *lun; 12574 12575 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12576 if ((lun != NULL) 12577 && (lun->delay_info.datamove_delay > 0)) { 12578 struct callout *callout; 12579 12580 callout = (struct callout *)&io->io_hdr.timer_bytes; 12581 callout_init(callout, /*mpsafe*/ 1); 12582 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12583 callout_reset(callout, 12584 lun->delay_info.datamove_delay * hz, 12585 ctl_datamove_timer_wakeup, io); 12586 if (lun->delay_info.datamove_type == 12587 CTL_DELAY_TYPE_ONESHOT) 12588 lun->delay_info.datamove_delay = 0; 12589 return; 12590 } 12591 } 12592#endif 12593 12594 /* 12595 * This command has been aborted. Set the port status, so we fail 12596 * the data move. 12597 */ 12598 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12599 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12600 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12601 io->io_hdr.nexus.targ_port, 12602 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12603 io->io_hdr.nexus.targ_lun); 12604 io->io_hdr.port_status = 31337; 12605 /* 12606 * Note that the backend, in this case, will get the 12607 * callback in its context. In other cases it may get 12608 * called in the frontend's interrupt thread context. 12609 */ 12610 io->scsiio.be_move_done(io); 12611 return; 12612 } 12613 12614 /* 12615 * If we're in XFER mode and this I/O is from the other shelf 12616 * controller, we need to send the DMA to the other side to 12617 * actually transfer the data to/from the host. In serialize only 12618 * mode the transfer happens below CTL and ctl_datamove() is only 12619 * called on the machine that originally received the I/O. 12620 */ 12621 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12622 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12623 union ctl_ha_msg msg; 12624 uint32_t sg_entries_sent; 12625 int do_sg_copy; 12626 int i; 12627 12628 memset(&msg, 0, sizeof(msg)); 12629 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12630 msg.hdr.original_sc = io->io_hdr.original_sc; 12631 msg.hdr.serializing_sc = io; 12632 msg.hdr.nexus = io->io_hdr.nexus; 12633 msg.dt.flags = io->io_hdr.flags; 12634 /* 12635 * We convert everything into a S/G list here. We can't 12636 * pass by reference, only by value between controllers. 12637 * So we can't pass a pointer to the S/G list, only as many 12638 * S/G entries as we can fit in here. If it's possible for 12639 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12640 * then we need to break this up into multiple transfers. 12641 */ 12642 if (io->scsiio.kern_sg_entries == 0) { 12643 msg.dt.kern_sg_entries = 1; 12644 /* 12645 * If this is in cached memory, flush the cache 12646 * before we send the DMA request to the other 12647 * controller. We want to do this in either the 12648 * read or the write case. The read case is 12649 * straightforward. In the write case, we want to 12650 * make sure nothing is in the local cache that 12651 * could overwrite the DMAed data. 12652 */ 12653 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12654 /* 12655 * XXX KDM use bus_dmamap_sync() here. 12656 */ 12657 } 12658 12659 /* 12660 * Convert to a physical address if this is a 12661 * virtual address. 12662 */ 12663 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12664 msg.dt.sg_list[0].addr = 12665 io->scsiio.kern_data_ptr; 12666 } else { 12667 /* 12668 * XXX KDM use busdma here! 12669 */ 12670#if 0 12671 msg.dt.sg_list[0].addr = (void *) 12672 vtophys(io->scsiio.kern_data_ptr); 12673#endif 12674 } 12675 12676 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12677 do_sg_copy = 0; 12678 } else { 12679 struct ctl_sg_entry *sgl; 12680 12681 do_sg_copy = 1; 12682 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12683 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12684 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12685 /* 12686 * XXX KDM use bus_dmamap_sync() here. 12687 */ 12688 } 12689 } 12690 12691 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12692 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12693 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12694 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12695 msg.dt.sg_sequence = 0; 12696 12697 /* 12698 * Loop until we've sent all of the S/G entries. On the 12699 * other end, we'll recompose these S/G entries into one 12700 * contiguous list before passing it to the 12701 */ 12702 for (sg_entries_sent = 0; sg_entries_sent < 12703 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12704 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12705 sizeof(msg.dt.sg_list[0])), 12706 msg.dt.kern_sg_entries - sg_entries_sent); 12707 12708 if (do_sg_copy != 0) { 12709 struct ctl_sg_entry *sgl; 12710 int j; 12711 12712 sgl = (struct ctl_sg_entry *) 12713 io->scsiio.kern_data_ptr; 12714 /* 12715 * If this is in cached memory, flush the cache 12716 * before we send the DMA request to the other 12717 * controller. We want to do this in either 12718 * the * read or the write case. The read 12719 * case is straightforward. In the write 12720 * case, we want to make sure nothing is 12721 * in the local cache that could overwrite 12722 * the DMAed data. 12723 */ 12724 12725 for (i = sg_entries_sent, j = 0; 12726 i < msg.dt.cur_sg_entries; i++, j++) { 12727 if ((io->io_hdr.flags & 12728 CTL_FLAG_NO_DATASYNC) == 0) { 12729 /* 12730 * XXX KDM use bus_dmamap_sync() 12731 */ 12732 } 12733 if ((io->io_hdr.flags & 12734 CTL_FLAG_BUS_ADDR) == 0) { 12735 /* 12736 * XXX KDM use busdma. 12737 */ 12738#if 0 12739 msg.dt.sg_list[j].addr =(void *) 12740 vtophys(sgl[i].addr); 12741#endif 12742 } else { 12743 msg.dt.sg_list[j].addr = 12744 sgl[i].addr; 12745 } 12746 msg.dt.sg_list[j].len = sgl[i].len; 12747 } 12748 } 12749 12750 sg_entries_sent += msg.dt.cur_sg_entries; 12751 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12752 msg.dt.sg_last = 1; 12753 else 12754 msg.dt.sg_last = 0; 12755 12756 /* 12757 * XXX KDM drop and reacquire the lock here? 12758 */ 12759 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12760 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12761 /* 12762 * XXX do something here. 12763 */ 12764 } 12765 12766 msg.dt.sent_sg_entries = sg_entries_sent; 12767 } 12768 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12769 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12770 ctl_failover_io(io, /*have_lock*/ 0); 12771 12772 } else { 12773 12774 /* 12775 * Lookup the fe_datamove() function for this particular 12776 * front end. 12777 */ 12778 fe_datamove = 12779 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12780 12781 fe_datamove(io); 12782 } 12783} 12784 12785static void 12786ctl_send_datamove_done(union ctl_io *io, int have_lock) 12787{ 12788 union ctl_ha_msg msg; 12789 int isc_status; 12790 12791 memset(&msg, 0, sizeof(msg)); 12792 12793 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12794 msg.hdr.original_sc = io; 12795 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12796 msg.hdr.nexus = io->io_hdr.nexus; 12797 msg.hdr.status = io->io_hdr.status; 12798 msg.scsi.tag_num = io->scsiio.tag_num; 12799 msg.scsi.tag_type = io->scsiio.tag_type; 12800 msg.scsi.scsi_status = io->scsiio.scsi_status; 12801 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12802 sizeof(io->scsiio.sense_data)); 12803 msg.scsi.sense_len = io->scsiio.sense_len; 12804 msg.scsi.sense_residual = io->scsiio.sense_residual; 12805 msg.scsi.fetd_status = io->io_hdr.port_status; 12806 msg.scsi.residual = io->scsiio.residual; 12807 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12808 12809 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12810 ctl_failover_io(io, /*have_lock*/ have_lock); 12811 return; 12812 } 12813 12814 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12815 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12816 /* XXX do something if this fails */ 12817 } 12818 12819} 12820 12821/* 12822 * The DMA to the remote side is done, now we need to tell the other side 12823 * we're done so it can continue with its data movement. 12824 */ 12825static void 12826ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12827{ 12828 union ctl_io *io; 12829 12830 io = rq->context; 12831 12832 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12833 printf("%s: ISC DMA write failed with error %d", __func__, 12834 rq->ret); 12835 ctl_set_internal_failure(&io->scsiio, 12836 /*sks_valid*/ 1, 12837 /*retry_count*/ rq->ret); 12838 } 12839 12840 ctl_dt_req_free(rq); 12841 12842 /* 12843 * In this case, we had to malloc the memory locally. Free it. 12844 */ 12845 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12846 int i; 12847 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12848 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12849 } 12850 /* 12851 * The data is in local and remote memory, so now we need to send 12852 * status (good or back) back to the other side. 12853 */ 12854 ctl_send_datamove_done(io, /*have_lock*/ 0); 12855} 12856 12857/* 12858 * We've moved the data from the host/controller into local memory. Now we 12859 * need to push it over to the remote controller's memory. 12860 */ 12861static int 12862ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12863{ 12864 int retval; 12865 12866 retval = 0; 12867 12868 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12869 ctl_datamove_remote_write_cb); 12870 12871 return (retval); 12872} 12873 12874static void 12875ctl_datamove_remote_write(union ctl_io *io) 12876{ 12877 int retval; 12878 void (*fe_datamove)(union ctl_io *io); 12879 12880 /* 12881 * - Get the data from the host/HBA into local memory. 12882 * - DMA memory from the local controller to the remote controller. 12883 * - Send status back to the remote controller. 12884 */ 12885 12886 retval = ctl_datamove_remote_sgl_setup(io); 12887 if (retval != 0) 12888 return; 12889 12890 /* Switch the pointer over so the FETD knows what to do */ 12891 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12892 12893 /* 12894 * Use a custom move done callback, since we need to send completion 12895 * back to the other controller, not to the backend on this side. 12896 */ 12897 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12898 12899 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12900 12901 fe_datamove(io); 12902 12903 return; 12904 12905} 12906 12907static int 12908ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12909{ 12910#if 0 12911 char str[256]; 12912 char path_str[64]; 12913 struct sbuf sb; 12914#endif 12915 12916 /* 12917 * In this case, we had to malloc the memory locally. Free it. 12918 */ 12919 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12920 int i; 12921 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12922 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12923 } 12924 12925#if 0 12926 scsi_path_string(io, path_str, sizeof(path_str)); 12927 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12928 sbuf_cat(&sb, path_str); 12929 scsi_command_string(&io->scsiio, NULL, &sb); 12930 sbuf_printf(&sb, "\n"); 12931 sbuf_cat(&sb, path_str); 12932 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12933 io->scsiio.tag_num, io->scsiio.tag_type); 12934 sbuf_cat(&sb, path_str); 12935 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12936 io->io_hdr.flags, io->io_hdr.status); 12937 sbuf_finish(&sb); 12938 printk("%s", sbuf_data(&sb)); 12939#endif 12940 12941 12942 /* 12943 * The read is done, now we need to send status (good or bad) back 12944 * to the other side. 12945 */ 12946 ctl_send_datamove_done(io, /*have_lock*/ 0); 12947 12948 return (0); 12949} 12950 12951static void 12952ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12953{ 12954 union ctl_io *io; 12955 void (*fe_datamove)(union ctl_io *io); 12956 12957 io = rq->context; 12958 12959 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12960 printf("%s: ISC DMA read failed with error %d", __func__, 12961 rq->ret); 12962 ctl_set_internal_failure(&io->scsiio, 12963 /*sks_valid*/ 1, 12964 /*retry_count*/ rq->ret); 12965 } 12966 12967 ctl_dt_req_free(rq); 12968 12969 /* Switch the pointer over so the FETD knows what to do */ 12970 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12971 12972 /* 12973 * Use a custom move done callback, since we need to send completion 12974 * back to the other controller, not to the backend on this side. 12975 */ 12976 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12977 12978 /* XXX KDM add checks like the ones in ctl_datamove? */ 12979 12980 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12981 12982 fe_datamove(io); 12983} 12984 12985static int 12986ctl_datamove_remote_sgl_setup(union ctl_io *io) 12987{ 12988 struct ctl_sg_entry *local_sglist, *remote_sglist; 12989 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12990 struct ctl_softc *softc; 12991 int retval; 12992 int i; 12993 12994 retval = 0; 12995 softc = control_softc; 12996 12997 local_sglist = io->io_hdr.local_sglist; 12998 local_dma_sglist = io->io_hdr.local_dma_sglist; 12999 remote_sglist = io->io_hdr.remote_sglist; 13000 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13001 13002 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13003 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13004 local_sglist[i].len = remote_sglist[i].len; 13005 13006 /* 13007 * XXX Detect the situation where the RS-level I/O 13008 * redirector on the other side has already read the 13009 * data off of the AOR RS on this side, and 13010 * transferred it to remote (mirror) memory on the 13011 * other side. Since we already have the data in 13012 * memory here, we just need to use it. 13013 * 13014 * XXX KDM this can probably be removed once we 13015 * get the cache device code in and take the 13016 * current AOR implementation out. 13017 */ 13018#ifdef NEEDTOPORT 13019 if ((remote_sglist[i].addr >= 13020 (void *)vtophys(softc->mirr->addr)) 13021 && (remote_sglist[i].addr < 13022 ((void *)vtophys(softc->mirr->addr) + 13023 CacheMirrorOffset))) { 13024 local_sglist[i].addr = remote_sglist[i].addr - 13025 CacheMirrorOffset; 13026 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13027 CTL_FLAG_DATA_IN) 13028 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13029 } else { 13030 local_sglist[i].addr = remote_sglist[i].addr + 13031 CacheMirrorOffset; 13032 } 13033#endif 13034#if 0 13035 printf("%s: local %p, remote %p, len %d\n", 13036 __func__, local_sglist[i].addr, 13037 remote_sglist[i].addr, local_sglist[i].len); 13038#endif 13039 } 13040 } else { 13041 uint32_t len_to_go; 13042 13043 /* 13044 * In this case, we don't have automatically allocated 13045 * memory for this I/O on this controller. This typically 13046 * happens with internal CTL I/O -- e.g. inquiry, mode 13047 * sense, etc. Anything coming from RAIDCore will have 13048 * a mirror area available. 13049 */ 13050 len_to_go = io->scsiio.kern_data_len; 13051 13052 /* 13053 * Clear the no datasync flag, we have to use malloced 13054 * buffers. 13055 */ 13056 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13057 13058 /* 13059 * The difficult thing here is that the size of the various 13060 * S/G segments may be different than the size from the 13061 * remote controller. That'll make it harder when DMAing 13062 * the data back to the other side. 13063 */ 13064 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13065 sizeof(io->io_hdr.remote_sglist[0])) && 13066 (len_to_go > 0); i++) { 13067 local_sglist[i].len = ctl_min(len_to_go, 131072); 13068 CTL_SIZE_8B(local_dma_sglist[i].len, 13069 local_sglist[i].len); 13070 local_sglist[i].addr = 13071 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13072 13073 local_dma_sglist[i].addr = local_sglist[i].addr; 13074 13075 if (local_sglist[i].addr == NULL) { 13076 int j; 13077 13078 printf("malloc failed for %zd bytes!", 13079 local_dma_sglist[i].len); 13080 for (j = 0; j < i; j++) { 13081 free(local_sglist[j].addr, M_CTL); 13082 } 13083 ctl_set_internal_failure(&io->scsiio, 13084 /*sks_valid*/ 1, 13085 /*retry_count*/ 4857); 13086 retval = 1; 13087 goto bailout_error; 13088 13089 } 13090 /* XXX KDM do we need a sync here? */ 13091 13092 len_to_go -= local_sglist[i].len; 13093 } 13094 /* 13095 * Reset the number of S/G entries accordingly. The 13096 * original number of S/G entries is available in 13097 * rem_sg_entries. 13098 */ 13099 io->scsiio.kern_sg_entries = i; 13100 13101#if 0 13102 printf("%s: kern_sg_entries = %d\n", __func__, 13103 io->scsiio.kern_sg_entries); 13104 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13105 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13106 local_sglist[i].addr, local_sglist[i].len, 13107 local_dma_sglist[i].len); 13108#endif 13109 } 13110 13111 13112 return (retval); 13113 13114bailout_error: 13115 13116 ctl_send_datamove_done(io, /*have_lock*/ 0); 13117 13118 return (retval); 13119} 13120 13121static int 13122ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13123 ctl_ha_dt_cb callback) 13124{ 13125 struct ctl_ha_dt_req *rq; 13126 struct ctl_sg_entry *remote_sglist, *local_sglist; 13127 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13128 uint32_t local_used, remote_used, total_used; 13129 int retval; 13130 int i, j; 13131 13132 retval = 0; 13133 13134 rq = ctl_dt_req_alloc(); 13135 13136 /* 13137 * If we failed to allocate the request, and if the DMA didn't fail 13138 * anyway, set busy status. This is just a resource allocation 13139 * failure. 13140 */ 13141 if ((rq == NULL) 13142 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13143 ctl_set_busy(&io->scsiio); 13144 13145 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13146 13147 if (rq != NULL) 13148 ctl_dt_req_free(rq); 13149 13150 /* 13151 * The data move failed. We need to return status back 13152 * to the other controller. No point in trying to DMA 13153 * data to the remote controller. 13154 */ 13155 13156 ctl_send_datamove_done(io, /*have_lock*/ 0); 13157 13158 retval = 1; 13159 13160 goto bailout; 13161 } 13162 13163 local_sglist = io->io_hdr.local_sglist; 13164 local_dma_sglist = io->io_hdr.local_dma_sglist; 13165 remote_sglist = io->io_hdr.remote_sglist; 13166 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13167 local_used = 0; 13168 remote_used = 0; 13169 total_used = 0; 13170 13171 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13172 rq->ret = CTL_HA_STATUS_SUCCESS; 13173 rq->context = io; 13174 callback(rq); 13175 goto bailout; 13176 } 13177 13178 /* 13179 * Pull/push the data over the wire from/to the other controller. 13180 * This takes into account the possibility that the local and 13181 * remote sglists may not be identical in terms of the size of 13182 * the elements and the number of elements. 13183 * 13184 * One fundamental assumption here is that the length allocated for 13185 * both the local and remote sglists is identical. Otherwise, we've 13186 * essentially got a coding error of some sort. 13187 */ 13188 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13189 int isc_ret; 13190 uint32_t cur_len, dma_length; 13191 uint8_t *tmp_ptr; 13192 13193 rq->id = CTL_HA_DATA_CTL; 13194 rq->command = command; 13195 rq->context = io; 13196 13197 /* 13198 * Both pointers should be aligned. But it is possible 13199 * that the allocation length is not. They should both 13200 * also have enough slack left over at the end, though, 13201 * to round up to the next 8 byte boundary. 13202 */ 13203 cur_len = ctl_min(local_sglist[i].len - local_used, 13204 remote_sglist[j].len - remote_used); 13205 13206 /* 13207 * In this case, we have a size issue and need to decrease 13208 * the size, except in the case where we actually have less 13209 * than 8 bytes left. In that case, we need to increase 13210 * the DMA length to get the last bit. 13211 */ 13212 if ((cur_len & 0x7) != 0) { 13213 if (cur_len > 0x7) { 13214 cur_len = cur_len - (cur_len & 0x7); 13215 dma_length = cur_len; 13216 } else { 13217 CTL_SIZE_8B(dma_length, cur_len); 13218 } 13219 13220 } else 13221 dma_length = cur_len; 13222 13223 /* 13224 * If we had to allocate memory for this I/O, instead of using 13225 * the non-cached mirror memory, we'll need to flush the cache 13226 * before trying to DMA to the other controller. 13227 * 13228 * We could end up doing this multiple times for the same 13229 * segment if we have a larger local segment than remote 13230 * segment. That shouldn't be an issue. 13231 */ 13232 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13233 /* 13234 * XXX KDM use bus_dmamap_sync() here. 13235 */ 13236 } 13237 13238 rq->size = dma_length; 13239 13240 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13241 tmp_ptr += local_used; 13242 13243 /* Use physical addresses when talking to ISC hardware */ 13244 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13245 /* XXX KDM use busdma */ 13246#if 0 13247 rq->local = vtophys(tmp_ptr); 13248#endif 13249 } else 13250 rq->local = tmp_ptr; 13251 13252 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13253 tmp_ptr += remote_used; 13254 rq->remote = tmp_ptr; 13255 13256 rq->callback = NULL; 13257 13258 local_used += cur_len; 13259 if (local_used >= local_sglist[i].len) { 13260 i++; 13261 local_used = 0; 13262 } 13263 13264 remote_used += cur_len; 13265 if (remote_used >= remote_sglist[j].len) { 13266 j++; 13267 remote_used = 0; 13268 } 13269 total_used += cur_len; 13270 13271 if (total_used >= io->scsiio.kern_data_len) 13272 rq->callback = callback; 13273 13274 if ((rq->size & 0x7) != 0) { 13275 printf("%s: warning: size %d is not on 8b boundary\n", 13276 __func__, rq->size); 13277 } 13278 if (((uintptr_t)rq->local & 0x7) != 0) { 13279 printf("%s: warning: local %p not on 8b boundary\n", 13280 __func__, rq->local); 13281 } 13282 if (((uintptr_t)rq->remote & 0x7) != 0) { 13283 printf("%s: warning: remote %p not on 8b boundary\n", 13284 __func__, rq->local); 13285 } 13286#if 0 13287 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13288 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13289 rq->local, rq->remote, rq->size); 13290#endif 13291 13292 isc_ret = ctl_dt_single(rq); 13293 if (isc_ret == CTL_HA_STATUS_WAIT) 13294 continue; 13295 13296 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13297 rq->ret = CTL_HA_STATUS_SUCCESS; 13298 } else { 13299 rq->ret = isc_ret; 13300 } 13301 callback(rq); 13302 goto bailout; 13303 } 13304 13305bailout: 13306 return (retval); 13307 13308} 13309 13310static void 13311ctl_datamove_remote_read(union ctl_io *io) 13312{ 13313 int retval; 13314 int i; 13315 13316 /* 13317 * This will send an error to the other controller in the case of a 13318 * failure. 13319 */ 13320 retval = ctl_datamove_remote_sgl_setup(io); 13321 if (retval != 0) 13322 return; 13323 13324 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13325 ctl_datamove_remote_read_cb); 13326 if ((retval != 0) 13327 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13328 /* 13329 * Make sure we free memory if there was an error.. The 13330 * ctl_datamove_remote_xfer() function will send the 13331 * datamove done message, or call the callback with an 13332 * error if there is a problem. 13333 */ 13334 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13335 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13336 } 13337 13338 return; 13339} 13340 13341/* 13342 * Process a datamove request from the other controller. This is used for 13343 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13344 * first. Once that is complete, the data gets DMAed into the remote 13345 * controller's memory. For reads, we DMA from the remote controller's 13346 * memory into our memory first, and then move it out to the FETD. 13347 */ 13348static void 13349ctl_datamove_remote(union ctl_io *io) 13350{ 13351 struct ctl_softc *softc; 13352 13353 softc = control_softc; 13354 13355 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13356 13357 /* 13358 * Note that we look for an aborted I/O here, but don't do some of 13359 * the other checks that ctl_datamove() normally does. 13360 * We don't need to run the datamove delay code, since that should 13361 * have been done if need be on the other controller. 13362 */ 13363 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13364 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13365 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13366 io->io_hdr.nexus.targ_port, 13367 io->io_hdr.nexus.targ_target.id, 13368 io->io_hdr.nexus.targ_lun); 13369 io->io_hdr.port_status = 31338; 13370 ctl_send_datamove_done(io, /*have_lock*/ 0); 13371 return; 13372 } 13373 13374 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13375 ctl_datamove_remote_write(io); 13376 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13377 ctl_datamove_remote_read(io); 13378 } else { 13379 union ctl_ha_msg msg; 13380 struct scsi_sense_data *sense; 13381 uint8_t sks[3]; 13382 int retry_count; 13383 13384 memset(&msg, 0, sizeof(msg)); 13385 13386 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13387 msg.hdr.status = CTL_SCSI_ERROR; 13388 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13389 13390 retry_count = 4243; 13391 13392 sense = &msg.scsi.sense_data; 13393 sks[0] = SSD_SCS_VALID; 13394 sks[1] = (retry_count >> 8) & 0xff; 13395 sks[2] = retry_count & 0xff; 13396 13397 /* "Internal target failure" */ 13398 scsi_set_sense_data(sense, 13399 /*sense_format*/ SSD_TYPE_NONE, 13400 /*current_error*/ 1, 13401 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13402 /*asc*/ 0x44, 13403 /*ascq*/ 0x00, 13404 /*type*/ SSD_ELEM_SKS, 13405 /*size*/ sizeof(sks), 13406 /*data*/ sks, 13407 SSD_ELEM_NONE); 13408 13409 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13410 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13411 ctl_failover_io(io, /*have_lock*/ 1); 13412 return; 13413 } 13414 13415 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13416 CTL_HA_STATUS_SUCCESS) { 13417 /* XXX KDM what to do if this fails? */ 13418 } 13419 return; 13420 } 13421 13422} 13423 13424static int 13425ctl_process_done(union ctl_io *io) 13426{ 13427 struct ctl_lun *lun; 13428 struct ctl_softc *ctl_softc; 13429 void (*fe_done)(union ctl_io *io); 13430 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13431 13432 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13433 13434 fe_done = 13435 control_softc->ctl_ports[targ_port]->fe_done; 13436 13437#ifdef CTL_TIME_IO 13438 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13439 char str[256]; 13440 char path_str[64]; 13441 struct sbuf sb; 13442 13443 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13444 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13445 13446 sbuf_cat(&sb, path_str); 13447 switch (io->io_hdr.io_type) { 13448 case CTL_IO_SCSI: 13449 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13450 sbuf_printf(&sb, "\n"); 13451 sbuf_cat(&sb, path_str); 13452 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13453 io->scsiio.tag_num, io->scsiio.tag_type); 13454 break; 13455 case CTL_IO_TASK: 13456 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13457 "Tag Type: %d\n", io->taskio.task_action, 13458 io->taskio.tag_num, io->taskio.tag_type); 13459 break; 13460 default: 13461 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13462 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13463 break; 13464 } 13465 sbuf_cat(&sb, path_str); 13466 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13467 (intmax_t)time_uptime - io->io_hdr.start_time); 13468 sbuf_finish(&sb); 13469 printf("%s", sbuf_data(&sb)); 13470 } 13471#endif /* CTL_TIME_IO */ 13472 13473 switch (io->io_hdr.io_type) { 13474 case CTL_IO_SCSI: 13475 break; 13476 case CTL_IO_TASK: 13477 if (bootverbose || verbose > 0) 13478 ctl_io_error_print(io, NULL); 13479 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13480 ctl_free_io(io); 13481 else 13482 fe_done(io); 13483 return (CTL_RETVAL_COMPLETE); 13484 break; 13485 default: 13486 printf("ctl_process_done: invalid io type %d\n", 13487 io->io_hdr.io_type); 13488 panic("ctl_process_done: invalid io type %d\n", 13489 io->io_hdr.io_type); 13490 break; /* NOTREACHED */ 13491 } 13492 13493 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13494 if (lun == NULL) { 13495 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13496 io->io_hdr.nexus.targ_mapped_lun)); 13497 fe_done(io); 13498 goto bailout; 13499 } 13500 ctl_softc = lun->ctl_softc; 13501 13502 mtx_lock(&lun->lun_lock); 13503 13504 /* 13505 * Check to see if we have any errors to inject here. We only 13506 * inject errors for commands that don't already have errors set. 13507 */ 13508 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13509 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13510 ctl_inject_error(lun, io); 13511 13512 /* 13513 * XXX KDM how do we treat commands that aren't completed 13514 * successfully? 13515 * 13516 * XXX KDM should we also track I/O latency? 13517 */ 13518 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13519 io->io_hdr.io_type == CTL_IO_SCSI) { 13520#ifdef CTL_TIME_IO 13521 struct bintime cur_bt; 13522#endif 13523 int type; 13524 13525 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13526 CTL_FLAG_DATA_IN) 13527 type = CTL_STATS_READ; 13528 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13529 CTL_FLAG_DATA_OUT) 13530 type = CTL_STATS_WRITE; 13531 else 13532 type = CTL_STATS_NO_IO; 13533 13534 lun->stats.ports[targ_port].bytes[type] += 13535 io->scsiio.kern_total_len; 13536 lun->stats.ports[targ_port].operations[type]++; 13537#ifdef CTL_TIME_IO 13538 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13539 &io->io_hdr.dma_bt); 13540 lun->stats.ports[targ_port].num_dmas[type] += 13541 io->io_hdr.num_dmas; 13542 getbintime(&cur_bt); 13543 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13544 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13545#endif 13546 } 13547 13548 /* 13549 * Remove this from the OOA queue. 13550 */ 13551 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13552 13553 /* 13554 * Run through the blocked queue on this LUN and see if anything 13555 * has become unblocked, now that this transaction is done. 13556 */ 13557 ctl_check_blocked(lun); 13558 13559 /* 13560 * If the LUN has been invalidated, free it if there is nothing 13561 * left on its OOA queue. 13562 */ 13563 if ((lun->flags & CTL_LUN_INVALID) 13564 && TAILQ_EMPTY(&lun->ooa_queue)) { 13565 mtx_unlock(&lun->lun_lock); 13566 mtx_lock(&ctl_softc->ctl_lock); 13567 ctl_free_lun(lun); 13568 mtx_unlock(&ctl_softc->ctl_lock); 13569 } else 13570 mtx_unlock(&lun->lun_lock); 13571 13572 /* 13573 * If this command has been aborted, make sure we set the status 13574 * properly. The FETD is responsible for freeing the I/O and doing 13575 * whatever it needs to do to clean up its state. 13576 */ 13577 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13578 ctl_set_task_aborted(&io->scsiio); 13579 13580 /* 13581 * We print out status for every task management command. For SCSI 13582 * commands, we filter out any unit attention errors; they happen 13583 * on every boot, and would clutter up the log. Note: task 13584 * management commands aren't printed here, they are printed above, 13585 * since they should never even make it down here. 13586 */ 13587 switch (io->io_hdr.io_type) { 13588 case CTL_IO_SCSI: { 13589 int error_code, sense_key, asc, ascq; 13590 13591 sense_key = 0; 13592 13593 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13594 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13595 /* 13596 * Since this is just for printing, no need to 13597 * show errors here. 13598 */ 13599 scsi_extract_sense_len(&io->scsiio.sense_data, 13600 io->scsiio.sense_len, 13601 &error_code, 13602 &sense_key, 13603 &asc, 13604 &ascq, 13605 /*show_errors*/ 0); 13606 } 13607 13608 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13609 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13610 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13611 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13612 13613 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13614 ctl_softc->skipped_prints++; 13615 } else { 13616 uint32_t skipped_prints; 13617 13618 skipped_prints = ctl_softc->skipped_prints; 13619 13620 ctl_softc->skipped_prints = 0; 13621 ctl_softc->last_print_jiffies = time_uptime; 13622 13623 if (skipped_prints > 0) { 13624#ifdef NEEDTOPORT 13625 csevent_log(CSC_CTL | CSC_SHELF_SW | 13626 CTL_ERROR_REPORT, 13627 csevent_LogType_Trace, 13628 csevent_Severity_Information, 13629 csevent_AlertLevel_Green, 13630 csevent_FRU_Firmware, 13631 csevent_FRU_Unknown, 13632 "High CTL error volume, %d prints " 13633 "skipped", skipped_prints); 13634#endif 13635 } 13636 if (bootverbose || verbose > 0) 13637 ctl_io_error_print(io, NULL); 13638 } 13639 } 13640 break; 13641 } 13642 case CTL_IO_TASK: 13643 if (bootverbose || verbose > 0) 13644 ctl_io_error_print(io, NULL); 13645 break; 13646 default: 13647 break; 13648 } 13649 13650 /* 13651 * Tell the FETD or the other shelf controller we're done with this 13652 * command. Note that only SCSI commands get to this point. Task 13653 * management commands are completed above. 13654 * 13655 * We only send status to the other controller if we're in XFER 13656 * mode. In SER_ONLY mode, the I/O is done on the controller that 13657 * received the I/O (from CTL's perspective), and so the status is 13658 * generated there. 13659 * 13660 * XXX KDM if we hold the lock here, we could cause a deadlock 13661 * if the frontend comes back in in this context to queue 13662 * something. 13663 */ 13664 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13665 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13666 union ctl_ha_msg msg; 13667 13668 memset(&msg, 0, sizeof(msg)); 13669 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13670 msg.hdr.original_sc = io->io_hdr.original_sc; 13671 msg.hdr.nexus = io->io_hdr.nexus; 13672 msg.hdr.status = io->io_hdr.status; 13673 msg.scsi.scsi_status = io->scsiio.scsi_status; 13674 msg.scsi.tag_num = io->scsiio.tag_num; 13675 msg.scsi.tag_type = io->scsiio.tag_type; 13676 msg.scsi.sense_len = io->scsiio.sense_len; 13677 msg.scsi.sense_residual = io->scsiio.sense_residual; 13678 msg.scsi.residual = io->scsiio.residual; 13679 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13680 sizeof(io->scsiio.sense_data)); 13681 /* 13682 * We copy this whether or not this is an I/O-related 13683 * command. Otherwise, we'd have to go and check to see 13684 * whether it's a read/write command, and it really isn't 13685 * worth it. 13686 */ 13687 memcpy(&msg.scsi.lbalen, 13688 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13689 sizeof(msg.scsi.lbalen)); 13690 13691 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13692 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13693 /* XXX do something here */ 13694 } 13695 13696 ctl_free_io(io); 13697 } else 13698 fe_done(io); 13699 13700bailout: 13701 13702 return (CTL_RETVAL_COMPLETE); 13703} 13704 13705#ifdef CTL_WITH_CA 13706/* 13707 * Front end should call this if it doesn't do autosense. When the request 13708 * sense comes back in from the initiator, we'll dequeue this and send it. 13709 */ 13710int 13711ctl_queue_sense(union ctl_io *io) 13712{ 13713 struct ctl_lun *lun; 13714 struct ctl_softc *ctl_softc; 13715 uint32_t initidx, targ_lun; 13716 13717 ctl_softc = control_softc; 13718 13719 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13720 13721 /* 13722 * LUN lookup will likely move to the ctl_work_thread() once we 13723 * have our new queueing infrastructure (that doesn't put things on 13724 * a per-LUN queue initially). That is so that we can handle 13725 * things like an INQUIRY to a LUN that we don't have enabled. We 13726 * can't deal with that right now. 13727 */ 13728 mtx_lock(&ctl_softc->ctl_lock); 13729 13730 /* 13731 * If we don't have a LUN for this, just toss the sense 13732 * information. 13733 */ 13734 targ_lun = io->io_hdr.nexus.targ_lun; 13735 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13736 if ((targ_lun < CTL_MAX_LUNS) 13737 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13738 lun = ctl_softc->ctl_luns[targ_lun]; 13739 else 13740 goto bailout; 13741 13742 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13743 13744 mtx_lock(&lun->lun_lock); 13745 /* 13746 * Already have CA set for this LUN...toss the sense information. 13747 */ 13748 if (ctl_is_set(lun->have_ca, initidx)) { 13749 mtx_unlock(&lun->lun_lock); 13750 goto bailout; 13751 } 13752 13753 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13754 ctl_min(sizeof(lun->pending_sense[initidx]), 13755 sizeof(io->scsiio.sense_data))); 13756 ctl_set_mask(lun->have_ca, initidx); 13757 mtx_unlock(&lun->lun_lock); 13758 13759bailout: 13760 mtx_unlock(&ctl_softc->ctl_lock); 13761 13762 ctl_free_io(io); 13763 13764 return (CTL_RETVAL_COMPLETE); 13765} 13766#endif 13767 13768/* 13769 * Primary command inlet from frontend ports. All SCSI and task I/O 13770 * requests must go through this function. 13771 */ 13772int 13773ctl_queue(union ctl_io *io) 13774{ 13775 struct ctl_softc *ctl_softc; 13776 13777 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13778 13779 ctl_softc = control_softc; 13780 13781#ifdef CTL_TIME_IO 13782 io->io_hdr.start_time = time_uptime; 13783 getbintime(&io->io_hdr.start_bt); 13784#endif /* CTL_TIME_IO */ 13785 13786 /* Map FE-specific LUN ID into global one. */ 13787 io->io_hdr.nexus.targ_mapped_lun = 13788 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13789 13790 switch (io->io_hdr.io_type) { 13791 case CTL_IO_SCSI: 13792 case CTL_IO_TASK: 13793 ctl_enqueue_incoming(io); 13794 break; 13795 default: 13796 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13797 return (EINVAL); 13798 } 13799 13800 return (CTL_RETVAL_COMPLETE); 13801} 13802 13803#ifdef CTL_IO_DELAY 13804static void 13805ctl_done_timer_wakeup(void *arg) 13806{ 13807 union ctl_io *io; 13808 13809 io = (union ctl_io *)arg; 13810 ctl_done(io); 13811} 13812#endif /* CTL_IO_DELAY */ 13813 13814void 13815ctl_done(union ctl_io *io) 13816{ 13817 struct ctl_softc *ctl_softc; 13818 13819 ctl_softc = control_softc; 13820 13821 /* 13822 * Enable this to catch duplicate completion issues. 13823 */ 13824#if 0 13825 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13826 printf("%s: type %d msg %d cdb %x iptl: " 13827 "%d:%d:%d:%d tag 0x%04x " 13828 "flag %#x status %x\n", 13829 __func__, 13830 io->io_hdr.io_type, 13831 io->io_hdr.msg_type, 13832 io->scsiio.cdb[0], 13833 io->io_hdr.nexus.initid.id, 13834 io->io_hdr.nexus.targ_port, 13835 io->io_hdr.nexus.targ_target.id, 13836 io->io_hdr.nexus.targ_lun, 13837 (io->io_hdr.io_type == 13838 CTL_IO_TASK) ? 13839 io->taskio.tag_num : 13840 io->scsiio.tag_num, 13841 io->io_hdr.flags, 13842 io->io_hdr.status); 13843 } else 13844 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13845#endif 13846 13847 /* 13848 * This is an internal copy of an I/O, and should not go through 13849 * the normal done processing logic. 13850 */ 13851 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13852 return; 13853 13854 /* 13855 * We need to send a msg to the serializing shelf to finish the IO 13856 * as well. We don't send a finish message to the other shelf if 13857 * this is a task management command. Task management commands 13858 * aren't serialized in the OOA queue, but rather just executed on 13859 * both shelf controllers for commands that originated on that 13860 * controller. 13861 */ 13862 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13863 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13864 union ctl_ha_msg msg_io; 13865 13866 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13867 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13868 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13869 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13870 } 13871 /* continue on to finish IO */ 13872 } 13873#ifdef CTL_IO_DELAY 13874 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13875 struct ctl_lun *lun; 13876 13877 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13878 13879 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13880 } else { 13881 struct ctl_lun *lun; 13882 13883 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13884 13885 if ((lun != NULL) 13886 && (lun->delay_info.done_delay > 0)) { 13887 struct callout *callout; 13888 13889 callout = (struct callout *)&io->io_hdr.timer_bytes; 13890 callout_init(callout, /*mpsafe*/ 1); 13891 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13892 callout_reset(callout, 13893 lun->delay_info.done_delay * hz, 13894 ctl_done_timer_wakeup, io); 13895 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13896 lun->delay_info.done_delay = 0; 13897 return; 13898 } 13899 } 13900#endif /* CTL_IO_DELAY */ 13901 13902 ctl_enqueue_done(io); 13903} 13904 13905int 13906ctl_isc(struct ctl_scsiio *ctsio) 13907{ 13908 struct ctl_lun *lun; 13909 int retval; 13910 13911 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13912 13913 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13914 13915 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13916 13917 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13918 13919 return (retval); 13920} 13921 13922 13923static void 13924ctl_work_thread(void *arg) 13925{ 13926 struct ctl_thread *thr = (struct ctl_thread *)arg; 13927 struct ctl_softc *softc = thr->ctl_softc; 13928 union ctl_io *io; 13929 int retval; 13930 13931 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13932 13933 for (;;) { 13934 retval = 0; 13935 13936 /* 13937 * We handle the queues in this order: 13938 * - ISC 13939 * - done queue (to free up resources, unblock other commands) 13940 * - RtR queue 13941 * - incoming queue 13942 * 13943 * If those queues are empty, we break out of the loop and 13944 * go to sleep. 13945 */ 13946 mtx_lock(&thr->queue_lock); 13947 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13948 if (io != NULL) { 13949 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13950 mtx_unlock(&thr->queue_lock); 13951 ctl_handle_isc(io); 13952 continue; 13953 } 13954 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13955 if (io != NULL) { 13956 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13957 /* clear any blocked commands, call fe_done */ 13958 mtx_unlock(&thr->queue_lock); 13959 retval = ctl_process_done(io); 13960 continue; 13961 } 13962 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13963 if (io != NULL) { 13964 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13965 mtx_unlock(&thr->queue_lock); 13966 if (io->io_hdr.io_type == CTL_IO_TASK) 13967 ctl_run_task(io); 13968 else 13969 ctl_scsiio_precheck(softc, &io->scsiio); 13970 continue; 13971 } 13972 if (!ctl_pause_rtr) { 13973 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13974 if (io != NULL) { 13975 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13976 mtx_unlock(&thr->queue_lock); 13977 retval = ctl_scsiio(&io->scsiio); 13978 if (retval != CTL_RETVAL_COMPLETE) 13979 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13980 continue; 13981 } 13982 } 13983 13984 /* Sleep until we have something to do. */ 13985 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13986 } 13987} 13988 13989static void 13990ctl_lun_thread(void *arg) 13991{ 13992 struct ctl_softc *softc = (struct ctl_softc *)arg; 13993 struct ctl_be_lun *be_lun; 13994 int retval; 13995 13996 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13997 13998 for (;;) { 13999 retval = 0; 14000 mtx_lock(&softc->ctl_lock); 14001 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14002 if (be_lun != NULL) { 14003 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14004 mtx_unlock(&softc->ctl_lock); 14005 ctl_create_lun(be_lun); 14006 continue; 14007 } 14008 14009 /* Sleep until we have something to do. */ 14010 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14011 PDROP | PRIBIO, "-", 0); 14012 } 14013} 14014 14015static void 14016ctl_enqueue_incoming(union ctl_io *io) 14017{ 14018 struct ctl_softc *softc = control_softc; 14019 struct ctl_thread *thr; 14020 u_int idx; 14021 14022 idx = (io->io_hdr.nexus.targ_port * 127 + 14023 io->io_hdr.nexus.initid.id) % worker_threads; 14024 thr = &softc->threads[idx]; 14025 mtx_lock(&thr->queue_lock); 14026 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14027 mtx_unlock(&thr->queue_lock); 14028 wakeup(thr); 14029} 14030 14031static void 14032ctl_enqueue_rtr(union ctl_io *io) 14033{ 14034 struct ctl_softc *softc = control_softc; 14035 struct ctl_thread *thr; 14036 14037 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14038 mtx_lock(&thr->queue_lock); 14039 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14040 mtx_unlock(&thr->queue_lock); 14041 wakeup(thr); 14042} 14043 14044static void 14045ctl_enqueue_done(union ctl_io *io) 14046{ 14047 struct ctl_softc *softc = control_softc; 14048 struct ctl_thread *thr; 14049 14050 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14051 mtx_lock(&thr->queue_lock); 14052 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14053 mtx_unlock(&thr->queue_lock); 14054 wakeup(thr); 14055} 14056 14057static void 14058ctl_enqueue_isc(union ctl_io *io) 14059{ 14060 struct ctl_softc *softc = control_softc; 14061 struct ctl_thread *thr; 14062 14063 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14064 mtx_lock(&thr->queue_lock); 14065 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14066 mtx_unlock(&thr->queue_lock); 14067 wakeup(thr); 14068} 14069 14070/* Initialization and failover */ 14071 14072void 14073ctl_init_isc_msg(void) 14074{ 14075 printf("CTL: Still calling this thing\n"); 14076} 14077 14078/* 14079 * Init component 14080 * Initializes component into configuration defined by bootMode 14081 * (see hasc-sv.c) 14082 * returns hasc_Status: 14083 * OK 14084 * ERROR - fatal error 14085 */ 14086static ctl_ha_comp_status 14087ctl_isc_init(struct ctl_ha_component *c) 14088{ 14089 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14090 14091 c->status = ret; 14092 return ret; 14093} 14094 14095/* Start component 14096 * Starts component in state requested. If component starts successfully, 14097 * it must set its own state to the requestrd state 14098 * When requested state is HASC_STATE_HA, the component may refine it 14099 * by adding _SLAVE or _MASTER flags. 14100 * Currently allowed state transitions are: 14101 * UNKNOWN->HA - initial startup 14102 * UNKNOWN->SINGLE - initial startup when no parter detected 14103 * HA->SINGLE - failover 14104 * returns ctl_ha_comp_status: 14105 * OK - component successfully started in requested state 14106 * FAILED - could not start the requested state, failover may 14107 * be possible 14108 * ERROR - fatal error detected, no future startup possible 14109 */ 14110static ctl_ha_comp_status 14111ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14112{ 14113 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14114 14115 printf("%s: go\n", __func__); 14116 14117 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14118 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14119 ctl_is_single = 0; 14120 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14121 != CTL_HA_STATUS_SUCCESS) { 14122 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14123 ret = CTL_HA_COMP_STATUS_ERROR; 14124 } 14125 } else if (CTL_HA_STATE_IS_HA(c->state) 14126 && CTL_HA_STATE_IS_SINGLE(state)){ 14127 // HA->SINGLE transition 14128 ctl_failover(); 14129 ctl_is_single = 1; 14130 } else { 14131 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14132 c->state, state); 14133 ret = CTL_HA_COMP_STATUS_ERROR; 14134 } 14135 if (CTL_HA_STATE_IS_SINGLE(state)) 14136 ctl_is_single = 1; 14137 14138 c->state = state; 14139 c->status = ret; 14140 return ret; 14141} 14142 14143/* 14144 * Quiesce component 14145 * The component must clear any error conditions (set status to OK) and 14146 * prepare itself to another Start call 14147 * returns ctl_ha_comp_status: 14148 * OK 14149 * ERROR 14150 */ 14151static ctl_ha_comp_status 14152ctl_isc_quiesce(struct ctl_ha_component *c) 14153{ 14154 int ret = CTL_HA_COMP_STATUS_OK; 14155 14156 ctl_pause_rtr = 1; 14157 c->status = ret; 14158 return ret; 14159} 14160 14161struct ctl_ha_component ctl_ha_component_ctlisc = 14162{ 14163 .name = "CTL ISC", 14164 .state = CTL_HA_STATE_UNKNOWN, 14165 .init = ctl_isc_init, 14166 .start = ctl_isc_start, 14167 .quiesce = ctl_isc_quiesce 14168}; 14169 14170/* 14171 * vim: ts=8 14172 */
|