xref: /freebsd/sys/cam/ctl/ctl.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /*-
2  * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3  * Copyright (c) 2012 The FreeBSD Foundation
4  * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Edward Tomasz Napierala
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17  *    substantially similar to the "NO WARRANTY" disclaimer below
18  *    ("Disclaimer") and any redistribution must be conditioned upon
19  *    including a substantially similar Disclaimer requirement for further
20  *    binary redistribution.
21  *
22  * NO WARRANTY
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * $Id$
36  */
37 /*
38  * CAM Target Layer, a SCSI device emulation subsystem.
39  *
40  * Author: Ken Merry <ken@FreeBSD.org>
41  */
42 
43 #define _CTL_C
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
54 #include <sys/bio.h>
55 #include <sys/fcntl.h>
56 #include <sys/lock.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
61 #include <sys/conf.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
64 #include <sys/sbuf.h>
65 #include <sys/smp.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
68 #include <vm/uma.h>
69 
70 #include <cam/cam.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
85 
86 struct ctl_softc *control_softc = NULL;
87 
88 /*
89  * Template mode pages.
90  */
91 
92 /*
93  * Note that these are default values only.  The actual values will be
94  * filled in when the user does a mode sense.
95  */
96 const static struct copan_debugconf_subpage debugconf_page_default = {
97 	DBGCNF_PAGE_CODE | SMPH_SPF,	/* page_code */
98 	DBGCNF_SUBPAGE_CODE,		/* subpage */
99 	{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
100 	 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
101 	DBGCNF_VERSION,			/* page_version */
102 	{CTL_TIME_IO_DEFAULT_SECS>>8,
103 	 CTL_TIME_IO_DEFAULT_SECS>>0},	/* ctl_time_io_secs */
104 };
105 
106 const static struct copan_debugconf_subpage debugconf_page_changeable = {
107 	DBGCNF_PAGE_CODE | SMPH_SPF,	/* page_code */
108 	DBGCNF_SUBPAGE_CODE,		/* subpage */
109 	{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
110 	 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
111 	0,				/* page_version */
112 	{0xff,0xff},			/* ctl_time_io_secs */
113 };
114 
115 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
116 	/*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
117 	/*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
118 	/*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
119 	/*read_retry_count*/0,
120 	/*correction_span*/0,
121 	/*head_offset_count*/0,
122 	/*data_strobe_offset_cnt*/0,
123 	/*byte8*/SMS_RWER_LBPERE,
124 	/*write_retry_count*/0,
125 	/*reserved2*/0,
126 	/*recovery_time_limit*/{0, 0},
127 };
128 
129 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
130 	/*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
131 	/*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
132 	/*byte3*/0,
133 	/*read_retry_count*/0,
134 	/*correction_span*/0,
135 	/*head_offset_count*/0,
136 	/*data_strobe_offset_cnt*/0,
137 	/*byte8*/0,
138 	/*write_retry_count*/0,
139 	/*reserved2*/0,
140 	/*recovery_time_limit*/{0, 0},
141 };
142 
143 const static struct scsi_format_page format_page_default = {
144 	/*page_code*/SMS_FORMAT_DEVICE_PAGE,
145 	/*page_length*/sizeof(struct scsi_format_page) - 2,
146 	/*tracks_per_zone*/ {0, 0},
147 	/*alt_sectors_per_zone*/ {0, 0},
148 	/*alt_tracks_per_zone*/ {0, 0},
149 	/*alt_tracks_per_lun*/ {0, 0},
150 	/*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
151 			        CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
152 	/*bytes_per_sector*/ {0, 0},
153 	/*interleave*/ {0, 0},
154 	/*track_skew*/ {0, 0},
155 	/*cylinder_skew*/ {0, 0},
156 	/*flags*/ SFP_HSEC,
157 	/*reserved*/ {0, 0, 0}
158 };
159 
160 const static struct scsi_format_page format_page_changeable = {
161 	/*page_code*/SMS_FORMAT_DEVICE_PAGE,
162 	/*page_length*/sizeof(struct scsi_format_page) - 2,
163 	/*tracks_per_zone*/ {0, 0},
164 	/*alt_sectors_per_zone*/ {0, 0},
165 	/*alt_tracks_per_zone*/ {0, 0},
166 	/*alt_tracks_per_lun*/ {0, 0},
167 	/*sectors_per_track*/ {0, 0},
168 	/*bytes_per_sector*/ {0, 0},
169 	/*interleave*/ {0, 0},
170 	/*track_skew*/ {0, 0},
171 	/*cylinder_skew*/ {0, 0},
172 	/*flags*/ 0,
173 	/*reserved*/ {0, 0, 0}
174 };
175 
176 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
177 	/*page_code*/SMS_RIGID_DISK_PAGE,
178 	/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
179 	/*cylinders*/ {0, 0, 0},
180 	/*heads*/ CTL_DEFAULT_HEADS,
181 	/*start_write_precomp*/ {0, 0, 0},
182 	/*start_reduced_current*/ {0, 0, 0},
183 	/*step_rate*/ {0, 0},
184 	/*landing_zone_cylinder*/ {0, 0, 0},
185 	/*rpl*/ SRDP_RPL_DISABLED,
186 	/*rotational_offset*/ 0,
187 	/*reserved1*/ 0,
188 	/*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
189 			   CTL_DEFAULT_ROTATION_RATE & 0xff},
190 	/*reserved2*/ {0, 0}
191 };
192 
193 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
194 	/*page_code*/SMS_RIGID_DISK_PAGE,
195 	/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
196 	/*cylinders*/ {0, 0, 0},
197 	/*heads*/ 0,
198 	/*start_write_precomp*/ {0, 0, 0},
199 	/*start_reduced_current*/ {0, 0, 0},
200 	/*step_rate*/ {0, 0},
201 	/*landing_zone_cylinder*/ {0, 0, 0},
202 	/*rpl*/ 0,
203 	/*rotational_offset*/ 0,
204 	/*reserved1*/ 0,
205 	/*rotation_rate*/ {0, 0},
206 	/*reserved2*/ {0, 0}
207 };
208 
209 const static struct scsi_caching_page caching_page_default = {
210 	/*page_code*/SMS_CACHING_PAGE,
211 	/*page_length*/sizeof(struct scsi_caching_page) - 2,
212 	/*flags1*/ SCP_DISC | SCP_WCE,
213 	/*ret_priority*/ 0,
214 	/*disable_pf_transfer_len*/ {0xff, 0xff},
215 	/*min_prefetch*/ {0, 0},
216 	/*max_prefetch*/ {0xff, 0xff},
217 	/*max_pf_ceiling*/ {0xff, 0xff},
218 	/*flags2*/ 0,
219 	/*cache_segments*/ 0,
220 	/*cache_seg_size*/ {0, 0},
221 	/*reserved*/ 0,
222 	/*non_cache_seg_size*/ {0, 0, 0}
223 };
224 
225 const static struct scsi_caching_page caching_page_changeable = {
226 	/*page_code*/SMS_CACHING_PAGE,
227 	/*page_length*/sizeof(struct scsi_caching_page) - 2,
228 	/*flags1*/ SCP_WCE | SCP_RCD,
229 	/*ret_priority*/ 0,
230 	/*disable_pf_transfer_len*/ {0, 0},
231 	/*min_prefetch*/ {0, 0},
232 	/*max_prefetch*/ {0, 0},
233 	/*max_pf_ceiling*/ {0, 0},
234 	/*flags2*/ 0,
235 	/*cache_segments*/ 0,
236 	/*cache_seg_size*/ {0, 0},
237 	/*reserved*/ 0,
238 	/*non_cache_seg_size*/ {0, 0, 0}
239 };
240 
241 const static struct scsi_control_page control_page_default = {
242 	/*page_code*/SMS_CONTROL_MODE_PAGE,
243 	/*page_length*/sizeof(struct scsi_control_page) - 2,
244 	/*rlec*/0,
245 	/*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
246 	/*eca_and_aen*/0,
247 	/*flags4*/SCP_TAS,
248 	/*aen_holdoff_period*/{0, 0},
249 	/*busy_timeout_period*/{0, 0},
250 	/*extended_selftest_completion_time*/{0, 0}
251 };
252 
253 const static struct scsi_control_page control_page_changeable = {
254 	/*page_code*/SMS_CONTROL_MODE_PAGE,
255 	/*page_length*/sizeof(struct scsi_control_page) - 2,
256 	/*rlec*/SCP_DSENSE,
257 	/*queue_flags*/SCP_QUEUE_ALG_MASK,
258 	/*eca_and_aen*/SCP_SWP,
259 	/*flags4*/0,
260 	/*aen_holdoff_period*/{0, 0},
261 	/*busy_timeout_period*/{0, 0},
262 	/*extended_selftest_completion_time*/{0, 0}
263 };
264 
265 #define CTL_CEM_LEN	(sizeof(struct scsi_control_ext_page) - 4)
266 
267 const static struct scsi_control_ext_page control_ext_page_default = {
268 	/*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
269 	/*subpage_code*/0x01,
270 	/*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
271 	/*flags*/0,
272 	/*prio*/0,
273 	/*max_sense*/0
274 };
275 
276 const static struct scsi_control_ext_page control_ext_page_changeable = {
277 	/*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
278 	/*subpage_code*/0x01,
279 	/*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
280 	/*flags*/0,
281 	/*prio*/0,
282 	/*max_sense*/0
283 };
284 
285 const static struct scsi_info_exceptions_page ie_page_default = {
286 	/*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
287 	/*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
288 	/*info_flags*/SIEP_FLAGS_DEXCPT,
289 	/*mrie*/0,
290 	/*interval_timer*/{0, 0, 0, 0},
291 	/*report_count*/{0, 0, 0, 0}
292 };
293 
294 const static struct scsi_info_exceptions_page ie_page_changeable = {
295 	/*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
296 	/*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
297 	/*info_flags*/0,
298 	/*mrie*/0,
299 	/*interval_timer*/{0, 0, 0, 0},
300 	/*report_count*/{0, 0, 0, 0}
301 };
302 
303 #define CTL_LBPM_LEN	(sizeof(struct ctl_logical_block_provisioning_page) - 4)
304 
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 	/*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 	/*subpage_code*/0x02,
308 	/*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
309 	/*flags*/0,
310 	/*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
311 	/*descr*/{}},
312 	{{/*flags*/0,
313 	  /*resource*/0x01,
314 	  /*reserved*/{0, 0},
315 	  /*count*/{0, 0, 0, 0}},
316 	 {/*flags*/0,
317 	  /*resource*/0x02,
318 	  /*reserved*/{0, 0},
319 	  /*count*/{0, 0, 0, 0}},
320 	 {/*flags*/0,
321 	  /*resource*/0xf1,
322 	  /*reserved*/{0, 0},
323 	  /*count*/{0, 0, 0, 0}},
324 	 {/*flags*/0,
325 	  /*resource*/0xf2,
326 	  /*reserved*/{0, 0},
327 	  /*count*/{0, 0, 0, 0}}
328 	}
329 };
330 
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 	/*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 	/*subpage_code*/0x02,
334 	/*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 	/*flags*/0,
336 	/*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
337 	/*descr*/{}},
338 	{{/*flags*/0,
339 	  /*resource*/0,
340 	  /*reserved*/{0, 0},
341 	  /*count*/{0, 0, 0, 0}},
342 	 {/*flags*/0,
343 	  /*resource*/0,
344 	  /*reserved*/{0, 0},
345 	  /*count*/{0, 0, 0, 0}},
346 	 {/*flags*/0,
347 	  /*resource*/0,
348 	  /*reserved*/{0, 0},
349 	  /*count*/{0, 0, 0, 0}},
350 	 {/*flags*/0,
351 	  /*resource*/0,
352 	  /*reserved*/{0, 0},
353 	  /*count*/{0, 0, 0, 0}}
354 	}
355 };
356 
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 	/*page_code*/SMS_CDDVD_CAPS_PAGE,
359 	/*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
360 	/*caps1*/0x3f,
361 	/*caps2*/0x00,
362 	/*caps3*/0xf0,
363 	/*caps4*/0x00,
364 	/*caps5*/0x29,
365 	/*caps6*/0x00,
366 	/*obsolete*/{0, 0},
367 	/*nvol_levels*/{0, 0},
368 	/*buffer_size*/{8, 0},
369 	/*obsolete2*/{0, 0},
370 	/*reserved*/0,
371 	/*digital*/0,
372 	/*obsolete3*/0,
373 	/*copy_management*/0,
374 	/*reserved2*/0,
375 	/*rotation_control*/0,
376 	/*cur_write_speed*/0,
377 	/*num_speed_descr*/0,
378 };
379 
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 	/*page_code*/SMS_CDDVD_CAPS_PAGE,
382 	/*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
383 	/*caps1*/0,
384 	/*caps2*/0,
385 	/*caps3*/0,
386 	/*caps4*/0,
387 	/*caps5*/0,
388 	/*caps6*/0,
389 	/*obsolete*/{0, 0},
390 	/*nvol_levels*/{0, 0},
391 	/*buffer_size*/{0, 0},
392 	/*obsolete2*/{0, 0},
393 	/*reserved*/0,
394 	/*digital*/0,
395 	/*obsolete3*/0,
396 	/*copy_management*/0,
397 	/*reserved2*/0,
398 	/*rotation_control*/0,
399 	/*cur_write_speed*/0,
400 	/*num_speed_descr*/0,
401 };
402 
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
406     &worker_threads, 1, "Number of worker threads");
407 static int ctl_debug = CTL_DEBUG_NONE;
408 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
409     &ctl_debug, 0, "Enabled debug flags");
410 
411 /*
412  * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
413  * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
414  * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
415  * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
416  */
417 #define SCSI_EVPD_NUM_SUPPORTED_PAGES	10
418 
419 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
420 				  int param);
421 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
422 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
423 static int ctl_init(void);
424 void ctl_shutdown(void);
425 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
426 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
427 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
428 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
429 			      struct ctl_ooa *ooa_hdr,
430 			      struct ctl_ooa_entry *kern_entries);
431 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
432 		     struct thread *td);
433 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
434 			 struct ctl_be_lun *be_lun);
435 static int ctl_free_lun(struct ctl_lun *lun);
436 static void ctl_create_lun(struct ctl_be_lun *be_lun);
437 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
438 
439 static int ctl_do_mode_select(union ctl_io *io);
440 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
441 			   uint64_t res_key, uint64_t sa_res_key,
442 			   uint8_t type, uint32_t residx,
443 			   struct ctl_scsiio *ctsio,
444 			   struct scsi_per_res_out *cdb,
445 			   struct scsi_per_res_out_parms* param);
446 static void ctl_pro_preempt_other(struct ctl_lun *lun,
447 				  union ctl_ha_msg *msg);
448 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
449 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
450 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
451 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
452 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
453 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
454 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
455 					 int alloc_len);
456 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
457 					 int alloc_len);
458 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
459 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
460 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
461 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
462 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
463 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
464     bool seq);
465 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
466 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
467     union ctl_io *pending_io, union ctl_io *ooa_io);
468 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
469 				union ctl_io *starting_io);
470 static int ctl_check_blocked(struct ctl_lun *lun);
471 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
472 				const struct ctl_cmd_entry *entry,
473 				struct ctl_scsiio *ctsio);
474 static void ctl_failover_lun(union ctl_io *io);
475 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
476 			       struct ctl_scsiio *ctsio);
477 static int ctl_scsiio(struct ctl_scsiio *ctsio);
478 
479 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
480 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
481 			    ctl_ua_type ua_type);
482 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
483 			 ctl_ua_type ua_type);
484 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
485 static int ctl_abort_task(union ctl_io *io);
486 static int ctl_abort_task_set(union ctl_io *io);
487 static int ctl_query_task(union ctl_io *io, int task_set);
488 static int ctl_i_t_nexus_reset(union ctl_io *io);
489 static int ctl_query_async_event(union ctl_io *io);
490 static void ctl_run_task(union ctl_io *io);
491 #ifdef CTL_IO_DELAY
492 static void ctl_datamove_timer_wakeup(void *arg);
493 static void ctl_done_timer_wakeup(void *arg);
494 #endif /* CTL_IO_DELAY */
495 
496 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
497 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
498 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
499 static void ctl_datamove_remote_write(union ctl_io *io);
500 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
501 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
502 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
503 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
504 				    ctl_ha_dt_cb callback);
505 static void ctl_datamove_remote_read(union ctl_io *io);
506 static void ctl_datamove_remote(union ctl_io *io);
507 static void ctl_process_done(union ctl_io *io);
508 static void ctl_lun_thread(void *arg);
509 static void ctl_thresh_thread(void *arg);
510 static void ctl_work_thread(void *arg);
511 static void ctl_enqueue_incoming(union ctl_io *io);
512 static void ctl_enqueue_rtr(union ctl_io *io);
513 static void ctl_enqueue_done(union ctl_io *io);
514 static void ctl_enqueue_isc(union ctl_io *io);
515 static const struct ctl_cmd_entry *
516     ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
517 static const struct ctl_cmd_entry *
518     ctl_validate_command(struct ctl_scsiio *ctsio);
519 static int ctl_cmd_applicable(uint8_t lun_type,
520     const struct ctl_cmd_entry *entry);
521 
522 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
523 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
524 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
525 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
526 
527 /*
528  * Load the serialization table.  This isn't very pretty, but is probably
529  * the easiest way to do it.
530  */
531 #include "ctl_ser_table.c"
532 
533 /*
534  * We only need to define open, close and ioctl routines for this driver.
535  */
536 static struct cdevsw ctl_cdevsw = {
537 	.d_version =	D_VERSION,
538 	.d_flags =	0,
539 	.d_open =	ctl_open,
540 	.d_close =	ctl_close,
541 	.d_ioctl =	ctl_ioctl,
542 	.d_name =	"ctl",
543 };
544 
545 
546 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
547 
548 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
549 
550 static moduledata_t ctl_moduledata = {
551 	"ctl",
552 	ctl_module_event_handler,
553 	NULL
554 };
555 
556 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
557 MODULE_VERSION(ctl, 1);
558 
559 static struct ctl_frontend ha_frontend =
560 {
561 	.name = "ha",
562 };
563 
564 static void
565 ctl_ha_datamove(union ctl_io *io)
566 {
567 	struct ctl_lun *lun;
568 	struct ctl_sg_entry *sgl;
569 	union ctl_ha_msg msg;
570 	uint32_t sg_entries_sent;
571 	int do_sg_copy, i, j;
572 
573 	lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
574 	memset(&msg.dt, 0, sizeof(msg.dt));
575 	msg.hdr.msg_type = CTL_MSG_DATAMOVE;
576 	msg.hdr.original_sc = io->io_hdr.original_sc;
577 	msg.hdr.serializing_sc = io;
578 	msg.hdr.nexus = io->io_hdr.nexus;
579 	msg.hdr.status = io->io_hdr.status;
580 	msg.dt.flags = io->io_hdr.flags;
581 
582 	/*
583 	 * We convert everything into a S/G list here.  We can't
584 	 * pass by reference, only by value between controllers.
585 	 * So we can't pass a pointer to the S/G list, only as many
586 	 * S/G entries as we can fit in here.  If it's possible for
587 	 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
588 	 * then we need to break this up into multiple transfers.
589 	 */
590 	if (io->scsiio.kern_sg_entries == 0) {
591 		msg.dt.kern_sg_entries = 1;
592 #if 0
593 		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
594 			msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
595 		} else {
596 			/* XXX KDM use busdma here! */
597 			msg.dt.sg_list[0].addr =
598 			    (void *)vtophys(io->scsiio.kern_data_ptr);
599 		}
600 #else
601 		KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
602 		    ("HA does not support BUS_ADDR"));
603 		msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
604 #endif
605 		msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
606 		do_sg_copy = 0;
607 	} else {
608 		msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
609 		do_sg_copy = 1;
610 	}
611 
612 	msg.dt.kern_data_len = io->scsiio.kern_data_len;
613 	msg.dt.kern_total_len = io->scsiio.kern_total_len;
614 	msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
615 	msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
616 	msg.dt.sg_sequence = 0;
617 
618 	/*
619 	 * Loop until we've sent all of the S/G entries.  On the
620 	 * other end, we'll recompose these S/G entries into one
621 	 * contiguous list before processing.
622 	 */
623 	for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
624 	    msg.dt.sg_sequence++) {
625 		msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
626 		    sizeof(msg.dt.sg_list[0])),
627 		    msg.dt.kern_sg_entries - sg_entries_sent);
628 		if (do_sg_copy != 0) {
629 			sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
630 			for (i = sg_entries_sent, j = 0;
631 			     i < msg.dt.cur_sg_entries; i++, j++) {
632 #if 0
633 				if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
634 					msg.dt.sg_list[j].addr = sgl[i].addr;
635 				} else {
636 					/* XXX KDM use busdma here! */
637 					msg.dt.sg_list[j].addr =
638 					    (void *)vtophys(sgl[i].addr);
639 				}
640 #else
641 				KASSERT((io->io_hdr.flags &
642 				    CTL_FLAG_BUS_ADDR) == 0,
643 				    ("HA does not support BUS_ADDR"));
644 				msg.dt.sg_list[j].addr = sgl[i].addr;
645 #endif
646 				msg.dt.sg_list[j].len = sgl[i].len;
647 			}
648 		}
649 
650 		sg_entries_sent += msg.dt.cur_sg_entries;
651 		msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
652 		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
653 		    sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
654 		    sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
655 		    M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
656 			io->io_hdr.port_status = 31341;
657 			io->scsiio.be_move_done(io);
658 			return;
659 		}
660 		msg.dt.sent_sg_entries = sg_entries_sent;
661 	}
662 
663 	/*
664 	 * Officially handover the request from us to peer.
665 	 * If failover has just happened, then we must return error.
666 	 * If failover happen just after, then it is not our problem.
667 	 */
668 	if (lun)
669 		mtx_lock(&lun->lun_lock);
670 	if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
671 		if (lun)
672 			mtx_unlock(&lun->lun_lock);
673 		io->io_hdr.port_status = 31342;
674 		io->scsiio.be_move_done(io);
675 		return;
676 	}
677 	io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
678 	io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
679 	if (lun)
680 		mtx_unlock(&lun->lun_lock);
681 }
682 
683 static void
684 ctl_ha_done(union ctl_io *io)
685 {
686 	union ctl_ha_msg msg;
687 
688 	if (io->io_hdr.io_type == CTL_IO_SCSI) {
689 		memset(&msg, 0, sizeof(msg));
690 		msg.hdr.msg_type = CTL_MSG_FINISH_IO;
691 		msg.hdr.original_sc = io->io_hdr.original_sc;
692 		msg.hdr.nexus = io->io_hdr.nexus;
693 		msg.hdr.status = io->io_hdr.status;
694 		msg.scsi.scsi_status = io->scsiio.scsi_status;
695 		msg.scsi.tag_num = io->scsiio.tag_num;
696 		msg.scsi.tag_type = io->scsiio.tag_type;
697 		msg.scsi.sense_len = io->scsiio.sense_len;
698 		msg.scsi.sense_residual = io->scsiio.sense_residual;
699 		msg.scsi.residual = io->scsiio.residual;
700 		memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
701 		    io->scsiio.sense_len);
702 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
703 		    sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
704 		    msg.scsi.sense_len, M_WAITOK);
705 	}
706 	ctl_free_io(io);
707 }
708 
709 static void
710 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
711 			    union ctl_ha_msg *msg_info)
712 {
713 	struct ctl_scsiio *ctsio;
714 
715 	if (msg_info->hdr.original_sc == NULL) {
716 		printf("%s: original_sc == NULL!\n", __func__);
717 		/* XXX KDM now what? */
718 		return;
719 	}
720 
721 	ctsio = &msg_info->hdr.original_sc->scsiio;
722 	ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
723 	ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
724 	ctsio->io_hdr.status = msg_info->hdr.status;
725 	ctsio->scsi_status = msg_info->scsi.scsi_status;
726 	ctsio->sense_len = msg_info->scsi.sense_len;
727 	ctsio->sense_residual = msg_info->scsi.sense_residual;
728 	ctsio->residual = msg_info->scsi.residual;
729 	memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
730 	       msg_info->scsi.sense_len);
731 	ctl_enqueue_isc((union ctl_io *)ctsio);
732 }
733 
734 static void
735 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
736 				union ctl_ha_msg *msg_info)
737 {
738 	struct ctl_scsiio *ctsio;
739 
740 	if (msg_info->hdr.serializing_sc == NULL) {
741 		printf("%s: serializing_sc == NULL!\n", __func__);
742 		/* XXX KDM now what? */
743 		return;
744 	}
745 
746 	ctsio = &msg_info->hdr.serializing_sc->scsiio;
747 	ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
748 	ctl_enqueue_isc((union ctl_io *)ctsio);
749 }
750 
751 void
752 ctl_isc_announce_lun(struct ctl_lun *lun)
753 {
754 	struct ctl_softc *softc = lun->ctl_softc;
755 	union ctl_ha_msg *msg;
756 	struct ctl_ha_msg_lun_pr_key pr_key;
757 	int i, k;
758 
759 	if (softc->ha_link != CTL_HA_LINK_ONLINE)
760 		return;
761 	mtx_lock(&lun->lun_lock);
762 	i = sizeof(msg->lun);
763 	if (lun->lun_devid)
764 		i += lun->lun_devid->len;
765 	i += sizeof(pr_key) * lun->pr_key_count;
766 alloc:
767 	mtx_unlock(&lun->lun_lock);
768 	msg = malloc(i, M_CTL, M_WAITOK);
769 	mtx_lock(&lun->lun_lock);
770 	k = sizeof(msg->lun);
771 	if (lun->lun_devid)
772 		k += lun->lun_devid->len;
773 	k += sizeof(pr_key) * lun->pr_key_count;
774 	if (i < k) {
775 		free(msg, M_CTL);
776 		i = k;
777 		goto alloc;
778 	}
779 	bzero(&msg->lun, sizeof(msg->lun));
780 	msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
781 	msg->hdr.nexus.targ_lun = lun->lun;
782 	msg->hdr.nexus.targ_mapped_lun = lun->lun;
783 	msg->lun.flags = lun->flags;
784 	msg->lun.pr_generation = lun->pr_generation;
785 	msg->lun.pr_res_idx = lun->pr_res_idx;
786 	msg->lun.pr_res_type = lun->pr_res_type;
787 	msg->lun.pr_key_count = lun->pr_key_count;
788 	i = 0;
789 	if (lun->lun_devid) {
790 		msg->lun.lun_devid_len = lun->lun_devid->len;
791 		memcpy(&msg->lun.data[i], lun->lun_devid->data,
792 		    msg->lun.lun_devid_len);
793 		i += msg->lun.lun_devid_len;
794 	}
795 	for (k = 0; k < CTL_MAX_INITIATORS; k++) {
796 		if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
797 			continue;
798 		pr_key.pr_iid = k;
799 		memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
800 		i += sizeof(pr_key);
801 	}
802 	mtx_unlock(&lun->lun_lock);
803 	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
804 	    M_WAITOK);
805 	free(msg, M_CTL);
806 
807 	if (lun->flags & CTL_LUN_PRIMARY_SC) {
808 		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
809 			ctl_isc_announce_mode(lun, -1,
810 			    lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
811 			    lun->mode_pages.index[i].subpage);
812 		}
813 	}
814 }
815 
816 void
817 ctl_isc_announce_port(struct ctl_port *port)
818 {
819 	struct ctl_softc *softc = port->ctl_softc;
820 	union ctl_ha_msg *msg;
821 	int i;
822 
823 	if (port->targ_port < softc->port_min ||
824 	    port->targ_port >= softc->port_max ||
825 	    softc->ha_link != CTL_HA_LINK_ONLINE)
826 		return;
827 	i = sizeof(msg->port) + strlen(port->port_name) + 1;
828 	if (port->lun_map)
829 		i += sizeof(uint32_t) * CTL_MAX_LUNS;
830 	if (port->port_devid)
831 		i += port->port_devid->len;
832 	if (port->target_devid)
833 		i += port->target_devid->len;
834 	if (port->init_devid)
835 		i += port->init_devid->len;
836 	msg = malloc(i, M_CTL, M_WAITOK);
837 	bzero(&msg->port, sizeof(msg->port));
838 	msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
839 	msg->hdr.nexus.targ_port = port->targ_port;
840 	msg->port.port_type = port->port_type;
841 	msg->port.physical_port = port->physical_port;
842 	msg->port.virtual_port = port->virtual_port;
843 	msg->port.status = port->status;
844 	i = 0;
845 	msg->port.name_len = sprintf(&msg->port.data[i],
846 	    "%d:%s", softc->ha_id, port->port_name) + 1;
847 	i += msg->port.name_len;
848 	if (port->lun_map) {
849 		msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
850 		memcpy(&msg->port.data[i], port->lun_map,
851 		    msg->port.lun_map_len);
852 		i += msg->port.lun_map_len;
853 	}
854 	if (port->port_devid) {
855 		msg->port.port_devid_len = port->port_devid->len;
856 		memcpy(&msg->port.data[i], port->port_devid->data,
857 		    msg->port.port_devid_len);
858 		i += msg->port.port_devid_len;
859 	}
860 	if (port->target_devid) {
861 		msg->port.target_devid_len = port->target_devid->len;
862 		memcpy(&msg->port.data[i], port->target_devid->data,
863 		    msg->port.target_devid_len);
864 		i += msg->port.target_devid_len;
865 	}
866 	if (port->init_devid) {
867 		msg->port.init_devid_len = port->init_devid->len;
868 		memcpy(&msg->port.data[i], port->init_devid->data,
869 		    msg->port.init_devid_len);
870 		i += msg->port.init_devid_len;
871 	}
872 	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
873 	    M_WAITOK);
874 	free(msg, M_CTL);
875 }
876 
877 void
878 ctl_isc_announce_iid(struct ctl_port *port, int iid)
879 {
880 	struct ctl_softc *softc = port->ctl_softc;
881 	union ctl_ha_msg *msg;
882 	int i, l;
883 
884 	if (port->targ_port < softc->port_min ||
885 	    port->targ_port >= softc->port_max ||
886 	    softc->ha_link != CTL_HA_LINK_ONLINE)
887 		return;
888 	mtx_lock(&softc->ctl_lock);
889 	i = sizeof(msg->iid);
890 	l = 0;
891 	if (port->wwpn_iid[iid].name)
892 		l = strlen(port->wwpn_iid[iid].name) + 1;
893 	i += l;
894 	msg = malloc(i, M_CTL, M_NOWAIT);
895 	if (msg == NULL) {
896 		mtx_unlock(&softc->ctl_lock);
897 		return;
898 	}
899 	bzero(&msg->iid, sizeof(msg->iid));
900 	msg->hdr.msg_type = CTL_MSG_IID_SYNC;
901 	msg->hdr.nexus.targ_port = port->targ_port;
902 	msg->hdr.nexus.initid = iid;
903 	msg->iid.in_use = port->wwpn_iid[iid].in_use;
904 	msg->iid.name_len = l;
905 	msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
906 	if (port->wwpn_iid[iid].name)
907 		strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
908 	mtx_unlock(&softc->ctl_lock);
909 	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
910 	free(msg, M_CTL);
911 }
912 
913 void
914 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
915     uint8_t page, uint8_t subpage)
916 {
917 	struct ctl_softc *softc = lun->ctl_softc;
918 	union ctl_ha_msg msg;
919 	u_int i;
920 
921 	if (softc->ha_link != CTL_HA_LINK_ONLINE)
922 		return;
923 	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
924 		if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
925 		    page && lun->mode_pages.index[i].subpage == subpage)
926 			break;
927 	}
928 	if (i == CTL_NUM_MODE_PAGES)
929 		return;
930 
931 	/* Don't try to replicate pages not present on this device. */
932 	if (lun->mode_pages.index[i].page_data == NULL)
933 		return;
934 
935 	bzero(&msg.mode, sizeof(msg.mode));
936 	msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
937 	msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
938 	msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
939 	msg.hdr.nexus.targ_lun = lun->lun;
940 	msg.hdr.nexus.targ_mapped_lun = lun->lun;
941 	msg.mode.page_code = page;
942 	msg.mode.subpage = subpage;
943 	msg.mode.page_len = lun->mode_pages.index[i].page_len;
944 	memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
945 	    msg.mode.page_len);
946 	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
947 	    M_WAITOK);
948 }
949 
950 static void
951 ctl_isc_ha_link_up(struct ctl_softc *softc)
952 {
953 	struct ctl_port *port;
954 	struct ctl_lun *lun;
955 	union ctl_ha_msg msg;
956 	int i;
957 
958 	/* Announce this node parameters to peer for validation. */
959 	msg.login.msg_type = CTL_MSG_LOGIN;
960 	msg.login.version = CTL_HA_VERSION;
961 	msg.login.ha_mode = softc->ha_mode;
962 	msg.login.ha_id = softc->ha_id;
963 	msg.login.max_luns = CTL_MAX_LUNS;
964 	msg.login.max_ports = CTL_MAX_PORTS;
965 	msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
966 	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
967 	    M_WAITOK);
968 
969 	STAILQ_FOREACH(port, &softc->port_list, links) {
970 		ctl_isc_announce_port(port);
971 		for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
972 			if (port->wwpn_iid[i].in_use)
973 				ctl_isc_announce_iid(port, i);
974 		}
975 	}
976 	STAILQ_FOREACH(lun, &softc->lun_list, links)
977 		ctl_isc_announce_lun(lun);
978 }
979 
980 static void
981 ctl_isc_ha_link_down(struct ctl_softc *softc)
982 {
983 	struct ctl_port *port;
984 	struct ctl_lun *lun;
985 	union ctl_io *io;
986 	int i;
987 
988 	mtx_lock(&softc->ctl_lock);
989 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
990 		mtx_lock(&lun->lun_lock);
991 		if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
992 			lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
993 			ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
994 		}
995 		mtx_unlock(&lun->lun_lock);
996 
997 		mtx_unlock(&softc->ctl_lock);
998 		io = ctl_alloc_io(softc->othersc_pool);
999 		mtx_lock(&softc->ctl_lock);
1000 		ctl_zero_io(io);
1001 		io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1002 		io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1003 		ctl_enqueue_isc(io);
1004 	}
1005 
1006 	STAILQ_FOREACH(port, &softc->port_list, links) {
1007 		if (port->targ_port >= softc->port_min &&
1008 		    port->targ_port < softc->port_max)
1009 			continue;
1010 		port->status &= ~CTL_PORT_STATUS_ONLINE;
1011 		for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1012 			port->wwpn_iid[i].in_use = 0;
1013 			free(port->wwpn_iid[i].name, M_CTL);
1014 			port->wwpn_iid[i].name = NULL;
1015 		}
1016 	}
1017 	mtx_unlock(&softc->ctl_lock);
1018 }
1019 
1020 static void
1021 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1022 {
1023 	struct ctl_lun *lun;
1024 	uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1025 
1026 	mtx_lock(&softc->ctl_lock);
1027 	if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
1028 	    (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
1029 		mtx_lock(&lun->lun_lock);
1030 		mtx_unlock(&softc->ctl_lock);
1031 		if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
1032 		    msg->ua.ua_set)
1033 			memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1034 		if (msg->ua.ua_all) {
1035 			if (msg->ua.ua_set)
1036 				ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1037 			else
1038 				ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1039 		} else {
1040 			if (msg->ua.ua_set)
1041 				ctl_est_ua(lun, iid, msg->ua.ua_type);
1042 			else
1043 				ctl_clr_ua(lun, iid, msg->ua.ua_type);
1044 		}
1045 		mtx_unlock(&lun->lun_lock);
1046 	} else
1047 		mtx_unlock(&softc->ctl_lock);
1048 }
1049 
1050 static void
1051 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1052 {
1053 	struct ctl_lun *lun;
1054 	struct ctl_ha_msg_lun_pr_key pr_key;
1055 	int i, k;
1056 	ctl_lun_flags oflags;
1057 	uint32_t targ_lun;
1058 
1059 	targ_lun = msg->hdr.nexus.targ_mapped_lun;
1060 	mtx_lock(&softc->ctl_lock);
1061 	if ((targ_lun >= CTL_MAX_LUNS) ||
1062 	    ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1063 		mtx_unlock(&softc->ctl_lock);
1064 		return;
1065 	}
1066 	mtx_lock(&lun->lun_lock);
1067 	mtx_unlock(&softc->ctl_lock);
1068 	if (lun->flags & CTL_LUN_DISABLED) {
1069 		mtx_unlock(&lun->lun_lock);
1070 		return;
1071 	}
1072 	i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1073 	if (msg->lun.lun_devid_len != i || (i > 0 &&
1074 	    memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1075 		mtx_unlock(&lun->lun_lock);
1076 		printf("%s: Received conflicting HA LUN %d\n",
1077 		    __func__, msg->hdr.nexus.targ_lun);
1078 		return;
1079 	} else {
1080 		/* Record whether peer is primary. */
1081 		oflags = lun->flags;
1082 		if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1083 		    (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1084 			lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1085 		else
1086 			lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1087 		if (oflags != lun->flags)
1088 			ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1089 
1090 		/* If peer is primary and we are not -- use data */
1091 		if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1092 		    (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1093 			lun->pr_generation = msg->lun.pr_generation;
1094 			lun->pr_res_idx = msg->lun.pr_res_idx;
1095 			lun->pr_res_type = msg->lun.pr_res_type;
1096 			lun->pr_key_count = msg->lun.pr_key_count;
1097 			for (k = 0; k < CTL_MAX_INITIATORS; k++)
1098 				ctl_clr_prkey(lun, k);
1099 			for (k = 0; k < msg->lun.pr_key_count; k++) {
1100 				memcpy(&pr_key, &msg->lun.data[i],
1101 				    sizeof(pr_key));
1102 				ctl_alloc_prkey(lun, pr_key.pr_iid);
1103 				ctl_set_prkey(lun, pr_key.pr_iid,
1104 				    pr_key.pr_key);
1105 				i += sizeof(pr_key);
1106 			}
1107 		}
1108 
1109 		mtx_unlock(&lun->lun_lock);
1110 		CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1111 		    __func__, msg->hdr.nexus.targ_lun,
1112 		    (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1113 		    "primary" : "secondary"));
1114 
1115 		/* If we are primary but peer doesn't know -- notify */
1116 		if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1117 		    (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1118 			ctl_isc_announce_lun(lun);
1119 	}
1120 }
1121 
1122 static void
1123 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1124 {
1125 	struct ctl_port *port;
1126 	struct ctl_lun *lun;
1127 	int i, new;
1128 
1129 	port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1130 	if (port == NULL) {
1131 		CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1132 		    msg->hdr.nexus.targ_port));
1133 		new = 1;
1134 		port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1135 		port->frontend = &ha_frontend;
1136 		port->targ_port = msg->hdr.nexus.targ_port;
1137 		port->fe_datamove = ctl_ha_datamove;
1138 		port->fe_done = ctl_ha_done;
1139 	} else if (port->frontend == &ha_frontend) {
1140 		CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1141 		    msg->hdr.nexus.targ_port));
1142 		new = 0;
1143 	} else {
1144 		printf("%s: Received conflicting HA port %d\n",
1145 		    __func__, msg->hdr.nexus.targ_port);
1146 		return;
1147 	}
1148 	port->port_type = msg->port.port_type;
1149 	port->physical_port = msg->port.physical_port;
1150 	port->virtual_port = msg->port.virtual_port;
1151 	port->status = msg->port.status;
1152 	i = 0;
1153 	free(port->port_name, M_CTL);
1154 	port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1155 	    M_CTL);
1156 	i += msg->port.name_len;
1157 	if (msg->port.lun_map_len != 0) {
1158 		if (port->lun_map == NULL)
1159 			port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
1160 			    M_CTL, M_WAITOK);
1161 		memcpy(port->lun_map, &msg->port.data[i],
1162 		    sizeof(uint32_t) * CTL_MAX_LUNS);
1163 		i += msg->port.lun_map_len;
1164 	} else {
1165 		free(port->lun_map, M_CTL);
1166 		port->lun_map = NULL;
1167 	}
1168 	if (msg->port.port_devid_len != 0) {
1169 		if (port->port_devid == NULL ||
1170 		    port->port_devid->len != msg->port.port_devid_len) {
1171 			free(port->port_devid, M_CTL);
1172 			port->port_devid = malloc(sizeof(struct ctl_devid) +
1173 			    msg->port.port_devid_len, M_CTL, M_WAITOK);
1174 		}
1175 		memcpy(port->port_devid->data, &msg->port.data[i],
1176 		    msg->port.port_devid_len);
1177 		port->port_devid->len = msg->port.port_devid_len;
1178 		i += msg->port.port_devid_len;
1179 	} else {
1180 		free(port->port_devid, M_CTL);
1181 		port->port_devid = NULL;
1182 	}
1183 	if (msg->port.target_devid_len != 0) {
1184 		if (port->target_devid == NULL ||
1185 		    port->target_devid->len != msg->port.target_devid_len) {
1186 			free(port->target_devid, M_CTL);
1187 			port->target_devid = malloc(sizeof(struct ctl_devid) +
1188 			    msg->port.target_devid_len, M_CTL, M_WAITOK);
1189 		}
1190 		memcpy(port->target_devid->data, &msg->port.data[i],
1191 		    msg->port.target_devid_len);
1192 		port->target_devid->len = msg->port.target_devid_len;
1193 		i += msg->port.target_devid_len;
1194 	} else {
1195 		free(port->target_devid, M_CTL);
1196 		port->target_devid = NULL;
1197 	}
1198 	if (msg->port.init_devid_len != 0) {
1199 		if (port->init_devid == NULL ||
1200 		    port->init_devid->len != msg->port.init_devid_len) {
1201 			free(port->init_devid, M_CTL);
1202 			port->init_devid = malloc(sizeof(struct ctl_devid) +
1203 			    msg->port.init_devid_len, M_CTL, M_WAITOK);
1204 		}
1205 		memcpy(port->init_devid->data, &msg->port.data[i],
1206 		    msg->port.init_devid_len);
1207 		port->init_devid->len = msg->port.init_devid_len;
1208 		i += msg->port.init_devid_len;
1209 	} else {
1210 		free(port->init_devid, M_CTL);
1211 		port->init_devid = NULL;
1212 	}
1213 	if (new) {
1214 		if (ctl_port_register(port) != 0) {
1215 			printf("%s: ctl_port_register() failed with error\n",
1216 			    __func__);
1217 		}
1218 	}
1219 	mtx_lock(&softc->ctl_lock);
1220 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
1221 		if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
1222 			continue;
1223 		mtx_lock(&lun->lun_lock);
1224 		ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1225 		mtx_unlock(&lun->lun_lock);
1226 	}
1227 	mtx_unlock(&softc->ctl_lock);
1228 }
1229 
1230 static void
1231 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1232 {
1233 	struct ctl_port *port;
1234 	int iid;
1235 
1236 	port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1237 	if (port == NULL) {
1238 		printf("%s: Received IID for unknown port %d\n",
1239 		    __func__, msg->hdr.nexus.targ_port);
1240 		return;
1241 	}
1242 	iid = msg->hdr.nexus.initid;
1243 	port->wwpn_iid[iid].in_use = msg->iid.in_use;
1244 	port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1245 	free(port->wwpn_iid[iid].name, M_CTL);
1246 	if (msg->iid.name_len) {
1247 		port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1248 		    msg->iid.name_len, M_CTL);
1249 	} else
1250 		port->wwpn_iid[iid].name = NULL;
1251 }
1252 
1253 static void
1254 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1255 {
1256 
1257 	if (msg->login.version != CTL_HA_VERSION) {
1258 		printf("CTL HA peers have different versions %d != %d\n",
1259 		    msg->login.version, CTL_HA_VERSION);
1260 		ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1261 		return;
1262 	}
1263 	if (msg->login.ha_mode != softc->ha_mode) {
1264 		printf("CTL HA peers have different ha_mode %d != %d\n",
1265 		    msg->login.ha_mode, softc->ha_mode);
1266 		ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1267 		return;
1268 	}
1269 	if (msg->login.ha_id == softc->ha_id) {
1270 		printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1271 		ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1272 		return;
1273 	}
1274 	if (msg->login.max_luns != CTL_MAX_LUNS ||
1275 	    msg->login.max_ports != CTL_MAX_PORTS ||
1276 	    msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1277 		printf("CTL HA peers have different limits\n");
1278 		ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1279 		return;
1280 	}
1281 }
1282 
1283 static void
1284 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1285 {
1286 	struct ctl_lun *lun;
1287 	u_int i;
1288 	uint32_t initidx, targ_lun;
1289 
1290 	targ_lun = msg->hdr.nexus.targ_mapped_lun;
1291 	mtx_lock(&softc->ctl_lock);
1292 	if ((targ_lun >= CTL_MAX_LUNS) ||
1293 	    ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1294 		mtx_unlock(&softc->ctl_lock);
1295 		return;
1296 	}
1297 	mtx_lock(&lun->lun_lock);
1298 	mtx_unlock(&softc->ctl_lock);
1299 	if (lun->flags & CTL_LUN_DISABLED) {
1300 		mtx_unlock(&lun->lun_lock);
1301 		return;
1302 	}
1303 	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1304 		if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1305 		    msg->mode.page_code &&
1306 		    lun->mode_pages.index[i].subpage == msg->mode.subpage)
1307 			break;
1308 	}
1309 	if (i == CTL_NUM_MODE_PAGES) {
1310 		mtx_unlock(&lun->lun_lock);
1311 		return;
1312 	}
1313 	memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1314 	    lun->mode_pages.index[i].page_len);
1315 	initidx = ctl_get_initindex(&msg->hdr.nexus);
1316 	if (initidx != -1)
1317 		ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1318 	mtx_unlock(&lun->lun_lock);
1319 }
1320 
1321 /*
1322  * ISC (Inter Shelf Communication) event handler.  Events from the HA
1323  * subsystem come in here.
1324  */
1325 static void
1326 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1327 {
1328 	struct ctl_softc *softc = control_softc;
1329 	union ctl_io *io;
1330 	struct ctl_prio *presio;
1331 	ctl_ha_status isc_status;
1332 
1333 	CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1334 	if (event == CTL_HA_EVT_MSG_RECV) {
1335 		union ctl_ha_msg *msg, msgbuf;
1336 
1337 		if (param > sizeof(msgbuf))
1338 			msg = malloc(param, M_CTL, M_WAITOK);
1339 		else
1340 			msg = &msgbuf;
1341 		isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1342 		    M_WAITOK);
1343 		if (isc_status != CTL_HA_STATUS_SUCCESS) {
1344 			printf("%s: Error receiving message: %d\n",
1345 			    __func__, isc_status);
1346 			if (msg != &msgbuf)
1347 				free(msg, M_CTL);
1348 			return;
1349 		}
1350 
1351 		CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1352 		switch (msg->hdr.msg_type) {
1353 		case CTL_MSG_SERIALIZE:
1354 			io = ctl_alloc_io(softc->othersc_pool);
1355 			ctl_zero_io(io);
1356 			// populate ctsio from msg
1357 			io->io_hdr.io_type = CTL_IO_SCSI;
1358 			io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1359 			io->io_hdr.original_sc = msg->hdr.original_sc;
1360 			io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1361 					    CTL_FLAG_IO_ACTIVE;
1362 			/*
1363 			 * If we're in serialization-only mode, we don't
1364 			 * want to go through full done processing.  Thus
1365 			 * the COPY flag.
1366 			 *
1367 			 * XXX KDM add another flag that is more specific.
1368 			 */
1369 			if (softc->ha_mode != CTL_HA_MODE_XFER)
1370 				io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1371 			io->io_hdr.nexus = msg->hdr.nexus;
1372 #if 0
1373 			printf("port %u, iid %u, lun %u\n",
1374 			       io->io_hdr.nexus.targ_port,
1375 			       io->io_hdr.nexus.initid,
1376 			       io->io_hdr.nexus.targ_lun);
1377 #endif
1378 			io->scsiio.tag_num = msg->scsi.tag_num;
1379 			io->scsiio.tag_type = msg->scsi.tag_type;
1380 #ifdef CTL_TIME_IO
1381 			io->io_hdr.start_time = time_uptime;
1382 			getbinuptime(&io->io_hdr.start_bt);
1383 #endif /* CTL_TIME_IO */
1384 			io->scsiio.cdb_len = msg->scsi.cdb_len;
1385 			memcpy(io->scsiio.cdb, msg->scsi.cdb,
1386 			       CTL_MAX_CDBLEN);
1387 			if (softc->ha_mode == CTL_HA_MODE_XFER) {
1388 				const struct ctl_cmd_entry *entry;
1389 
1390 				entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1391 				io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1392 				io->io_hdr.flags |=
1393 					entry->flags & CTL_FLAG_DATA_MASK;
1394 			}
1395 			ctl_enqueue_isc(io);
1396 			break;
1397 
1398 		/* Performed on the Originating SC, XFER mode only */
1399 		case CTL_MSG_DATAMOVE: {
1400 			struct ctl_sg_entry *sgl;
1401 			int i, j;
1402 
1403 			io = msg->hdr.original_sc;
1404 			if (io == NULL) {
1405 				printf("%s: original_sc == NULL!\n", __func__);
1406 				/* XXX KDM do something here */
1407 				break;
1408 			}
1409 			io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1410 			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1411 			/*
1412 			 * Keep track of this, we need to send it back over
1413 			 * when the datamove is complete.
1414 			 */
1415 			io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1416 			if (msg->hdr.status == CTL_SUCCESS)
1417 				io->io_hdr.status = msg->hdr.status;
1418 
1419 			if (msg->dt.sg_sequence == 0) {
1420 #ifdef CTL_TIME_IO
1421 				getbinuptime(&io->io_hdr.dma_start_bt);
1422 #endif
1423 				i = msg->dt.kern_sg_entries +
1424 				    msg->dt.kern_data_len /
1425 				    CTL_HA_DATAMOVE_SEGMENT + 1;
1426 				sgl = malloc(sizeof(*sgl) * i, M_CTL,
1427 				    M_WAITOK | M_ZERO);
1428 				io->io_hdr.remote_sglist = sgl;
1429 				io->io_hdr.local_sglist =
1430 				    &sgl[msg->dt.kern_sg_entries];
1431 
1432 				io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1433 
1434 				io->scsiio.kern_sg_entries =
1435 					msg->dt.kern_sg_entries;
1436 				io->scsiio.rem_sg_entries =
1437 					msg->dt.kern_sg_entries;
1438 				io->scsiio.kern_data_len =
1439 					msg->dt.kern_data_len;
1440 				io->scsiio.kern_total_len =
1441 					msg->dt.kern_total_len;
1442 				io->scsiio.kern_data_resid =
1443 					msg->dt.kern_data_resid;
1444 				io->scsiio.kern_rel_offset =
1445 					msg->dt.kern_rel_offset;
1446 				io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1447 				io->io_hdr.flags |= msg->dt.flags &
1448 				    CTL_FLAG_BUS_ADDR;
1449 			} else
1450 				sgl = (struct ctl_sg_entry *)
1451 					io->scsiio.kern_data_ptr;
1452 
1453 			for (i = msg->dt.sent_sg_entries, j = 0;
1454 			     i < (msg->dt.sent_sg_entries +
1455 			     msg->dt.cur_sg_entries); i++, j++) {
1456 				sgl[i].addr = msg->dt.sg_list[j].addr;
1457 				sgl[i].len = msg->dt.sg_list[j].len;
1458 
1459 #if 0
1460 				printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1461 				    __func__, sgl[i].addr, sgl[i].len, j, i);
1462 #endif
1463 			}
1464 
1465 			/*
1466 			 * If this is the last piece of the I/O, we've got
1467 			 * the full S/G list.  Queue processing in the thread.
1468 			 * Otherwise wait for the next piece.
1469 			 */
1470 			if (msg->dt.sg_last != 0)
1471 				ctl_enqueue_isc(io);
1472 			break;
1473 		}
1474 		/* Performed on the Serializing (primary) SC, XFER mode only */
1475 		case CTL_MSG_DATAMOVE_DONE: {
1476 			if (msg->hdr.serializing_sc == NULL) {
1477 				printf("%s: serializing_sc == NULL!\n",
1478 				       __func__);
1479 				/* XXX KDM now what? */
1480 				break;
1481 			}
1482 			/*
1483 			 * We grab the sense information here in case
1484 			 * there was a failure, so we can return status
1485 			 * back to the initiator.
1486 			 */
1487 			io = msg->hdr.serializing_sc;
1488 			io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1489 			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1490 			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1491 			io->io_hdr.port_status = msg->scsi.fetd_status;
1492 			io->scsiio.residual = msg->scsi.residual;
1493 			if (msg->hdr.status != CTL_STATUS_NONE) {
1494 				io->io_hdr.status = msg->hdr.status;
1495 				io->scsiio.scsi_status = msg->scsi.scsi_status;
1496 				io->scsiio.sense_len = msg->scsi.sense_len;
1497 				io->scsiio.sense_residual =msg->scsi.sense_residual;
1498 				memcpy(&io->scsiio.sense_data,
1499 				    &msg->scsi.sense_data,
1500 				    msg->scsi.sense_len);
1501 				if (msg->hdr.status == CTL_SUCCESS)
1502 					io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1503 			}
1504 			ctl_enqueue_isc(io);
1505 			break;
1506 		}
1507 
1508 		/* Preformed on Originating SC, SER_ONLY mode */
1509 		case CTL_MSG_R2R:
1510 			io = msg->hdr.original_sc;
1511 			if (io == NULL) {
1512 				printf("%s: original_sc == NULL!\n",
1513 				    __func__);
1514 				break;
1515 			}
1516 			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1517 			io->io_hdr.msg_type = CTL_MSG_R2R;
1518 			io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1519 			ctl_enqueue_isc(io);
1520 			break;
1521 
1522 		/*
1523 		 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1524 		 * mode.
1525 		 * Performed on the Originating (i.e. secondary) SC in XFER
1526 		 * mode
1527 		 */
1528 		case CTL_MSG_FINISH_IO:
1529 			if (softc->ha_mode == CTL_HA_MODE_XFER)
1530 				ctl_isc_handler_finish_xfer(softc, msg);
1531 			else
1532 				ctl_isc_handler_finish_ser_only(softc, msg);
1533 			break;
1534 
1535 		/* Preformed on Originating SC */
1536 		case CTL_MSG_BAD_JUJU:
1537 			io = msg->hdr.original_sc;
1538 			if (io == NULL) {
1539 				printf("%s: Bad JUJU!, original_sc is NULL!\n",
1540 				       __func__);
1541 				break;
1542 			}
1543 			ctl_copy_sense_data(msg, io);
1544 			/*
1545 			 * IO should have already been cleaned up on other
1546 			 * SC so clear this flag so we won't send a message
1547 			 * back to finish the IO there.
1548 			 */
1549 			io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1550 			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1551 
1552 			/* io = msg->hdr.serializing_sc; */
1553 			io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1554 			ctl_enqueue_isc(io);
1555 			break;
1556 
1557 		/* Handle resets sent from the other side */
1558 		case CTL_MSG_MANAGE_TASKS: {
1559 			struct ctl_taskio *taskio;
1560 			taskio = (struct ctl_taskio *)ctl_alloc_io(
1561 			    softc->othersc_pool);
1562 			ctl_zero_io((union ctl_io *)taskio);
1563 			taskio->io_hdr.io_type = CTL_IO_TASK;
1564 			taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1565 			taskio->io_hdr.nexus = msg->hdr.nexus;
1566 			taskio->task_action = msg->task.task_action;
1567 			taskio->tag_num = msg->task.tag_num;
1568 			taskio->tag_type = msg->task.tag_type;
1569 #ifdef CTL_TIME_IO
1570 			taskio->io_hdr.start_time = time_uptime;
1571 			getbinuptime(&taskio->io_hdr.start_bt);
1572 #endif /* CTL_TIME_IO */
1573 			ctl_run_task((union ctl_io *)taskio);
1574 			break;
1575 		}
1576 		/* Persistent Reserve action which needs attention */
1577 		case CTL_MSG_PERS_ACTION:
1578 			presio = (struct ctl_prio *)ctl_alloc_io(
1579 			    softc->othersc_pool);
1580 			ctl_zero_io((union ctl_io *)presio);
1581 			presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1582 			presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1583 			presio->io_hdr.nexus = msg->hdr.nexus;
1584 			presio->pr_msg = msg->pr;
1585 			ctl_enqueue_isc((union ctl_io *)presio);
1586 			break;
1587 		case CTL_MSG_UA:
1588 			ctl_isc_ua(softc, msg, param);
1589 			break;
1590 		case CTL_MSG_PORT_SYNC:
1591 			ctl_isc_port_sync(softc, msg, param);
1592 			break;
1593 		case CTL_MSG_LUN_SYNC:
1594 			ctl_isc_lun_sync(softc, msg, param);
1595 			break;
1596 		case CTL_MSG_IID_SYNC:
1597 			ctl_isc_iid_sync(softc, msg, param);
1598 			break;
1599 		case CTL_MSG_LOGIN:
1600 			ctl_isc_login(softc, msg, param);
1601 			break;
1602 		case CTL_MSG_MODE_SYNC:
1603 			ctl_isc_mode_sync(softc, msg, param);
1604 			break;
1605 		default:
1606 			printf("Received HA message of unknown type %d\n",
1607 			    msg->hdr.msg_type);
1608 			ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1609 			break;
1610 		}
1611 		if (msg != &msgbuf)
1612 			free(msg, M_CTL);
1613 	} else if (event == CTL_HA_EVT_LINK_CHANGE) {
1614 		printf("CTL: HA link status changed from %d to %d\n",
1615 		    softc->ha_link, param);
1616 		if (param == softc->ha_link)
1617 			return;
1618 		if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1619 			softc->ha_link = param;
1620 			ctl_isc_ha_link_down(softc);
1621 		} else {
1622 			softc->ha_link = param;
1623 			if (softc->ha_link == CTL_HA_LINK_ONLINE)
1624 				ctl_isc_ha_link_up(softc);
1625 		}
1626 		return;
1627 	} else {
1628 		printf("ctl_isc_event_handler: Unknown event %d\n", event);
1629 		return;
1630 	}
1631 }
1632 
1633 static void
1634 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1635 {
1636 
1637 	memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1638 	    src->scsi.sense_len);
1639 	dest->scsiio.scsi_status = src->scsi.scsi_status;
1640 	dest->scsiio.sense_len = src->scsi.sense_len;
1641 	dest->io_hdr.status = src->hdr.status;
1642 }
1643 
1644 static void
1645 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1646 {
1647 
1648 	memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1649 	    src->scsiio.sense_len);
1650 	dest->scsi.scsi_status = src->scsiio.scsi_status;
1651 	dest->scsi.sense_len = src->scsiio.sense_len;
1652 	dest->hdr.status = src->io_hdr.status;
1653 }
1654 
1655 void
1656 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1657 {
1658 	struct ctl_softc *softc = lun->ctl_softc;
1659 	ctl_ua_type *pu;
1660 
1661 	if (initidx < softc->init_min || initidx >= softc->init_max)
1662 		return;
1663 	mtx_assert(&lun->lun_lock, MA_OWNED);
1664 	pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1665 	if (pu == NULL)
1666 		return;
1667 	pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1668 }
1669 
1670 void
1671 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1672 {
1673 	int i;
1674 
1675 	mtx_assert(&lun->lun_lock, MA_OWNED);
1676 	if (lun->pending_ua[port] == NULL)
1677 		return;
1678 	for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1679 		if (port * CTL_MAX_INIT_PER_PORT + i == except)
1680 			continue;
1681 		lun->pending_ua[port][i] |= ua;
1682 	}
1683 }
1684 
1685 void
1686 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1687 {
1688 	struct ctl_softc *softc = lun->ctl_softc;
1689 	int i;
1690 
1691 	mtx_assert(&lun->lun_lock, MA_OWNED);
1692 	for (i = softc->port_min; i < softc->port_max; i++)
1693 		ctl_est_ua_port(lun, i, except, ua);
1694 }
1695 
1696 void
1697 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1698 {
1699 	struct ctl_softc *softc = lun->ctl_softc;
1700 	ctl_ua_type *pu;
1701 
1702 	if (initidx < softc->init_min || initidx >= softc->init_max)
1703 		return;
1704 	mtx_assert(&lun->lun_lock, MA_OWNED);
1705 	pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1706 	if (pu == NULL)
1707 		return;
1708 	pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1709 }
1710 
1711 void
1712 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1713 {
1714 	struct ctl_softc *softc = lun->ctl_softc;
1715 	int i, j;
1716 
1717 	mtx_assert(&lun->lun_lock, MA_OWNED);
1718 	for (i = softc->port_min; i < softc->port_max; i++) {
1719 		if (lun->pending_ua[i] == NULL)
1720 			continue;
1721 		for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1722 			if (i * CTL_MAX_INIT_PER_PORT + j == except)
1723 				continue;
1724 			lun->pending_ua[i][j] &= ~ua;
1725 		}
1726 	}
1727 }
1728 
1729 void
1730 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1731     ctl_ua_type ua_type)
1732 {
1733 	struct ctl_lun *lun;
1734 
1735 	mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1736 	STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1737 		mtx_lock(&lun->lun_lock);
1738 		ctl_clr_ua(lun, initidx, ua_type);
1739 		mtx_unlock(&lun->lun_lock);
1740 	}
1741 }
1742 
1743 static int
1744 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1745 {
1746 	struct ctl_softc *softc = (struct ctl_softc *)arg1;
1747 	struct ctl_lun *lun;
1748 	struct ctl_lun_req ireq;
1749 	int error, value;
1750 
1751 	value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1752 	error = sysctl_handle_int(oidp, &value, 0, req);
1753 	if ((error != 0) || (req->newptr == NULL))
1754 		return (error);
1755 
1756 	mtx_lock(&softc->ctl_lock);
1757 	if (value == 0)
1758 		softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1759 	else
1760 		softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1761 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
1762 		mtx_unlock(&softc->ctl_lock);
1763 		bzero(&ireq, sizeof(ireq));
1764 		ireq.reqtype = CTL_LUNREQ_MODIFY;
1765 		ireq.reqdata.modify.lun_id = lun->lun;
1766 		lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1767 		    curthread);
1768 		if (ireq.status != CTL_LUN_OK) {
1769 			printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1770 			    __func__, ireq.status, ireq.error_str);
1771 		}
1772 		mtx_lock(&softc->ctl_lock);
1773 	}
1774 	mtx_unlock(&softc->ctl_lock);
1775 	return (0);
1776 }
1777 
1778 static int
1779 ctl_init(void)
1780 {
1781 	struct make_dev_args args;
1782 	struct ctl_softc *softc;
1783 	void *other_pool;
1784 	int i, error;
1785 
1786 	softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1787 			       M_WAITOK | M_ZERO);
1788 
1789 	make_dev_args_init(&args);
1790 	args.mda_devsw = &ctl_cdevsw;
1791 	args.mda_uid = UID_ROOT;
1792 	args.mda_gid = GID_OPERATOR;
1793 	args.mda_mode = 0600;
1794 	args.mda_si_drv1 = softc;
1795 	error = make_dev_s(&args, &softc->dev, "cam/ctl");
1796 	if (error != 0) {
1797 		free(control_softc, M_DEVBUF);
1798 		return (error);
1799 	}
1800 
1801 	sysctl_ctx_init(&softc->sysctl_ctx);
1802 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1803 		SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1804 		CTLFLAG_RD, 0, "CAM Target Layer");
1805 
1806 	if (softc->sysctl_tree == NULL) {
1807 		printf("%s: unable to allocate sysctl tree\n", __func__);
1808 		destroy_dev(softc->dev);
1809 		free(control_softc, M_DEVBUF);
1810 		control_softc = NULL;
1811 		return (ENOMEM);
1812 	}
1813 
1814 	mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1815 	softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1816 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1817 	softc->flags = 0;
1818 
1819 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1820 	    OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1821 	    "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1822 
1823 	/*
1824 	 * In Copan's HA scheme, the "master" and "slave" roles are
1825 	 * figured out through the slot the controller is in.  Although it
1826 	 * is an active/active system, someone has to be in charge.
1827 	 */
1828 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1829 	    OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1830 	    "HA head ID (0 - no HA)");
1831 	if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1832 		softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1833 		softc->is_single = 1;
1834 		softc->port_cnt = CTL_MAX_PORTS;
1835 		softc->port_min = 0;
1836 	} else {
1837 		softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1838 		softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1839 	}
1840 	softc->port_max = softc->port_min + softc->port_cnt;
1841 	softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1842 	softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1843 
1844 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1845 	    OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1846 	    "HA link state (0 - offline, 1 - unknown, 2 - online)");
1847 
1848 	STAILQ_INIT(&softc->lun_list);
1849 	STAILQ_INIT(&softc->pending_lun_queue);
1850 	STAILQ_INIT(&softc->fe_list);
1851 	STAILQ_INIT(&softc->port_list);
1852 	STAILQ_INIT(&softc->be_list);
1853 	ctl_tpc_init(softc);
1854 
1855 	if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1856 	                    &other_pool) != 0)
1857 	{
1858 		printf("ctl: can't allocate %d entry other SC pool, "
1859 		       "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1860 		return (ENOMEM);
1861 	}
1862 	softc->othersc_pool = other_pool;
1863 
1864 	if (worker_threads <= 0)
1865 		worker_threads = max(1, mp_ncpus / 4);
1866 	if (worker_threads > CTL_MAX_THREADS)
1867 		worker_threads = CTL_MAX_THREADS;
1868 
1869 	for (i = 0; i < worker_threads; i++) {
1870 		struct ctl_thread *thr = &softc->threads[i];
1871 
1872 		mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1873 		thr->ctl_softc = softc;
1874 		STAILQ_INIT(&thr->incoming_queue);
1875 		STAILQ_INIT(&thr->rtr_queue);
1876 		STAILQ_INIT(&thr->done_queue);
1877 		STAILQ_INIT(&thr->isc_queue);
1878 
1879 		error = kproc_kthread_add(ctl_work_thread, thr,
1880 		    &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1881 		if (error != 0) {
1882 			printf("error creating CTL work thread!\n");
1883 			ctl_pool_free(other_pool);
1884 			return (error);
1885 		}
1886 	}
1887 	error = kproc_kthread_add(ctl_lun_thread, softc,
1888 	    &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1889 	if (error != 0) {
1890 		printf("error creating CTL lun thread!\n");
1891 		ctl_pool_free(other_pool);
1892 		return (error);
1893 	}
1894 	error = kproc_kthread_add(ctl_thresh_thread, softc,
1895 	    &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1896 	if (error != 0) {
1897 		printf("error creating CTL threshold thread!\n");
1898 		ctl_pool_free(other_pool);
1899 		return (error);
1900 	}
1901 
1902 	SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1903 	    OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1904 	    softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1905 
1906 	if (softc->is_single == 0) {
1907 		ctl_frontend_register(&ha_frontend);
1908 		if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1909 			printf("ctl_init: ctl_ha_msg_init failed.\n");
1910 			softc->is_single = 1;
1911 		} else
1912 		if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1913 		    != CTL_HA_STATUS_SUCCESS) {
1914 			printf("ctl_init: ctl_ha_msg_register failed.\n");
1915 			softc->is_single = 1;
1916 		}
1917 	}
1918 	return (0);
1919 }
1920 
1921 void
1922 ctl_shutdown(void)
1923 {
1924 	struct ctl_softc *softc = control_softc;
1925 	struct ctl_lun *lun, *next_lun;
1926 
1927 	if (softc->is_single == 0) {
1928 		ctl_ha_msg_shutdown(softc);
1929 		if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1930 		    != CTL_HA_STATUS_SUCCESS)
1931 			printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1932 		if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1933 			printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1934 		ctl_frontend_deregister(&ha_frontend);
1935 	}
1936 
1937 	mtx_lock(&softc->ctl_lock);
1938 
1939 	STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1940 		ctl_free_lun(lun);
1941 
1942 	mtx_unlock(&softc->ctl_lock);
1943 
1944 #if 0
1945 	ctl_shutdown_thread(softc->work_thread);
1946 	mtx_destroy(&softc->queue_lock);
1947 #endif
1948 
1949 	ctl_tpc_shutdown(softc);
1950 	uma_zdestroy(softc->io_zone);
1951 	mtx_destroy(&softc->ctl_lock);
1952 
1953 	destroy_dev(softc->dev);
1954 
1955 	sysctl_ctx_free(&softc->sysctl_ctx);
1956 
1957 	free(control_softc, M_DEVBUF);
1958 	control_softc = NULL;
1959 }
1960 
1961 static int
1962 ctl_module_event_handler(module_t mod, int what, void *arg)
1963 {
1964 
1965 	switch (what) {
1966 	case MOD_LOAD:
1967 		return (ctl_init());
1968 	case MOD_UNLOAD:
1969 		return (EBUSY);
1970 	default:
1971 		return (EOPNOTSUPP);
1972 	}
1973 }
1974 
1975 /*
1976  * XXX KDM should we do some access checks here?  Bump a reference count to
1977  * prevent a CTL module from being unloaded while someone has it open?
1978  */
1979 static int
1980 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1981 {
1982 	return (0);
1983 }
1984 
1985 static int
1986 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1987 {
1988 	return (0);
1989 }
1990 
1991 /*
1992  * Remove an initiator by port number and initiator ID.
1993  * Returns 0 for success, -1 for failure.
1994  */
1995 int
1996 ctl_remove_initiator(struct ctl_port *port, int iid)
1997 {
1998 	struct ctl_softc *softc = port->ctl_softc;
1999 
2000 	mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2001 
2002 	if (iid > CTL_MAX_INIT_PER_PORT) {
2003 		printf("%s: initiator ID %u > maximun %u!\n",
2004 		       __func__, iid, CTL_MAX_INIT_PER_PORT);
2005 		return (-1);
2006 	}
2007 
2008 	mtx_lock(&softc->ctl_lock);
2009 	port->wwpn_iid[iid].in_use--;
2010 	port->wwpn_iid[iid].last_use = time_uptime;
2011 	mtx_unlock(&softc->ctl_lock);
2012 	ctl_isc_announce_iid(port, iid);
2013 
2014 	return (0);
2015 }
2016 
2017 /*
2018  * Add an initiator to the initiator map.
2019  * Returns iid for success, < 0 for failure.
2020  */
2021 int
2022 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2023 {
2024 	struct ctl_softc *softc = port->ctl_softc;
2025 	time_t best_time;
2026 	int i, best;
2027 
2028 	mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2029 
2030 	if (iid >= CTL_MAX_INIT_PER_PORT) {
2031 		printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2032 		       __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2033 		free(name, M_CTL);
2034 		return (-1);
2035 	}
2036 
2037 	mtx_lock(&softc->ctl_lock);
2038 
2039 	if (iid < 0 && (wwpn != 0 || name != NULL)) {
2040 		for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2041 			if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2042 				iid = i;
2043 				break;
2044 			}
2045 			if (name != NULL && port->wwpn_iid[i].name != NULL &&
2046 			    strcmp(name, port->wwpn_iid[i].name) == 0) {
2047 				iid = i;
2048 				break;
2049 			}
2050 		}
2051 	}
2052 
2053 	if (iid < 0) {
2054 		for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2055 			if (port->wwpn_iid[i].in_use == 0 &&
2056 			    port->wwpn_iid[i].wwpn == 0 &&
2057 			    port->wwpn_iid[i].name == NULL) {
2058 				iid = i;
2059 				break;
2060 			}
2061 		}
2062 	}
2063 
2064 	if (iid < 0) {
2065 		best = -1;
2066 		best_time = INT32_MAX;
2067 		for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2068 			if (port->wwpn_iid[i].in_use == 0) {
2069 				if (port->wwpn_iid[i].last_use < best_time) {
2070 					best = i;
2071 					best_time = port->wwpn_iid[i].last_use;
2072 				}
2073 			}
2074 		}
2075 		iid = best;
2076 	}
2077 
2078 	if (iid < 0) {
2079 		mtx_unlock(&softc->ctl_lock);
2080 		free(name, M_CTL);
2081 		return (-2);
2082 	}
2083 
2084 	if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2085 		/*
2086 		 * This is not an error yet.
2087 		 */
2088 		if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2089 #if 0
2090 			printf("%s: port %d iid %u WWPN %#jx arrived"
2091 			    " again\n", __func__, port->targ_port,
2092 			    iid, (uintmax_t)wwpn);
2093 #endif
2094 			goto take;
2095 		}
2096 		if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2097 		    strcmp(name, port->wwpn_iid[iid].name) == 0) {
2098 #if 0
2099 			printf("%s: port %d iid %u name '%s' arrived"
2100 			    " again\n", __func__, port->targ_port,
2101 			    iid, name);
2102 #endif
2103 			goto take;
2104 		}
2105 
2106 		/*
2107 		 * This is an error, but what do we do about it?  The
2108 		 * driver is telling us we have a new WWPN for this
2109 		 * initiator ID, so we pretty much need to use it.
2110 		 */
2111 		printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2112 		    " but WWPN %#jx '%s' is still at that address\n",
2113 		    __func__, port->targ_port, iid, wwpn, name,
2114 		    (uintmax_t)port->wwpn_iid[iid].wwpn,
2115 		    port->wwpn_iid[iid].name);
2116 
2117 		/*
2118 		 * XXX KDM clear have_ca and ua_pending on each LUN for
2119 		 * this initiator.
2120 		 */
2121 	}
2122 take:
2123 	free(port->wwpn_iid[iid].name, M_CTL);
2124 	port->wwpn_iid[iid].name = name;
2125 	port->wwpn_iid[iid].wwpn = wwpn;
2126 	port->wwpn_iid[iid].in_use++;
2127 	mtx_unlock(&softc->ctl_lock);
2128 	ctl_isc_announce_iid(port, iid);
2129 
2130 	return (iid);
2131 }
2132 
2133 static int
2134 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2135 {
2136 	int len;
2137 
2138 	switch (port->port_type) {
2139 	case CTL_PORT_FC:
2140 	{
2141 		struct scsi_transportid_fcp *id =
2142 		    (struct scsi_transportid_fcp *)buf;
2143 		if (port->wwpn_iid[iid].wwpn == 0)
2144 			return (0);
2145 		memset(id, 0, sizeof(*id));
2146 		id->format_protocol = SCSI_PROTO_FC;
2147 		scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2148 		return (sizeof(*id));
2149 	}
2150 	case CTL_PORT_ISCSI:
2151 	{
2152 		struct scsi_transportid_iscsi_port *id =
2153 		    (struct scsi_transportid_iscsi_port *)buf;
2154 		if (port->wwpn_iid[iid].name == NULL)
2155 			return (0);
2156 		memset(id, 0, 256);
2157 		id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2158 		    SCSI_PROTO_ISCSI;
2159 		len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2160 		len = roundup2(min(len, 252), 4);
2161 		scsi_ulto2b(len, id->additional_length);
2162 		return (sizeof(*id) + len);
2163 	}
2164 	case CTL_PORT_SAS:
2165 	{
2166 		struct scsi_transportid_sas *id =
2167 		    (struct scsi_transportid_sas *)buf;
2168 		if (port->wwpn_iid[iid].wwpn == 0)
2169 			return (0);
2170 		memset(id, 0, sizeof(*id));
2171 		id->format_protocol = SCSI_PROTO_SAS;
2172 		scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2173 		return (sizeof(*id));
2174 	}
2175 	default:
2176 	{
2177 		struct scsi_transportid_spi *id =
2178 		    (struct scsi_transportid_spi *)buf;
2179 		memset(id, 0, sizeof(*id));
2180 		id->format_protocol = SCSI_PROTO_SPI;
2181 		scsi_ulto2b(iid, id->scsi_addr);
2182 		scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2183 		return (sizeof(*id));
2184 	}
2185 	}
2186 }
2187 
2188 /*
2189  * Serialize a command that went down the "wrong" side, and so was sent to
2190  * this controller for execution.  The logic is a little different than the
2191  * standard case in ctl_scsiio_precheck().  Errors in this case need to get
2192  * sent back to the other side, but in the success case, we execute the
2193  * command on this side (XFER mode) or tell the other side to execute it
2194  * (SER_ONLY mode).
2195  */
2196 static int
2197 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2198 {
2199 	struct ctl_softc *softc = control_softc;
2200 	union ctl_ha_msg msg_info;
2201 	struct ctl_port *port;
2202 	struct ctl_lun *lun;
2203 	const struct ctl_cmd_entry *entry;
2204 	int retval = 0;
2205 	uint32_t targ_lun;
2206 
2207 	targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2208 	mtx_lock(&softc->ctl_lock);
2209 
2210 	/* Make sure that we know about this port. */
2211 	port = ctl_io_port(&ctsio->io_hdr);
2212 	if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2213 		ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2214 					 /*retry_count*/ 1);
2215 		goto badjuju;
2216 	}
2217 
2218 	/* Make sure that we know about this LUN. */
2219 	if ((targ_lun < CTL_MAX_LUNS) &&
2220 	    ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
2221 		mtx_lock(&lun->lun_lock);
2222 		mtx_unlock(&softc->ctl_lock);
2223 		/*
2224 		 * If the LUN is invalid, pretend that it doesn't exist.
2225 		 * It will go away as soon as all pending I/O has been
2226 		 * completed.
2227 		 */
2228 		if (lun->flags & CTL_LUN_DISABLED) {
2229 			mtx_unlock(&lun->lun_lock);
2230 			lun = NULL;
2231 		}
2232 	} else {
2233 		mtx_unlock(&softc->ctl_lock);
2234 		lun = NULL;
2235 	}
2236 	if (lun == NULL) {
2237 		/*
2238 		 * The other node would not send this request to us unless
2239 		 * received announce that we are primary node for this LUN.
2240 		 * If this LUN does not exist now, it is probably result of
2241 		 * a race, so respond to initiator in the most opaque way.
2242 		 */
2243 		ctl_set_busy(ctsio);
2244 		goto badjuju;
2245 	}
2246 
2247 	entry = ctl_get_cmd_entry(ctsio, NULL);
2248 	if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2249 		mtx_unlock(&lun->lun_lock);
2250 		goto badjuju;
2251 	}
2252 
2253 	ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2254 	ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2255 
2256 	/*
2257 	 * Every I/O goes into the OOA queue for a
2258 	 * particular LUN, and stays there until completion.
2259 	 */
2260 #ifdef CTL_TIME_IO
2261 	if (TAILQ_EMPTY(&lun->ooa_queue))
2262 		lun->idle_time += getsbinuptime() - lun->last_busy;
2263 #endif
2264 	TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2265 
2266 	switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2267 		(union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2268 		 ooa_links))) {
2269 	case CTL_ACTION_BLOCK:
2270 		ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2271 		TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2272 				  blocked_links);
2273 		mtx_unlock(&lun->lun_lock);
2274 		break;
2275 	case CTL_ACTION_PASS:
2276 	case CTL_ACTION_SKIP:
2277 		if (softc->ha_mode == CTL_HA_MODE_XFER) {
2278 			ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2279 			ctl_enqueue_rtr((union ctl_io *)ctsio);
2280 			mtx_unlock(&lun->lun_lock);
2281 		} else {
2282 			ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2283 			mtx_unlock(&lun->lun_lock);
2284 
2285 			/* send msg back to other side */
2286 			msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2287 			msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2288 			msg_info.hdr.msg_type = CTL_MSG_R2R;
2289 			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2290 			    sizeof(msg_info.hdr), M_WAITOK);
2291 		}
2292 		break;
2293 	case CTL_ACTION_OVERLAP:
2294 		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2295 		mtx_unlock(&lun->lun_lock);
2296 		ctl_set_overlapped_cmd(ctsio);
2297 		goto badjuju;
2298 	case CTL_ACTION_OVERLAP_TAG:
2299 		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2300 		mtx_unlock(&lun->lun_lock);
2301 		ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2302 		goto badjuju;
2303 	case CTL_ACTION_ERROR:
2304 	default:
2305 		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2306 		mtx_unlock(&lun->lun_lock);
2307 
2308 		ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2309 					 /*retry_count*/ 0);
2310 badjuju:
2311 		ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2312 		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2313 		msg_info.hdr.serializing_sc = NULL;
2314 		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2315 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2316 		    sizeof(msg_info.scsi), M_WAITOK);
2317 		retval = 1;
2318 		break;
2319 	}
2320 	return (retval);
2321 }
2322 
2323 /*
2324  * Returns 0 for success, errno for failure.
2325  */
2326 static void
2327 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2328 		   struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2329 {
2330 	union ctl_io *io;
2331 
2332 	mtx_lock(&lun->lun_lock);
2333 	for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2334 	     (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2335 	     ooa_links)) {
2336 		struct ctl_ooa_entry *entry;
2337 
2338 		/*
2339 		 * If we've got more than we can fit, just count the
2340 		 * remaining entries.
2341 		 */
2342 		if (*cur_fill_num >= ooa_hdr->alloc_num)
2343 			continue;
2344 
2345 		entry = &kern_entries[*cur_fill_num];
2346 
2347 		entry->tag_num = io->scsiio.tag_num;
2348 		entry->lun_num = lun->lun;
2349 #ifdef CTL_TIME_IO
2350 		entry->start_bt = io->io_hdr.start_bt;
2351 #endif
2352 		bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2353 		entry->cdb_len = io->scsiio.cdb_len;
2354 		if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2355 			entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2356 
2357 		if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2358 			entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2359 
2360 		if (io->io_hdr.flags & CTL_FLAG_ABORT)
2361 			entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2362 
2363 		if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2364 			entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2365 
2366 		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2367 			entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2368 	}
2369 	mtx_unlock(&lun->lun_lock);
2370 }
2371 
2372 static void *
2373 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2374 		 size_t error_str_len)
2375 {
2376 	void *kptr;
2377 
2378 	kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2379 
2380 	if (copyin(user_addr, kptr, len) != 0) {
2381 		snprintf(error_str, error_str_len, "Error copying %d bytes "
2382 			 "from user address %p to kernel address %p", len,
2383 			 user_addr, kptr);
2384 		free(kptr, M_CTL);
2385 		return (NULL);
2386 	}
2387 
2388 	return (kptr);
2389 }
2390 
2391 static void
2392 ctl_free_args(int num_args, struct ctl_be_arg *args)
2393 {
2394 	int i;
2395 
2396 	if (args == NULL)
2397 		return;
2398 
2399 	for (i = 0; i < num_args; i++) {
2400 		free(args[i].kname, M_CTL);
2401 		free(args[i].kvalue, M_CTL);
2402 	}
2403 
2404 	free(args, M_CTL);
2405 }
2406 
2407 static struct ctl_be_arg *
2408 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2409 		char *error_str, size_t error_str_len)
2410 {
2411 	struct ctl_be_arg *args;
2412 	int i;
2413 
2414 	args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2415 				error_str, error_str_len);
2416 
2417 	if (args == NULL)
2418 		goto bailout;
2419 
2420 	for (i = 0; i < num_args; i++) {
2421 		args[i].kname = NULL;
2422 		args[i].kvalue = NULL;
2423 	}
2424 
2425 	for (i = 0; i < num_args; i++) {
2426 		uint8_t *tmpptr;
2427 
2428 		args[i].kname = ctl_copyin_alloc(args[i].name,
2429 			args[i].namelen, error_str, error_str_len);
2430 		if (args[i].kname == NULL)
2431 			goto bailout;
2432 
2433 		if (args[i].kname[args[i].namelen - 1] != '\0') {
2434 			snprintf(error_str, error_str_len, "Argument %d "
2435 				 "name is not NUL-terminated", i);
2436 			goto bailout;
2437 		}
2438 
2439 		if (args[i].flags & CTL_BEARG_RD) {
2440 			tmpptr = ctl_copyin_alloc(args[i].value,
2441 				args[i].vallen, error_str, error_str_len);
2442 			if (tmpptr == NULL)
2443 				goto bailout;
2444 			if ((args[i].flags & CTL_BEARG_ASCII)
2445 			 && (tmpptr[args[i].vallen - 1] != '\0')) {
2446 				snprintf(error_str, error_str_len, "Argument "
2447 				    "%d value is not NUL-terminated", i);
2448 				free(tmpptr, M_CTL);
2449 				goto bailout;
2450 			}
2451 			args[i].kvalue = tmpptr;
2452 		} else {
2453 			args[i].kvalue = malloc(args[i].vallen,
2454 			    M_CTL, M_WAITOK | M_ZERO);
2455 		}
2456 	}
2457 
2458 	return (args);
2459 bailout:
2460 
2461 	ctl_free_args(num_args, args);
2462 
2463 	return (NULL);
2464 }
2465 
2466 static void
2467 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2468 {
2469 	int i;
2470 
2471 	for (i = 0; i < num_args; i++) {
2472 		if (args[i].flags & CTL_BEARG_WR)
2473 			copyout(args[i].kvalue, args[i].value, args[i].vallen);
2474 	}
2475 }
2476 
2477 /*
2478  * Escape characters that are illegal or not recommended in XML.
2479  */
2480 int
2481 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2482 {
2483 	char *end = str + size;
2484 	int retval;
2485 
2486 	retval = 0;
2487 
2488 	for (; *str && str < end; str++) {
2489 		switch (*str) {
2490 		case '&':
2491 			retval = sbuf_printf(sb, "&amp;");
2492 			break;
2493 		case '>':
2494 			retval = sbuf_printf(sb, "&gt;");
2495 			break;
2496 		case '<':
2497 			retval = sbuf_printf(sb, "&lt;");
2498 			break;
2499 		default:
2500 			retval = sbuf_putc(sb, *str);
2501 			break;
2502 		}
2503 
2504 		if (retval != 0)
2505 			break;
2506 
2507 	}
2508 
2509 	return (retval);
2510 }
2511 
2512 static void
2513 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2514 {
2515 	struct scsi_vpd_id_descriptor *desc;
2516 	int i;
2517 
2518 	if (id == NULL || id->len < 4)
2519 		return;
2520 	desc = (struct scsi_vpd_id_descriptor *)id->data;
2521 	switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2522 	case SVPD_ID_TYPE_T10:
2523 		sbuf_printf(sb, "t10.");
2524 		break;
2525 	case SVPD_ID_TYPE_EUI64:
2526 		sbuf_printf(sb, "eui.");
2527 		break;
2528 	case SVPD_ID_TYPE_NAA:
2529 		sbuf_printf(sb, "naa.");
2530 		break;
2531 	case SVPD_ID_TYPE_SCSI_NAME:
2532 		break;
2533 	}
2534 	switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2535 	case SVPD_ID_CODESET_BINARY:
2536 		for (i = 0; i < desc->length; i++)
2537 			sbuf_printf(sb, "%02x", desc->identifier[i]);
2538 		break;
2539 	case SVPD_ID_CODESET_ASCII:
2540 		sbuf_printf(sb, "%.*s", (int)desc->length,
2541 		    (char *)desc->identifier);
2542 		break;
2543 	case SVPD_ID_CODESET_UTF8:
2544 		sbuf_printf(sb, "%s", (char *)desc->identifier);
2545 		break;
2546 	}
2547 }
2548 
2549 static int
2550 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2551 	  struct thread *td)
2552 {
2553 	struct ctl_softc *softc = dev->si_drv1;
2554 	struct ctl_lun *lun;
2555 	int retval;
2556 
2557 	retval = 0;
2558 
2559 	switch (cmd) {
2560 	case CTL_IO:
2561 		retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2562 		break;
2563 	case CTL_ENABLE_PORT:
2564 	case CTL_DISABLE_PORT:
2565 	case CTL_SET_PORT_WWNS: {
2566 		struct ctl_port *port;
2567 		struct ctl_port_entry *entry;
2568 
2569 		entry = (struct ctl_port_entry *)addr;
2570 
2571 		mtx_lock(&softc->ctl_lock);
2572 		STAILQ_FOREACH(port, &softc->port_list, links) {
2573 			int action, done;
2574 
2575 			if (port->targ_port < softc->port_min ||
2576 			    port->targ_port >= softc->port_max)
2577 				continue;
2578 
2579 			action = 0;
2580 			done = 0;
2581 			if ((entry->port_type == CTL_PORT_NONE)
2582 			 && (entry->targ_port == port->targ_port)) {
2583 				/*
2584 				 * If the user only wants to enable or
2585 				 * disable or set WWNs on a specific port,
2586 				 * do the operation and we're done.
2587 				 */
2588 				action = 1;
2589 				done = 1;
2590 			} else if (entry->port_type & port->port_type) {
2591 				/*
2592 				 * Compare the user's type mask with the
2593 				 * particular frontend type to see if we
2594 				 * have a match.
2595 				 */
2596 				action = 1;
2597 				done = 0;
2598 
2599 				/*
2600 				 * Make sure the user isn't trying to set
2601 				 * WWNs on multiple ports at the same time.
2602 				 */
2603 				if (cmd == CTL_SET_PORT_WWNS) {
2604 					printf("%s: Can't set WWNs on "
2605 					       "multiple ports\n", __func__);
2606 					retval = EINVAL;
2607 					break;
2608 				}
2609 			}
2610 			if (action == 0)
2611 				continue;
2612 
2613 			/*
2614 			 * XXX KDM we have to drop the lock here, because
2615 			 * the online/offline operations can potentially
2616 			 * block.  We need to reference count the frontends
2617 			 * so they can't go away,
2618 			 */
2619 			if (cmd == CTL_ENABLE_PORT) {
2620 				mtx_unlock(&softc->ctl_lock);
2621 				ctl_port_online(port);
2622 				mtx_lock(&softc->ctl_lock);
2623 			} else if (cmd == CTL_DISABLE_PORT) {
2624 				mtx_unlock(&softc->ctl_lock);
2625 				ctl_port_offline(port);
2626 				mtx_lock(&softc->ctl_lock);
2627 			} else if (cmd == CTL_SET_PORT_WWNS) {
2628 				ctl_port_set_wwns(port,
2629 				    (entry->flags & CTL_PORT_WWNN_VALID) ?
2630 				    1 : 0, entry->wwnn,
2631 				    (entry->flags & CTL_PORT_WWPN_VALID) ?
2632 				    1 : 0, entry->wwpn);
2633 			}
2634 			if (done != 0)
2635 				break;
2636 		}
2637 		mtx_unlock(&softc->ctl_lock);
2638 		break;
2639 	}
2640 	case CTL_GET_OOA: {
2641 		struct ctl_ooa *ooa_hdr;
2642 		struct ctl_ooa_entry *entries;
2643 		uint32_t cur_fill_num;
2644 
2645 		ooa_hdr = (struct ctl_ooa *)addr;
2646 
2647 		if ((ooa_hdr->alloc_len == 0)
2648 		 || (ooa_hdr->alloc_num == 0)) {
2649 			printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2650 			       "must be non-zero\n", __func__,
2651 			       ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2652 			retval = EINVAL;
2653 			break;
2654 		}
2655 
2656 		if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2657 		    sizeof(struct ctl_ooa_entry))) {
2658 			printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2659 			       "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2660 			       __func__, ooa_hdr->alloc_len,
2661 			       ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2662 			retval = EINVAL;
2663 			break;
2664 		}
2665 
2666 		entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2667 		if (entries == NULL) {
2668 			printf("%s: could not allocate %d bytes for OOA "
2669 			       "dump\n", __func__, ooa_hdr->alloc_len);
2670 			retval = ENOMEM;
2671 			break;
2672 		}
2673 
2674 		mtx_lock(&softc->ctl_lock);
2675 		if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2676 		 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2677 		  || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2678 			mtx_unlock(&softc->ctl_lock);
2679 			free(entries, M_CTL);
2680 			printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2681 			       __func__, (uintmax_t)ooa_hdr->lun_num);
2682 			retval = EINVAL;
2683 			break;
2684 		}
2685 
2686 		cur_fill_num = 0;
2687 
2688 		if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2689 			STAILQ_FOREACH(lun, &softc->lun_list, links) {
2690 				ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2691 				    ooa_hdr, entries);
2692 			}
2693 		} else {
2694 			lun = softc->ctl_luns[ooa_hdr->lun_num];
2695 			ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2696 			    entries);
2697 		}
2698 		mtx_unlock(&softc->ctl_lock);
2699 
2700 		ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2701 		ooa_hdr->fill_len = ooa_hdr->fill_num *
2702 			sizeof(struct ctl_ooa_entry);
2703 		retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2704 		if (retval != 0) {
2705 			printf("%s: error copying out %d bytes for OOA dump\n",
2706 			       __func__, ooa_hdr->fill_len);
2707 		}
2708 
2709 		getbinuptime(&ooa_hdr->cur_bt);
2710 
2711 		if (cur_fill_num > ooa_hdr->alloc_num) {
2712 			ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2713 			ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2714 		} else {
2715 			ooa_hdr->dropped_num = 0;
2716 			ooa_hdr->status = CTL_OOA_OK;
2717 		}
2718 
2719 		free(entries, M_CTL);
2720 		break;
2721 	}
2722 	case CTL_DELAY_IO: {
2723 		struct ctl_io_delay_info *delay_info;
2724 
2725 		delay_info = (struct ctl_io_delay_info *)addr;
2726 
2727 #ifdef CTL_IO_DELAY
2728 		mtx_lock(&softc->ctl_lock);
2729 
2730 		if ((delay_info->lun_id >= CTL_MAX_LUNS)
2731 		 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2732 			delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2733 		} else {
2734 			lun = softc->ctl_luns[delay_info->lun_id];
2735 			mtx_lock(&lun->lun_lock);
2736 
2737 			delay_info->status = CTL_DELAY_STATUS_OK;
2738 
2739 			switch (delay_info->delay_type) {
2740 			case CTL_DELAY_TYPE_CONT:
2741 				break;
2742 			case CTL_DELAY_TYPE_ONESHOT:
2743 				break;
2744 			default:
2745 				delay_info->status =
2746 					CTL_DELAY_STATUS_INVALID_TYPE;
2747 				break;
2748 			}
2749 
2750 			switch (delay_info->delay_loc) {
2751 			case CTL_DELAY_LOC_DATAMOVE:
2752 				lun->delay_info.datamove_type =
2753 					delay_info->delay_type;
2754 				lun->delay_info.datamove_delay =
2755 					delay_info->delay_secs;
2756 				break;
2757 			case CTL_DELAY_LOC_DONE:
2758 				lun->delay_info.done_type =
2759 					delay_info->delay_type;
2760 				lun->delay_info.done_delay =
2761 					delay_info->delay_secs;
2762 				break;
2763 			default:
2764 				delay_info->status =
2765 					CTL_DELAY_STATUS_INVALID_LOC;
2766 				break;
2767 			}
2768 			mtx_unlock(&lun->lun_lock);
2769 		}
2770 
2771 		mtx_unlock(&softc->ctl_lock);
2772 #else
2773 		delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2774 #endif /* CTL_IO_DELAY */
2775 		break;
2776 	}
2777 	case CTL_GETSTATS: {
2778 		struct ctl_stats *stats;
2779 		int i;
2780 
2781 		stats = (struct ctl_stats *)addr;
2782 
2783 		if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2784 		     stats->alloc_len) {
2785 			stats->status = CTL_SS_NEED_MORE_SPACE;
2786 			stats->num_luns = softc->num_luns;
2787 			break;
2788 		}
2789 		/*
2790 		 * XXX KDM no locking here.  If the LUN list changes,
2791 		 * things can blow up.
2792 		 */
2793 		i = 0;
2794 		STAILQ_FOREACH(lun, &softc->lun_list, links) {
2795 			retval = copyout(&lun->stats, &stats->lun_stats[i++],
2796 					 sizeof(lun->stats));
2797 			if (retval != 0)
2798 				break;
2799 		}
2800 		stats->num_luns = softc->num_luns;
2801 		stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2802 				 softc->num_luns;
2803 		stats->status = CTL_SS_OK;
2804 #ifdef CTL_TIME_IO
2805 		stats->flags = CTL_STATS_FLAG_TIME_VALID;
2806 #else
2807 		stats->flags = CTL_STATS_FLAG_NONE;
2808 #endif
2809 		getnanouptime(&stats->timestamp);
2810 		break;
2811 	}
2812 	case CTL_ERROR_INJECT: {
2813 		struct ctl_error_desc *err_desc, *new_err_desc;
2814 
2815 		err_desc = (struct ctl_error_desc *)addr;
2816 
2817 		new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2818 				      M_WAITOK | M_ZERO);
2819 		bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2820 
2821 		mtx_lock(&softc->ctl_lock);
2822 		lun = softc->ctl_luns[err_desc->lun_id];
2823 		if (lun == NULL) {
2824 			mtx_unlock(&softc->ctl_lock);
2825 			free(new_err_desc, M_CTL);
2826 			printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2827 			       __func__, (uintmax_t)err_desc->lun_id);
2828 			retval = EINVAL;
2829 			break;
2830 		}
2831 		mtx_lock(&lun->lun_lock);
2832 		mtx_unlock(&softc->ctl_lock);
2833 
2834 		/*
2835 		 * We could do some checking here to verify the validity
2836 		 * of the request, but given the complexity of error
2837 		 * injection requests, the checking logic would be fairly
2838 		 * complex.
2839 		 *
2840 		 * For now, if the request is invalid, it just won't get
2841 		 * executed and might get deleted.
2842 		 */
2843 		STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2844 
2845 		/*
2846 		 * XXX KDM check to make sure the serial number is unique,
2847 		 * in case we somehow manage to wrap.  That shouldn't
2848 		 * happen for a very long time, but it's the right thing to
2849 		 * do.
2850 		 */
2851 		new_err_desc->serial = lun->error_serial;
2852 		err_desc->serial = lun->error_serial;
2853 		lun->error_serial++;
2854 
2855 		mtx_unlock(&lun->lun_lock);
2856 		break;
2857 	}
2858 	case CTL_ERROR_INJECT_DELETE: {
2859 		struct ctl_error_desc *delete_desc, *desc, *desc2;
2860 		int delete_done;
2861 
2862 		delete_desc = (struct ctl_error_desc *)addr;
2863 		delete_done = 0;
2864 
2865 		mtx_lock(&softc->ctl_lock);
2866 		lun = softc->ctl_luns[delete_desc->lun_id];
2867 		if (lun == NULL) {
2868 			mtx_unlock(&softc->ctl_lock);
2869 			printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2870 			       __func__, (uintmax_t)delete_desc->lun_id);
2871 			retval = EINVAL;
2872 			break;
2873 		}
2874 		mtx_lock(&lun->lun_lock);
2875 		mtx_unlock(&softc->ctl_lock);
2876 		STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2877 			if (desc->serial != delete_desc->serial)
2878 				continue;
2879 
2880 			STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2881 				      links);
2882 			free(desc, M_CTL);
2883 			delete_done = 1;
2884 		}
2885 		mtx_unlock(&lun->lun_lock);
2886 		if (delete_done == 0) {
2887 			printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2888 			       "error serial %ju on LUN %u\n", __func__,
2889 			       delete_desc->serial, delete_desc->lun_id);
2890 			retval = EINVAL;
2891 			break;
2892 		}
2893 		break;
2894 	}
2895 	case CTL_DUMP_STRUCTS: {
2896 		int i, j, k;
2897 		struct ctl_port *port;
2898 		struct ctl_frontend *fe;
2899 
2900 		mtx_lock(&softc->ctl_lock);
2901 		printf("CTL Persistent Reservation information start:\n");
2902 		for (i = 0; i < CTL_MAX_LUNS; i++) {
2903 			lun = softc->ctl_luns[i];
2904 
2905 			if ((lun == NULL)
2906 			 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2907 				continue;
2908 
2909 			for (j = 0; j < CTL_MAX_PORTS; j++) {
2910 				if (lun->pr_keys[j] == NULL)
2911 					continue;
2912 				for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2913 					if (lun->pr_keys[j][k] == 0)
2914 						continue;
2915 					printf("  LUN %d port %d iid %d key "
2916 					       "%#jx\n", i, j, k,
2917 					       (uintmax_t)lun->pr_keys[j][k]);
2918 				}
2919 			}
2920 		}
2921 		printf("CTL Persistent Reservation information end\n");
2922 		printf("CTL Ports:\n");
2923 		STAILQ_FOREACH(port, &softc->port_list, links) {
2924 			printf("  Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2925 			       "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2926 			       port->frontend->name, port->port_type,
2927 			       port->physical_port, port->virtual_port,
2928 			       (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2929 			for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2930 				if (port->wwpn_iid[j].in_use == 0 &&
2931 				    port->wwpn_iid[j].wwpn == 0 &&
2932 				    port->wwpn_iid[j].name == NULL)
2933 					continue;
2934 
2935 				printf("    iid %u use %d WWPN %#jx '%s'\n",
2936 				    j, port->wwpn_iid[j].in_use,
2937 				    (uintmax_t)port->wwpn_iid[j].wwpn,
2938 				    port->wwpn_iid[j].name);
2939 			}
2940 		}
2941 		printf("CTL Port information end\n");
2942 		mtx_unlock(&softc->ctl_lock);
2943 		/*
2944 		 * XXX KDM calling this without a lock.  We'd likely want
2945 		 * to drop the lock before calling the frontend's dump
2946 		 * routine anyway.
2947 		 */
2948 		printf("CTL Frontends:\n");
2949 		STAILQ_FOREACH(fe, &softc->fe_list, links) {
2950 			printf("  Frontend '%s'\n", fe->name);
2951 			if (fe->fe_dump != NULL)
2952 				fe->fe_dump();
2953 		}
2954 		printf("CTL Frontend information end\n");
2955 		break;
2956 	}
2957 	case CTL_LUN_REQ: {
2958 		struct ctl_lun_req *lun_req;
2959 		struct ctl_backend_driver *backend;
2960 
2961 		lun_req = (struct ctl_lun_req *)addr;
2962 
2963 		backend = ctl_backend_find(lun_req->backend);
2964 		if (backend == NULL) {
2965 			lun_req->status = CTL_LUN_ERROR;
2966 			snprintf(lun_req->error_str,
2967 				 sizeof(lun_req->error_str),
2968 				 "Backend \"%s\" not found.",
2969 				 lun_req->backend);
2970 			break;
2971 		}
2972 		if (lun_req->num_be_args > 0) {
2973 			lun_req->kern_be_args = ctl_copyin_args(
2974 				lun_req->num_be_args,
2975 				lun_req->be_args,
2976 				lun_req->error_str,
2977 				sizeof(lun_req->error_str));
2978 			if (lun_req->kern_be_args == NULL) {
2979 				lun_req->status = CTL_LUN_ERROR;
2980 				break;
2981 			}
2982 		}
2983 
2984 		retval = backend->ioctl(dev, cmd, addr, flag, td);
2985 
2986 		if (lun_req->num_be_args > 0) {
2987 			ctl_copyout_args(lun_req->num_be_args,
2988 				      lun_req->kern_be_args);
2989 			ctl_free_args(lun_req->num_be_args,
2990 				      lun_req->kern_be_args);
2991 		}
2992 		break;
2993 	}
2994 	case CTL_LUN_LIST: {
2995 		struct sbuf *sb;
2996 		struct ctl_lun_list *list;
2997 		struct ctl_option *opt;
2998 
2999 		list = (struct ctl_lun_list *)addr;
3000 
3001 		/*
3002 		 * Allocate a fixed length sbuf here, based on the length
3003 		 * of the user's buffer.  We could allocate an auto-extending
3004 		 * buffer, and then tell the user how much larger our
3005 		 * amount of data is than his buffer, but that presents
3006 		 * some problems:
3007 		 *
3008 		 * 1.  The sbuf(9) routines use a blocking malloc, and so
3009 		 *     we can't hold a lock while calling them with an
3010 		 *     auto-extending buffer.
3011  		 *
3012 		 * 2.  There is not currently a LUN reference counting
3013 		 *     mechanism, outside of outstanding transactions on
3014 		 *     the LUN's OOA queue.  So a LUN could go away on us
3015 		 *     while we're getting the LUN number, backend-specific
3016 		 *     information, etc.  Thus, given the way things
3017 		 *     currently work, we need to hold the CTL lock while
3018 		 *     grabbing LUN information.
3019 		 *
3020 		 * So, from the user's standpoint, the best thing to do is
3021 		 * allocate what he thinks is a reasonable buffer length,
3022 		 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3023 		 * double the buffer length and try again.  (And repeat
3024 		 * that until he succeeds.)
3025 		 */
3026 		sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3027 		if (sb == NULL) {
3028 			list->status = CTL_LUN_LIST_ERROR;
3029 			snprintf(list->error_str, sizeof(list->error_str),
3030 				 "Unable to allocate %d bytes for LUN list",
3031 				 list->alloc_len);
3032 			break;
3033 		}
3034 
3035 		sbuf_printf(sb, "<ctllunlist>\n");
3036 
3037 		mtx_lock(&softc->ctl_lock);
3038 		STAILQ_FOREACH(lun, &softc->lun_list, links) {
3039 			mtx_lock(&lun->lun_lock);
3040 			retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3041 					     (uintmax_t)lun->lun);
3042 
3043 			/*
3044 			 * Bail out as soon as we see that we've overfilled
3045 			 * the buffer.
3046 			 */
3047 			if (retval != 0)
3048 				break;
3049 
3050 			retval = sbuf_printf(sb, "\t<backend_type>%s"
3051 					     "</backend_type>\n",
3052 					     (lun->backend == NULL) ?  "none" :
3053 					     lun->backend->name);
3054 
3055 			if (retval != 0)
3056 				break;
3057 
3058 			retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3059 					     lun->be_lun->lun_type);
3060 
3061 			if (retval != 0)
3062 				break;
3063 
3064 			if (lun->backend == NULL) {
3065 				retval = sbuf_printf(sb, "</lun>\n");
3066 				if (retval != 0)
3067 					break;
3068 				continue;
3069 			}
3070 
3071 			retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3072 					     (lun->be_lun->maxlba > 0) ?
3073 					     lun->be_lun->maxlba + 1 : 0);
3074 
3075 			if (retval != 0)
3076 				break;
3077 
3078 			retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3079 					     lun->be_lun->blocksize);
3080 
3081 			if (retval != 0)
3082 				break;
3083 
3084 			retval = sbuf_printf(sb, "\t<serial_number>");
3085 
3086 			if (retval != 0)
3087 				break;
3088 
3089 			retval = ctl_sbuf_printf_esc(sb,
3090 			    lun->be_lun->serial_num,
3091 			    sizeof(lun->be_lun->serial_num));
3092 
3093 			if (retval != 0)
3094 				break;
3095 
3096 			retval = sbuf_printf(sb, "</serial_number>\n");
3097 
3098 			if (retval != 0)
3099 				break;
3100 
3101 			retval = sbuf_printf(sb, "\t<device_id>");
3102 
3103 			if (retval != 0)
3104 				break;
3105 
3106 			retval = ctl_sbuf_printf_esc(sb,
3107 			    lun->be_lun->device_id,
3108 			    sizeof(lun->be_lun->device_id));
3109 
3110 			if (retval != 0)
3111 				break;
3112 
3113 			retval = sbuf_printf(sb, "</device_id>\n");
3114 
3115 			if (retval != 0)
3116 				break;
3117 
3118 			if (lun->backend->lun_info != NULL) {
3119 				retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3120 				if (retval != 0)
3121 					break;
3122 			}
3123 			STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3124 				retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3125 				    opt->name, opt->value, opt->name);
3126 				if (retval != 0)
3127 					break;
3128 			}
3129 
3130 			retval = sbuf_printf(sb, "</lun>\n");
3131 
3132 			if (retval != 0)
3133 				break;
3134 			mtx_unlock(&lun->lun_lock);
3135 		}
3136 		if (lun != NULL)
3137 			mtx_unlock(&lun->lun_lock);
3138 		mtx_unlock(&softc->ctl_lock);
3139 
3140 		if ((retval != 0)
3141 		 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3142 			retval = 0;
3143 			sbuf_delete(sb);
3144 			list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3145 			snprintf(list->error_str, sizeof(list->error_str),
3146 				 "Out of space, %d bytes is too small",
3147 				 list->alloc_len);
3148 			break;
3149 		}
3150 
3151 		sbuf_finish(sb);
3152 
3153 		retval = copyout(sbuf_data(sb), list->lun_xml,
3154 				 sbuf_len(sb) + 1);
3155 
3156 		list->fill_len = sbuf_len(sb) + 1;
3157 		list->status = CTL_LUN_LIST_OK;
3158 		sbuf_delete(sb);
3159 		break;
3160 	}
3161 	case CTL_ISCSI: {
3162 		struct ctl_iscsi *ci;
3163 		struct ctl_frontend *fe;
3164 
3165 		ci = (struct ctl_iscsi *)addr;
3166 
3167 		fe = ctl_frontend_find("iscsi");
3168 		if (fe == NULL) {
3169 			ci->status = CTL_ISCSI_ERROR;
3170 			snprintf(ci->error_str, sizeof(ci->error_str),
3171 			    "Frontend \"iscsi\" not found.");
3172 			break;
3173 		}
3174 
3175 		retval = fe->ioctl(dev, cmd, addr, flag, td);
3176 		break;
3177 	}
3178 	case CTL_PORT_REQ: {
3179 		struct ctl_req *req;
3180 		struct ctl_frontend *fe;
3181 
3182 		req = (struct ctl_req *)addr;
3183 
3184 		fe = ctl_frontend_find(req->driver);
3185 		if (fe == NULL) {
3186 			req->status = CTL_LUN_ERROR;
3187 			snprintf(req->error_str, sizeof(req->error_str),
3188 			    "Frontend \"%s\" not found.", req->driver);
3189 			break;
3190 		}
3191 		if (req->num_args > 0) {
3192 			req->kern_args = ctl_copyin_args(req->num_args,
3193 			    req->args, req->error_str, sizeof(req->error_str));
3194 			if (req->kern_args == NULL) {
3195 				req->status = CTL_LUN_ERROR;
3196 				break;
3197 			}
3198 		}
3199 
3200 		if (fe->ioctl)
3201 			retval = fe->ioctl(dev, cmd, addr, flag, td);
3202 		else
3203 			retval = ENODEV;
3204 
3205 		if (req->num_args > 0) {
3206 			ctl_copyout_args(req->num_args, req->kern_args);
3207 			ctl_free_args(req->num_args, req->kern_args);
3208 		}
3209 		break;
3210 	}
3211 	case CTL_PORT_LIST: {
3212 		struct sbuf *sb;
3213 		struct ctl_port *port;
3214 		struct ctl_lun_list *list;
3215 		struct ctl_option *opt;
3216 		int j;
3217 		uint32_t plun;
3218 
3219 		list = (struct ctl_lun_list *)addr;
3220 
3221 		sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3222 		if (sb == NULL) {
3223 			list->status = CTL_LUN_LIST_ERROR;
3224 			snprintf(list->error_str, sizeof(list->error_str),
3225 				 "Unable to allocate %d bytes for LUN list",
3226 				 list->alloc_len);
3227 			break;
3228 		}
3229 
3230 		sbuf_printf(sb, "<ctlportlist>\n");
3231 
3232 		mtx_lock(&softc->ctl_lock);
3233 		STAILQ_FOREACH(port, &softc->port_list, links) {
3234 			retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3235 					     (uintmax_t)port->targ_port);
3236 
3237 			/*
3238 			 * Bail out as soon as we see that we've overfilled
3239 			 * the buffer.
3240 			 */
3241 			if (retval != 0)
3242 				break;
3243 
3244 			retval = sbuf_printf(sb, "\t<frontend_type>%s"
3245 			    "</frontend_type>\n", port->frontend->name);
3246 			if (retval != 0)
3247 				break;
3248 
3249 			retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3250 					     port->port_type);
3251 			if (retval != 0)
3252 				break;
3253 
3254 			retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3255 			    (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3256 			if (retval != 0)
3257 				break;
3258 
3259 			retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3260 			    port->port_name);
3261 			if (retval != 0)
3262 				break;
3263 
3264 			retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3265 			    port->physical_port);
3266 			if (retval != 0)
3267 				break;
3268 
3269 			retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3270 			    port->virtual_port);
3271 			if (retval != 0)
3272 				break;
3273 
3274 			if (port->target_devid != NULL) {
3275 				sbuf_printf(sb, "\t<target>");
3276 				ctl_id_sbuf(port->target_devid, sb);
3277 				sbuf_printf(sb, "</target>\n");
3278 			}
3279 
3280 			if (port->port_devid != NULL) {
3281 				sbuf_printf(sb, "\t<port>");
3282 				ctl_id_sbuf(port->port_devid, sb);
3283 				sbuf_printf(sb, "</port>\n");
3284 			}
3285 
3286 			if (port->port_info != NULL) {
3287 				retval = port->port_info(port->onoff_arg, sb);
3288 				if (retval != 0)
3289 					break;
3290 			}
3291 			STAILQ_FOREACH(opt, &port->options, links) {
3292 				retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3293 				    opt->name, opt->value, opt->name);
3294 				if (retval != 0)
3295 					break;
3296 			}
3297 
3298 			if (port->lun_map != NULL) {
3299 				sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3300 				for (j = 0; j < CTL_MAX_LUNS; j++) {
3301 					plun = ctl_lun_map_from_port(port, j);
3302 					if (plun >= CTL_MAX_LUNS)
3303 						continue;
3304 					sbuf_printf(sb,
3305 					    "\t<lun id=\"%u\">%u</lun>\n",
3306 					    j, plun);
3307 				}
3308 			}
3309 
3310 			for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3311 				if (port->wwpn_iid[j].in_use == 0 ||
3312 				    (port->wwpn_iid[j].wwpn == 0 &&
3313 				     port->wwpn_iid[j].name == NULL))
3314 					continue;
3315 
3316 				if (port->wwpn_iid[j].name != NULL)
3317 					retval = sbuf_printf(sb,
3318 					    "\t<initiator id=\"%u\">%s</initiator>\n",
3319 					    j, port->wwpn_iid[j].name);
3320 				else
3321 					retval = sbuf_printf(sb,
3322 					    "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3323 					    j, port->wwpn_iid[j].wwpn);
3324 				if (retval != 0)
3325 					break;
3326 			}
3327 			if (retval != 0)
3328 				break;
3329 
3330 			retval = sbuf_printf(sb, "</targ_port>\n");
3331 			if (retval != 0)
3332 				break;
3333 		}
3334 		mtx_unlock(&softc->ctl_lock);
3335 
3336 		if ((retval != 0)
3337 		 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3338 			retval = 0;
3339 			sbuf_delete(sb);
3340 			list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3341 			snprintf(list->error_str, sizeof(list->error_str),
3342 				 "Out of space, %d bytes is too small",
3343 				 list->alloc_len);
3344 			break;
3345 		}
3346 
3347 		sbuf_finish(sb);
3348 
3349 		retval = copyout(sbuf_data(sb), list->lun_xml,
3350 				 sbuf_len(sb) + 1);
3351 
3352 		list->fill_len = sbuf_len(sb) + 1;
3353 		list->status = CTL_LUN_LIST_OK;
3354 		sbuf_delete(sb);
3355 		break;
3356 	}
3357 	case CTL_LUN_MAP: {
3358 		struct ctl_lun_map *lm  = (struct ctl_lun_map *)addr;
3359 		struct ctl_port *port;
3360 
3361 		mtx_lock(&softc->ctl_lock);
3362 		if (lm->port < softc->port_min ||
3363 		    lm->port >= softc->port_max ||
3364 		    (port = softc->ctl_ports[lm->port]) == NULL) {
3365 			mtx_unlock(&softc->ctl_lock);
3366 			return (ENXIO);
3367 		}
3368 		if (port->status & CTL_PORT_STATUS_ONLINE) {
3369 			STAILQ_FOREACH(lun, &softc->lun_list, links) {
3370 				if (ctl_lun_map_to_port(port, lun->lun) >=
3371 				    CTL_MAX_LUNS)
3372 					continue;
3373 				mtx_lock(&lun->lun_lock);
3374 				ctl_est_ua_port(lun, lm->port, -1,
3375 				    CTL_UA_LUN_CHANGE);
3376 				mtx_unlock(&lun->lun_lock);
3377 			}
3378 		}
3379 		mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3380 		if (lm->plun < CTL_MAX_LUNS) {
3381 			if (lm->lun == UINT32_MAX)
3382 				retval = ctl_lun_map_unset(port, lm->plun);
3383 			else if (lm->lun < CTL_MAX_LUNS &&
3384 			    softc->ctl_luns[lm->lun] != NULL)
3385 				retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3386 			else
3387 				return (ENXIO);
3388 		} else if (lm->plun == UINT32_MAX) {
3389 			if (lm->lun == UINT32_MAX)
3390 				retval = ctl_lun_map_deinit(port);
3391 			else
3392 				retval = ctl_lun_map_init(port);
3393 		} else
3394 			return (ENXIO);
3395 		if (port->status & CTL_PORT_STATUS_ONLINE)
3396 			ctl_isc_announce_port(port);
3397 		break;
3398 	}
3399 	default: {
3400 		/* XXX KDM should we fix this? */
3401 #if 0
3402 		struct ctl_backend_driver *backend;
3403 		unsigned int type;
3404 		int found;
3405 
3406 		found = 0;
3407 
3408 		/*
3409 		 * We encode the backend type as the ioctl type for backend
3410 		 * ioctls.  So parse it out here, and then search for a
3411 		 * backend of this type.
3412 		 */
3413 		type = _IOC_TYPE(cmd);
3414 
3415 		STAILQ_FOREACH(backend, &softc->be_list, links) {
3416 			if (backend->type == type) {
3417 				found = 1;
3418 				break;
3419 			}
3420 		}
3421 		if (found == 0) {
3422 			printf("ctl: unknown ioctl command %#lx or backend "
3423 			       "%d\n", cmd, type);
3424 			retval = EINVAL;
3425 			break;
3426 		}
3427 		retval = backend->ioctl(dev, cmd, addr, flag, td);
3428 #endif
3429 		retval = ENOTTY;
3430 		break;
3431 	}
3432 	}
3433 	return (retval);
3434 }
3435 
3436 uint32_t
3437 ctl_get_initindex(struct ctl_nexus *nexus)
3438 {
3439 	return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3440 }
3441 
3442 int
3443 ctl_lun_map_init(struct ctl_port *port)
3444 {
3445 	struct ctl_softc *softc = port->ctl_softc;
3446 	struct ctl_lun *lun;
3447 	uint32_t i;
3448 
3449 	if (port->lun_map == NULL)
3450 		port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3451 		    M_CTL, M_NOWAIT);
3452 	if (port->lun_map == NULL)
3453 		return (ENOMEM);
3454 	for (i = 0; i < CTL_MAX_LUNS; i++)
3455 		port->lun_map[i] = UINT32_MAX;
3456 	if (port->status & CTL_PORT_STATUS_ONLINE) {
3457 		if (port->lun_disable != NULL) {
3458 			STAILQ_FOREACH(lun, &softc->lun_list, links)
3459 				port->lun_disable(port->targ_lun_arg, lun->lun);
3460 		}
3461 		ctl_isc_announce_port(port);
3462 	}
3463 	return (0);
3464 }
3465 
3466 int
3467 ctl_lun_map_deinit(struct ctl_port *port)
3468 {
3469 	struct ctl_softc *softc = port->ctl_softc;
3470 	struct ctl_lun *lun;
3471 
3472 	if (port->lun_map == NULL)
3473 		return (0);
3474 	free(port->lun_map, M_CTL);
3475 	port->lun_map = NULL;
3476 	if (port->status & CTL_PORT_STATUS_ONLINE) {
3477 		if (port->lun_enable != NULL) {
3478 			STAILQ_FOREACH(lun, &softc->lun_list, links)
3479 				port->lun_enable(port->targ_lun_arg, lun->lun);
3480 		}
3481 		ctl_isc_announce_port(port);
3482 	}
3483 	return (0);
3484 }
3485 
3486 int
3487 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3488 {
3489 	int status;
3490 	uint32_t old;
3491 
3492 	if (port->lun_map == NULL) {
3493 		status = ctl_lun_map_init(port);
3494 		if (status != 0)
3495 			return (status);
3496 	}
3497 	old = port->lun_map[plun];
3498 	port->lun_map[plun] = glun;
3499 	if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3500 		if (port->lun_enable != NULL)
3501 			port->lun_enable(port->targ_lun_arg, plun);
3502 		ctl_isc_announce_port(port);
3503 	}
3504 	return (0);
3505 }
3506 
3507 int
3508 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3509 {
3510 	uint32_t old;
3511 
3512 	if (port->lun_map == NULL)
3513 		return (0);
3514 	old = port->lun_map[plun];
3515 	port->lun_map[plun] = UINT32_MAX;
3516 	if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3517 		if (port->lun_disable != NULL)
3518 			port->lun_disable(port->targ_lun_arg, plun);
3519 		ctl_isc_announce_port(port);
3520 	}
3521 	return (0);
3522 }
3523 
3524 uint32_t
3525 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3526 {
3527 
3528 	if (port == NULL)
3529 		return (UINT32_MAX);
3530 	if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3531 		return (lun_id);
3532 	return (port->lun_map[lun_id]);
3533 }
3534 
3535 uint32_t
3536 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3537 {
3538 	uint32_t i;
3539 
3540 	if (port == NULL)
3541 		return (UINT32_MAX);
3542 	if (port->lun_map == NULL)
3543 		return (lun_id);
3544 	for (i = 0; i < CTL_MAX_LUNS; i++) {
3545 		if (port->lun_map[i] == lun_id)
3546 			return (i);
3547 	}
3548 	return (UINT32_MAX);
3549 }
3550 
3551 uint32_t
3552 ctl_decode_lun(uint64_t encoded)
3553 {
3554 	uint8_t lun[8];
3555 	uint32_t result = 0xffffffff;
3556 
3557 	be64enc(lun, encoded);
3558 	switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3559 	case RPL_LUNDATA_ATYP_PERIPH:
3560 		if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3561 		    lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3562 			result = lun[1];
3563 		break;
3564 	case RPL_LUNDATA_ATYP_FLAT:
3565 		if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3566 		    lun[6] == 0 && lun[7] == 0)
3567 			result = ((lun[0] & 0x3f) << 8) + lun[1];
3568 		break;
3569 	case RPL_LUNDATA_ATYP_EXTLUN:
3570 		switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3571 		case 0x02:
3572 			switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3573 			case 0x00:
3574 				result = lun[1];
3575 				break;
3576 			case 0x10:
3577 				result = (lun[1] << 16) + (lun[2] << 8) +
3578 				    lun[3];
3579 				break;
3580 			case 0x20:
3581 				if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3582 					result = (lun[2] << 24) +
3583 					    (lun[3] << 16) + (lun[4] << 8) +
3584 					    lun[5];
3585 				break;
3586 			}
3587 			break;
3588 		case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3589 			result = 0xffffffff;
3590 			break;
3591 		}
3592 		break;
3593 	}
3594 	return (result);
3595 }
3596 
3597 uint64_t
3598 ctl_encode_lun(uint32_t decoded)
3599 {
3600 	uint64_t l = decoded;
3601 
3602 	if (l <= 0xff)
3603 		return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3604 	if (l <= 0x3fff)
3605 		return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3606 	if (l <= 0xffffff)
3607 		return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3608 		    (l << 32));
3609 	return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3610 }
3611 
3612 static struct ctl_port *
3613 ctl_io_port(struct ctl_io_hdr *io_hdr)
3614 {
3615 
3616 	return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3617 }
3618 
3619 int
3620 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3621 {
3622 	int i;
3623 
3624 	for (i = first; i < last; i++) {
3625 		if ((mask[i / 32] & (1 << (i % 32))) == 0)
3626 			return (i);
3627 	}
3628 	return (-1);
3629 }
3630 
3631 int
3632 ctl_set_mask(uint32_t *mask, uint32_t bit)
3633 {
3634 	uint32_t chunk, piece;
3635 
3636 	chunk = bit >> 5;
3637 	piece = bit % (sizeof(uint32_t) * 8);
3638 
3639 	if ((mask[chunk] & (1 << piece)) != 0)
3640 		return (-1);
3641 	else
3642 		mask[chunk] |= (1 << piece);
3643 
3644 	return (0);
3645 }
3646 
3647 int
3648 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3649 {
3650 	uint32_t chunk, piece;
3651 
3652 	chunk = bit >> 5;
3653 	piece = bit % (sizeof(uint32_t) * 8);
3654 
3655 	if ((mask[chunk] & (1 << piece)) == 0)
3656 		return (-1);
3657 	else
3658 		mask[chunk] &= ~(1 << piece);
3659 
3660 	return (0);
3661 }
3662 
3663 int
3664 ctl_is_set(uint32_t *mask, uint32_t bit)
3665 {
3666 	uint32_t chunk, piece;
3667 
3668 	chunk = bit >> 5;
3669 	piece = bit % (sizeof(uint32_t) * 8);
3670 
3671 	if ((mask[chunk] & (1 << piece)) == 0)
3672 		return (0);
3673 	else
3674 		return (1);
3675 }
3676 
3677 static uint64_t
3678 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3679 {
3680 	uint64_t *t;
3681 
3682 	t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3683 	if (t == NULL)
3684 		return (0);
3685 	return (t[residx % CTL_MAX_INIT_PER_PORT]);
3686 }
3687 
3688 static void
3689 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3690 {
3691 	uint64_t *t;
3692 
3693 	t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3694 	if (t == NULL)
3695 		return;
3696 	t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3697 }
3698 
3699 static void
3700 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3701 {
3702 	uint64_t *p;
3703 	u_int i;
3704 
3705 	i = residx/CTL_MAX_INIT_PER_PORT;
3706 	if (lun->pr_keys[i] != NULL)
3707 		return;
3708 	mtx_unlock(&lun->lun_lock);
3709 	p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3710 	    M_WAITOK | M_ZERO);
3711 	mtx_lock(&lun->lun_lock);
3712 	if (lun->pr_keys[i] == NULL)
3713 		lun->pr_keys[i] = p;
3714 	else
3715 		free(p, M_CTL);
3716 }
3717 
3718 static void
3719 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3720 {
3721 	uint64_t *t;
3722 
3723 	t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3724 	KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3725 	t[residx % CTL_MAX_INIT_PER_PORT] = key;
3726 }
3727 
3728 /*
3729  * ctl_softc, pool_name, total_ctl_io are passed in.
3730  * npool is passed out.
3731  */
3732 int
3733 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3734 		uint32_t total_ctl_io, void **npool)
3735 {
3736 #ifdef IO_POOLS
3737 	struct ctl_io_pool *pool;
3738 
3739 	pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3740 					    M_NOWAIT | M_ZERO);
3741 	if (pool == NULL)
3742 		return (ENOMEM);
3743 
3744 	snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3745 	pool->ctl_softc = ctl_softc;
3746 	pool->zone = uma_zsecond_create(pool->name, NULL,
3747 	    NULL, NULL, NULL, ctl_softc->io_zone);
3748 	/* uma_prealloc(pool->zone, total_ctl_io); */
3749 
3750 	*npool = pool;
3751 #else
3752 	*npool = ctl_softc->io_zone;
3753 #endif
3754 	return (0);
3755 }
3756 
3757 void
3758 ctl_pool_free(struct ctl_io_pool *pool)
3759 {
3760 
3761 	if (pool == NULL)
3762 		return;
3763 
3764 #ifdef IO_POOLS
3765 	uma_zdestroy(pool->zone);
3766 	free(pool, M_CTL);
3767 #endif
3768 }
3769 
3770 union ctl_io *
3771 ctl_alloc_io(void *pool_ref)
3772 {
3773 	union ctl_io *io;
3774 #ifdef IO_POOLS
3775 	struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3776 
3777 	io = uma_zalloc(pool->zone, M_WAITOK);
3778 #else
3779 	io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3780 #endif
3781 	if (io != NULL)
3782 		io->io_hdr.pool = pool_ref;
3783 	return (io);
3784 }
3785 
3786 union ctl_io *
3787 ctl_alloc_io_nowait(void *pool_ref)
3788 {
3789 	union ctl_io *io;
3790 #ifdef IO_POOLS
3791 	struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3792 
3793 	io = uma_zalloc(pool->zone, M_NOWAIT);
3794 #else
3795 	io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3796 #endif
3797 	if (io != NULL)
3798 		io->io_hdr.pool = pool_ref;
3799 	return (io);
3800 }
3801 
3802 void
3803 ctl_free_io(union ctl_io *io)
3804 {
3805 #ifdef IO_POOLS
3806 	struct ctl_io_pool *pool;
3807 #endif
3808 
3809 	if (io == NULL)
3810 		return;
3811 
3812 #ifdef IO_POOLS
3813 	pool = (struct ctl_io_pool *)io->io_hdr.pool;
3814 	uma_zfree(pool->zone, io);
3815 #else
3816 	uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3817 #endif
3818 }
3819 
3820 void
3821 ctl_zero_io(union ctl_io *io)
3822 {
3823 	void *pool_ref;
3824 
3825 	if (io == NULL)
3826 		return;
3827 
3828 	/*
3829 	 * May need to preserve linked list pointers at some point too.
3830 	 */
3831 	pool_ref = io->io_hdr.pool;
3832 	memset(io, 0, sizeof(*io));
3833 	io->io_hdr.pool = pool_ref;
3834 }
3835 
3836 int
3837 ctl_expand_number(const char *buf, uint64_t *num)
3838 {
3839 	char *endptr;
3840 	uint64_t number;
3841 	unsigned shift;
3842 
3843 	number = strtoq(buf, &endptr, 0);
3844 
3845 	switch (tolower((unsigned char)*endptr)) {
3846 	case 'e':
3847 		shift = 60;
3848 		break;
3849 	case 'p':
3850 		shift = 50;
3851 		break;
3852 	case 't':
3853 		shift = 40;
3854 		break;
3855 	case 'g':
3856 		shift = 30;
3857 		break;
3858 	case 'm':
3859 		shift = 20;
3860 		break;
3861 	case 'k':
3862 		shift = 10;
3863 		break;
3864 	case 'b':
3865 	case '\0': /* No unit. */
3866 		*num = number;
3867 		return (0);
3868 	default:
3869 		/* Unrecognized unit. */
3870 		return (-1);
3871 	}
3872 
3873 	if ((number << shift) >> shift != number) {
3874 		/* Overflow */
3875 		return (-1);
3876 	}
3877 	*num = number << shift;
3878 	return (0);
3879 }
3880 
3881 
3882 /*
3883  * This routine could be used in the future to load default and/or saved
3884  * mode page parameters for a particuar lun.
3885  */
3886 static int
3887 ctl_init_page_index(struct ctl_lun *lun)
3888 {
3889 	int i, page_code;
3890 	struct ctl_page_index *page_index;
3891 	const char *value;
3892 	uint64_t ival;
3893 
3894 	memcpy(&lun->mode_pages.index, page_index_template,
3895 	       sizeof(page_index_template));
3896 
3897 	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3898 
3899 		page_index = &lun->mode_pages.index[i];
3900 		if (lun->be_lun->lun_type == T_DIRECT &&
3901 		    (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3902 			continue;
3903 		if (lun->be_lun->lun_type == T_PROCESSOR &&
3904 		    (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3905 			continue;
3906 		if (lun->be_lun->lun_type == T_CDROM &&
3907 		    (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3908 			continue;
3909 
3910 		page_code = page_index->page_code & SMPH_PC_MASK;
3911 		switch (page_code) {
3912 		case SMS_RW_ERROR_RECOVERY_PAGE: {
3913 			KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3914 			    ("subpage %#x for page %#x is incorrect!",
3915 			    page_index->subpage, page_code));
3916 			memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3917 			       &rw_er_page_default,
3918 			       sizeof(rw_er_page_default));
3919 			memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3920 			       &rw_er_page_changeable,
3921 			       sizeof(rw_er_page_changeable));
3922 			memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3923 			       &rw_er_page_default,
3924 			       sizeof(rw_er_page_default));
3925 			memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3926 			       &rw_er_page_default,
3927 			       sizeof(rw_er_page_default));
3928 			page_index->page_data =
3929 				(uint8_t *)lun->mode_pages.rw_er_page;
3930 			break;
3931 		}
3932 		case SMS_FORMAT_DEVICE_PAGE: {
3933 			struct scsi_format_page *format_page;
3934 
3935 			KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3936 			    ("subpage %#x for page %#x is incorrect!",
3937 			    page_index->subpage, page_code));
3938 
3939 			/*
3940 			 * Sectors per track are set above.  Bytes per
3941 			 * sector need to be set here on a per-LUN basis.
3942 			 */
3943 			memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3944 			       &format_page_default,
3945 			       sizeof(format_page_default));
3946 			memcpy(&lun->mode_pages.format_page[
3947 			       CTL_PAGE_CHANGEABLE], &format_page_changeable,
3948 			       sizeof(format_page_changeable));
3949 			memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3950 			       &format_page_default,
3951 			       sizeof(format_page_default));
3952 			memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3953 			       &format_page_default,
3954 			       sizeof(format_page_default));
3955 
3956 			format_page = &lun->mode_pages.format_page[
3957 				CTL_PAGE_CURRENT];
3958 			scsi_ulto2b(lun->be_lun->blocksize,
3959 				    format_page->bytes_per_sector);
3960 
3961 			format_page = &lun->mode_pages.format_page[
3962 				CTL_PAGE_DEFAULT];
3963 			scsi_ulto2b(lun->be_lun->blocksize,
3964 				    format_page->bytes_per_sector);
3965 
3966 			format_page = &lun->mode_pages.format_page[
3967 				CTL_PAGE_SAVED];
3968 			scsi_ulto2b(lun->be_lun->blocksize,
3969 				    format_page->bytes_per_sector);
3970 
3971 			page_index->page_data =
3972 				(uint8_t *)lun->mode_pages.format_page;
3973 			break;
3974 		}
3975 		case SMS_RIGID_DISK_PAGE: {
3976 			struct scsi_rigid_disk_page *rigid_disk_page;
3977 			uint32_t sectors_per_cylinder;
3978 			uint64_t cylinders;
3979 #ifndef	__XSCALE__
3980 			int shift;
3981 #endif /* !__XSCALE__ */
3982 
3983 			KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3984 			    ("subpage %#x for page %#x is incorrect!",
3985 			    page_index->subpage, page_code));
3986 
3987 			/*
3988 			 * Rotation rate and sectors per track are set
3989 			 * above.  We calculate the cylinders here based on
3990 			 * capacity.  Due to the number of heads and
3991 			 * sectors per track we're using, smaller arrays
3992 			 * may turn out to have 0 cylinders.  Linux and
3993 			 * FreeBSD don't pay attention to these mode pages
3994 			 * to figure out capacity, but Solaris does.  It
3995 			 * seems to deal with 0 cylinders just fine, and
3996 			 * works out a fake geometry based on the capacity.
3997 			 */
3998 			memcpy(&lun->mode_pages.rigid_disk_page[
3999 			       CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4000 			       sizeof(rigid_disk_page_default));
4001 			memcpy(&lun->mode_pages.rigid_disk_page[
4002 			       CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4003 			       sizeof(rigid_disk_page_changeable));
4004 
4005 			sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4006 				CTL_DEFAULT_HEADS;
4007 
4008 			/*
4009 			 * The divide method here will be more accurate,
4010 			 * probably, but results in floating point being
4011 			 * used in the kernel on i386 (__udivdi3()).  On the
4012 			 * XScale, though, __udivdi3() is implemented in
4013 			 * software.
4014 			 *
4015 			 * The shift method for cylinder calculation is
4016 			 * accurate if sectors_per_cylinder is a power of
4017 			 * 2.  Otherwise it might be slightly off -- you
4018 			 * might have a bit of a truncation problem.
4019 			 */
4020 #ifdef	__XSCALE__
4021 			cylinders = (lun->be_lun->maxlba + 1) /
4022 				sectors_per_cylinder;
4023 #else
4024 			for (shift = 31; shift > 0; shift--) {
4025 				if (sectors_per_cylinder & (1 << shift))
4026 					break;
4027 			}
4028 			cylinders = (lun->be_lun->maxlba + 1) >> shift;
4029 #endif
4030 
4031 			/*
4032 			 * We've basically got 3 bytes, or 24 bits for the
4033 			 * cylinder size in the mode page.  If we're over,
4034 			 * just round down to 2^24.
4035 			 */
4036 			if (cylinders > 0xffffff)
4037 				cylinders = 0xffffff;
4038 
4039 			rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4040 				CTL_PAGE_DEFAULT];
4041 			scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4042 
4043 			if ((value = ctl_get_opt(&lun->be_lun->options,
4044 			    "rpm")) != NULL) {
4045 				scsi_ulto2b(strtol(value, NULL, 0),
4046 				     rigid_disk_page->rotation_rate);
4047 			}
4048 
4049 			memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4050 			       &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4051 			       sizeof(rigid_disk_page_default));
4052 			memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4053 			       &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4054 			       sizeof(rigid_disk_page_default));
4055 
4056 			page_index->page_data =
4057 				(uint8_t *)lun->mode_pages.rigid_disk_page;
4058 			break;
4059 		}
4060 		case SMS_CACHING_PAGE: {
4061 			struct scsi_caching_page *caching_page;
4062 
4063 			KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4064 			    ("subpage %#x for page %#x is incorrect!",
4065 			    page_index->subpage, page_code));
4066 			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4067 			       &caching_page_default,
4068 			       sizeof(caching_page_default));
4069 			memcpy(&lun->mode_pages.caching_page[
4070 			       CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4071 			       sizeof(caching_page_changeable));
4072 			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4073 			       &caching_page_default,
4074 			       sizeof(caching_page_default));
4075 			caching_page = &lun->mode_pages.caching_page[
4076 			    CTL_PAGE_SAVED];
4077 			value = ctl_get_opt(&lun->be_lun->options, "writecache");
4078 			if (value != NULL && strcmp(value, "off") == 0)
4079 				caching_page->flags1 &= ~SCP_WCE;
4080 			value = ctl_get_opt(&lun->be_lun->options, "readcache");
4081 			if (value != NULL && strcmp(value, "off") == 0)
4082 				caching_page->flags1 |= SCP_RCD;
4083 			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4084 			       &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4085 			       sizeof(caching_page_default));
4086 			page_index->page_data =
4087 				(uint8_t *)lun->mode_pages.caching_page;
4088 			break;
4089 		}
4090 		case SMS_CONTROL_MODE_PAGE: {
4091 			switch (page_index->subpage) {
4092 			case SMS_SUBPAGE_PAGE_0: {
4093 				struct scsi_control_page *control_page;
4094 
4095 				memcpy(&lun->mode_pages.control_page[
4096 				    CTL_PAGE_DEFAULT],
4097 				       &control_page_default,
4098 				       sizeof(control_page_default));
4099 				memcpy(&lun->mode_pages.control_page[
4100 				    CTL_PAGE_CHANGEABLE],
4101 				       &control_page_changeable,
4102 				       sizeof(control_page_changeable));
4103 				memcpy(&lun->mode_pages.control_page[
4104 				    CTL_PAGE_SAVED],
4105 				       &control_page_default,
4106 				       sizeof(control_page_default));
4107 				control_page = &lun->mode_pages.control_page[
4108 				    CTL_PAGE_SAVED];
4109 				value = ctl_get_opt(&lun->be_lun->options,
4110 				    "reordering");
4111 				if (value != NULL &&
4112 				    strcmp(value, "unrestricted") == 0) {
4113 					control_page->queue_flags &=
4114 					    ~SCP_QUEUE_ALG_MASK;
4115 					control_page->queue_flags |=
4116 					    SCP_QUEUE_ALG_UNRESTRICTED;
4117 				}
4118 				memcpy(&lun->mode_pages.control_page[
4119 				    CTL_PAGE_CURRENT],
4120 				       &lun->mode_pages.control_page[
4121 				    CTL_PAGE_SAVED],
4122 				       sizeof(control_page_default));
4123 				page_index->page_data =
4124 				    (uint8_t *)lun->mode_pages.control_page;
4125 				break;
4126 			}
4127 			case 0x01:
4128 				memcpy(&lun->mode_pages.control_ext_page[
4129 				    CTL_PAGE_DEFAULT],
4130 				       &control_ext_page_default,
4131 				       sizeof(control_ext_page_default));
4132 				memcpy(&lun->mode_pages.control_ext_page[
4133 				    CTL_PAGE_CHANGEABLE],
4134 				       &control_ext_page_changeable,
4135 				       sizeof(control_ext_page_changeable));
4136 				memcpy(&lun->mode_pages.control_ext_page[
4137 				    CTL_PAGE_SAVED],
4138 				       &control_ext_page_default,
4139 				       sizeof(control_ext_page_default));
4140 				memcpy(&lun->mode_pages.control_ext_page[
4141 				    CTL_PAGE_CURRENT],
4142 				       &lun->mode_pages.control_ext_page[
4143 				    CTL_PAGE_SAVED],
4144 				       sizeof(control_ext_page_default));
4145 				page_index->page_data =
4146 				    (uint8_t *)lun->mode_pages.control_ext_page;
4147 				break;
4148 			default:
4149 				panic("subpage %#x for page %#x is incorrect!",
4150 				      page_index->subpage, page_code);
4151 			}
4152 			break;
4153 		}
4154 		case SMS_INFO_EXCEPTIONS_PAGE: {
4155 			switch (page_index->subpage) {
4156 			case SMS_SUBPAGE_PAGE_0:
4157 				memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4158 				       &ie_page_default,
4159 				       sizeof(ie_page_default));
4160 				memcpy(&lun->mode_pages.ie_page[
4161 				       CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4162 				       sizeof(ie_page_changeable));
4163 				memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4164 				       &ie_page_default,
4165 				       sizeof(ie_page_default));
4166 				memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4167 				       &ie_page_default,
4168 				       sizeof(ie_page_default));
4169 				page_index->page_data =
4170 					(uint8_t *)lun->mode_pages.ie_page;
4171 				break;
4172 			case 0x02: {
4173 				struct ctl_logical_block_provisioning_page *page;
4174 
4175 				memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4176 				       &lbp_page_default,
4177 				       sizeof(lbp_page_default));
4178 				memcpy(&lun->mode_pages.lbp_page[
4179 				       CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4180 				       sizeof(lbp_page_changeable));
4181 				memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4182 				       &lbp_page_default,
4183 				       sizeof(lbp_page_default));
4184 				page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4185 				value = ctl_get_opt(&lun->be_lun->options,
4186 				    "avail-threshold");
4187 				if (value != NULL &&
4188 				    ctl_expand_number(value, &ival) == 0) {
4189 					page->descr[0].flags |= SLBPPD_ENABLED |
4190 					    SLBPPD_ARMING_DEC;
4191 					if (lun->be_lun->blocksize)
4192 						ival /= lun->be_lun->blocksize;
4193 					else
4194 						ival /= 512;
4195 					scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4196 					    page->descr[0].count);
4197 				}
4198 				value = ctl_get_opt(&lun->be_lun->options,
4199 				    "used-threshold");
4200 				if (value != NULL &&
4201 				    ctl_expand_number(value, &ival) == 0) {
4202 					page->descr[1].flags |= SLBPPD_ENABLED |
4203 					    SLBPPD_ARMING_INC;
4204 					if (lun->be_lun->blocksize)
4205 						ival /= lun->be_lun->blocksize;
4206 					else
4207 						ival /= 512;
4208 					scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4209 					    page->descr[1].count);
4210 				}
4211 				value = ctl_get_opt(&lun->be_lun->options,
4212 				    "pool-avail-threshold");
4213 				if (value != NULL &&
4214 				    ctl_expand_number(value, &ival) == 0) {
4215 					page->descr[2].flags |= SLBPPD_ENABLED |
4216 					    SLBPPD_ARMING_DEC;
4217 					if (lun->be_lun->blocksize)
4218 						ival /= lun->be_lun->blocksize;
4219 					else
4220 						ival /= 512;
4221 					scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4222 					    page->descr[2].count);
4223 				}
4224 				value = ctl_get_opt(&lun->be_lun->options,
4225 				    "pool-used-threshold");
4226 				if (value != NULL &&
4227 				    ctl_expand_number(value, &ival) == 0) {
4228 					page->descr[3].flags |= SLBPPD_ENABLED |
4229 					    SLBPPD_ARMING_INC;
4230 					if (lun->be_lun->blocksize)
4231 						ival /= lun->be_lun->blocksize;
4232 					else
4233 						ival /= 512;
4234 					scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4235 					    page->descr[3].count);
4236 				}
4237 				memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4238 				       &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4239 				       sizeof(lbp_page_default));
4240 				page_index->page_data =
4241 					(uint8_t *)lun->mode_pages.lbp_page;
4242 				break;
4243 			}
4244 			default:
4245 				panic("subpage %#x for page %#x is incorrect!",
4246 				      page_index->subpage, page_code);
4247 			}
4248 			break;
4249 		}
4250 		case SMS_CDDVD_CAPS_PAGE:{
4251 			KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4252 			    ("subpage %#x for page %#x is incorrect!",
4253 			    page_index->subpage, page_code));
4254 			memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4255 			       &cddvd_page_default,
4256 			       sizeof(cddvd_page_default));
4257 			memcpy(&lun->mode_pages.cddvd_page[
4258 			       CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4259 			       sizeof(cddvd_page_changeable));
4260 			memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4261 			       &cddvd_page_default,
4262 			       sizeof(cddvd_page_default));
4263 			memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4264 			       &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4265 			       sizeof(cddvd_page_default));
4266 			page_index->page_data =
4267 				(uint8_t *)lun->mode_pages.cddvd_page;
4268 			break;
4269 		}
4270 		case SMS_VENDOR_SPECIFIC_PAGE:{
4271 			switch (page_index->subpage) {
4272 			case DBGCNF_SUBPAGE_CODE: {
4273 				memcpy(&lun->mode_pages.debugconf_subpage[
4274 				       CTL_PAGE_CURRENT],
4275 				       &debugconf_page_default,
4276 				       sizeof(debugconf_page_default));
4277 				memcpy(&lun->mode_pages.debugconf_subpage[
4278 				       CTL_PAGE_CHANGEABLE],
4279 				       &debugconf_page_changeable,
4280 				       sizeof(debugconf_page_changeable));
4281 				memcpy(&lun->mode_pages.debugconf_subpage[
4282 				       CTL_PAGE_DEFAULT],
4283 				       &debugconf_page_default,
4284 				       sizeof(debugconf_page_default));
4285 				memcpy(&lun->mode_pages.debugconf_subpage[
4286 				       CTL_PAGE_SAVED],
4287 				       &debugconf_page_default,
4288 				       sizeof(debugconf_page_default));
4289 				page_index->page_data =
4290 				    (uint8_t *)lun->mode_pages.debugconf_subpage;
4291 				break;
4292 			}
4293 			default:
4294 				panic("subpage %#x for page %#x is incorrect!",
4295 				      page_index->subpage, page_code);
4296 			}
4297 			break;
4298 		}
4299 		default:
4300 			panic("invalid page code value %#x", page_code);
4301 		}
4302 	}
4303 
4304 	return (CTL_RETVAL_COMPLETE);
4305 }
4306 
4307 static int
4308 ctl_init_log_page_index(struct ctl_lun *lun)
4309 {
4310 	struct ctl_page_index *page_index;
4311 	int i, j, k, prev;
4312 
4313 	memcpy(&lun->log_pages.index, log_page_index_template,
4314 	       sizeof(log_page_index_template));
4315 
4316 	prev = -1;
4317 	for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4318 
4319 		page_index = &lun->log_pages.index[i];
4320 		if (lun->be_lun->lun_type == T_DIRECT &&
4321 		    (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4322 			continue;
4323 		if (lun->be_lun->lun_type == T_PROCESSOR &&
4324 		    (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4325 			continue;
4326 		if (lun->be_lun->lun_type == T_CDROM &&
4327 		    (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4328 			continue;
4329 
4330 		if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4331 		    lun->backend->lun_attr == NULL)
4332 			continue;
4333 
4334 		if (page_index->page_code != prev) {
4335 			lun->log_pages.pages_page[j] = page_index->page_code;
4336 			prev = page_index->page_code;
4337 			j++;
4338 		}
4339 		lun->log_pages.subpages_page[k*2] = page_index->page_code;
4340 		lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4341 		k++;
4342 	}
4343 	lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4344 	lun->log_pages.index[0].page_len = j;
4345 	lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4346 	lun->log_pages.index[1].page_len = k * 2;
4347 	lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4348 	lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4349 	lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4350 	lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4351 
4352 	return (CTL_RETVAL_COMPLETE);
4353 }
4354 
4355 static int
4356 hex2bin(const char *str, uint8_t *buf, int buf_size)
4357 {
4358 	int i;
4359 	u_char c;
4360 
4361 	memset(buf, 0, buf_size);
4362 	while (isspace(str[0]))
4363 		str++;
4364 	if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4365 		str += 2;
4366 	buf_size *= 2;
4367 	for (i = 0; str[i] != 0 && i < buf_size; i++) {
4368 		c = str[i];
4369 		if (isdigit(c))
4370 			c -= '0';
4371 		else if (isalpha(c))
4372 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4373 		else
4374 			break;
4375 		if (c >= 16)
4376 			break;
4377 		if ((i & 1) == 0)
4378 			buf[i / 2] |= (c << 4);
4379 		else
4380 			buf[i / 2] |= c;
4381 	}
4382 	return ((i + 1) / 2);
4383 }
4384 
4385 /*
4386  * LUN allocation.
4387  *
4388  * Requirements:
4389  * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4390  *   wants us to allocate the LUN and he can block.
4391  * - ctl_softc is always set
4392  * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4393  *
4394  * Returns 0 for success, non-zero (errno) for failure.
4395  */
4396 static int
4397 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4398 	      struct ctl_be_lun *const be_lun)
4399 {
4400 	struct ctl_lun *nlun, *lun;
4401 	struct scsi_vpd_id_descriptor *desc;
4402 	struct scsi_vpd_id_t10 *t10id;
4403 	const char *eui, *naa, *scsiname, *vendor, *value;
4404 	int lun_number, i, lun_malloced;
4405 	int devidlen, idlen1, idlen2 = 0, len;
4406 
4407 	if (be_lun == NULL)
4408 		return (EINVAL);
4409 
4410 	/*
4411 	 * We currently only support Direct Access or Processor LUN types.
4412 	 */
4413 	switch (be_lun->lun_type) {
4414 	case T_DIRECT:
4415 	case T_PROCESSOR:
4416 	case T_CDROM:
4417 		break;
4418 	case T_SEQUENTIAL:
4419 	case T_CHANGER:
4420 	default:
4421 		be_lun->lun_config_status(be_lun->be_lun,
4422 					  CTL_LUN_CONFIG_FAILURE);
4423 		break;
4424 	}
4425 	if (ctl_lun == NULL) {
4426 		lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4427 		lun_malloced = 1;
4428 	} else {
4429 		lun_malloced = 0;
4430 		lun = ctl_lun;
4431 	}
4432 
4433 	memset(lun, 0, sizeof(*lun));
4434 	if (lun_malloced)
4435 		lun->flags = CTL_LUN_MALLOCED;
4436 
4437 	/* Generate LUN ID. */
4438 	devidlen = max(CTL_DEVID_MIN_LEN,
4439 	    strnlen(be_lun->device_id, CTL_DEVID_LEN));
4440 	idlen1 = sizeof(*t10id) + devidlen;
4441 	len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4442 	scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4443 	if (scsiname != NULL) {
4444 		idlen2 = roundup2(strlen(scsiname) + 1, 4);
4445 		len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4446 	}
4447 	eui = ctl_get_opt(&be_lun->options, "eui");
4448 	if (eui != NULL) {
4449 		len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4450 	}
4451 	naa = ctl_get_opt(&be_lun->options, "naa");
4452 	if (naa != NULL) {
4453 		len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4454 	}
4455 	lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4456 	    M_CTL, M_WAITOK | M_ZERO);
4457 	desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4458 	desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4459 	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4460 	desc->length = idlen1;
4461 	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4462 	memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4463 	if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4464 		strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4465 	} else {
4466 		strncpy(t10id->vendor, vendor,
4467 		    min(sizeof(t10id->vendor), strlen(vendor)));
4468 	}
4469 	strncpy((char *)t10id->vendor_spec_id,
4470 	    (char *)be_lun->device_id, devidlen);
4471 	if (scsiname != NULL) {
4472 		desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4473 		    desc->length);
4474 		desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4475 		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4476 		    SVPD_ID_TYPE_SCSI_NAME;
4477 		desc->length = idlen2;
4478 		strlcpy(desc->identifier, scsiname, idlen2);
4479 	}
4480 	if (eui != NULL) {
4481 		desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4482 		    desc->length);
4483 		desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4484 		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4485 		    SVPD_ID_TYPE_EUI64;
4486 		desc->length = hex2bin(eui, desc->identifier, 16);
4487 		desc->length = desc->length > 12 ? 16 :
4488 		    (desc->length > 8 ? 12 : 8);
4489 		len -= 16 - desc->length;
4490 	}
4491 	if (naa != NULL) {
4492 		desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4493 		    desc->length);
4494 		desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4495 		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4496 		    SVPD_ID_TYPE_NAA;
4497 		desc->length = hex2bin(naa, desc->identifier, 16);
4498 		desc->length = desc->length > 8 ? 16 : 8;
4499 		len -= 16 - desc->length;
4500 	}
4501 	lun->lun_devid->len = len;
4502 
4503 	mtx_lock(&ctl_softc->ctl_lock);
4504 	/*
4505 	 * See if the caller requested a particular LUN number.  If so, see
4506 	 * if it is available.  Otherwise, allocate the first available LUN.
4507 	 */
4508 	if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4509 		if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4510 		 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4511 			mtx_unlock(&ctl_softc->ctl_lock);
4512 			if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4513 				printf("ctl: requested LUN ID %d is higher "
4514 				       "than CTL_MAX_LUNS - 1 (%d)\n",
4515 				       be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4516 			} else {
4517 				/*
4518 				 * XXX KDM return an error, or just assign
4519 				 * another LUN ID in this case??
4520 				 */
4521 				printf("ctl: requested LUN ID %d is already "
4522 				       "in use\n", be_lun->req_lun_id);
4523 			}
4524 			if (lun->flags & CTL_LUN_MALLOCED)
4525 				free(lun, M_CTL);
4526 			be_lun->lun_config_status(be_lun->be_lun,
4527 						  CTL_LUN_CONFIG_FAILURE);
4528 			return (ENOSPC);
4529 		}
4530 		lun_number = be_lun->req_lun_id;
4531 	} else {
4532 		lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4533 		if (lun_number == -1) {
4534 			mtx_unlock(&ctl_softc->ctl_lock);
4535 			printf("ctl: can't allocate LUN, out of LUNs\n");
4536 			if (lun->flags & CTL_LUN_MALLOCED)
4537 				free(lun, M_CTL);
4538 			be_lun->lun_config_status(be_lun->be_lun,
4539 						  CTL_LUN_CONFIG_FAILURE);
4540 			return (ENOSPC);
4541 		}
4542 	}
4543 	ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4544 
4545 	mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4546 	lun->lun = lun_number;
4547 	lun->be_lun = be_lun;
4548 	/*
4549 	 * The processor LUN is always enabled.  Disk LUNs come on line
4550 	 * disabled, and must be enabled by the backend.
4551 	 */
4552 	lun->flags |= CTL_LUN_DISABLED;
4553 	lun->backend = be_lun->be;
4554 	be_lun->ctl_lun = lun;
4555 	be_lun->lun_id = lun_number;
4556 	atomic_add_int(&be_lun->be->num_luns, 1);
4557 	if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4558 		lun->flags |= CTL_LUN_EJECTED;
4559 	if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4560 		lun->flags |= CTL_LUN_NO_MEDIA;
4561 	if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4562 		lun->flags |= CTL_LUN_STOPPED;
4563 
4564 	if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4565 		lun->flags |= CTL_LUN_PRIMARY_SC;
4566 
4567 	value = ctl_get_opt(&be_lun->options, "removable");
4568 	if (value != NULL) {
4569 		if (strcmp(value, "on") == 0)
4570 			lun->flags |= CTL_LUN_REMOVABLE;
4571 	} else if (be_lun->lun_type == T_CDROM)
4572 		lun->flags |= CTL_LUN_REMOVABLE;
4573 
4574 	lun->ctl_softc = ctl_softc;
4575 #ifdef CTL_TIME_IO
4576 	lun->last_busy = getsbinuptime();
4577 #endif
4578 	TAILQ_INIT(&lun->ooa_queue);
4579 	TAILQ_INIT(&lun->blocked_queue);
4580 	STAILQ_INIT(&lun->error_list);
4581 	ctl_tpc_lun_init(lun);
4582 
4583 	/*
4584 	 * Initialize the mode and log page index.
4585 	 */
4586 	ctl_init_page_index(lun);
4587 	ctl_init_log_page_index(lun);
4588 
4589 	/*
4590 	 * Now, before we insert this lun on the lun list, set the lun
4591 	 * inventory changed UA for all other luns.
4592 	 */
4593 	STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4594 		mtx_lock(&nlun->lun_lock);
4595 		ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4596 		mtx_unlock(&nlun->lun_lock);
4597 	}
4598 
4599 	STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4600 
4601 	ctl_softc->ctl_luns[lun_number] = lun;
4602 
4603 	ctl_softc->num_luns++;
4604 
4605 	/* Setup statistics gathering */
4606 	lun->stats.device_type = be_lun->lun_type;
4607 	lun->stats.lun_number = lun_number;
4608 	lun->stats.blocksize = be_lun->blocksize;
4609 	if (be_lun->blocksize == 0)
4610 		lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4611 	for (i = 0;i < CTL_MAX_PORTS;i++)
4612 		lun->stats.ports[i].targ_port = i;
4613 
4614 	mtx_unlock(&ctl_softc->ctl_lock);
4615 
4616 	lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4617 	return (0);
4618 }
4619 
4620 /*
4621  * Delete a LUN.
4622  * Assumptions:
4623  * - LUN has already been marked invalid and any pending I/O has been taken
4624  *   care of.
4625  */
4626 static int
4627 ctl_free_lun(struct ctl_lun *lun)
4628 {
4629 	struct ctl_softc *softc;
4630 	struct ctl_lun *nlun;
4631 	int i;
4632 
4633 	softc = lun->ctl_softc;
4634 
4635 	mtx_assert(&softc->ctl_lock, MA_OWNED);
4636 
4637 	STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4638 
4639 	ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4640 
4641 	softc->ctl_luns[lun->lun] = NULL;
4642 
4643 	if (!TAILQ_EMPTY(&lun->ooa_queue))
4644 		panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4645 
4646 	softc->num_luns--;
4647 
4648 	/*
4649 	 * Tell the backend to free resources, if this LUN has a backend.
4650 	 */
4651 	atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4652 	lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4653 
4654 	ctl_tpc_lun_shutdown(lun);
4655 	mtx_destroy(&lun->lun_lock);
4656 	free(lun->lun_devid, M_CTL);
4657 	for (i = 0; i < CTL_MAX_PORTS; i++)
4658 		free(lun->pending_ua[i], M_CTL);
4659 	for (i = 0; i < CTL_MAX_PORTS; i++)
4660 		free(lun->pr_keys[i], M_CTL);
4661 	free(lun->write_buffer, M_CTL);
4662 	if (lun->flags & CTL_LUN_MALLOCED)
4663 		free(lun, M_CTL);
4664 
4665 	STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4666 		mtx_lock(&nlun->lun_lock);
4667 		ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4668 		mtx_unlock(&nlun->lun_lock);
4669 	}
4670 
4671 	return (0);
4672 }
4673 
4674 static void
4675 ctl_create_lun(struct ctl_be_lun *be_lun)
4676 {
4677 
4678 	/*
4679 	 * ctl_alloc_lun() should handle all potential failure cases.
4680 	 */
4681 	ctl_alloc_lun(control_softc, NULL, be_lun);
4682 }
4683 
4684 int
4685 ctl_add_lun(struct ctl_be_lun *be_lun)
4686 {
4687 	struct ctl_softc *softc = control_softc;
4688 
4689 	mtx_lock(&softc->ctl_lock);
4690 	STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4691 	mtx_unlock(&softc->ctl_lock);
4692 	wakeup(&softc->pending_lun_queue);
4693 
4694 	return (0);
4695 }
4696 
4697 int
4698 ctl_enable_lun(struct ctl_be_lun *be_lun)
4699 {
4700 	struct ctl_softc *softc;
4701 	struct ctl_port *port, *nport;
4702 	struct ctl_lun *lun;
4703 	int retval;
4704 
4705 	lun = (struct ctl_lun *)be_lun->ctl_lun;
4706 	softc = lun->ctl_softc;
4707 
4708 	mtx_lock(&softc->ctl_lock);
4709 	mtx_lock(&lun->lun_lock);
4710 	if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4711 		/*
4712 		 * eh?  Why did we get called if the LUN is already
4713 		 * enabled?
4714 		 */
4715 		mtx_unlock(&lun->lun_lock);
4716 		mtx_unlock(&softc->ctl_lock);
4717 		return (0);
4718 	}
4719 	lun->flags &= ~CTL_LUN_DISABLED;
4720 	mtx_unlock(&lun->lun_lock);
4721 
4722 	STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4723 		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4724 		    port->lun_map != NULL || port->lun_enable == NULL)
4725 			continue;
4726 
4727 		/*
4728 		 * Drop the lock while we call the FETD's enable routine.
4729 		 * This can lead to a callback into CTL (at least in the
4730 		 * case of the internal initiator frontend.
4731 		 */
4732 		mtx_unlock(&softc->ctl_lock);
4733 		retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4734 		mtx_lock(&softc->ctl_lock);
4735 		if (retval != 0) {
4736 			printf("%s: FETD %s port %d returned error "
4737 			       "%d for lun_enable on lun %jd\n",
4738 			       __func__, port->port_name, port->targ_port,
4739 			       retval, (intmax_t)lun->lun);
4740 		}
4741 	}
4742 
4743 	mtx_unlock(&softc->ctl_lock);
4744 	ctl_isc_announce_lun(lun);
4745 
4746 	return (0);
4747 }
4748 
4749 int
4750 ctl_disable_lun(struct ctl_be_lun *be_lun)
4751 {
4752 	struct ctl_softc *softc;
4753 	struct ctl_port *port;
4754 	struct ctl_lun *lun;
4755 	int retval;
4756 
4757 	lun = (struct ctl_lun *)be_lun->ctl_lun;
4758 	softc = lun->ctl_softc;
4759 
4760 	mtx_lock(&softc->ctl_lock);
4761 	mtx_lock(&lun->lun_lock);
4762 	if (lun->flags & CTL_LUN_DISABLED) {
4763 		mtx_unlock(&lun->lun_lock);
4764 		mtx_unlock(&softc->ctl_lock);
4765 		return (0);
4766 	}
4767 	lun->flags |= CTL_LUN_DISABLED;
4768 	mtx_unlock(&lun->lun_lock);
4769 
4770 	STAILQ_FOREACH(port, &softc->port_list, links) {
4771 		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4772 		    port->lun_map != NULL || port->lun_disable == NULL)
4773 			continue;
4774 
4775 		/*
4776 		 * Drop the lock before we call the frontend's disable
4777 		 * routine, to avoid lock order reversals.
4778 		 *
4779 		 * XXX KDM what happens if the frontend list changes while
4780 		 * we're traversing it?  It's unlikely, but should be handled.
4781 		 */
4782 		mtx_unlock(&softc->ctl_lock);
4783 		retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4784 		mtx_lock(&softc->ctl_lock);
4785 		if (retval != 0) {
4786 			printf("%s: FETD %s port %d returned error "
4787 			       "%d for lun_disable on lun %jd\n",
4788 			       __func__, port->port_name, port->targ_port,
4789 			       retval, (intmax_t)lun->lun);
4790 		}
4791 	}
4792 
4793 	mtx_unlock(&softc->ctl_lock);
4794 	ctl_isc_announce_lun(lun);
4795 
4796 	return (0);
4797 }
4798 
4799 int
4800 ctl_start_lun(struct ctl_be_lun *be_lun)
4801 {
4802 	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4803 
4804 	mtx_lock(&lun->lun_lock);
4805 	lun->flags &= ~CTL_LUN_STOPPED;
4806 	mtx_unlock(&lun->lun_lock);
4807 	return (0);
4808 }
4809 
4810 int
4811 ctl_stop_lun(struct ctl_be_lun *be_lun)
4812 {
4813 	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4814 
4815 	mtx_lock(&lun->lun_lock);
4816 	lun->flags |= CTL_LUN_STOPPED;
4817 	mtx_unlock(&lun->lun_lock);
4818 	return (0);
4819 }
4820 
4821 int
4822 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4823 {
4824 	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4825 
4826 	mtx_lock(&lun->lun_lock);
4827 	lun->flags |= CTL_LUN_NO_MEDIA;
4828 	mtx_unlock(&lun->lun_lock);
4829 	return (0);
4830 }
4831 
4832 int
4833 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4834 {
4835 	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4836 	union ctl_ha_msg msg;
4837 
4838 	mtx_lock(&lun->lun_lock);
4839 	lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4840 	if (lun->flags & CTL_LUN_REMOVABLE)
4841 		ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4842 	mtx_unlock(&lun->lun_lock);
4843 	if ((lun->flags & CTL_LUN_REMOVABLE) &&
4844 	    lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4845 		bzero(&msg.ua, sizeof(msg.ua));
4846 		msg.hdr.msg_type = CTL_MSG_UA;
4847 		msg.hdr.nexus.initid = -1;
4848 		msg.hdr.nexus.targ_port = -1;
4849 		msg.hdr.nexus.targ_lun = lun->lun;
4850 		msg.hdr.nexus.targ_mapped_lun = lun->lun;
4851 		msg.ua.ua_all = 1;
4852 		msg.ua.ua_set = 1;
4853 		msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4854 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4855 		    M_WAITOK);
4856 	}
4857 	return (0);
4858 }
4859 
4860 int
4861 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4862 {
4863 	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4864 
4865 	mtx_lock(&lun->lun_lock);
4866 	lun->flags |= CTL_LUN_EJECTED;
4867 	mtx_unlock(&lun->lun_lock);
4868 	return (0);
4869 }
4870 
4871 int
4872 ctl_lun_primary(struct ctl_be_lun *be_lun)
4873 {
4874 	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4875 
4876 	mtx_lock(&lun->lun_lock);
4877 	lun->flags |= CTL_LUN_PRIMARY_SC;
4878 	ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4879 	mtx_unlock(&lun->lun_lock);
4880 	ctl_isc_announce_lun(lun);
4881 	return (0);
4882 }
4883 
4884 int
4885 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4886 {
4887 	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4888 
4889 	mtx_lock(&lun->lun_lock);
4890 	lun->flags &= ~CTL_LUN_PRIMARY_SC;
4891 	ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4892 	mtx_unlock(&lun->lun_lock);
4893 	ctl_isc_announce_lun(lun);
4894 	return (0);
4895 }
4896 
4897 int
4898 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4899 {
4900 	struct ctl_softc *softc;
4901 	struct ctl_lun *lun;
4902 
4903 	lun = (struct ctl_lun *)be_lun->ctl_lun;
4904 	softc = lun->ctl_softc;
4905 
4906 	mtx_lock(&lun->lun_lock);
4907 
4908 	/*
4909 	 * The LUN needs to be disabled before it can be marked invalid.
4910 	 */
4911 	if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4912 		mtx_unlock(&lun->lun_lock);
4913 		return (-1);
4914 	}
4915 	/*
4916 	 * Mark the LUN invalid.
4917 	 */
4918 	lun->flags |= CTL_LUN_INVALID;
4919 
4920 	/*
4921 	 * If there is nothing in the OOA queue, go ahead and free the LUN.
4922 	 * If we have something in the OOA queue, we'll free it when the
4923 	 * last I/O completes.
4924 	 */
4925 	if (TAILQ_EMPTY(&lun->ooa_queue)) {
4926 		mtx_unlock(&lun->lun_lock);
4927 		mtx_lock(&softc->ctl_lock);
4928 		ctl_free_lun(lun);
4929 		mtx_unlock(&softc->ctl_lock);
4930 	} else
4931 		mtx_unlock(&lun->lun_lock);
4932 
4933 	return (0);
4934 }
4935 
4936 void
4937 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4938 {
4939 	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4940 	union ctl_ha_msg msg;
4941 
4942 	mtx_lock(&lun->lun_lock);
4943 	ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4944 	mtx_unlock(&lun->lun_lock);
4945 	if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4946 		/* Send msg to other side. */
4947 		bzero(&msg.ua, sizeof(msg.ua));
4948 		msg.hdr.msg_type = CTL_MSG_UA;
4949 		msg.hdr.nexus.initid = -1;
4950 		msg.hdr.nexus.targ_port = -1;
4951 		msg.hdr.nexus.targ_lun = lun->lun;
4952 		msg.hdr.nexus.targ_mapped_lun = lun->lun;
4953 		msg.ua.ua_all = 1;
4954 		msg.ua.ua_set = 1;
4955 		msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4956 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4957 		    M_WAITOK);
4958 	}
4959 }
4960 
4961 /*
4962  * Backend "memory move is complete" callback for requests that never
4963  * make it down to say RAIDCore's configuration code.
4964  */
4965 int
4966 ctl_config_move_done(union ctl_io *io)
4967 {
4968 	int retval;
4969 
4970 	CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4971 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4972 	    ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4973 
4974 	if ((io->io_hdr.port_status != 0) &&
4975 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4976 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4977 		/*
4978 		 * For hardware error sense keys, the sense key
4979 		 * specific value is defined to be a retry count,
4980 		 * but we use it to pass back an internal FETD
4981 		 * error code.  XXX KDM  Hopefully the FETD is only
4982 		 * using 16 bits for an error code, since that's
4983 		 * all the space we have in the sks field.
4984 		 */
4985 		ctl_set_internal_failure(&io->scsiio,
4986 					 /*sks_valid*/ 1,
4987 					 /*retry_count*/
4988 					 io->io_hdr.port_status);
4989 	}
4990 
4991 	if (ctl_debug & CTL_DEBUG_CDB_DATA)
4992 		ctl_data_print(io);
4993 	if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4994 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4995 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4996 	    ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4997 		/*
4998 		 * XXX KDM just assuming a single pointer here, and not a
4999 		 * S/G list.  If we start using S/G lists for config data,
5000 		 * we'll need to know how to clean them up here as well.
5001 		 */
5002 		if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5003 			free(io->scsiio.kern_data_ptr, M_CTL);
5004 		ctl_done(io);
5005 		retval = CTL_RETVAL_COMPLETE;
5006 	} else {
5007 		/*
5008 		 * XXX KDM now we need to continue data movement.  Some
5009 		 * options:
5010 		 * - call ctl_scsiio() again?  We don't do this for data
5011 		 *   writes, because for those at least we know ahead of
5012 		 *   time where the write will go and how long it is.  For
5013 		 *   config writes, though, that information is largely
5014 		 *   contained within the write itself, thus we need to
5015 		 *   parse out the data again.
5016 		 *
5017 		 * - Call some other function once the data is in?
5018 		 */
5019 
5020 		/*
5021 		 * XXX KDM call ctl_scsiio() again for now, and check flag
5022 		 * bits to see whether we're allocated or not.
5023 		 */
5024 		retval = ctl_scsiio(&io->scsiio);
5025 	}
5026 	return (retval);
5027 }
5028 
5029 /*
5030  * This gets called by a backend driver when it is done with a
5031  * data_submit method.
5032  */
5033 void
5034 ctl_data_submit_done(union ctl_io *io)
5035 {
5036 	/*
5037 	 * If the IO_CONT flag is set, we need to call the supplied
5038 	 * function to continue processing the I/O, instead of completing
5039 	 * the I/O just yet.
5040 	 *
5041 	 * If there is an error, though, we don't want to keep processing.
5042 	 * Instead, just send status back to the initiator.
5043 	 */
5044 	if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5045 	    (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5046 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5047 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5048 		io->scsiio.io_cont(io);
5049 		return;
5050 	}
5051 	ctl_done(io);
5052 }
5053 
5054 /*
5055  * This gets called by a backend driver when it is done with a
5056  * configuration write.
5057  */
5058 void
5059 ctl_config_write_done(union ctl_io *io)
5060 {
5061 	uint8_t *buf;
5062 
5063 	/*
5064 	 * If the IO_CONT flag is set, we need to call the supplied
5065 	 * function to continue processing the I/O, instead of completing
5066 	 * the I/O just yet.
5067 	 *
5068 	 * If there is an error, though, we don't want to keep processing.
5069 	 * Instead, just send status back to the initiator.
5070 	 */
5071 	if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5072 	    (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5073 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5074 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5075 		io->scsiio.io_cont(io);
5076 		return;
5077 	}
5078 	/*
5079 	 * Since a configuration write can be done for commands that actually
5080 	 * have data allocated, like write buffer, and commands that have
5081 	 * no data, like start/stop unit, we need to check here.
5082 	 */
5083 	if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5084 		buf = io->scsiio.kern_data_ptr;
5085 	else
5086 		buf = NULL;
5087 	ctl_done(io);
5088 	if (buf)
5089 		free(buf, M_CTL);
5090 }
5091 
5092 void
5093 ctl_config_read_done(union ctl_io *io)
5094 {
5095 	uint8_t *buf;
5096 
5097 	/*
5098 	 * If there is some error -- we are done, skip data transfer.
5099 	 */
5100 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5101 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5102 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5103 		if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5104 			buf = io->scsiio.kern_data_ptr;
5105 		else
5106 			buf = NULL;
5107 		ctl_done(io);
5108 		if (buf)
5109 			free(buf, M_CTL);
5110 		return;
5111 	}
5112 
5113 	/*
5114 	 * If the IO_CONT flag is set, we need to call the supplied
5115 	 * function to continue processing the I/O, instead of completing
5116 	 * the I/O just yet.
5117 	 */
5118 	if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5119 		io->scsiio.io_cont(io);
5120 		return;
5121 	}
5122 
5123 	ctl_datamove(io);
5124 }
5125 
5126 /*
5127  * SCSI release command.
5128  */
5129 int
5130 ctl_scsi_release(struct ctl_scsiio *ctsio)
5131 {
5132 	struct ctl_lun *lun;
5133 	uint32_t residx;
5134 
5135 	CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5136 
5137 	residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5138 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5139 
5140 	/*
5141 	 * XXX KDM right now, we only support LUN reservation.  We don't
5142 	 * support 3rd party reservations, or extent reservations, which
5143 	 * might actually need the parameter list.  If we've gotten this
5144 	 * far, we've got a LUN reservation.  Anything else got kicked out
5145 	 * above.  So, according to SPC, ignore the length.
5146 	 */
5147 
5148 	mtx_lock(&lun->lun_lock);
5149 
5150 	/*
5151 	 * According to SPC, it is not an error for an intiator to attempt
5152 	 * to release a reservation on a LUN that isn't reserved, or that
5153 	 * is reserved by another initiator.  The reservation can only be
5154 	 * released, though, by the initiator who made it or by one of
5155 	 * several reset type events.
5156 	 */
5157 	if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5158 			lun->flags &= ~CTL_LUN_RESERVED;
5159 
5160 	mtx_unlock(&lun->lun_lock);
5161 
5162 	ctl_set_success(ctsio);
5163 	ctl_done((union ctl_io *)ctsio);
5164 	return (CTL_RETVAL_COMPLETE);
5165 }
5166 
5167 int
5168 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5169 {
5170 	struct ctl_lun *lun;
5171 	uint32_t residx;
5172 
5173 	CTL_DEBUG_PRINT(("ctl_reserve\n"));
5174 
5175 	residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5176 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5177 
5178 	/*
5179 	 * XXX KDM right now, we only support LUN reservation.  We don't
5180 	 * support 3rd party reservations, or extent reservations, which
5181 	 * might actually need the parameter list.  If we've gotten this
5182 	 * far, we've got a LUN reservation.  Anything else got kicked out
5183 	 * above.  So, according to SPC, ignore the length.
5184 	 */
5185 
5186 	mtx_lock(&lun->lun_lock);
5187 	if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5188 		ctl_set_reservation_conflict(ctsio);
5189 		goto bailout;
5190 	}
5191 
5192 	/* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5193 	if (lun->flags & CTL_LUN_PR_RESERVED) {
5194 		ctl_set_success(ctsio);
5195 		goto bailout;
5196 	}
5197 
5198 	lun->flags |= CTL_LUN_RESERVED;
5199 	lun->res_idx = residx;
5200 	ctl_set_success(ctsio);
5201 
5202 bailout:
5203 	mtx_unlock(&lun->lun_lock);
5204 	ctl_done((union ctl_io *)ctsio);
5205 	return (CTL_RETVAL_COMPLETE);
5206 }
5207 
5208 int
5209 ctl_start_stop(struct ctl_scsiio *ctsio)
5210 {
5211 	struct scsi_start_stop_unit *cdb;
5212 	struct ctl_lun *lun;
5213 	int retval;
5214 
5215 	CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5216 
5217 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5218 	cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5219 
5220 	if ((cdb->how & SSS_PC_MASK) == 0) {
5221 		if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5222 		    (cdb->how & SSS_START) == 0) {
5223 			uint32_t residx;
5224 
5225 			residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5226 			if (ctl_get_prkey(lun, residx) == 0 ||
5227 			    (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5228 
5229 				ctl_set_reservation_conflict(ctsio);
5230 				ctl_done((union ctl_io *)ctsio);
5231 				return (CTL_RETVAL_COMPLETE);
5232 			}
5233 		}
5234 
5235 		if ((cdb->how & SSS_LOEJ) &&
5236 		    (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5237 			ctl_set_invalid_field(ctsio,
5238 					      /*sks_valid*/ 1,
5239 					      /*command*/ 1,
5240 					      /*field*/ 4,
5241 					      /*bit_valid*/ 1,
5242 					      /*bit*/ 1);
5243 			ctl_done((union ctl_io *)ctsio);
5244 			return (CTL_RETVAL_COMPLETE);
5245 		}
5246 
5247 		if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5248 		    lun->prevent_count > 0) {
5249 			/* "Medium removal prevented" */
5250 			ctl_set_sense(ctsio, /*current_error*/ 1,
5251 			    /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5252 			     SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5253 			    /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5254 			ctl_done((union ctl_io *)ctsio);
5255 			return (CTL_RETVAL_COMPLETE);
5256 		}
5257 	}
5258 
5259 	retval = lun->backend->config_write((union ctl_io *)ctsio);
5260 	return (retval);
5261 }
5262 
5263 int
5264 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5265 {
5266 	struct ctl_lun *lun;
5267 	struct scsi_prevent *cdb;
5268 	int retval;
5269 	uint32_t initidx;
5270 
5271 	CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5272 
5273 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5274 	cdb = (struct scsi_prevent *)ctsio->cdb;
5275 
5276 	if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5277 		ctl_set_invalid_opcode(ctsio);
5278 		ctl_done((union ctl_io *)ctsio);
5279 		return (CTL_RETVAL_COMPLETE);
5280 	}
5281 
5282 	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5283 	mtx_lock(&lun->lun_lock);
5284 	if ((cdb->how & PR_PREVENT) &&
5285 	    ctl_is_set(lun->prevent, initidx) == 0) {
5286 		ctl_set_mask(lun->prevent, initidx);
5287 		lun->prevent_count++;
5288 	} else if ((cdb->how & PR_PREVENT) == 0 &&
5289 	    ctl_is_set(lun->prevent, initidx)) {
5290 		ctl_clear_mask(lun->prevent, initidx);
5291 		lun->prevent_count--;
5292 	}
5293 	mtx_unlock(&lun->lun_lock);
5294 	retval = lun->backend->config_write((union ctl_io *)ctsio);
5295 	return (retval);
5296 }
5297 
5298 /*
5299  * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5300  * we don't really do anything with the LBA and length fields if the user
5301  * passes them in.  Instead we'll just flush out the cache for the entire
5302  * LUN.
5303  */
5304 int
5305 ctl_sync_cache(struct ctl_scsiio *ctsio)
5306 {
5307 	struct ctl_lun *lun;
5308 	struct ctl_softc *softc;
5309 	struct ctl_lba_len_flags *lbalen;
5310 	uint64_t starting_lba;
5311 	uint32_t block_count;
5312 	int retval;
5313 	uint8_t byte2;
5314 
5315 	CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5316 
5317 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5318 	softc = lun->ctl_softc;
5319 	retval = 0;
5320 
5321 	switch (ctsio->cdb[0]) {
5322 	case SYNCHRONIZE_CACHE: {
5323 		struct scsi_sync_cache *cdb;
5324 		cdb = (struct scsi_sync_cache *)ctsio->cdb;
5325 
5326 		starting_lba = scsi_4btoul(cdb->begin_lba);
5327 		block_count = scsi_2btoul(cdb->lb_count);
5328 		byte2 = cdb->byte2;
5329 		break;
5330 	}
5331 	case SYNCHRONIZE_CACHE_16: {
5332 		struct scsi_sync_cache_16 *cdb;
5333 		cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5334 
5335 		starting_lba = scsi_8btou64(cdb->begin_lba);
5336 		block_count = scsi_4btoul(cdb->lb_count);
5337 		byte2 = cdb->byte2;
5338 		break;
5339 	}
5340 	default:
5341 		ctl_set_invalid_opcode(ctsio);
5342 		ctl_done((union ctl_io *)ctsio);
5343 		goto bailout;
5344 		break; /* NOTREACHED */
5345 	}
5346 
5347 	/*
5348 	 * We check the LBA and length, but don't do anything with them.
5349 	 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5350 	 * get flushed.  This check will just help satisfy anyone who wants
5351 	 * to see an error for an out of range LBA.
5352 	 */
5353 	if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5354 		ctl_set_lba_out_of_range(ctsio);
5355 		ctl_done((union ctl_io *)ctsio);
5356 		goto bailout;
5357 	}
5358 
5359 	lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5360 	lbalen->lba = starting_lba;
5361 	lbalen->len = block_count;
5362 	lbalen->flags = byte2;
5363 	retval = lun->backend->config_write((union ctl_io *)ctsio);
5364 
5365 bailout:
5366 	return (retval);
5367 }
5368 
5369 int
5370 ctl_format(struct ctl_scsiio *ctsio)
5371 {
5372 	struct scsi_format *cdb;
5373 	struct ctl_lun *lun;
5374 	int length, defect_list_len;
5375 
5376 	CTL_DEBUG_PRINT(("ctl_format\n"));
5377 
5378 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5379 
5380 	cdb = (struct scsi_format *)ctsio->cdb;
5381 
5382 	length = 0;
5383 	if (cdb->byte2 & SF_FMTDATA) {
5384 		if (cdb->byte2 & SF_LONGLIST)
5385 			length = sizeof(struct scsi_format_header_long);
5386 		else
5387 			length = sizeof(struct scsi_format_header_short);
5388 	}
5389 
5390 	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5391 	 && (length > 0)) {
5392 		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5393 		ctsio->kern_data_len = length;
5394 		ctsio->kern_total_len = length;
5395 		ctsio->kern_data_resid = 0;
5396 		ctsio->kern_rel_offset = 0;
5397 		ctsio->kern_sg_entries = 0;
5398 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5399 		ctsio->be_move_done = ctl_config_move_done;
5400 		ctl_datamove((union ctl_io *)ctsio);
5401 
5402 		return (CTL_RETVAL_COMPLETE);
5403 	}
5404 
5405 	defect_list_len = 0;
5406 
5407 	if (cdb->byte2 & SF_FMTDATA) {
5408 		if (cdb->byte2 & SF_LONGLIST) {
5409 			struct scsi_format_header_long *header;
5410 
5411 			header = (struct scsi_format_header_long *)
5412 				ctsio->kern_data_ptr;
5413 
5414 			defect_list_len = scsi_4btoul(header->defect_list_len);
5415 			if (defect_list_len != 0) {
5416 				ctl_set_invalid_field(ctsio,
5417 						      /*sks_valid*/ 1,
5418 						      /*command*/ 0,
5419 						      /*field*/ 2,
5420 						      /*bit_valid*/ 0,
5421 						      /*bit*/ 0);
5422 				goto bailout;
5423 			}
5424 		} else {
5425 			struct scsi_format_header_short *header;
5426 
5427 			header = (struct scsi_format_header_short *)
5428 				ctsio->kern_data_ptr;
5429 
5430 			defect_list_len = scsi_2btoul(header->defect_list_len);
5431 			if (defect_list_len != 0) {
5432 				ctl_set_invalid_field(ctsio,
5433 						      /*sks_valid*/ 1,
5434 						      /*command*/ 0,
5435 						      /*field*/ 2,
5436 						      /*bit_valid*/ 0,
5437 						      /*bit*/ 0);
5438 				goto bailout;
5439 			}
5440 		}
5441 	}
5442 
5443 	ctl_set_success(ctsio);
5444 bailout:
5445 
5446 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5447 		free(ctsio->kern_data_ptr, M_CTL);
5448 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5449 	}
5450 
5451 	ctl_done((union ctl_io *)ctsio);
5452 	return (CTL_RETVAL_COMPLETE);
5453 }
5454 
5455 int
5456 ctl_read_buffer(struct ctl_scsiio *ctsio)
5457 {
5458 	struct ctl_lun *lun;
5459 	uint64_t buffer_offset;
5460 	uint32_t len;
5461 	uint8_t byte2;
5462 	static uint8_t descr[4];
5463 	static uint8_t echo_descr[4] = { 0 };
5464 
5465 	CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5466 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5467 	switch (ctsio->cdb[0]) {
5468 	case READ_BUFFER: {
5469 		struct scsi_read_buffer *cdb;
5470 
5471 		cdb = (struct scsi_read_buffer *)ctsio->cdb;
5472 		buffer_offset = scsi_3btoul(cdb->offset);
5473 		len = scsi_3btoul(cdb->length);
5474 		byte2 = cdb->byte2;
5475 		break;
5476 	}
5477 	case READ_BUFFER_16: {
5478 		struct scsi_read_buffer_16 *cdb;
5479 
5480 		cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5481 		buffer_offset = scsi_8btou64(cdb->offset);
5482 		len = scsi_4btoul(cdb->length);
5483 		byte2 = cdb->byte2;
5484 		break;
5485 	}
5486 	default: /* This shouldn't happen. */
5487 		ctl_set_invalid_opcode(ctsio);
5488 		ctl_done((union ctl_io *)ctsio);
5489 		return (CTL_RETVAL_COMPLETE);
5490 	}
5491 
5492 	if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5493 	    (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5494 	    (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5495 		ctl_set_invalid_field(ctsio,
5496 				      /*sks_valid*/ 1,
5497 				      /*command*/ 1,
5498 				      /*field*/ 1,
5499 				      /*bit_valid*/ 1,
5500 				      /*bit*/ 4);
5501 		ctl_done((union ctl_io *)ctsio);
5502 		return (CTL_RETVAL_COMPLETE);
5503 	}
5504 
5505 	if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5506 	    buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5507 		ctl_set_invalid_field(ctsio,
5508 				      /*sks_valid*/ 1,
5509 				      /*command*/ 1,
5510 				      /*field*/ 6,
5511 				      /*bit_valid*/ 0,
5512 				      /*bit*/ 0);
5513 		ctl_done((union ctl_io *)ctsio);
5514 		return (CTL_RETVAL_COMPLETE);
5515 	}
5516 
5517 	if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5518 		descr[0] = 0;
5519 		scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5520 		ctsio->kern_data_ptr = descr;
5521 		len = min(len, sizeof(descr));
5522 	} else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5523 		ctsio->kern_data_ptr = echo_descr;
5524 		len = min(len, sizeof(echo_descr));
5525 	} else {
5526 		if (lun->write_buffer == NULL) {
5527 			lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5528 			    M_CTL, M_WAITOK);
5529 		}
5530 		ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5531 	}
5532 	ctsio->kern_data_len = len;
5533 	ctsio->kern_total_len = len;
5534 	ctsio->kern_data_resid = 0;
5535 	ctsio->kern_rel_offset = 0;
5536 	ctsio->kern_sg_entries = 0;
5537 	ctl_set_success(ctsio);
5538 	ctsio->be_move_done = ctl_config_move_done;
5539 	ctl_datamove((union ctl_io *)ctsio);
5540 	return (CTL_RETVAL_COMPLETE);
5541 }
5542 
5543 int
5544 ctl_write_buffer(struct ctl_scsiio *ctsio)
5545 {
5546 	struct scsi_write_buffer *cdb;
5547 	struct ctl_lun *lun;
5548 	int buffer_offset, len;
5549 
5550 	CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5551 
5552 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5553 	cdb = (struct scsi_write_buffer *)ctsio->cdb;
5554 
5555 	if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5556 		ctl_set_invalid_field(ctsio,
5557 				      /*sks_valid*/ 1,
5558 				      /*command*/ 1,
5559 				      /*field*/ 1,
5560 				      /*bit_valid*/ 1,
5561 				      /*bit*/ 4);
5562 		ctl_done((union ctl_io *)ctsio);
5563 		return (CTL_RETVAL_COMPLETE);
5564 	}
5565 
5566 	len = scsi_3btoul(cdb->length);
5567 	buffer_offset = scsi_3btoul(cdb->offset);
5568 
5569 	if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5570 		ctl_set_invalid_field(ctsio,
5571 				      /*sks_valid*/ 1,
5572 				      /*command*/ 1,
5573 				      /*field*/ 6,
5574 				      /*bit_valid*/ 0,
5575 				      /*bit*/ 0);
5576 		ctl_done((union ctl_io *)ctsio);
5577 		return (CTL_RETVAL_COMPLETE);
5578 	}
5579 
5580 	/*
5581 	 * If we've got a kernel request that hasn't been malloced yet,
5582 	 * malloc it and tell the caller the data buffer is here.
5583 	 */
5584 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5585 		if (lun->write_buffer == NULL) {
5586 			lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5587 			    M_CTL, M_WAITOK);
5588 		}
5589 		ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5590 		ctsio->kern_data_len = len;
5591 		ctsio->kern_total_len = len;
5592 		ctsio->kern_data_resid = 0;
5593 		ctsio->kern_rel_offset = 0;
5594 		ctsio->kern_sg_entries = 0;
5595 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5596 		ctsio->be_move_done = ctl_config_move_done;
5597 		ctl_datamove((union ctl_io *)ctsio);
5598 
5599 		return (CTL_RETVAL_COMPLETE);
5600 	}
5601 
5602 	ctl_set_success(ctsio);
5603 	ctl_done((union ctl_io *)ctsio);
5604 	return (CTL_RETVAL_COMPLETE);
5605 }
5606 
5607 int
5608 ctl_write_same(struct ctl_scsiio *ctsio)
5609 {
5610 	struct ctl_lun *lun;
5611 	struct ctl_lba_len_flags *lbalen;
5612 	uint64_t lba;
5613 	uint32_t num_blocks;
5614 	int len, retval;
5615 	uint8_t byte2;
5616 
5617 	CTL_DEBUG_PRINT(("ctl_write_same\n"));
5618 
5619 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5620 
5621 	switch (ctsio->cdb[0]) {
5622 	case WRITE_SAME_10: {
5623 		struct scsi_write_same_10 *cdb;
5624 
5625 		cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5626 
5627 		lba = scsi_4btoul(cdb->addr);
5628 		num_blocks = scsi_2btoul(cdb->length);
5629 		byte2 = cdb->byte2;
5630 		break;
5631 	}
5632 	case WRITE_SAME_16: {
5633 		struct scsi_write_same_16 *cdb;
5634 
5635 		cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5636 
5637 		lba = scsi_8btou64(cdb->addr);
5638 		num_blocks = scsi_4btoul(cdb->length);
5639 		byte2 = cdb->byte2;
5640 		break;
5641 	}
5642 	default:
5643 		/*
5644 		 * We got a command we don't support.  This shouldn't
5645 		 * happen, commands should be filtered out above us.
5646 		 */
5647 		ctl_set_invalid_opcode(ctsio);
5648 		ctl_done((union ctl_io *)ctsio);
5649 
5650 		return (CTL_RETVAL_COMPLETE);
5651 		break; /* NOTREACHED */
5652 	}
5653 
5654 	/* ANCHOR flag can be used only together with UNMAP */
5655 	if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5656 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5657 		    /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5658 		ctl_done((union ctl_io *)ctsio);
5659 		return (CTL_RETVAL_COMPLETE);
5660 	}
5661 
5662 	/*
5663 	 * The first check is to make sure we're in bounds, the second
5664 	 * check is to catch wrap-around problems.  If the lba + num blocks
5665 	 * is less than the lba, then we've wrapped around and the block
5666 	 * range is invalid anyway.
5667 	 */
5668 	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5669 	 || ((lba + num_blocks) < lba)) {
5670 		ctl_set_lba_out_of_range(ctsio);
5671 		ctl_done((union ctl_io *)ctsio);
5672 		return (CTL_RETVAL_COMPLETE);
5673 	}
5674 
5675 	/* Zero number of blocks means "to the last logical block" */
5676 	if (num_blocks == 0) {
5677 		if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5678 			ctl_set_invalid_field(ctsio,
5679 					      /*sks_valid*/ 0,
5680 					      /*command*/ 1,
5681 					      /*field*/ 0,
5682 					      /*bit_valid*/ 0,
5683 					      /*bit*/ 0);
5684 			ctl_done((union ctl_io *)ctsio);
5685 			return (CTL_RETVAL_COMPLETE);
5686 		}
5687 		num_blocks = (lun->be_lun->maxlba + 1) - lba;
5688 	}
5689 
5690 	len = lun->be_lun->blocksize;
5691 
5692 	/*
5693 	 * If we've got a kernel request that hasn't been malloced yet,
5694 	 * malloc it and tell the caller the data buffer is here.
5695 	 */
5696 	if ((byte2 & SWS_NDOB) == 0 &&
5697 	    (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5698 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5699 		ctsio->kern_data_len = len;
5700 		ctsio->kern_total_len = len;
5701 		ctsio->kern_data_resid = 0;
5702 		ctsio->kern_rel_offset = 0;
5703 		ctsio->kern_sg_entries = 0;
5704 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5705 		ctsio->be_move_done = ctl_config_move_done;
5706 		ctl_datamove((union ctl_io *)ctsio);
5707 
5708 		return (CTL_RETVAL_COMPLETE);
5709 	}
5710 
5711 	lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5712 	lbalen->lba = lba;
5713 	lbalen->len = num_blocks;
5714 	lbalen->flags = byte2;
5715 	retval = lun->backend->config_write((union ctl_io *)ctsio);
5716 
5717 	return (retval);
5718 }
5719 
5720 int
5721 ctl_unmap(struct ctl_scsiio *ctsio)
5722 {
5723 	struct ctl_lun *lun;
5724 	struct scsi_unmap *cdb;
5725 	struct ctl_ptr_len_flags *ptrlen;
5726 	struct scsi_unmap_header *hdr;
5727 	struct scsi_unmap_desc *buf, *end, *endnz, *range;
5728 	uint64_t lba;
5729 	uint32_t num_blocks;
5730 	int len, retval;
5731 	uint8_t byte2;
5732 
5733 	CTL_DEBUG_PRINT(("ctl_unmap\n"));
5734 
5735 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5736 	cdb = (struct scsi_unmap *)ctsio->cdb;
5737 
5738 	len = scsi_2btoul(cdb->length);
5739 	byte2 = cdb->byte2;
5740 
5741 	/*
5742 	 * If we've got a kernel request that hasn't been malloced yet,
5743 	 * malloc it and tell the caller the data buffer is here.
5744 	 */
5745 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5746 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5747 		ctsio->kern_data_len = len;
5748 		ctsio->kern_total_len = len;
5749 		ctsio->kern_data_resid = 0;
5750 		ctsio->kern_rel_offset = 0;
5751 		ctsio->kern_sg_entries = 0;
5752 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5753 		ctsio->be_move_done = ctl_config_move_done;
5754 		ctl_datamove((union ctl_io *)ctsio);
5755 
5756 		return (CTL_RETVAL_COMPLETE);
5757 	}
5758 
5759 	len = ctsio->kern_total_len - ctsio->kern_data_resid;
5760 	hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5761 	if (len < sizeof (*hdr) ||
5762 	    len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5763 	    len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5764 	    scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5765 		ctl_set_invalid_field(ctsio,
5766 				      /*sks_valid*/ 0,
5767 				      /*command*/ 0,
5768 				      /*field*/ 0,
5769 				      /*bit_valid*/ 0,
5770 				      /*bit*/ 0);
5771 		goto done;
5772 	}
5773 	len = scsi_2btoul(hdr->desc_length);
5774 	buf = (struct scsi_unmap_desc *)(hdr + 1);
5775 	end = buf + len / sizeof(*buf);
5776 
5777 	endnz = buf;
5778 	for (range = buf; range < end; range++) {
5779 		lba = scsi_8btou64(range->lba);
5780 		num_blocks = scsi_4btoul(range->length);
5781 		if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5782 		 || ((lba + num_blocks) < lba)) {
5783 			ctl_set_lba_out_of_range(ctsio);
5784 			ctl_done((union ctl_io *)ctsio);
5785 			return (CTL_RETVAL_COMPLETE);
5786 		}
5787 		if (num_blocks != 0)
5788 			endnz = range + 1;
5789 	}
5790 
5791 	/*
5792 	 * Block backend can not handle zero last range.
5793 	 * Filter it out and return if there is nothing left.
5794 	 */
5795 	len = (uint8_t *)endnz - (uint8_t *)buf;
5796 	if (len == 0) {
5797 		ctl_set_success(ctsio);
5798 		goto done;
5799 	}
5800 
5801 	mtx_lock(&lun->lun_lock);
5802 	ptrlen = (struct ctl_ptr_len_flags *)
5803 	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5804 	ptrlen->ptr = (void *)buf;
5805 	ptrlen->len = len;
5806 	ptrlen->flags = byte2;
5807 	ctl_check_blocked(lun);
5808 	mtx_unlock(&lun->lun_lock);
5809 
5810 	retval = lun->backend->config_write((union ctl_io *)ctsio);
5811 	return (retval);
5812 
5813 done:
5814 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5815 		free(ctsio->kern_data_ptr, M_CTL);
5816 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5817 	}
5818 	ctl_done((union ctl_io *)ctsio);
5819 	return (CTL_RETVAL_COMPLETE);
5820 }
5821 
5822 /*
5823  * Note that this function currently doesn't actually do anything inside
5824  * CTL to enforce things if the DQue bit is turned on.
5825  *
5826  * Also note that this function can't be used in the default case, because
5827  * the DQue bit isn't set in the changeable mask for the control mode page
5828  * anyway.  This is just here as an example for how to implement a page
5829  * handler, and a placeholder in case we want to allow the user to turn
5830  * tagged queueing on and off.
5831  *
5832  * The D_SENSE bit handling is functional, however, and will turn
5833  * descriptor sense on and off for a given LUN.
5834  */
5835 int
5836 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5837 			 struct ctl_page_index *page_index, uint8_t *page_ptr)
5838 {
5839 	struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5840 	struct ctl_lun *lun;
5841 	int set_ua;
5842 	uint32_t initidx;
5843 
5844 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5845 	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5846 	set_ua = 0;
5847 
5848 	user_cp = (struct scsi_control_page *)page_ptr;
5849 	current_cp = (struct scsi_control_page *)
5850 		(page_index->page_data + (page_index->page_len *
5851 		CTL_PAGE_CURRENT));
5852 	saved_cp = (struct scsi_control_page *)
5853 		(page_index->page_data + (page_index->page_len *
5854 		CTL_PAGE_SAVED));
5855 
5856 	mtx_lock(&lun->lun_lock);
5857 	if (((current_cp->rlec & SCP_DSENSE) == 0)
5858 	 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5859 		/*
5860 		 * Descriptor sense is currently turned off and the user
5861 		 * wants to turn it on.
5862 		 */
5863 		current_cp->rlec |= SCP_DSENSE;
5864 		saved_cp->rlec |= SCP_DSENSE;
5865 		lun->flags |= CTL_LUN_SENSE_DESC;
5866 		set_ua = 1;
5867 	} else if (((current_cp->rlec & SCP_DSENSE) != 0)
5868 		&& ((user_cp->rlec & SCP_DSENSE) == 0)) {
5869 		/*
5870 		 * Descriptor sense is currently turned on, and the user
5871 		 * wants to turn it off.
5872 		 */
5873 		current_cp->rlec &= ~SCP_DSENSE;
5874 		saved_cp->rlec &= ~SCP_DSENSE;
5875 		lun->flags &= ~CTL_LUN_SENSE_DESC;
5876 		set_ua = 1;
5877 	}
5878 	if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5879 	    (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5880 		current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5881 		current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5882 		saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5883 		saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5884 		set_ua = 1;
5885 	}
5886 	if ((current_cp->eca_and_aen & SCP_SWP) !=
5887 	    (user_cp->eca_and_aen & SCP_SWP)) {
5888 		current_cp->eca_and_aen &= ~SCP_SWP;
5889 		current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5890 		saved_cp->eca_and_aen &= ~SCP_SWP;
5891 		saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5892 		set_ua = 1;
5893 	}
5894 	if (set_ua != 0)
5895 		ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5896 	mtx_unlock(&lun->lun_lock);
5897 	if (set_ua) {
5898 		ctl_isc_announce_mode(lun,
5899 		    ctl_get_initindex(&ctsio->io_hdr.nexus),
5900 		    page_index->page_code, page_index->subpage);
5901 	}
5902 	return (0);
5903 }
5904 
5905 int
5906 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5907 		     struct ctl_page_index *page_index, uint8_t *page_ptr)
5908 {
5909 	struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5910 	struct ctl_lun *lun;
5911 	int set_ua;
5912 	uint32_t initidx;
5913 
5914 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5915 	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5916 	set_ua = 0;
5917 
5918 	user_cp = (struct scsi_caching_page *)page_ptr;
5919 	current_cp = (struct scsi_caching_page *)
5920 		(page_index->page_data + (page_index->page_len *
5921 		CTL_PAGE_CURRENT));
5922 	saved_cp = (struct scsi_caching_page *)
5923 		(page_index->page_data + (page_index->page_len *
5924 		CTL_PAGE_SAVED));
5925 
5926 	mtx_lock(&lun->lun_lock);
5927 	if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5928 	    (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5929 		current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5930 		current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5931 		saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5932 		saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5933 		set_ua = 1;
5934 	}
5935 	if (set_ua != 0)
5936 		ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5937 	mtx_unlock(&lun->lun_lock);
5938 	if (set_ua) {
5939 		ctl_isc_announce_mode(lun,
5940 		    ctl_get_initindex(&ctsio->io_hdr.nexus),
5941 		    page_index->page_code, page_index->subpage);
5942 	}
5943 	return (0);
5944 }
5945 
5946 int
5947 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5948 				struct ctl_page_index *page_index,
5949 				uint8_t *page_ptr)
5950 {
5951 	uint8_t *c;
5952 	int i;
5953 
5954 	c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5955 	ctl_time_io_secs =
5956 		(c[0] << 8) |
5957 		(c[1] << 0) |
5958 		0;
5959 	CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5960 	printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5961 	printf("page data:");
5962 	for (i=0; i<8; i++)
5963 		printf(" %.2x",page_ptr[i]);
5964 	printf("\n");
5965 	return (0);
5966 }
5967 
5968 int
5969 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5970 			       struct ctl_page_index *page_index,
5971 			       int pc)
5972 {
5973 	struct copan_debugconf_subpage *page;
5974 
5975 	page = (struct copan_debugconf_subpage *)page_index->page_data +
5976 		(page_index->page_len * pc);
5977 
5978 	switch (pc) {
5979 	case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5980 	case SMS_PAGE_CTRL_DEFAULT >> 6:
5981 	case SMS_PAGE_CTRL_SAVED >> 6:
5982 		/*
5983 		 * We don't update the changeable or default bits for this page.
5984 		 */
5985 		break;
5986 	case SMS_PAGE_CTRL_CURRENT >> 6:
5987 		page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5988 		page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5989 		break;
5990 	default:
5991 		break;
5992 	}
5993 	return (0);
5994 }
5995 
5996 
5997 static int
5998 ctl_do_mode_select(union ctl_io *io)
5999 {
6000 	struct scsi_mode_page_header *page_header;
6001 	struct ctl_page_index *page_index;
6002 	struct ctl_scsiio *ctsio;
6003 	int page_len, page_len_offset, page_len_size;
6004 	union ctl_modepage_info *modepage_info;
6005 	struct ctl_lun *lun;
6006 	int *len_left, *len_used;
6007 	int retval, i;
6008 
6009 	ctsio = &io->scsiio;
6010 	page_index = NULL;
6011 	page_len = 0;
6012 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6013 
6014 	modepage_info = (union ctl_modepage_info *)
6015 		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6016 	len_left = &modepage_info->header.len_left;
6017 	len_used = &modepage_info->header.len_used;
6018 
6019 do_next_page:
6020 
6021 	page_header = (struct scsi_mode_page_header *)
6022 		(ctsio->kern_data_ptr + *len_used);
6023 
6024 	if (*len_left == 0) {
6025 		free(ctsio->kern_data_ptr, M_CTL);
6026 		ctl_set_success(ctsio);
6027 		ctl_done((union ctl_io *)ctsio);
6028 		return (CTL_RETVAL_COMPLETE);
6029 	} else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6030 
6031 		free(ctsio->kern_data_ptr, M_CTL);
6032 		ctl_set_param_len_error(ctsio);
6033 		ctl_done((union ctl_io *)ctsio);
6034 		return (CTL_RETVAL_COMPLETE);
6035 
6036 	} else if ((page_header->page_code & SMPH_SPF)
6037 		&& (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6038 
6039 		free(ctsio->kern_data_ptr, M_CTL);
6040 		ctl_set_param_len_error(ctsio);
6041 		ctl_done((union ctl_io *)ctsio);
6042 		return (CTL_RETVAL_COMPLETE);
6043 	}
6044 
6045 
6046 	/*
6047 	 * XXX KDM should we do something with the block descriptor?
6048 	 */
6049 	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6050 		page_index = &lun->mode_pages.index[i];
6051 		if (lun->be_lun->lun_type == T_DIRECT &&
6052 		    (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6053 			continue;
6054 		if (lun->be_lun->lun_type == T_PROCESSOR &&
6055 		    (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6056 			continue;
6057 		if (lun->be_lun->lun_type == T_CDROM &&
6058 		    (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6059 			continue;
6060 
6061 		if ((page_index->page_code & SMPH_PC_MASK) !=
6062 		    (page_header->page_code & SMPH_PC_MASK))
6063 			continue;
6064 
6065 		/*
6066 		 * If neither page has a subpage code, then we've got a
6067 		 * match.
6068 		 */
6069 		if (((page_index->page_code & SMPH_SPF) == 0)
6070 		 && ((page_header->page_code & SMPH_SPF) == 0)) {
6071 			page_len = page_header->page_length;
6072 			break;
6073 		}
6074 
6075 		/*
6076 		 * If both pages have subpages, then the subpage numbers
6077 		 * have to match.
6078 		 */
6079 		if ((page_index->page_code & SMPH_SPF)
6080 		  && (page_header->page_code & SMPH_SPF)) {
6081 			struct scsi_mode_page_header_sp *sph;
6082 
6083 			sph = (struct scsi_mode_page_header_sp *)page_header;
6084 			if (page_index->subpage == sph->subpage) {
6085 				page_len = scsi_2btoul(sph->page_length);
6086 				break;
6087 			}
6088 		}
6089 	}
6090 
6091 	/*
6092 	 * If we couldn't find the page, or if we don't have a mode select
6093 	 * handler for it, send back an error to the user.
6094 	 */
6095 	if ((i >= CTL_NUM_MODE_PAGES)
6096 	 || (page_index->select_handler == NULL)) {
6097 		ctl_set_invalid_field(ctsio,
6098 				      /*sks_valid*/ 1,
6099 				      /*command*/ 0,
6100 				      /*field*/ *len_used,
6101 				      /*bit_valid*/ 0,
6102 				      /*bit*/ 0);
6103 		free(ctsio->kern_data_ptr, M_CTL);
6104 		ctl_done((union ctl_io *)ctsio);
6105 		return (CTL_RETVAL_COMPLETE);
6106 	}
6107 
6108 	if (page_index->page_code & SMPH_SPF) {
6109 		page_len_offset = 2;
6110 		page_len_size = 2;
6111 	} else {
6112 		page_len_size = 1;
6113 		page_len_offset = 1;
6114 	}
6115 
6116 	/*
6117 	 * If the length the initiator gives us isn't the one we specify in
6118 	 * the mode page header, or if they didn't specify enough data in
6119 	 * the CDB to avoid truncating this page, kick out the request.
6120 	 */
6121 	if ((page_len != (page_index->page_len - page_len_offset -
6122 			  page_len_size))
6123 	 || (*len_left < page_index->page_len)) {
6124 
6125 
6126 		ctl_set_invalid_field(ctsio,
6127 				      /*sks_valid*/ 1,
6128 				      /*command*/ 0,
6129 				      /*field*/ *len_used + page_len_offset,
6130 				      /*bit_valid*/ 0,
6131 				      /*bit*/ 0);
6132 		free(ctsio->kern_data_ptr, M_CTL);
6133 		ctl_done((union ctl_io *)ctsio);
6134 		return (CTL_RETVAL_COMPLETE);
6135 	}
6136 
6137 	/*
6138 	 * Run through the mode page, checking to make sure that the bits
6139 	 * the user changed are actually legal for him to change.
6140 	 */
6141 	for (i = 0; i < page_index->page_len; i++) {
6142 		uint8_t *user_byte, *change_mask, *current_byte;
6143 		int bad_bit;
6144 		int j;
6145 
6146 		user_byte = (uint8_t *)page_header + i;
6147 		change_mask = page_index->page_data +
6148 			      (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6149 		current_byte = page_index->page_data +
6150 			       (page_index->page_len * CTL_PAGE_CURRENT) + i;
6151 
6152 		/*
6153 		 * Check to see whether the user set any bits in this byte
6154 		 * that he is not allowed to set.
6155 		 */
6156 		if ((*user_byte & ~(*change_mask)) ==
6157 		    (*current_byte & ~(*change_mask)))
6158 			continue;
6159 
6160 		/*
6161 		 * Go through bit by bit to determine which one is illegal.
6162 		 */
6163 		bad_bit = 0;
6164 		for (j = 7; j >= 0; j--) {
6165 			if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6166 			    (((1 << i) & ~(*change_mask)) & *current_byte)) {
6167 				bad_bit = i;
6168 				break;
6169 			}
6170 		}
6171 		ctl_set_invalid_field(ctsio,
6172 				      /*sks_valid*/ 1,
6173 				      /*command*/ 0,
6174 				      /*field*/ *len_used + i,
6175 				      /*bit_valid*/ 1,
6176 				      /*bit*/ bad_bit);
6177 		free(ctsio->kern_data_ptr, M_CTL);
6178 		ctl_done((union ctl_io *)ctsio);
6179 		return (CTL_RETVAL_COMPLETE);
6180 	}
6181 
6182 	/*
6183 	 * Decrement these before we call the page handler, since we may
6184 	 * end up getting called back one way or another before the handler
6185 	 * returns to this context.
6186 	 */
6187 	*len_left -= page_index->page_len;
6188 	*len_used += page_index->page_len;
6189 
6190 	retval = page_index->select_handler(ctsio, page_index,
6191 					    (uint8_t *)page_header);
6192 
6193 	/*
6194 	 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6195 	 * wait until this queued command completes to finish processing
6196 	 * the mode page.  If it returns anything other than
6197 	 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6198 	 * already set the sense information, freed the data pointer, and
6199 	 * completed the io for us.
6200 	 */
6201 	if (retval != CTL_RETVAL_COMPLETE)
6202 		goto bailout_no_done;
6203 
6204 	/*
6205 	 * If the initiator sent us more than one page, parse the next one.
6206 	 */
6207 	if (*len_left > 0)
6208 		goto do_next_page;
6209 
6210 	ctl_set_success(ctsio);
6211 	free(ctsio->kern_data_ptr, M_CTL);
6212 	ctl_done((union ctl_io *)ctsio);
6213 
6214 bailout_no_done:
6215 
6216 	return (CTL_RETVAL_COMPLETE);
6217 
6218 }
6219 
6220 int
6221 ctl_mode_select(struct ctl_scsiio *ctsio)
6222 {
6223 	int param_len, pf, sp;
6224 	int header_size, bd_len;
6225 	union ctl_modepage_info *modepage_info;
6226 
6227 	switch (ctsio->cdb[0]) {
6228 	case MODE_SELECT_6: {
6229 		struct scsi_mode_select_6 *cdb;
6230 
6231 		cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6232 
6233 		pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6234 		sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6235 		param_len = cdb->length;
6236 		header_size = sizeof(struct scsi_mode_header_6);
6237 		break;
6238 	}
6239 	case MODE_SELECT_10: {
6240 		struct scsi_mode_select_10 *cdb;
6241 
6242 		cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6243 
6244 		pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6245 		sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6246 		param_len = scsi_2btoul(cdb->length);
6247 		header_size = sizeof(struct scsi_mode_header_10);
6248 		break;
6249 	}
6250 	default:
6251 		ctl_set_invalid_opcode(ctsio);
6252 		ctl_done((union ctl_io *)ctsio);
6253 		return (CTL_RETVAL_COMPLETE);
6254 	}
6255 
6256 	/*
6257 	 * From SPC-3:
6258 	 * "A parameter list length of zero indicates that the Data-Out Buffer
6259 	 * shall be empty. This condition shall not be considered as an error."
6260 	 */
6261 	if (param_len == 0) {
6262 		ctl_set_success(ctsio);
6263 		ctl_done((union ctl_io *)ctsio);
6264 		return (CTL_RETVAL_COMPLETE);
6265 	}
6266 
6267 	/*
6268 	 * Since we'll hit this the first time through, prior to
6269 	 * allocation, we don't need to free a data buffer here.
6270 	 */
6271 	if (param_len < header_size) {
6272 		ctl_set_param_len_error(ctsio);
6273 		ctl_done((union ctl_io *)ctsio);
6274 		return (CTL_RETVAL_COMPLETE);
6275 	}
6276 
6277 	/*
6278 	 * Allocate the data buffer and grab the user's data.  In theory,
6279 	 * we shouldn't have to sanity check the parameter list length here
6280 	 * because the maximum size is 64K.  We should be able to malloc
6281 	 * that much without too many problems.
6282 	 */
6283 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6284 		ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6285 		ctsio->kern_data_len = param_len;
6286 		ctsio->kern_total_len = param_len;
6287 		ctsio->kern_data_resid = 0;
6288 		ctsio->kern_rel_offset = 0;
6289 		ctsio->kern_sg_entries = 0;
6290 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6291 		ctsio->be_move_done = ctl_config_move_done;
6292 		ctl_datamove((union ctl_io *)ctsio);
6293 
6294 		return (CTL_RETVAL_COMPLETE);
6295 	}
6296 
6297 	switch (ctsio->cdb[0]) {
6298 	case MODE_SELECT_6: {
6299 		struct scsi_mode_header_6 *mh6;
6300 
6301 		mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6302 		bd_len = mh6->blk_desc_len;
6303 		break;
6304 	}
6305 	case MODE_SELECT_10: {
6306 		struct scsi_mode_header_10 *mh10;
6307 
6308 		mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6309 		bd_len = scsi_2btoul(mh10->blk_desc_len);
6310 		break;
6311 	}
6312 	default:
6313 		panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6314 	}
6315 
6316 	if (param_len < (header_size + bd_len)) {
6317 		free(ctsio->kern_data_ptr, M_CTL);
6318 		ctl_set_param_len_error(ctsio);
6319 		ctl_done((union ctl_io *)ctsio);
6320 		return (CTL_RETVAL_COMPLETE);
6321 	}
6322 
6323 	/*
6324 	 * Set the IO_CONT flag, so that if this I/O gets passed to
6325 	 * ctl_config_write_done(), it'll get passed back to
6326 	 * ctl_do_mode_select() for further processing, or completion if
6327 	 * we're all done.
6328 	 */
6329 	ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6330 	ctsio->io_cont = ctl_do_mode_select;
6331 
6332 	modepage_info = (union ctl_modepage_info *)
6333 		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6334 	memset(modepage_info, 0, sizeof(*modepage_info));
6335 	modepage_info->header.len_left = param_len - header_size - bd_len;
6336 	modepage_info->header.len_used = header_size + bd_len;
6337 
6338 	return (ctl_do_mode_select((union ctl_io *)ctsio));
6339 }
6340 
6341 int
6342 ctl_mode_sense(struct ctl_scsiio *ctsio)
6343 {
6344 	struct ctl_lun *lun;
6345 	int pc, page_code, dbd, llba, subpage;
6346 	int alloc_len, page_len, header_len, total_len;
6347 	struct scsi_mode_block_descr *block_desc;
6348 	struct ctl_page_index *page_index;
6349 
6350 	dbd = 0;
6351 	llba = 0;
6352 	block_desc = NULL;
6353 
6354 	CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6355 
6356 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6357 	switch (ctsio->cdb[0]) {
6358 	case MODE_SENSE_6: {
6359 		struct scsi_mode_sense_6 *cdb;
6360 
6361 		cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6362 
6363 		header_len = sizeof(struct scsi_mode_hdr_6);
6364 		if (cdb->byte2 & SMS_DBD)
6365 			dbd = 1;
6366 		else
6367 			header_len += sizeof(struct scsi_mode_block_descr);
6368 
6369 		pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6370 		page_code = cdb->page & SMS_PAGE_CODE;
6371 		subpage = cdb->subpage;
6372 		alloc_len = cdb->length;
6373 		break;
6374 	}
6375 	case MODE_SENSE_10: {
6376 		struct scsi_mode_sense_10 *cdb;
6377 
6378 		cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6379 
6380 		header_len = sizeof(struct scsi_mode_hdr_10);
6381 
6382 		if (cdb->byte2 & SMS_DBD)
6383 			dbd = 1;
6384 		else
6385 			header_len += sizeof(struct scsi_mode_block_descr);
6386 		if (cdb->byte2 & SMS10_LLBAA)
6387 			llba = 1;
6388 		pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6389 		page_code = cdb->page & SMS_PAGE_CODE;
6390 		subpage = cdb->subpage;
6391 		alloc_len = scsi_2btoul(cdb->length);
6392 		break;
6393 	}
6394 	default:
6395 		ctl_set_invalid_opcode(ctsio);
6396 		ctl_done((union ctl_io *)ctsio);
6397 		return (CTL_RETVAL_COMPLETE);
6398 		break; /* NOTREACHED */
6399 	}
6400 
6401 	/*
6402 	 * We have to make a first pass through to calculate the size of
6403 	 * the pages that match the user's query.  Then we allocate enough
6404 	 * memory to hold it, and actually copy the data into the buffer.
6405 	 */
6406 	switch (page_code) {
6407 	case SMS_ALL_PAGES_PAGE: {
6408 		u_int i;
6409 
6410 		page_len = 0;
6411 
6412 		/*
6413 		 * At the moment, values other than 0 and 0xff here are
6414 		 * reserved according to SPC-3.
6415 		 */
6416 		if ((subpage != SMS_SUBPAGE_PAGE_0)
6417 		 && (subpage != SMS_SUBPAGE_ALL)) {
6418 			ctl_set_invalid_field(ctsio,
6419 					      /*sks_valid*/ 1,
6420 					      /*command*/ 1,
6421 					      /*field*/ 3,
6422 					      /*bit_valid*/ 0,
6423 					      /*bit*/ 0);
6424 			ctl_done((union ctl_io *)ctsio);
6425 			return (CTL_RETVAL_COMPLETE);
6426 		}
6427 
6428 		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6429 			page_index = &lun->mode_pages.index[i];
6430 
6431 			/* Make sure the page is supported for this dev type */
6432 			if (lun->be_lun->lun_type == T_DIRECT &&
6433 			    (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6434 				continue;
6435 			if (lun->be_lun->lun_type == T_PROCESSOR &&
6436 			    (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6437 				continue;
6438 			if (lun->be_lun->lun_type == T_CDROM &&
6439 			    (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6440 				continue;
6441 
6442 			/*
6443 			 * We don't use this subpage if the user didn't
6444 			 * request all subpages.
6445 			 */
6446 			if ((page_index->subpage != 0)
6447 			 && (subpage == SMS_SUBPAGE_PAGE_0))
6448 				continue;
6449 
6450 #if 0
6451 			printf("found page %#x len %d\n",
6452 			       page_index->page_code & SMPH_PC_MASK,
6453 			       page_index->page_len);
6454 #endif
6455 			page_len += page_index->page_len;
6456 		}
6457 		break;
6458 	}
6459 	default: {
6460 		u_int i;
6461 
6462 		page_len = 0;
6463 
6464 		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6465 			page_index = &lun->mode_pages.index[i];
6466 
6467 			/* Make sure the page is supported for this dev type */
6468 			if (lun->be_lun->lun_type == T_DIRECT &&
6469 			    (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6470 				continue;
6471 			if (lun->be_lun->lun_type == T_PROCESSOR &&
6472 			    (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6473 				continue;
6474 			if (lun->be_lun->lun_type == T_CDROM &&
6475 			    (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6476 				continue;
6477 
6478 			/* Look for the right page code */
6479 			if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6480 				continue;
6481 
6482 			/* Look for the right subpage or the subpage wildcard*/
6483 			if ((page_index->subpage != subpage)
6484 			 && (subpage != SMS_SUBPAGE_ALL))
6485 				continue;
6486 
6487 #if 0
6488 			printf("found page %#x len %d\n",
6489 			       page_index->page_code & SMPH_PC_MASK,
6490 			       page_index->page_len);
6491 #endif
6492 
6493 			page_len += page_index->page_len;
6494 		}
6495 
6496 		if (page_len == 0) {
6497 			ctl_set_invalid_field(ctsio,
6498 					      /*sks_valid*/ 1,
6499 					      /*command*/ 1,
6500 					      /*field*/ 2,
6501 					      /*bit_valid*/ 1,
6502 					      /*bit*/ 5);
6503 			ctl_done((union ctl_io *)ctsio);
6504 			return (CTL_RETVAL_COMPLETE);
6505 		}
6506 		break;
6507 	}
6508 	}
6509 
6510 	total_len = header_len + page_len;
6511 #if 0
6512 	printf("header_len = %d, page_len = %d, total_len = %d\n",
6513 	       header_len, page_len, total_len);
6514 #endif
6515 
6516 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6517 	ctsio->kern_sg_entries = 0;
6518 	ctsio->kern_data_resid = 0;
6519 	ctsio->kern_rel_offset = 0;
6520 	if (total_len < alloc_len) {
6521 		ctsio->residual = alloc_len - total_len;
6522 		ctsio->kern_data_len = total_len;
6523 		ctsio->kern_total_len = total_len;
6524 	} else {
6525 		ctsio->residual = 0;
6526 		ctsio->kern_data_len = alloc_len;
6527 		ctsio->kern_total_len = alloc_len;
6528 	}
6529 
6530 	switch (ctsio->cdb[0]) {
6531 	case MODE_SENSE_6: {
6532 		struct scsi_mode_hdr_6 *header;
6533 
6534 		header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6535 
6536 		header->datalen = MIN(total_len - 1, 254);
6537 		if (lun->be_lun->lun_type == T_DIRECT) {
6538 			header->dev_specific = 0x10; /* DPOFUA */
6539 			if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6540 			    (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6541 			    .eca_and_aen & SCP_SWP) != 0)
6542 				    header->dev_specific |= 0x80; /* WP */
6543 		}
6544 		if (dbd)
6545 			header->block_descr_len = 0;
6546 		else
6547 			header->block_descr_len =
6548 				sizeof(struct scsi_mode_block_descr);
6549 		block_desc = (struct scsi_mode_block_descr *)&header[1];
6550 		break;
6551 	}
6552 	case MODE_SENSE_10: {
6553 		struct scsi_mode_hdr_10 *header;
6554 		int datalen;
6555 
6556 		header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6557 
6558 		datalen = MIN(total_len - 2, 65533);
6559 		scsi_ulto2b(datalen, header->datalen);
6560 		if (lun->be_lun->lun_type == T_DIRECT) {
6561 			header->dev_specific = 0x10; /* DPOFUA */
6562 			if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6563 			    (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6564 			    .eca_and_aen & SCP_SWP) != 0)
6565 				    header->dev_specific |= 0x80; /* WP */
6566 		}
6567 		if (dbd)
6568 			scsi_ulto2b(0, header->block_descr_len);
6569 		else
6570 			scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6571 				    header->block_descr_len);
6572 		block_desc = (struct scsi_mode_block_descr *)&header[1];
6573 		break;
6574 	}
6575 	default:
6576 		panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6577 	}
6578 
6579 	/*
6580 	 * If we've got a disk, use its blocksize in the block
6581 	 * descriptor.  Otherwise, just set it to 0.
6582 	 */
6583 	if (dbd == 0) {
6584 		if (lun->be_lun->lun_type == T_DIRECT)
6585 			scsi_ulto3b(lun->be_lun->blocksize,
6586 				    block_desc->block_len);
6587 		else
6588 			scsi_ulto3b(0, block_desc->block_len);
6589 	}
6590 
6591 	switch (page_code) {
6592 	case SMS_ALL_PAGES_PAGE: {
6593 		int i, data_used;
6594 
6595 		data_used = header_len;
6596 		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6597 			struct ctl_page_index *page_index;
6598 
6599 			page_index = &lun->mode_pages.index[i];
6600 			if (lun->be_lun->lun_type == T_DIRECT &&
6601 			    (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6602 				continue;
6603 			if (lun->be_lun->lun_type == T_PROCESSOR &&
6604 			    (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6605 				continue;
6606 			if (lun->be_lun->lun_type == T_CDROM &&
6607 			    (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6608 				continue;
6609 
6610 			/*
6611 			 * We don't use this subpage if the user didn't
6612 			 * request all subpages.  We already checked (above)
6613 			 * to make sure the user only specified a subpage
6614 			 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6615 			 */
6616 			if ((page_index->subpage != 0)
6617 			 && (subpage == SMS_SUBPAGE_PAGE_0))
6618 				continue;
6619 
6620 			/*
6621 			 * Call the handler, if it exists, to update the
6622 			 * page to the latest values.
6623 			 */
6624 			if (page_index->sense_handler != NULL)
6625 				page_index->sense_handler(ctsio, page_index,pc);
6626 
6627 			memcpy(ctsio->kern_data_ptr + data_used,
6628 			       page_index->page_data +
6629 			       (page_index->page_len * pc),
6630 			       page_index->page_len);
6631 			data_used += page_index->page_len;
6632 		}
6633 		break;
6634 	}
6635 	default: {
6636 		int i, data_used;
6637 
6638 		data_used = header_len;
6639 
6640 		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6641 			struct ctl_page_index *page_index;
6642 
6643 			page_index = &lun->mode_pages.index[i];
6644 
6645 			/* Look for the right page code */
6646 			if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6647 				continue;
6648 
6649 			/* Look for the right subpage or the subpage wildcard*/
6650 			if ((page_index->subpage != subpage)
6651 			 && (subpage != SMS_SUBPAGE_ALL))
6652 				continue;
6653 
6654 			/* Make sure the page is supported for this dev type */
6655 			if (lun->be_lun->lun_type == T_DIRECT &&
6656 			    (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6657 				continue;
6658 			if (lun->be_lun->lun_type == T_PROCESSOR &&
6659 			    (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6660 				continue;
6661 			if (lun->be_lun->lun_type == T_CDROM &&
6662 			    (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6663 				continue;
6664 
6665 			/*
6666 			 * Call the handler, if it exists, to update the
6667 			 * page to the latest values.
6668 			 */
6669 			if (page_index->sense_handler != NULL)
6670 				page_index->sense_handler(ctsio, page_index,pc);
6671 
6672 			memcpy(ctsio->kern_data_ptr + data_used,
6673 			       page_index->page_data +
6674 			       (page_index->page_len * pc),
6675 			       page_index->page_len);
6676 			data_used += page_index->page_len;
6677 		}
6678 		break;
6679 	}
6680 	}
6681 
6682 	ctl_set_success(ctsio);
6683 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6684 	ctsio->be_move_done = ctl_config_move_done;
6685 	ctl_datamove((union ctl_io *)ctsio);
6686 	return (CTL_RETVAL_COMPLETE);
6687 }
6688 
6689 int
6690 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6691 			       struct ctl_page_index *page_index,
6692 			       int pc)
6693 {
6694 	struct ctl_lun *lun;
6695 	struct scsi_log_param_header *phdr;
6696 	uint8_t *data;
6697 	uint64_t val;
6698 
6699 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6700 	data = page_index->page_data;
6701 
6702 	if (lun->backend->lun_attr != NULL &&
6703 	    (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6704 	     != UINT64_MAX) {
6705 		phdr = (struct scsi_log_param_header *)data;
6706 		scsi_ulto2b(0x0001, phdr->param_code);
6707 		phdr->param_control = SLP_LBIN | SLP_LP;
6708 		phdr->param_len = 8;
6709 		data = (uint8_t *)(phdr + 1);
6710 		scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6711 		data[4] = 0x02; /* per-pool */
6712 		data += phdr->param_len;
6713 	}
6714 
6715 	if (lun->backend->lun_attr != NULL &&
6716 	    (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6717 	     != UINT64_MAX) {
6718 		phdr = (struct scsi_log_param_header *)data;
6719 		scsi_ulto2b(0x0002, phdr->param_code);
6720 		phdr->param_control = SLP_LBIN | SLP_LP;
6721 		phdr->param_len = 8;
6722 		data = (uint8_t *)(phdr + 1);
6723 		scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6724 		data[4] = 0x01; /* per-LUN */
6725 		data += phdr->param_len;
6726 	}
6727 
6728 	if (lun->backend->lun_attr != NULL &&
6729 	    (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6730 	     != UINT64_MAX) {
6731 		phdr = (struct scsi_log_param_header *)data;
6732 		scsi_ulto2b(0x00f1, phdr->param_code);
6733 		phdr->param_control = SLP_LBIN | SLP_LP;
6734 		phdr->param_len = 8;
6735 		data = (uint8_t *)(phdr + 1);
6736 		scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6737 		data[4] = 0x02; /* per-pool */
6738 		data += phdr->param_len;
6739 	}
6740 
6741 	if (lun->backend->lun_attr != NULL &&
6742 	    (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6743 	     != UINT64_MAX) {
6744 		phdr = (struct scsi_log_param_header *)data;
6745 		scsi_ulto2b(0x00f2, phdr->param_code);
6746 		phdr->param_control = SLP_LBIN | SLP_LP;
6747 		phdr->param_len = 8;
6748 		data = (uint8_t *)(phdr + 1);
6749 		scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6750 		data[4] = 0x02; /* per-pool */
6751 		data += phdr->param_len;
6752 	}
6753 
6754 	page_index->page_len = data - page_index->page_data;
6755 	return (0);
6756 }
6757 
6758 int
6759 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6760 			       struct ctl_page_index *page_index,
6761 			       int pc)
6762 {
6763 	struct ctl_lun *lun;
6764 	struct stat_page *data;
6765 	uint64_t rn, wn, rb, wb;
6766 	struct bintime rt, wt;
6767 	int i;
6768 
6769 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6770 	data = (struct stat_page *)page_index->page_data;
6771 
6772 	scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6773 	data->sap.hdr.param_control = SLP_LBIN;
6774 	data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6775 	    sizeof(struct scsi_log_param_header);
6776 	rn = wn = rb = wb = 0;
6777 	bintime_clear(&rt);
6778 	bintime_clear(&wt);
6779 	for (i = 0; i < CTL_MAX_PORTS; i++) {
6780 		rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6781 		wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6782 		rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6783 		wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6784 		bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6785 		bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6786 	}
6787 	scsi_u64to8b(rn, data->sap.read_num);
6788 	scsi_u64to8b(wn, data->sap.write_num);
6789 	if (lun->stats.blocksize > 0) {
6790 		scsi_u64to8b(wb / lun->stats.blocksize,
6791 		    data->sap.recvieved_lba);
6792 		scsi_u64to8b(rb / lun->stats.blocksize,
6793 		    data->sap.transmitted_lba);
6794 	}
6795 	scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6796 	    data->sap.read_int);
6797 	scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6798 	    data->sap.write_int);
6799 	scsi_u64to8b(0, data->sap.weighted_num);
6800 	scsi_u64to8b(0, data->sap.weighted_int);
6801 	scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6802 	data->it.hdr.param_control = SLP_LBIN;
6803 	data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6804 	    sizeof(struct scsi_log_param_header);
6805 #ifdef CTL_TIME_IO
6806 	scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6807 #endif
6808 	scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6809 	data->it.hdr.param_control = SLP_LBIN;
6810 	data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6811 	    sizeof(struct scsi_log_param_header);
6812 	scsi_ulto4b(3, data->ti.exponent);
6813 	scsi_ulto4b(1, data->ti.integer);
6814 
6815 	page_index->page_len = sizeof(*data);
6816 	return (0);
6817 }
6818 
6819 int
6820 ctl_log_sense(struct ctl_scsiio *ctsio)
6821 {
6822 	struct ctl_lun *lun;
6823 	int i, pc, page_code, subpage;
6824 	int alloc_len, total_len;
6825 	struct ctl_page_index *page_index;
6826 	struct scsi_log_sense *cdb;
6827 	struct scsi_log_header *header;
6828 
6829 	CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6830 
6831 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6832 	cdb = (struct scsi_log_sense *)ctsio->cdb;
6833 	pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6834 	page_code = cdb->page & SLS_PAGE_CODE;
6835 	subpage = cdb->subpage;
6836 	alloc_len = scsi_2btoul(cdb->length);
6837 
6838 	page_index = NULL;
6839 	for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6840 		page_index = &lun->log_pages.index[i];
6841 
6842 		/* Look for the right page code */
6843 		if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6844 			continue;
6845 
6846 		/* Look for the right subpage or the subpage wildcard*/
6847 		if (page_index->subpage != subpage)
6848 			continue;
6849 
6850 		break;
6851 	}
6852 	if (i >= CTL_NUM_LOG_PAGES) {
6853 		ctl_set_invalid_field(ctsio,
6854 				      /*sks_valid*/ 1,
6855 				      /*command*/ 1,
6856 				      /*field*/ 2,
6857 				      /*bit_valid*/ 0,
6858 				      /*bit*/ 0);
6859 		ctl_done((union ctl_io *)ctsio);
6860 		return (CTL_RETVAL_COMPLETE);
6861 	}
6862 
6863 	total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6864 
6865 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6866 	ctsio->kern_sg_entries = 0;
6867 	ctsio->kern_data_resid = 0;
6868 	ctsio->kern_rel_offset = 0;
6869 	if (total_len < alloc_len) {
6870 		ctsio->residual = alloc_len - total_len;
6871 		ctsio->kern_data_len = total_len;
6872 		ctsio->kern_total_len = total_len;
6873 	} else {
6874 		ctsio->residual = 0;
6875 		ctsio->kern_data_len = alloc_len;
6876 		ctsio->kern_total_len = alloc_len;
6877 	}
6878 
6879 	header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6880 	header->page = page_index->page_code;
6881 	if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6882 		header->page |= SL_DS;
6883 	if (page_index->subpage) {
6884 		header->page |= SL_SPF;
6885 		header->subpage = page_index->subpage;
6886 	}
6887 	scsi_ulto2b(page_index->page_len, header->datalen);
6888 
6889 	/*
6890 	 * Call the handler, if it exists, to update the
6891 	 * page to the latest values.
6892 	 */
6893 	if (page_index->sense_handler != NULL)
6894 		page_index->sense_handler(ctsio, page_index, pc);
6895 
6896 	memcpy(header + 1, page_index->page_data, page_index->page_len);
6897 
6898 	ctl_set_success(ctsio);
6899 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6900 	ctsio->be_move_done = ctl_config_move_done;
6901 	ctl_datamove((union ctl_io *)ctsio);
6902 	return (CTL_RETVAL_COMPLETE);
6903 }
6904 
6905 int
6906 ctl_read_capacity(struct ctl_scsiio *ctsio)
6907 {
6908 	struct scsi_read_capacity *cdb;
6909 	struct scsi_read_capacity_data *data;
6910 	struct ctl_lun *lun;
6911 	uint32_t lba;
6912 
6913 	CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6914 
6915 	cdb = (struct scsi_read_capacity *)ctsio->cdb;
6916 
6917 	lba = scsi_4btoul(cdb->addr);
6918 	if (((cdb->pmi & SRC_PMI) == 0)
6919 	 && (lba != 0)) {
6920 		ctl_set_invalid_field(/*ctsio*/ ctsio,
6921 				      /*sks_valid*/ 1,
6922 				      /*command*/ 1,
6923 				      /*field*/ 2,
6924 				      /*bit_valid*/ 0,
6925 				      /*bit*/ 0);
6926 		ctl_done((union ctl_io *)ctsio);
6927 		return (CTL_RETVAL_COMPLETE);
6928 	}
6929 
6930 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6931 
6932 	ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6933 	data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6934 	ctsio->residual = 0;
6935 	ctsio->kern_data_len = sizeof(*data);
6936 	ctsio->kern_total_len = sizeof(*data);
6937 	ctsio->kern_data_resid = 0;
6938 	ctsio->kern_rel_offset = 0;
6939 	ctsio->kern_sg_entries = 0;
6940 
6941 	/*
6942 	 * If the maximum LBA is greater than 0xfffffffe, the user must
6943 	 * issue a SERVICE ACTION IN (16) command, with the read capacity
6944 	 * serivce action set.
6945 	 */
6946 	if (lun->be_lun->maxlba > 0xfffffffe)
6947 		scsi_ulto4b(0xffffffff, data->addr);
6948 	else
6949 		scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6950 
6951 	/*
6952 	 * XXX KDM this may not be 512 bytes...
6953 	 */
6954 	scsi_ulto4b(lun->be_lun->blocksize, data->length);
6955 
6956 	ctl_set_success(ctsio);
6957 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6958 	ctsio->be_move_done = ctl_config_move_done;
6959 	ctl_datamove((union ctl_io *)ctsio);
6960 	return (CTL_RETVAL_COMPLETE);
6961 }
6962 
6963 int
6964 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6965 {
6966 	struct scsi_read_capacity_16 *cdb;
6967 	struct scsi_read_capacity_data_long *data;
6968 	struct ctl_lun *lun;
6969 	uint64_t lba;
6970 	uint32_t alloc_len;
6971 
6972 	CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6973 
6974 	cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6975 
6976 	alloc_len = scsi_4btoul(cdb->alloc_len);
6977 	lba = scsi_8btou64(cdb->addr);
6978 
6979 	if ((cdb->reladr & SRC16_PMI)
6980 	 && (lba != 0)) {
6981 		ctl_set_invalid_field(/*ctsio*/ ctsio,
6982 				      /*sks_valid*/ 1,
6983 				      /*command*/ 1,
6984 				      /*field*/ 2,
6985 				      /*bit_valid*/ 0,
6986 				      /*bit*/ 0);
6987 		ctl_done((union ctl_io *)ctsio);
6988 		return (CTL_RETVAL_COMPLETE);
6989 	}
6990 
6991 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6992 
6993 	ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6994 	data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6995 
6996 	if (sizeof(*data) < alloc_len) {
6997 		ctsio->residual = alloc_len - sizeof(*data);
6998 		ctsio->kern_data_len = sizeof(*data);
6999 		ctsio->kern_total_len = sizeof(*data);
7000 	} else {
7001 		ctsio->residual = 0;
7002 		ctsio->kern_data_len = alloc_len;
7003 		ctsio->kern_total_len = alloc_len;
7004 	}
7005 	ctsio->kern_data_resid = 0;
7006 	ctsio->kern_rel_offset = 0;
7007 	ctsio->kern_sg_entries = 0;
7008 
7009 	scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7010 	/* XXX KDM this may not be 512 bytes... */
7011 	scsi_ulto4b(lun->be_lun->blocksize, data->length);
7012 	data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7013 	scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7014 	if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7015 		data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7016 
7017 	ctl_set_success(ctsio);
7018 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7019 	ctsio->be_move_done = ctl_config_move_done;
7020 	ctl_datamove((union ctl_io *)ctsio);
7021 	return (CTL_RETVAL_COMPLETE);
7022 }
7023 
7024 int
7025 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7026 {
7027 	struct scsi_get_lba_status *cdb;
7028 	struct scsi_get_lba_status_data *data;
7029 	struct ctl_lun *lun;
7030 	struct ctl_lba_len_flags *lbalen;
7031 	uint64_t lba;
7032 	uint32_t alloc_len, total_len;
7033 	int retval;
7034 
7035 	CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7036 
7037 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7038 	cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7039 	lba = scsi_8btou64(cdb->addr);
7040 	alloc_len = scsi_4btoul(cdb->alloc_len);
7041 
7042 	if (lba > lun->be_lun->maxlba) {
7043 		ctl_set_lba_out_of_range(ctsio);
7044 		ctl_done((union ctl_io *)ctsio);
7045 		return (CTL_RETVAL_COMPLETE);
7046 	}
7047 
7048 	total_len = sizeof(*data) + sizeof(data->descr[0]);
7049 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7050 	data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7051 
7052 	if (total_len < alloc_len) {
7053 		ctsio->residual = alloc_len - total_len;
7054 		ctsio->kern_data_len = total_len;
7055 		ctsio->kern_total_len = total_len;
7056 	} else {
7057 		ctsio->residual = 0;
7058 		ctsio->kern_data_len = alloc_len;
7059 		ctsio->kern_total_len = alloc_len;
7060 	}
7061 	ctsio->kern_data_resid = 0;
7062 	ctsio->kern_rel_offset = 0;
7063 	ctsio->kern_sg_entries = 0;
7064 
7065 	/* Fill dummy data in case backend can't tell anything. */
7066 	scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7067 	scsi_u64to8b(lba, data->descr[0].addr);
7068 	scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7069 	    data->descr[0].length);
7070 	data->descr[0].status = 0; /* Mapped or unknown. */
7071 
7072 	ctl_set_success(ctsio);
7073 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7074 	ctsio->be_move_done = ctl_config_move_done;
7075 
7076 	lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7077 	lbalen->lba = lba;
7078 	lbalen->len = total_len;
7079 	lbalen->flags = 0;
7080 	retval = lun->backend->config_read((union ctl_io *)ctsio);
7081 	return (CTL_RETVAL_COMPLETE);
7082 }
7083 
7084 int
7085 ctl_read_defect(struct ctl_scsiio *ctsio)
7086 {
7087 	struct scsi_read_defect_data_10 *ccb10;
7088 	struct scsi_read_defect_data_12 *ccb12;
7089 	struct scsi_read_defect_data_hdr_10 *data10;
7090 	struct scsi_read_defect_data_hdr_12 *data12;
7091 	uint32_t alloc_len, data_len;
7092 	uint8_t format;
7093 
7094 	CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7095 
7096 	if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7097 		ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7098 		format = ccb10->format;
7099 		alloc_len = scsi_2btoul(ccb10->alloc_length);
7100 		data_len = sizeof(*data10);
7101 	} else {
7102 		ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7103 		format = ccb12->format;
7104 		alloc_len = scsi_4btoul(ccb12->alloc_length);
7105 		data_len = sizeof(*data12);
7106 	}
7107 	if (alloc_len == 0) {
7108 		ctl_set_success(ctsio);
7109 		ctl_done((union ctl_io *)ctsio);
7110 		return (CTL_RETVAL_COMPLETE);
7111 	}
7112 
7113 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7114 	if (data_len < alloc_len) {
7115 		ctsio->residual = alloc_len - data_len;
7116 		ctsio->kern_data_len = data_len;
7117 		ctsio->kern_total_len = data_len;
7118 	} else {
7119 		ctsio->residual = 0;
7120 		ctsio->kern_data_len = alloc_len;
7121 		ctsio->kern_total_len = alloc_len;
7122 	}
7123 	ctsio->kern_data_resid = 0;
7124 	ctsio->kern_rel_offset = 0;
7125 	ctsio->kern_sg_entries = 0;
7126 
7127 	if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7128 		data10 = (struct scsi_read_defect_data_hdr_10 *)
7129 		    ctsio->kern_data_ptr;
7130 		data10->format = format;
7131 		scsi_ulto2b(0, data10->length);
7132 	} else {
7133 		data12 = (struct scsi_read_defect_data_hdr_12 *)
7134 		    ctsio->kern_data_ptr;
7135 		data12->format = format;
7136 		scsi_ulto2b(0, data12->generation);
7137 		scsi_ulto4b(0, data12->length);
7138 	}
7139 
7140 	ctl_set_success(ctsio);
7141 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7142 	ctsio->be_move_done = ctl_config_move_done;
7143 	ctl_datamove((union ctl_io *)ctsio);
7144 	return (CTL_RETVAL_COMPLETE);
7145 }
7146 
7147 int
7148 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7149 {
7150 	struct scsi_maintenance_in *cdb;
7151 	int retval;
7152 	int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7153 	int num_ha_groups, num_target_ports, shared_group;
7154 	struct ctl_lun *lun;
7155 	struct ctl_softc *softc;
7156 	struct ctl_port *port;
7157 	struct scsi_target_group_data *rtg_ptr;
7158 	struct scsi_target_group_data_extended *rtg_ext_ptr;
7159 	struct scsi_target_port_group_descriptor *tpg_desc;
7160 
7161 	CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7162 
7163 	cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7164 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7165 	softc = lun->ctl_softc;
7166 
7167 	retval = CTL_RETVAL_COMPLETE;
7168 
7169 	switch (cdb->byte2 & STG_PDF_MASK) {
7170 	case STG_PDF_LENGTH:
7171 		ext = 0;
7172 		break;
7173 	case STG_PDF_EXTENDED:
7174 		ext = 1;
7175 		break;
7176 	default:
7177 		ctl_set_invalid_field(/*ctsio*/ ctsio,
7178 				      /*sks_valid*/ 1,
7179 				      /*command*/ 1,
7180 				      /*field*/ 2,
7181 				      /*bit_valid*/ 1,
7182 				      /*bit*/ 5);
7183 		ctl_done((union ctl_io *)ctsio);
7184 		return(retval);
7185 	}
7186 
7187 	num_target_ports = 0;
7188 	shared_group = (softc->is_single != 0);
7189 	mtx_lock(&softc->ctl_lock);
7190 	STAILQ_FOREACH(port, &softc->port_list, links) {
7191 		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7192 			continue;
7193 		if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7194 			continue;
7195 		num_target_ports++;
7196 		if (port->status & CTL_PORT_STATUS_HA_SHARED)
7197 			shared_group = 1;
7198 	}
7199 	mtx_unlock(&softc->ctl_lock);
7200 	num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7201 
7202 	if (ext)
7203 		total_len = sizeof(struct scsi_target_group_data_extended);
7204 	else
7205 		total_len = sizeof(struct scsi_target_group_data);
7206 	total_len += sizeof(struct scsi_target_port_group_descriptor) *
7207 		(shared_group + num_ha_groups) +
7208 	    sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7209 
7210 	alloc_len = scsi_4btoul(cdb->length);
7211 
7212 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7213 
7214 	ctsio->kern_sg_entries = 0;
7215 
7216 	if (total_len < alloc_len) {
7217 		ctsio->residual = alloc_len - total_len;
7218 		ctsio->kern_data_len = total_len;
7219 		ctsio->kern_total_len = total_len;
7220 	} else {
7221 		ctsio->residual = 0;
7222 		ctsio->kern_data_len = alloc_len;
7223 		ctsio->kern_total_len = alloc_len;
7224 	}
7225 	ctsio->kern_data_resid = 0;
7226 	ctsio->kern_rel_offset = 0;
7227 
7228 	if (ext) {
7229 		rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7230 		    ctsio->kern_data_ptr;
7231 		scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7232 		rtg_ext_ptr->format_type = 0x10;
7233 		rtg_ext_ptr->implicit_transition_time = 0;
7234 		tpg_desc = &rtg_ext_ptr->groups[0];
7235 	} else {
7236 		rtg_ptr = (struct scsi_target_group_data *)
7237 		    ctsio->kern_data_ptr;
7238 		scsi_ulto4b(total_len - 4, rtg_ptr->length);
7239 		tpg_desc = &rtg_ptr->groups[0];
7240 	}
7241 
7242 	mtx_lock(&softc->ctl_lock);
7243 	pg = softc->port_min / softc->port_cnt;
7244 	if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7245 		/* Some shelf is known to be primary. */
7246 		if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7247 			os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7248 		else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7249 			os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7250 		else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7251 			os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7252 		else
7253 			os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7254 		if (lun->flags & CTL_LUN_PRIMARY_SC) {
7255 			ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7256 		} else {
7257 			ts = os;
7258 			os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7259 		}
7260 	} else {
7261 		/* No known primary shelf. */
7262 		if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7263 			ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7264 			os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7265 		} else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7266 			ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7267 			os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7268 		} else {
7269 			ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7270 		}
7271 	}
7272 	if (shared_group) {
7273 		tpg_desc->pref_state = ts;
7274 		tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7275 		    TPG_U_SUP | TPG_T_SUP;
7276 		scsi_ulto2b(1, tpg_desc->target_port_group);
7277 		tpg_desc->status = TPG_IMPLICIT;
7278 		pc = 0;
7279 		STAILQ_FOREACH(port, &softc->port_list, links) {
7280 			if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7281 				continue;
7282 			if (!softc->is_single &&
7283 			    (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7284 				continue;
7285 			if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7286 				continue;
7287 			scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7288 			    relative_target_port_identifier);
7289 			pc++;
7290 		}
7291 		tpg_desc->target_port_count = pc;
7292 		tpg_desc = (struct scsi_target_port_group_descriptor *)
7293 		    &tpg_desc->descriptors[pc];
7294 	}
7295 	for (g = 0; g < num_ha_groups; g++) {
7296 		tpg_desc->pref_state = (g == pg) ? ts : os;
7297 		tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7298 		    TPG_U_SUP | TPG_T_SUP;
7299 		scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7300 		tpg_desc->status = TPG_IMPLICIT;
7301 		pc = 0;
7302 		STAILQ_FOREACH(port, &softc->port_list, links) {
7303 			if (port->targ_port < g * softc->port_cnt ||
7304 			    port->targ_port >= (g + 1) * softc->port_cnt)
7305 				continue;
7306 			if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7307 				continue;
7308 			if (port->status & CTL_PORT_STATUS_HA_SHARED)
7309 				continue;
7310 			if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7311 				continue;
7312 			scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7313 			    relative_target_port_identifier);
7314 			pc++;
7315 		}
7316 		tpg_desc->target_port_count = pc;
7317 		tpg_desc = (struct scsi_target_port_group_descriptor *)
7318 		    &tpg_desc->descriptors[pc];
7319 	}
7320 	mtx_unlock(&softc->ctl_lock);
7321 
7322 	ctl_set_success(ctsio);
7323 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7324 	ctsio->be_move_done = ctl_config_move_done;
7325 	ctl_datamove((union ctl_io *)ctsio);
7326 	return(retval);
7327 }
7328 
7329 int
7330 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7331 {
7332 	struct ctl_lun *lun;
7333 	struct scsi_report_supported_opcodes *cdb;
7334 	const struct ctl_cmd_entry *entry, *sentry;
7335 	struct scsi_report_supported_opcodes_all *all;
7336 	struct scsi_report_supported_opcodes_descr *descr;
7337 	struct scsi_report_supported_opcodes_one *one;
7338 	int retval;
7339 	int alloc_len, total_len;
7340 	int opcode, service_action, i, j, num;
7341 
7342 	CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7343 
7344 	cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7345 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7346 
7347 	retval = CTL_RETVAL_COMPLETE;
7348 
7349 	opcode = cdb->requested_opcode;
7350 	service_action = scsi_2btoul(cdb->requested_service_action);
7351 	switch (cdb->options & RSO_OPTIONS_MASK) {
7352 	case RSO_OPTIONS_ALL:
7353 		num = 0;
7354 		for (i = 0; i < 256; i++) {
7355 			entry = &ctl_cmd_table[i];
7356 			if (entry->flags & CTL_CMD_FLAG_SA5) {
7357 				for (j = 0; j < 32; j++) {
7358 					sentry = &((const struct ctl_cmd_entry *)
7359 					    entry->execute)[j];
7360 					if (ctl_cmd_applicable(
7361 					    lun->be_lun->lun_type, sentry))
7362 						num++;
7363 				}
7364 			} else {
7365 				if (ctl_cmd_applicable(lun->be_lun->lun_type,
7366 				    entry))
7367 					num++;
7368 			}
7369 		}
7370 		total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7371 		    num * sizeof(struct scsi_report_supported_opcodes_descr);
7372 		break;
7373 	case RSO_OPTIONS_OC:
7374 		if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7375 			ctl_set_invalid_field(/*ctsio*/ ctsio,
7376 					      /*sks_valid*/ 1,
7377 					      /*command*/ 1,
7378 					      /*field*/ 2,
7379 					      /*bit_valid*/ 1,
7380 					      /*bit*/ 2);
7381 			ctl_done((union ctl_io *)ctsio);
7382 			return (CTL_RETVAL_COMPLETE);
7383 		}
7384 		total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7385 		break;
7386 	case RSO_OPTIONS_OC_SA:
7387 		if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7388 		    service_action >= 32) {
7389 			ctl_set_invalid_field(/*ctsio*/ ctsio,
7390 					      /*sks_valid*/ 1,
7391 					      /*command*/ 1,
7392 					      /*field*/ 2,
7393 					      /*bit_valid*/ 1,
7394 					      /*bit*/ 2);
7395 			ctl_done((union ctl_io *)ctsio);
7396 			return (CTL_RETVAL_COMPLETE);
7397 		}
7398 		total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7399 		break;
7400 	default:
7401 		ctl_set_invalid_field(/*ctsio*/ ctsio,
7402 				      /*sks_valid*/ 1,
7403 				      /*command*/ 1,
7404 				      /*field*/ 2,
7405 				      /*bit_valid*/ 1,
7406 				      /*bit*/ 2);
7407 		ctl_done((union ctl_io *)ctsio);
7408 		return (CTL_RETVAL_COMPLETE);
7409 	}
7410 
7411 	alloc_len = scsi_4btoul(cdb->length);
7412 
7413 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7414 
7415 	ctsio->kern_sg_entries = 0;
7416 
7417 	if (total_len < alloc_len) {
7418 		ctsio->residual = alloc_len - total_len;
7419 		ctsio->kern_data_len = total_len;
7420 		ctsio->kern_total_len = total_len;
7421 	} else {
7422 		ctsio->residual = 0;
7423 		ctsio->kern_data_len = alloc_len;
7424 		ctsio->kern_total_len = alloc_len;
7425 	}
7426 	ctsio->kern_data_resid = 0;
7427 	ctsio->kern_rel_offset = 0;
7428 
7429 	switch (cdb->options & RSO_OPTIONS_MASK) {
7430 	case RSO_OPTIONS_ALL:
7431 		all = (struct scsi_report_supported_opcodes_all *)
7432 		    ctsio->kern_data_ptr;
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 						continue;
7443 					descr = &all->descr[num++];
7444 					descr->opcode = i;
7445 					scsi_ulto2b(j, descr->service_action);
7446 					descr->flags = RSO_SERVACTV;
7447 					scsi_ulto2b(sentry->length,
7448 					    descr->cdb_length);
7449 				}
7450 			} else {
7451 				if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7452 				    entry))
7453 					continue;
7454 				descr = &all->descr[num++];
7455 				descr->opcode = i;
7456 				scsi_ulto2b(0, descr->service_action);
7457 				descr->flags = 0;
7458 				scsi_ulto2b(entry->length, descr->cdb_length);
7459 			}
7460 		}
7461 		scsi_ulto4b(
7462 		    num * sizeof(struct scsi_report_supported_opcodes_descr),
7463 		    all->length);
7464 		break;
7465 	case RSO_OPTIONS_OC:
7466 		one = (struct scsi_report_supported_opcodes_one *)
7467 		    ctsio->kern_data_ptr;
7468 		entry = &ctl_cmd_table[opcode];
7469 		goto fill_one;
7470 	case RSO_OPTIONS_OC_SA:
7471 		one = (struct scsi_report_supported_opcodes_one *)
7472 		    ctsio->kern_data_ptr;
7473 		entry = &ctl_cmd_table[opcode];
7474 		entry = &((const struct ctl_cmd_entry *)
7475 		    entry->execute)[service_action];
7476 fill_one:
7477 		if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7478 			one->support = 3;
7479 			scsi_ulto2b(entry->length, one->cdb_length);
7480 			one->cdb_usage[0] = opcode;
7481 			memcpy(&one->cdb_usage[1], entry->usage,
7482 			    entry->length - 1);
7483 		} else
7484 			one->support = 1;
7485 		break;
7486 	}
7487 
7488 	ctl_set_success(ctsio);
7489 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7490 	ctsio->be_move_done = ctl_config_move_done;
7491 	ctl_datamove((union ctl_io *)ctsio);
7492 	return(retval);
7493 }
7494 
7495 int
7496 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7497 {
7498 	struct scsi_report_supported_tmf *cdb;
7499 	struct scsi_report_supported_tmf_data *data;
7500 	int retval;
7501 	int alloc_len, total_len;
7502 
7503 	CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7504 
7505 	cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7506 
7507 	retval = CTL_RETVAL_COMPLETE;
7508 
7509 	total_len = sizeof(struct scsi_report_supported_tmf_data);
7510 	alloc_len = scsi_4btoul(cdb->length);
7511 
7512 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7513 
7514 	ctsio->kern_sg_entries = 0;
7515 
7516 	if (total_len < alloc_len) {
7517 		ctsio->residual = alloc_len - total_len;
7518 		ctsio->kern_data_len = total_len;
7519 		ctsio->kern_total_len = total_len;
7520 	} else {
7521 		ctsio->residual = 0;
7522 		ctsio->kern_data_len = alloc_len;
7523 		ctsio->kern_total_len = alloc_len;
7524 	}
7525 	ctsio->kern_data_resid = 0;
7526 	ctsio->kern_rel_offset = 0;
7527 
7528 	data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7529 	data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7530 	    RST_TRS;
7531 	data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7532 
7533 	ctl_set_success(ctsio);
7534 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7535 	ctsio->be_move_done = ctl_config_move_done;
7536 	ctl_datamove((union ctl_io *)ctsio);
7537 	return (retval);
7538 }
7539 
7540 int
7541 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7542 {
7543 	struct scsi_report_timestamp *cdb;
7544 	struct scsi_report_timestamp_data *data;
7545 	struct timeval tv;
7546 	int64_t timestamp;
7547 	int retval;
7548 	int alloc_len, total_len;
7549 
7550 	CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7551 
7552 	cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7553 
7554 	retval = CTL_RETVAL_COMPLETE;
7555 
7556 	total_len = sizeof(struct scsi_report_timestamp_data);
7557 	alloc_len = scsi_4btoul(cdb->length);
7558 
7559 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7560 
7561 	ctsio->kern_sg_entries = 0;
7562 
7563 	if (total_len < alloc_len) {
7564 		ctsio->residual = alloc_len - total_len;
7565 		ctsio->kern_data_len = total_len;
7566 		ctsio->kern_total_len = total_len;
7567 	} else {
7568 		ctsio->residual = 0;
7569 		ctsio->kern_data_len = alloc_len;
7570 		ctsio->kern_total_len = alloc_len;
7571 	}
7572 	ctsio->kern_data_resid = 0;
7573 	ctsio->kern_rel_offset = 0;
7574 
7575 	data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7576 	scsi_ulto2b(sizeof(*data) - 2, data->length);
7577 	data->origin = RTS_ORIG_OUTSIDE;
7578 	getmicrotime(&tv);
7579 	timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7580 	scsi_ulto4b(timestamp >> 16, data->timestamp);
7581 	scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7582 
7583 	ctl_set_success(ctsio);
7584 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7585 	ctsio->be_move_done = ctl_config_move_done;
7586 	ctl_datamove((union ctl_io *)ctsio);
7587 	return (retval);
7588 }
7589 
7590 int
7591 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7592 {
7593 	struct scsi_per_res_in *cdb;
7594 	int alloc_len, total_len = 0;
7595 	/* struct scsi_per_res_in_rsrv in_data; */
7596 	struct ctl_lun *lun;
7597 	struct ctl_softc *softc;
7598 	uint64_t key;
7599 
7600 	CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7601 
7602 	cdb = (struct scsi_per_res_in *)ctsio->cdb;
7603 
7604 	alloc_len = scsi_2btoul(cdb->length);
7605 
7606 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7607 	softc = lun->ctl_softc;
7608 
7609 retry:
7610 	mtx_lock(&lun->lun_lock);
7611 	switch (cdb->action) {
7612 	case SPRI_RK: /* read keys */
7613 		total_len = sizeof(struct scsi_per_res_in_keys) +
7614 			lun->pr_key_count *
7615 			sizeof(struct scsi_per_res_key);
7616 		break;
7617 	case SPRI_RR: /* read reservation */
7618 		if (lun->flags & CTL_LUN_PR_RESERVED)
7619 			total_len = sizeof(struct scsi_per_res_in_rsrv);
7620 		else
7621 			total_len = sizeof(struct scsi_per_res_in_header);
7622 		break;
7623 	case SPRI_RC: /* report capabilities */
7624 		total_len = sizeof(struct scsi_per_res_cap);
7625 		break;
7626 	case SPRI_RS: /* read full status */
7627 		total_len = sizeof(struct scsi_per_res_in_header) +
7628 		    (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7629 		    lun->pr_key_count;
7630 		break;
7631 	default:
7632 		panic("%s: Invalid PR type %#x", __func__, cdb->action);
7633 	}
7634 	mtx_unlock(&lun->lun_lock);
7635 
7636 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7637 
7638 	if (total_len < alloc_len) {
7639 		ctsio->residual = alloc_len - total_len;
7640 		ctsio->kern_data_len = total_len;
7641 		ctsio->kern_total_len = total_len;
7642 	} else {
7643 		ctsio->residual = 0;
7644 		ctsio->kern_data_len = alloc_len;
7645 		ctsio->kern_total_len = alloc_len;
7646 	}
7647 
7648 	ctsio->kern_data_resid = 0;
7649 	ctsio->kern_rel_offset = 0;
7650 	ctsio->kern_sg_entries = 0;
7651 
7652 	mtx_lock(&lun->lun_lock);
7653 	switch (cdb->action) {
7654 	case SPRI_RK: { // read keys
7655         struct scsi_per_res_in_keys *res_keys;
7656 		int i, key_count;
7657 
7658 		res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7659 
7660 		/*
7661 		 * We had to drop the lock to allocate our buffer, which
7662 		 * leaves time for someone to come in with another
7663 		 * persistent reservation.  (That is unlikely, though,
7664 		 * since this should be the only persistent reservation
7665 		 * command active right now.)
7666 		 */
7667 		if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7668 		    (lun->pr_key_count *
7669 		     sizeof(struct scsi_per_res_key)))){
7670 			mtx_unlock(&lun->lun_lock);
7671 			free(ctsio->kern_data_ptr, M_CTL);
7672 			printf("%s: reservation length changed, retrying\n",
7673 			       __func__);
7674 			goto retry;
7675 		}
7676 
7677 		scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7678 
7679 		scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7680 			     lun->pr_key_count, res_keys->header.length);
7681 
7682 		for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7683 			if ((key = ctl_get_prkey(lun, i)) == 0)
7684 				continue;
7685 
7686 			/*
7687 			 * We used lun->pr_key_count to calculate the
7688 			 * size to allocate.  If it turns out the number of
7689 			 * initiators with the registered flag set is
7690 			 * larger than that (i.e. they haven't been kept in
7691 			 * sync), we've got a problem.
7692 			 */
7693 			if (key_count >= lun->pr_key_count) {
7694 				key_count++;
7695 				continue;
7696 			}
7697 			scsi_u64to8b(key, res_keys->keys[key_count].key);
7698 			key_count++;
7699 		}
7700 		break;
7701 	}
7702 	case SPRI_RR: { // read reservation
7703 		struct scsi_per_res_in_rsrv *res;
7704 		int tmp_len, header_only;
7705 
7706 		res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7707 
7708 		scsi_ulto4b(lun->pr_generation, res->header.generation);
7709 
7710 		if (lun->flags & CTL_LUN_PR_RESERVED)
7711 		{
7712 			tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7713 			scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7714 				    res->header.length);
7715 			header_only = 0;
7716 		} else {
7717 			tmp_len = sizeof(struct scsi_per_res_in_header);
7718 			scsi_ulto4b(0, res->header.length);
7719 			header_only = 1;
7720 		}
7721 
7722 		/*
7723 		 * We had to drop the lock to allocate our buffer, which
7724 		 * leaves time for someone to come in with another
7725 		 * persistent reservation.  (That is unlikely, though,
7726 		 * since this should be the only persistent reservation
7727 		 * command active right now.)
7728 		 */
7729 		if (tmp_len != total_len) {
7730 			mtx_unlock(&lun->lun_lock);
7731 			free(ctsio->kern_data_ptr, M_CTL);
7732 			printf("%s: reservation status changed, retrying\n",
7733 			       __func__);
7734 			goto retry;
7735 		}
7736 
7737 		/*
7738 		 * No reservation held, so we're done.
7739 		 */
7740 		if (header_only != 0)
7741 			break;
7742 
7743 		/*
7744 		 * If the registration is an All Registrants type, the key
7745 		 * is 0, since it doesn't really matter.
7746 		 */
7747 		if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7748 			scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7749 			    res->data.reservation);
7750 		}
7751 		res->data.scopetype = lun->pr_res_type;
7752 		break;
7753 	}
7754 	case SPRI_RC:     //report capabilities
7755 	{
7756 		struct scsi_per_res_cap *res_cap;
7757 		uint16_t type_mask;
7758 
7759 		res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7760 		scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7761 		res_cap->flags1 = SPRI_CRH;
7762 		res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7763 		type_mask = SPRI_TM_WR_EX_AR |
7764 			    SPRI_TM_EX_AC_RO |
7765 			    SPRI_TM_WR_EX_RO |
7766 			    SPRI_TM_EX_AC |
7767 			    SPRI_TM_WR_EX |
7768 			    SPRI_TM_EX_AC_AR;
7769 		scsi_ulto2b(type_mask, res_cap->type_mask);
7770 		break;
7771 	}
7772 	case SPRI_RS: { // read full status
7773 		struct scsi_per_res_in_full *res_status;
7774 		struct scsi_per_res_in_full_desc *res_desc;
7775 		struct ctl_port *port;
7776 		int i, len;
7777 
7778 		res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7779 
7780 		/*
7781 		 * We had to drop the lock to allocate our buffer, which
7782 		 * leaves time for someone to come in with another
7783 		 * persistent reservation.  (That is unlikely, though,
7784 		 * since this should be the only persistent reservation
7785 		 * command active right now.)
7786 		 */
7787 		if (total_len < (sizeof(struct scsi_per_res_in_header) +
7788 		    (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7789 		     lun->pr_key_count)){
7790 			mtx_unlock(&lun->lun_lock);
7791 			free(ctsio->kern_data_ptr, M_CTL);
7792 			printf("%s: reservation length changed, retrying\n",
7793 			       __func__);
7794 			goto retry;
7795 		}
7796 
7797 		scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7798 
7799 		res_desc = &res_status->desc[0];
7800 		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7801 			if ((key = ctl_get_prkey(lun, i)) == 0)
7802 				continue;
7803 
7804 			scsi_u64to8b(key, res_desc->res_key.key);
7805 			if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7806 			    (lun->pr_res_idx == i ||
7807 			     lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7808 				res_desc->flags = SPRI_FULL_R_HOLDER;
7809 				res_desc->scopetype = lun->pr_res_type;
7810 			}
7811 			scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7812 			    res_desc->rel_trgt_port_id);
7813 			len = 0;
7814 			port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7815 			if (port != NULL)
7816 				len = ctl_create_iid(port,
7817 				    i % CTL_MAX_INIT_PER_PORT,
7818 				    res_desc->transport_id);
7819 			scsi_ulto4b(len, res_desc->additional_length);
7820 			res_desc = (struct scsi_per_res_in_full_desc *)
7821 			    &res_desc->transport_id[len];
7822 		}
7823 		scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7824 		    res_status->header.length);
7825 		break;
7826 	}
7827 	default:
7828 		panic("%s: Invalid PR type %#x", __func__, cdb->action);
7829 	}
7830 	mtx_unlock(&lun->lun_lock);
7831 
7832 	ctl_set_success(ctsio);
7833 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7834 	ctsio->be_move_done = ctl_config_move_done;
7835 	ctl_datamove((union ctl_io *)ctsio);
7836 	return (CTL_RETVAL_COMPLETE);
7837 }
7838 
7839 /*
7840  * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7841  * it should return.
7842  */
7843 static int
7844 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7845 		uint64_t sa_res_key, uint8_t type, uint32_t residx,
7846 		struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7847 		struct scsi_per_res_out_parms* param)
7848 {
7849 	union ctl_ha_msg persis_io;
7850 	int i;
7851 
7852 	mtx_lock(&lun->lun_lock);
7853 	if (sa_res_key == 0) {
7854 		if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7855 			/* validate scope and type */
7856 			if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7857 			     SPR_LU_SCOPE) {
7858 				mtx_unlock(&lun->lun_lock);
7859 				ctl_set_invalid_field(/*ctsio*/ ctsio,
7860 						      /*sks_valid*/ 1,
7861 						      /*command*/ 1,
7862 						      /*field*/ 2,
7863 						      /*bit_valid*/ 1,
7864 						      /*bit*/ 4);
7865 				ctl_done((union ctl_io *)ctsio);
7866 				return (1);
7867 			}
7868 
7869 		        if (type>8 || type==2 || type==4 || type==0) {
7870 				mtx_unlock(&lun->lun_lock);
7871 				ctl_set_invalid_field(/*ctsio*/ ctsio,
7872        	           				      /*sks_valid*/ 1,
7873 						      /*command*/ 1,
7874 						      /*field*/ 2,
7875 						      /*bit_valid*/ 1,
7876 						      /*bit*/ 0);
7877 				ctl_done((union ctl_io *)ctsio);
7878 				return (1);
7879 		        }
7880 
7881 			/*
7882 			 * Unregister everybody else and build UA for
7883 			 * them
7884 			 */
7885 			for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7886 				if (i == residx || ctl_get_prkey(lun, i) == 0)
7887 					continue;
7888 
7889 				ctl_clr_prkey(lun, i);
7890 				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7891 			}
7892 			lun->pr_key_count = 1;
7893 			lun->pr_res_type = type;
7894 			if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7895 			    lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7896 				lun->pr_res_idx = residx;
7897 			lun->pr_generation++;
7898 			mtx_unlock(&lun->lun_lock);
7899 
7900 			/* send msg to other side */
7901 			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7902 			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7903 			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7904 			persis_io.pr.pr_info.residx = lun->pr_res_idx;
7905 			persis_io.pr.pr_info.res_type = type;
7906 			memcpy(persis_io.pr.pr_info.sa_res_key,
7907 			       param->serv_act_res_key,
7908 			       sizeof(param->serv_act_res_key));
7909 			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7910 			    sizeof(persis_io.pr), M_WAITOK);
7911 		} else {
7912 			/* not all registrants */
7913 			mtx_unlock(&lun->lun_lock);
7914 			free(ctsio->kern_data_ptr, M_CTL);
7915 			ctl_set_invalid_field(ctsio,
7916 					      /*sks_valid*/ 1,
7917 					      /*command*/ 0,
7918 					      /*field*/ 8,
7919 					      /*bit_valid*/ 0,
7920 					      /*bit*/ 0);
7921 			ctl_done((union ctl_io *)ctsio);
7922 			return (1);
7923 		}
7924 	} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7925 		|| !(lun->flags & CTL_LUN_PR_RESERVED)) {
7926 		int found = 0;
7927 
7928 		if (res_key == sa_res_key) {
7929 			/* special case */
7930 			/*
7931 			 * The spec implies this is not good but doesn't
7932 			 * say what to do. There are two choices either
7933 			 * generate a res conflict or check condition
7934 			 * with illegal field in parameter data. Since
7935 			 * that is what is done when the sa_res_key is
7936 			 * zero I'll take that approach since this has
7937 			 * to do with the sa_res_key.
7938 			 */
7939 			mtx_unlock(&lun->lun_lock);
7940 			free(ctsio->kern_data_ptr, M_CTL);
7941 			ctl_set_invalid_field(ctsio,
7942 					      /*sks_valid*/ 1,
7943 					      /*command*/ 0,
7944 					      /*field*/ 8,
7945 					      /*bit_valid*/ 0,
7946 					      /*bit*/ 0);
7947 			ctl_done((union ctl_io *)ctsio);
7948 			return (1);
7949 		}
7950 
7951 		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7952 			if (ctl_get_prkey(lun, i) != sa_res_key)
7953 				continue;
7954 
7955 			found = 1;
7956 			ctl_clr_prkey(lun, i);
7957 			lun->pr_key_count--;
7958 			ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7959 		}
7960 		if (!found) {
7961 			mtx_unlock(&lun->lun_lock);
7962 			free(ctsio->kern_data_ptr, M_CTL);
7963 			ctl_set_reservation_conflict(ctsio);
7964 			ctl_done((union ctl_io *)ctsio);
7965 			return (CTL_RETVAL_COMPLETE);
7966 		}
7967 		lun->pr_generation++;
7968 		mtx_unlock(&lun->lun_lock);
7969 
7970 		/* send msg to other side */
7971 		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7972 		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7973 		persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7974 		persis_io.pr.pr_info.residx = lun->pr_res_idx;
7975 		persis_io.pr.pr_info.res_type = type;
7976 		memcpy(persis_io.pr.pr_info.sa_res_key,
7977 		       param->serv_act_res_key,
7978 		       sizeof(param->serv_act_res_key));
7979 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7980 		    sizeof(persis_io.pr), M_WAITOK);
7981 	} else {
7982 		/* Reserved but not all registrants */
7983 		/* sa_res_key is res holder */
7984 		if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7985 			/* validate scope and type */
7986 			if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7987 			     SPR_LU_SCOPE) {
7988 				mtx_unlock(&lun->lun_lock);
7989 				ctl_set_invalid_field(/*ctsio*/ ctsio,
7990 						      /*sks_valid*/ 1,
7991 						      /*command*/ 1,
7992 						      /*field*/ 2,
7993 						      /*bit_valid*/ 1,
7994 						      /*bit*/ 4);
7995 				ctl_done((union ctl_io *)ctsio);
7996 				return (1);
7997 			}
7998 
7999 			if (type>8 || type==2 || type==4 || type==0) {
8000 				mtx_unlock(&lun->lun_lock);
8001 				ctl_set_invalid_field(/*ctsio*/ ctsio,
8002 						      /*sks_valid*/ 1,
8003 						      /*command*/ 1,
8004 						      /*field*/ 2,
8005 						      /*bit_valid*/ 1,
8006 						      /*bit*/ 0);
8007 				ctl_done((union ctl_io *)ctsio);
8008 				return (1);
8009 			}
8010 
8011 			/*
8012 			 * Do the following:
8013 			 * if sa_res_key != res_key remove all
8014 			 * registrants w/sa_res_key and generate UA
8015 			 * for these registrants(Registrations
8016 			 * Preempted) if it wasn't an exclusive
8017 			 * reservation generate UA(Reservations
8018 			 * Preempted) for all other registered nexuses
8019 			 * if the type has changed. Establish the new
8020 			 * reservation and holder. If res_key and
8021 			 * sa_res_key are the same do the above
8022 			 * except don't unregister the res holder.
8023 			 */
8024 
8025 			for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8026 				if (i == residx || ctl_get_prkey(lun, i) == 0)
8027 					continue;
8028 
8029 				if (sa_res_key == ctl_get_prkey(lun, i)) {
8030 					ctl_clr_prkey(lun, i);
8031 					lun->pr_key_count--;
8032 					ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8033 				} else if (type != lun->pr_res_type &&
8034 				    (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8035 				     lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8036 					ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8037 				}
8038 			}
8039 			lun->pr_res_type = type;
8040 			if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8041 			    lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8042 				lun->pr_res_idx = residx;
8043 			else
8044 				lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8045 			lun->pr_generation++;
8046 			mtx_unlock(&lun->lun_lock);
8047 
8048 			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8049 			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8050 			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8051 			persis_io.pr.pr_info.residx = lun->pr_res_idx;
8052 			persis_io.pr.pr_info.res_type = type;
8053 			memcpy(persis_io.pr.pr_info.sa_res_key,
8054 			       param->serv_act_res_key,
8055 			       sizeof(param->serv_act_res_key));
8056 			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8057 			    sizeof(persis_io.pr), M_WAITOK);
8058 		} else {
8059 			/*
8060 			 * sa_res_key is not the res holder just
8061 			 * remove registrants
8062 			 */
8063 			int found=0;
8064 
8065 			for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8066 				if (sa_res_key != ctl_get_prkey(lun, i))
8067 					continue;
8068 
8069 				found = 1;
8070 				ctl_clr_prkey(lun, i);
8071 				lun->pr_key_count--;
8072 				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8073 			}
8074 
8075 			if (!found) {
8076 				mtx_unlock(&lun->lun_lock);
8077 				free(ctsio->kern_data_ptr, M_CTL);
8078 				ctl_set_reservation_conflict(ctsio);
8079 				ctl_done((union ctl_io *)ctsio);
8080 		        	return (1);
8081 			}
8082 			lun->pr_generation++;
8083 			mtx_unlock(&lun->lun_lock);
8084 
8085 			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8086 			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8087 			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8088 			persis_io.pr.pr_info.residx = lun->pr_res_idx;
8089 			persis_io.pr.pr_info.res_type = type;
8090 			memcpy(persis_io.pr.pr_info.sa_res_key,
8091 			       param->serv_act_res_key,
8092 			       sizeof(param->serv_act_res_key));
8093 			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8094 			    sizeof(persis_io.pr), M_WAITOK);
8095 		}
8096 	}
8097 	return (0);
8098 }
8099 
8100 static void
8101 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8102 {
8103 	uint64_t sa_res_key;
8104 	int i;
8105 
8106 	sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8107 
8108 	if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8109 	 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8110 	 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8111 		if (sa_res_key == 0) {
8112 			/*
8113 			 * Unregister everybody else and build UA for
8114 			 * them
8115 			 */
8116 			for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8117 				if (i == msg->pr.pr_info.residx ||
8118 				    ctl_get_prkey(lun, i) == 0)
8119 					continue;
8120 
8121 				ctl_clr_prkey(lun, i);
8122 				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8123 			}
8124 
8125 			lun->pr_key_count = 1;
8126 			lun->pr_res_type = msg->pr.pr_info.res_type;
8127 			if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8128 			    lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8129 				lun->pr_res_idx = msg->pr.pr_info.residx;
8130 		} else {
8131 		        for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8132 				if (sa_res_key == ctl_get_prkey(lun, i))
8133 					continue;
8134 
8135 				ctl_clr_prkey(lun, i);
8136 				lun->pr_key_count--;
8137 				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8138 			}
8139 		}
8140 	} else {
8141 		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8142 			if (i == msg->pr.pr_info.residx ||
8143 			    ctl_get_prkey(lun, i) == 0)
8144 				continue;
8145 
8146 			if (sa_res_key == ctl_get_prkey(lun, i)) {
8147 				ctl_clr_prkey(lun, i);
8148 				lun->pr_key_count--;
8149 				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8150 			} else if (msg->pr.pr_info.res_type != lun->pr_res_type
8151 			    && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8152 			     lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8153 				ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8154 			}
8155 		}
8156 		lun->pr_res_type = msg->pr.pr_info.res_type;
8157 		if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8158 		    lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8159 			lun->pr_res_idx = msg->pr.pr_info.residx;
8160 		else
8161 			lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8162 	}
8163 	lun->pr_generation++;
8164 
8165 }
8166 
8167 
8168 int
8169 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8170 {
8171 	int retval;
8172 	u_int32_t param_len;
8173 	struct scsi_per_res_out *cdb;
8174 	struct ctl_lun *lun;
8175 	struct scsi_per_res_out_parms* param;
8176 	struct ctl_softc *softc;
8177 	uint32_t residx;
8178 	uint64_t res_key, sa_res_key, key;
8179 	uint8_t type;
8180 	union ctl_ha_msg persis_io;
8181 	int    i;
8182 
8183 	CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8184 
8185 	retval = CTL_RETVAL_COMPLETE;
8186 
8187 	cdb = (struct scsi_per_res_out *)ctsio->cdb;
8188 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8189 	softc = lun->ctl_softc;
8190 
8191 	/*
8192 	 * We only support whole-LUN scope.  The scope & type are ignored for
8193 	 * register, register and ignore existing key and clear.
8194 	 * We sometimes ignore scope and type on preempts too!!
8195 	 * Verify reservation type here as well.
8196 	 */
8197 	type = cdb->scope_type & SPR_TYPE_MASK;
8198 	if ((cdb->action == SPRO_RESERVE)
8199 	 || (cdb->action == SPRO_RELEASE)) {
8200 		if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8201 			ctl_set_invalid_field(/*ctsio*/ ctsio,
8202 					      /*sks_valid*/ 1,
8203 					      /*command*/ 1,
8204 					      /*field*/ 2,
8205 					      /*bit_valid*/ 1,
8206 					      /*bit*/ 4);
8207 			ctl_done((union ctl_io *)ctsio);
8208 			return (CTL_RETVAL_COMPLETE);
8209 		}
8210 
8211 		if (type>8 || type==2 || type==4 || type==0) {
8212 			ctl_set_invalid_field(/*ctsio*/ ctsio,
8213 					      /*sks_valid*/ 1,
8214 					      /*command*/ 1,
8215 					      /*field*/ 2,
8216 					      /*bit_valid*/ 1,
8217 					      /*bit*/ 0);
8218 			ctl_done((union ctl_io *)ctsio);
8219 			return (CTL_RETVAL_COMPLETE);
8220 		}
8221 	}
8222 
8223 	param_len = scsi_4btoul(cdb->length);
8224 
8225 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8226 		ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8227 		ctsio->kern_data_len = param_len;
8228 		ctsio->kern_total_len = param_len;
8229 		ctsio->kern_data_resid = 0;
8230 		ctsio->kern_rel_offset = 0;
8231 		ctsio->kern_sg_entries = 0;
8232 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8233 		ctsio->be_move_done = ctl_config_move_done;
8234 		ctl_datamove((union ctl_io *)ctsio);
8235 
8236 		return (CTL_RETVAL_COMPLETE);
8237 	}
8238 
8239 	param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8240 
8241 	residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8242 	res_key = scsi_8btou64(param->res_key.key);
8243 	sa_res_key = scsi_8btou64(param->serv_act_res_key);
8244 
8245 	/*
8246 	 * Validate the reservation key here except for SPRO_REG_IGNO
8247 	 * This must be done for all other service actions
8248 	 */
8249 	if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8250 		mtx_lock(&lun->lun_lock);
8251 		if ((key = ctl_get_prkey(lun, residx)) != 0) {
8252 			if (res_key != key) {
8253 				/*
8254 				 * The current key passed in doesn't match
8255 				 * the one the initiator previously
8256 				 * registered.
8257 				 */
8258 				mtx_unlock(&lun->lun_lock);
8259 				free(ctsio->kern_data_ptr, M_CTL);
8260 				ctl_set_reservation_conflict(ctsio);
8261 				ctl_done((union ctl_io *)ctsio);
8262 				return (CTL_RETVAL_COMPLETE);
8263 			}
8264 		} else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8265 			/*
8266 			 * We are not registered
8267 			 */
8268 			mtx_unlock(&lun->lun_lock);
8269 			free(ctsio->kern_data_ptr, M_CTL);
8270 			ctl_set_reservation_conflict(ctsio);
8271 			ctl_done((union ctl_io *)ctsio);
8272 			return (CTL_RETVAL_COMPLETE);
8273 		} else if (res_key != 0) {
8274 			/*
8275 			 * We are not registered and trying to register but
8276 			 * the register key isn't zero.
8277 			 */
8278 			mtx_unlock(&lun->lun_lock);
8279 			free(ctsio->kern_data_ptr, M_CTL);
8280 			ctl_set_reservation_conflict(ctsio);
8281 			ctl_done((union ctl_io *)ctsio);
8282 			return (CTL_RETVAL_COMPLETE);
8283 		}
8284 		mtx_unlock(&lun->lun_lock);
8285 	}
8286 
8287 	switch (cdb->action & SPRO_ACTION_MASK) {
8288 	case SPRO_REGISTER:
8289 	case SPRO_REG_IGNO: {
8290 
8291 #if 0
8292 		printf("Registration received\n");
8293 #endif
8294 
8295 		/*
8296 		 * We don't support any of these options, as we report in
8297 		 * the read capabilities request (see
8298 		 * ctl_persistent_reserve_in(), above).
8299 		 */
8300 		if ((param->flags & SPR_SPEC_I_PT)
8301 		 || (param->flags & SPR_ALL_TG_PT)
8302 		 || (param->flags & SPR_APTPL)) {
8303 			int bit_ptr;
8304 
8305 			if (param->flags & SPR_APTPL)
8306 				bit_ptr = 0;
8307 			else if (param->flags & SPR_ALL_TG_PT)
8308 				bit_ptr = 2;
8309 			else /* SPR_SPEC_I_PT */
8310 				bit_ptr = 3;
8311 
8312 			free(ctsio->kern_data_ptr, M_CTL);
8313 			ctl_set_invalid_field(ctsio,
8314 					      /*sks_valid*/ 1,
8315 					      /*command*/ 0,
8316 					      /*field*/ 20,
8317 					      /*bit_valid*/ 1,
8318 					      /*bit*/ bit_ptr);
8319 			ctl_done((union ctl_io *)ctsio);
8320 			return (CTL_RETVAL_COMPLETE);
8321 		}
8322 
8323 		mtx_lock(&lun->lun_lock);
8324 
8325 		/*
8326 		 * The initiator wants to clear the
8327 		 * key/unregister.
8328 		 */
8329 		if (sa_res_key == 0) {
8330 			if ((res_key == 0
8331 			  && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8332 			 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8333 			  && ctl_get_prkey(lun, residx) == 0)) {
8334 				mtx_unlock(&lun->lun_lock);
8335 				goto done;
8336 			}
8337 
8338 			ctl_clr_prkey(lun, residx);
8339 			lun->pr_key_count--;
8340 
8341 			if (residx == lun->pr_res_idx) {
8342 				lun->flags &= ~CTL_LUN_PR_RESERVED;
8343 				lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8344 
8345 				if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8346 				     lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8347 				    lun->pr_key_count) {
8348 					/*
8349 					 * If the reservation is a registrants
8350 					 * only type we need to generate a UA
8351 					 * for other registered inits.  The
8352 					 * sense code should be RESERVATIONS
8353 					 * RELEASED
8354 					 */
8355 
8356 					for (i = softc->init_min; i < softc->init_max; i++){
8357 						if (ctl_get_prkey(lun, i) == 0)
8358 							continue;
8359 						ctl_est_ua(lun, i,
8360 						    CTL_UA_RES_RELEASE);
8361 					}
8362 				}
8363 				lun->pr_res_type = 0;
8364 			} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8365 				if (lun->pr_key_count==0) {
8366 					lun->flags &= ~CTL_LUN_PR_RESERVED;
8367 					lun->pr_res_type = 0;
8368 					lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8369 				}
8370 			}
8371 			lun->pr_generation++;
8372 			mtx_unlock(&lun->lun_lock);
8373 
8374 			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8375 			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8376 			persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8377 			persis_io.pr.pr_info.residx = residx;
8378 			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8379 			    sizeof(persis_io.pr), M_WAITOK);
8380 		} else /* sa_res_key != 0 */ {
8381 
8382 			/*
8383 			 * If we aren't registered currently then increment
8384 			 * the key count and set the registered flag.
8385 			 */
8386 			ctl_alloc_prkey(lun, residx);
8387 			if (ctl_get_prkey(lun, residx) == 0)
8388 				lun->pr_key_count++;
8389 			ctl_set_prkey(lun, residx, sa_res_key);
8390 			lun->pr_generation++;
8391 			mtx_unlock(&lun->lun_lock);
8392 
8393 			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8394 			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8395 			persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8396 			persis_io.pr.pr_info.residx = residx;
8397 			memcpy(persis_io.pr.pr_info.sa_res_key,
8398 			       param->serv_act_res_key,
8399 			       sizeof(param->serv_act_res_key));
8400 			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8401 			    sizeof(persis_io.pr), M_WAITOK);
8402 		}
8403 
8404 		break;
8405 	}
8406 	case SPRO_RESERVE:
8407 #if 0
8408                 printf("Reserve executed type %d\n", type);
8409 #endif
8410 		mtx_lock(&lun->lun_lock);
8411 		if (lun->flags & CTL_LUN_PR_RESERVED) {
8412 			/*
8413 			 * if this isn't the reservation holder and it's
8414 			 * not a "all registrants" type or if the type is
8415 			 * different then we have a conflict
8416 			 */
8417 			if ((lun->pr_res_idx != residx
8418 			  && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8419 			 || lun->pr_res_type != type) {
8420 				mtx_unlock(&lun->lun_lock);
8421 				free(ctsio->kern_data_ptr, M_CTL);
8422 				ctl_set_reservation_conflict(ctsio);
8423 				ctl_done((union ctl_io *)ctsio);
8424 				return (CTL_RETVAL_COMPLETE);
8425 			}
8426 			mtx_unlock(&lun->lun_lock);
8427 		} else /* create a reservation */ {
8428 			/*
8429 			 * If it's not an "all registrants" type record
8430 			 * reservation holder
8431 			 */
8432 			if (type != SPR_TYPE_WR_EX_AR
8433 			 && type != SPR_TYPE_EX_AC_AR)
8434 				lun->pr_res_idx = residx; /* Res holder */
8435 			else
8436 				lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8437 
8438 			lun->flags |= CTL_LUN_PR_RESERVED;
8439 			lun->pr_res_type = type;
8440 
8441 			mtx_unlock(&lun->lun_lock);
8442 
8443 			/* send msg to other side */
8444 			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8445 			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8446 			persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8447 			persis_io.pr.pr_info.residx = lun->pr_res_idx;
8448 			persis_io.pr.pr_info.res_type = type;
8449 			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8450 			    sizeof(persis_io.pr), M_WAITOK);
8451 		}
8452 		break;
8453 
8454 	case SPRO_RELEASE:
8455 		mtx_lock(&lun->lun_lock);
8456 		if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8457 			/* No reservation exists return good status */
8458 			mtx_unlock(&lun->lun_lock);
8459 			goto done;
8460 		}
8461 		/*
8462 		 * Is this nexus a reservation holder?
8463 		 */
8464 		if (lun->pr_res_idx != residx
8465 		 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8466 			/*
8467 			 * not a res holder return good status but
8468 			 * do nothing
8469 			 */
8470 			mtx_unlock(&lun->lun_lock);
8471 			goto done;
8472 		}
8473 
8474 		if (lun->pr_res_type != type) {
8475 			mtx_unlock(&lun->lun_lock);
8476 			free(ctsio->kern_data_ptr, M_CTL);
8477 			ctl_set_illegal_pr_release(ctsio);
8478 			ctl_done((union ctl_io *)ctsio);
8479 			return (CTL_RETVAL_COMPLETE);
8480 		}
8481 
8482 		/* okay to release */
8483 		lun->flags &= ~CTL_LUN_PR_RESERVED;
8484 		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8485 		lun->pr_res_type = 0;
8486 
8487 		/*
8488 		 * if this isn't an exclusive access
8489 		 * res generate UA for all other
8490 		 * registrants.
8491 		 */
8492 		if (type != SPR_TYPE_EX_AC
8493 		 && type != SPR_TYPE_WR_EX) {
8494 			for (i = softc->init_min; i < softc->init_max; i++) {
8495 				if (i == residx || ctl_get_prkey(lun, i) == 0)
8496 					continue;
8497 				ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8498 			}
8499 		}
8500 		mtx_unlock(&lun->lun_lock);
8501 
8502 		/* Send msg to other side */
8503 		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8504 		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8505 		persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8506 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8507 		     sizeof(persis_io.pr), M_WAITOK);
8508 		break;
8509 
8510 	case SPRO_CLEAR:
8511 		/* send msg to other side */
8512 
8513 		mtx_lock(&lun->lun_lock);
8514 		lun->flags &= ~CTL_LUN_PR_RESERVED;
8515 		lun->pr_res_type = 0;
8516 		lun->pr_key_count = 0;
8517 		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8518 
8519 		ctl_clr_prkey(lun, residx);
8520 		for (i = 0; i < CTL_MAX_INITIATORS; i++)
8521 			if (ctl_get_prkey(lun, i) != 0) {
8522 				ctl_clr_prkey(lun, i);
8523 				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8524 			}
8525 		lun->pr_generation++;
8526 		mtx_unlock(&lun->lun_lock);
8527 
8528 		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8529 		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8530 		persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8531 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8532 		     sizeof(persis_io.pr), M_WAITOK);
8533 		break;
8534 
8535 	case SPRO_PREEMPT:
8536 	case SPRO_PRE_ABO: {
8537 		int nretval;
8538 
8539 		nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8540 					  residx, ctsio, cdb, param);
8541 		if (nretval != 0)
8542 			return (CTL_RETVAL_COMPLETE);
8543 		break;
8544 	}
8545 	default:
8546 		panic("%s: Invalid PR type %#x", __func__, cdb->action);
8547 	}
8548 
8549 done:
8550 	free(ctsio->kern_data_ptr, M_CTL);
8551 	ctl_set_success(ctsio);
8552 	ctl_done((union ctl_io *)ctsio);
8553 
8554 	return (retval);
8555 }
8556 
8557 /*
8558  * This routine is for handling a message from the other SC pertaining to
8559  * persistent reserve out. All the error checking will have been done
8560  * so only perorming the action need be done here to keep the two
8561  * in sync.
8562  */
8563 static void
8564 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8565 {
8566 	struct ctl_softc *softc = control_softc;
8567 	struct ctl_lun *lun;
8568 	int i;
8569 	uint32_t residx, targ_lun;
8570 
8571 	targ_lun = msg->hdr.nexus.targ_mapped_lun;
8572 	mtx_lock(&softc->ctl_lock);
8573 	if ((targ_lun >= CTL_MAX_LUNS) ||
8574 	    ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8575 		mtx_unlock(&softc->ctl_lock);
8576 		return;
8577 	}
8578 	mtx_lock(&lun->lun_lock);
8579 	mtx_unlock(&softc->ctl_lock);
8580 	if (lun->flags & CTL_LUN_DISABLED) {
8581 		mtx_unlock(&lun->lun_lock);
8582 		return;
8583 	}
8584 	residx = ctl_get_initindex(&msg->hdr.nexus);
8585 	switch(msg->pr.pr_info.action) {
8586 	case CTL_PR_REG_KEY:
8587 		ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8588 		if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8589 			lun->pr_key_count++;
8590 		ctl_set_prkey(lun, msg->pr.pr_info.residx,
8591 		    scsi_8btou64(msg->pr.pr_info.sa_res_key));
8592 		lun->pr_generation++;
8593 		break;
8594 
8595 	case CTL_PR_UNREG_KEY:
8596 		ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8597 		lun->pr_key_count--;
8598 
8599 		/* XXX Need to see if the reservation has been released */
8600 		/* if so do we need to generate UA? */
8601 		if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8602 			lun->flags &= ~CTL_LUN_PR_RESERVED;
8603 			lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8604 
8605 			if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8606 			     lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8607 			    lun->pr_key_count) {
8608 				/*
8609 				 * If the reservation is a registrants
8610 				 * only type we need to generate a UA
8611 				 * for other registered inits.  The
8612 				 * sense code should be RESERVATIONS
8613 				 * RELEASED
8614 				 */
8615 
8616 				for (i = softc->init_min; i < softc->init_max; i++) {
8617 					if (ctl_get_prkey(lun, i) == 0)
8618 						continue;
8619 
8620 					ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8621 				}
8622 			}
8623 			lun->pr_res_type = 0;
8624 		} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8625 			if (lun->pr_key_count==0) {
8626 				lun->flags &= ~CTL_LUN_PR_RESERVED;
8627 				lun->pr_res_type = 0;
8628 				lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8629 			}
8630 		}
8631 		lun->pr_generation++;
8632 		break;
8633 
8634 	case CTL_PR_RESERVE:
8635 		lun->flags |= CTL_LUN_PR_RESERVED;
8636 		lun->pr_res_type = msg->pr.pr_info.res_type;
8637 		lun->pr_res_idx = msg->pr.pr_info.residx;
8638 
8639 		break;
8640 
8641 	case CTL_PR_RELEASE:
8642 		/*
8643 		 * if this isn't an exclusive access res generate UA for all
8644 		 * other registrants.
8645 		 */
8646 		if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8647 		    lun->pr_res_type != SPR_TYPE_WR_EX) {
8648 			for (i = softc->init_min; i < softc->init_max; i++)
8649 				if (i == residx || ctl_get_prkey(lun, i) == 0)
8650 					continue;
8651 				ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8652 		}
8653 
8654 		lun->flags &= ~CTL_LUN_PR_RESERVED;
8655 		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8656 		lun->pr_res_type = 0;
8657 		break;
8658 
8659 	case CTL_PR_PREEMPT:
8660 		ctl_pro_preempt_other(lun, msg);
8661 		break;
8662 	case CTL_PR_CLEAR:
8663 		lun->flags &= ~CTL_LUN_PR_RESERVED;
8664 		lun->pr_res_type = 0;
8665 		lun->pr_key_count = 0;
8666 		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8667 
8668 		for (i=0; i < CTL_MAX_INITIATORS; i++) {
8669 			if (ctl_get_prkey(lun, i) == 0)
8670 				continue;
8671 			ctl_clr_prkey(lun, i);
8672 			ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8673 		}
8674 		lun->pr_generation++;
8675 		break;
8676 	}
8677 
8678 	mtx_unlock(&lun->lun_lock);
8679 }
8680 
8681 int
8682 ctl_read_write(struct ctl_scsiio *ctsio)
8683 {
8684 	struct ctl_lun *lun;
8685 	struct ctl_lba_len_flags *lbalen;
8686 	uint64_t lba;
8687 	uint32_t num_blocks;
8688 	int flags, retval;
8689 	int isread;
8690 
8691 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8692 
8693 	CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8694 
8695 	flags = 0;
8696 	isread = ctsio->cdb[0] == READ_6  || ctsio->cdb[0] == READ_10
8697 	      || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8698 	switch (ctsio->cdb[0]) {
8699 	case READ_6:
8700 	case WRITE_6: {
8701 		struct scsi_rw_6 *cdb;
8702 
8703 		cdb = (struct scsi_rw_6 *)ctsio->cdb;
8704 
8705 		lba = scsi_3btoul(cdb->addr);
8706 		/* only 5 bits are valid in the most significant address byte */
8707 		lba &= 0x1fffff;
8708 		num_blocks = cdb->length;
8709 		/*
8710 		 * This is correct according to SBC-2.
8711 		 */
8712 		if (num_blocks == 0)
8713 			num_blocks = 256;
8714 		break;
8715 	}
8716 	case READ_10:
8717 	case WRITE_10: {
8718 		struct scsi_rw_10 *cdb;
8719 
8720 		cdb = (struct scsi_rw_10 *)ctsio->cdb;
8721 		if (cdb->byte2 & SRW10_FUA)
8722 			flags |= CTL_LLF_FUA;
8723 		if (cdb->byte2 & SRW10_DPO)
8724 			flags |= CTL_LLF_DPO;
8725 		lba = scsi_4btoul(cdb->addr);
8726 		num_blocks = scsi_2btoul(cdb->length);
8727 		break;
8728 	}
8729 	case WRITE_VERIFY_10: {
8730 		struct scsi_write_verify_10 *cdb;
8731 
8732 		cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8733 		flags |= CTL_LLF_FUA;
8734 		if (cdb->byte2 & SWV_DPO)
8735 			flags |= CTL_LLF_DPO;
8736 		lba = scsi_4btoul(cdb->addr);
8737 		num_blocks = scsi_2btoul(cdb->length);
8738 		break;
8739 	}
8740 	case READ_12:
8741 	case WRITE_12: {
8742 		struct scsi_rw_12 *cdb;
8743 
8744 		cdb = (struct scsi_rw_12 *)ctsio->cdb;
8745 		if (cdb->byte2 & SRW12_FUA)
8746 			flags |= CTL_LLF_FUA;
8747 		if (cdb->byte2 & SRW12_DPO)
8748 			flags |= CTL_LLF_DPO;
8749 		lba = scsi_4btoul(cdb->addr);
8750 		num_blocks = scsi_4btoul(cdb->length);
8751 		break;
8752 	}
8753 	case WRITE_VERIFY_12: {
8754 		struct scsi_write_verify_12 *cdb;
8755 
8756 		cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8757 		flags |= CTL_LLF_FUA;
8758 		if (cdb->byte2 & SWV_DPO)
8759 			flags |= CTL_LLF_DPO;
8760 		lba = scsi_4btoul(cdb->addr);
8761 		num_blocks = scsi_4btoul(cdb->length);
8762 		break;
8763 	}
8764 	case READ_16:
8765 	case WRITE_16: {
8766 		struct scsi_rw_16 *cdb;
8767 
8768 		cdb = (struct scsi_rw_16 *)ctsio->cdb;
8769 		if (cdb->byte2 & SRW12_FUA)
8770 			flags |= CTL_LLF_FUA;
8771 		if (cdb->byte2 & SRW12_DPO)
8772 			flags |= CTL_LLF_DPO;
8773 		lba = scsi_8btou64(cdb->addr);
8774 		num_blocks = scsi_4btoul(cdb->length);
8775 		break;
8776 	}
8777 	case WRITE_ATOMIC_16: {
8778 		struct scsi_write_atomic_16 *cdb;
8779 
8780 		if (lun->be_lun->atomicblock == 0) {
8781 			ctl_set_invalid_opcode(ctsio);
8782 			ctl_done((union ctl_io *)ctsio);
8783 			return (CTL_RETVAL_COMPLETE);
8784 		}
8785 
8786 		cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8787 		if (cdb->byte2 & SRW12_FUA)
8788 			flags |= CTL_LLF_FUA;
8789 		if (cdb->byte2 & SRW12_DPO)
8790 			flags |= CTL_LLF_DPO;
8791 		lba = scsi_8btou64(cdb->addr);
8792 		num_blocks = scsi_2btoul(cdb->length);
8793 		if (num_blocks > lun->be_lun->atomicblock) {
8794 			ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8795 			    /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8796 			    /*bit*/ 0);
8797 			ctl_done((union ctl_io *)ctsio);
8798 			return (CTL_RETVAL_COMPLETE);
8799 		}
8800 		break;
8801 	}
8802 	case WRITE_VERIFY_16: {
8803 		struct scsi_write_verify_16 *cdb;
8804 
8805 		cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8806 		flags |= CTL_LLF_FUA;
8807 		if (cdb->byte2 & SWV_DPO)
8808 			flags |= CTL_LLF_DPO;
8809 		lba = scsi_8btou64(cdb->addr);
8810 		num_blocks = scsi_4btoul(cdb->length);
8811 		break;
8812 	}
8813 	default:
8814 		/*
8815 		 * We got a command we don't support.  This shouldn't
8816 		 * happen, commands should be filtered out above us.
8817 		 */
8818 		ctl_set_invalid_opcode(ctsio);
8819 		ctl_done((union ctl_io *)ctsio);
8820 
8821 		return (CTL_RETVAL_COMPLETE);
8822 		break; /* NOTREACHED */
8823 	}
8824 
8825 	/*
8826 	 * The first check is to make sure we're in bounds, the second
8827 	 * check is to catch wrap-around problems.  If the lba + num blocks
8828 	 * is less than the lba, then we've wrapped around and the block
8829 	 * range is invalid anyway.
8830 	 */
8831 	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8832 	 || ((lba + num_blocks) < lba)) {
8833 		ctl_set_lba_out_of_range(ctsio);
8834 		ctl_done((union ctl_io *)ctsio);
8835 		return (CTL_RETVAL_COMPLETE);
8836 	}
8837 
8838 	/*
8839 	 * According to SBC-3, a transfer length of 0 is not an error.
8840 	 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8841 	 * translates to 256 blocks for those commands.
8842 	 */
8843 	if (num_blocks == 0) {
8844 		ctl_set_success(ctsio);
8845 		ctl_done((union ctl_io *)ctsio);
8846 		return (CTL_RETVAL_COMPLETE);
8847 	}
8848 
8849 	/* Set FUA and/or DPO if caches are disabled. */
8850 	if (isread) {
8851 		if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8852 		    SCP_RCD) != 0)
8853 			flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8854 	} else {
8855 		if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8856 		    SCP_WCE) == 0)
8857 			flags |= CTL_LLF_FUA;
8858 	}
8859 
8860 	lbalen = (struct ctl_lba_len_flags *)
8861 	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8862 	lbalen->lba = lba;
8863 	lbalen->len = num_blocks;
8864 	lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8865 
8866 	ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8867 	ctsio->kern_rel_offset = 0;
8868 
8869 	CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8870 
8871 	retval = lun->backend->data_submit((union ctl_io *)ctsio);
8872 	return (retval);
8873 }
8874 
8875 static int
8876 ctl_cnw_cont(union ctl_io *io)
8877 {
8878 	struct ctl_scsiio *ctsio;
8879 	struct ctl_lun *lun;
8880 	struct ctl_lba_len_flags *lbalen;
8881 	int retval;
8882 
8883 	ctsio = &io->scsiio;
8884 	ctsio->io_hdr.status = CTL_STATUS_NONE;
8885 	ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8886 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8887 	lbalen = (struct ctl_lba_len_flags *)
8888 	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8889 	lbalen->flags &= ~CTL_LLF_COMPARE;
8890 	lbalen->flags |= CTL_LLF_WRITE;
8891 
8892 	CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8893 	retval = lun->backend->data_submit((union ctl_io *)ctsio);
8894 	return (retval);
8895 }
8896 
8897 int
8898 ctl_cnw(struct ctl_scsiio *ctsio)
8899 {
8900 	struct ctl_lun *lun;
8901 	struct ctl_lba_len_flags *lbalen;
8902 	uint64_t lba;
8903 	uint32_t num_blocks;
8904 	int flags, retval;
8905 
8906 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8907 
8908 	CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8909 
8910 	flags = 0;
8911 	switch (ctsio->cdb[0]) {
8912 	case COMPARE_AND_WRITE: {
8913 		struct scsi_compare_and_write *cdb;
8914 
8915 		cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8916 		if (cdb->byte2 & SRW10_FUA)
8917 			flags |= CTL_LLF_FUA;
8918 		if (cdb->byte2 & SRW10_DPO)
8919 			flags |= CTL_LLF_DPO;
8920 		lba = scsi_8btou64(cdb->addr);
8921 		num_blocks = cdb->length;
8922 		break;
8923 	}
8924 	default:
8925 		/*
8926 		 * We got a command we don't support.  This shouldn't
8927 		 * happen, commands should be filtered out above us.
8928 		 */
8929 		ctl_set_invalid_opcode(ctsio);
8930 		ctl_done((union ctl_io *)ctsio);
8931 
8932 		return (CTL_RETVAL_COMPLETE);
8933 		break; /* NOTREACHED */
8934 	}
8935 
8936 	/*
8937 	 * The first check is to make sure we're in bounds, the second
8938 	 * check is to catch wrap-around problems.  If the lba + num blocks
8939 	 * is less than the lba, then we've wrapped around and the block
8940 	 * range is invalid anyway.
8941 	 */
8942 	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8943 	 || ((lba + num_blocks) < lba)) {
8944 		ctl_set_lba_out_of_range(ctsio);
8945 		ctl_done((union ctl_io *)ctsio);
8946 		return (CTL_RETVAL_COMPLETE);
8947 	}
8948 
8949 	/*
8950 	 * According to SBC-3, a transfer length of 0 is not an error.
8951 	 */
8952 	if (num_blocks == 0) {
8953 		ctl_set_success(ctsio);
8954 		ctl_done((union ctl_io *)ctsio);
8955 		return (CTL_RETVAL_COMPLETE);
8956 	}
8957 
8958 	/* Set FUA if write cache is disabled. */
8959 	if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8960 	    SCP_WCE) == 0)
8961 		flags |= CTL_LLF_FUA;
8962 
8963 	ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8964 	ctsio->kern_rel_offset = 0;
8965 
8966 	/*
8967 	 * Set the IO_CONT flag, so that if this I/O gets passed to
8968 	 * ctl_data_submit_done(), it'll get passed back to
8969 	 * ctl_ctl_cnw_cont() for further processing.
8970 	 */
8971 	ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8972 	ctsio->io_cont = ctl_cnw_cont;
8973 
8974 	lbalen = (struct ctl_lba_len_flags *)
8975 	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8976 	lbalen->lba = lba;
8977 	lbalen->len = num_blocks;
8978 	lbalen->flags = CTL_LLF_COMPARE | flags;
8979 
8980 	CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8981 	retval = lun->backend->data_submit((union ctl_io *)ctsio);
8982 	return (retval);
8983 }
8984 
8985 int
8986 ctl_verify(struct ctl_scsiio *ctsio)
8987 {
8988 	struct ctl_lun *lun;
8989 	struct ctl_lba_len_flags *lbalen;
8990 	uint64_t lba;
8991 	uint32_t num_blocks;
8992 	int bytchk, flags;
8993 	int retval;
8994 
8995 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8996 
8997 	CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8998 
8999 	bytchk = 0;
9000 	flags = CTL_LLF_FUA;
9001 	switch (ctsio->cdb[0]) {
9002 	case VERIFY_10: {
9003 		struct scsi_verify_10 *cdb;
9004 
9005 		cdb = (struct scsi_verify_10 *)ctsio->cdb;
9006 		if (cdb->byte2 & SVFY_BYTCHK)
9007 			bytchk = 1;
9008 		if (cdb->byte2 & SVFY_DPO)
9009 			flags |= CTL_LLF_DPO;
9010 		lba = scsi_4btoul(cdb->addr);
9011 		num_blocks = scsi_2btoul(cdb->length);
9012 		break;
9013 	}
9014 	case VERIFY_12: {
9015 		struct scsi_verify_12 *cdb;
9016 
9017 		cdb = (struct scsi_verify_12 *)ctsio->cdb;
9018 		if (cdb->byte2 & SVFY_BYTCHK)
9019 			bytchk = 1;
9020 		if (cdb->byte2 & SVFY_DPO)
9021 			flags |= CTL_LLF_DPO;
9022 		lba = scsi_4btoul(cdb->addr);
9023 		num_blocks = scsi_4btoul(cdb->length);
9024 		break;
9025 	}
9026 	case VERIFY_16: {
9027 		struct scsi_rw_16 *cdb;
9028 
9029 		cdb = (struct scsi_rw_16 *)ctsio->cdb;
9030 		if (cdb->byte2 & SVFY_BYTCHK)
9031 			bytchk = 1;
9032 		if (cdb->byte2 & SVFY_DPO)
9033 			flags |= CTL_LLF_DPO;
9034 		lba = scsi_8btou64(cdb->addr);
9035 		num_blocks = scsi_4btoul(cdb->length);
9036 		break;
9037 	}
9038 	default:
9039 		/*
9040 		 * We got a command we don't support.  This shouldn't
9041 		 * happen, commands should be filtered out above us.
9042 		 */
9043 		ctl_set_invalid_opcode(ctsio);
9044 		ctl_done((union ctl_io *)ctsio);
9045 		return (CTL_RETVAL_COMPLETE);
9046 	}
9047 
9048 	/*
9049 	 * The first check is to make sure we're in bounds, the second
9050 	 * check is to catch wrap-around problems.  If the lba + num blocks
9051 	 * is less than the lba, then we've wrapped around and the block
9052 	 * range is invalid anyway.
9053 	 */
9054 	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9055 	 || ((lba + num_blocks) < lba)) {
9056 		ctl_set_lba_out_of_range(ctsio);
9057 		ctl_done((union ctl_io *)ctsio);
9058 		return (CTL_RETVAL_COMPLETE);
9059 	}
9060 
9061 	/*
9062 	 * According to SBC-3, a transfer length of 0 is not an error.
9063 	 */
9064 	if (num_blocks == 0) {
9065 		ctl_set_success(ctsio);
9066 		ctl_done((union ctl_io *)ctsio);
9067 		return (CTL_RETVAL_COMPLETE);
9068 	}
9069 
9070 	lbalen = (struct ctl_lba_len_flags *)
9071 	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9072 	lbalen->lba = lba;
9073 	lbalen->len = num_blocks;
9074 	if (bytchk) {
9075 		lbalen->flags = CTL_LLF_COMPARE | flags;
9076 		ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9077 	} else {
9078 		lbalen->flags = CTL_LLF_VERIFY | flags;
9079 		ctsio->kern_total_len = 0;
9080 	}
9081 	ctsio->kern_rel_offset = 0;
9082 
9083 	CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9084 	retval = lun->backend->data_submit((union ctl_io *)ctsio);
9085 	return (retval);
9086 }
9087 
9088 int
9089 ctl_report_luns(struct ctl_scsiio *ctsio)
9090 {
9091 	struct ctl_softc *softc;
9092 	struct scsi_report_luns *cdb;
9093 	struct scsi_report_luns_data *lun_data;
9094 	struct ctl_lun *lun, *request_lun;
9095 	struct ctl_port *port;
9096 	int num_luns, retval;
9097 	uint32_t alloc_len, lun_datalen;
9098 	int num_filled;
9099 	uint32_t initidx, targ_lun_id, lun_id;
9100 
9101 	retval = CTL_RETVAL_COMPLETE;
9102 	cdb = (struct scsi_report_luns *)ctsio->cdb;
9103 	port = ctl_io_port(&ctsio->io_hdr);
9104 	softc = port->ctl_softc;
9105 
9106 	CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9107 
9108 	mtx_lock(&softc->ctl_lock);
9109 	num_luns = 0;
9110 	for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9111 		if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9112 			num_luns++;
9113 	}
9114 	mtx_unlock(&softc->ctl_lock);
9115 
9116 	switch (cdb->select_report) {
9117 	case RPL_REPORT_DEFAULT:
9118 	case RPL_REPORT_ALL:
9119 	case RPL_REPORT_NONSUBSID:
9120 		break;
9121 	case RPL_REPORT_WELLKNOWN:
9122 	case RPL_REPORT_ADMIN:
9123 	case RPL_REPORT_CONGLOM:
9124 		num_luns = 0;
9125 		break;
9126 	default:
9127 		ctl_set_invalid_field(ctsio,
9128 				      /*sks_valid*/ 1,
9129 				      /*command*/ 1,
9130 				      /*field*/ 2,
9131 				      /*bit_valid*/ 0,
9132 				      /*bit*/ 0);
9133 		ctl_done((union ctl_io *)ctsio);
9134 		return (retval);
9135 		break; /* NOTREACHED */
9136 	}
9137 
9138 	alloc_len = scsi_4btoul(cdb->length);
9139 	/*
9140 	 * The initiator has to allocate at least 16 bytes for this request,
9141 	 * so he can at least get the header and the first LUN.  Otherwise
9142 	 * we reject the request (per SPC-3 rev 14, section 6.21).
9143 	 */
9144 	if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9145 	    sizeof(struct scsi_report_luns_lundata))) {
9146 		ctl_set_invalid_field(ctsio,
9147 				      /*sks_valid*/ 1,
9148 				      /*command*/ 1,
9149 				      /*field*/ 6,
9150 				      /*bit_valid*/ 0,
9151 				      /*bit*/ 0);
9152 		ctl_done((union ctl_io *)ctsio);
9153 		return (retval);
9154 	}
9155 
9156 	request_lun = (struct ctl_lun *)
9157 		ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9158 
9159 	lun_datalen = sizeof(*lun_data) +
9160 		(num_luns * sizeof(struct scsi_report_luns_lundata));
9161 
9162 	ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9163 	lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9164 	ctsio->kern_sg_entries = 0;
9165 
9166 	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9167 
9168 	mtx_lock(&softc->ctl_lock);
9169 	for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9170 		lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9171 		if (lun_id >= CTL_MAX_LUNS)
9172 			continue;
9173 		lun = softc->ctl_luns[lun_id];
9174 		if (lun == NULL)
9175 			continue;
9176 
9177 		be64enc(lun_data->luns[num_filled++].lundata,
9178 		    ctl_encode_lun(targ_lun_id));
9179 
9180 		/*
9181 		 * According to SPC-3, rev 14 section 6.21:
9182 		 *
9183 		 * "The execution of a REPORT LUNS command to any valid and
9184 		 * installed logical unit shall clear the REPORTED LUNS DATA
9185 		 * HAS CHANGED unit attention condition for all logical
9186 		 * units of that target with respect to the requesting
9187 		 * initiator. A valid and installed logical unit is one
9188 		 * having a PERIPHERAL QUALIFIER of 000b in the standard
9189 		 * INQUIRY data (see 6.4.2)."
9190 		 *
9191 		 * If request_lun is NULL, the LUN this report luns command
9192 		 * was issued to is either disabled or doesn't exist. In that
9193 		 * case, we shouldn't clear any pending lun change unit
9194 		 * attention.
9195 		 */
9196 		if (request_lun != NULL) {
9197 			mtx_lock(&lun->lun_lock);
9198 			ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9199 			mtx_unlock(&lun->lun_lock);
9200 		}
9201 	}
9202 	mtx_unlock(&softc->ctl_lock);
9203 
9204 	/*
9205 	 * It's quite possible that we've returned fewer LUNs than we allocated
9206 	 * space for.  Trim it.
9207 	 */
9208 	lun_datalen = sizeof(*lun_data) +
9209 		(num_filled * sizeof(struct scsi_report_luns_lundata));
9210 
9211 	if (lun_datalen < alloc_len) {
9212 		ctsio->residual = alloc_len - lun_datalen;
9213 		ctsio->kern_data_len = lun_datalen;
9214 		ctsio->kern_total_len = lun_datalen;
9215 	} else {
9216 		ctsio->residual = 0;
9217 		ctsio->kern_data_len = alloc_len;
9218 		ctsio->kern_total_len = alloc_len;
9219 	}
9220 	ctsio->kern_data_resid = 0;
9221 	ctsio->kern_rel_offset = 0;
9222 	ctsio->kern_sg_entries = 0;
9223 
9224 	/*
9225 	 * We set this to the actual data length, regardless of how much
9226 	 * space we actually have to return results.  If the user looks at
9227 	 * this value, he'll know whether or not he allocated enough space
9228 	 * and reissue the command if necessary.  We don't support well
9229 	 * known logical units, so if the user asks for that, return none.
9230 	 */
9231 	scsi_ulto4b(lun_datalen - 8, lun_data->length);
9232 
9233 	/*
9234 	 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9235 	 * this request.
9236 	 */
9237 	ctl_set_success(ctsio);
9238 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9239 	ctsio->be_move_done = ctl_config_move_done;
9240 	ctl_datamove((union ctl_io *)ctsio);
9241 	return (retval);
9242 }
9243 
9244 int
9245 ctl_request_sense(struct ctl_scsiio *ctsio)
9246 {
9247 	struct scsi_request_sense *cdb;
9248 	struct scsi_sense_data *sense_ptr;
9249 	struct ctl_softc *ctl_softc;
9250 	struct ctl_lun *lun;
9251 	uint32_t initidx;
9252 	int have_error;
9253 	scsi_sense_data_type sense_format;
9254 	ctl_ua_type ua_type;
9255 
9256 	cdb = (struct scsi_request_sense *)ctsio->cdb;
9257 
9258 	ctl_softc = control_softc;
9259 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9260 
9261 	CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9262 
9263 	/*
9264 	 * Determine which sense format the user wants.
9265 	 */
9266 	if (cdb->byte2 & SRS_DESC)
9267 		sense_format = SSD_TYPE_DESC;
9268 	else
9269 		sense_format = SSD_TYPE_FIXED;
9270 
9271 	ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9272 	sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9273 	ctsio->kern_sg_entries = 0;
9274 
9275 	/*
9276 	 * struct scsi_sense_data, which is currently set to 256 bytes, is
9277 	 * larger than the largest allowed value for the length field in the
9278 	 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9279 	 */
9280 	ctsio->residual = 0;
9281 	ctsio->kern_data_len = cdb->length;
9282 	ctsio->kern_total_len = cdb->length;
9283 
9284 	ctsio->kern_data_resid = 0;
9285 	ctsio->kern_rel_offset = 0;
9286 	ctsio->kern_sg_entries = 0;
9287 
9288 	/*
9289 	 * If we don't have a LUN, we don't have any pending sense.
9290 	 */
9291 	if (lun == NULL)
9292 		goto no_sense;
9293 
9294 	have_error = 0;
9295 	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9296 	/*
9297 	 * Check for pending sense, and then for pending unit attentions.
9298 	 * Pending sense gets returned first, then pending unit attentions.
9299 	 */
9300 	mtx_lock(&lun->lun_lock);
9301 #ifdef CTL_WITH_CA
9302 	if (ctl_is_set(lun->have_ca, initidx)) {
9303 		scsi_sense_data_type stored_format;
9304 
9305 		/*
9306 		 * Check to see which sense format was used for the stored
9307 		 * sense data.
9308 		 */
9309 		stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9310 
9311 		/*
9312 		 * If the user requested a different sense format than the
9313 		 * one we stored, then we need to convert it to the other
9314 		 * format.  If we're going from descriptor to fixed format
9315 		 * sense data, we may lose things in translation, depending
9316 		 * on what options were used.
9317 		 *
9318 		 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9319 		 * for some reason we'll just copy it out as-is.
9320 		 */
9321 		if ((stored_format == SSD_TYPE_FIXED)
9322 		 && (sense_format == SSD_TYPE_DESC))
9323 			ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9324 			    &lun->pending_sense[initidx],
9325 			    (struct scsi_sense_data_desc *)sense_ptr);
9326 		else if ((stored_format == SSD_TYPE_DESC)
9327 		      && (sense_format == SSD_TYPE_FIXED))
9328 			ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9329 			    &lun->pending_sense[initidx],
9330 			    (struct scsi_sense_data_fixed *)sense_ptr);
9331 		else
9332 			memcpy(sense_ptr, &lun->pending_sense[initidx],
9333 			       MIN(sizeof(*sense_ptr),
9334 			       sizeof(lun->pending_sense[initidx])));
9335 
9336 		ctl_clear_mask(lun->have_ca, initidx);
9337 		have_error = 1;
9338 	} else
9339 #endif
9340 	{
9341 		ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9342 		if (ua_type != CTL_UA_NONE)
9343 			have_error = 1;
9344 		if (ua_type == CTL_UA_LUN_CHANGE) {
9345 			mtx_unlock(&lun->lun_lock);
9346 			mtx_lock(&ctl_softc->ctl_lock);
9347 			ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9348 			mtx_unlock(&ctl_softc->ctl_lock);
9349 			mtx_lock(&lun->lun_lock);
9350 		}
9351 
9352 	}
9353 	mtx_unlock(&lun->lun_lock);
9354 
9355 	/*
9356 	 * We already have a pending error, return it.
9357 	 */
9358 	if (have_error != 0) {
9359 		/*
9360 		 * We report the SCSI status as OK, since the status of the
9361 		 * request sense command itself is OK.
9362 		 * We report 0 for the sense length, because we aren't doing
9363 		 * autosense in this case.  We're reporting sense as
9364 		 * parameter data.
9365 		 */
9366 		ctl_set_success(ctsio);
9367 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9368 		ctsio->be_move_done = ctl_config_move_done;
9369 		ctl_datamove((union ctl_io *)ctsio);
9370 		return (CTL_RETVAL_COMPLETE);
9371 	}
9372 
9373 no_sense:
9374 
9375 	/*
9376 	 * No sense information to report, so we report that everything is
9377 	 * okay.
9378 	 */
9379 	ctl_set_sense_data(sense_ptr,
9380 			   lun,
9381 			   sense_format,
9382 			   /*current_error*/ 1,
9383 			   /*sense_key*/ SSD_KEY_NO_SENSE,
9384 			   /*asc*/ 0x00,
9385 			   /*ascq*/ 0x00,
9386 			   SSD_ELEM_NONE);
9387 
9388 	/*
9389 	 * We report 0 for the sense length, because we aren't doing
9390 	 * autosense in this case.  We're reporting sense as parameter data.
9391 	 */
9392 	ctl_set_success(ctsio);
9393 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9394 	ctsio->be_move_done = ctl_config_move_done;
9395 	ctl_datamove((union ctl_io *)ctsio);
9396 	return (CTL_RETVAL_COMPLETE);
9397 }
9398 
9399 int
9400 ctl_tur(struct ctl_scsiio *ctsio)
9401 {
9402 
9403 	CTL_DEBUG_PRINT(("ctl_tur\n"));
9404 
9405 	ctl_set_success(ctsio);
9406 	ctl_done((union ctl_io *)ctsio);
9407 
9408 	return (CTL_RETVAL_COMPLETE);
9409 }
9410 
9411 /*
9412  * SCSI VPD page 0x00, the Supported VPD Pages page.
9413  */
9414 static int
9415 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9416 {
9417 	struct scsi_vpd_supported_pages *pages;
9418 	int sup_page_size;
9419 	struct ctl_lun *lun;
9420 	int p;
9421 
9422 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9423 
9424 	sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9425 	    SCSI_EVPD_NUM_SUPPORTED_PAGES;
9426 	ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9427 	pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9428 	ctsio->kern_sg_entries = 0;
9429 
9430 	if (sup_page_size < alloc_len) {
9431 		ctsio->residual = alloc_len - sup_page_size;
9432 		ctsio->kern_data_len = sup_page_size;
9433 		ctsio->kern_total_len = sup_page_size;
9434 	} else {
9435 		ctsio->residual = 0;
9436 		ctsio->kern_data_len = alloc_len;
9437 		ctsio->kern_total_len = alloc_len;
9438 	}
9439 	ctsio->kern_data_resid = 0;
9440 	ctsio->kern_rel_offset = 0;
9441 	ctsio->kern_sg_entries = 0;
9442 
9443 	/*
9444 	 * The control device is always connected.  The disk device, on the
9445 	 * other hand, may not be online all the time.  Need to change this
9446 	 * to figure out whether the disk device is actually online or not.
9447 	 */
9448 	if (lun != NULL)
9449 		pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9450 				lun->be_lun->lun_type;
9451 	else
9452 		pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9453 
9454 	p = 0;
9455 	/* Supported VPD pages */
9456 	pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9457 	/* Serial Number */
9458 	pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9459 	/* Device Identification */
9460 	pages->page_list[p++] = SVPD_DEVICE_ID;
9461 	/* Extended INQUIRY Data */
9462 	pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9463 	/* Mode Page Policy */
9464 	pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9465 	/* SCSI Ports */
9466 	pages->page_list[p++] = SVPD_SCSI_PORTS;
9467 	/* Third-party Copy */
9468 	pages->page_list[p++] = SVPD_SCSI_TPC;
9469 	if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9470 		/* Block limits */
9471 		pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9472 		/* Block Device Characteristics */
9473 		pages->page_list[p++] = SVPD_BDC;
9474 		/* Logical Block Provisioning */
9475 		pages->page_list[p++] = SVPD_LBP;
9476 	}
9477 	pages->length = p;
9478 
9479 	ctl_set_success(ctsio);
9480 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9481 	ctsio->be_move_done = ctl_config_move_done;
9482 	ctl_datamove((union ctl_io *)ctsio);
9483 	return (CTL_RETVAL_COMPLETE);
9484 }
9485 
9486 /*
9487  * SCSI VPD page 0x80, the Unit Serial Number page.
9488  */
9489 static int
9490 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9491 {
9492 	struct scsi_vpd_unit_serial_number *sn_ptr;
9493 	struct ctl_lun *lun;
9494 	int data_len;
9495 
9496 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9497 
9498 	data_len = 4 + CTL_SN_LEN;
9499 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9500 	sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9501 	if (data_len < alloc_len) {
9502 		ctsio->residual = alloc_len - data_len;
9503 		ctsio->kern_data_len = data_len;
9504 		ctsio->kern_total_len = data_len;
9505 	} else {
9506 		ctsio->residual = 0;
9507 		ctsio->kern_data_len = alloc_len;
9508 		ctsio->kern_total_len = alloc_len;
9509 	}
9510 	ctsio->kern_data_resid = 0;
9511 	ctsio->kern_rel_offset = 0;
9512 	ctsio->kern_sg_entries = 0;
9513 
9514 	/*
9515 	 * The control device is always connected.  The disk device, on the
9516 	 * other hand, may not be online all the time.  Need to change this
9517 	 * to figure out whether the disk device is actually online or not.
9518 	 */
9519 	if (lun != NULL)
9520 		sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9521 				  lun->be_lun->lun_type;
9522 	else
9523 		sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9524 
9525 	sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9526 	sn_ptr->length = CTL_SN_LEN;
9527 	/*
9528 	 * If we don't have a LUN, we just leave the serial number as
9529 	 * all spaces.
9530 	 */
9531 	if (lun != NULL) {
9532 		strncpy((char *)sn_ptr->serial_num,
9533 			(char *)lun->be_lun->serial_num, CTL_SN_LEN);
9534 	} else
9535 		memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9536 
9537 	ctl_set_success(ctsio);
9538 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9539 	ctsio->be_move_done = ctl_config_move_done;
9540 	ctl_datamove((union ctl_io *)ctsio);
9541 	return (CTL_RETVAL_COMPLETE);
9542 }
9543 
9544 
9545 /*
9546  * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9547  */
9548 static int
9549 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9550 {
9551 	struct scsi_vpd_extended_inquiry_data *eid_ptr;
9552 	struct ctl_lun *lun;
9553 	int data_len;
9554 
9555 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9556 
9557 	data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9558 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9559 	eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9560 	ctsio->kern_sg_entries = 0;
9561 
9562 	if (data_len < alloc_len) {
9563 		ctsio->residual = alloc_len - data_len;
9564 		ctsio->kern_data_len = data_len;
9565 		ctsio->kern_total_len = data_len;
9566 	} else {
9567 		ctsio->residual = 0;
9568 		ctsio->kern_data_len = alloc_len;
9569 		ctsio->kern_total_len = alloc_len;
9570 	}
9571 	ctsio->kern_data_resid = 0;
9572 	ctsio->kern_rel_offset = 0;
9573 	ctsio->kern_sg_entries = 0;
9574 
9575 	/*
9576 	 * The control device is always connected.  The disk device, on the
9577 	 * other hand, may not be online all the time.
9578 	 */
9579 	if (lun != NULL)
9580 		eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9581 				     lun->be_lun->lun_type;
9582 	else
9583 		eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9584 	eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9585 	scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9586 	/*
9587 	 * We support head of queue, ordered and simple tags.
9588 	 */
9589 	eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9590 	/*
9591 	 * Volatile cache supported.
9592 	 */
9593 	eid_ptr->flags3 = SVPD_EID_V_SUP;
9594 
9595 	/*
9596 	 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9597 	 * attention for a particular IT nexus on all LUNs once we report
9598 	 * it to that nexus once.  This bit is required as of SPC-4.
9599 	 */
9600 	eid_ptr->flags4 = SVPD_EID_LUICLT;
9601 
9602 	/*
9603 	 * XXX KDM in order to correctly answer this, we would need
9604 	 * information from the SIM to determine how much sense data it
9605 	 * can send.  So this would really be a path inquiry field, most
9606 	 * likely.  This can be set to a maximum of 252 according to SPC-4,
9607 	 * but the hardware may or may not be able to support that much.
9608 	 * 0 just means that the maximum sense data length is not reported.
9609 	 */
9610 	eid_ptr->max_sense_length = 0;
9611 
9612 	ctl_set_success(ctsio);
9613 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9614 	ctsio->be_move_done = ctl_config_move_done;
9615 	ctl_datamove((union ctl_io *)ctsio);
9616 	return (CTL_RETVAL_COMPLETE);
9617 }
9618 
9619 static int
9620 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9621 {
9622 	struct scsi_vpd_mode_page_policy *mpp_ptr;
9623 	struct ctl_lun *lun;
9624 	int data_len;
9625 
9626 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9627 
9628 	data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9629 	    sizeof(struct scsi_vpd_mode_page_policy_descr);
9630 
9631 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9632 	mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9633 	ctsio->kern_sg_entries = 0;
9634 
9635 	if (data_len < alloc_len) {
9636 		ctsio->residual = alloc_len - data_len;
9637 		ctsio->kern_data_len = data_len;
9638 		ctsio->kern_total_len = data_len;
9639 	} else {
9640 		ctsio->residual = 0;
9641 		ctsio->kern_data_len = alloc_len;
9642 		ctsio->kern_total_len = alloc_len;
9643 	}
9644 	ctsio->kern_data_resid = 0;
9645 	ctsio->kern_rel_offset = 0;
9646 	ctsio->kern_sg_entries = 0;
9647 
9648 	/*
9649 	 * The control device is always connected.  The disk device, on the
9650 	 * other hand, may not be online all the time.
9651 	 */
9652 	if (lun != NULL)
9653 		mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9654 				     lun->be_lun->lun_type;
9655 	else
9656 		mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9657 	mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9658 	scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9659 	mpp_ptr->descr[0].page_code = 0x3f;
9660 	mpp_ptr->descr[0].subpage_code = 0xff;
9661 	mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9662 
9663 	ctl_set_success(ctsio);
9664 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9665 	ctsio->be_move_done = ctl_config_move_done;
9666 	ctl_datamove((union ctl_io *)ctsio);
9667 	return (CTL_RETVAL_COMPLETE);
9668 }
9669 
9670 /*
9671  * SCSI VPD page 0x83, the Device Identification page.
9672  */
9673 static int
9674 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9675 {
9676 	struct scsi_vpd_device_id *devid_ptr;
9677 	struct scsi_vpd_id_descriptor *desc;
9678 	struct ctl_softc *softc;
9679 	struct ctl_lun *lun;
9680 	struct ctl_port *port;
9681 	int data_len, g;
9682 	uint8_t proto;
9683 
9684 	softc = control_softc;
9685 
9686 	port = ctl_io_port(&ctsio->io_hdr);
9687 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9688 
9689 	data_len = sizeof(struct scsi_vpd_device_id) +
9690 	    sizeof(struct scsi_vpd_id_descriptor) +
9691 		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9692 	    sizeof(struct scsi_vpd_id_descriptor) +
9693 		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9694 	if (lun && lun->lun_devid)
9695 		data_len += lun->lun_devid->len;
9696 	if (port && port->port_devid)
9697 		data_len += port->port_devid->len;
9698 	if (port && port->target_devid)
9699 		data_len += port->target_devid->len;
9700 
9701 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9702 	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9703 	ctsio->kern_sg_entries = 0;
9704 
9705 	if (data_len < alloc_len) {
9706 		ctsio->residual = alloc_len - data_len;
9707 		ctsio->kern_data_len = data_len;
9708 		ctsio->kern_total_len = data_len;
9709 	} else {
9710 		ctsio->residual = 0;
9711 		ctsio->kern_data_len = alloc_len;
9712 		ctsio->kern_total_len = alloc_len;
9713 	}
9714 	ctsio->kern_data_resid = 0;
9715 	ctsio->kern_rel_offset = 0;
9716 	ctsio->kern_sg_entries = 0;
9717 
9718 	/*
9719 	 * The control device is always connected.  The disk device, on the
9720 	 * other hand, may not be online all the time.
9721 	 */
9722 	if (lun != NULL)
9723 		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9724 				     lun->be_lun->lun_type;
9725 	else
9726 		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9727 	devid_ptr->page_code = SVPD_DEVICE_ID;
9728 	scsi_ulto2b(data_len - 4, devid_ptr->length);
9729 
9730 	if (port && port->port_type == CTL_PORT_FC)
9731 		proto = SCSI_PROTO_FC << 4;
9732 	else if (port && port->port_type == CTL_PORT_ISCSI)
9733 		proto = SCSI_PROTO_ISCSI << 4;
9734 	else
9735 		proto = SCSI_PROTO_SPI << 4;
9736 	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9737 
9738 	/*
9739 	 * We're using a LUN association here.  i.e., this device ID is a
9740 	 * per-LUN identifier.
9741 	 */
9742 	if (lun && lun->lun_devid) {
9743 		memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9744 		desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9745 		    lun->lun_devid->len);
9746 	}
9747 
9748 	/*
9749 	 * This is for the WWPN which is a port association.
9750 	 */
9751 	if (port && port->port_devid) {
9752 		memcpy(desc, port->port_devid->data, port->port_devid->len);
9753 		desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9754 		    port->port_devid->len);
9755 	}
9756 
9757 	/*
9758 	 * This is for the Relative Target Port(type 4h) identifier
9759 	 */
9760 	desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9761 	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9762 	    SVPD_ID_TYPE_RELTARG;
9763 	desc->length = 4;
9764 	scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9765 	desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9766 	    sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9767 
9768 	/*
9769 	 * This is for the Target Port Group(type 5h) identifier
9770 	 */
9771 	desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9772 	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9773 	    SVPD_ID_TYPE_TPORTGRP;
9774 	desc->length = 4;
9775 	if (softc->is_single ||
9776 	    (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9777 		g = 1;
9778 	else
9779 		g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9780 	scsi_ulto2b(g, &desc->identifier[2]);
9781 	desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9782 	    sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9783 
9784 	/*
9785 	 * This is for the Target identifier
9786 	 */
9787 	if (port && port->target_devid) {
9788 		memcpy(desc, port->target_devid->data, port->target_devid->len);
9789 	}
9790 
9791 	ctl_set_success(ctsio);
9792 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9793 	ctsio->be_move_done = ctl_config_move_done;
9794 	ctl_datamove((union ctl_io *)ctsio);
9795 	return (CTL_RETVAL_COMPLETE);
9796 }
9797 
9798 static int
9799 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9800 {
9801 	struct ctl_softc *softc = control_softc;
9802 	struct scsi_vpd_scsi_ports *sp;
9803 	struct scsi_vpd_port_designation *pd;
9804 	struct scsi_vpd_port_designation_cont *pdc;
9805 	struct ctl_lun *lun;
9806 	struct ctl_port *port;
9807 	int data_len, num_target_ports, iid_len, id_len;
9808 
9809 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9810 
9811 	num_target_ports = 0;
9812 	iid_len = 0;
9813 	id_len = 0;
9814 	mtx_lock(&softc->ctl_lock);
9815 	STAILQ_FOREACH(port, &softc->port_list, links) {
9816 		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9817 			continue;
9818 		if (lun != NULL &&
9819 		    ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9820 			continue;
9821 		num_target_ports++;
9822 		if (port->init_devid)
9823 			iid_len += port->init_devid->len;
9824 		if (port->port_devid)
9825 			id_len += port->port_devid->len;
9826 	}
9827 	mtx_unlock(&softc->ctl_lock);
9828 
9829 	data_len = sizeof(struct scsi_vpd_scsi_ports) +
9830 	    num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9831 	     sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9832 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9833 	sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9834 	ctsio->kern_sg_entries = 0;
9835 
9836 	if (data_len < alloc_len) {
9837 		ctsio->residual = alloc_len - data_len;
9838 		ctsio->kern_data_len = data_len;
9839 		ctsio->kern_total_len = data_len;
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 		sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9856 				  lun->be_lun->lun_type;
9857 	else
9858 		sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9859 
9860 	sp->page_code = SVPD_SCSI_PORTS;
9861 	scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9862 	    sp->page_length);
9863 	pd = &sp->design[0];
9864 
9865 	mtx_lock(&softc->ctl_lock);
9866 	STAILQ_FOREACH(port, &softc->port_list, links) {
9867 		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9868 			continue;
9869 		if (lun != NULL &&
9870 		    ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9871 			continue;
9872 		scsi_ulto2b(port->targ_port, pd->relative_port_id);
9873 		if (port->init_devid) {
9874 			iid_len = port->init_devid->len;
9875 			memcpy(pd->initiator_transportid,
9876 			    port->init_devid->data, port->init_devid->len);
9877 		} else
9878 			iid_len = 0;
9879 		scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9880 		pdc = (struct scsi_vpd_port_designation_cont *)
9881 		    (&pd->initiator_transportid[iid_len]);
9882 		if (port->port_devid) {
9883 			id_len = port->port_devid->len;
9884 			memcpy(pdc->target_port_descriptors,
9885 			    port->port_devid->data, port->port_devid->len);
9886 		} else
9887 			id_len = 0;
9888 		scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9889 		pd = (struct scsi_vpd_port_designation *)
9890 		    ((uint8_t *)pdc->target_port_descriptors + id_len);
9891 	}
9892 	mtx_unlock(&softc->ctl_lock);
9893 
9894 	ctl_set_success(ctsio);
9895 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9896 	ctsio->be_move_done = ctl_config_move_done;
9897 	ctl_datamove((union ctl_io *)ctsio);
9898 	return (CTL_RETVAL_COMPLETE);
9899 }
9900 
9901 static int
9902 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9903 {
9904 	struct scsi_vpd_block_limits *bl_ptr;
9905 	struct ctl_lun *lun;
9906 
9907 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9908 
9909 	ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9910 	bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9911 	ctsio->kern_sg_entries = 0;
9912 
9913 	if (sizeof(*bl_ptr) < alloc_len) {
9914 		ctsio->residual = alloc_len - sizeof(*bl_ptr);
9915 		ctsio->kern_data_len = sizeof(*bl_ptr);
9916 		ctsio->kern_total_len = sizeof(*bl_ptr);
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.  Need to change this
9929 	 * to figure out whether the disk device is actually online or not.
9930 	 */
9931 	if (lun != NULL)
9932 		bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9933 				  lun->be_lun->lun_type;
9934 	else
9935 		bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9936 
9937 	bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9938 	scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9939 	bl_ptr->max_cmp_write_len = 0xff;
9940 	scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9941 	if (lun != NULL) {
9942 		scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9943 		if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9944 			scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9945 			scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9946 			if (lun->be_lun->ublockexp != 0) {
9947 				scsi_ulto4b((1 << lun->be_lun->ublockexp),
9948 				    bl_ptr->opt_unmap_grain);
9949 				scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9950 				    bl_ptr->unmap_grain_align);
9951 			}
9952 		}
9953 		scsi_ulto4b(lun->be_lun->atomicblock,
9954 		    bl_ptr->max_atomic_transfer_length);
9955 		scsi_ulto4b(0, bl_ptr->atomic_alignment);
9956 		scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9957 		scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9958 		scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9959 	}
9960 	scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9961 
9962 	ctl_set_success(ctsio);
9963 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9964 	ctsio->be_move_done = ctl_config_move_done;
9965 	ctl_datamove((union ctl_io *)ctsio);
9966 	return (CTL_RETVAL_COMPLETE);
9967 }
9968 
9969 static int
9970 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9971 {
9972 	struct scsi_vpd_block_device_characteristics *bdc_ptr;
9973 	struct ctl_lun *lun;
9974 	const char *value;
9975 	u_int i;
9976 
9977 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9978 
9979 	ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9980 	bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9981 	ctsio->kern_sg_entries = 0;
9982 
9983 	if (sizeof(*bdc_ptr) < alloc_len) {
9984 		ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9985 		ctsio->kern_data_len = sizeof(*bdc_ptr);
9986 		ctsio->kern_total_len = sizeof(*bdc_ptr);
9987 	} else {
9988 		ctsio->residual = 0;
9989 		ctsio->kern_data_len = alloc_len;
9990 		ctsio->kern_total_len = alloc_len;
9991 	}
9992 	ctsio->kern_data_resid = 0;
9993 	ctsio->kern_rel_offset = 0;
9994 	ctsio->kern_sg_entries = 0;
9995 
9996 	/*
9997 	 * The control device is always connected.  The disk device, on the
9998 	 * other hand, may not be online all the time.  Need to change this
9999 	 * to figure out whether the disk device is actually online or not.
10000 	 */
10001 	if (lun != NULL)
10002 		bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10003 				  lun->be_lun->lun_type;
10004 	else
10005 		bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10006 	bdc_ptr->page_code = SVPD_BDC;
10007 	scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10008 	if (lun != NULL &&
10009 	    (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10010 		i = strtol(value, NULL, 0);
10011 	else
10012 		i = CTL_DEFAULT_ROTATION_RATE;
10013 	scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10014 	if (lun != NULL &&
10015 	    (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10016 		i = strtol(value, NULL, 0);
10017 	else
10018 		i = 0;
10019 	bdc_ptr->wab_wac_ff = (i & 0x0f);
10020 	bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10021 
10022 	ctl_set_success(ctsio);
10023 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10024 	ctsio->be_move_done = ctl_config_move_done;
10025 	ctl_datamove((union ctl_io *)ctsio);
10026 	return (CTL_RETVAL_COMPLETE);
10027 }
10028 
10029 static int
10030 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10031 {
10032 	struct scsi_vpd_logical_block_prov *lbp_ptr;
10033 	struct ctl_lun *lun;
10034 
10035 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10036 
10037 	ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10038 	lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10039 	ctsio->kern_sg_entries = 0;
10040 
10041 	if (sizeof(*lbp_ptr) < alloc_len) {
10042 		ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10043 		ctsio->kern_data_len = sizeof(*lbp_ptr);
10044 		ctsio->kern_total_len = sizeof(*lbp_ptr);
10045 	} else {
10046 		ctsio->residual = 0;
10047 		ctsio->kern_data_len = alloc_len;
10048 		ctsio->kern_total_len = alloc_len;
10049 	}
10050 	ctsio->kern_data_resid = 0;
10051 	ctsio->kern_rel_offset = 0;
10052 	ctsio->kern_sg_entries = 0;
10053 
10054 	/*
10055 	 * The control device is always connected.  The disk device, on the
10056 	 * other hand, may not be online all the time.  Need to change this
10057 	 * to figure out whether the disk device is actually online or not.
10058 	 */
10059 	if (lun != NULL)
10060 		lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10061 				  lun->be_lun->lun_type;
10062 	else
10063 		lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10064 
10065 	lbp_ptr->page_code = SVPD_LBP;
10066 	scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10067 	lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10068 	if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10069 		lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10070 		    SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10071 		lbp_ptr->prov_type = SVPD_LBP_THIN;
10072 	}
10073 
10074 	ctl_set_success(ctsio);
10075 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10076 	ctsio->be_move_done = ctl_config_move_done;
10077 	ctl_datamove((union ctl_io *)ctsio);
10078 	return (CTL_RETVAL_COMPLETE);
10079 }
10080 
10081 /*
10082  * INQUIRY with the EVPD bit set.
10083  */
10084 static int
10085 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10086 {
10087 	struct ctl_lun *lun;
10088 	struct scsi_inquiry *cdb;
10089 	int alloc_len, retval;
10090 
10091 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10092 	cdb = (struct scsi_inquiry *)ctsio->cdb;
10093 	alloc_len = scsi_2btoul(cdb->length);
10094 
10095 	switch (cdb->page_code) {
10096 	case SVPD_SUPPORTED_PAGES:
10097 		retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10098 		break;
10099 	case SVPD_UNIT_SERIAL_NUMBER:
10100 		retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10101 		break;
10102 	case SVPD_DEVICE_ID:
10103 		retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10104 		break;
10105 	case SVPD_EXTENDED_INQUIRY_DATA:
10106 		retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10107 		break;
10108 	case SVPD_MODE_PAGE_POLICY:
10109 		retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10110 		break;
10111 	case SVPD_SCSI_PORTS:
10112 		retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10113 		break;
10114 	case SVPD_SCSI_TPC:
10115 		retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10116 		break;
10117 	case SVPD_BLOCK_LIMITS:
10118 		if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10119 			goto err;
10120 		retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10121 		break;
10122 	case SVPD_BDC:
10123 		if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10124 			goto err;
10125 		retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10126 		break;
10127 	case SVPD_LBP:
10128 		if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10129 			goto err;
10130 		retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10131 		break;
10132 	default:
10133 err:
10134 		ctl_set_invalid_field(ctsio,
10135 				      /*sks_valid*/ 1,
10136 				      /*command*/ 1,
10137 				      /*field*/ 2,
10138 				      /*bit_valid*/ 0,
10139 				      /*bit*/ 0);
10140 		ctl_done((union ctl_io *)ctsio);
10141 		retval = CTL_RETVAL_COMPLETE;
10142 		break;
10143 	}
10144 
10145 	return (retval);
10146 }
10147 
10148 /*
10149  * Standard INQUIRY data.
10150  */
10151 static int
10152 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10153 {
10154 	struct scsi_inquiry_data *inq_ptr;
10155 	struct scsi_inquiry *cdb;
10156 	struct ctl_softc *softc = control_softc;
10157 	struct ctl_port *port;
10158 	struct ctl_lun *lun;
10159 	char *val;
10160 	uint32_t alloc_len, data_len;
10161 	ctl_port_type port_type;
10162 
10163 	port = ctl_io_port(&ctsio->io_hdr);
10164 	port_type = port->port_type;
10165 	if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10166 		port_type = CTL_PORT_SCSI;
10167 
10168 	lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10169 	cdb = (struct scsi_inquiry *)ctsio->cdb;
10170 	alloc_len = scsi_2btoul(cdb->length);
10171 
10172 	/*
10173 	 * We malloc the full inquiry data size here and fill it
10174 	 * in.  If the user only asks for less, we'll give him
10175 	 * that much.
10176 	 */
10177 	data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10178 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10179 	inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10180 	ctsio->kern_sg_entries = 0;
10181 	ctsio->kern_data_resid = 0;
10182 	ctsio->kern_rel_offset = 0;
10183 
10184 	if (data_len < alloc_len) {
10185 		ctsio->residual = alloc_len - data_len;
10186 		ctsio->kern_data_len = data_len;
10187 		ctsio->kern_total_len = data_len;
10188 	} else {
10189 		ctsio->residual = 0;
10190 		ctsio->kern_data_len = alloc_len;
10191 		ctsio->kern_total_len = alloc_len;
10192 	}
10193 
10194 	if (lun != NULL) {
10195 		if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10196 		    softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10197 			inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10198 			    lun->be_lun->lun_type;
10199 		} else {
10200 			inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10201 			    lun->be_lun->lun_type;
10202 		}
10203 		if (lun->flags & CTL_LUN_REMOVABLE)
10204 			inq_ptr->dev_qual2 |= SID_RMB;
10205 	} else
10206 		inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10207 
10208 	/* RMB in byte 2 is 0 */
10209 	inq_ptr->version = SCSI_REV_SPC4;
10210 
10211 	/*
10212 	 * According to SAM-3, even if a device only supports a single
10213 	 * level of LUN addressing, it should still set the HISUP bit:
10214 	 *
10215 	 * 4.9.1 Logical unit numbers overview
10216 	 *
10217 	 * All logical unit number formats described in this standard are
10218 	 * hierarchical in structure even when only a single level in that
10219 	 * hierarchy is used. The HISUP bit shall be set to one in the
10220 	 * standard INQUIRY data (see SPC-2) when any logical unit number
10221 	 * format described in this standard is used.  Non-hierarchical
10222 	 * formats are outside the scope of this standard.
10223 	 *
10224 	 * Therefore we set the HiSup bit here.
10225 	 *
10226 	 * The response format is 2, per SPC-3.
10227 	 */
10228 	inq_ptr->response_format = SID_HiSup | 2;
10229 
10230 	inq_ptr->additional_length = data_len -
10231 	    (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10232 	CTL_DEBUG_PRINT(("additional_length = %d\n",
10233 			 inq_ptr->additional_length));
10234 
10235 	inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10236 	if (port_type == CTL_PORT_SCSI)
10237 		inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10238 	inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10239 	inq_ptr->flags = SID_CmdQue;
10240 	if (port_type == CTL_PORT_SCSI)
10241 		inq_ptr->flags |= SID_WBus16 | SID_Sync;
10242 
10243 	/*
10244 	 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10245 	 * We have 8 bytes for the vendor name, and 16 bytes for the device
10246 	 * name and 4 bytes for the revision.
10247 	 */
10248 	if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10249 	    "vendor")) == NULL) {
10250 		strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10251 	} else {
10252 		memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10253 		strncpy(inq_ptr->vendor, val,
10254 		    min(sizeof(inq_ptr->vendor), strlen(val)));
10255 	}
10256 	if (lun == NULL) {
10257 		strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10258 		    sizeof(inq_ptr->product));
10259 	} else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10260 		switch (lun->be_lun->lun_type) {
10261 		case T_DIRECT:
10262 			strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10263 			    sizeof(inq_ptr->product));
10264 			break;
10265 		case T_PROCESSOR:
10266 			strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10267 			    sizeof(inq_ptr->product));
10268 			break;
10269 		case T_CDROM:
10270 			strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10271 			    sizeof(inq_ptr->product));
10272 			break;
10273 		default:
10274 			strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10275 			    sizeof(inq_ptr->product));
10276 			break;
10277 		}
10278 	} else {
10279 		memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10280 		strncpy(inq_ptr->product, val,
10281 		    min(sizeof(inq_ptr->product), strlen(val)));
10282 	}
10283 
10284 	/*
10285 	 * XXX make this a macro somewhere so it automatically gets
10286 	 * incremented when we make changes.
10287 	 */
10288 	if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10289 	    "revision")) == NULL) {
10290 		strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10291 	} else {
10292 		memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10293 		strncpy(inq_ptr->revision, val,
10294 		    min(sizeof(inq_ptr->revision), strlen(val)));
10295 	}
10296 
10297 	/*
10298 	 * For parallel SCSI, we support double transition and single
10299 	 * transition clocking.  We also support QAS (Quick Arbitration
10300 	 * and Selection) and Information Unit transfers on both the
10301 	 * control and array devices.
10302 	 */
10303 	if (port_type == CTL_PORT_SCSI)
10304 		inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10305 				    SID_SPI_IUS;
10306 
10307 	/* SAM-5 (no version claimed) */
10308 	scsi_ulto2b(0x00A0, inq_ptr->version1);
10309 	/* SPC-4 (no version claimed) */
10310 	scsi_ulto2b(0x0460, inq_ptr->version2);
10311 	if (port_type == CTL_PORT_FC) {
10312 		/* FCP-2 ANSI INCITS.350:2003 */
10313 		scsi_ulto2b(0x0917, inq_ptr->version3);
10314 	} else if (port_type == CTL_PORT_SCSI) {
10315 		/* SPI-4 ANSI INCITS.362:200x */
10316 		scsi_ulto2b(0x0B56, inq_ptr->version3);
10317 	} else if (port_type == CTL_PORT_ISCSI) {
10318 		/* iSCSI (no version claimed) */
10319 		scsi_ulto2b(0x0960, inq_ptr->version3);
10320 	} else if (port_type == CTL_PORT_SAS) {
10321 		/* SAS (no version claimed) */
10322 		scsi_ulto2b(0x0BE0, inq_ptr->version3);
10323 	}
10324 
10325 	if (lun == NULL) {
10326 		/* SBC-4 (no version claimed) */
10327 		scsi_ulto2b(0x0600, inq_ptr->version4);
10328 	} else {
10329 		switch (lun->be_lun->lun_type) {
10330 		case T_DIRECT:
10331 			/* SBC-4 (no version claimed) */
10332 			scsi_ulto2b(0x0600, inq_ptr->version4);
10333 			break;
10334 		case T_PROCESSOR:
10335 			break;
10336 		case T_CDROM:
10337 			/* MMC-6 (no version claimed) */
10338 			scsi_ulto2b(0x04E0, inq_ptr->version4);
10339 			break;
10340 		default:
10341 			break;
10342 		}
10343 	}
10344 
10345 	ctl_set_success(ctsio);
10346 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10347 	ctsio->be_move_done = ctl_config_move_done;
10348 	ctl_datamove((union ctl_io *)ctsio);
10349 	return (CTL_RETVAL_COMPLETE);
10350 }
10351 
10352 int
10353 ctl_inquiry(struct ctl_scsiio *ctsio)
10354 {
10355 	struct scsi_inquiry *cdb;
10356 	int retval;
10357 
10358 	CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10359 
10360 	cdb = (struct scsi_inquiry *)ctsio->cdb;
10361 	if (cdb->byte2 & SI_EVPD)
10362 		retval = ctl_inquiry_evpd(ctsio);
10363 	else if (cdb->page_code == 0)
10364 		retval = ctl_inquiry_std(ctsio);
10365 	else {
10366 		ctl_set_invalid_field(ctsio,
10367 				      /*sks_valid*/ 1,
10368 				      /*command*/ 1,
10369 				      /*field*/ 2,
10370 				      /*bit_valid*/ 0,
10371 				      /*bit*/ 0);
10372 		ctl_done((union ctl_io *)ctsio);
10373 		return (CTL_RETVAL_COMPLETE);
10374 	}
10375 
10376 	return (retval);
10377 }
10378 
10379 int
10380 ctl_get_config(struct ctl_scsiio *ctsio)
10381 {
10382 	struct scsi_get_config_header *hdr;
10383 	struct scsi_get_config_feature *feature;
10384 	struct scsi_get_config *cdb;
10385 	struct ctl_lun *lun;
10386 	uint32_t alloc_len, data_len;
10387 	int rt, starting;
10388 
10389 	lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10390 	cdb = (struct scsi_get_config *)ctsio->cdb;
10391 	rt = (cdb->rt & SGC_RT_MASK);
10392 	starting = scsi_2btoul(cdb->starting_feature);
10393 	alloc_len = scsi_2btoul(cdb->length);
10394 
10395 	data_len = sizeof(struct scsi_get_config_header) +
10396 	    sizeof(struct scsi_get_config_feature) + 8 +
10397 	    sizeof(struct scsi_get_config_feature) + 8 +
10398 	    sizeof(struct scsi_get_config_feature) + 4 +
10399 	    sizeof(struct scsi_get_config_feature) + 4 +
10400 	    sizeof(struct scsi_get_config_feature) + 8 +
10401 	    sizeof(struct scsi_get_config_feature) +
10402 	    sizeof(struct scsi_get_config_feature) + 4 +
10403 	    sizeof(struct scsi_get_config_feature) + 4 +
10404 	    sizeof(struct scsi_get_config_feature) + 4 +
10405 	    sizeof(struct scsi_get_config_feature) + 4 +
10406 	    sizeof(struct scsi_get_config_feature) + 4 +
10407 	    sizeof(struct scsi_get_config_feature) + 4;
10408 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10409 	ctsio->kern_sg_entries = 0;
10410 	ctsio->kern_data_resid = 0;
10411 	ctsio->kern_rel_offset = 0;
10412 
10413 	hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10414 	if (lun->flags & CTL_LUN_NO_MEDIA)
10415 		scsi_ulto2b(0x0000, hdr->current_profile);
10416 	else
10417 		scsi_ulto2b(0x0010, hdr->current_profile);
10418 	feature = (struct scsi_get_config_feature *)(hdr + 1);
10419 
10420 	if (starting > 0x003b)
10421 		goto done;
10422 	if (starting > 0x003a)
10423 		goto f3b;
10424 	if (starting > 0x002b)
10425 		goto f3a;
10426 	if (starting > 0x002a)
10427 		goto f2b;
10428 	if (starting > 0x001f)
10429 		goto f2a;
10430 	if (starting > 0x001e)
10431 		goto f1f;
10432 	if (starting > 0x001d)
10433 		goto f1e;
10434 	if (starting > 0x0010)
10435 		goto f1d;
10436 	if (starting > 0x0003)
10437 		goto f10;
10438 	if (starting > 0x0002)
10439 		goto f3;
10440 	if (starting > 0x0001)
10441 		goto f2;
10442 	if (starting > 0x0000)
10443 		goto f1;
10444 
10445 	/* Profile List */
10446 	scsi_ulto2b(0x0000, feature->feature_code);
10447 	feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10448 	feature->add_length = 8;
10449 	scsi_ulto2b(0x0008, &feature->feature_data[0]);	/* CD-ROM */
10450 	feature->feature_data[2] = 0x00;
10451 	scsi_ulto2b(0x0010, &feature->feature_data[4]);	/* DVD-ROM */
10452 	feature->feature_data[6] = 0x01;
10453 	feature = (struct scsi_get_config_feature *)
10454 	    &feature->feature_data[feature->add_length];
10455 
10456 f1:	/* Core */
10457 	scsi_ulto2b(0x0001, feature->feature_code);
10458 	feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10459 	feature->add_length = 8;
10460 	scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10461 	feature->feature_data[4] = 0x03;
10462 	feature = (struct scsi_get_config_feature *)
10463 	    &feature->feature_data[feature->add_length];
10464 
10465 f2:	/* Morphing */
10466 	scsi_ulto2b(0x0002, feature->feature_code);
10467 	feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10468 	feature->add_length = 4;
10469 	feature->feature_data[0] = 0x02;
10470 	feature = (struct scsi_get_config_feature *)
10471 	    &feature->feature_data[feature->add_length];
10472 
10473 f3:	/* Removable Medium */
10474 	scsi_ulto2b(0x0003, feature->feature_code);
10475 	feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10476 	feature->add_length = 4;
10477 	feature->feature_data[0] = 0x39;
10478 	feature = (struct scsi_get_config_feature *)
10479 	    &feature->feature_data[feature->add_length];
10480 
10481 	if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10482 		goto done;
10483 
10484 f10:	/* Random Read */
10485 	scsi_ulto2b(0x0010, feature->feature_code);
10486 	feature->flags = 0x00;
10487 	if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10488 		feature->flags |= SGC_F_CURRENT;
10489 	feature->add_length = 8;
10490 	scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10491 	scsi_ulto2b(1, &feature->feature_data[4]);
10492 	feature->feature_data[6] = 0x00;
10493 	feature = (struct scsi_get_config_feature *)
10494 	    &feature->feature_data[feature->add_length];
10495 
10496 f1d:	/* Multi-Read */
10497 	scsi_ulto2b(0x001D, feature->feature_code);
10498 	feature->flags = 0x00;
10499 	if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10500 		feature->flags |= SGC_F_CURRENT;
10501 	feature->add_length = 0;
10502 	feature = (struct scsi_get_config_feature *)
10503 	    &feature->feature_data[feature->add_length];
10504 
10505 f1e:	/* CD Read */
10506 	scsi_ulto2b(0x001E, feature->feature_code);
10507 	feature->flags = 0x00;
10508 	if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10509 		feature->flags |= SGC_F_CURRENT;
10510 	feature->add_length = 4;
10511 	feature->feature_data[0] = 0x00;
10512 	feature = (struct scsi_get_config_feature *)
10513 	    &feature->feature_data[feature->add_length];
10514 
10515 f1f:	/* DVD Read */
10516 	scsi_ulto2b(0x001F, feature->feature_code);
10517 	feature->flags = 0x08;
10518 	if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10519 		feature->flags |= SGC_F_CURRENT;
10520 	feature->add_length = 4;
10521 	feature->feature_data[0] = 0x01;
10522 	feature->feature_data[2] = 0x03;
10523 	feature = (struct scsi_get_config_feature *)
10524 	    &feature->feature_data[feature->add_length];
10525 
10526 f2a:	/* DVD+RW */
10527 	scsi_ulto2b(0x002A, feature->feature_code);
10528 	feature->flags = 0x04;
10529 	if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10530 		feature->flags |= SGC_F_CURRENT;
10531 	feature->add_length = 4;
10532 	feature->feature_data[0] = 0x00;
10533 	feature->feature_data[1] = 0x00;
10534 	feature = (struct scsi_get_config_feature *)
10535 	    &feature->feature_data[feature->add_length];
10536 
10537 f2b:	/* DVD+R */
10538 	scsi_ulto2b(0x002B, feature->feature_code);
10539 	feature->flags = 0x00;
10540 	if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10541 		feature->flags |= SGC_F_CURRENT;
10542 	feature->add_length = 4;
10543 	feature->feature_data[0] = 0x00;
10544 	feature = (struct scsi_get_config_feature *)
10545 	    &feature->feature_data[feature->add_length];
10546 
10547 f3a:	/* DVD+RW Dual Layer */
10548 	scsi_ulto2b(0x003A, feature->feature_code);
10549 	feature->flags = 0x00;
10550 	if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10551 		feature->flags |= SGC_F_CURRENT;
10552 	feature->add_length = 4;
10553 	feature->feature_data[0] = 0x00;
10554 	feature->feature_data[1] = 0x00;
10555 	feature = (struct scsi_get_config_feature *)
10556 	    &feature->feature_data[feature->add_length];
10557 
10558 f3b:	/* DVD+R Dual Layer */
10559 	scsi_ulto2b(0x003B, feature->feature_code);
10560 	feature->flags = 0x00;
10561 	if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10562 		feature->flags |= SGC_F_CURRENT;
10563 	feature->add_length = 4;
10564 	feature->feature_data[0] = 0x00;
10565 	feature = (struct scsi_get_config_feature *)
10566 	    &feature->feature_data[feature->add_length];
10567 
10568 done:
10569 	data_len = (uint8_t *)feature - (uint8_t *)hdr;
10570 	if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10571 		feature = (struct scsi_get_config_feature *)(hdr + 1);
10572 		if (scsi_2btoul(feature->feature_code) == starting)
10573 			feature = (struct scsi_get_config_feature *)
10574 			    &feature->feature_data[feature->add_length];
10575 		data_len = (uint8_t *)feature - (uint8_t *)hdr;
10576 	}
10577 	scsi_ulto4b(data_len - 4, hdr->data_length);
10578 	if (data_len < alloc_len) {
10579 		ctsio->residual = alloc_len - data_len;
10580 		ctsio->kern_data_len = data_len;
10581 		ctsio->kern_total_len = data_len;
10582 	} else {
10583 		ctsio->residual = 0;
10584 		ctsio->kern_data_len = alloc_len;
10585 		ctsio->kern_total_len = alloc_len;
10586 	}
10587 
10588 	ctl_set_success(ctsio);
10589 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10590 	ctsio->be_move_done = ctl_config_move_done;
10591 	ctl_datamove((union ctl_io *)ctsio);
10592 	return (CTL_RETVAL_COMPLETE);
10593 }
10594 
10595 int
10596 ctl_get_event_status(struct ctl_scsiio *ctsio)
10597 {
10598 	struct scsi_get_event_status_header *hdr;
10599 	struct scsi_get_event_status *cdb;
10600 	struct ctl_lun *lun;
10601 	uint32_t alloc_len, data_len;
10602 	int notif_class;
10603 
10604 	lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10605 	cdb = (struct scsi_get_event_status *)ctsio->cdb;
10606 	if ((cdb->byte2 & SGESN_POLLED) == 0) {
10607 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10608 		    /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10609 		ctl_done((union ctl_io *)ctsio);
10610 		return (CTL_RETVAL_COMPLETE);
10611 	}
10612 	notif_class = cdb->notif_class;
10613 	alloc_len = scsi_2btoul(cdb->length);
10614 
10615 	data_len = sizeof(struct scsi_get_event_status_header);
10616 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10617 	ctsio->kern_sg_entries = 0;
10618 	ctsio->kern_data_resid = 0;
10619 	ctsio->kern_rel_offset = 0;
10620 
10621 	if (data_len < alloc_len) {
10622 		ctsio->residual = alloc_len - data_len;
10623 		ctsio->kern_data_len = data_len;
10624 		ctsio->kern_total_len = data_len;
10625 	} else {
10626 		ctsio->residual = 0;
10627 		ctsio->kern_data_len = alloc_len;
10628 		ctsio->kern_total_len = alloc_len;
10629 	}
10630 
10631 	hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10632 	scsi_ulto2b(0, hdr->descr_length);
10633 	hdr->nea_class = SGESN_NEA;
10634 	hdr->supported_class = 0;
10635 
10636 	ctl_set_success(ctsio);
10637 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10638 	ctsio->be_move_done = ctl_config_move_done;
10639 	ctl_datamove((union ctl_io *)ctsio);
10640 	return (CTL_RETVAL_COMPLETE);
10641 }
10642 
10643 int
10644 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10645 {
10646 	struct scsi_mechanism_status_header *hdr;
10647 	struct scsi_mechanism_status *cdb;
10648 	struct ctl_lun *lun;
10649 	uint32_t alloc_len, data_len;
10650 
10651 	lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10652 	cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10653 	alloc_len = scsi_2btoul(cdb->length);
10654 
10655 	data_len = sizeof(struct scsi_mechanism_status_header);
10656 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10657 	ctsio->kern_sg_entries = 0;
10658 	ctsio->kern_data_resid = 0;
10659 	ctsio->kern_rel_offset = 0;
10660 
10661 	if (data_len < alloc_len) {
10662 		ctsio->residual = alloc_len - data_len;
10663 		ctsio->kern_data_len = data_len;
10664 		ctsio->kern_total_len = data_len;
10665 	} else {
10666 		ctsio->residual = 0;
10667 		ctsio->kern_data_len = alloc_len;
10668 		ctsio->kern_total_len = alloc_len;
10669 	}
10670 
10671 	hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10672 	hdr->state1 = 0x00;
10673 	hdr->state2 = 0xe0;
10674 	scsi_ulto3b(0, hdr->lba);
10675 	hdr->slots_num = 0;
10676 	scsi_ulto2b(0, hdr->slots_length);
10677 
10678 	ctl_set_success(ctsio);
10679 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10680 	ctsio->be_move_done = ctl_config_move_done;
10681 	ctl_datamove((union ctl_io *)ctsio);
10682 	return (CTL_RETVAL_COMPLETE);
10683 }
10684 
10685 static void
10686 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10687 {
10688 
10689 	lba += 150;
10690 	buf[0] = 0;
10691 	buf[1] = bin2bcd((lba / 75) / 60);
10692 	buf[2] = bin2bcd((lba / 75) % 60);
10693 	buf[3] = bin2bcd(lba % 75);
10694 }
10695 
10696 int
10697 ctl_read_toc(struct ctl_scsiio *ctsio)
10698 {
10699 	struct scsi_read_toc_hdr *hdr;
10700 	struct scsi_read_toc_type01_descr *descr;
10701 	struct scsi_read_toc *cdb;
10702 	struct ctl_lun *lun;
10703 	uint32_t alloc_len, data_len;
10704 	int format, msf;
10705 
10706 	lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10707 	cdb = (struct scsi_read_toc *)ctsio->cdb;
10708 	msf = (cdb->byte2 & CD_MSF) != 0;
10709 	format = cdb->format;
10710 	alloc_len = scsi_2btoul(cdb->data_len);
10711 
10712 	data_len = sizeof(struct scsi_read_toc_hdr);
10713 	if (format == 0)
10714 		data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10715 	else
10716 		data_len += sizeof(struct scsi_read_toc_type01_descr);
10717 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10718 	ctsio->kern_sg_entries = 0;
10719 	ctsio->kern_data_resid = 0;
10720 	ctsio->kern_rel_offset = 0;
10721 
10722 	if (data_len < alloc_len) {
10723 		ctsio->residual = alloc_len - data_len;
10724 		ctsio->kern_data_len = data_len;
10725 		ctsio->kern_total_len = data_len;
10726 	} else {
10727 		ctsio->residual = 0;
10728 		ctsio->kern_data_len = alloc_len;
10729 		ctsio->kern_total_len = alloc_len;
10730 	}
10731 
10732 	hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10733 	if (format == 0) {
10734 		scsi_ulto2b(0x12, hdr->data_length);
10735 		hdr->first = 1;
10736 		hdr->last = 1;
10737 		descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10738 		descr->addr_ctl = 0x14;
10739 		descr->track_number = 1;
10740 		if (msf)
10741 			ctl_ultomsf(0, descr->track_start);
10742 		else
10743 			scsi_ulto4b(0, descr->track_start);
10744 		descr++;
10745 		descr->addr_ctl = 0x14;
10746 		descr->track_number = 0xaa;
10747 		if (msf)
10748 			ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10749 		else
10750 			scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10751 	} else {
10752 		scsi_ulto2b(0x0a, hdr->data_length);
10753 		hdr->first = 1;
10754 		hdr->last = 1;
10755 		descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10756 		descr->addr_ctl = 0x14;
10757 		descr->track_number = 1;
10758 		if (msf)
10759 			ctl_ultomsf(0, descr->track_start);
10760 		else
10761 			scsi_ulto4b(0, descr->track_start);
10762 	}
10763 
10764 	ctl_set_success(ctsio);
10765 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10766 	ctsio->be_move_done = ctl_config_move_done;
10767 	ctl_datamove((union ctl_io *)ctsio);
10768 	return (CTL_RETVAL_COMPLETE);
10769 }
10770 
10771 /*
10772  * For known CDB types, parse the LBA and length.
10773  */
10774 static int
10775 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10776 {
10777 	if (io->io_hdr.io_type != CTL_IO_SCSI)
10778 		return (1);
10779 
10780 	switch (io->scsiio.cdb[0]) {
10781 	case COMPARE_AND_WRITE: {
10782 		struct scsi_compare_and_write *cdb;
10783 
10784 		cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10785 
10786 		*lba = scsi_8btou64(cdb->addr);
10787 		*len = cdb->length;
10788 		break;
10789 	}
10790 	case READ_6:
10791 	case WRITE_6: {
10792 		struct scsi_rw_6 *cdb;
10793 
10794 		cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10795 
10796 		*lba = scsi_3btoul(cdb->addr);
10797 		/* only 5 bits are valid in the most significant address byte */
10798 		*lba &= 0x1fffff;
10799 		*len = cdb->length;
10800 		break;
10801 	}
10802 	case READ_10:
10803 	case WRITE_10: {
10804 		struct scsi_rw_10 *cdb;
10805 
10806 		cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10807 
10808 		*lba = scsi_4btoul(cdb->addr);
10809 		*len = scsi_2btoul(cdb->length);
10810 		break;
10811 	}
10812 	case WRITE_VERIFY_10: {
10813 		struct scsi_write_verify_10 *cdb;
10814 
10815 		cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10816 
10817 		*lba = scsi_4btoul(cdb->addr);
10818 		*len = scsi_2btoul(cdb->length);
10819 		break;
10820 	}
10821 	case READ_12:
10822 	case WRITE_12: {
10823 		struct scsi_rw_12 *cdb;
10824 
10825 		cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10826 
10827 		*lba = scsi_4btoul(cdb->addr);
10828 		*len = scsi_4btoul(cdb->length);
10829 		break;
10830 	}
10831 	case WRITE_VERIFY_12: {
10832 		struct scsi_write_verify_12 *cdb;
10833 
10834 		cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10835 
10836 		*lba = scsi_4btoul(cdb->addr);
10837 		*len = scsi_4btoul(cdb->length);
10838 		break;
10839 	}
10840 	case READ_16:
10841 	case WRITE_16: {
10842 		struct scsi_rw_16 *cdb;
10843 
10844 		cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10845 
10846 		*lba = scsi_8btou64(cdb->addr);
10847 		*len = scsi_4btoul(cdb->length);
10848 		break;
10849 	}
10850 	case WRITE_ATOMIC_16: {
10851 		struct scsi_write_atomic_16 *cdb;
10852 
10853 		cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10854 
10855 		*lba = scsi_8btou64(cdb->addr);
10856 		*len = scsi_2btoul(cdb->length);
10857 		break;
10858 	}
10859 	case WRITE_VERIFY_16: {
10860 		struct scsi_write_verify_16 *cdb;
10861 
10862 		cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10863 
10864 		*lba = scsi_8btou64(cdb->addr);
10865 		*len = scsi_4btoul(cdb->length);
10866 		break;
10867 	}
10868 	case WRITE_SAME_10: {
10869 		struct scsi_write_same_10 *cdb;
10870 
10871 		cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10872 
10873 		*lba = scsi_4btoul(cdb->addr);
10874 		*len = scsi_2btoul(cdb->length);
10875 		break;
10876 	}
10877 	case WRITE_SAME_16: {
10878 		struct scsi_write_same_16 *cdb;
10879 
10880 		cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10881 
10882 		*lba = scsi_8btou64(cdb->addr);
10883 		*len = scsi_4btoul(cdb->length);
10884 		break;
10885 	}
10886 	case VERIFY_10: {
10887 		struct scsi_verify_10 *cdb;
10888 
10889 		cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10890 
10891 		*lba = scsi_4btoul(cdb->addr);
10892 		*len = scsi_2btoul(cdb->length);
10893 		break;
10894 	}
10895 	case VERIFY_12: {
10896 		struct scsi_verify_12 *cdb;
10897 
10898 		cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10899 
10900 		*lba = scsi_4btoul(cdb->addr);
10901 		*len = scsi_4btoul(cdb->length);
10902 		break;
10903 	}
10904 	case VERIFY_16: {
10905 		struct scsi_verify_16 *cdb;
10906 
10907 		cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10908 
10909 		*lba = scsi_8btou64(cdb->addr);
10910 		*len = scsi_4btoul(cdb->length);
10911 		break;
10912 	}
10913 	case UNMAP: {
10914 		*lba = 0;
10915 		*len = UINT64_MAX;
10916 		break;
10917 	}
10918 	case SERVICE_ACTION_IN: {	/* GET LBA STATUS */
10919 		struct scsi_get_lba_status *cdb;
10920 
10921 		cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10922 		*lba = scsi_8btou64(cdb->addr);
10923 		*len = UINT32_MAX;
10924 		break;
10925 	}
10926 	default:
10927 		return (1);
10928 		break; /* NOTREACHED */
10929 	}
10930 
10931 	return (0);
10932 }
10933 
10934 static ctl_action
10935 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10936     bool seq)
10937 {
10938 	uint64_t endlba1, endlba2;
10939 
10940 	endlba1 = lba1 + len1 - (seq ? 0 : 1);
10941 	endlba2 = lba2 + len2 - 1;
10942 
10943 	if ((endlba1 < lba2) || (endlba2 < lba1))
10944 		return (CTL_ACTION_PASS);
10945 	else
10946 		return (CTL_ACTION_BLOCK);
10947 }
10948 
10949 static int
10950 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10951 {
10952 	struct ctl_ptr_len_flags *ptrlen;
10953 	struct scsi_unmap_desc *buf, *end, *range;
10954 	uint64_t lba;
10955 	uint32_t len;
10956 
10957 	/* If not UNMAP -- go other way. */
10958 	if (io->io_hdr.io_type != CTL_IO_SCSI ||
10959 	    io->scsiio.cdb[0] != UNMAP)
10960 		return (CTL_ACTION_ERROR);
10961 
10962 	/* If UNMAP without data -- block and wait for data. */
10963 	ptrlen = (struct ctl_ptr_len_flags *)
10964 	    &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10965 	if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10966 	    ptrlen->ptr == NULL)
10967 		return (CTL_ACTION_BLOCK);
10968 
10969 	/* UNMAP with data -- check for collision. */
10970 	buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10971 	end = buf + ptrlen->len / sizeof(*buf);
10972 	for (range = buf; range < end; range++) {
10973 		lba = scsi_8btou64(range->lba);
10974 		len = scsi_4btoul(range->length);
10975 		if ((lba < lba2 + len2) && (lba + len > lba2))
10976 			return (CTL_ACTION_BLOCK);
10977 	}
10978 	return (CTL_ACTION_PASS);
10979 }
10980 
10981 static ctl_action
10982 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10983 {
10984 	uint64_t lba1, lba2;
10985 	uint64_t len1, len2;
10986 	int retval;
10987 
10988 	if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10989 		return (CTL_ACTION_ERROR);
10990 
10991 	retval = ctl_extent_check_unmap(io1, lba2, len2);
10992 	if (retval != CTL_ACTION_ERROR)
10993 		return (retval);
10994 
10995 	if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10996 		return (CTL_ACTION_ERROR);
10997 
10998 	if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10999 		seq = FALSE;
11000 	return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
11001 }
11002 
11003 static ctl_action
11004 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
11005 {
11006 	uint64_t lba1, lba2;
11007 	uint64_t len1, len2;
11008 
11009 	if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11010 		return (CTL_ACTION_PASS);
11011 	if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11012 		return (CTL_ACTION_ERROR);
11013 	if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11014 		return (CTL_ACTION_ERROR);
11015 
11016 	if (lba1 + len1 == lba2)
11017 		return (CTL_ACTION_BLOCK);
11018 	return (CTL_ACTION_PASS);
11019 }
11020 
11021 static ctl_action
11022 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11023     union ctl_io *ooa_io)
11024 {
11025 	const struct ctl_cmd_entry *pending_entry, *ooa_entry;
11026 	const ctl_serialize_action *serialize_row;
11027 
11028 	/*
11029 	 * The initiator attempted multiple untagged commands at the same
11030 	 * time.  Can't do that.
11031 	 */
11032 	if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11033 	 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11034 	 && ((pending_io->io_hdr.nexus.targ_port ==
11035 	      ooa_io->io_hdr.nexus.targ_port)
11036 	  && (pending_io->io_hdr.nexus.initid ==
11037 	      ooa_io->io_hdr.nexus.initid))
11038 	 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11039 	      CTL_FLAG_STATUS_SENT)) == 0))
11040 		return (CTL_ACTION_OVERLAP);
11041 
11042 	/*
11043 	 * The initiator attempted to send multiple tagged commands with
11044 	 * the same ID.  (It's fine if different initiators have the same
11045 	 * tag ID.)
11046 	 *
11047 	 * Even if all of those conditions are true, we don't kill the I/O
11048 	 * if the command ahead of us has been aborted.  We won't end up
11049 	 * sending it to the FETD, and it's perfectly legal to resend a
11050 	 * command with the same tag number as long as the previous
11051 	 * instance of this tag number has been aborted somehow.
11052 	 */
11053 	if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11054 	 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11055 	 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11056 	 && ((pending_io->io_hdr.nexus.targ_port ==
11057 	      ooa_io->io_hdr.nexus.targ_port)
11058 	  && (pending_io->io_hdr.nexus.initid ==
11059 	      ooa_io->io_hdr.nexus.initid))
11060 	 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11061 	      CTL_FLAG_STATUS_SENT)) == 0))
11062 		return (CTL_ACTION_OVERLAP_TAG);
11063 
11064 	/*
11065 	 * If we get a head of queue tag, SAM-3 says that we should
11066 	 * immediately execute it.
11067 	 *
11068 	 * What happens if this command would normally block for some other
11069 	 * reason?  e.g. a request sense with a head of queue tag
11070 	 * immediately after a write.  Normally that would block, but this
11071 	 * will result in its getting executed immediately...
11072 	 *
11073 	 * We currently return "pass" instead of "skip", so we'll end up
11074 	 * going through the rest of the queue to check for overlapped tags.
11075 	 *
11076 	 * XXX KDM check for other types of blockage first??
11077 	 */
11078 	if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11079 		return (CTL_ACTION_PASS);
11080 
11081 	/*
11082 	 * Ordered tags have to block until all items ahead of them
11083 	 * have completed.  If we get called with an ordered tag, we always
11084 	 * block, if something else is ahead of us in the queue.
11085 	 */
11086 	if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11087 		return (CTL_ACTION_BLOCK);
11088 
11089 	/*
11090 	 * Simple tags get blocked until all head of queue and ordered tags
11091 	 * ahead of them have completed.  I'm lumping untagged commands in
11092 	 * with simple tags here.  XXX KDM is that the right thing to do?
11093 	 */
11094 	if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11095 	  || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11096 	 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11097 	  || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11098 		return (CTL_ACTION_BLOCK);
11099 
11100 	pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11101 	KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11102 	    ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11103 	     __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11104 	     pending_io->scsiio.cdb[1], pending_io));
11105 	ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11106 	if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11107 		return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11108 	KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11109 	    ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11110 	     __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11111 	     ooa_io->scsiio.cdb[1], ooa_io));
11112 
11113 	serialize_row = ctl_serialize_table[ooa_entry->seridx];
11114 
11115 	switch (serialize_row[pending_entry->seridx]) {
11116 	case CTL_SER_BLOCK:
11117 		return (CTL_ACTION_BLOCK);
11118 	case CTL_SER_EXTENT:
11119 		return (ctl_extent_check(ooa_io, pending_io,
11120 		    (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11121 	case CTL_SER_EXTENTOPT:
11122 		if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11123 		    & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11124 			return (ctl_extent_check(ooa_io, pending_io,
11125 			    (lun->be_lun &&
11126 			     lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11127 		return (CTL_ACTION_PASS);
11128 	case CTL_SER_EXTENTSEQ:
11129 		if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11130 			return (ctl_extent_check_seq(ooa_io, pending_io));
11131 		return (CTL_ACTION_PASS);
11132 	case CTL_SER_PASS:
11133 		return (CTL_ACTION_PASS);
11134 	case CTL_SER_BLOCKOPT:
11135 		if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11136 		    & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11137 			return (CTL_ACTION_BLOCK);
11138 		return (CTL_ACTION_PASS);
11139 	case CTL_SER_SKIP:
11140 		return (CTL_ACTION_SKIP);
11141 	default:
11142 		panic("%s: Invalid serialization value %d for %d => %d",
11143 		    __func__, serialize_row[pending_entry->seridx],
11144 		    pending_entry->seridx, ooa_entry->seridx);
11145 	}
11146 
11147 	return (CTL_ACTION_ERROR);
11148 }
11149 
11150 /*
11151  * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11152  * Assumptions:
11153  * - pending_io is generally either incoming, or on the blocked queue
11154  * - starting I/O is the I/O we want to start the check with.
11155  */
11156 static ctl_action
11157 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11158 	      union ctl_io *starting_io)
11159 {
11160 	union ctl_io *ooa_io;
11161 	ctl_action action;
11162 
11163 	mtx_assert(&lun->lun_lock, MA_OWNED);
11164 
11165 	/*
11166 	 * Run back along the OOA queue, starting with the current
11167 	 * blocked I/O and going through every I/O before it on the
11168 	 * queue.  If starting_io is NULL, we'll just end up returning
11169 	 * CTL_ACTION_PASS.
11170 	 */
11171 	for (ooa_io = starting_io; ooa_io != NULL;
11172 	     ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11173 	     ooa_links)){
11174 
11175 		/*
11176 		 * This routine just checks to see whether
11177 		 * cur_blocked is blocked by ooa_io, which is ahead
11178 		 * of it in the queue.  It doesn't queue/dequeue
11179 		 * cur_blocked.
11180 		 */
11181 		action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11182 		switch (action) {
11183 		case CTL_ACTION_BLOCK:
11184 		case CTL_ACTION_OVERLAP:
11185 		case CTL_ACTION_OVERLAP_TAG:
11186 		case CTL_ACTION_SKIP:
11187 		case CTL_ACTION_ERROR:
11188 			return (action);
11189 			break; /* NOTREACHED */
11190 		case CTL_ACTION_PASS:
11191 			break;
11192 		default:
11193 			panic("%s: Invalid action %d\n", __func__, action);
11194 		}
11195 	}
11196 
11197 	return (CTL_ACTION_PASS);
11198 }
11199 
11200 /*
11201  * Assumptions:
11202  * - An I/O has just completed, and has been removed from the per-LUN OOA
11203  *   queue, so some items on the blocked queue may now be unblocked.
11204  */
11205 static int
11206 ctl_check_blocked(struct ctl_lun *lun)
11207 {
11208 	struct ctl_softc *softc = lun->ctl_softc;
11209 	union ctl_io *cur_blocked, *next_blocked;
11210 
11211 	mtx_assert(&lun->lun_lock, MA_OWNED);
11212 
11213 	/*
11214 	 * Run forward from the head of the blocked queue, checking each
11215 	 * entry against the I/Os prior to it on the OOA queue to see if
11216 	 * there is still any blockage.
11217 	 *
11218 	 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11219 	 * with our removing a variable on it while it is traversing the
11220 	 * list.
11221 	 */
11222 	for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11223 	     cur_blocked != NULL; cur_blocked = next_blocked) {
11224 		union ctl_io *prev_ooa;
11225 		ctl_action action;
11226 
11227 		next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11228 							  blocked_links);
11229 
11230 		prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11231 						      ctl_ooaq, ooa_links);
11232 
11233 		/*
11234 		 * If cur_blocked happens to be the first item in the OOA
11235 		 * queue now, prev_ooa will be NULL, and the action
11236 		 * returned will just be CTL_ACTION_PASS.
11237 		 */
11238 		action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11239 
11240 		switch (action) {
11241 		case CTL_ACTION_BLOCK:
11242 			/* Nothing to do here, still blocked */
11243 			break;
11244 		case CTL_ACTION_OVERLAP:
11245 		case CTL_ACTION_OVERLAP_TAG:
11246 			/*
11247 			 * This shouldn't happen!  In theory we've already
11248 			 * checked this command for overlap...
11249 			 */
11250 			break;
11251 		case CTL_ACTION_PASS:
11252 		case CTL_ACTION_SKIP: {
11253 			const struct ctl_cmd_entry *entry;
11254 
11255 			/*
11256 			 * The skip case shouldn't happen, this transaction
11257 			 * should have never made it onto the blocked queue.
11258 			 */
11259 			/*
11260 			 * This I/O is no longer blocked, we can remove it
11261 			 * from the blocked queue.  Since this is a TAILQ
11262 			 * (doubly linked list), we can do O(1) removals
11263 			 * from any place on the list.
11264 			 */
11265 			TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11266 				     blocked_links);
11267 			cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11268 
11269 			if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11270 			    (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11271 				/*
11272 				 * Need to send IO back to original side to
11273 				 * run
11274 				 */
11275 				union ctl_ha_msg msg_info;
11276 
11277 				cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11278 				msg_info.hdr.original_sc =
11279 					cur_blocked->io_hdr.original_sc;
11280 				msg_info.hdr.serializing_sc = cur_blocked;
11281 				msg_info.hdr.msg_type = CTL_MSG_R2R;
11282 				ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11283 				    sizeof(msg_info.hdr), M_NOWAIT);
11284 				break;
11285 			}
11286 			entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11287 
11288 			/*
11289 			 * Check this I/O for LUN state changes that may
11290 			 * have happened while this command was blocked.
11291 			 * The LUN state may have been changed by a command
11292 			 * ahead of us in the queue, so we need to re-check
11293 			 * for any states that can be caused by SCSI
11294 			 * commands.
11295 			 */
11296 			if (ctl_scsiio_lun_check(lun, entry,
11297 						 &cur_blocked->scsiio) == 0) {
11298 				cur_blocked->io_hdr.flags |=
11299 				                      CTL_FLAG_IS_WAS_ON_RTR;
11300 				ctl_enqueue_rtr(cur_blocked);
11301 			} else
11302 				ctl_done(cur_blocked);
11303 			break;
11304 		}
11305 		default:
11306 			/*
11307 			 * This probably shouldn't happen -- we shouldn't
11308 			 * get CTL_ACTION_ERROR, or anything else.
11309 			 */
11310 			break;
11311 		}
11312 	}
11313 
11314 	return (CTL_RETVAL_COMPLETE);
11315 }
11316 
11317 /*
11318  * This routine (with one exception) checks LUN flags that can be set by
11319  * commands ahead of us in the OOA queue.  These flags have to be checked
11320  * when a command initially comes in, and when we pull a command off the
11321  * blocked queue and are preparing to execute it.  The reason we have to
11322  * check these flags for commands on the blocked queue is that the LUN
11323  * state may have been changed by a command ahead of us while we're on the
11324  * blocked queue.
11325  *
11326  * Ordering is somewhat important with these checks, so please pay
11327  * careful attention to the placement of any new checks.
11328  */
11329 static int
11330 ctl_scsiio_lun_check(struct ctl_lun *lun,
11331     const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11332 {
11333 	struct ctl_softc *softc = lun->ctl_softc;
11334 	int retval;
11335 	uint32_t residx;
11336 
11337 	retval = 0;
11338 
11339 	mtx_assert(&lun->lun_lock, MA_OWNED);
11340 
11341 	/*
11342 	 * If this shelf is a secondary shelf controller, we may have to
11343 	 * reject some commands disallowed by HA mode and link state.
11344 	 */
11345 	if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11346 		if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11347 		    (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11348 			ctl_set_lun_unavail(ctsio);
11349 			retval = 1;
11350 			goto bailout;
11351 		}
11352 		if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11353 		    (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11354 			ctl_set_lun_transit(ctsio);
11355 			retval = 1;
11356 			goto bailout;
11357 		}
11358 		if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11359 		    (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11360 			ctl_set_lun_standby(ctsio);
11361 			retval = 1;
11362 			goto bailout;
11363 		}
11364 
11365 		/* The rest of checks are only done on executing side */
11366 		if (softc->ha_mode == CTL_HA_MODE_XFER)
11367 			goto bailout;
11368 	}
11369 
11370 	if (entry->pattern & CTL_LUN_PAT_WRITE) {
11371 		if (lun->be_lun &&
11372 		    lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11373 			ctl_set_hw_write_protected(ctsio);
11374 			retval = 1;
11375 			goto bailout;
11376 		}
11377 		if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11378 		    .eca_and_aen & SCP_SWP) != 0) {
11379 			ctl_set_sense(ctsio, /*current_error*/ 1,
11380 			    /*sense_key*/ SSD_KEY_DATA_PROTECT,
11381 			    /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11382 			retval = 1;
11383 			goto bailout;
11384 		}
11385 	}
11386 
11387 	/*
11388 	 * Check for a reservation conflict.  If this command isn't allowed
11389 	 * even on reserved LUNs, and if this initiator isn't the one who
11390 	 * reserved us, reject the command with a reservation conflict.
11391 	 */
11392 	residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11393 	if ((lun->flags & CTL_LUN_RESERVED)
11394 	 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11395 		if (lun->res_idx != residx) {
11396 			ctl_set_reservation_conflict(ctsio);
11397 			retval = 1;
11398 			goto bailout;
11399 		}
11400 	}
11401 
11402 	if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11403 	    (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11404 		/* No reservation or command is allowed. */;
11405 	} else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11406 	    (lun->pr_res_type == SPR_TYPE_WR_EX ||
11407 	     lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11408 	     lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11409 		/* The command is allowed for Write Exclusive resv. */;
11410 	} else {
11411 		/*
11412 		 * if we aren't registered or it's a res holder type
11413 		 * reservation and this isn't the res holder then set a
11414 		 * conflict.
11415 		 */
11416 		if (ctl_get_prkey(lun, residx) == 0 ||
11417 		    (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11418 			ctl_set_reservation_conflict(ctsio);
11419 			retval = 1;
11420 			goto bailout;
11421 		}
11422 	}
11423 
11424 	if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11425 		if (lun->flags & CTL_LUN_EJECTED)
11426 			ctl_set_lun_ejected(ctsio);
11427 		else if (lun->flags & CTL_LUN_NO_MEDIA) {
11428 			if (lun->flags & CTL_LUN_REMOVABLE)
11429 				ctl_set_lun_no_media(ctsio);
11430 			else
11431 				ctl_set_lun_int_reqd(ctsio);
11432 		} else if (lun->flags & CTL_LUN_STOPPED)
11433 			ctl_set_lun_stopped(ctsio);
11434 		else
11435 			goto bailout;
11436 		retval = 1;
11437 		goto bailout;
11438 	}
11439 
11440 bailout:
11441 	return (retval);
11442 }
11443 
11444 static void
11445 ctl_failover_io(union ctl_io *io, int have_lock)
11446 {
11447 	ctl_set_busy(&io->scsiio);
11448 	ctl_done(io);
11449 }
11450 
11451 static void
11452 ctl_failover_lun(union ctl_io *rio)
11453 {
11454 	struct ctl_softc *softc = control_softc;
11455 	struct ctl_lun *lun;
11456 	struct ctl_io_hdr *io, *next_io;
11457 	uint32_t targ_lun;
11458 
11459 	targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11460 	CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11461 
11462 	/* Find and lock the LUN. */
11463 	mtx_lock(&softc->ctl_lock);
11464 	if ((targ_lun < CTL_MAX_LUNS) &&
11465 	    ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11466 		mtx_lock(&lun->lun_lock);
11467 		mtx_unlock(&softc->ctl_lock);
11468 		if (lun->flags & CTL_LUN_DISABLED) {
11469 			mtx_unlock(&lun->lun_lock);
11470 			return;
11471 		}
11472 	} else {
11473 		mtx_unlock(&softc->ctl_lock);
11474 		return;
11475 	}
11476 
11477 	if (softc->ha_mode == CTL_HA_MODE_XFER) {
11478 		TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11479 			/* We are master */
11480 			if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11481 				if (io->flags & CTL_FLAG_IO_ACTIVE) {
11482 					io->flags |= CTL_FLAG_ABORT;
11483 					io->flags |= CTL_FLAG_FAILOVER;
11484 				} else { /* This can be only due to DATAMOVE */
11485 					io->msg_type = CTL_MSG_DATAMOVE_DONE;
11486 					io->flags &= ~CTL_FLAG_DMA_INPROG;
11487 					io->flags |= CTL_FLAG_IO_ACTIVE;
11488 					io->port_status = 31340;
11489 					ctl_enqueue_isc((union ctl_io *)io);
11490 				}
11491 			}
11492 			/* We are slave */
11493 			if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11494 				io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11495 				if (io->flags & CTL_FLAG_IO_ACTIVE) {
11496 					io->flags |= CTL_FLAG_FAILOVER;
11497 				} else {
11498 					ctl_set_busy(&((union ctl_io *)io)->
11499 					    scsiio);
11500 					ctl_done((union ctl_io *)io);
11501 				}
11502 			}
11503 		}
11504 	} else { /* SERIALIZE modes */
11505 		TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11506 		    next_io) {
11507 			/* We are master */
11508 			if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11509 				TAILQ_REMOVE(&lun->blocked_queue, io,
11510 				    blocked_links);
11511 				io->flags &= ~CTL_FLAG_BLOCKED;
11512 				TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11513 				ctl_free_io((union ctl_io *)io);
11514 			}
11515 		}
11516 		TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11517 			/* We are master */
11518 			if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11519 				TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11520 				ctl_free_io((union ctl_io *)io);
11521 			}
11522 			/* We are slave */
11523 			if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11524 				io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11525 				if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11526 					ctl_set_busy(&((union ctl_io *)io)->
11527 					    scsiio);
11528 					ctl_done((union ctl_io *)io);
11529 				}
11530 			}
11531 		}
11532 		ctl_check_blocked(lun);
11533 	}
11534 	mtx_unlock(&lun->lun_lock);
11535 }
11536 
11537 static int
11538 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11539 {
11540 	struct ctl_lun *lun;
11541 	const struct ctl_cmd_entry *entry;
11542 	uint32_t initidx, targ_lun;
11543 	int retval;
11544 
11545 	retval = 0;
11546 
11547 	lun = NULL;
11548 
11549 	targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11550 	if ((targ_lun < CTL_MAX_LUNS)
11551 	 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11552 		/*
11553 		 * If the LUN is invalid, pretend that it doesn't exist.
11554 		 * It will go away as soon as all pending I/O has been
11555 		 * completed.
11556 		 */
11557 		mtx_lock(&lun->lun_lock);
11558 		if (lun->flags & CTL_LUN_DISABLED) {
11559 			mtx_unlock(&lun->lun_lock);
11560 			lun = NULL;
11561 			ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11562 			ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11563 		} else {
11564 			ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11565 			ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11566 				lun->be_lun;
11567 
11568 			/*
11569 			 * Every I/O goes into the OOA queue for a
11570 			 * particular LUN, and stays there until completion.
11571 			 */
11572 #ifdef CTL_TIME_IO
11573 			if (TAILQ_EMPTY(&lun->ooa_queue)) {
11574 				lun->idle_time += getsbinuptime() -
11575 				    lun->last_busy;
11576 			}
11577 #endif
11578 			TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11579 			    ooa_links);
11580 		}
11581 	} else {
11582 		ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11583 		ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11584 	}
11585 
11586 	/* Get command entry and return error if it is unsuppotyed. */
11587 	entry = ctl_validate_command(ctsio);
11588 	if (entry == NULL) {
11589 		if (lun)
11590 			mtx_unlock(&lun->lun_lock);
11591 		return (retval);
11592 	}
11593 
11594 	ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11595 	ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11596 
11597 	/*
11598 	 * Check to see whether we can send this command to LUNs that don't
11599 	 * exist.  This should pretty much only be the case for inquiry
11600 	 * and request sense.  Further checks, below, really require having
11601 	 * a LUN, so we can't really check the command anymore.  Just put
11602 	 * it on the rtr queue.
11603 	 */
11604 	if (lun == NULL) {
11605 		if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11606 			ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11607 			ctl_enqueue_rtr((union ctl_io *)ctsio);
11608 			return (retval);
11609 		}
11610 
11611 		ctl_set_unsupported_lun(ctsio);
11612 		ctl_done((union ctl_io *)ctsio);
11613 		CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11614 		return (retval);
11615 	} else {
11616 		/*
11617 		 * Make sure we support this particular command on this LUN.
11618 		 * e.g., we don't support writes to the control LUN.
11619 		 */
11620 		if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11621 			mtx_unlock(&lun->lun_lock);
11622 			ctl_set_invalid_opcode(ctsio);
11623 			ctl_done((union ctl_io *)ctsio);
11624 			return (retval);
11625 		}
11626 	}
11627 
11628 	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11629 
11630 #ifdef CTL_WITH_CA
11631 	/*
11632 	 * If we've got a request sense, it'll clear the contingent
11633 	 * allegiance condition.  Otherwise, if we have a CA condition for
11634 	 * this initiator, clear it, because it sent down a command other
11635 	 * than request sense.
11636 	 */
11637 	if ((ctsio->cdb[0] != REQUEST_SENSE)
11638 	 && (ctl_is_set(lun->have_ca, initidx)))
11639 		ctl_clear_mask(lun->have_ca, initidx);
11640 #endif
11641 
11642 	/*
11643 	 * If the command has this flag set, it handles its own unit
11644 	 * attention reporting, we shouldn't do anything.  Otherwise we
11645 	 * check for any pending unit attentions, and send them back to the
11646 	 * initiator.  We only do this when a command initially comes in,
11647 	 * not when we pull it off the blocked queue.
11648 	 *
11649 	 * According to SAM-3, section 5.3.2, the order that things get
11650 	 * presented back to the host is basically unit attentions caused
11651 	 * by some sort of reset event, busy status, reservation conflicts
11652 	 * or task set full, and finally any other status.
11653 	 *
11654 	 * One issue here is that some of the unit attentions we report
11655 	 * don't fall into the "reset" category (e.g. "reported luns data
11656 	 * has changed").  So reporting it here, before the reservation
11657 	 * check, may be technically wrong.  I guess the only thing to do
11658 	 * would be to check for and report the reset events here, and then
11659 	 * check for the other unit attention types after we check for a
11660 	 * reservation conflict.
11661 	 *
11662 	 * XXX KDM need to fix this
11663 	 */
11664 	if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11665 		ctl_ua_type ua_type;
11666 
11667 		ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11668 		    SSD_TYPE_NONE);
11669 		if (ua_type != CTL_UA_NONE) {
11670 			mtx_unlock(&lun->lun_lock);
11671 			ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11672 			ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11673 			ctsio->sense_len = SSD_FULL_SIZE;
11674 			ctl_done((union ctl_io *)ctsio);
11675 			return (retval);
11676 		}
11677 	}
11678 
11679 
11680 	if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11681 		mtx_unlock(&lun->lun_lock);
11682 		ctl_done((union ctl_io *)ctsio);
11683 		return (retval);
11684 	}
11685 
11686 	/*
11687 	 * XXX CHD this is where we want to send IO to other side if
11688 	 * this LUN is secondary on this SC. We will need to make a copy
11689 	 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11690 	 * the copy we send as FROM_OTHER.
11691 	 * We also need to stuff the address of the original IO so we can
11692 	 * find it easily. Something similar will need be done on the other
11693 	 * side so when we are done we can find the copy.
11694 	 */
11695 	if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11696 	    (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11697 	    (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11698 		union ctl_ha_msg msg_info;
11699 		int isc_retval;
11700 
11701 		ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11702 		ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11703 		mtx_unlock(&lun->lun_lock);
11704 
11705 		msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11706 		msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11707 		msg_info.hdr.serializing_sc = NULL;
11708 		msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11709 		msg_info.scsi.tag_num = ctsio->tag_num;
11710 		msg_info.scsi.tag_type = ctsio->tag_type;
11711 		msg_info.scsi.cdb_len = ctsio->cdb_len;
11712 		memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11713 
11714 		if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11715 		    sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11716 		    M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11717 			ctl_set_busy(ctsio);
11718 			ctl_done((union ctl_io *)ctsio);
11719 			return (retval);
11720 		}
11721 		return (retval);
11722 	}
11723 
11724 	switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11725 			      (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11726 			      ctl_ooaq, ooa_links))) {
11727 	case CTL_ACTION_BLOCK:
11728 		ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11729 		TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11730 				  blocked_links);
11731 		mtx_unlock(&lun->lun_lock);
11732 		return (retval);
11733 	case CTL_ACTION_PASS:
11734 	case CTL_ACTION_SKIP:
11735 		ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11736 		mtx_unlock(&lun->lun_lock);
11737 		ctl_enqueue_rtr((union ctl_io *)ctsio);
11738 		break;
11739 	case CTL_ACTION_OVERLAP:
11740 		mtx_unlock(&lun->lun_lock);
11741 		ctl_set_overlapped_cmd(ctsio);
11742 		ctl_done((union ctl_io *)ctsio);
11743 		break;
11744 	case CTL_ACTION_OVERLAP_TAG:
11745 		mtx_unlock(&lun->lun_lock);
11746 		ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11747 		ctl_done((union ctl_io *)ctsio);
11748 		break;
11749 	case CTL_ACTION_ERROR:
11750 	default:
11751 		mtx_unlock(&lun->lun_lock);
11752 		ctl_set_internal_failure(ctsio,
11753 					 /*sks_valid*/ 0,
11754 					 /*retry_count*/ 0);
11755 		ctl_done((union ctl_io *)ctsio);
11756 		break;
11757 	}
11758 	return (retval);
11759 }
11760 
11761 const struct ctl_cmd_entry *
11762 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11763 {
11764 	const struct ctl_cmd_entry *entry;
11765 	int service_action;
11766 
11767 	entry = &ctl_cmd_table[ctsio->cdb[0]];
11768 	if (sa)
11769 		*sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11770 	if (entry->flags & CTL_CMD_FLAG_SA5) {
11771 		service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11772 		entry = &((const struct ctl_cmd_entry *)
11773 		    entry->execute)[service_action];
11774 	}
11775 	return (entry);
11776 }
11777 
11778 const struct ctl_cmd_entry *
11779 ctl_validate_command(struct ctl_scsiio *ctsio)
11780 {
11781 	const struct ctl_cmd_entry *entry;
11782 	int i, sa;
11783 	uint8_t diff;
11784 
11785 	entry = ctl_get_cmd_entry(ctsio, &sa);
11786 	if (entry->execute == NULL) {
11787 		if (sa)
11788 			ctl_set_invalid_field(ctsio,
11789 					      /*sks_valid*/ 1,
11790 					      /*command*/ 1,
11791 					      /*field*/ 1,
11792 					      /*bit_valid*/ 1,
11793 					      /*bit*/ 4);
11794 		else
11795 			ctl_set_invalid_opcode(ctsio);
11796 		ctl_done((union ctl_io *)ctsio);
11797 		return (NULL);
11798 	}
11799 	KASSERT(entry->length > 0,
11800 	    ("Not defined length for command 0x%02x/0x%02x",
11801 	     ctsio->cdb[0], ctsio->cdb[1]));
11802 	for (i = 1; i < entry->length; i++) {
11803 		diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11804 		if (diff == 0)
11805 			continue;
11806 		ctl_set_invalid_field(ctsio,
11807 				      /*sks_valid*/ 1,
11808 				      /*command*/ 1,
11809 				      /*field*/ i,
11810 				      /*bit_valid*/ 1,
11811 				      /*bit*/ fls(diff) - 1);
11812 		ctl_done((union ctl_io *)ctsio);
11813 		return (NULL);
11814 	}
11815 	return (entry);
11816 }
11817 
11818 static int
11819 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11820 {
11821 
11822 	switch (lun_type) {
11823 	case T_DIRECT:
11824 		if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11825 			return (0);
11826 		break;
11827 	case T_PROCESSOR:
11828 		if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11829 			return (0);
11830 		break;
11831 	case T_CDROM:
11832 		if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11833 			return (0);
11834 		break;
11835 	default:
11836 		return (0);
11837 	}
11838 	return (1);
11839 }
11840 
11841 static int
11842 ctl_scsiio(struct ctl_scsiio *ctsio)
11843 {
11844 	int retval;
11845 	const struct ctl_cmd_entry *entry;
11846 
11847 	retval = CTL_RETVAL_COMPLETE;
11848 
11849 	CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11850 
11851 	entry = ctl_get_cmd_entry(ctsio, NULL);
11852 
11853 	/*
11854 	 * If this I/O has been aborted, just send it straight to
11855 	 * ctl_done() without executing it.
11856 	 */
11857 	if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11858 		ctl_done((union ctl_io *)ctsio);
11859 		goto bailout;
11860 	}
11861 
11862 	/*
11863 	 * All the checks should have been handled by ctl_scsiio_precheck().
11864 	 * We should be clear now to just execute the I/O.
11865 	 */
11866 	retval = entry->execute(ctsio);
11867 
11868 bailout:
11869 	return (retval);
11870 }
11871 
11872 /*
11873  * Since we only implement one target right now, a bus reset simply resets
11874  * our single target.
11875  */
11876 static int
11877 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11878 {
11879 	return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11880 }
11881 
11882 static int
11883 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11884 		 ctl_ua_type ua_type)
11885 {
11886 	struct ctl_port *port;
11887 	struct ctl_lun *lun;
11888 	int retval;
11889 
11890 	if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11891 		union ctl_ha_msg msg_info;
11892 
11893 		msg_info.hdr.nexus = io->io_hdr.nexus;
11894 		if (ua_type==CTL_UA_TARG_RESET)
11895 			msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11896 		else
11897 			msg_info.task.task_action = CTL_TASK_BUS_RESET;
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, &msg_info,
11902 		    sizeof(msg_info.task), M_WAITOK);
11903 	}
11904 	retval = 0;
11905 
11906 	mtx_lock(&softc->ctl_lock);
11907 	port = ctl_io_port(&io->io_hdr);
11908 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
11909 		if (port != NULL &&
11910 		    ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11911 			continue;
11912 		retval += ctl_do_lun_reset(lun, io, ua_type);
11913 	}
11914 	mtx_unlock(&softc->ctl_lock);
11915 	io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11916 	return (retval);
11917 }
11918 
11919 /*
11920  * The LUN should always be set.  The I/O is optional, and is used to
11921  * distinguish between I/Os sent by this initiator, and by other
11922  * initiators.  We set unit attention for initiators other than this one.
11923  * SAM-3 is vague on this point.  It does say that a unit attention should
11924  * be established for other initiators when a LUN is reset (see section
11925  * 5.7.3), but it doesn't specifically say that the unit attention should
11926  * be established for this particular initiator when a LUN is reset.  Here
11927  * is the relevant text, from SAM-3 rev 8:
11928  *
11929  * 5.7.2 When a SCSI initiator port aborts its own tasks
11930  *
11931  * When a SCSI initiator port causes its own task(s) to be aborted, no
11932  * notification that the task(s) have been aborted shall be returned to
11933  * the SCSI initiator port other than the completion response for the
11934  * command or task management function action that caused the task(s) to
11935  * be aborted and notification(s) associated with related effects of the
11936  * action (e.g., a reset unit attention condition).
11937  *
11938  * XXX KDM for now, we're setting unit attention for all initiators.
11939  */
11940 static int
11941 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11942 {
11943 	union ctl_io *xio;
11944 #if 0
11945 	uint32_t initidx;
11946 #endif
11947 	int i;
11948 
11949 	mtx_lock(&lun->lun_lock);
11950 	/*
11951 	 * Run through the OOA queue and abort each I/O.
11952 	 */
11953 	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11954 	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11955 		xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11956 	}
11957 
11958 	/*
11959 	 * This version sets unit attention for every
11960 	 */
11961 #if 0
11962 	initidx = ctl_get_initindex(&io->io_hdr.nexus);
11963 	ctl_est_ua_all(lun, initidx, ua_type);
11964 #else
11965 	ctl_est_ua_all(lun, -1, ua_type);
11966 #endif
11967 
11968 	/*
11969 	 * A reset (any kind, really) clears reservations established with
11970 	 * RESERVE/RELEASE.  It does not clear reservations established
11971 	 * with PERSISTENT RESERVE OUT, but we don't support that at the
11972 	 * moment anyway.  See SPC-2, section 5.6.  SPC-3 doesn't address
11973 	 * reservations made with the RESERVE/RELEASE commands, because
11974 	 * those commands are obsolete in SPC-3.
11975 	 */
11976 	lun->flags &= ~CTL_LUN_RESERVED;
11977 
11978 #ifdef CTL_WITH_CA
11979 	for (i = 0; i < CTL_MAX_INITIATORS; i++)
11980 		ctl_clear_mask(lun->have_ca, i);
11981 #endif
11982 	lun->prevent_count = 0;
11983 	for (i = 0; i < CTL_MAX_INITIATORS; i++)
11984 		ctl_clear_mask(lun->prevent, i);
11985 	mtx_unlock(&lun->lun_lock);
11986 
11987 	return (0);
11988 }
11989 
11990 static int
11991 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11992 {
11993 	struct ctl_lun *lun;
11994 	uint32_t targ_lun;
11995 	int retval;
11996 
11997 	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11998 	mtx_lock(&softc->ctl_lock);
11999 	if ((targ_lun >= CTL_MAX_LUNS) ||
12000 	    (lun = softc->ctl_luns[targ_lun]) == NULL) {
12001 		mtx_unlock(&softc->ctl_lock);
12002 		io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12003 		return (1);
12004 	}
12005 	retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
12006 	mtx_unlock(&softc->ctl_lock);
12007 	io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12008 
12009 	if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
12010 		union ctl_ha_msg msg_info;
12011 
12012 		msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12013 		msg_info.hdr.nexus = io->io_hdr.nexus;
12014 		msg_info.task.task_action = CTL_TASK_LUN_RESET;
12015 		msg_info.hdr.original_sc = NULL;
12016 		msg_info.hdr.serializing_sc = NULL;
12017 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12018 		    sizeof(msg_info.task), M_WAITOK);
12019 	}
12020 	return (retval);
12021 }
12022 
12023 static void
12024 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12025     int other_sc)
12026 {
12027 	union ctl_io *xio;
12028 
12029 	mtx_assert(&lun->lun_lock, MA_OWNED);
12030 
12031 	/*
12032 	 * Run through the OOA queue and attempt to find the given I/O.
12033 	 * The target port, initiator ID, tag type and tag number have to
12034 	 * match the values that we got from the initiator.  If we have an
12035 	 * untagged command to abort, simply abort the first untagged command
12036 	 * we come to.  We only allow one untagged command at a time of course.
12037 	 */
12038 	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12039 	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12040 
12041 		if ((targ_port == UINT32_MAX ||
12042 		     targ_port == xio->io_hdr.nexus.targ_port) &&
12043 		    (init_id == UINT32_MAX ||
12044 		     init_id == xio->io_hdr.nexus.initid)) {
12045 			if (targ_port != xio->io_hdr.nexus.targ_port ||
12046 			    init_id != xio->io_hdr.nexus.initid)
12047 				xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12048 			xio->io_hdr.flags |= CTL_FLAG_ABORT;
12049 			if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12050 				union ctl_ha_msg msg_info;
12051 
12052 				msg_info.hdr.nexus = xio->io_hdr.nexus;
12053 				msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12054 				msg_info.task.tag_num = xio->scsiio.tag_num;
12055 				msg_info.task.tag_type = xio->scsiio.tag_type;
12056 				msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12057 				msg_info.hdr.original_sc = NULL;
12058 				msg_info.hdr.serializing_sc = NULL;
12059 				ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12060 				    sizeof(msg_info.task), M_NOWAIT);
12061 			}
12062 		}
12063 	}
12064 }
12065 
12066 static int
12067 ctl_abort_task_set(union ctl_io *io)
12068 {
12069 	struct ctl_softc *softc = control_softc;
12070 	struct ctl_lun *lun;
12071 	uint32_t targ_lun;
12072 
12073 	/*
12074 	 * Look up the LUN.
12075 	 */
12076 	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12077 	mtx_lock(&softc->ctl_lock);
12078 	if ((targ_lun >= CTL_MAX_LUNS) ||
12079 	    (lun = softc->ctl_luns[targ_lun]) == NULL) {
12080 		mtx_unlock(&softc->ctl_lock);
12081 		io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12082 		return (1);
12083 	}
12084 
12085 	mtx_lock(&lun->lun_lock);
12086 	mtx_unlock(&softc->ctl_lock);
12087 	if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12088 		ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12089 		    io->io_hdr.nexus.initid,
12090 		    (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12091 	} else { /* CTL_TASK_CLEAR_TASK_SET */
12092 		ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12093 		    (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12094 	}
12095 	mtx_unlock(&lun->lun_lock);
12096 	io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12097 	return (0);
12098 }
12099 
12100 static int
12101 ctl_i_t_nexus_reset(union ctl_io *io)
12102 {
12103 	struct ctl_softc *softc = control_softc;
12104 	struct ctl_lun *lun;
12105 	uint32_t initidx;
12106 
12107 	if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12108 		union ctl_ha_msg msg_info;
12109 
12110 		msg_info.hdr.nexus = io->io_hdr.nexus;
12111 		msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12112 		msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12113 		msg_info.hdr.original_sc = NULL;
12114 		msg_info.hdr.serializing_sc = NULL;
12115 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12116 		    sizeof(msg_info.task), M_WAITOK);
12117 	}
12118 
12119 	initidx = ctl_get_initindex(&io->io_hdr.nexus);
12120 	mtx_lock(&softc->ctl_lock);
12121 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
12122 		mtx_lock(&lun->lun_lock);
12123 		ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12124 		    io->io_hdr.nexus.initid, 1);
12125 #ifdef CTL_WITH_CA
12126 		ctl_clear_mask(lun->have_ca, initidx);
12127 #endif
12128 		if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12129 			lun->flags &= ~CTL_LUN_RESERVED;
12130 		if (ctl_is_set(lun->prevent, initidx)) {
12131 			ctl_clear_mask(lun->prevent, initidx);
12132 			lun->prevent_count--;
12133 		}
12134 		ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12135 		mtx_unlock(&lun->lun_lock);
12136 	}
12137 	mtx_unlock(&softc->ctl_lock);
12138 	io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12139 	return (0);
12140 }
12141 
12142 static int
12143 ctl_abort_task(union ctl_io *io)
12144 {
12145 	union ctl_io *xio;
12146 	struct ctl_lun *lun;
12147 	struct ctl_softc *softc;
12148 #if 0
12149 	struct sbuf sb;
12150 	char printbuf[128];
12151 #endif
12152 	int found;
12153 	uint32_t targ_lun;
12154 
12155 	softc = control_softc;
12156 	found = 0;
12157 
12158 	/*
12159 	 * Look up the LUN.
12160 	 */
12161 	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12162 	mtx_lock(&softc->ctl_lock);
12163 	if ((targ_lun >= CTL_MAX_LUNS) ||
12164 	    (lun = softc->ctl_luns[targ_lun]) == NULL) {
12165 		mtx_unlock(&softc->ctl_lock);
12166 		io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12167 		return (1);
12168 	}
12169 
12170 #if 0
12171 	printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12172 	       lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12173 #endif
12174 
12175 	mtx_lock(&lun->lun_lock);
12176 	mtx_unlock(&softc->ctl_lock);
12177 	/*
12178 	 * Run through the OOA queue and attempt to find the given I/O.
12179 	 * The target port, initiator ID, tag type and tag number have to
12180 	 * match the values that we got from the initiator.  If we have an
12181 	 * untagged command to abort, simply abort the first untagged command
12182 	 * we come to.  We only allow one untagged command at a time of course.
12183 	 */
12184 	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12185 	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12186 #if 0
12187 		sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12188 
12189 		sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12190 			    lun->lun, xio->scsiio.tag_num,
12191 			    xio->scsiio.tag_type,
12192 			    (xio->io_hdr.blocked_links.tqe_prev
12193 			    == NULL) ? "" : " BLOCKED",
12194 			    (xio->io_hdr.flags &
12195 			    CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12196 			    (xio->io_hdr.flags &
12197 			    CTL_FLAG_ABORT) ? " ABORT" : "",
12198 			    (xio->io_hdr.flags &
12199 			    CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12200 		ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12201 		sbuf_finish(&sb);
12202 		printf("%s\n", sbuf_data(&sb));
12203 #endif
12204 
12205 		if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12206 		 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12207 		 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12208 			continue;
12209 
12210 		/*
12211 		 * If the abort says that the task is untagged, the
12212 		 * task in the queue must be untagged.  Otherwise,
12213 		 * we just check to see whether the tag numbers
12214 		 * match.  This is because the QLogic firmware
12215 		 * doesn't pass back the tag type in an abort
12216 		 * request.
12217 		 */
12218 #if 0
12219 		if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12220 		  && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12221 		 || (xio->scsiio.tag_num == io->taskio.tag_num))
12222 #endif
12223 		/*
12224 		 * XXX KDM we've got problems with FC, because it
12225 		 * doesn't send down a tag type with aborts.  So we
12226 		 * can only really go by the tag number...
12227 		 * This may cause problems with parallel SCSI.
12228 		 * Need to figure that out!!
12229 		 */
12230 		if (xio->scsiio.tag_num == io->taskio.tag_num) {
12231 			xio->io_hdr.flags |= CTL_FLAG_ABORT;
12232 			found = 1;
12233 			if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12234 			    !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12235 				union ctl_ha_msg msg_info;
12236 
12237 				msg_info.hdr.nexus = io->io_hdr.nexus;
12238 				msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12239 				msg_info.task.tag_num = io->taskio.tag_num;
12240 				msg_info.task.tag_type = io->taskio.tag_type;
12241 				msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12242 				msg_info.hdr.original_sc = NULL;
12243 				msg_info.hdr.serializing_sc = NULL;
12244 #if 0
12245 				printf("Sent Abort to other side\n");
12246 #endif
12247 				ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12248 				    sizeof(msg_info.task), M_NOWAIT);
12249 			}
12250 #if 0
12251 			printf("ctl_abort_task: found I/O to abort\n");
12252 #endif
12253 		}
12254 	}
12255 	mtx_unlock(&lun->lun_lock);
12256 
12257 	if (found == 0) {
12258 		/*
12259 		 * This isn't really an error.  It's entirely possible for
12260 		 * the abort and command completion to cross on the wire.
12261 		 * This is more of an informative/diagnostic error.
12262 		 */
12263 #if 0
12264 		printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12265 		       "%u:%u:%u tag %d type %d\n",
12266 		       io->io_hdr.nexus.initid,
12267 		       io->io_hdr.nexus.targ_port,
12268 		       io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12269 		       io->taskio.tag_type);
12270 #endif
12271 	}
12272 	io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12273 	return (0);
12274 }
12275 
12276 static int
12277 ctl_query_task(union ctl_io *io, int task_set)
12278 {
12279 	union ctl_io *xio;
12280 	struct ctl_lun *lun;
12281 	struct ctl_softc *softc;
12282 	int found = 0;
12283 	uint32_t targ_lun;
12284 
12285 	softc = control_softc;
12286 	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12287 	mtx_lock(&softc->ctl_lock);
12288 	if ((targ_lun >= CTL_MAX_LUNS) ||
12289 	    (lun = softc->ctl_luns[targ_lun]) == NULL) {
12290 		mtx_unlock(&softc->ctl_lock);
12291 		io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12292 		return (1);
12293 	}
12294 	mtx_lock(&lun->lun_lock);
12295 	mtx_unlock(&softc->ctl_lock);
12296 	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12297 	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12298 
12299 		if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12300 		 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12301 		 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12302 			continue;
12303 
12304 		if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12305 			found = 1;
12306 			break;
12307 		}
12308 	}
12309 	mtx_unlock(&lun->lun_lock);
12310 	if (found)
12311 		io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12312 	else
12313 		io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12314 	return (0);
12315 }
12316 
12317 static int
12318 ctl_query_async_event(union ctl_io *io)
12319 {
12320 	struct ctl_lun *lun;
12321 	struct ctl_softc *softc;
12322 	ctl_ua_type ua;
12323 	uint32_t targ_lun, initidx;
12324 
12325 	softc = control_softc;
12326 	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12327 	mtx_lock(&softc->ctl_lock);
12328 	if ((targ_lun >= CTL_MAX_LUNS) ||
12329 	    (lun = softc->ctl_luns[targ_lun]) == NULL) {
12330 		mtx_unlock(&softc->ctl_lock);
12331 		io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12332 		return (1);
12333 	}
12334 	mtx_lock(&lun->lun_lock);
12335 	mtx_unlock(&softc->ctl_lock);
12336 	initidx = ctl_get_initindex(&io->io_hdr.nexus);
12337 	ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12338 	mtx_unlock(&lun->lun_lock);
12339 	if (ua != CTL_UA_NONE)
12340 		io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12341 	else
12342 		io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12343 	return (0);
12344 }
12345 
12346 static void
12347 ctl_run_task(union ctl_io *io)
12348 {
12349 	struct ctl_softc *softc = control_softc;
12350 	int retval = 1;
12351 
12352 	CTL_DEBUG_PRINT(("ctl_run_task\n"));
12353 	KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12354 	    ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12355 	io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12356 	bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12357 	switch (io->taskio.task_action) {
12358 	case CTL_TASK_ABORT_TASK:
12359 		retval = ctl_abort_task(io);
12360 		break;
12361 	case CTL_TASK_ABORT_TASK_SET:
12362 	case CTL_TASK_CLEAR_TASK_SET:
12363 		retval = ctl_abort_task_set(io);
12364 		break;
12365 	case CTL_TASK_CLEAR_ACA:
12366 		break;
12367 	case CTL_TASK_I_T_NEXUS_RESET:
12368 		retval = ctl_i_t_nexus_reset(io);
12369 		break;
12370 	case CTL_TASK_LUN_RESET:
12371 		retval = ctl_lun_reset(softc, io);
12372 		break;
12373 	case CTL_TASK_TARGET_RESET:
12374 		retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12375 		break;
12376 	case CTL_TASK_BUS_RESET:
12377 		retval = ctl_bus_reset(softc, io);
12378 		break;
12379 	case CTL_TASK_PORT_LOGIN:
12380 		break;
12381 	case CTL_TASK_PORT_LOGOUT:
12382 		break;
12383 	case CTL_TASK_QUERY_TASK:
12384 		retval = ctl_query_task(io, 0);
12385 		break;
12386 	case CTL_TASK_QUERY_TASK_SET:
12387 		retval = ctl_query_task(io, 1);
12388 		break;
12389 	case CTL_TASK_QUERY_ASYNC_EVENT:
12390 		retval = ctl_query_async_event(io);
12391 		break;
12392 	default:
12393 		printf("%s: got unknown task management event %d\n",
12394 		       __func__, io->taskio.task_action);
12395 		break;
12396 	}
12397 	if (retval == 0)
12398 		io->io_hdr.status = CTL_SUCCESS;
12399 	else
12400 		io->io_hdr.status = CTL_ERROR;
12401 	ctl_done(io);
12402 }
12403 
12404 /*
12405  * For HA operation.  Handle commands that come in from the other
12406  * controller.
12407  */
12408 static void
12409 ctl_handle_isc(union ctl_io *io)
12410 {
12411 	int free_io;
12412 	struct ctl_lun *lun;
12413 	struct ctl_softc *softc = control_softc;
12414 	uint32_t targ_lun;
12415 
12416 	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12417 	lun = softc->ctl_luns[targ_lun];
12418 
12419 	switch (io->io_hdr.msg_type) {
12420 	case CTL_MSG_SERIALIZE:
12421 		free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12422 		break;
12423 	case CTL_MSG_R2R: {
12424 		const struct ctl_cmd_entry *entry;
12425 
12426 		/*
12427 		 * This is only used in SER_ONLY mode.
12428 		 */
12429 		free_io = 0;
12430 		entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12431 		mtx_lock(&lun->lun_lock);
12432 		if (ctl_scsiio_lun_check(lun,
12433 		    entry, (struct ctl_scsiio *)io) != 0) {
12434 			mtx_unlock(&lun->lun_lock);
12435 			ctl_done(io);
12436 			break;
12437 		}
12438 		io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12439 		mtx_unlock(&lun->lun_lock);
12440 		ctl_enqueue_rtr(io);
12441 		break;
12442 	}
12443 	case CTL_MSG_FINISH_IO:
12444 		if (softc->ha_mode == CTL_HA_MODE_XFER) {
12445 			free_io = 0;
12446 			ctl_done(io);
12447 		} else {
12448 			free_io = 1;
12449 			mtx_lock(&lun->lun_lock);
12450 			TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12451 				     ooa_links);
12452 			ctl_check_blocked(lun);
12453 			mtx_unlock(&lun->lun_lock);
12454 		}
12455 		break;
12456 	case CTL_MSG_PERS_ACTION:
12457 		ctl_hndl_per_res_out_on_other_sc(
12458 			(union ctl_ha_msg *)&io->presio.pr_msg);
12459 		free_io = 1;
12460 		break;
12461 	case CTL_MSG_BAD_JUJU:
12462 		free_io = 0;
12463 		ctl_done(io);
12464 		break;
12465 	case CTL_MSG_DATAMOVE:
12466 		/* Only used in XFER mode */
12467 		free_io = 0;
12468 		ctl_datamove_remote(io);
12469 		break;
12470 	case CTL_MSG_DATAMOVE_DONE:
12471 		/* Only used in XFER mode */
12472 		free_io = 0;
12473 		io->scsiio.be_move_done(io);
12474 		break;
12475 	case CTL_MSG_FAILOVER:
12476 		ctl_failover_lun(io);
12477 		free_io = 1;
12478 		break;
12479 	default:
12480 		free_io = 1;
12481 		printf("%s: Invalid message type %d\n",
12482 		       __func__, io->io_hdr.msg_type);
12483 		break;
12484 	}
12485 	if (free_io)
12486 		ctl_free_io(io);
12487 
12488 }
12489 
12490 
12491 /*
12492  * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12493  * there is no match.
12494  */
12495 static ctl_lun_error_pattern
12496 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12497 {
12498 	const struct ctl_cmd_entry *entry;
12499 	ctl_lun_error_pattern filtered_pattern, pattern;
12500 
12501 	pattern = desc->error_pattern;
12502 
12503 	/*
12504 	 * XXX KDM we need more data passed into this function to match a
12505 	 * custom pattern, and we actually need to implement custom pattern
12506 	 * matching.
12507 	 */
12508 	if (pattern & CTL_LUN_PAT_CMD)
12509 		return (CTL_LUN_PAT_CMD);
12510 
12511 	if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12512 		return (CTL_LUN_PAT_ANY);
12513 
12514 	entry = ctl_get_cmd_entry(ctsio, NULL);
12515 
12516 	filtered_pattern = entry->pattern & pattern;
12517 
12518 	/*
12519 	 * If the user requested specific flags in the pattern (e.g.
12520 	 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12521 	 * flags.
12522 	 *
12523 	 * If the user did not specify any flags, it doesn't matter whether
12524 	 * or not the command supports the flags.
12525 	 */
12526 	if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12527 	     (pattern & ~CTL_LUN_PAT_MASK))
12528 		return (CTL_LUN_PAT_NONE);
12529 
12530 	/*
12531 	 * If the user asked for a range check, see if the requested LBA
12532 	 * range overlaps with this command's LBA range.
12533 	 */
12534 	if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12535 		uint64_t lba1;
12536 		uint64_t len1;
12537 		ctl_action action;
12538 		int retval;
12539 
12540 		retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12541 		if (retval != 0)
12542 			return (CTL_LUN_PAT_NONE);
12543 
12544 		action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12545 					      desc->lba_range.len, FALSE);
12546 		/*
12547 		 * A "pass" means that the LBA ranges don't overlap, so
12548 		 * this doesn't match the user's range criteria.
12549 		 */
12550 		if (action == CTL_ACTION_PASS)
12551 			return (CTL_LUN_PAT_NONE);
12552 	}
12553 
12554 	return (filtered_pattern);
12555 }
12556 
12557 static void
12558 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12559 {
12560 	struct ctl_error_desc *desc, *desc2;
12561 
12562 	mtx_assert(&lun->lun_lock, MA_OWNED);
12563 
12564 	STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12565 		ctl_lun_error_pattern pattern;
12566 		/*
12567 		 * Check to see whether this particular command matches
12568 		 * the pattern in the descriptor.
12569 		 */
12570 		pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12571 		if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12572 			continue;
12573 
12574 		switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12575 		case CTL_LUN_INJ_ABORTED:
12576 			ctl_set_aborted(&io->scsiio);
12577 			break;
12578 		case CTL_LUN_INJ_MEDIUM_ERR:
12579 			ctl_set_medium_error(&io->scsiio,
12580 			    (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12581 			     CTL_FLAG_DATA_OUT);
12582 			break;
12583 		case CTL_LUN_INJ_UA:
12584 			/* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET
12585 			 * OCCURRED */
12586 			ctl_set_ua(&io->scsiio, 0x29, 0x00);
12587 			break;
12588 		case CTL_LUN_INJ_CUSTOM:
12589 			/*
12590 			 * We're assuming the user knows what he is doing.
12591 			 * Just copy the sense information without doing
12592 			 * checks.
12593 			 */
12594 			bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12595 			      MIN(sizeof(desc->custom_sense),
12596 				  sizeof(io->scsiio.sense_data)));
12597 			io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12598 			io->scsiio.sense_len = SSD_FULL_SIZE;
12599 			io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12600 			break;
12601 		case CTL_LUN_INJ_NONE:
12602 		default:
12603 			/*
12604 			 * If this is an error injection type we don't know
12605 			 * about, clear the continuous flag (if it is set)
12606 			 * so it will get deleted below.
12607 			 */
12608 			desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12609 			break;
12610 		}
12611 		/*
12612 		 * By default, each error injection action is a one-shot
12613 		 */
12614 		if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12615 			continue;
12616 
12617 		STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12618 
12619 		free(desc, M_CTL);
12620 	}
12621 }
12622 
12623 #ifdef CTL_IO_DELAY
12624 static void
12625 ctl_datamove_timer_wakeup(void *arg)
12626 {
12627 	union ctl_io *io;
12628 
12629 	io = (union ctl_io *)arg;
12630 
12631 	ctl_datamove(io);
12632 }
12633 #endif /* CTL_IO_DELAY */
12634 
12635 void
12636 ctl_datamove(union ctl_io *io)
12637 {
12638 	struct ctl_lun *lun;
12639 	void (*fe_datamove)(union ctl_io *io);
12640 
12641 	mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12642 
12643 	CTL_DEBUG_PRINT(("ctl_datamove\n"));
12644 
12645 	lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12646 #ifdef CTL_TIME_IO
12647 	if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12648 		char str[256];
12649 		char path_str[64];
12650 		struct sbuf sb;
12651 
12652 		ctl_scsi_path_string(io, path_str, sizeof(path_str));
12653 		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12654 
12655 		sbuf_cat(&sb, path_str);
12656 		switch (io->io_hdr.io_type) {
12657 		case CTL_IO_SCSI:
12658 			ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12659 			sbuf_printf(&sb, "\n");
12660 			sbuf_cat(&sb, path_str);
12661 			sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12662 				    io->scsiio.tag_num, io->scsiio.tag_type);
12663 			break;
12664 		case CTL_IO_TASK:
12665 			sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12666 				    "Tag Type: %d\n", io->taskio.task_action,
12667 				    io->taskio.tag_num, io->taskio.tag_type);
12668 			break;
12669 		default:
12670 			panic("%s: Invalid CTL I/O type %d\n",
12671 			    __func__, io->io_hdr.io_type);
12672 		}
12673 		sbuf_cat(&sb, path_str);
12674 		sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12675 			    (intmax_t)time_uptime - io->io_hdr.start_time);
12676 		sbuf_finish(&sb);
12677 		printf("%s", sbuf_data(&sb));
12678 	}
12679 #endif /* CTL_TIME_IO */
12680 
12681 #ifdef CTL_IO_DELAY
12682 	if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12683 		io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12684 	} else {
12685 		if ((lun != NULL)
12686 		 && (lun->delay_info.datamove_delay > 0)) {
12687 
12688 			callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12689 			io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12690 			callout_reset(&io->io_hdr.delay_callout,
12691 				      lun->delay_info.datamove_delay * hz,
12692 				      ctl_datamove_timer_wakeup, io);
12693 			if (lun->delay_info.datamove_type ==
12694 			    CTL_DELAY_TYPE_ONESHOT)
12695 				lun->delay_info.datamove_delay = 0;
12696 			return;
12697 		}
12698 	}
12699 #endif
12700 
12701 	/*
12702 	 * This command has been aborted.  Set the port status, so we fail
12703 	 * the data move.
12704 	 */
12705 	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12706 		printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12707 		       io->scsiio.tag_num, io->io_hdr.nexus.initid,
12708 		       io->io_hdr.nexus.targ_port,
12709 		       io->io_hdr.nexus.targ_lun);
12710 		io->io_hdr.port_status = 31337;
12711 		/*
12712 		 * Note that the backend, in this case, will get the
12713 		 * callback in its context.  In other cases it may get
12714 		 * called in the frontend's interrupt thread context.
12715 		 */
12716 		io->scsiio.be_move_done(io);
12717 		return;
12718 	}
12719 
12720 	/* Don't confuse frontend with zero length data move. */
12721 	if (io->scsiio.kern_data_len == 0) {
12722 		io->scsiio.be_move_done(io);
12723 		return;
12724 	}
12725 
12726 	fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12727 	fe_datamove(io);
12728 }
12729 
12730 static void
12731 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12732 {
12733 	union ctl_ha_msg msg;
12734 #ifdef CTL_TIME_IO
12735 	struct bintime cur_bt;
12736 #endif
12737 
12738 	memset(&msg, 0, sizeof(msg));
12739 	msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12740 	msg.hdr.original_sc = io;
12741 	msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12742 	msg.hdr.nexus = io->io_hdr.nexus;
12743 	msg.hdr.status = io->io_hdr.status;
12744 	msg.scsi.tag_num = io->scsiio.tag_num;
12745 	msg.scsi.tag_type = io->scsiio.tag_type;
12746 	msg.scsi.scsi_status = io->scsiio.scsi_status;
12747 	memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12748 	       io->scsiio.sense_len);
12749 	msg.scsi.sense_len = io->scsiio.sense_len;
12750 	msg.scsi.sense_residual = io->scsiio.sense_residual;
12751 	msg.scsi.fetd_status = io->io_hdr.port_status;
12752 	msg.scsi.residual = io->scsiio.residual;
12753 	io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12754 	if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12755 		ctl_failover_io(io, /*have_lock*/ have_lock);
12756 		return;
12757 	}
12758 	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12759 	    sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12760 	    msg.scsi.sense_len, M_WAITOK);
12761 
12762 #ifdef CTL_TIME_IO
12763 	getbinuptime(&cur_bt);
12764 	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12765 	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12766 #endif
12767 	io->io_hdr.num_dmas++;
12768 }
12769 
12770 /*
12771  * The DMA to the remote side is done, now we need to tell the other side
12772  * we're done so it can continue with its data movement.
12773  */
12774 static void
12775 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12776 {
12777 	union ctl_io *io;
12778 	uint32_t i;
12779 
12780 	io = rq->context;
12781 
12782 	if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12783 		printf("%s: ISC DMA write failed with error %d", __func__,
12784 		       rq->ret);
12785 		ctl_set_internal_failure(&io->scsiio,
12786 					 /*sks_valid*/ 1,
12787 					 /*retry_count*/ rq->ret);
12788 	}
12789 
12790 	ctl_dt_req_free(rq);
12791 
12792 	for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12793 		free(io->io_hdr.local_sglist[i].addr, M_CTL);
12794 	free(io->io_hdr.remote_sglist, M_CTL);
12795 	io->io_hdr.remote_sglist = NULL;
12796 	io->io_hdr.local_sglist = NULL;
12797 
12798 	/*
12799 	 * The data is in local and remote memory, so now we need to send
12800 	 * status (good or back) back to the other side.
12801 	 */
12802 	ctl_send_datamove_done(io, /*have_lock*/ 0);
12803 }
12804 
12805 /*
12806  * We've moved the data from the host/controller into local memory.  Now we
12807  * need to push it over to the remote controller's memory.
12808  */
12809 static int
12810 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12811 {
12812 	int retval;
12813 
12814 	retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12815 					  ctl_datamove_remote_write_cb);
12816 	return (retval);
12817 }
12818 
12819 static void
12820 ctl_datamove_remote_write(union ctl_io *io)
12821 {
12822 	int retval;
12823 	void (*fe_datamove)(union ctl_io *io);
12824 
12825 	/*
12826 	 * - Get the data from the host/HBA into local memory.
12827 	 * - DMA memory from the local controller to the remote controller.
12828 	 * - Send status back to the remote controller.
12829 	 */
12830 
12831 	retval = ctl_datamove_remote_sgl_setup(io);
12832 	if (retval != 0)
12833 		return;
12834 
12835 	/* Switch the pointer over so the FETD knows what to do */
12836 	io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12837 
12838 	/*
12839 	 * Use a custom move done callback, since we need to send completion
12840 	 * back to the other controller, not to the backend on this side.
12841 	 */
12842 	io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12843 
12844 	fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12845 	fe_datamove(io);
12846 }
12847 
12848 static int
12849 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12850 {
12851 #if 0
12852 	char str[256];
12853 	char path_str[64];
12854 	struct sbuf sb;
12855 #endif
12856 	uint32_t i;
12857 
12858 	for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12859 		free(io->io_hdr.local_sglist[i].addr, M_CTL);
12860 	free(io->io_hdr.remote_sglist, M_CTL);
12861 	io->io_hdr.remote_sglist = NULL;
12862 	io->io_hdr.local_sglist = NULL;
12863 
12864 #if 0
12865 	scsi_path_string(io, path_str, sizeof(path_str));
12866 	sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12867 	sbuf_cat(&sb, path_str);
12868 	scsi_command_string(&io->scsiio, NULL, &sb);
12869 	sbuf_printf(&sb, "\n");
12870 	sbuf_cat(&sb, path_str);
12871 	sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12872 		    io->scsiio.tag_num, io->scsiio.tag_type);
12873 	sbuf_cat(&sb, path_str);
12874 	sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12875 		    io->io_hdr.flags, io->io_hdr.status);
12876 	sbuf_finish(&sb);
12877 	printk("%s", sbuf_data(&sb));
12878 #endif
12879 
12880 
12881 	/*
12882 	 * The read is done, now we need to send status (good or bad) back
12883 	 * to the other side.
12884 	 */
12885 	ctl_send_datamove_done(io, /*have_lock*/ 0);
12886 
12887 	return (0);
12888 }
12889 
12890 static void
12891 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12892 {
12893 	union ctl_io *io;
12894 	void (*fe_datamove)(union ctl_io *io);
12895 
12896 	io = rq->context;
12897 
12898 	if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12899 		printf("%s: ISC DMA read failed with error %d\n", __func__,
12900 		       rq->ret);
12901 		ctl_set_internal_failure(&io->scsiio,
12902 					 /*sks_valid*/ 1,
12903 					 /*retry_count*/ rq->ret);
12904 	}
12905 
12906 	ctl_dt_req_free(rq);
12907 
12908 	/* Switch the pointer over so the FETD knows what to do */
12909 	io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12910 
12911 	/*
12912 	 * Use a custom move done callback, since we need to send completion
12913 	 * back to the other controller, not to the backend on this side.
12914 	 */
12915 	io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12916 
12917 	/* XXX KDM add checks like the ones in ctl_datamove? */
12918 
12919 	fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12920 	fe_datamove(io);
12921 }
12922 
12923 static int
12924 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12925 {
12926 	struct ctl_sg_entry *local_sglist;
12927 	uint32_t len_to_go;
12928 	int retval;
12929 	int i;
12930 
12931 	retval = 0;
12932 	local_sglist = io->io_hdr.local_sglist;
12933 	len_to_go = io->scsiio.kern_data_len;
12934 
12935 	/*
12936 	 * The difficult thing here is that the size of the various
12937 	 * S/G segments may be different than the size from the
12938 	 * remote controller.  That'll make it harder when DMAing
12939 	 * the data back to the other side.
12940 	 */
12941 	for (i = 0; len_to_go > 0; i++) {
12942 		local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12943 		local_sglist[i].addr =
12944 		    malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12945 
12946 		len_to_go -= local_sglist[i].len;
12947 	}
12948 	/*
12949 	 * Reset the number of S/G entries accordingly.  The original
12950 	 * number of S/G entries is available in rem_sg_entries.
12951 	 */
12952 	io->scsiio.kern_sg_entries = i;
12953 
12954 #if 0
12955 	printf("%s: kern_sg_entries = %d\n", __func__,
12956 	       io->scsiio.kern_sg_entries);
12957 	for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12958 		printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12959 		       local_sglist[i].addr, local_sglist[i].len);
12960 #endif
12961 
12962 	return (retval);
12963 }
12964 
12965 static int
12966 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12967 			 ctl_ha_dt_cb callback)
12968 {
12969 	struct ctl_ha_dt_req *rq;
12970 	struct ctl_sg_entry *remote_sglist, *local_sglist;
12971 	uint32_t local_used, remote_used, total_used;
12972 	int i, j, isc_ret;
12973 
12974 	rq = ctl_dt_req_alloc();
12975 
12976 	/*
12977 	 * If we failed to allocate the request, and if the DMA didn't fail
12978 	 * anyway, set busy status.  This is just a resource allocation
12979 	 * failure.
12980 	 */
12981 	if ((rq == NULL)
12982 	 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12983 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12984 		ctl_set_busy(&io->scsiio);
12985 
12986 	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12987 	    (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12988 
12989 		if (rq != NULL)
12990 			ctl_dt_req_free(rq);
12991 
12992 		/*
12993 		 * The data move failed.  We need to return status back
12994 		 * to the other controller.  No point in trying to DMA
12995 		 * data to the remote controller.
12996 		 */
12997 
12998 		ctl_send_datamove_done(io, /*have_lock*/ 0);
12999 
13000 		return (1);
13001 	}
13002 
13003 	local_sglist = io->io_hdr.local_sglist;
13004 	remote_sglist = io->io_hdr.remote_sglist;
13005 	local_used = 0;
13006 	remote_used = 0;
13007 	total_used = 0;
13008 
13009 	/*
13010 	 * Pull/push the data over the wire from/to the other controller.
13011 	 * This takes into account the possibility that the local and
13012 	 * remote sglists may not be identical in terms of the size of
13013 	 * the elements and the number of elements.
13014 	 *
13015 	 * One fundamental assumption here is that the length allocated for
13016 	 * both the local and remote sglists is identical.  Otherwise, we've
13017 	 * essentially got a coding error of some sort.
13018 	 */
13019 	isc_ret = CTL_HA_STATUS_SUCCESS;
13020 	for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13021 		uint32_t cur_len;
13022 		uint8_t *tmp_ptr;
13023 
13024 		rq->command = command;
13025 		rq->context = io;
13026 
13027 		/*
13028 		 * Both pointers should be aligned.  But it is possible
13029 		 * that the allocation length is not.  They should both
13030 		 * also have enough slack left over at the end, though,
13031 		 * to round up to the next 8 byte boundary.
13032 		 */
13033 		cur_len = MIN(local_sglist[i].len - local_used,
13034 			      remote_sglist[j].len - remote_used);
13035 		rq->size = cur_len;
13036 
13037 		tmp_ptr = (uint8_t *)local_sglist[i].addr;
13038 		tmp_ptr += local_used;
13039 
13040 #if 0
13041 		/* Use physical addresses when talking to ISC hardware */
13042 		if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13043 			/* XXX KDM use busdma */
13044 			rq->local = vtophys(tmp_ptr);
13045 		} else
13046 			rq->local = tmp_ptr;
13047 #else
13048 		KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13049 		    ("HA does not support BUS_ADDR"));
13050 		rq->local = tmp_ptr;
13051 #endif
13052 
13053 		tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13054 		tmp_ptr += remote_used;
13055 		rq->remote = tmp_ptr;
13056 
13057 		rq->callback = NULL;
13058 
13059 		local_used += cur_len;
13060 		if (local_used >= local_sglist[i].len) {
13061 			i++;
13062 			local_used = 0;
13063 		}
13064 
13065 		remote_used += cur_len;
13066 		if (remote_used >= remote_sglist[j].len) {
13067 			j++;
13068 			remote_used = 0;
13069 		}
13070 		total_used += cur_len;
13071 
13072 		if (total_used >= io->scsiio.kern_data_len)
13073 			rq->callback = callback;
13074 
13075 #if 0
13076 		printf("%s: %s: local %p remote %p size %d\n", __func__,
13077 		       (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13078 		       rq->local, rq->remote, rq->size);
13079 #endif
13080 
13081 		isc_ret = ctl_dt_single(rq);
13082 		if (isc_ret > CTL_HA_STATUS_SUCCESS)
13083 			break;
13084 	}
13085 	if (isc_ret != CTL_HA_STATUS_WAIT) {
13086 		rq->ret = isc_ret;
13087 		callback(rq);
13088 	}
13089 
13090 	return (0);
13091 }
13092 
13093 static void
13094 ctl_datamove_remote_read(union ctl_io *io)
13095 {
13096 	int retval;
13097 	uint32_t i;
13098 
13099 	/*
13100 	 * This will send an error to the other controller in the case of a
13101 	 * failure.
13102 	 */
13103 	retval = ctl_datamove_remote_sgl_setup(io);
13104 	if (retval != 0)
13105 		return;
13106 
13107 	retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13108 					  ctl_datamove_remote_read_cb);
13109 	if (retval != 0) {
13110 		/*
13111 		 * Make sure we free memory if there was an error..  The
13112 		 * ctl_datamove_remote_xfer() function will send the
13113 		 * datamove done message, or call the callback with an
13114 		 * error if there is a problem.
13115 		 */
13116 		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13117 			free(io->io_hdr.local_sglist[i].addr, M_CTL);
13118 		free(io->io_hdr.remote_sglist, M_CTL);
13119 		io->io_hdr.remote_sglist = NULL;
13120 		io->io_hdr.local_sglist = NULL;
13121 	}
13122 }
13123 
13124 /*
13125  * Process a datamove request from the other controller.  This is used for
13126  * XFER mode only, not SER_ONLY mode.  For writes, we DMA into local memory
13127  * first.  Once that is complete, the data gets DMAed into the remote
13128  * controller's memory.  For reads, we DMA from the remote controller's
13129  * memory into our memory first, and then move it out to the FETD.
13130  */
13131 static void
13132 ctl_datamove_remote(union ctl_io *io)
13133 {
13134 
13135 	mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13136 
13137 	if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13138 		ctl_failover_io(io, /*have_lock*/ 0);
13139 		return;
13140 	}
13141 
13142 	/*
13143 	 * Note that we look for an aborted I/O here, but don't do some of
13144 	 * the other checks that ctl_datamove() normally does.
13145 	 * We don't need to run the datamove delay code, since that should
13146 	 * have been done if need be on the other controller.
13147 	 */
13148 	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13149 		printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13150 		       io->scsiio.tag_num, io->io_hdr.nexus.initid,
13151 		       io->io_hdr.nexus.targ_port,
13152 		       io->io_hdr.nexus.targ_lun);
13153 		io->io_hdr.port_status = 31338;
13154 		ctl_send_datamove_done(io, /*have_lock*/ 0);
13155 		return;
13156 	}
13157 
13158 	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13159 		ctl_datamove_remote_write(io);
13160 	else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13161 		ctl_datamove_remote_read(io);
13162 	else {
13163 		io->io_hdr.port_status = 31339;
13164 		ctl_send_datamove_done(io, /*have_lock*/ 0);
13165 	}
13166 }
13167 
13168 static void
13169 ctl_process_done(union ctl_io *io)
13170 {
13171 	struct ctl_lun *lun;
13172 	struct ctl_softc *softc = control_softc;
13173 	void (*fe_done)(union ctl_io *io);
13174 	union ctl_ha_msg msg;
13175 	uint32_t targ_port = io->io_hdr.nexus.targ_port;
13176 
13177 	CTL_DEBUG_PRINT(("ctl_process_done\n"));
13178 	fe_done = softc->ctl_ports[targ_port]->fe_done;
13179 
13180 #ifdef CTL_TIME_IO
13181 	if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13182 		char str[256];
13183 		char path_str[64];
13184 		struct sbuf sb;
13185 
13186 		ctl_scsi_path_string(io, path_str, sizeof(path_str));
13187 		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13188 
13189 		sbuf_cat(&sb, path_str);
13190 		switch (io->io_hdr.io_type) {
13191 		case CTL_IO_SCSI:
13192 			ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13193 			sbuf_printf(&sb, "\n");
13194 			sbuf_cat(&sb, path_str);
13195 			sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13196 				    io->scsiio.tag_num, io->scsiio.tag_type);
13197 			break;
13198 		case CTL_IO_TASK:
13199 			sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13200 				    "Tag Type: %d\n", io->taskio.task_action,
13201 				    io->taskio.tag_num, io->taskio.tag_type);
13202 			break;
13203 		default:
13204 			panic("%s: Invalid CTL I/O type %d\n",
13205 			    __func__, io->io_hdr.io_type);
13206 		}
13207 		sbuf_cat(&sb, path_str);
13208 		sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13209 			    (intmax_t)time_uptime - io->io_hdr.start_time);
13210 		sbuf_finish(&sb);
13211 		printf("%s", sbuf_data(&sb));
13212 	}
13213 #endif /* CTL_TIME_IO */
13214 
13215 	switch (io->io_hdr.io_type) {
13216 	case CTL_IO_SCSI:
13217 		break;
13218 	case CTL_IO_TASK:
13219 		if (ctl_debug & CTL_DEBUG_INFO)
13220 			ctl_io_error_print(io, NULL);
13221 		fe_done(io);
13222 		return;
13223 	default:
13224 		panic("%s: Invalid CTL I/O type %d\n",
13225 		    __func__, io->io_hdr.io_type);
13226 	}
13227 
13228 	lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13229 	if (lun == NULL) {
13230 		CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13231 				 io->io_hdr.nexus.targ_mapped_lun));
13232 		goto bailout;
13233 	}
13234 
13235 	mtx_lock(&lun->lun_lock);
13236 
13237 	/*
13238 	 * Check to see if we have any errors to inject here.  We only
13239 	 * inject errors for commands that don't already have errors set.
13240 	 */
13241 	if (!STAILQ_EMPTY(&lun->error_list) &&
13242 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13243 	    ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13244 		ctl_inject_error(lun, io);
13245 
13246 	/*
13247 	 * XXX KDM how do we treat commands that aren't completed
13248 	 * successfully?
13249 	 *
13250 	 * XXX KDM should we also track I/O latency?
13251 	 */
13252 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13253 	    io->io_hdr.io_type == CTL_IO_SCSI) {
13254 #ifdef CTL_TIME_IO
13255 		struct bintime cur_bt;
13256 #endif
13257 		int type;
13258 
13259 		if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13260 		    CTL_FLAG_DATA_IN)
13261 			type = CTL_STATS_READ;
13262 		else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13263 		    CTL_FLAG_DATA_OUT)
13264 			type = CTL_STATS_WRITE;
13265 		else
13266 			type = CTL_STATS_NO_IO;
13267 
13268 		lun->stats.ports[targ_port].bytes[type] +=
13269 		    io->scsiio.kern_total_len;
13270 		lun->stats.ports[targ_port].operations[type]++;
13271 #ifdef CTL_TIME_IO
13272 		bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13273 		   &io->io_hdr.dma_bt);
13274 		getbinuptime(&cur_bt);
13275 		bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13276 		bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13277 #endif
13278 		lun->stats.ports[targ_port].num_dmas[type] +=
13279 		    io->io_hdr.num_dmas;
13280 	}
13281 
13282 	/*
13283 	 * Remove this from the OOA queue.
13284 	 */
13285 	TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13286 #ifdef CTL_TIME_IO
13287 	if (TAILQ_EMPTY(&lun->ooa_queue))
13288 		lun->last_busy = getsbinuptime();
13289 #endif
13290 
13291 	/*
13292 	 * Run through the blocked queue on this LUN and see if anything
13293 	 * has become unblocked, now that this transaction is done.
13294 	 */
13295 	ctl_check_blocked(lun);
13296 
13297 	/*
13298 	 * If the LUN has been invalidated, free it if there is nothing
13299 	 * left on its OOA queue.
13300 	 */
13301 	if ((lun->flags & CTL_LUN_INVALID)
13302 	 && TAILQ_EMPTY(&lun->ooa_queue)) {
13303 		mtx_unlock(&lun->lun_lock);
13304 		mtx_lock(&softc->ctl_lock);
13305 		ctl_free_lun(lun);
13306 		mtx_unlock(&softc->ctl_lock);
13307 	} else
13308 		mtx_unlock(&lun->lun_lock);
13309 
13310 bailout:
13311 
13312 	/*
13313 	 * If this command has been aborted, make sure we set the status
13314 	 * properly.  The FETD is responsible for freeing the I/O and doing
13315 	 * whatever it needs to do to clean up its state.
13316 	 */
13317 	if (io->io_hdr.flags & CTL_FLAG_ABORT)
13318 		ctl_set_task_aborted(&io->scsiio);
13319 
13320 	/*
13321 	 * If enabled, print command error status.
13322 	 */
13323 	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13324 	    (ctl_debug & CTL_DEBUG_INFO) != 0)
13325 		ctl_io_error_print(io, NULL);
13326 
13327 	/*
13328 	 * Tell the FETD or the other shelf controller we're done with this
13329 	 * command.  Note that only SCSI commands get to this point.  Task
13330 	 * management commands are completed above.
13331 	 */
13332 	if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13333 	    (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13334 		memset(&msg, 0, sizeof(msg));
13335 		msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13336 		msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13337 		msg.hdr.nexus = io->io_hdr.nexus;
13338 		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13339 		    sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13340 		    M_WAITOK);
13341 	}
13342 
13343 	fe_done(io);
13344 }
13345 
13346 #ifdef CTL_WITH_CA
13347 /*
13348  * Front end should call this if it doesn't do autosense.  When the request
13349  * sense comes back in from the initiator, we'll dequeue this and send it.
13350  */
13351 int
13352 ctl_queue_sense(union ctl_io *io)
13353 {
13354 	struct ctl_lun *lun;
13355 	struct ctl_port *port;
13356 	struct ctl_softc *softc;
13357 	uint32_t initidx, targ_lun;
13358 
13359 	softc = control_softc;
13360 
13361 	CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13362 
13363 	/*
13364 	 * LUN lookup will likely move to the ctl_work_thread() once we
13365 	 * have our new queueing infrastructure (that doesn't put things on
13366 	 * a per-LUN queue initially).  That is so that we can handle
13367 	 * things like an INQUIRY to a LUN that we don't have enabled.  We
13368 	 * can't deal with that right now.
13369 	 */
13370 	mtx_lock(&softc->ctl_lock);
13371 
13372 	/*
13373 	 * If we don't have a LUN for this, just toss the sense
13374 	 * information.
13375 	 */
13376 	port = ctl_io_port(&ctsio->io_hdr);
13377 	targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13378 	if ((targ_lun < CTL_MAX_LUNS)
13379 	 && (softc->ctl_luns[targ_lun] != NULL))
13380 		lun = softc->ctl_luns[targ_lun];
13381 	else
13382 		goto bailout;
13383 
13384 	initidx = ctl_get_initindex(&io->io_hdr.nexus);
13385 
13386 	mtx_lock(&lun->lun_lock);
13387 	/*
13388 	 * Already have CA set for this LUN...toss the sense information.
13389 	 */
13390 	if (ctl_is_set(lun->have_ca, initidx)) {
13391 		mtx_unlock(&lun->lun_lock);
13392 		goto bailout;
13393 	}
13394 
13395 	memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13396 	       MIN(sizeof(lun->pending_sense[initidx]),
13397 	       sizeof(io->scsiio.sense_data)));
13398 	ctl_set_mask(lun->have_ca, initidx);
13399 	mtx_unlock(&lun->lun_lock);
13400 
13401 bailout:
13402 	mtx_unlock(&softc->ctl_lock);
13403 
13404 	ctl_free_io(io);
13405 
13406 	return (CTL_RETVAL_COMPLETE);
13407 }
13408 #endif
13409 
13410 /*
13411  * Primary command inlet from frontend ports.  All SCSI and task I/O
13412  * requests must go through this function.
13413  */
13414 int
13415 ctl_queue(union ctl_io *io)
13416 {
13417 	struct ctl_port *port;
13418 
13419 	CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13420 
13421 #ifdef CTL_TIME_IO
13422 	io->io_hdr.start_time = time_uptime;
13423 	getbinuptime(&io->io_hdr.start_bt);
13424 #endif /* CTL_TIME_IO */
13425 
13426 	/* Map FE-specific LUN ID into global one. */
13427 	port = ctl_io_port(&io->io_hdr);
13428 	io->io_hdr.nexus.targ_mapped_lun =
13429 	    ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13430 
13431 	switch (io->io_hdr.io_type) {
13432 	case CTL_IO_SCSI:
13433 	case CTL_IO_TASK:
13434 		if (ctl_debug & CTL_DEBUG_CDB)
13435 			ctl_io_print(io);
13436 		ctl_enqueue_incoming(io);
13437 		break;
13438 	default:
13439 		printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13440 		return (EINVAL);
13441 	}
13442 
13443 	return (CTL_RETVAL_COMPLETE);
13444 }
13445 
13446 #ifdef CTL_IO_DELAY
13447 static void
13448 ctl_done_timer_wakeup(void *arg)
13449 {
13450 	union ctl_io *io;
13451 
13452 	io = (union ctl_io *)arg;
13453 	ctl_done(io);
13454 }
13455 #endif /* CTL_IO_DELAY */
13456 
13457 void
13458 ctl_serseq_done(union ctl_io *io)
13459 {
13460 	struct ctl_lun *lun;
13461 
13462 	lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13463 	if (lun->be_lun == NULL ||
13464 	    lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13465 		return;
13466 	mtx_lock(&lun->lun_lock);
13467 	io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13468 	ctl_check_blocked(lun);
13469 	mtx_unlock(&lun->lun_lock);
13470 }
13471 
13472 void
13473 ctl_done(union ctl_io *io)
13474 {
13475 
13476 	/*
13477 	 * Enable this to catch duplicate completion issues.
13478 	 */
13479 #if 0
13480 	if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13481 		printf("%s: type %d msg %d cdb %x iptl: "
13482 		       "%u:%u:%u tag 0x%04x "
13483 		       "flag %#x status %x\n",
13484 			__func__,
13485 			io->io_hdr.io_type,
13486 			io->io_hdr.msg_type,
13487 			io->scsiio.cdb[0],
13488 			io->io_hdr.nexus.initid,
13489 			io->io_hdr.nexus.targ_port,
13490 			io->io_hdr.nexus.targ_lun,
13491 			(io->io_hdr.io_type ==
13492 			CTL_IO_TASK) ?
13493 			io->taskio.tag_num :
13494 			io->scsiio.tag_num,
13495 		        io->io_hdr.flags,
13496 			io->io_hdr.status);
13497 	} else
13498 		io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13499 #endif
13500 
13501 	/*
13502 	 * This is an internal copy of an I/O, and should not go through
13503 	 * the normal done processing logic.
13504 	 */
13505 	if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13506 		return;
13507 
13508 #ifdef CTL_IO_DELAY
13509 	if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13510 		struct ctl_lun *lun;
13511 
13512 		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13513 
13514 		io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13515 	} else {
13516 		struct ctl_lun *lun;
13517 
13518 		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13519 
13520 		if ((lun != NULL)
13521 		 && (lun->delay_info.done_delay > 0)) {
13522 
13523 			callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13524 			io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13525 			callout_reset(&io->io_hdr.delay_callout,
13526 				      lun->delay_info.done_delay * hz,
13527 				      ctl_done_timer_wakeup, io);
13528 			if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13529 				lun->delay_info.done_delay = 0;
13530 			return;
13531 		}
13532 	}
13533 #endif /* CTL_IO_DELAY */
13534 
13535 	ctl_enqueue_done(io);
13536 }
13537 
13538 static void
13539 ctl_work_thread(void *arg)
13540 {
13541 	struct ctl_thread *thr = (struct ctl_thread *)arg;
13542 	struct ctl_softc *softc = thr->ctl_softc;
13543 	union ctl_io *io;
13544 	int retval;
13545 
13546 	CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13547 
13548 	for (;;) {
13549 		/*
13550 		 * We handle the queues in this order:
13551 		 * - ISC
13552 		 * - done queue (to free up resources, unblock other commands)
13553 		 * - RtR queue
13554 		 * - incoming queue
13555 		 *
13556 		 * If those queues are empty, we break out of the loop and
13557 		 * go to sleep.
13558 		 */
13559 		mtx_lock(&thr->queue_lock);
13560 		io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13561 		if (io != NULL) {
13562 			STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13563 			mtx_unlock(&thr->queue_lock);
13564 			ctl_handle_isc(io);
13565 			continue;
13566 		}
13567 		io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13568 		if (io != NULL) {
13569 			STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13570 			/* clear any blocked commands, call fe_done */
13571 			mtx_unlock(&thr->queue_lock);
13572 			ctl_process_done(io);
13573 			continue;
13574 		}
13575 		io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13576 		if (io != NULL) {
13577 			STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13578 			mtx_unlock(&thr->queue_lock);
13579 			if (io->io_hdr.io_type == CTL_IO_TASK)
13580 				ctl_run_task(io);
13581 			else
13582 				ctl_scsiio_precheck(softc, &io->scsiio);
13583 			continue;
13584 		}
13585 		io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13586 		if (io != NULL) {
13587 			STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13588 			mtx_unlock(&thr->queue_lock);
13589 			retval = ctl_scsiio(&io->scsiio);
13590 			if (retval != CTL_RETVAL_COMPLETE)
13591 				CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13592 			continue;
13593 		}
13594 
13595 		/* Sleep until we have something to do. */
13596 		mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13597 	}
13598 }
13599 
13600 static void
13601 ctl_lun_thread(void *arg)
13602 {
13603 	struct ctl_softc *softc = (struct ctl_softc *)arg;
13604 	struct ctl_be_lun *be_lun;
13605 
13606 	CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13607 
13608 	for (;;) {
13609 		mtx_lock(&softc->ctl_lock);
13610 		be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13611 		if (be_lun != NULL) {
13612 			STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13613 			mtx_unlock(&softc->ctl_lock);
13614 			ctl_create_lun(be_lun);
13615 			continue;
13616 		}
13617 
13618 		/* Sleep until we have something to do. */
13619 		mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13620 		    PDROP | PRIBIO, "-", 0);
13621 	}
13622 }
13623 
13624 static void
13625 ctl_thresh_thread(void *arg)
13626 {
13627 	struct ctl_softc *softc = (struct ctl_softc *)arg;
13628 	struct ctl_lun *lun;
13629 	struct scsi_da_rw_recovery_page *rwpage;
13630 	struct ctl_logical_block_provisioning_page *page;
13631 	const char *attr;
13632 	union ctl_ha_msg msg;
13633 	uint64_t thres, val;
13634 	int i, e, set;
13635 
13636 	CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13637 
13638 	for (;;) {
13639 		mtx_lock(&softc->ctl_lock);
13640 		STAILQ_FOREACH(lun, &softc->lun_list, links) {
13641 			if ((lun->flags & CTL_LUN_DISABLED) ||
13642 			    (lun->flags & CTL_LUN_NO_MEDIA) ||
13643 			    lun->backend->lun_attr == NULL)
13644 				continue;
13645 			if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13646 			    softc->ha_mode == CTL_HA_MODE_XFER)
13647 				continue;
13648 			rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13649 			if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13650 				continue;
13651 			e = 0;
13652 			page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13653 			for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13654 				if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13655 					continue;
13656 				thres = scsi_4btoul(page->descr[i].count);
13657 				thres <<= CTL_LBP_EXPONENT;
13658 				switch (page->descr[i].resource) {
13659 				case 0x01:
13660 					attr = "blocksavail";
13661 					break;
13662 				case 0x02:
13663 					attr = "blocksused";
13664 					break;
13665 				case 0xf1:
13666 					attr = "poolblocksavail";
13667 					break;
13668 				case 0xf2:
13669 					attr = "poolblocksused";
13670 					break;
13671 				default:
13672 					continue;
13673 				}
13674 				mtx_unlock(&softc->ctl_lock); // XXX
13675 				val = lun->backend->lun_attr(
13676 				    lun->be_lun->be_lun, attr);
13677 				mtx_lock(&softc->ctl_lock);
13678 				if (val == UINT64_MAX)
13679 					continue;
13680 				if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13681 				    == SLBPPD_ARMING_INC)
13682 					e = (val >= thres);
13683 				else
13684 					e = (val <= thres);
13685 				if (e)
13686 					break;
13687 			}
13688 			mtx_lock(&lun->lun_lock);
13689 			if (e) {
13690 				scsi_u64to8b((uint8_t *)&page->descr[i] -
13691 				    (uint8_t *)page, lun->ua_tpt_info);
13692 				if (lun->lasttpt == 0 ||
13693 				    time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13694 					lun->lasttpt = time_uptime;
13695 					ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13696 					set = 1;
13697 				} else
13698 					set = 0;
13699 			} else {
13700 				lun->lasttpt = 0;
13701 				ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13702 				set = -1;
13703 			}
13704 			mtx_unlock(&lun->lun_lock);
13705 			if (set != 0 &&
13706 			    lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13707 				/* Send msg to other side. */
13708 				bzero(&msg.ua, sizeof(msg.ua));
13709 				msg.hdr.msg_type = CTL_MSG_UA;
13710 				msg.hdr.nexus.initid = -1;
13711 				msg.hdr.nexus.targ_port = -1;
13712 				msg.hdr.nexus.targ_lun = lun->lun;
13713 				msg.hdr.nexus.targ_mapped_lun = lun->lun;
13714 				msg.ua.ua_all = 1;
13715 				msg.ua.ua_set = (set > 0);
13716 				msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13717 				memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13718 				mtx_unlock(&softc->ctl_lock); // XXX
13719 				ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13720 				    sizeof(msg.ua), M_WAITOK);
13721 				mtx_lock(&softc->ctl_lock);
13722 			}
13723 		}
13724 		mtx_unlock(&softc->ctl_lock);
13725 		pause("-", CTL_LBP_PERIOD * hz);
13726 	}
13727 }
13728 
13729 static void
13730 ctl_enqueue_incoming(union ctl_io *io)
13731 {
13732 	struct ctl_softc *softc = control_softc;
13733 	struct ctl_thread *thr;
13734 	u_int idx;
13735 
13736 	idx = (io->io_hdr.nexus.targ_port * 127 +
13737 	       io->io_hdr.nexus.initid) % worker_threads;
13738 	thr = &softc->threads[idx];
13739 	mtx_lock(&thr->queue_lock);
13740 	STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13741 	mtx_unlock(&thr->queue_lock);
13742 	wakeup(thr);
13743 }
13744 
13745 static void
13746 ctl_enqueue_rtr(union ctl_io *io)
13747 {
13748 	struct ctl_softc *softc = control_softc;
13749 	struct ctl_thread *thr;
13750 
13751 	thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13752 	mtx_lock(&thr->queue_lock);
13753 	STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13754 	mtx_unlock(&thr->queue_lock);
13755 	wakeup(thr);
13756 }
13757 
13758 static void
13759 ctl_enqueue_done(union ctl_io *io)
13760 {
13761 	struct ctl_softc *softc = control_softc;
13762 	struct ctl_thread *thr;
13763 
13764 	thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13765 	mtx_lock(&thr->queue_lock);
13766 	STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13767 	mtx_unlock(&thr->queue_lock);
13768 	wakeup(thr);
13769 }
13770 
13771 static void
13772 ctl_enqueue_isc(union ctl_io *io)
13773 {
13774 	struct ctl_softc *softc = control_softc;
13775 	struct ctl_thread *thr;
13776 
13777 	thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13778 	mtx_lock(&thr->queue_lock);
13779 	STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13780 	mtx_unlock(&thr->queue_lock);
13781 	wakeup(thr);
13782 }
13783 
13784 /*
13785  *  vim: ts=8
13786  */
13787