ctl.c revision 287720
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: head/sys/cam/ctl/ctl.c 287720 2015-09-12 18:29:05Z mav $");
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_da.h>
73#include <cam/ctl/ctl_io.h>
74#include <cam/ctl/ctl.h>
75#include <cam/ctl/ctl_frontend.h>
76#include <cam/ctl/ctl_util.h>
77#include <cam/ctl/ctl_backend.h>
78#include <cam/ctl/ctl_ioctl.h>
79#include <cam/ctl/ctl_ha.h>
80#include <cam/ctl/ctl_private.h>
81#include <cam/ctl/ctl_debug.h>
82#include <cam/ctl/ctl_scsi_all.h>
83#include <cam/ctl/ctl_error.h>
84
85struct ctl_softc *control_softc = NULL;
86
87/*
88 * Template mode pages.
89 */
90
91/*
92 * Note that these are default values only.  The actual values will be
93 * filled in when the user does a mode sense.
94 */
95const static struct copan_debugconf_subpage debugconf_page_default = {
96	DBGCNF_PAGE_CODE | SMPH_SPF,	/* page_code */
97	DBGCNF_SUBPAGE_CODE,		/* subpage */
98	{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
99	 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
100	DBGCNF_VERSION,			/* page_version */
101	{CTL_TIME_IO_DEFAULT_SECS>>8,
102	 CTL_TIME_IO_DEFAULT_SECS>>0},	/* ctl_time_io_secs */
103};
104
105const static struct copan_debugconf_subpage debugconf_page_changeable = {
106	DBGCNF_PAGE_CODE | SMPH_SPF,	/* page_code */
107	DBGCNF_SUBPAGE_CODE,		/* subpage */
108	{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
109	 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
110	0,				/* page_version */
111	{0xff,0xff},			/* ctl_time_io_secs */
112};
113
114const static struct scsi_da_rw_recovery_page rw_er_page_default = {
115	/*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
116	/*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
117	/*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
118	/*read_retry_count*/0,
119	/*correction_span*/0,
120	/*head_offset_count*/0,
121	/*data_strobe_offset_cnt*/0,
122	/*byte8*/SMS_RWER_LBPERE,
123	/*write_retry_count*/0,
124	/*reserved2*/0,
125	/*recovery_time_limit*/{0, 0},
126};
127
128const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
129	/*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
130	/*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
131	/*byte3*/0,
132	/*read_retry_count*/0,
133	/*correction_span*/0,
134	/*head_offset_count*/0,
135	/*data_strobe_offset_cnt*/0,
136	/*byte8*/0,
137	/*write_retry_count*/0,
138	/*reserved2*/0,
139	/*recovery_time_limit*/{0, 0},
140};
141
142const static struct scsi_format_page format_page_default = {
143	/*page_code*/SMS_FORMAT_DEVICE_PAGE,
144	/*page_length*/sizeof(struct scsi_format_page) - 2,
145	/*tracks_per_zone*/ {0, 0},
146	/*alt_sectors_per_zone*/ {0, 0},
147	/*alt_tracks_per_zone*/ {0, 0},
148	/*alt_tracks_per_lun*/ {0, 0},
149	/*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
150			        CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
151	/*bytes_per_sector*/ {0, 0},
152	/*interleave*/ {0, 0},
153	/*track_skew*/ {0, 0},
154	/*cylinder_skew*/ {0, 0},
155	/*flags*/ SFP_HSEC,
156	/*reserved*/ {0, 0, 0}
157};
158
159const static struct scsi_format_page format_page_changeable = {
160	/*page_code*/SMS_FORMAT_DEVICE_PAGE,
161	/*page_length*/sizeof(struct scsi_format_page) - 2,
162	/*tracks_per_zone*/ {0, 0},
163	/*alt_sectors_per_zone*/ {0, 0},
164	/*alt_tracks_per_zone*/ {0, 0},
165	/*alt_tracks_per_lun*/ {0, 0},
166	/*sectors_per_track*/ {0, 0},
167	/*bytes_per_sector*/ {0, 0},
168	/*interleave*/ {0, 0},
169	/*track_skew*/ {0, 0},
170	/*cylinder_skew*/ {0, 0},
171	/*flags*/ 0,
172	/*reserved*/ {0, 0, 0}
173};
174
175const static struct scsi_rigid_disk_page rigid_disk_page_default = {
176	/*page_code*/SMS_RIGID_DISK_PAGE,
177	/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
178	/*cylinders*/ {0, 0, 0},
179	/*heads*/ CTL_DEFAULT_HEADS,
180	/*start_write_precomp*/ {0, 0, 0},
181	/*start_reduced_current*/ {0, 0, 0},
182	/*step_rate*/ {0, 0},
183	/*landing_zone_cylinder*/ {0, 0, 0},
184	/*rpl*/ SRDP_RPL_DISABLED,
185	/*rotational_offset*/ 0,
186	/*reserved1*/ 0,
187	/*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
188			   CTL_DEFAULT_ROTATION_RATE & 0xff},
189	/*reserved2*/ {0, 0}
190};
191
192const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
193	/*page_code*/SMS_RIGID_DISK_PAGE,
194	/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
195	/*cylinders*/ {0, 0, 0},
196	/*heads*/ 0,
197	/*start_write_precomp*/ {0, 0, 0},
198	/*start_reduced_current*/ {0, 0, 0},
199	/*step_rate*/ {0, 0},
200	/*landing_zone_cylinder*/ {0, 0, 0},
201	/*rpl*/ 0,
202	/*rotational_offset*/ 0,
203	/*reserved1*/ 0,
204	/*rotation_rate*/ {0, 0},
205	/*reserved2*/ {0, 0}
206};
207
208const static struct scsi_caching_page caching_page_default = {
209	/*page_code*/SMS_CACHING_PAGE,
210	/*page_length*/sizeof(struct scsi_caching_page) - 2,
211	/*flags1*/ SCP_DISC | SCP_WCE,
212	/*ret_priority*/ 0,
213	/*disable_pf_transfer_len*/ {0xff, 0xff},
214	/*min_prefetch*/ {0, 0},
215	/*max_prefetch*/ {0xff, 0xff},
216	/*max_pf_ceiling*/ {0xff, 0xff},
217	/*flags2*/ 0,
218	/*cache_segments*/ 0,
219	/*cache_seg_size*/ {0, 0},
220	/*reserved*/ 0,
221	/*non_cache_seg_size*/ {0, 0, 0}
222};
223
224const static struct scsi_caching_page caching_page_changeable = {
225	/*page_code*/SMS_CACHING_PAGE,
226	/*page_length*/sizeof(struct scsi_caching_page) - 2,
227	/*flags1*/ SCP_WCE | SCP_RCD,
228	/*ret_priority*/ 0,
229	/*disable_pf_transfer_len*/ {0, 0},
230	/*min_prefetch*/ {0, 0},
231	/*max_prefetch*/ {0, 0},
232	/*max_pf_ceiling*/ {0, 0},
233	/*flags2*/ 0,
234	/*cache_segments*/ 0,
235	/*cache_seg_size*/ {0, 0},
236	/*reserved*/ 0,
237	/*non_cache_seg_size*/ {0, 0, 0}
238};
239
240const static struct scsi_control_page control_page_default = {
241	/*page_code*/SMS_CONTROL_MODE_PAGE,
242	/*page_length*/sizeof(struct scsi_control_page) - 2,
243	/*rlec*/0,
244	/*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
245	/*eca_and_aen*/0,
246	/*flags4*/SCP_TAS,
247	/*aen_holdoff_period*/{0, 0},
248	/*busy_timeout_period*/{0, 0},
249	/*extended_selftest_completion_time*/{0, 0}
250};
251
252const static struct scsi_control_page control_page_changeable = {
253	/*page_code*/SMS_CONTROL_MODE_PAGE,
254	/*page_length*/sizeof(struct scsi_control_page) - 2,
255	/*rlec*/SCP_DSENSE,
256	/*queue_flags*/SCP_QUEUE_ALG_MASK,
257	/*eca_and_aen*/SCP_SWP,
258	/*flags4*/0,
259	/*aen_holdoff_period*/{0, 0},
260	/*busy_timeout_period*/{0, 0},
261	/*extended_selftest_completion_time*/{0, 0}
262};
263
264const static struct scsi_info_exceptions_page ie_page_default = {
265	/*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
266	/*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
267	/*info_flags*/SIEP_FLAGS_DEXCPT,
268	/*mrie*/0,
269	/*interval_timer*/{0, 0, 0, 0},
270	/*report_count*/{0, 0, 0, 0}
271};
272
273const static struct scsi_info_exceptions_page ie_page_changeable = {
274	/*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
275	/*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
276	/*info_flags*/0,
277	/*mrie*/0,
278	/*interval_timer*/{0, 0, 0, 0},
279	/*report_count*/{0, 0, 0, 0}
280};
281
282#define CTL_LBPM_LEN	(sizeof(struct ctl_logical_block_provisioning_page) - 4)
283
284const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
285	/*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
286	/*subpage_code*/0x02,
287	/*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
288	/*flags*/0,
289	/*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
290	/*descr*/{}},
291	{{/*flags*/0,
292	  /*resource*/0x01,
293	  /*reserved*/{0, 0},
294	  /*count*/{0, 0, 0, 0}},
295	 {/*flags*/0,
296	  /*resource*/0x02,
297	  /*reserved*/{0, 0},
298	  /*count*/{0, 0, 0, 0}},
299	 {/*flags*/0,
300	  /*resource*/0xf1,
301	  /*reserved*/{0, 0},
302	  /*count*/{0, 0, 0, 0}},
303	 {/*flags*/0,
304	  /*resource*/0xf2,
305	  /*reserved*/{0, 0},
306	  /*count*/{0, 0, 0, 0}}
307	}
308};
309
310const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
311	/*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
312	/*subpage_code*/0x02,
313	/*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
314	/*flags*/0,
315	/*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
316	/*descr*/{}},
317	{{/*flags*/0,
318	  /*resource*/0,
319	  /*reserved*/{0, 0},
320	  /*count*/{0, 0, 0, 0}},
321	 {/*flags*/0,
322	  /*resource*/0,
323	  /*reserved*/{0, 0},
324	  /*count*/{0, 0, 0, 0}},
325	 {/*flags*/0,
326	  /*resource*/0,
327	  /*reserved*/{0, 0},
328	  /*count*/{0, 0, 0, 0}},
329	 {/*flags*/0,
330	  /*resource*/0,
331	  /*reserved*/{0, 0},
332	  /*count*/{0, 0, 0, 0}}
333	}
334};
335
336SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
337static int worker_threads = -1;
338SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
339    &worker_threads, 1, "Number of worker threads");
340static int ctl_debug = CTL_DEBUG_NONE;
341SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
342    &ctl_debug, 0, "Enabled debug flags");
343
344/*
345 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
346 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
347 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
348 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
349 */
350#define SCSI_EVPD_NUM_SUPPORTED_PAGES	10
351
352static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
353				  int param);
354static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
355static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
356static int ctl_init(void);
357void ctl_shutdown(void);
358static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
359static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
360static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
361static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
362			      struct ctl_ooa *ooa_hdr,
363			      struct ctl_ooa_entry *kern_entries);
364static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
365		     struct thread *td);
366static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
367			 struct ctl_be_lun *be_lun);
368static int ctl_free_lun(struct ctl_lun *lun);
369static void ctl_create_lun(struct ctl_be_lun *be_lun);
370static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
371
372static int ctl_do_mode_select(union ctl_io *io);
373static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
374			   uint64_t res_key, uint64_t sa_res_key,
375			   uint8_t type, uint32_t residx,
376			   struct ctl_scsiio *ctsio,
377			   struct scsi_per_res_out *cdb,
378			   struct scsi_per_res_out_parms* param);
379static void ctl_pro_preempt_other(struct ctl_lun *lun,
380				  union ctl_ha_msg *msg);
381static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
382static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
383static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
384static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
385static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
386static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
387static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
388					 int alloc_len);
389static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
390					 int alloc_len);
391static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
392static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
393static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
394static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
395static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
396static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
397    bool seq);
398static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
399static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
400    union ctl_io *pending_io, union ctl_io *ooa_io);
401static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
402				union ctl_io *starting_io);
403static int ctl_check_blocked(struct ctl_lun *lun);
404static int ctl_scsiio_lun_check(struct ctl_lun *lun,
405				const struct ctl_cmd_entry *entry,
406				struct ctl_scsiio *ctsio);
407static void ctl_failover_lun(struct ctl_lun *lun);
408static void ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua);
409static void ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua);
410static void ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua);
411static void ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua);
412static void ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
413			 ctl_ua_type ua_type);
414static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
415			       struct ctl_scsiio *ctsio);
416static int ctl_scsiio(struct ctl_scsiio *ctsio);
417
418static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
419static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
420			    ctl_ua_type ua_type);
421static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
422			 ctl_ua_type ua_type);
423static int ctl_abort_task(union ctl_io *io);
424static int ctl_abort_task_set(union ctl_io *io);
425static int ctl_i_t_nexus_reset(union ctl_io *io);
426static void ctl_run_task(union ctl_io *io);
427#ifdef CTL_IO_DELAY
428static void ctl_datamove_timer_wakeup(void *arg);
429static void ctl_done_timer_wakeup(void *arg);
430#endif /* CTL_IO_DELAY */
431
432static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
433static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
434static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
435static void ctl_datamove_remote_write(union ctl_io *io);
436static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
437static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
438static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
439static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
440				    ctl_ha_dt_cb callback);
441static void ctl_datamove_remote_read(union ctl_io *io);
442static void ctl_datamove_remote(union ctl_io *io);
443static int ctl_process_done(union ctl_io *io);
444static void ctl_lun_thread(void *arg);
445static void ctl_thresh_thread(void *arg);
446static void ctl_work_thread(void *arg);
447static void ctl_enqueue_incoming(union ctl_io *io);
448static void ctl_enqueue_rtr(union ctl_io *io);
449static void ctl_enqueue_done(union ctl_io *io);
450static void ctl_enqueue_isc(union ctl_io *io);
451static const struct ctl_cmd_entry *
452    ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
453static const struct ctl_cmd_entry *
454    ctl_validate_command(struct ctl_scsiio *ctsio);
455static int ctl_cmd_applicable(uint8_t lun_type,
456    const struct ctl_cmd_entry *entry);
457
458static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
459static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
460static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
461static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
462
463/*
464 * Load the serialization table.  This isn't very pretty, but is probably
465 * the easiest way to do it.
466 */
467#include "ctl_ser_table.c"
468
469/*
470 * We only need to define open, close and ioctl routines for this driver.
471 */
472static struct cdevsw ctl_cdevsw = {
473	.d_version =	D_VERSION,
474	.d_flags =	0,
475	.d_open =	ctl_open,
476	.d_close =	ctl_close,
477	.d_ioctl =	ctl_ioctl,
478	.d_name =	"ctl",
479};
480
481
482MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
483
484static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
485
486static moduledata_t ctl_moduledata = {
487	"ctl",
488	ctl_module_event_handler,
489	NULL
490};
491
492DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
493MODULE_VERSION(ctl, 1);
494
495static struct ctl_frontend ha_frontend =
496{
497	.name = "ha",
498};
499
500static void
501ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
502			    union ctl_ha_msg *msg_info)
503{
504	struct ctl_scsiio *ctsio;
505
506	if (msg_info->hdr.original_sc == NULL) {
507		printf("%s: original_sc == NULL!\n", __func__);
508		/* XXX KDM now what? */
509		return;
510	}
511
512	ctsio = &msg_info->hdr.original_sc->scsiio;
513	ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
514	ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
515	ctsio->io_hdr.status = msg_info->hdr.status;
516	ctsio->scsi_status = msg_info->scsi.scsi_status;
517	ctsio->sense_len = msg_info->scsi.sense_len;
518	ctsio->sense_residual = msg_info->scsi.sense_residual;
519	ctsio->residual = msg_info->scsi.residual;
520	memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
521	       msg_info->scsi.sense_len);
522	memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
523	       &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
524	ctl_enqueue_isc((union ctl_io *)ctsio);
525}
526
527static void
528ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
529				union ctl_ha_msg *msg_info)
530{
531	struct ctl_scsiio *ctsio;
532
533	if (msg_info->hdr.serializing_sc == NULL) {
534		printf("%s: serializing_sc == NULL!\n", __func__);
535		/* XXX KDM now what? */
536		return;
537	}
538
539	ctsio = &msg_info->hdr.serializing_sc->scsiio;
540	ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
541	ctl_enqueue_isc((union ctl_io *)ctsio);
542}
543
544void
545ctl_isc_announce_lun(struct ctl_lun *lun)
546{
547	struct ctl_softc *softc = lun->ctl_softc;
548	union ctl_ha_msg *msg;
549	struct ctl_ha_msg_lun_pr_key pr_key;
550	int i, k;
551
552	if (softc->ha_link != CTL_HA_LINK_ONLINE)
553		return;
554	mtx_lock(&lun->lun_lock);
555	i = sizeof(msg->lun);
556	if (lun->lun_devid)
557		i += lun->lun_devid->len;
558	i += sizeof(pr_key) * lun->pr_key_count;
559alloc:
560	mtx_unlock(&lun->lun_lock);
561	msg = malloc(i, M_CTL, M_WAITOK);
562	mtx_lock(&lun->lun_lock);
563	k = sizeof(msg->lun);
564	if (lun->lun_devid)
565		k += lun->lun_devid->len;
566	k += sizeof(pr_key) * lun->pr_key_count;
567	if (i < k) {
568		free(msg, M_CTL);
569		i = k;
570		goto alloc;
571	}
572	bzero(&msg->lun, sizeof(msg->lun));
573	msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
574	msg->hdr.nexus.targ_lun = lun->lun;
575	msg->hdr.nexus.targ_mapped_lun = lun->lun;
576	msg->lun.flags = lun->flags;
577	msg->lun.pr_generation = lun->PRGeneration;
578	msg->lun.pr_res_idx = lun->pr_res_idx;
579	msg->lun.pr_res_type = lun->res_type;
580	msg->lun.pr_key_count = lun->pr_key_count;
581	i = 0;
582	if (lun->lun_devid) {
583		msg->lun.lun_devid_len = lun->lun_devid->len;
584		memcpy(&msg->lun.data[i], lun->lun_devid->data,
585		    msg->lun.lun_devid_len);
586		i += msg->lun.lun_devid_len;
587	}
588	for (k = 0; k < CTL_MAX_INITIATORS; k++) {
589		if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
590			continue;
591		pr_key.pr_iid = k;
592		memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
593		i += sizeof(pr_key);
594	}
595	mtx_unlock(&lun->lun_lock);
596	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
597	    M_WAITOK);
598	free(msg, M_CTL);
599}
600
601void
602ctl_isc_announce_port(struct ctl_port *port)
603{
604	struct ctl_softc *softc = control_softc;
605	union ctl_ha_msg *msg;
606	int i;
607
608	if (port->targ_port < softc->port_min ||
609	    port->targ_port >= softc->port_max ||
610	    softc->ha_link != CTL_HA_LINK_ONLINE)
611		return;
612	i = sizeof(msg->port) + strlen(port->port_name) + 1;
613	if (port->lun_map)
614		i += sizeof(uint32_t) * CTL_MAX_LUNS;
615	if (port->port_devid)
616		i += port->port_devid->len;
617	if (port->target_devid)
618		i += port->target_devid->len;
619	msg = malloc(i, M_CTL, M_WAITOK);
620	bzero(&msg->port, sizeof(msg->port));
621	msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
622	msg->hdr.nexus.targ_port = port->targ_port;
623	msg->port.port_type = port->port_type;
624	msg->port.physical_port = port->physical_port;
625	msg->port.virtual_port = port->virtual_port;
626	msg->port.status = port->status;
627	i = 0;
628	msg->port.name_len = sprintf(&msg->port.data[i],
629	    "%d:%s", softc->ha_id, port->port_name) + 1;
630	i += msg->port.name_len;
631	if (port->lun_map) {
632		msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
633		memcpy(&msg->port.data[i], port->lun_map,
634		    msg->port.lun_map_len);
635		i += msg->port.lun_map_len;
636	}
637	if (port->port_devid) {
638		msg->port.port_devid_len = port->port_devid->len;
639		memcpy(&msg->port.data[i], port->port_devid->data,
640		    msg->port.port_devid_len);
641		i += msg->port.port_devid_len;
642	}
643	if (port->target_devid) {
644		msg->port.target_devid_len = port->target_devid->len;
645		memcpy(&msg->port.data[i], port->target_devid->data,
646		    msg->port.target_devid_len);
647		i += msg->port.target_devid_len;
648	}
649	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
650	    M_WAITOK);
651	free(msg, M_CTL);
652}
653
654static void
655ctl_isc_ha_link_up(struct ctl_softc *softc)
656{
657	struct ctl_port *port;
658	struct ctl_lun *lun;
659
660	STAILQ_FOREACH(port, &softc->port_list, links)
661		ctl_isc_announce_port(port);
662	STAILQ_FOREACH(lun, &softc->lun_list, links)
663		ctl_isc_announce_lun(lun);
664}
665
666static void
667ctl_isc_ha_link_down(struct ctl_softc *softc)
668{
669	struct ctl_port *port;
670	struct ctl_lun *lun;
671	union ctl_io *io;
672
673	mtx_lock(&softc->ctl_lock);
674	STAILQ_FOREACH(lun, &softc->lun_list, links) {
675		mtx_lock(&lun->lun_lock);
676		if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
677			lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
678			ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
679		}
680		mtx_unlock(&lun->lun_lock);
681
682		mtx_unlock(&softc->ctl_lock);
683		io = ctl_alloc_io(softc->othersc_pool);
684		mtx_lock(&softc->ctl_lock);
685		ctl_zero_io(io);
686		io->io_hdr.msg_type = CTL_MSG_FAILOVER;
687		io->io_hdr.nexus.targ_mapped_lun = lun->lun;
688		ctl_enqueue_isc(io);
689	}
690
691	STAILQ_FOREACH(port, &softc->port_list, links) {
692		if (port->targ_port >= softc->port_min &&
693		    port->targ_port < softc->port_max)
694			continue;
695		port->status &= ~CTL_PORT_STATUS_ONLINE;
696	}
697	mtx_unlock(&softc->ctl_lock);
698}
699
700static void
701ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
702{
703	struct ctl_lun *lun;
704	uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
705
706	mtx_lock(&softc->ctl_lock);
707	if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
708	    (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
709		mtx_lock(&lun->lun_lock);
710		mtx_unlock(&softc->ctl_lock);
711		if (msg->ua.ua_all) {
712			if (msg->ua.ua_set)
713				ctl_est_ua_all(lun, iid, msg->ua.ua_type);
714			else
715				ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
716		} else {
717			if (msg->ua.ua_set)
718				ctl_est_ua(lun, iid, msg->ua.ua_type);
719			else
720				ctl_clr_ua(lun, iid, msg->ua.ua_type);
721		}
722		mtx_unlock(&lun->lun_lock);
723	} else
724		mtx_unlock(&softc->ctl_lock);
725}
726
727static void
728ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
729{
730	struct ctl_lun *lun;
731	struct ctl_ha_msg_lun_pr_key pr_key;
732	int i, k;
733	ctl_lun_flags oflags;
734	uint32_t targ_lun;
735
736	targ_lun = msg->hdr.nexus.targ_mapped_lun;
737	mtx_lock(&softc->ctl_lock);
738	if ((targ_lun >= CTL_MAX_LUNS) ||
739	    ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
740		mtx_unlock(&softc->ctl_lock);
741		return;
742	}
743	mtx_lock(&lun->lun_lock);
744	mtx_unlock(&softc->ctl_lock);
745	if (lun->flags & CTL_LUN_DISABLED) {
746		mtx_unlock(&lun->lun_lock);
747		return;
748	}
749	i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
750	if (msg->lun.lun_devid_len != i || (i > 0 &&
751	    memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
752		mtx_unlock(&lun->lun_lock);
753		printf("%s: Received conflicting HA LUN %d\n",
754		    __func__, msg->hdr.nexus.targ_lun);
755		return;
756	} else {
757		/* Record whether peer is primary. */
758		oflags = lun->flags;
759		if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
760		    (msg->lun.flags & CTL_LUN_DISABLED) == 0)
761			lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
762		else
763			lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
764		if (oflags != lun->flags)
765			ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
766
767		/* If peer is primary and we are not -- use data */
768		if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
769		    (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
770			lun->PRGeneration = msg->lun.pr_generation;
771			lun->pr_res_idx = msg->lun.pr_res_idx;
772			lun->res_type = msg->lun.pr_res_type;
773			lun->pr_key_count = msg->lun.pr_key_count;
774			for (k = 0; k < CTL_MAX_INITIATORS; k++)
775				ctl_clr_prkey(lun, k);
776			for (k = 0; k < msg->lun.pr_key_count; k++) {
777				memcpy(&pr_key, &msg->lun.data[i],
778				    sizeof(pr_key));
779				ctl_alloc_prkey(lun, pr_key.pr_iid);
780				ctl_set_prkey(lun, pr_key.pr_iid,
781				    pr_key.pr_key);
782				i += sizeof(pr_key);
783			}
784		}
785
786		mtx_unlock(&lun->lun_lock);
787		CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
788		    __func__, msg->hdr.nexus.targ_lun,
789		    (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
790		    "primary" : "secondary"));
791
792		/* If we are primary but peer doesn't know -- notify */
793		if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
794		    (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
795			ctl_isc_announce_lun(lun);
796	}
797}
798
799static void
800ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
801{
802	struct ctl_port *port;
803	int i, new;
804
805	port = softc->ctl_ports[msg->hdr.nexus.targ_port];
806	if (port == NULL) {
807		CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
808		    msg->hdr.nexus.targ_port));
809		new = 1;
810		port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
811		port->frontend = &ha_frontend;
812		port->targ_port = msg->hdr.nexus.targ_port;
813	} else if (port->frontend == &ha_frontend) {
814		CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
815		    msg->hdr.nexus.targ_port));
816		new = 0;
817	} else {
818		printf("%s: Received conflicting HA port %d\n",
819		    __func__, msg->hdr.nexus.targ_port);
820		return;
821	}
822	port->port_type = msg->port.port_type;
823	port->physical_port = msg->port.physical_port;
824	port->virtual_port = msg->port.virtual_port;
825	port->status = msg->port.status;
826	i = 0;
827	free(port->port_name, M_CTL);
828	port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
829	    M_CTL);
830	i += msg->port.name_len;
831	if (msg->port.lun_map_len != 0) {
832		if (port->lun_map == NULL)
833			port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
834			    M_CTL, M_WAITOK);
835		memcpy(port->lun_map, &msg->port.data[i],
836		    sizeof(uint32_t) * CTL_MAX_LUNS);
837		i += msg->port.lun_map_len;
838	} else {
839		free(port->lun_map, M_CTL);
840		port->lun_map = NULL;
841	}
842	if (msg->port.port_devid_len != 0) {
843		if (port->port_devid == NULL ||
844		    port->port_devid->len != msg->port.port_devid_len) {
845			free(port->port_devid, M_CTL);
846			port->port_devid = malloc(sizeof(struct ctl_devid) +
847			    msg->port.port_devid_len, M_CTL, M_WAITOK);
848		}
849		memcpy(port->port_devid->data, &msg->port.data[i],
850		    msg->port.port_devid_len);
851		port->port_devid->len = msg->port.port_devid_len;
852		i += msg->port.port_devid_len;
853	} else {
854		free(port->port_devid, M_CTL);
855		port->port_devid = NULL;
856	}
857	if (msg->port.target_devid_len != 0) {
858		if (port->target_devid == NULL ||
859		    port->target_devid->len != msg->port.target_devid_len) {
860			free(port->target_devid, M_CTL);
861			port->target_devid = malloc(sizeof(struct ctl_devid) +
862			    msg->port.target_devid_len, M_CTL, M_WAITOK);
863		}
864		memcpy(port->target_devid->data, &msg->port.data[i],
865		    msg->port.target_devid_len);
866		port->target_devid->len = msg->port.target_devid_len;
867		i += msg->port.target_devid_len;
868	} else {
869		free(port->port_devid, M_CTL);
870		port->port_devid = NULL;
871	}
872	if (new) {
873		if (ctl_port_register(port) != 0) {
874			printf("%s: ctl_port_register() failed with error\n",
875			    __func__);
876		}
877	}
878}
879
880/*
881 * ISC (Inter Shelf Communication) event handler.  Events from the HA
882 * subsystem come in here.
883 */
884static void
885ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
886{
887	struct ctl_softc *softc;
888	union ctl_io *io;
889	struct ctl_prio *presio;
890	ctl_ha_status isc_status;
891
892	softc = control_softc;
893	CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
894	if (event == CTL_HA_EVT_MSG_RECV) {
895		union ctl_ha_msg *msg, msgbuf;
896
897		if (param > sizeof(msgbuf))
898			msg = malloc(param, M_CTL, M_WAITOK);
899		else
900			msg = &msgbuf;
901		isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
902		    M_WAITOK);
903		if (isc_status != CTL_HA_STATUS_SUCCESS) {
904			printf("%s: Error receiving message: %d\n",
905			    __func__, isc_status);
906			if (msg != &msgbuf)
907				free(msg, M_CTL);
908			return;
909		}
910
911		CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
912		switch (msg->hdr.msg_type) {
913		case CTL_MSG_SERIALIZE:
914			io = ctl_alloc_io(softc->othersc_pool);
915			ctl_zero_io(io);
916			// populate ctsio from msg
917			io->io_hdr.io_type = CTL_IO_SCSI;
918			io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
919			io->io_hdr.original_sc = msg->hdr.original_sc;
920			io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
921					    CTL_FLAG_IO_ACTIVE;
922			/*
923			 * If we're in serialization-only mode, we don't
924			 * want to go through full done processing.  Thus
925			 * the COPY flag.
926			 *
927			 * XXX KDM add another flag that is more specific.
928			 */
929			if (softc->ha_mode != CTL_HA_MODE_XFER)
930				io->io_hdr.flags |= CTL_FLAG_INT_COPY;
931			io->io_hdr.nexus = msg->hdr.nexus;
932#if 0
933			printf("port %u, iid %u, lun %u\n",
934			       io->io_hdr.nexus.targ_port,
935			       io->io_hdr.nexus.initid,
936			       io->io_hdr.nexus.targ_lun);
937#endif
938			io->scsiio.tag_num = msg->scsi.tag_num;
939			io->scsiio.tag_type = msg->scsi.tag_type;
940#ifdef CTL_TIME_IO
941			io->io_hdr.start_time = time_uptime;
942			getbintime(&io->io_hdr.start_bt);
943#endif /* CTL_TIME_IO */
944			io->scsiio.cdb_len = msg->scsi.cdb_len;
945			memcpy(io->scsiio.cdb, msg->scsi.cdb,
946			       CTL_MAX_CDBLEN);
947			if (softc->ha_mode == CTL_HA_MODE_XFER) {
948				const struct ctl_cmd_entry *entry;
949
950				entry = ctl_get_cmd_entry(&io->scsiio, NULL);
951				io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
952				io->io_hdr.flags |=
953					entry->flags & CTL_FLAG_DATA_MASK;
954			}
955			ctl_enqueue_isc(io);
956			break;
957
958		/* Performed on the Originating SC, XFER mode only */
959		case CTL_MSG_DATAMOVE: {
960			struct ctl_sg_entry *sgl;
961			int i, j;
962
963			io = msg->hdr.original_sc;
964			if (io == NULL) {
965				printf("%s: original_sc == NULL!\n", __func__);
966				/* XXX KDM do something here */
967				break;
968			}
969			io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
970			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
971			/*
972			 * Keep track of this, we need to send it back over
973			 * when the datamove is complete.
974			 */
975			io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
976
977			if (msg->dt.sg_sequence == 0) {
978				i = msg->dt.kern_sg_entries +
979				    io->scsiio.kern_data_len /
980				    CTL_HA_DATAMOVE_SEGMENT + 1;
981				sgl = malloc(sizeof(*sgl) * i, M_CTL,
982				    M_WAITOK | M_ZERO);
983				io->io_hdr.remote_sglist = sgl;
984				io->io_hdr.local_sglist =
985				    &sgl[msg->dt.kern_sg_entries];
986
987				io->scsiio.kern_data_ptr = (uint8_t *)sgl;
988
989				io->scsiio.kern_sg_entries =
990					msg->dt.kern_sg_entries;
991				io->scsiio.rem_sg_entries =
992					msg->dt.kern_sg_entries;
993				io->scsiio.kern_data_len =
994					msg->dt.kern_data_len;
995				io->scsiio.kern_total_len =
996					msg->dt.kern_total_len;
997				io->scsiio.kern_data_resid =
998					msg->dt.kern_data_resid;
999				io->scsiio.kern_rel_offset =
1000					msg->dt.kern_rel_offset;
1001				io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1002				io->io_hdr.flags |= msg->dt.flags &
1003				    CTL_FLAG_BUS_ADDR;
1004			} else
1005				sgl = (struct ctl_sg_entry *)
1006					io->scsiio.kern_data_ptr;
1007
1008			for (i = msg->dt.sent_sg_entries, j = 0;
1009			     i < (msg->dt.sent_sg_entries +
1010			     msg->dt.cur_sg_entries); i++, j++) {
1011				sgl[i].addr = msg->dt.sg_list[j].addr;
1012				sgl[i].len = msg->dt.sg_list[j].len;
1013
1014#if 0
1015				printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
1016				       __func__,
1017				       msg->dt.sg_list[j].addr,
1018				       msg->dt.sg_list[j].len,
1019				       sgl[i].addr, sgl[i].len, j, i);
1020#endif
1021			}
1022
1023			/*
1024			 * If this is the last piece of the I/O, we've got
1025			 * the full S/G list.  Queue processing in the thread.
1026			 * Otherwise wait for the next piece.
1027			 */
1028			if (msg->dt.sg_last != 0)
1029				ctl_enqueue_isc(io);
1030			break;
1031		}
1032		/* Performed on the Serializing (primary) SC, XFER mode only */
1033		case CTL_MSG_DATAMOVE_DONE: {
1034			if (msg->hdr.serializing_sc == NULL) {
1035				printf("%s: serializing_sc == NULL!\n",
1036				       __func__);
1037				/* XXX KDM now what? */
1038				break;
1039			}
1040			/*
1041			 * We grab the sense information here in case
1042			 * there was a failure, so we can return status
1043			 * back to the initiator.
1044			 */
1045			io = msg->hdr.serializing_sc;
1046			io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1047			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1048			io->io_hdr.port_status = msg->scsi.fetd_status;
1049			io->scsiio.residual = msg->scsi.residual;
1050			if (msg->hdr.status != CTL_STATUS_NONE) {
1051				io->io_hdr.status = msg->hdr.status;
1052				io->scsiio.scsi_status = msg->scsi.scsi_status;
1053				io->scsiio.sense_len = msg->scsi.sense_len;
1054				io->scsiio.sense_residual =msg->scsi.sense_residual;
1055				memcpy(&io->scsiio.sense_data,
1056				    &msg->scsi.sense_data,
1057				    msg->scsi.sense_len);
1058			}
1059			ctl_enqueue_isc(io);
1060			break;
1061		}
1062
1063		/* Preformed on Originating SC, SER_ONLY mode */
1064		case CTL_MSG_R2R:
1065			io = msg->hdr.original_sc;
1066			if (io == NULL) {
1067				printf("%s: original_sc == NULL!\n",
1068				    __func__);
1069				break;
1070			}
1071			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1072			io->io_hdr.msg_type = CTL_MSG_R2R;
1073			io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1074			ctl_enqueue_isc(io);
1075			break;
1076
1077		/*
1078		 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1079		 * mode.
1080		 * Performed on the Originating (i.e. secondary) SC in XFER
1081		 * mode
1082		 */
1083		case CTL_MSG_FINISH_IO:
1084			if (softc->ha_mode == CTL_HA_MODE_XFER)
1085				ctl_isc_handler_finish_xfer(softc, msg);
1086			else
1087				ctl_isc_handler_finish_ser_only(softc, msg);
1088			break;
1089
1090		/* Preformed on Originating SC */
1091		case CTL_MSG_BAD_JUJU:
1092			io = msg->hdr.original_sc;
1093			if (io == NULL) {
1094				printf("%s: Bad JUJU!, original_sc is NULL!\n",
1095				       __func__);
1096				break;
1097			}
1098			ctl_copy_sense_data(msg, io);
1099			/*
1100			 * IO should have already been cleaned up on other
1101			 * SC so clear this flag so we won't send a message
1102			 * back to finish the IO there.
1103			 */
1104			io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1105			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1106
1107			/* io = msg->hdr.serializing_sc; */
1108			io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1109			ctl_enqueue_isc(io);
1110			break;
1111
1112		/* Handle resets sent from the other side */
1113		case CTL_MSG_MANAGE_TASKS: {
1114			struct ctl_taskio *taskio;
1115			taskio = (struct ctl_taskio *)ctl_alloc_io(
1116			    softc->othersc_pool);
1117			ctl_zero_io((union ctl_io *)taskio);
1118			taskio->io_hdr.io_type = CTL_IO_TASK;
1119			taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1120			taskio->io_hdr.nexus = msg->hdr.nexus;
1121			taskio->task_action = msg->task.task_action;
1122			taskio->tag_num = msg->task.tag_num;
1123			taskio->tag_type = msg->task.tag_type;
1124#ifdef CTL_TIME_IO
1125			taskio->io_hdr.start_time = time_uptime;
1126			getbintime(&taskio->io_hdr.start_bt);
1127#endif /* CTL_TIME_IO */
1128			ctl_run_task((union ctl_io *)taskio);
1129			break;
1130		}
1131		/* Persistent Reserve action which needs attention */
1132		case CTL_MSG_PERS_ACTION:
1133			presio = (struct ctl_prio *)ctl_alloc_io(
1134			    softc->othersc_pool);
1135			ctl_zero_io((union ctl_io *)presio);
1136			presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1137			presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1138			presio->io_hdr.nexus = msg->hdr.nexus;
1139			presio->pr_msg = msg->pr;
1140			ctl_enqueue_isc((union ctl_io *)presio);
1141			break;
1142		case CTL_MSG_UA:
1143			ctl_isc_ua(softc, msg, param);
1144			break;
1145		case CTL_MSG_PORT_SYNC:
1146			ctl_isc_port_sync(softc, msg, param);
1147			break;
1148		case CTL_MSG_LUN_SYNC:
1149			ctl_isc_lun_sync(softc, msg, param);
1150			break;
1151		default:
1152			printf("Received HA message of unknown type %d\n",
1153			    msg->hdr.msg_type);
1154			break;
1155		}
1156		if (msg != &msgbuf)
1157			free(msg, M_CTL);
1158	} else if (event == CTL_HA_EVT_LINK_CHANGE) {
1159		printf("CTL: HA link status changed from %d to %d\n",
1160		    softc->ha_link, param);
1161		if (param == softc->ha_link)
1162			return;
1163		if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1164			softc->ha_link = param;
1165			ctl_isc_ha_link_down(softc);
1166		} else {
1167			softc->ha_link = param;
1168			if (softc->ha_link == CTL_HA_LINK_ONLINE)
1169				ctl_isc_ha_link_up(softc);
1170		}
1171		return;
1172	} else {
1173		printf("ctl_isc_event_handler: Unknown event %d\n", event);
1174		return;
1175	}
1176}
1177
1178static void
1179ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1180{
1181
1182	memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1183	    src->scsi.sense_len);
1184	dest->scsiio.scsi_status = src->scsi.scsi_status;
1185	dest->scsiio.sense_len = src->scsi.sense_len;
1186	dest->io_hdr.status = src->hdr.status;
1187}
1188
1189static void
1190ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1191{
1192
1193	memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1194	    src->scsiio.sense_len);
1195	dest->scsi.scsi_status = src->scsiio.scsi_status;
1196	dest->scsi.sense_len = src->scsiio.sense_len;
1197	dest->hdr.status = src->io_hdr.status;
1198}
1199
1200static void
1201ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1202{
1203	struct ctl_softc *softc = lun->ctl_softc;
1204	ctl_ua_type *pu;
1205
1206	if (initidx < softc->init_min || initidx >= softc->init_max)
1207		return;
1208	mtx_assert(&lun->lun_lock, MA_OWNED);
1209	pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1210	if (pu == NULL)
1211		return;
1212	pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1213}
1214
1215static void
1216ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1217{
1218	struct ctl_softc *softc = lun->ctl_softc;
1219	int i, j;
1220
1221	mtx_assert(&lun->lun_lock, MA_OWNED);
1222	for (i = softc->port_min; i < softc->port_max; i++) {
1223		if (lun->pending_ua[i] == NULL)
1224			continue;
1225		for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1226			if (i * CTL_MAX_INIT_PER_PORT + j == except)
1227				continue;
1228			lun->pending_ua[i][j] |= ua;
1229		}
1230	}
1231}
1232
1233static void
1234ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1235{
1236	struct ctl_softc *softc = lun->ctl_softc;
1237	ctl_ua_type *pu;
1238
1239	if (initidx < softc->init_min || initidx >= softc->init_max)
1240		return;
1241	mtx_assert(&lun->lun_lock, MA_OWNED);
1242	pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1243	if (pu == NULL)
1244		return;
1245	pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1246}
1247
1248static void
1249ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1250{
1251	struct ctl_softc *softc = lun->ctl_softc;
1252	int i, j;
1253
1254	mtx_assert(&lun->lun_lock, MA_OWNED);
1255	for (i = softc->port_min; i < softc->port_max; i++) {
1256		if (lun->pending_ua[i] == NULL)
1257			continue;
1258		for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1259			if (i * CTL_MAX_INIT_PER_PORT + j == except)
1260				continue;
1261			lun->pending_ua[i][j] &= ~ua;
1262		}
1263	}
1264}
1265
1266static void
1267ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1268    ctl_ua_type ua_type)
1269{
1270	struct ctl_lun *lun;
1271
1272	mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1273	STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1274		mtx_lock(&lun->lun_lock);
1275		ctl_clr_ua(lun, initidx, ua_type);
1276		mtx_unlock(&lun->lun_lock);
1277	}
1278}
1279
1280static int
1281ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1282{
1283	struct ctl_softc *softc = (struct ctl_softc *)arg1;
1284	struct ctl_lun *lun;
1285	struct ctl_lun_req ireq;
1286	int error, value;
1287
1288	value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1289	error = sysctl_handle_int(oidp, &value, 0, req);
1290	if ((error != 0) || (req->newptr == NULL))
1291		return (error);
1292
1293	mtx_lock(&softc->ctl_lock);
1294	if (value == 0)
1295		softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1296	else
1297		softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1298	STAILQ_FOREACH(lun, &softc->lun_list, links) {
1299		mtx_unlock(&softc->ctl_lock);
1300		bzero(&ireq, sizeof(ireq));
1301		ireq.reqtype = CTL_LUNREQ_MODIFY;
1302		ireq.reqdata.modify.lun_id = lun->lun;
1303		lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1304		    curthread);
1305		if (ireq.status != CTL_LUN_OK) {
1306			printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1307			    __func__, ireq.status, ireq.error_str);
1308		}
1309		mtx_lock(&softc->ctl_lock);
1310	}
1311	mtx_unlock(&softc->ctl_lock);
1312	return (0);
1313}
1314
1315static int
1316ctl_init(void)
1317{
1318	struct ctl_softc *softc;
1319	void *other_pool;
1320	int i, error, retval;
1321
1322	retval = 0;
1323	control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1324			       M_WAITOK | M_ZERO);
1325	softc = control_softc;
1326
1327	softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1328			      "cam/ctl");
1329
1330	softc->dev->si_drv1 = softc;
1331
1332	sysctl_ctx_init(&softc->sysctl_ctx);
1333	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1334		SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1335		CTLFLAG_RD, 0, "CAM Target Layer");
1336
1337	if (softc->sysctl_tree == NULL) {
1338		printf("%s: unable to allocate sysctl tree\n", __func__);
1339		destroy_dev(softc->dev);
1340		free(control_softc, M_DEVBUF);
1341		control_softc = NULL;
1342		return (ENOMEM);
1343	}
1344
1345	mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1346	softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1347	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1348	softc->open_count = 0;
1349
1350	/*
1351	 * Default to actually sending a SYNCHRONIZE CACHE command down to
1352	 * the drive.
1353	 */
1354	softc->flags = CTL_FLAG_REAL_SYNC;
1355
1356	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1357	    OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1358	    "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1359
1360	/*
1361	 * In Copan's HA scheme, the "master" and "slave" roles are
1362	 * figured out through the slot the controller is in.  Although it
1363	 * is an active/active system, someone has to be in charge.
1364	 */
1365	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1366	    OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1367	    "HA head ID (0 - no HA)");
1368	if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1369		softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1370		softc->is_single = 1;
1371		softc->port_cnt = CTL_MAX_PORTS;
1372		softc->port_min = 0;
1373	} else {
1374		softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1375		softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1376	}
1377	softc->port_max = softc->port_min + softc->port_cnt;
1378	softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1379	softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1380
1381	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1382	    OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1383	    "HA link state (0 - offline, 1 - unknown, 2 - online)");
1384
1385	STAILQ_INIT(&softc->lun_list);
1386	STAILQ_INIT(&softc->pending_lun_queue);
1387	STAILQ_INIT(&softc->fe_list);
1388	STAILQ_INIT(&softc->port_list);
1389	STAILQ_INIT(&softc->be_list);
1390	ctl_tpc_init(softc);
1391
1392	if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1393	                    &other_pool) != 0)
1394	{
1395		printf("ctl: can't allocate %d entry other SC pool, "
1396		       "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1397		return (ENOMEM);
1398	}
1399	softc->othersc_pool = other_pool;
1400
1401	if (worker_threads <= 0)
1402		worker_threads = max(1, mp_ncpus / 4);
1403	if (worker_threads > CTL_MAX_THREADS)
1404		worker_threads = CTL_MAX_THREADS;
1405
1406	for (i = 0; i < worker_threads; i++) {
1407		struct ctl_thread *thr = &softc->threads[i];
1408
1409		mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1410		thr->ctl_softc = softc;
1411		STAILQ_INIT(&thr->incoming_queue);
1412		STAILQ_INIT(&thr->rtr_queue);
1413		STAILQ_INIT(&thr->done_queue);
1414		STAILQ_INIT(&thr->isc_queue);
1415
1416		error = kproc_kthread_add(ctl_work_thread, thr,
1417		    &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1418		if (error != 0) {
1419			printf("error creating CTL work thread!\n");
1420			ctl_pool_free(other_pool);
1421			return (error);
1422		}
1423	}
1424	error = kproc_kthread_add(ctl_lun_thread, softc,
1425	    &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1426	if (error != 0) {
1427		printf("error creating CTL lun thread!\n");
1428		ctl_pool_free(other_pool);
1429		return (error);
1430	}
1431	error = kproc_kthread_add(ctl_thresh_thread, softc,
1432	    &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1433	if (error != 0) {
1434		printf("error creating CTL threshold thread!\n");
1435		ctl_pool_free(other_pool);
1436		return (error);
1437	}
1438
1439	SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1440	    OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1441	    softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1442
1443	if (softc->is_single == 0) {
1444		ctl_frontend_register(&ha_frontend);
1445		if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1446			printf("ctl_init: ctl_ha_msg_init failed.\n");
1447			softc->is_single = 1;
1448		} else
1449		if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1450		    != CTL_HA_STATUS_SUCCESS) {
1451			printf("ctl_init: ctl_ha_msg_register failed.\n");
1452			softc->is_single = 1;
1453		}
1454	}
1455	return (0);
1456}
1457
1458void
1459ctl_shutdown(void)
1460{
1461	struct ctl_softc *softc;
1462	struct ctl_lun *lun, *next_lun;
1463
1464	softc = (struct ctl_softc *)control_softc;
1465
1466	if (softc->is_single == 0) {
1467		if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1468		    != CTL_HA_STATUS_SUCCESS) {
1469			printf("ctl_shutdown: ctl_ha_msg_deregister failed.\n");
1470		}
1471		if (ctl_ha_msg_shutdown(softc) != CTL_HA_STATUS_SUCCESS) {
1472			printf("ctl_shutdown: ctl_ha_msg_shutdown failed.\n");
1473		}
1474		ctl_frontend_deregister(&ha_frontend);
1475	}
1476
1477	mtx_lock(&softc->ctl_lock);
1478
1479	/*
1480	 * Free up each LUN.
1481	 */
1482	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1483		next_lun = STAILQ_NEXT(lun, links);
1484		ctl_free_lun(lun);
1485	}
1486
1487	mtx_unlock(&softc->ctl_lock);
1488
1489#if 0
1490	ctl_shutdown_thread(softc->work_thread);
1491	mtx_destroy(&softc->queue_lock);
1492#endif
1493
1494	ctl_tpc_shutdown(softc);
1495	uma_zdestroy(softc->io_zone);
1496	mtx_destroy(&softc->ctl_lock);
1497
1498	destroy_dev(softc->dev);
1499
1500	sysctl_ctx_free(&softc->sysctl_ctx);
1501
1502	free(control_softc, M_DEVBUF);
1503	control_softc = NULL;
1504}
1505
1506static int
1507ctl_module_event_handler(module_t mod, int what, void *arg)
1508{
1509
1510	switch (what) {
1511	case MOD_LOAD:
1512		return (ctl_init());
1513	case MOD_UNLOAD:
1514		return (EBUSY);
1515	default:
1516		return (EOPNOTSUPP);
1517	}
1518}
1519
1520/*
1521 * XXX KDM should we do some access checks here?  Bump a reference count to
1522 * prevent a CTL module from being unloaded while someone has it open?
1523 */
1524static int
1525ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1526{
1527	return (0);
1528}
1529
1530static int
1531ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1532{
1533	return (0);
1534}
1535
1536/*
1537 * Remove an initiator by port number and initiator ID.
1538 * Returns 0 for success, -1 for failure.
1539 */
1540int
1541ctl_remove_initiator(struct ctl_port *port, int iid)
1542{
1543	struct ctl_softc *softc = control_softc;
1544
1545	mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1546
1547	if (iid > CTL_MAX_INIT_PER_PORT) {
1548		printf("%s: initiator ID %u > maximun %u!\n",
1549		       __func__, iid, CTL_MAX_INIT_PER_PORT);
1550		return (-1);
1551	}
1552
1553	mtx_lock(&softc->ctl_lock);
1554	port->wwpn_iid[iid].in_use--;
1555	port->wwpn_iid[iid].last_use = time_uptime;
1556	mtx_unlock(&softc->ctl_lock);
1557
1558	return (0);
1559}
1560
1561/*
1562 * Add an initiator to the initiator map.
1563 * Returns iid for success, < 0 for failure.
1564 */
1565int
1566ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1567{
1568	struct ctl_softc *softc = control_softc;
1569	time_t best_time;
1570	int i, best;
1571
1572	mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1573
1574	if (iid >= CTL_MAX_INIT_PER_PORT) {
1575		printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1576		       __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1577		free(name, M_CTL);
1578		return (-1);
1579	}
1580
1581	mtx_lock(&softc->ctl_lock);
1582
1583	if (iid < 0 && (wwpn != 0 || name != NULL)) {
1584		for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1585			if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1586				iid = i;
1587				break;
1588			}
1589			if (name != NULL && port->wwpn_iid[i].name != NULL &&
1590			    strcmp(name, port->wwpn_iid[i].name) == 0) {
1591				iid = i;
1592				break;
1593			}
1594		}
1595	}
1596
1597	if (iid < 0) {
1598		for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1599			if (port->wwpn_iid[i].in_use == 0 &&
1600			    port->wwpn_iid[i].wwpn == 0 &&
1601			    port->wwpn_iid[i].name == NULL) {
1602				iid = i;
1603				break;
1604			}
1605		}
1606	}
1607
1608	if (iid < 0) {
1609		best = -1;
1610		best_time = INT32_MAX;
1611		for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1612			if (port->wwpn_iid[i].in_use == 0) {
1613				if (port->wwpn_iid[i].last_use < best_time) {
1614					best = i;
1615					best_time = port->wwpn_iid[i].last_use;
1616				}
1617			}
1618		}
1619		iid = best;
1620	}
1621
1622	if (iid < 0) {
1623		mtx_unlock(&softc->ctl_lock);
1624		free(name, M_CTL);
1625		return (-2);
1626	}
1627
1628	if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1629		/*
1630		 * This is not an error yet.
1631		 */
1632		if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1633#if 0
1634			printf("%s: port %d iid %u WWPN %#jx arrived"
1635			    " again\n", __func__, port->targ_port,
1636			    iid, (uintmax_t)wwpn);
1637#endif
1638			goto take;
1639		}
1640		if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1641		    strcmp(name, port->wwpn_iid[iid].name) == 0) {
1642#if 0
1643			printf("%s: port %d iid %u name '%s' arrived"
1644			    " again\n", __func__, port->targ_port,
1645			    iid, name);
1646#endif
1647			goto take;
1648		}
1649
1650		/*
1651		 * This is an error, but what do we do about it?  The
1652		 * driver is telling us we have a new WWPN for this
1653		 * initiator ID, so we pretty much need to use it.
1654		 */
1655		printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1656		    " but WWPN %#jx '%s' is still at that address\n",
1657		    __func__, port->targ_port, iid, wwpn, name,
1658		    (uintmax_t)port->wwpn_iid[iid].wwpn,
1659		    port->wwpn_iid[iid].name);
1660
1661		/*
1662		 * XXX KDM clear have_ca and ua_pending on each LUN for
1663		 * this initiator.
1664		 */
1665	}
1666take:
1667	free(port->wwpn_iid[iid].name, M_CTL);
1668	port->wwpn_iid[iid].name = name;
1669	port->wwpn_iid[iid].wwpn = wwpn;
1670	port->wwpn_iid[iid].in_use++;
1671	mtx_unlock(&softc->ctl_lock);
1672
1673	return (iid);
1674}
1675
1676static int
1677ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1678{
1679	int len;
1680
1681	switch (port->port_type) {
1682	case CTL_PORT_FC:
1683	{
1684		struct scsi_transportid_fcp *id =
1685		    (struct scsi_transportid_fcp *)buf;
1686		if (port->wwpn_iid[iid].wwpn == 0)
1687			return (0);
1688		memset(id, 0, sizeof(*id));
1689		id->format_protocol = SCSI_PROTO_FC;
1690		scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1691		return (sizeof(*id));
1692	}
1693	case CTL_PORT_ISCSI:
1694	{
1695		struct scsi_transportid_iscsi_port *id =
1696		    (struct scsi_transportid_iscsi_port *)buf;
1697		if (port->wwpn_iid[iid].name == NULL)
1698			return (0);
1699		memset(id, 0, 256);
1700		id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1701		    SCSI_PROTO_ISCSI;
1702		len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1703		len = roundup2(min(len, 252), 4);
1704		scsi_ulto2b(len, id->additional_length);
1705		return (sizeof(*id) + len);
1706	}
1707	case CTL_PORT_SAS:
1708	{
1709		struct scsi_transportid_sas *id =
1710		    (struct scsi_transportid_sas *)buf;
1711		if (port->wwpn_iid[iid].wwpn == 0)
1712			return (0);
1713		memset(id, 0, sizeof(*id));
1714		id->format_protocol = SCSI_PROTO_SAS;
1715		scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1716		return (sizeof(*id));
1717	}
1718	default:
1719	{
1720		struct scsi_transportid_spi *id =
1721		    (struct scsi_transportid_spi *)buf;
1722		memset(id, 0, sizeof(*id));
1723		id->format_protocol = SCSI_PROTO_SPI;
1724		scsi_ulto2b(iid, id->scsi_addr);
1725		scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1726		return (sizeof(*id));
1727	}
1728	}
1729}
1730
1731/*
1732 * Serialize a command that went down the "wrong" side, and so was sent to
1733 * this controller for execution.  The logic is a little different than the
1734 * standard case in ctl_scsiio_precheck().  Errors in this case need to get
1735 * sent back to the other side, but in the success case, we execute the
1736 * command on this side (XFER mode) or tell the other side to execute it
1737 * (SER_ONLY mode).
1738 */
1739static int
1740ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1741{
1742	struct ctl_softc *softc;
1743	union ctl_ha_msg msg_info;
1744	struct ctl_lun *lun;
1745	const struct ctl_cmd_entry *entry;
1746	int retval = 0;
1747	uint32_t targ_lun;
1748
1749	softc = control_softc;
1750
1751	targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1752	mtx_lock(&softc->ctl_lock);
1753	if ((targ_lun < CTL_MAX_LUNS) &&
1754	    ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
1755		mtx_lock(&lun->lun_lock);
1756		mtx_unlock(&softc->ctl_lock);
1757		/*
1758		 * If the LUN is invalid, pretend that it doesn't exist.
1759		 * It will go away as soon as all pending I/O has been
1760		 * completed.
1761		 */
1762		if (lun->flags & CTL_LUN_DISABLED) {
1763			mtx_unlock(&lun->lun_lock);
1764			lun = NULL;
1765		}
1766	} else {
1767		mtx_unlock(&softc->ctl_lock);
1768		lun = NULL;
1769	}
1770	if (lun == NULL) {
1771		/*
1772		 * The other node would not send this request to us unless
1773		 * received announce that we are primary node for this LUN.
1774		 * If this LUN does not exist now, it is probably result of
1775		 * a race, so respond to initiator in the most opaque way.
1776		 */
1777		ctl_set_busy(ctsio);
1778		ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1779		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1780		msg_info.hdr.serializing_sc = NULL;
1781		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1782		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1783		    sizeof(msg_info.scsi), M_WAITOK);
1784		return(1);
1785	}
1786
1787	entry = ctl_get_cmd_entry(ctsio, NULL);
1788	if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
1789		mtx_unlock(&lun->lun_lock);
1790		ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1791		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1792		msg_info.hdr.serializing_sc = NULL;
1793		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1794		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1795		    sizeof(msg_info.scsi), M_WAITOK);
1796		return(1);
1797	}
1798
1799	ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
1800	ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
1801
1802	/*
1803	 * Every I/O goes into the OOA queue for a
1804	 * particular LUN, and stays there until completion.
1805	 */
1806#ifdef CTL_TIME_IO
1807	if (TAILQ_EMPTY(&lun->ooa_queue))
1808		lun->idle_time += getsbinuptime() - lun->last_busy;
1809#endif
1810	TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1811
1812	switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1813		(union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1814		 ooa_links))) {
1815	case CTL_ACTION_BLOCK:
1816		ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1817		TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1818				  blocked_links);
1819		mtx_unlock(&lun->lun_lock);
1820		break;
1821	case CTL_ACTION_PASS:
1822	case CTL_ACTION_SKIP:
1823		if (softc->ha_mode == CTL_HA_MODE_XFER) {
1824			ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1825			ctl_enqueue_rtr((union ctl_io *)ctsio);
1826			mtx_unlock(&lun->lun_lock);
1827		} else {
1828			ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
1829			mtx_unlock(&lun->lun_lock);
1830
1831			/* send msg back to other side */
1832			msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1833			msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1834			msg_info.hdr.msg_type = CTL_MSG_R2R;
1835			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1836			    sizeof(msg_info.hdr), M_WAITOK);
1837		}
1838		break;
1839	case CTL_ACTION_OVERLAP:
1840		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1841		mtx_unlock(&lun->lun_lock);
1842		retval = 1;
1843
1844		ctl_set_overlapped_cmd(ctsio);
1845		ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1846		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1847		msg_info.hdr.serializing_sc = NULL;
1848		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1849		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1850		    sizeof(msg_info.scsi), M_WAITOK);
1851		break;
1852	case CTL_ACTION_OVERLAP_TAG:
1853		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1854		mtx_unlock(&lun->lun_lock);
1855		retval = 1;
1856		ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
1857		ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1858		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1859		msg_info.hdr.serializing_sc = NULL;
1860		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1861		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1862		    sizeof(msg_info.scsi), M_WAITOK);
1863		break;
1864	case CTL_ACTION_ERROR:
1865	default:
1866		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1867		mtx_unlock(&lun->lun_lock);
1868		retval = 1;
1869
1870		ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
1871					 /*retry_count*/ 0);
1872		ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1873		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1874		msg_info.hdr.serializing_sc = NULL;
1875		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1876		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1877		    sizeof(msg_info.scsi), M_WAITOK);
1878		break;
1879	}
1880	return (retval);
1881}
1882
1883/*
1884 * Returns 0 for success, errno for failure.
1885 */
1886static int
1887ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1888		   struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1889{
1890	union ctl_io *io;
1891	int retval;
1892
1893	retval = 0;
1894
1895	mtx_lock(&lun->lun_lock);
1896	for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1897	     (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1898	     ooa_links)) {
1899		struct ctl_ooa_entry *entry;
1900
1901		/*
1902		 * If we've got more than we can fit, just count the
1903		 * remaining entries.
1904		 */
1905		if (*cur_fill_num >= ooa_hdr->alloc_num)
1906			continue;
1907
1908		entry = &kern_entries[*cur_fill_num];
1909
1910		entry->tag_num = io->scsiio.tag_num;
1911		entry->lun_num = lun->lun;
1912#ifdef CTL_TIME_IO
1913		entry->start_bt = io->io_hdr.start_bt;
1914#endif
1915		bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
1916		entry->cdb_len = io->scsiio.cdb_len;
1917		if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1918			entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1919
1920		if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1921			entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
1922
1923		if (io->io_hdr.flags & CTL_FLAG_ABORT)
1924			entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1925
1926		if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1927			entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
1928
1929		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1930			entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
1931	}
1932	mtx_unlock(&lun->lun_lock);
1933
1934	return (retval);
1935}
1936
1937static void *
1938ctl_copyin_alloc(void *user_addr, int len, char *error_str,
1939		 size_t error_str_len)
1940{
1941	void *kptr;
1942
1943	kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
1944
1945	if (copyin(user_addr, kptr, len) != 0) {
1946		snprintf(error_str, error_str_len, "Error copying %d bytes "
1947			 "from user address %p to kernel address %p", len,
1948			 user_addr, kptr);
1949		free(kptr, M_CTL);
1950		return (NULL);
1951	}
1952
1953	return (kptr);
1954}
1955
1956static void
1957ctl_free_args(int num_args, struct ctl_be_arg *args)
1958{
1959	int i;
1960
1961	if (args == NULL)
1962		return;
1963
1964	for (i = 0; i < num_args; i++) {
1965		free(args[i].kname, M_CTL);
1966		free(args[i].kvalue, M_CTL);
1967	}
1968
1969	free(args, M_CTL);
1970}
1971
1972static struct ctl_be_arg *
1973ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
1974		char *error_str, size_t error_str_len)
1975{
1976	struct ctl_be_arg *args;
1977	int i;
1978
1979	args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
1980				error_str, error_str_len);
1981
1982	if (args == NULL)
1983		goto bailout;
1984
1985	for (i = 0; i < num_args; i++) {
1986		args[i].kname = NULL;
1987		args[i].kvalue = NULL;
1988	}
1989
1990	for (i = 0; i < num_args; i++) {
1991		uint8_t *tmpptr;
1992
1993		args[i].kname = ctl_copyin_alloc(args[i].name,
1994			args[i].namelen, error_str, error_str_len);
1995		if (args[i].kname == NULL)
1996			goto bailout;
1997
1998		if (args[i].kname[args[i].namelen - 1] != '\0') {
1999			snprintf(error_str, error_str_len, "Argument %d "
2000				 "name is not NUL-terminated", i);
2001			goto bailout;
2002		}
2003
2004		if (args[i].flags & CTL_BEARG_RD) {
2005			tmpptr = ctl_copyin_alloc(args[i].value,
2006				args[i].vallen, error_str, error_str_len);
2007			if (tmpptr == NULL)
2008				goto bailout;
2009			if ((args[i].flags & CTL_BEARG_ASCII)
2010			 && (tmpptr[args[i].vallen - 1] != '\0')) {
2011				snprintf(error_str, error_str_len, "Argument "
2012				    "%d value is not NUL-terminated", i);
2013				goto bailout;
2014			}
2015			args[i].kvalue = tmpptr;
2016		} else {
2017			args[i].kvalue = malloc(args[i].vallen,
2018			    M_CTL, M_WAITOK | M_ZERO);
2019		}
2020	}
2021
2022	return (args);
2023bailout:
2024
2025	ctl_free_args(num_args, args);
2026
2027	return (NULL);
2028}
2029
2030static void
2031ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2032{
2033	int i;
2034
2035	for (i = 0; i < num_args; i++) {
2036		if (args[i].flags & CTL_BEARG_WR)
2037			copyout(args[i].kvalue, args[i].value, args[i].vallen);
2038	}
2039}
2040
2041/*
2042 * Escape characters that are illegal or not recommended in XML.
2043 */
2044int
2045ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2046{
2047	char *end = str + size;
2048	int retval;
2049
2050	retval = 0;
2051
2052	for (; *str && str < end; str++) {
2053		switch (*str) {
2054		case '&':
2055			retval = sbuf_printf(sb, "&amp;");
2056			break;
2057		case '>':
2058			retval = sbuf_printf(sb, "&gt;");
2059			break;
2060		case '<':
2061			retval = sbuf_printf(sb, "&lt;");
2062			break;
2063		default:
2064			retval = sbuf_putc(sb, *str);
2065			break;
2066		}
2067
2068		if (retval != 0)
2069			break;
2070
2071	}
2072
2073	return (retval);
2074}
2075
2076static void
2077ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2078{
2079	struct scsi_vpd_id_descriptor *desc;
2080	int i;
2081
2082	if (id == NULL || id->len < 4)
2083		return;
2084	desc = (struct scsi_vpd_id_descriptor *)id->data;
2085	switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2086	case SVPD_ID_TYPE_T10:
2087		sbuf_printf(sb, "t10.");
2088		break;
2089	case SVPD_ID_TYPE_EUI64:
2090		sbuf_printf(sb, "eui.");
2091		break;
2092	case SVPD_ID_TYPE_NAA:
2093		sbuf_printf(sb, "naa.");
2094		break;
2095	case SVPD_ID_TYPE_SCSI_NAME:
2096		break;
2097	}
2098	switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2099	case SVPD_ID_CODESET_BINARY:
2100		for (i = 0; i < desc->length; i++)
2101			sbuf_printf(sb, "%02x", desc->identifier[i]);
2102		break;
2103	case SVPD_ID_CODESET_ASCII:
2104		sbuf_printf(sb, "%.*s", (int)desc->length,
2105		    (char *)desc->identifier);
2106		break;
2107	case SVPD_ID_CODESET_UTF8:
2108		sbuf_printf(sb, "%s", (char *)desc->identifier);
2109		break;
2110	}
2111}
2112
2113static int
2114ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2115	  struct thread *td)
2116{
2117	struct ctl_softc *softc;
2118	int retval;
2119
2120	softc = control_softc;
2121
2122	retval = 0;
2123
2124	switch (cmd) {
2125	case CTL_IO:
2126		retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2127		break;
2128	case CTL_ENABLE_PORT:
2129	case CTL_DISABLE_PORT:
2130	case CTL_SET_PORT_WWNS: {
2131		struct ctl_port *port;
2132		struct ctl_port_entry *entry;
2133
2134		entry = (struct ctl_port_entry *)addr;
2135
2136		mtx_lock(&softc->ctl_lock);
2137		STAILQ_FOREACH(port, &softc->port_list, links) {
2138			int action, done;
2139
2140			if (port->targ_port < softc->port_min ||
2141			    port->targ_port >= softc->port_max)
2142				continue;
2143
2144			action = 0;
2145			done = 0;
2146			if ((entry->port_type == CTL_PORT_NONE)
2147			 && (entry->targ_port == port->targ_port)) {
2148				/*
2149				 * If the user only wants to enable or
2150				 * disable or set WWNs on a specific port,
2151				 * do the operation and we're done.
2152				 */
2153				action = 1;
2154				done = 1;
2155			} else if (entry->port_type & port->port_type) {
2156				/*
2157				 * Compare the user's type mask with the
2158				 * particular frontend type to see if we
2159				 * have a match.
2160				 */
2161				action = 1;
2162				done = 0;
2163
2164				/*
2165				 * Make sure the user isn't trying to set
2166				 * WWNs on multiple ports at the same time.
2167				 */
2168				if (cmd == CTL_SET_PORT_WWNS) {
2169					printf("%s: Can't set WWNs on "
2170					       "multiple ports\n", __func__);
2171					retval = EINVAL;
2172					break;
2173				}
2174			}
2175			if (action == 0)
2176				continue;
2177
2178			/*
2179			 * XXX KDM we have to drop the lock here, because
2180			 * the online/offline operations can potentially
2181			 * block.  We need to reference count the frontends
2182			 * so they can't go away,
2183			 */
2184			if (cmd == CTL_ENABLE_PORT) {
2185				mtx_unlock(&softc->ctl_lock);
2186				ctl_port_online(port);
2187				mtx_lock(&softc->ctl_lock);
2188			} else if (cmd == CTL_DISABLE_PORT) {
2189				mtx_unlock(&softc->ctl_lock);
2190				ctl_port_offline(port);
2191				mtx_lock(&softc->ctl_lock);
2192			} else if (cmd == CTL_SET_PORT_WWNS) {
2193				ctl_port_set_wwns(port,
2194				    (entry->flags & CTL_PORT_WWNN_VALID) ?
2195				    1 : 0, entry->wwnn,
2196				    (entry->flags & CTL_PORT_WWPN_VALID) ?
2197				    1 : 0, entry->wwpn);
2198			}
2199			if (done != 0)
2200				break;
2201		}
2202		mtx_unlock(&softc->ctl_lock);
2203		break;
2204	}
2205	case CTL_GET_PORT_LIST: {
2206		struct ctl_port *port;
2207		struct ctl_port_list *list;
2208		int i;
2209
2210		list = (struct ctl_port_list *)addr;
2211
2212		if (list->alloc_len != (list->alloc_num *
2213		    sizeof(struct ctl_port_entry))) {
2214			printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2215			       "alloc_num %u * sizeof(struct ctl_port_entry) "
2216			       "%zu\n", __func__, list->alloc_len,
2217			       list->alloc_num, sizeof(struct ctl_port_entry));
2218			retval = EINVAL;
2219			break;
2220		}
2221		list->fill_len = 0;
2222		list->fill_num = 0;
2223		list->dropped_num = 0;
2224		i = 0;
2225		mtx_lock(&softc->ctl_lock);
2226		STAILQ_FOREACH(port, &softc->port_list, links) {
2227			struct ctl_port_entry entry, *list_entry;
2228
2229			if (list->fill_num >= list->alloc_num) {
2230				list->dropped_num++;
2231				continue;
2232			}
2233
2234			entry.port_type = port->port_type;
2235			strlcpy(entry.port_name, port->port_name,
2236				sizeof(entry.port_name));
2237			entry.targ_port = port->targ_port;
2238			entry.physical_port = port->physical_port;
2239			entry.virtual_port = port->virtual_port;
2240			entry.wwnn = port->wwnn;
2241			entry.wwpn = port->wwpn;
2242			if (port->status & CTL_PORT_STATUS_ONLINE)
2243				entry.online = 1;
2244			else
2245				entry.online = 0;
2246
2247			list_entry = &list->entries[i];
2248
2249			retval = copyout(&entry, list_entry, sizeof(entry));
2250			if (retval != 0) {
2251				printf("%s: CTL_GET_PORT_LIST: copyout "
2252				       "returned %d\n", __func__, retval);
2253				break;
2254			}
2255			i++;
2256			list->fill_num++;
2257			list->fill_len += sizeof(entry);
2258		}
2259		mtx_unlock(&softc->ctl_lock);
2260
2261		/*
2262		 * If this is non-zero, we had a copyout fault, so there's
2263		 * probably no point in attempting to set the status inside
2264		 * the structure.
2265		 */
2266		if (retval != 0)
2267			break;
2268
2269		if (list->dropped_num > 0)
2270			list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2271		else
2272			list->status = CTL_PORT_LIST_OK;
2273		break;
2274	}
2275	case CTL_DUMP_OOA: {
2276		struct ctl_lun *lun;
2277		union ctl_io *io;
2278		char printbuf[128];
2279		struct sbuf sb;
2280
2281		mtx_lock(&softc->ctl_lock);
2282		printf("Dumping OOA queues:\n");
2283		STAILQ_FOREACH(lun, &softc->lun_list, links) {
2284			mtx_lock(&lun->lun_lock);
2285			for (io = (union ctl_io *)TAILQ_FIRST(
2286			     &lun->ooa_queue); io != NULL;
2287			     io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2288			     ooa_links)) {
2289				sbuf_new(&sb, printbuf, sizeof(printbuf),
2290					 SBUF_FIXEDLEN);
2291				sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2292					    (intmax_t)lun->lun,
2293					    io->scsiio.tag_num,
2294					    (io->io_hdr.flags &
2295					    CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2296					    (io->io_hdr.flags &
2297					    CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2298					    (io->io_hdr.flags &
2299					    CTL_FLAG_ABORT) ? " ABORT" : "",
2300			                    (io->io_hdr.flags &
2301		                        CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2302				ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2303				sbuf_finish(&sb);
2304				printf("%s\n", sbuf_data(&sb));
2305			}
2306			mtx_unlock(&lun->lun_lock);
2307		}
2308		printf("OOA queues dump done\n");
2309		mtx_unlock(&softc->ctl_lock);
2310		break;
2311	}
2312	case CTL_GET_OOA: {
2313		struct ctl_lun *lun;
2314		struct ctl_ooa *ooa_hdr;
2315		struct ctl_ooa_entry *entries;
2316		uint32_t cur_fill_num;
2317
2318		ooa_hdr = (struct ctl_ooa *)addr;
2319
2320		if ((ooa_hdr->alloc_len == 0)
2321		 || (ooa_hdr->alloc_num == 0)) {
2322			printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2323			       "must be non-zero\n", __func__,
2324			       ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2325			retval = EINVAL;
2326			break;
2327		}
2328
2329		if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2330		    sizeof(struct ctl_ooa_entry))) {
2331			printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2332			       "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2333			       __func__, ooa_hdr->alloc_len,
2334			       ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2335			retval = EINVAL;
2336			break;
2337		}
2338
2339		entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2340		if (entries == NULL) {
2341			printf("%s: could not allocate %d bytes for OOA "
2342			       "dump\n", __func__, ooa_hdr->alloc_len);
2343			retval = ENOMEM;
2344			break;
2345		}
2346
2347		mtx_lock(&softc->ctl_lock);
2348		if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2349		 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2350		  || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2351			mtx_unlock(&softc->ctl_lock);
2352			free(entries, M_CTL);
2353			printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2354			       __func__, (uintmax_t)ooa_hdr->lun_num);
2355			retval = EINVAL;
2356			break;
2357		}
2358
2359		cur_fill_num = 0;
2360
2361		if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2362			STAILQ_FOREACH(lun, &softc->lun_list, links) {
2363				retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2364					ooa_hdr, entries);
2365				if (retval != 0)
2366					break;
2367			}
2368			if (retval != 0) {
2369				mtx_unlock(&softc->ctl_lock);
2370				free(entries, M_CTL);
2371				break;
2372			}
2373		} else {
2374			lun = softc->ctl_luns[ooa_hdr->lun_num];
2375
2376			retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2377						    entries);
2378		}
2379		mtx_unlock(&softc->ctl_lock);
2380
2381		ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2382		ooa_hdr->fill_len = ooa_hdr->fill_num *
2383			sizeof(struct ctl_ooa_entry);
2384		retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2385		if (retval != 0) {
2386			printf("%s: error copying out %d bytes for OOA dump\n",
2387			       __func__, ooa_hdr->fill_len);
2388		}
2389
2390		getbintime(&ooa_hdr->cur_bt);
2391
2392		if (cur_fill_num > ooa_hdr->alloc_num) {
2393			ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2394			ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2395		} else {
2396			ooa_hdr->dropped_num = 0;
2397			ooa_hdr->status = CTL_OOA_OK;
2398		}
2399
2400		free(entries, M_CTL);
2401		break;
2402	}
2403	case CTL_CHECK_OOA: {
2404		union ctl_io *io;
2405		struct ctl_lun *lun;
2406		struct ctl_ooa_info *ooa_info;
2407
2408
2409		ooa_info = (struct ctl_ooa_info *)addr;
2410
2411		if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2412			ooa_info->status = CTL_OOA_INVALID_LUN;
2413			break;
2414		}
2415		mtx_lock(&softc->ctl_lock);
2416		lun = softc->ctl_luns[ooa_info->lun_id];
2417		if (lun == NULL) {
2418			mtx_unlock(&softc->ctl_lock);
2419			ooa_info->status = CTL_OOA_INVALID_LUN;
2420			break;
2421		}
2422		mtx_lock(&lun->lun_lock);
2423		mtx_unlock(&softc->ctl_lock);
2424		ooa_info->num_entries = 0;
2425		for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2426		     io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2427		     &io->io_hdr, ooa_links)) {
2428			ooa_info->num_entries++;
2429		}
2430		mtx_unlock(&lun->lun_lock);
2431
2432		ooa_info->status = CTL_OOA_SUCCESS;
2433
2434		break;
2435	}
2436	case CTL_DELAY_IO: {
2437		struct ctl_io_delay_info *delay_info;
2438#ifdef CTL_IO_DELAY
2439		struct ctl_lun *lun;
2440#endif /* CTL_IO_DELAY */
2441
2442		delay_info = (struct ctl_io_delay_info *)addr;
2443
2444#ifdef CTL_IO_DELAY
2445		mtx_lock(&softc->ctl_lock);
2446
2447		if ((delay_info->lun_id >= CTL_MAX_LUNS)
2448		 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2449			delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2450		} else {
2451			lun = softc->ctl_luns[delay_info->lun_id];
2452			mtx_lock(&lun->lun_lock);
2453
2454			delay_info->status = CTL_DELAY_STATUS_OK;
2455
2456			switch (delay_info->delay_type) {
2457			case CTL_DELAY_TYPE_CONT:
2458				break;
2459			case CTL_DELAY_TYPE_ONESHOT:
2460				break;
2461			default:
2462				delay_info->status =
2463					CTL_DELAY_STATUS_INVALID_TYPE;
2464				break;
2465			}
2466
2467			switch (delay_info->delay_loc) {
2468			case CTL_DELAY_LOC_DATAMOVE:
2469				lun->delay_info.datamove_type =
2470					delay_info->delay_type;
2471				lun->delay_info.datamove_delay =
2472					delay_info->delay_secs;
2473				break;
2474			case CTL_DELAY_LOC_DONE:
2475				lun->delay_info.done_type =
2476					delay_info->delay_type;
2477				lun->delay_info.done_delay =
2478					delay_info->delay_secs;
2479				break;
2480			default:
2481				delay_info->status =
2482					CTL_DELAY_STATUS_INVALID_LOC;
2483				break;
2484			}
2485			mtx_unlock(&lun->lun_lock);
2486		}
2487
2488		mtx_unlock(&softc->ctl_lock);
2489#else
2490		delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2491#endif /* CTL_IO_DELAY */
2492		break;
2493	}
2494	case CTL_REALSYNC_SET: {
2495		int *syncstate;
2496
2497		syncstate = (int *)addr;
2498
2499		mtx_lock(&softc->ctl_lock);
2500		switch (*syncstate) {
2501		case 0:
2502			softc->flags &= ~CTL_FLAG_REAL_SYNC;
2503			break;
2504		case 1:
2505			softc->flags |= CTL_FLAG_REAL_SYNC;
2506			break;
2507		default:
2508			retval = EINVAL;
2509			break;
2510		}
2511		mtx_unlock(&softc->ctl_lock);
2512		break;
2513	}
2514	case CTL_REALSYNC_GET: {
2515		int *syncstate;
2516
2517		syncstate = (int*)addr;
2518
2519		mtx_lock(&softc->ctl_lock);
2520		if (softc->flags & CTL_FLAG_REAL_SYNC)
2521			*syncstate = 1;
2522		else
2523			*syncstate = 0;
2524		mtx_unlock(&softc->ctl_lock);
2525
2526		break;
2527	}
2528	case CTL_SETSYNC:
2529	case CTL_GETSYNC: {
2530		struct ctl_sync_info *sync_info;
2531		struct ctl_lun *lun;
2532
2533		sync_info = (struct ctl_sync_info *)addr;
2534
2535		mtx_lock(&softc->ctl_lock);
2536		lun = softc->ctl_luns[sync_info->lun_id];
2537		if (lun == NULL) {
2538			mtx_unlock(&softc->ctl_lock);
2539			sync_info->status = CTL_GS_SYNC_NO_LUN;
2540			break;
2541		}
2542		/*
2543		 * Get or set the sync interval.  We're not bounds checking
2544		 * in the set case, hopefully the user won't do something
2545		 * silly.
2546		 */
2547		mtx_lock(&lun->lun_lock);
2548		mtx_unlock(&softc->ctl_lock);
2549		if (cmd == CTL_GETSYNC)
2550			sync_info->sync_interval = lun->sync_interval;
2551		else
2552			lun->sync_interval = sync_info->sync_interval;
2553		mtx_unlock(&lun->lun_lock);
2554
2555		sync_info->status = CTL_GS_SYNC_OK;
2556
2557		break;
2558	}
2559	case CTL_GETSTATS: {
2560		struct ctl_stats *stats;
2561		struct ctl_lun *lun;
2562		int i;
2563
2564		stats = (struct ctl_stats *)addr;
2565
2566		if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2567		     stats->alloc_len) {
2568			stats->status = CTL_SS_NEED_MORE_SPACE;
2569			stats->num_luns = softc->num_luns;
2570			break;
2571		}
2572		/*
2573		 * XXX KDM no locking here.  If the LUN list changes,
2574		 * things can blow up.
2575		 */
2576		for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2577		     i++, lun = STAILQ_NEXT(lun, links)) {
2578			retval = copyout(&lun->stats, &stats->lun_stats[i],
2579					 sizeof(lun->stats));
2580			if (retval != 0)
2581				break;
2582		}
2583		stats->num_luns = softc->num_luns;
2584		stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2585				 softc->num_luns;
2586		stats->status = CTL_SS_OK;
2587#ifdef CTL_TIME_IO
2588		stats->flags = CTL_STATS_FLAG_TIME_VALID;
2589#else
2590		stats->flags = CTL_STATS_FLAG_NONE;
2591#endif
2592		getnanouptime(&stats->timestamp);
2593		break;
2594	}
2595	case CTL_ERROR_INJECT: {
2596		struct ctl_error_desc *err_desc, *new_err_desc;
2597		struct ctl_lun *lun;
2598
2599		err_desc = (struct ctl_error_desc *)addr;
2600
2601		new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2602				      M_WAITOK | M_ZERO);
2603		bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2604
2605		mtx_lock(&softc->ctl_lock);
2606		lun = softc->ctl_luns[err_desc->lun_id];
2607		if (lun == NULL) {
2608			mtx_unlock(&softc->ctl_lock);
2609			free(new_err_desc, M_CTL);
2610			printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2611			       __func__, (uintmax_t)err_desc->lun_id);
2612			retval = EINVAL;
2613			break;
2614		}
2615		mtx_lock(&lun->lun_lock);
2616		mtx_unlock(&softc->ctl_lock);
2617
2618		/*
2619		 * We could do some checking here to verify the validity
2620		 * of the request, but given the complexity of error
2621		 * injection requests, the checking logic would be fairly
2622		 * complex.
2623		 *
2624		 * For now, if the request is invalid, it just won't get
2625		 * executed and might get deleted.
2626		 */
2627		STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2628
2629		/*
2630		 * XXX KDM check to make sure the serial number is unique,
2631		 * in case we somehow manage to wrap.  That shouldn't
2632		 * happen for a very long time, but it's the right thing to
2633		 * do.
2634		 */
2635		new_err_desc->serial = lun->error_serial;
2636		err_desc->serial = lun->error_serial;
2637		lun->error_serial++;
2638
2639		mtx_unlock(&lun->lun_lock);
2640		break;
2641	}
2642	case CTL_ERROR_INJECT_DELETE: {
2643		struct ctl_error_desc *delete_desc, *desc, *desc2;
2644		struct ctl_lun *lun;
2645		int delete_done;
2646
2647		delete_desc = (struct ctl_error_desc *)addr;
2648		delete_done = 0;
2649
2650		mtx_lock(&softc->ctl_lock);
2651		lun = softc->ctl_luns[delete_desc->lun_id];
2652		if (lun == NULL) {
2653			mtx_unlock(&softc->ctl_lock);
2654			printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2655			       __func__, (uintmax_t)delete_desc->lun_id);
2656			retval = EINVAL;
2657			break;
2658		}
2659		mtx_lock(&lun->lun_lock);
2660		mtx_unlock(&softc->ctl_lock);
2661		STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2662			if (desc->serial != delete_desc->serial)
2663				continue;
2664
2665			STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2666				      links);
2667			free(desc, M_CTL);
2668			delete_done = 1;
2669		}
2670		mtx_unlock(&lun->lun_lock);
2671		if (delete_done == 0) {
2672			printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2673			       "error serial %ju on LUN %u\n", __func__,
2674			       delete_desc->serial, delete_desc->lun_id);
2675			retval = EINVAL;
2676			break;
2677		}
2678		break;
2679	}
2680	case CTL_DUMP_STRUCTS: {
2681		int i, j, k;
2682		struct ctl_port *port;
2683		struct ctl_frontend *fe;
2684
2685		mtx_lock(&softc->ctl_lock);
2686		printf("CTL Persistent Reservation information start:\n");
2687		for (i = 0; i < CTL_MAX_LUNS; i++) {
2688			struct ctl_lun *lun;
2689
2690			lun = softc->ctl_luns[i];
2691
2692			if ((lun == NULL)
2693			 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2694				continue;
2695
2696			for (j = 0; j < CTL_MAX_PORTS; j++) {
2697				if (lun->pr_keys[j] == NULL)
2698					continue;
2699				for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2700					if (lun->pr_keys[j][k] == 0)
2701						continue;
2702					printf("  LUN %d port %d iid %d key "
2703					       "%#jx\n", i, j, k,
2704					       (uintmax_t)lun->pr_keys[j][k]);
2705				}
2706			}
2707		}
2708		printf("CTL Persistent Reservation information end\n");
2709		printf("CTL Ports:\n");
2710		STAILQ_FOREACH(port, &softc->port_list, links) {
2711			printf("  Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2712			       "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2713			       port->frontend->name, port->port_type,
2714			       port->physical_port, port->virtual_port,
2715			       (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2716			for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2717				if (port->wwpn_iid[j].in_use == 0 &&
2718				    port->wwpn_iid[j].wwpn == 0 &&
2719				    port->wwpn_iid[j].name == NULL)
2720					continue;
2721
2722				printf("    iid %u use %d WWPN %#jx '%s'\n",
2723				    j, port->wwpn_iid[j].in_use,
2724				    (uintmax_t)port->wwpn_iid[j].wwpn,
2725				    port->wwpn_iid[j].name);
2726			}
2727		}
2728		printf("CTL Port information end\n");
2729		mtx_unlock(&softc->ctl_lock);
2730		/*
2731		 * XXX KDM calling this without a lock.  We'd likely want
2732		 * to drop the lock before calling the frontend's dump
2733		 * routine anyway.
2734		 */
2735		printf("CTL Frontends:\n");
2736		STAILQ_FOREACH(fe, &softc->fe_list, links) {
2737			printf("  Frontend '%s'\n", fe->name);
2738			if (fe->fe_dump != NULL)
2739				fe->fe_dump();
2740		}
2741		printf("CTL Frontend information end\n");
2742		break;
2743	}
2744	case CTL_LUN_REQ: {
2745		struct ctl_lun_req *lun_req;
2746		struct ctl_backend_driver *backend;
2747
2748		lun_req = (struct ctl_lun_req *)addr;
2749
2750		backend = ctl_backend_find(lun_req->backend);
2751		if (backend == NULL) {
2752			lun_req->status = CTL_LUN_ERROR;
2753			snprintf(lun_req->error_str,
2754				 sizeof(lun_req->error_str),
2755				 "Backend \"%s\" not found.",
2756				 lun_req->backend);
2757			break;
2758		}
2759		if (lun_req->num_be_args > 0) {
2760			lun_req->kern_be_args = ctl_copyin_args(
2761				lun_req->num_be_args,
2762				lun_req->be_args,
2763				lun_req->error_str,
2764				sizeof(lun_req->error_str));
2765			if (lun_req->kern_be_args == NULL) {
2766				lun_req->status = CTL_LUN_ERROR;
2767				break;
2768			}
2769		}
2770
2771		retval = backend->ioctl(dev, cmd, addr, flag, td);
2772
2773		if (lun_req->num_be_args > 0) {
2774			ctl_copyout_args(lun_req->num_be_args,
2775				      lun_req->kern_be_args);
2776			ctl_free_args(lun_req->num_be_args,
2777				      lun_req->kern_be_args);
2778		}
2779		break;
2780	}
2781	case CTL_LUN_LIST: {
2782		struct sbuf *sb;
2783		struct ctl_lun *lun;
2784		struct ctl_lun_list *list;
2785		struct ctl_option *opt;
2786
2787		list = (struct ctl_lun_list *)addr;
2788
2789		/*
2790		 * Allocate a fixed length sbuf here, based on the length
2791		 * of the user's buffer.  We could allocate an auto-extending
2792		 * buffer, and then tell the user how much larger our
2793		 * amount of data is than his buffer, but that presents
2794		 * some problems:
2795		 *
2796		 * 1.  The sbuf(9) routines use a blocking malloc, and so
2797		 *     we can't hold a lock while calling them with an
2798		 *     auto-extending buffer.
2799 		 *
2800		 * 2.  There is not currently a LUN reference counting
2801		 *     mechanism, outside of outstanding transactions on
2802		 *     the LUN's OOA queue.  So a LUN could go away on us
2803		 *     while we're getting the LUN number, backend-specific
2804		 *     information, etc.  Thus, given the way things
2805		 *     currently work, we need to hold the CTL lock while
2806		 *     grabbing LUN information.
2807		 *
2808		 * So, from the user's standpoint, the best thing to do is
2809		 * allocate what he thinks is a reasonable buffer length,
2810		 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2811		 * double the buffer length and try again.  (And repeat
2812		 * that until he succeeds.)
2813		 */
2814		sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2815		if (sb == NULL) {
2816			list->status = CTL_LUN_LIST_ERROR;
2817			snprintf(list->error_str, sizeof(list->error_str),
2818				 "Unable to allocate %d bytes for LUN list",
2819				 list->alloc_len);
2820			break;
2821		}
2822
2823		sbuf_printf(sb, "<ctllunlist>\n");
2824
2825		mtx_lock(&softc->ctl_lock);
2826		STAILQ_FOREACH(lun, &softc->lun_list, links) {
2827			mtx_lock(&lun->lun_lock);
2828			retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
2829					     (uintmax_t)lun->lun);
2830
2831			/*
2832			 * Bail out as soon as we see that we've overfilled
2833			 * the buffer.
2834			 */
2835			if (retval != 0)
2836				break;
2837
2838			retval = sbuf_printf(sb, "\t<backend_type>%s"
2839					     "</backend_type>\n",
2840					     (lun->backend == NULL) ?  "none" :
2841					     lun->backend->name);
2842
2843			if (retval != 0)
2844				break;
2845
2846			retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
2847					     lun->be_lun->lun_type);
2848
2849			if (retval != 0)
2850				break;
2851
2852			if (lun->backend == NULL) {
2853				retval = sbuf_printf(sb, "</lun>\n");
2854				if (retval != 0)
2855					break;
2856				continue;
2857			}
2858
2859			retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
2860					     (lun->be_lun->maxlba > 0) ?
2861					     lun->be_lun->maxlba + 1 : 0);
2862
2863			if (retval != 0)
2864				break;
2865
2866			retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
2867					     lun->be_lun->blocksize);
2868
2869			if (retval != 0)
2870				break;
2871
2872			retval = sbuf_printf(sb, "\t<serial_number>");
2873
2874			if (retval != 0)
2875				break;
2876
2877			retval = ctl_sbuf_printf_esc(sb,
2878			    lun->be_lun->serial_num,
2879			    sizeof(lun->be_lun->serial_num));
2880
2881			if (retval != 0)
2882				break;
2883
2884			retval = sbuf_printf(sb, "</serial_number>\n");
2885
2886			if (retval != 0)
2887				break;
2888
2889			retval = sbuf_printf(sb, "\t<device_id>");
2890
2891			if (retval != 0)
2892				break;
2893
2894			retval = ctl_sbuf_printf_esc(sb,
2895			    lun->be_lun->device_id,
2896			    sizeof(lun->be_lun->device_id));
2897
2898			if (retval != 0)
2899				break;
2900
2901			retval = sbuf_printf(sb, "</device_id>\n");
2902
2903			if (retval != 0)
2904				break;
2905
2906			if (lun->backend->lun_info != NULL) {
2907				retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
2908				if (retval != 0)
2909					break;
2910			}
2911			STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
2912				retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
2913				    opt->name, opt->value, opt->name);
2914				if (retval != 0)
2915					break;
2916			}
2917
2918			retval = sbuf_printf(sb, "</lun>\n");
2919
2920			if (retval != 0)
2921				break;
2922			mtx_unlock(&lun->lun_lock);
2923		}
2924		if (lun != NULL)
2925			mtx_unlock(&lun->lun_lock);
2926		mtx_unlock(&softc->ctl_lock);
2927
2928		if ((retval != 0)
2929		 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
2930			retval = 0;
2931			sbuf_delete(sb);
2932			list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
2933			snprintf(list->error_str, sizeof(list->error_str),
2934				 "Out of space, %d bytes is too small",
2935				 list->alloc_len);
2936			break;
2937		}
2938
2939		sbuf_finish(sb);
2940
2941		retval = copyout(sbuf_data(sb), list->lun_xml,
2942				 sbuf_len(sb) + 1);
2943
2944		list->fill_len = sbuf_len(sb) + 1;
2945		list->status = CTL_LUN_LIST_OK;
2946		sbuf_delete(sb);
2947		break;
2948	}
2949	case CTL_ISCSI: {
2950		struct ctl_iscsi *ci;
2951		struct ctl_frontend *fe;
2952
2953		ci = (struct ctl_iscsi *)addr;
2954
2955		fe = ctl_frontend_find("iscsi");
2956		if (fe == NULL) {
2957			ci->status = CTL_ISCSI_ERROR;
2958			snprintf(ci->error_str, sizeof(ci->error_str),
2959			    "Frontend \"iscsi\" not found.");
2960			break;
2961		}
2962
2963		retval = fe->ioctl(dev, cmd, addr, flag, td);
2964		break;
2965	}
2966	case CTL_PORT_REQ: {
2967		struct ctl_req *req;
2968		struct ctl_frontend *fe;
2969
2970		req = (struct ctl_req *)addr;
2971
2972		fe = ctl_frontend_find(req->driver);
2973		if (fe == NULL) {
2974			req->status = CTL_LUN_ERROR;
2975			snprintf(req->error_str, sizeof(req->error_str),
2976			    "Frontend \"%s\" not found.", req->driver);
2977			break;
2978		}
2979		if (req->num_args > 0) {
2980			req->kern_args = ctl_copyin_args(req->num_args,
2981			    req->args, req->error_str, sizeof(req->error_str));
2982			if (req->kern_args == NULL) {
2983				req->status = CTL_LUN_ERROR;
2984				break;
2985			}
2986		}
2987
2988		if (fe->ioctl)
2989			retval = fe->ioctl(dev, cmd, addr, flag, td);
2990		else
2991			retval = ENODEV;
2992
2993		if (req->num_args > 0) {
2994			ctl_copyout_args(req->num_args, req->kern_args);
2995			ctl_free_args(req->num_args, req->kern_args);
2996		}
2997		break;
2998	}
2999	case CTL_PORT_LIST: {
3000		struct sbuf *sb;
3001		struct ctl_port *port;
3002		struct ctl_lun_list *list;
3003		struct ctl_option *opt;
3004		int j;
3005		uint32_t plun;
3006
3007		list = (struct ctl_lun_list *)addr;
3008
3009		sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3010		if (sb == NULL) {
3011			list->status = CTL_LUN_LIST_ERROR;
3012			snprintf(list->error_str, sizeof(list->error_str),
3013				 "Unable to allocate %d bytes for LUN list",
3014				 list->alloc_len);
3015			break;
3016		}
3017
3018		sbuf_printf(sb, "<ctlportlist>\n");
3019
3020		mtx_lock(&softc->ctl_lock);
3021		STAILQ_FOREACH(port, &softc->port_list, links) {
3022			retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3023					     (uintmax_t)port->targ_port);
3024
3025			/*
3026			 * Bail out as soon as we see that we've overfilled
3027			 * the buffer.
3028			 */
3029			if (retval != 0)
3030				break;
3031
3032			retval = sbuf_printf(sb, "\t<frontend_type>%s"
3033			    "</frontend_type>\n", port->frontend->name);
3034			if (retval != 0)
3035				break;
3036
3037			retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3038					     port->port_type);
3039			if (retval != 0)
3040				break;
3041
3042			retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3043			    (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3044			if (retval != 0)
3045				break;
3046
3047			retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3048			    port->port_name);
3049			if (retval != 0)
3050				break;
3051
3052			retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3053			    port->physical_port);
3054			if (retval != 0)
3055				break;
3056
3057			retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3058			    port->virtual_port);
3059			if (retval != 0)
3060				break;
3061
3062			if (port->target_devid != NULL) {
3063				sbuf_printf(sb, "\t<target>");
3064				ctl_id_sbuf(port->target_devid, sb);
3065				sbuf_printf(sb, "</target>\n");
3066			}
3067
3068			if (port->port_devid != NULL) {
3069				sbuf_printf(sb, "\t<port>");
3070				ctl_id_sbuf(port->port_devid, sb);
3071				sbuf_printf(sb, "</port>\n");
3072			}
3073
3074			if (port->port_info != NULL) {
3075				retval = port->port_info(port->onoff_arg, sb);
3076				if (retval != 0)
3077					break;
3078			}
3079			STAILQ_FOREACH(opt, &port->options, links) {
3080				retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3081				    opt->name, opt->value, opt->name);
3082				if (retval != 0)
3083					break;
3084			}
3085
3086			if (port->lun_map != NULL) {
3087				sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3088				for (j = 0; j < CTL_MAX_LUNS; j++) {
3089					plun = ctl_lun_map_from_port(port, j);
3090					if (plun >= CTL_MAX_LUNS)
3091						continue;
3092					sbuf_printf(sb,
3093					    "\t<lun id=\"%u\">%u</lun>\n",
3094					    j, plun);
3095				}
3096			}
3097
3098			for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3099				if (port->wwpn_iid[j].in_use == 0 ||
3100				    (port->wwpn_iid[j].wwpn == 0 &&
3101				     port->wwpn_iid[j].name == NULL))
3102					continue;
3103
3104				if (port->wwpn_iid[j].name != NULL)
3105					retval = sbuf_printf(sb,
3106					    "\t<initiator id=\"%u\">%s</initiator>\n",
3107					    j, port->wwpn_iid[j].name);
3108				else
3109					retval = sbuf_printf(sb,
3110					    "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3111					    j, port->wwpn_iid[j].wwpn);
3112				if (retval != 0)
3113					break;
3114			}
3115			if (retval != 0)
3116				break;
3117
3118			retval = sbuf_printf(sb, "</targ_port>\n");
3119			if (retval != 0)
3120				break;
3121		}
3122		mtx_unlock(&softc->ctl_lock);
3123
3124		if ((retval != 0)
3125		 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3126			retval = 0;
3127			sbuf_delete(sb);
3128			list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3129			snprintf(list->error_str, sizeof(list->error_str),
3130				 "Out of space, %d bytes is too small",
3131				 list->alloc_len);
3132			break;
3133		}
3134
3135		sbuf_finish(sb);
3136
3137		retval = copyout(sbuf_data(sb), list->lun_xml,
3138				 sbuf_len(sb) + 1);
3139
3140		list->fill_len = sbuf_len(sb) + 1;
3141		list->status = CTL_LUN_LIST_OK;
3142		sbuf_delete(sb);
3143		break;
3144	}
3145	case CTL_LUN_MAP: {
3146		struct ctl_lun_map *lm  = (struct ctl_lun_map *)addr;
3147		struct ctl_port *port;
3148
3149		mtx_lock(&softc->ctl_lock);
3150		if (lm->port < softc->port_min ||
3151		    lm->port >= softc->port_max ||
3152		    (port = softc->ctl_ports[lm->port]) == NULL) {
3153			mtx_unlock(&softc->ctl_lock);
3154			return (ENXIO);
3155		}
3156		mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3157		if (lm->plun < CTL_MAX_LUNS) {
3158			if (lm->lun == UINT32_MAX)
3159				retval = ctl_lun_map_unset(port, lm->plun);
3160			else if (lm->lun < CTL_MAX_LUNS &&
3161			    softc->ctl_luns[lm->lun] != NULL)
3162				retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3163			else
3164				return (ENXIO);
3165		} else if (lm->plun == UINT32_MAX) {
3166			if (lm->lun == UINT32_MAX)
3167				retval = ctl_lun_map_deinit(port);
3168			else
3169				retval = ctl_lun_map_init(port);
3170		} else
3171			return (ENXIO);
3172		break;
3173	}
3174	default: {
3175		/* XXX KDM should we fix this? */
3176#if 0
3177		struct ctl_backend_driver *backend;
3178		unsigned int type;
3179		int found;
3180
3181		found = 0;
3182
3183		/*
3184		 * We encode the backend type as the ioctl type for backend
3185		 * ioctls.  So parse it out here, and then search for a
3186		 * backend of this type.
3187		 */
3188		type = _IOC_TYPE(cmd);
3189
3190		STAILQ_FOREACH(backend, &softc->be_list, links) {
3191			if (backend->type == type) {
3192				found = 1;
3193				break;
3194			}
3195		}
3196		if (found == 0) {
3197			printf("ctl: unknown ioctl command %#lx or backend "
3198			       "%d\n", cmd, type);
3199			retval = EINVAL;
3200			break;
3201		}
3202		retval = backend->ioctl(dev, cmd, addr, flag, td);
3203#endif
3204		retval = ENOTTY;
3205		break;
3206	}
3207	}
3208	return (retval);
3209}
3210
3211uint32_t
3212ctl_get_initindex(struct ctl_nexus *nexus)
3213{
3214	return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3215}
3216
3217int
3218ctl_lun_map_init(struct ctl_port *port)
3219{
3220	struct ctl_softc *softc = control_softc;
3221	struct ctl_lun *lun;
3222	uint32_t i;
3223
3224	if (port->lun_map == NULL)
3225		port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3226		    M_CTL, M_NOWAIT);
3227	if (port->lun_map == NULL)
3228		return (ENOMEM);
3229	for (i = 0; i < CTL_MAX_LUNS; i++)
3230		port->lun_map[i] = UINT32_MAX;
3231	if (port->status & CTL_PORT_STATUS_ONLINE) {
3232		if (port->lun_disable != NULL) {
3233			STAILQ_FOREACH(lun, &softc->lun_list, links)
3234				port->lun_disable(port->targ_lun_arg, lun->lun);
3235		}
3236		ctl_isc_announce_port(port);
3237	}
3238	return (0);
3239}
3240
3241int
3242ctl_lun_map_deinit(struct ctl_port *port)
3243{
3244	struct ctl_softc *softc = control_softc;
3245	struct ctl_lun *lun;
3246
3247	if (port->lun_map == NULL)
3248		return (0);
3249	free(port->lun_map, M_CTL);
3250	port->lun_map = NULL;
3251	if (port->status & CTL_PORT_STATUS_ONLINE) {
3252		if (port->lun_enable != NULL) {
3253			STAILQ_FOREACH(lun, &softc->lun_list, links)
3254				port->lun_enable(port->targ_lun_arg, lun->lun);
3255		}
3256		ctl_isc_announce_port(port);
3257	}
3258	return (0);
3259}
3260
3261int
3262ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3263{
3264	int status;
3265	uint32_t old;
3266
3267	if (port->lun_map == NULL) {
3268		status = ctl_lun_map_init(port);
3269		if (status != 0)
3270			return (status);
3271	}
3272	old = port->lun_map[plun];
3273	port->lun_map[plun] = glun;
3274	if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3275		if (port->lun_enable != NULL)
3276			port->lun_enable(port->targ_lun_arg, plun);
3277		ctl_isc_announce_port(port);
3278	}
3279	return (0);
3280}
3281
3282int
3283ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3284{
3285	uint32_t old;
3286
3287	if (port->lun_map == NULL)
3288		return (0);
3289	old = port->lun_map[plun];
3290	port->lun_map[plun] = UINT32_MAX;
3291	if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3292		if (port->lun_disable != NULL)
3293			port->lun_disable(port->targ_lun_arg, plun);
3294		ctl_isc_announce_port(port);
3295	}
3296	return (0);
3297}
3298
3299uint32_t
3300ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3301{
3302
3303	if (port == NULL)
3304		return (UINT32_MAX);
3305	if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3306		return (lun_id);
3307	return (port->lun_map[lun_id]);
3308}
3309
3310uint32_t
3311ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3312{
3313	uint32_t i;
3314
3315	if (port == NULL)
3316		return (UINT32_MAX);
3317	if (port->lun_map == NULL)
3318		return (lun_id);
3319	for (i = 0; i < CTL_MAX_LUNS; i++) {
3320		if (port->lun_map[i] == lun_id)
3321			return (i);
3322	}
3323	return (UINT32_MAX);
3324}
3325
3326static struct ctl_port *
3327ctl_io_port(struct ctl_io_hdr *io_hdr)
3328{
3329
3330	return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3331}
3332
3333int
3334ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3335{
3336	int i;
3337
3338	for (i = first; i < last; i++) {
3339		if ((mask[i / 32] & (1 << (i % 32))) == 0)
3340			return (i);
3341	}
3342	return (-1);
3343}
3344
3345int
3346ctl_set_mask(uint32_t *mask, uint32_t bit)
3347{
3348	uint32_t chunk, piece;
3349
3350	chunk = bit >> 5;
3351	piece = bit % (sizeof(uint32_t) * 8);
3352
3353	if ((mask[chunk] & (1 << piece)) != 0)
3354		return (-1);
3355	else
3356		mask[chunk] |= (1 << piece);
3357
3358	return (0);
3359}
3360
3361int
3362ctl_clear_mask(uint32_t *mask, uint32_t bit)
3363{
3364	uint32_t chunk, piece;
3365
3366	chunk = bit >> 5;
3367	piece = bit % (sizeof(uint32_t) * 8);
3368
3369	if ((mask[chunk] & (1 << piece)) == 0)
3370		return (-1);
3371	else
3372		mask[chunk] &= ~(1 << piece);
3373
3374	return (0);
3375}
3376
3377int
3378ctl_is_set(uint32_t *mask, uint32_t bit)
3379{
3380	uint32_t chunk, piece;
3381
3382	chunk = bit >> 5;
3383	piece = bit % (sizeof(uint32_t) * 8);
3384
3385	if ((mask[chunk] & (1 << piece)) == 0)
3386		return (0);
3387	else
3388		return (1);
3389}
3390
3391static uint64_t
3392ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3393{
3394	uint64_t *t;
3395
3396	t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3397	if (t == NULL)
3398		return (0);
3399	return (t[residx % CTL_MAX_INIT_PER_PORT]);
3400}
3401
3402static void
3403ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3404{
3405	uint64_t *t;
3406
3407	t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3408	if (t == NULL)
3409		return;
3410	t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3411}
3412
3413static void
3414ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3415{
3416	uint64_t *p;
3417	u_int i;
3418
3419	i = residx/CTL_MAX_INIT_PER_PORT;
3420	if (lun->pr_keys[i] != NULL)
3421		return;
3422	mtx_unlock(&lun->lun_lock);
3423	p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3424	    M_WAITOK | M_ZERO);
3425	mtx_lock(&lun->lun_lock);
3426	if (lun->pr_keys[i] == NULL)
3427		lun->pr_keys[i] = p;
3428	else
3429		free(p, M_CTL);
3430}
3431
3432static void
3433ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3434{
3435	uint64_t *t;
3436
3437	t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3438	KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3439	t[residx % CTL_MAX_INIT_PER_PORT] = key;
3440}
3441
3442/*
3443 * ctl_softc, pool_name, total_ctl_io are passed in.
3444 * npool is passed out.
3445 */
3446int
3447ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3448		uint32_t total_ctl_io, void **npool)
3449{
3450#ifdef IO_POOLS
3451	struct ctl_io_pool *pool;
3452
3453	pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3454					    M_NOWAIT | M_ZERO);
3455	if (pool == NULL)
3456		return (ENOMEM);
3457
3458	snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3459	pool->ctl_softc = ctl_softc;
3460	pool->zone = uma_zsecond_create(pool->name, NULL,
3461	    NULL, NULL, NULL, ctl_softc->io_zone);
3462	/* uma_prealloc(pool->zone, total_ctl_io); */
3463
3464	*npool = pool;
3465#else
3466	*npool = ctl_softc->io_zone;
3467#endif
3468	return (0);
3469}
3470
3471void
3472ctl_pool_free(struct ctl_io_pool *pool)
3473{
3474
3475	if (pool == NULL)
3476		return;
3477
3478#ifdef IO_POOLS
3479	uma_zdestroy(pool->zone);
3480	free(pool, M_CTL);
3481#endif
3482}
3483
3484union ctl_io *
3485ctl_alloc_io(void *pool_ref)
3486{
3487	union ctl_io *io;
3488#ifdef IO_POOLS
3489	struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3490
3491	io = uma_zalloc(pool->zone, M_WAITOK);
3492#else
3493	io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3494#endif
3495	if (io != NULL)
3496		io->io_hdr.pool = pool_ref;
3497	return (io);
3498}
3499
3500union ctl_io *
3501ctl_alloc_io_nowait(void *pool_ref)
3502{
3503	union ctl_io *io;
3504#ifdef IO_POOLS
3505	struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3506
3507	io = uma_zalloc(pool->zone, M_NOWAIT);
3508#else
3509	io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3510#endif
3511	if (io != NULL)
3512		io->io_hdr.pool = pool_ref;
3513	return (io);
3514}
3515
3516void
3517ctl_free_io(union ctl_io *io)
3518{
3519#ifdef IO_POOLS
3520	struct ctl_io_pool *pool;
3521#endif
3522
3523	if (io == NULL)
3524		return;
3525
3526#ifdef IO_POOLS
3527	pool = (struct ctl_io_pool *)io->io_hdr.pool;
3528	uma_zfree(pool->zone, io);
3529#else
3530	uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3531#endif
3532}
3533
3534void
3535ctl_zero_io(union ctl_io *io)
3536{
3537	void *pool_ref;
3538
3539	if (io == NULL)
3540		return;
3541
3542	/*
3543	 * May need to preserve linked list pointers at some point too.
3544	 */
3545	pool_ref = io->io_hdr.pool;
3546	memset(io, 0, sizeof(*io));
3547	io->io_hdr.pool = pool_ref;
3548}
3549
3550/*
3551 * This routine is currently used for internal copies of ctl_ios that need
3552 * to persist for some reason after we've already returned status to the
3553 * FETD.  (Thus the flag set.)
3554 *
3555 * XXX XXX
3556 * Note that this makes a blind copy of all fields in the ctl_io, except
3557 * for the pool reference.  This includes any memory that has been
3558 * allocated!  That memory will no longer be valid after done has been
3559 * called, so this would be VERY DANGEROUS for command that actually does
3560 * any reads or writes.  Right now (11/7/2005), this is only used for immediate
3561 * start and stop commands, which don't transfer any data, so this is not a
3562 * problem.  If it is used for anything else, the caller would also need to
3563 * allocate data buffer space and this routine would need to be modified to
3564 * copy the data buffer(s) as well.
3565 */
3566void
3567ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3568{
3569	void *pool_ref;
3570
3571	if ((src == NULL)
3572	 || (dest == NULL))
3573		return;
3574
3575	/*
3576	 * May need to preserve linked list pointers at some point too.
3577	 */
3578	pool_ref = dest->io_hdr.pool;
3579
3580	memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3581
3582	dest->io_hdr.pool = pool_ref;
3583	/*
3584	 * We need to know that this is an internal copy, and doesn't need
3585	 * to get passed back to the FETD that allocated it.
3586	 */
3587	dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3588}
3589
3590int
3591ctl_expand_number(const char *buf, uint64_t *num)
3592{
3593	char *endptr;
3594	uint64_t number;
3595	unsigned shift;
3596
3597	number = strtoq(buf, &endptr, 0);
3598
3599	switch (tolower((unsigned char)*endptr)) {
3600	case 'e':
3601		shift = 60;
3602		break;
3603	case 'p':
3604		shift = 50;
3605		break;
3606	case 't':
3607		shift = 40;
3608		break;
3609	case 'g':
3610		shift = 30;
3611		break;
3612	case 'm':
3613		shift = 20;
3614		break;
3615	case 'k':
3616		shift = 10;
3617		break;
3618	case 'b':
3619	case '\0': /* No unit. */
3620		*num = number;
3621		return (0);
3622	default:
3623		/* Unrecognized unit. */
3624		return (-1);
3625	}
3626
3627	if ((number << shift) >> shift != number) {
3628		/* Overflow */
3629		return (-1);
3630	}
3631	*num = number << shift;
3632	return (0);
3633}
3634
3635
3636/*
3637 * This routine could be used in the future to load default and/or saved
3638 * mode page parameters for a particuar lun.
3639 */
3640static int
3641ctl_init_page_index(struct ctl_lun *lun)
3642{
3643	int i;
3644	struct ctl_page_index *page_index;
3645	const char *value;
3646	uint64_t ival;
3647
3648	memcpy(&lun->mode_pages.index, page_index_template,
3649	       sizeof(page_index_template));
3650
3651	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3652
3653		page_index = &lun->mode_pages.index[i];
3654		/*
3655		 * If this is a disk-only mode page, there's no point in
3656		 * setting it up.  For some pages, we have to have some
3657		 * basic information about the disk in order to calculate the
3658		 * mode page data.
3659		 */
3660		if ((lun->be_lun->lun_type != T_DIRECT)
3661		 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3662			continue;
3663
3664		switch (page_index->page_code & SMPH_PC_MASK) {
3665		case SMS_RW_ERROR_RECOVERY_PAGE: {
3666			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3667				panic("subpage is incorrect!");
3668			memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3669			       &rw_er_page_default,
3670			       sizeof(rw_er_page_default));
3671			memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3672			       &rw_er_page_changeable,
3673			       sizeof(rw_er_page_changeable));
3674			memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3675			       &rw_er_page_default,
3676			       sizeof(rw_er_page_default));
3677			memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3678			       &rw_er_page_default,
3679			       sizeof(rw_er_page_default));
3680			page_index->page_data =
3681				(uint8_t *)lun->mode_pages.rw_er_page;
3682			break;
3683		}
3684		case SMS_FORMAT_DEVICE_PAGE: {
3685			struct scsi_format_page *format_page;
3686
3687			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3688				panic("subpage is incorrect!");
3689
3690			/*
3691			 * Sectors per track are set above.  Bytes per
3692			 * sector need to be set here on a per-LUN basis.
3693			 */
3694			memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3695			       &format_page_default,
3696			       sizeof(format_page_default));
3697			memcpy(&lun->mode_pages.format_page[
3698			       CTL_PAGE_CHANGEABLE], &format_page_changeable,
3699			       sizeof(format_page_changeable));
3700			memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3701			       &format_page_default,
3702			       sizeof(format_page_default));
3703			memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3704			       &format_page_default,
3705			       sizeof(format_page_default));
3706
3707			format_page = &lun->mode_pages.format_page[
3708				CTL_PAGE_CURRENT];
3709			scsi_ulto2b(lun->be_lun->blocksize,
3710				    format_page->bytes_per_sector);
3711
3712			format_page = &lun->mode_pages.format_page[
3713				CTL_PAGE_DEFAULT];
3714			scsi_ulto2b(lun->be_lun->blocksize,
3715				    format_page->bytes_per_sector);
3716
3717			format_page = &lun->mode_pages.format_page[
3718				CTL_PAGE_SAVED];
3719			scsi_ulto2b(lun->be_lun->blocksize,
3720				    format_page->bytes_per_sector);
3721
3722			page_index->page_data =
3723				(uint8_t *)lun->mode_pages.format_page;
3724			break;
3725		}
3726		case SMS_RIGID_DISK_PAGE: {
3727			struct scsi_rigid_disk_page *rigid_disk_page;
3728			uint32_t sectors_per_cylinder;
3729			uint64_t cylinders;
3730#ifndef	__XSCALE__
3731			int shift;
3732#endif /* !__XSCALE__ */
3733
3734			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3735				panic("invalid subpage value %d",
3736				      page_index->subpage);
3737
3738			/*
3739			 * Rotation rate and sectors per track are set
3740			 * above.  We calculate the cylinders here based on
3741			 * capacity.  Due to the number of heads and
3742			 * sectors per track we're using, smaller arrays
3743			 * may turn out to have 0 cylinders.  Linux and
3744			 * FreeBSD don't pay attention to these mode pages
3745			 * to figure out capacity, but Solaris does.  It
3746			 * seems to deal with 0 cylinders just fine, and
3747			 * works out a fake geometry based on the capacity.
3748			 */
3749			memcpy(&lun->mode_pages.rigid_disk_page[
3750			       CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3751			       sizeof(rigid_disk_page_default));
3752			memcpy(&lun->mode_pages.rigid_disk_page[
3753			       CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3754			       sizeof(rigid_disk_page_changeable));
3755
3756			sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3757				CTL_DEFAULT_HEADS;
3758
3759			/*
3760			 * The divide method here will be more accurate,
3761			 * probably, but results in floating point being
3762			 * used in the kernel on i386 (__udivdi3()).  On the
3763			 * XScale, though, __udivdi3() is implemented in
3764			 * software.
3765			 *
3766			 * The shift method for cylinder calculation is
3767			 * accurate if sectors_per_cylinder is a power of
3768			 * 2.  Otherwise it might be slightly off -- you
3769			 * might have a bit of a truncation problem.
3770			 */
3771#ifdef	__XSCALE__
3772			cylinders = (lun->be_lun->maxlba + 1) /
3773				sectors_per_cylinder;
3774#else
3775			for (shift = 31; shift > 0; shift--) {
3776				if (sectors_per_cylinder & (1 << shift))
3777					break;
3778			}
3779			cylinders = (lun->be_lun->maxlba + 1) >> shift;
3780#endif
3781
3782			/*
3783			 * We've basically got 3 bytes, or 24 bits for the
3784			 * cylinder size in the mode page.  If we're over,
3785			 * just round down to 2^24.
3786			 */
3787			if (cylinders > 0xffffff)
3788				cylinders = 0xffffff;
3789
3790			rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3791				CTL_PAGE_DEFAULT];
3792			scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3793
3794			if ((value = ctl_get_opt(&lun->be_lun->options,
3795			    "rpm")) != NULL) {
3796				scsi_ulto2b(strtol(value, NULL, 0),
3797				     rigid_disk_page->rotation_rate);
3798			}
3799
3800			memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
3801			       &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3802			       sizeof(rigid_disk_page_default));
3803			memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
3804			       &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3805			       sizeof(rigid_disk_page_default));
3806
3807			page_index->page_data =
3808				(uint8_t *)lun->mode_pages.rigid_disk_page;
3809			break;
3810		}
3811		case SMS_CACHING_PAGE: {
3812			struct scsi_caching_page *caching_page;
3813
3814			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3815				panic("invalid subpage value %d",
3816				      page_index->subpage);
3817			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3818			       &caching_page_default,
3819			       sizeof(caching_page_default));
3820			memcpy(&lun->mode_pages.caching_page[
3821			       CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3822			       sizeof(caching_page_changeable));
3823			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3824			       &caching_page_default,
3825			       sizeof(caching_page_default));
3826			caching_page = &lun->mode_pages.caching_page[
3827			    CTL_PAGE_SAVED];
3828			value = ctl_get_opt(&lun->be_lun->options, "writecache");
3829			if (value != NULL && strcmp(value, "off") == 0)
3830				caching_page->flags1 &= ~SCP_WCE;
3831			value = ctl_get_opt(&lun->be_lun->options, "readcache");
3832			if (value != NULL && strcmp(value, "off") == 0)
3833				caching_page->flags1 |= SCP_RCD;
3834			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3835			       &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3836			       sizeof(caching_page_default));
3837			page_index->page_data =
3838				(uint8_t *)lun->mode_pages.caching_page;
3839			break;
3840		}
3841		case SMS_CONTROL_MODE_PAGE: {
3842			struct scsi_control_page *control_page;
3843
3844			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3845				panic("invalid subpage value %d",
3846				      page_index->subpage);
3847
3848			memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
3849			       &control_page_default,
3850			       sizeof(control_page_default));
3851			memcpy(&lun->mode_pages.control_page[
3852			       CTL_PAGE_CHANGEABLE], &control_page_changeable,
3853			       sizeof(control_page_changeable));
3854			memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
3855			       &control_page_default,
3856			       sizeof(control_page_default));
3857			control_page = &lun->mode_pages.control_page[
3858			    CTL_PAGE_SAVED];
3859			value = ctl_get_opt(&lun->be_lun->options, "reordering");
3860			if (value != NULL && strcmp(value, "unrestricted") == 0) {
3861				control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
3862				control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
3863			}
3864			memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
3865			       &lun->mode_pages.control_page[CTL_PAGE_SAVED],
3866			       sizeof(control_page_default));
3867			page_index->page_data =
3868				(uint8_t *)lun->mode_pages.control_page;
3869			break;
3870
3871		}
3872		case SMS_INFO_EXCEPTIONS_PAGE: {
3873			switch (page_index->subpage) {
3874			case SMS_SUBPAGE_PAGE_0:
3875				memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
3876				       &ie_page_default,
3877				       sizeof(ie_page_default));
3878				memcpy(&lun->mode_pages.ie_page[
3879				       CTL_PAGE_CHANGEABLE], &ie_page_changeable,
3880				       sizeof(ie_page_changeable));
3881				memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
3882				       &ie_page_default,
3883				       sizeof(ie_page_default));
3884				memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
3885				       &ie_page_default,
3886				       sizeof(ie_page_default));
3887				page_index->page_data =
3888					(uint8_t *)lun->mode_pages.ie_page;
3889				break;
3890			case 0x02: {
3891				struct ctl_logical_block_provisioning_page *page;
3892
3893				memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
3894				       &lbp_page_default,
3895				       sizeof(lbp_page_default));
3896				memcpy(&lun->mode_pages.lbp_page[
3897				       CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
3898				       sizeof(lbp_page_changeable));
3899				memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
3900				       &lbp_page_default,
3901				       sizeof(lbp_page_default));
3902				page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
3903				value = ctl_get_opt(&lun->be_lun->options,
3904				    "avail-threshold");
3905				if (value != NULL &&
3906				    ctl_expand_number(value, &ival) == 0) {
3907					page->descr[0].flags |= SLBPPD_ENABLED |
3908					    SLBPPD_ARMING_DEC;
3909					if (lun->be_lun->blocksize)
3910						ival /= lun->be_lun->blocksize;
3911					else
3912						ival /= 512;
3913					scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3914					    page->descr[0].count);
3915				}
3916				value = ctl_get_opt(&lun->be_lun->options,
3917				    "used-threshold");
3918				if (value != NULL &&
3919				    ctl_expand_number(value, &ival) == 0) {
3920					page->descr[1].flags |= SLBPPD_ENABLED |
3921					    SLBPPD_ARMING_INC;
3922					if (lun->be_lun->blocksize)
3923						ival /= lun->be_lun->blocksize;
3924					else
3925						ival /= 512;
3926					scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3927					    page->descr[1].count);
3928				}
3929				value = ctl_get_opt(&lun->be_lun->options,
3930				    "pool-avail-threshold");
3931				if (value != NULL &&
3932				    ctl_expand_number(value, &ival) == 0) {
3933					page->descr[2].flags |= SLBPPD_ENABLED |
3934					    SLBPPD_ARMING_DEC;
3935					if (lun->be_lun->blocksize)
3936						ival /= lun->be_lun->blocksize;
3937					else
3938						ival /= 512;
3939					scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3940					    page->descr[2].count);
3941				}
3942				value = ctl_get_opt(&lun->be_lun->options,
3943				    "pool-used-threshold");
3944				if (value != NULL &&
3945				    ctl_expand_number(value, &ival) == 0) {
3946					page->descr[3].flags |= SLBPPD_ENABLED |
3947					    SLBPPD_ARMING_INC;
3948					if (lun->be_lun->blocksize)
3949						ival /= lun->be_lun->blocksize;
3950					else
3951						ival /= 512;
3952					scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3953					    page->descr[3].count);
3954				}
3955				memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
3956				       &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
3957				       sizeof(lbp_page_default));
3958				page_index->page_data =
3959					(uint8_t *)lun->mode_pages.lbp_page;
3960			}}
3961			break;
3962		}
3963		case SMS_VENDOR_SPECIFIC_PAGE:{
3964			switch (page_index->subpage) {
3965			case DBGCNF_SUBPAGE_CODE: {
3966				struct copan_debugconf_subpage *current_page,
3967							       *saved_page;
3968
3969				memcpy(&lun->mode_pages.debugconf_subpage[
3970				       CTL_PAGE_CURRENT],
3971				       &debugconf_page_default,
3972				       sizeof(debugconf_page_default));
3973				memcpy(&lun->mode_pages.debugconf_subpage[
3974				       CTL_PAGE_CHANGEABLE],
3975				       &debugconf_page_changeable,
3976				       sizeof(debugconf_page_changeable));
3977				memcpy(&lun->mode_pages.debugconf_subpage[
3978				       CTL_PAGE_DEFAULT],
3979				       &debugconf_page_default,
3980				       sizeof(debugconf_page_default));
3981				memcpy(&lun->mode_pages.debugconf_subpage[
3982				       CTL_PAGE_SAVED],
3983				       &debugconf_page_default,
3984				       sizeof(debugconf_page_default));
3985				page_index->page_data =
3986					(uint8_t *)lun->mode_pages.debugconf_subpage;
3987
3988				current_page = (struct copan_debugconf_subpage *)
3989					(page_index->page_data +
3990					 (page_index->page_len *
3991					  CTL_PAGE_CURRENT));
3992				saved_page = (struct copan_debugconf_subpage *)
3993					(page_index->page_data +
3994					 (page_index->page_len *
3995					  CTL_PAGE_SAVED));
3996				break;
3997			}
3998			default:
3999				panic("invalid subpage value %d",
4000				      page_index->subpage);
4001				break;
4002			}
4003   			break;
4004		}
4005		default:
4006			panic("invalid page value %d",
4007			      page_index->page_code & SMPH_PC_MASK);
4008			break;
4009    	}
4010	}
4011
4012	return (CTL_RETVAL_COMPLETE);
4013}
4014
4015static int
4016ctl_init_log_page_index(struct ctl_lun *lun)
4017{
4018	struct ctl_page_index *page_index;
4019	int i, j, k, prev;
4020
4021	memcpy(&lun->log_pages.index, log_page_index_template,
4022	       sizeof(log_page_index_template));
4023
4024	prev = -1;
4025	for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4026
4027		page_index = &lun->log_pages.index[i];
4028		/*
4029		 * If this is a disk-only mode page, there's no point in
4030		 * setting it up.  For some pages, we have to have some
4031		 * basic information about the disk in order to calculate the
4032		 * mode page data.
4033		 */
4034		if ((lun->be_lun->lun_type != T_DIRECT)
4035		 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4036			continue;
4037
4038		if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4039		     lun->backend->lun_attr == NULL)
4040			continue;
4041
4042		if (page_index->page_code != prev) {
4043			lun->log_pages.pages_page[j] = page_index->page_code;
4044			prev = page_index->page_code;
4045			j++;
4046		}
4047		lun->log_pages.subpages_page[k*2] = page_index->page_code;
4048		lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4049		k++;
4050	}
4051	lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4052	lun->log_pages.index[0].page_len = j;
4053	lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4054	lun->log_pages.index[1].page_len = k * 2;
4055	lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4056	lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4057	lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4058	lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4059
4060	return (CTL_RETVAL_COMPLETE);
4061}
4062
4063static int
4064hex2bin(const char *str, uint8_t *buf, int buf_size)
4065{
4066	int i;
4067	u_char c;
4068
4069	memset(buf, 0, buf_size);
4070	while (isspace(str[0]))
4071		str++;
4072	if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4073		str += 2;
4074	buf_size *= 2;
4075	for (i = 0; str[i] != 0 && i < buf_size; i++) {
4076		c = str[i];
4077		if (isdigit(c))
4078			c -= '0';
4079		else if (isalpha(c))
4080			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4081		else
4082			break;
4083		if (c >= 16)
4084			break;
4085		if ((i & 1) == 0)
4086			buf[i / 2] |= (c << 4);
4087		else
4088			buf[i / 2] |= c;
4089	}
4090	return ((i + 1) / 2);
4091}
4092
4093/*
4094 * LUN allocation.
4095 *
4096 * Requirements:
4097 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4098 *   wants us to allocate the LUN and he can block.
4099 * - ctl_softc is always set
4100 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4101 *
4102 * Returns 0 for success, non-zero (errno) for failure.
4103 */
4104static int
4105ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4106	      struct ctl_be_lun *const be_lun)
4107{
4108	struct ctl_lun *nlun, *lun;
4109	struct scsi_vpd_id_descriptor *desc;
4110	struct scsi_vpd_id_t10 *t10id;
4111	const char *eui, *naa, *scsiname, *vendor;
4112	int lun_number, i, lun_malloced;
4113	int devidlen, idlen1, idlen2 = 0, len;
4114
4115	if (be_lun == NULL)
4116		return (EINVAL);
4117
4118	/*
4119	 * We currently only support Direct Access or Processor LUN types.
4120	 */
4121	switch (be_lun->lun_type) {
4122	case T_DIRECT:
4123		break;
4124	case T_PROCESSOR:
4125		break;
4126	case T_SEQUENTIAL:
4127	case T_CHANGER:
4128	default:
4129		be_lun->lun_config_status(be_lun->be_lun,
4130					  CTL_LUN_CONFIG_FAILURE);
4131		break;
4132	}
4133	if (ctl_lun == NULL) {
4134		lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4135		lun_malloced = 1;
4136	} else {
4137		lun_malloced = 0;
4138		lun = ctl_lun;
4139	}
4140
4141	memset(lun, 0, sizeof(*lun));
4142	if (lun_malloced)
4143		lun->flags = CTL_LUN_MALLOCED;
4144
4145	/* Generate LUN ID. */
4146	devidlen = max(CTL_DEVID_MIN_LEN,
4147	    strnlen(be_lun->device_id, CTL_DEVID_LEN));
4148	idlen1 = sizeof(*t10id) + devidlen;
4149	len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4150	scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4151	if (scsiname != NULL) {
4152		idlen2 = roundup2(strlen(scsiname) + 1, 4);
4153		len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4154	}
4155	eui = ctl_get_opt(&be_lun->options, "eui");
4156	if (eui != NULL) {
4157		len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4158	}
4159	naa = ctl_get_opt(&be_lun->options, "naa");
4160	if (naa != NULL) {
4161		len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4162	}
4163	lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4164	    M_CTL, M_WAITOK | M_ZERO);
4165	desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4166	desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4167	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4168	desc->length = idlen1;
4169	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4170	memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4171	if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4172		strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4173	} else {
4174		strncpy(t10id->vendor, vendor,
4175		    min(sizeof(t10id->vendor), strlen(vendor)));
4176	}
4177	strncpy((char *)t10id->vendor_spec_id,
4178	    (char *)be_lun->device_id, devidlen);
4179	if (scsiname != NULL) {
4180		desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4181		    desc->length);
4182		desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4183		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4184		    SVPD_ID_TYPE_SCSI_NAME;
4185		desc->length = idlen2;
4186		strlcpy(desc->identifier, scsiname, idlen2);
4187	}
4188	if (eui != NULL) {
4189		desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4190		    desc->length);
4191		desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4192		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4193		    SVPD_ID_TYPE_EUI64;
4194		desc->length = hex2bin(eui, desc->identifier, 16);
4195		desc->length = desc->length > 12 ? 16 :
4196		    (desc->length > 8 ? 12 : 8);
4197		len -= 16 - desc->length;
4198	}
4199	if (naa != NULL) {
4200		desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4201		    desc->length);
4202		desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4203		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4204		    SVPD_ID_TYPE_NAA;
4205		desc->length = hex2bin(naa, desc->identifier, 16);
4206		desc->length = desc->length > 8 ? 16 : 8;
4207		len -= 16 - desc->length;
4208	}
4209	lun->lun_devid->len = len;
4210
4211	mtx_lock(&ctl_softc->ctl_lock);
4212	/*
4213	 * See if the caller requested a particular LUN number.  If so, see
4214	 * if it is available.  Otherwise, allocate the first available LUN.
4215	 */
4216	if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4217		if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4218		 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4219			mtx_unlock(&ctl_softc->ctl_lock);
4220			if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4221				printf("ctl: requested LUN ID %d is higher "
4222				       "than CTL_MAX_LUNS - 1 (%d)\n",
4223				       be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4224			} else {
4225				/*
4226				 * XXX KDM return an error, or just assign
4227				 * another LUN ID in this case??
4228				 */
4229				printf("ctl: requested LUN ID %d is already "
4230				       "in use\n", be_lun->req_lun_id);
4231			}
4232			if (lun->flags & CTL_LUN_MALLOCED)
4233				free(lun, M_CTL);
4234			be_lun->lun_config_status(be_lun->be_lun,
4235						  CTL_LUN_CONFIG_FAILURE);
4236			return (ENOSPC);
4237		}
4238		lun_number = be_lun->req_lun_id;
4239	} else {
4240		lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4241		if (lun_number == -1) {
4242			mtx_unlock(&ctl_softc->ctl_lock);
4243			printf("ctl: can't allocate LUN, out of LUNs\n");
4244			if (lun->flags & CTL_LUN_MALLOCED)
4245				free(lun, M_CTL);
4246			be_lun->lun_config_status(be_lun->be_lun,
4247						  CTL_LUN_CONFIG_FAILURE);
4248			return (ENOSPC);
4249		}
4250	}
4251	ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4252
4253	mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4254	lun->lun = lun_number;
4255	lun->be_lun = be_lun;
4256	/*
4257	 * The processor LUN is always enabled.  Disk LUNs come on line
4258	 * disabled, and must be enabled by the backend.
4259	 */
4260	lun->flags |= CTL_LUN_DISABLED;
4261	lun->backend = be_lun->be;
4262	be_lun->ctl_lun = lun;
4263	be_lun->lun_id = lun_number;
4264	atomic_add_int(&be_lun->be->num_luns, 1);
4265	if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4266		lun->flags |= CTL_LUN_OFFLINE;
4267
4268	if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4269		lun->flags |= CTL_LUN_STOPPED;
4270
4271	if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4272		lun->flags |= CTL_LUN_INOPERABLE;
4273
4274	if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4275		lun->flags |= CTL_LUN_PRIMARY_SC;
4276
4277	lun->ctl_softc = ctl_softc;
4278#ifdef CTL_TIME_IO
4279	lun->last_busy = getsbinuptime();
4280#endif
4281	TAILQ_INIT(&lun->ooa_queue);
4282	TAILQ_INIT(&lun->blocked_queue);
4283	STAILQ_INIT(&lun->error_list);
4284	ctl_tpc_lun_init(lun);
4285
4286	/*
4287	 * Initialize the mode and log page index.
4288	 */
4289	ctl_init_page_index(lun);
4290	ctl_init_log_page_index(lun);
4291
4292	/*
4293	 * Now, before we insert this lun on the lun list, set the lun
4294	 * inventory changed UA for all other luns.
4295	 */
4296	STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4297		mtx_lock(&nlun->lun_lock);
4298		ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4299		mtx_unlock(&nlun->lun_lock);
4300	}
4301
4302	STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4303
4304	ctl_softc->ctl_luns[lun_number] = lun;
4305
4306	ctl_softc->num_luns++;
4307
4308	/* Setup statistics gathering */
4309	lun->stats.device_type = be_lun->lun_type;
4310	lun->stats.lun_number = lun_number;
4311	if (lun->stats.device_type == T_DIRECT)
4312		lun->stats.blocksize = be_lun->blocksize;
4313	else
4314		lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4315	for (i = 0;i < CTL_MAX_PORTS;i++)
4316		lun->stats.ports[i].targ_port = i;
4317
4318	mtx_unlock(&ctl_softc->ctl_lock);
4319
4320	lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4321	return (0);
4322}
4323
4324/*
4325 * Delete a LUN.
4326 * Assumptions:
4327 * - LUN has already been marked invalid and any pending I/O has been taken
4328 *   care of.
4329 */
4330static int
4331ctl_free_lun(struct ctl_lun *lun)
4332{
4333	struct ctl_softc *softc;
4334	struct ctl_lun *nlun;
4335	int i;
4336
4337	softc = lun->ctl_softc;
4338
4339	mtx_assert(&softc->ctl_lock, MA_OWNED);
4340
4341	STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4342
4343	ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4344
4345	softc->ctl_luns[lun->lun] = NULL;
4346
4347	if (!TAILQ_EMPTY(&lun->ooa_queue))
4348		panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4349
4350	softc->num_luns--;
4351
4352	/*
4353	 * Tell the backend to free resources, if this LUN has a backend.
4354	 */
4355	atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4356	lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4357
4358	ctl_tpc_lun_shutdown(lun);
4359	mtx_destroy(&lun->lun_lock);
4360	free(lun->lun_devid, M_CTL);
4361	for (i = 0; i < CTL_MAX_PORTS; i++)
4362		free(lun->pending_ua[i], M_CTL);
4363	for (i = 0; i < CTL_MAX_PORTS; i++)
4364		free(lun->pr_keys[i], M_CTL);
4365	free(lun->write_buffer, M_CTL);
4366	if (lun->flags & CTL_LUN_MALLOCED)
4367		free(lun, M_CTL);
4368
4369	STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4370		mtx_lock(&nlun->lun_lock);
4371		ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4372		mtx_unlock(&nlun->lun_lock);
4373	}
4374
4375	return (0);
4376}
4377
4378static void
4379ctl_create_lun(struct ctl_be_lun *be_lun)
4380{
4381	struct ctl_softc *softc;
4382
4383	softc = control_softc;
4384
4385	/*
4386	 * ctl_alloc_lun() should handle all potential failure cases.
4387	 */
4388	ctl_alloc_lun(softc, NULL, be_lun);
4389}
4390
4391int
4392ctl_add_lun(struct ctl_be_lun *be_lun)
4393{
4394	struct ctl_softc *softc = control_softc;
4395
4396	mtx_lock(&softc->ctl_lock);
4397	STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4398	mtx_unlock(&softc->ctl_lock);
4399	wakeup(&softc->pending_lun_queue);
4400
4401	return (0);
4402}
4403
4404int
4405ctl_enable_lun(struct ctl_be_lun *be_lun)
4406{
4407	struct ctl_softc *softc;
4408	struct ctl_port *port, *nport;
4409	struct ctl_lun *lun;
4410	int retval;
4411
4412	lun = (struct ctl_lun *)be_lun->ctl_lun;
4413	softc = lun->ctl_softc;
4414
4415	mtx_lock(&softc->ctl_lock);
4416	mtx_lock(&lun->lun_lock);
4417	if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4418		/*
4419		 * eh?  Why did we get called if the LUN is already
4420		 * enabled?
4421		 */
4422		mtx_unlock(&lun->lun_lock);
4423		mtx_unlock(&softc->ctl_lock);
4424		return (0);
4425	}
4426	lun->flags &= ~CTL_LUN_DISABLED;
4427	mtx_unlock(&lun->lun_lock);
4428
4429	for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4430		nport = STAILQ_NEXT(port, links);
4431		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4432		    port->lun_map != NULL || port->lun_enable == NULL)
4433			continue;
4434
4435		/*
4436		 * Drop the lock while we call the FETD's enable routine.
4437		 * This can lead to a callback into CTL (at least in the
4438		 * case of the internal initiator frontend.
4439		 */
4440		mtx_unlock(&softc->ctl_lock);
4441		retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4442		mtx_lock(&softc->ctl_lock);
4443		if (retval != 0) {
4444			printf("%s: FETD %s port %d returned error "
4445			       "%d for lun_enable on lun %jd\n",
4446			       __func__, port->port_name, port->targ_port,
4447			       retval, (intmax_t)lun->lun);
4448		}
4449	}
4450
4451	mtx_unlock(&softc->ctl_lock);
4452	ctl_isc_announce_lun(lun);
4453
4454	return (0);
4455}
4456
4457int
4458ctl_disable_lun(struct ctl_be_lun *be_lun)
4459{
4460	struct ctl_softc *softc;
4461	struct ctl_port *port;
4462	struct ctl_lun *lun;
4463	int retval;
4464
4465	lun = (struct ctl_lun *)be_lun->ctl_lun;
4466	softc = lun->ctl_softc;
4467
4468	mtx_lock(&softc->ctl_lock);
4469	mtx_lock(&lun->lun_lock);
4470	if (lun->flags & CTL_LUN_DISABLED) {
4471		mtx_unlock(&lun->lun_lock);
4472		mtx_unlock(&softc->ctl_lock);
4473		return (0);
4474	}
4475	lun->flags |= CTL_LUN_DISABLED;
4476	mtx_unlock(&lun->lun_lock);
4477
4478	STAILQ_FOREACH(port, &softc->port_list, links) {
4479		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4480		    port->lun_map != NULL || port->lun_disable == NULL)
4481			continue;
4482
4483		/*
4484		 * Drop the lock before we call the frontend's disable
4485		 * routine, to avoid lock order reversals.
4486		 *
4487		 * XXX KDM what happens if the frontend list changes while
4488		 * we're traversing it?  It's unlikely, but should be handled.
4489		 */
4490		mtx_unlock(&softc->ctl_lock);
4491		retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4492		mtx_lock(&softc->ctl_lock);
4493		if (retval != 0) {
4494			printf("%s: FETD %s port %d returned error "
4495			       "%d for lun_disable on lun %jd\n",
4496			       __func__, port->port_name, port->targ_port,
4497			       retval, (intmax_t)lun->lun);
4498		}
4499	}
4500
4501	mtx_unlock(&softc->ctl_lock);
4502	ctl_isc_announce_lun(lun);
4503
4504	return (0);
4505}
4506
4507int
4508ctl_start_lun(struct ctl_be_lun *be_lun)
4509{
4510	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4511
4512	mtx_lock(&lun->lun_lock);
4513	lun->flags &= ~CTL_LUN_STOPPED;
4514	mtx_unlock(&lun->lun_lock);
4515	return (0);
4516}
4517
4518int
4519ctl_stop_lun(struct ctl_be_lun *be_lun)
4520{
4521	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4522
4523	mtx_lock(&lun->lun_lock);
4524	lun->flags |= CTL_LUN_STOPPED;
4525	mtx_unlock(&lun->lun_lock);
4526	return (0);
4527}
4528
4529int
4530ctl_lun_offline(struct ctl_be_lun *be_lun)
4531{
4532	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4533
4534	mtx_lock(&lun->lun_lock);
4535	lun->flags |= CTL_LUN_OFFLINE;
4536	mtx_unlock(&lun->lun_lock);
4537	return (0);
4538}
4539
4540int
4541ctl_lun_online(struct ctl_be_lun *be_lun)
4542{
4543	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4544
4545	mtx_lock(&lun->lun_lock);
4546	lun->flags &= ~CTL_LUN_OFFLINE;
4547	mtx_unlock(&lun->lun_lock);
4548	return (0);
4549}
4550
4551int
4552ctl_lun_primary(struct ctl_be_lun *be_lun)
4553{
4554	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4555
4556	mtx_lock(&lun->lun_lock);
4557	lun->flags |= CTL_LUN_PRIMARY_SC;
4558	ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4559	mtx_unlock(&lun->lun_lock);
4560	ctl_isc_announce_lun(lun);
4561	return (0);
4562}
4563
4564int
4565ctl_lun_secondary(struct ctl_be_lun *be_lun)
4566{
4567	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4568
4569	mtx_lock(&lun->lun_lock);
4570	lun->flags &= ~CTL_LUN_PRIMARY_SC;
4571	ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4572	mtx_unlock(&lun->lun_lock);
4573	ctl_isc_announce_lun(lun);
4574	return (0);
4575}
4576
4577int
4578ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4579{
4580	struct ctl_softc *softc;
4581	struct ctl_lun *lun;
4582
4583	lun = (struct ctl_lun *)be_lun->ctl_lun;
4584	softc = lun->ctl_softc;
4585
4586	mtx_lock(&lun->lun_lock);
4587
4588	/*
4589	 * The LUN needs to be disabled before it can be marked invalid.
4590	 */
4591	if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4592		mtx_unlock(&lun->lun_lock);
4593		return (-1);
4594	}
4595	/*
4596	 * Mark the LUN invalid.
4597	 */
4598	lun->flags |= CTL_LUN_INVALID;
4599
4600	/*
4601	 * If there is nothing in the OOA queue, go ahead and free the LUN.
4602	 * If we have something in the OOA queue, we'll free it when the
4603	 * last I/O completes.
4604	 */
4605	if (TAILQ_EMPTY(&lun->ooa_queue)) {
4606		mtx_unlock(&lun->lun_lock);
4607		mtx_lock(&softc->ctl_lock);
4608		ctl_free_lun(lun);
4609		mtx_unlock(&softc->ctl_lock);
4610	} else
4611		mtx_unlock(&lun->lun_lock);
4612
4613	return (0);
4614}
4615
4616int
4617ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4618{
4619	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4620
4621	mtx_lock(&lun->lun_lock);
4622	lun->flags |= CTL_LUN_INOPERABLE;
4623	mtx_unlock(&lun->lun_lock);
4624	return (0);
4625}
4626
4627int
4628ctl_lun_operable(struct ctl_be_lun *be_lun)
4629{
4630	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4631
4632	mtx_lock(&lun->lun_lock);
4633	lun->flags &= ~CTL_LUN_INOPERABLE;
4634	mtx_unlock(&lun->lun_lock);
4635	return (0);
4636}
4637
4638void
4639ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4640{
4641	struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4642	union ctl_ha_msg msg;
4643
4644	mtx_lock(&lun->lun_lock);
4645	ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
4646	mtx_unlock(&lun->lun_lock);
4647	if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4648		/* Send msg to other side. */
4649		bzero(&msg.ua, sizeof(msg.ua));
4650		msg.hdr.msg_type = CTL_MSG_UA;
4651		msg.hdr.nexus.initid = -1;
4652		msg.hdr.nexus.targ_port = -1;
4653		msg.hdr.nexus.targ_lun = lun->lun;
4654		msg.hdr.nexus.targ_mapped_lun = lun->lun;
4655		msg.ua.ua_all = 1;
4656		msg.ua.ua_set = 1;
4657		msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED;
4658		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4659		    M_WAITOK);
4660	}
4661}
4662
4663/*
4664 * Backend "memory move is complete" callback for requests that never
4665 * make it down to say RAIDCore's configuration code.
4666 */
4667int
4668ctl_config_move_done(union ctl_io *io)
4669{
4670	int retval;
4671
4672	CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4673	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4674	    ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4675
4676	if ((io->io_hdr.port_status != 0) &&
4677	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4678	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4679		/*
4680		 * For hardware error sense keys, the sense key
4681		 * specific value is defined to be a retry count,
4682		 * but we use it to pass back an internal FETD
4683		 * error code.  XXX KDM  Hopefully the FETD is only
4684		 * using 16 bits for an error code, since that's
4685		 * all the space we have in the sks field.
4686		 */
4687		ctl_set_internal_failure(&io->scsiio,
4688					 /*sks_valid*/ 1,
4689					 /*retry_count*/
4690					 io->io_hdr.port_status);
4691	}
4692
4693	if (ctl_debug & CTL_DEBUG_CDB_DATA)
4694		ctl_data_print(io);
4695	if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4696	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4697	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4698	    ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4699		/*
4700		 * XXX KDM just assuming a single pointer here, and not a
4701		 * S/G list.  If we start using S/G lists for config data,
4702		 * we'll need to know how to clean them up here as well.
4703		 */
4704		if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4705			free(io->scsiio.kern_data_ptr, M_CTL);
4706		ctl_done(io);
4707		retval = CTL_RETVAL_COMPLETE;
4708	} else {
4709		/*
4710		 * XXX KDM now we need to continue data movement.  Some
4711		 * options:
4712		 * - call ctl_scsiio() again?  We don't do this for data
4713		 *   writes, because for those at least we know ahead of
4714		 *   time where the write will go and how long it is.  For
4715		 *   config writes, though, that information is largely
4716		 *   contained within the write itself, thus we need to
4717		 *   parse out the data again.
4718		 *
4719		 * - Call some other function once the data is in?
4720		 */
4721
4722		/*
4723		 * XXX KDM call ctl_scsiio() again for now, and check flag
4724		 * bits to see whether we're allocated or not.
4725		 */
4726		retval = ctl_scsiio(&io->scsiio);
4727	}
4728	return (retval);
4729}
4730
4731/*
4732 * This gets called by a backend driver when it is done with a
4733 * data_submit method.
4734 */
4735void
4736ctl_data_submit_done(union ctl_io *io)
4737{
4738	/*
4739	 * If the IO_CONT flag is set, we need to call the supplied
4740	 * function to continue processing the I/O, instead of completing
4741	 * the I/O just yet.
4742	 *
4743	 * If there is an error, though, we don't want to keep processing.
4744	 * Instead, just send status back to the initiator.
4745	 */
4746	if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4747	    (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4748	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4749	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4750		io->scsiio.io_cont(io);
4751		return;
4752	}
4753	ctl_done(io);
4754}
4755
4756/*
4757 * This gets called by a backend driver when it is done with a
4758 * configuration write.
4759 */
4760void
4761ctl_config_write_done(union ctl_io *io)
4762{
4763	uint8_t *buf;
4764
4765	/*
4766	 * If the IO_CONT flag is set, we need to call the supplied
4767	 * function to continue processing the I/O, instead of completing
4768	 * the I/O just yet.
4769	 *
4770	 * If there is an error, though, we don't want to keep processing.
4771	 * Instead, just send status back to the initiator.
4772	 */
4773	if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4774	    (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4775	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4776	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4777		io->scsiio.io_cont(io);
4778		return;
4779	}
4780	/*
4781	 * Since a configuration write can be done for commands that actually
4782	 * have data allocated, like write buffer, and commands that have
4783	 * no data, like start/stop unit, we need to check here.
4784	 */
4785	if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4786		buf = io->scsiio.kern_data_ptr;
4787	else
4788		buf = NULL;
4789	ctl_done(io);
4790	if (buf)
4791		free(buf, M_CTL);
4792}
4793
4794void
4795ctl_config_read_done(union ctl_io *io)
4796{
4797	uint8_t *buf;
4798
4799	/*
4800	 * If there is some error -- we are done, skip data transfer.
4801	 */
4802	if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
4803	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4804	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
4805		if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4806			buf = io->scsiio.kern_data_ptr;
4807		else
4808			buf = NULL;
4809		ctl_done(io);
4810		if (buf)
4811			free(buf, M_CTL);
4812		return;
4813	}
4814
4815	/*
4816	 * If the IO_CONT flag is set, we need to call the supplied
4817	 * function to continue processing the I/O, instead of completing
4818	 * the I/O just yet.
4819	 */
4820	if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
4821		io->scsiio.io_cont(io);
4822		return;
4823	}
4824
4825	ctl_datamove(io);
4826}
4827
4828/*
4829 * SCSI release command.
4830 */
4831int
4832ctl_scsi_release(struct ctl_scsiio *ctsio)
4833{
4834	int length, longid, thirdparty_id, resv_id;
4835	struct ctl_lun *lun;
4836	uint32_t residx;
4837
4838	length = 0;
4839	resv_id = 0;
4840
4841	CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4842
4843	residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4844	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4845
4846	switch (ctsio->cdb[0]) {
4847	case RELEASE_10: {
4848		struct scsi_release_10 *cdb;
4849
4850		cdb = (struct scsi_release_10 *)ctsio->cdb;
4851
4852		if (cdb->byte2 & SR10_LONGID)
4853			longid = 1;
4854		else
4855			thirdparty_id = cdb->thirdparty_id;
4856
4857		resv_id = cdb->resv_id;
4858		length = scsi_2btoul(cdb->length);
4859		break;
4860	}
4861	}
4862
4863
4864	/*
4865	 * XXX KDM right now, we only support LUN reservation.  We don't
4866	 * support 3rd party reservations, or extent reservations, which
4867	 * might actually need the parameter list.  If we've gotten this
4868	 * far, we've got a LUN reservation.  Anything else got kicked out
4869	 * above.  So, according to SPC, ignore the length.
4870	 */
4871	length = 0;
4872
4873	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4874	 && (length > 0)) {
4875		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4876		ctsio->kern_data_len = length;
4877		ctsio->kern_total_len = length;
4878		ctsio->kern_data_resid = 0;
4879		ctsio->kern_rel_offset = 0;
4880		ctsio->kern_sg_entries = 0;
4881		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4882		ctsio->be_move_done = ctl_config_move_done;
4883		ctl_datamove((union ctl_io *)ctsio);
4884
4885		return (CTL_RETVAL_COMPLETE);
4886	}
4887
4888	if (length > 0)
4889		thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
4890
4891	mtx_lock(&lun->lun_lock);
4892
4893	/*
4894	 * According to SPC, it is not an error for an intiator to attempt
4895	 * to release a reservation on a LUN that isn't reserved, or that
4896	 * is reserved by another initiator.  The reservation can only be
4897	 * released, though, by the initiator who made it or by one of
4898	 * several reset type events.
4899	 */
4900	if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
4901			lun->flags &= ~CTL_LUN_RESERVED;
4902
4903	mtx_unlock(&lun->lun_lock);
4904
4905	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
4906		free(ctsio->kern_data_ptr, M_CTL);
4907		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
4908	}
4909
4910	ctl_set_success(ctsio);
4911	ctl_done((union ctl_io *)ctsio);
4912	return (CTL_RETVAL_COMPLETE);
4913}
4914
4915int
4916ctl_scsi_reserve(struct ctl_scsiio *ctsio)
4917{
4918	int extent, thirdparty, longid;
4919	int resv_id, length;
4920	uint64_t thirdparty_id;
4921	struct ctl_lun *lun;
4922	uint32_t residx;
4923
4924	extent = 0;
4925	thirdparty = 0;
4926	longid = 0;
4927	resv_id = 0;
4928	length = 0;
4929	thirdparty_id = 0;
4930
4931	CTL_DEBUG_PRINT(("ctl_reserve\n"));
4932
4933	residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4934	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4935
4936	switch (ctsio->cdb[0]) {
4937	case RESERVE_10: {
4938		struct scsi_reserve_10 *cdb;
4939
4940		cdb = (struct scsi_reserve_10 *)ctsio->cdb;
4941
4942		if (cdb->byte2 & SR10_LONGID)
4943			longid = 1;
4944		else
4945			thirdparty_id = cdb->thirdparty_id;
4946
4947		resv_id = cdb->resv_id;
4948		length = scsi_2btoul(cdb->length);
4949		break;
4950	}
4951	}
4952
4953	/*
4954	 * XXX KDM right now, we only support LUN reservation.  We don't
4955	 * support 3rd party reservations, or extent reservations, which
4956	 * might actually need the parameter list.  If we've gotten this
4957	 * far, we've got a LUN reservation.  Anything else got kicked out
4958	 * above.  So, according to SPC, ignore the length.
4959	 */
4960	length = 0;
4961
4962	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4963	 && (length > 0)) {
4964		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4965		ctsio->kern_data_len = length;
4966		ctsio->kern_total_len = length;
4967		ctsio->kern_data_resid = 0;
4968		ctsio->kern_rel_offset = 0;
4969		ctsio->kern_sg_entries = 0;
4970		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4971		ctsio->be_move_done = ctl_config_move_done;
4972		ctl_datamove((union ctl_io *)ctsio);
4973
4974		return (CTL_RETVAL_COMPLETE);
4975	}
4976
4977	if (length > 0)
4978		thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
4979
4980	mtx_lock(&lun->lun_lock);
4981	if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
4982		ctl_set_reservation_conflict(ctsio);
4983		goto bailout;
4984	}
4985
4986	lun->flags |= CTL_LUN_RESERVED;
4987	lun->res_idx = residx;
4988
4989	ctl_set_success(ctsio);
4990
4991bailout:
4992	mtx_unlock(&lun->lun_lock);
4993
4994	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
4995		free(ctsio->kern_data_ptr, M_CTL);
4996		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
4997	}
4998
4999	ctl_done((union ctl_io *)ctsio);
5000	return (CTL_RETVAL_COMPLETE);
5001}
5002
5003int
5004ctl_start_stop(struct ctl_scsiio *ctsio)
5005{
5006	struct scsi_start_stop_unit *cdb;
5007	struct ctl_lun *lun;
5008	int retval;
5009
5010	CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5011
5012	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5013	retval = 0;
5014
5015	cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5016
5017	/*
5018	 * XXX KDM
5019	 * We don't support the immediate bit on a stop unit.  In order to
5020	 * do that, we would need to code up a way to know that a stop is
5021	 * pending, and hold off any new commands until it completes, one
5022	 * way or another.  Then we could accept or reject those commands
5023	 * depending on its status.  We would almost need to do the reverse
5024	 * of what we do below for an immediate start -- return the copy of
5025	 * the ctl_io to the FETD with status to send to the host (and to
5026	 * free the copy!) and then free the original I/O once the stop
5027	 * actually completes.  That way, the OOA queue mechanism can work
5028	 * to block commands that shouldn't proceed.  Another alternative
5029	 * would be to put the copy in the queue in place of the original,
5030	 * and return the original back to the caller.  That could be
5031	 * slightly safer..
5032	 */
5033	if ((cdb->byte2 & SSS_IMMED)
5034	 && ((cdb->how & SSS_START) == 0)) {
5035		ctl_set_invalid_field(ctsio,
5036				      /*sks_valid*/ 1,
5037				      /*command*/ 1,
5038				      /*field*/ 1,
5039				      /*bit_valid*/ 1,
5040				      /*bit*/ 0);
5041		ctl_done((union ctl_io *)ctsio);
5042		return (CTL_RETVAL_COMPLETE);
5043	}
5044
5045	if ((lun->flags & CTL_LUN_PR_RESERVED)
5046	 && ((cdb->how & SSS_START)==0)) {
5047		uint32_t residx;
5048
5049		residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5050		if (ctl_get_prkey(lun, residx) == 0
5051		 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5052
5053			ctl_set_reservation_conflict(ctsio);
5054			ctl_done((union ctl_io *)ctsio);
5055			return (CTL_RETVAL_COMPLETE);
5056		}
5057	}
5058
5059	/*
5060	 * If there is no backend on this device, we can't start or stop
5061	 * it.  In theory we shouldn't get any start/stop commands in the
5062	 * first place at this level if the LUN doesn't have a backend.
5063	 * That should get stopped by the command decode code.
5064	 */
5065	if (lun->backend == NULL) {
5066		ctl_set_invalid_opcode(ctsio);
5067		ctl_done((union ctl_io *)ctsio);
5068		return (CTL_RETVAL_COMPLETE);
5069	}
5070
5071	/*
5072	 * XXX KDM Copan-specific offline behavior.
5073	 * Figure out a reasonable way to port this?
5074	 */
5075#ifdef NEEDTOPORT
5076	mtx_lock(&lun->lun_lock);
5077
5078	if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5079	 && (lun->flags & CTL_LUN_OFFLINE)) {
5080		/*
5081		 * If the LUN is offline, and the on/offline bit isn't set,
5082		 * reject the start or stop.  Otherwise, let it through.
5083		 */
5084		mtx_unlock(&lun->lun_lock);
5085		ctl_set_lun_not_ready(ctsio);
5086		ctl_done((union ctl_io *)ctsio);
5087	} else {
5088		mtx_unlock(&lun->lun_lock);
5089#endif /* NEEDTOPORT */
5090		/*
5091		 * This could be a start or a stop when we're online,
5092		 * or a stop/offline or start/online.  A start or stop when
5093		 * we're offline is covered in the case above.
5094		 */
5095		/*
5096		 * In the non-immediate case, we send the request to
5097		 * the backend and return status to the user when
5098		 * it is done.
5099		 *
5100		 * In the immediate case, we allocate a new ctl_io
5101		 * to hold a copy of the request, and send that to
5102		 * the backend.  We then set good status on the
5103		 * user's request and return it immediately.
5104		 */
5105		if (cdb->byte2 & SSS_IMMED) {
5106			union ctl_io *new_io;
5107
5108			new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5109			ctl_copy_io((union ctl_io *)ctsio, new_io);
5110			retval = lun->backend->config_write(new_io);
5111			ctl_set_success(ctsio);
5112			ctl_done((union ctl_io *)ctsio);
5113		} else {
5114			retval = lun->backend->config_write(
5115				(union ctl_io *)ctsio);
5116		}
5117#ifdef NEEDTOPORT
5118	}
5119#endif
5120	return (retval);
5121}
5122
5123/*
5124 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5125 * we don't really do anything with the LBA and length fields if the user
5126 * passes them in.  Instead we'll just flush out the cache for the entire
5127 * LUN.
5128 */
5129int
5130ctl_sync_cache(struct ctl_scsiio *ctsio)
5131{
5132	struct ctl_lun *lun;
5133	struct ctl_softc *softc;
5134	struct ctl_lba_len_flags *lbalen;
5135	uint64_t starting_lba;
5136	uint32_t block_count;
5137	int retval;
5138	uint8_t byte2;
5139
5140	CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5141
5142	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5143	softc = lun->ctl_softc;
5144	retval = 0;
5145
5146	switch (ctsio->cdb[0]) {
5147	case SYNCHRONIZE_CACHE: {
5148		struct scsi_sync_cache *cdb;
5149		cdb = (struct scsi_sync_cache *)ctsio->cdb;
5150
5151		starting_lba = scsi_4btoul(cdb->begin_lba);
5152		block_count = scsi_2btoul(cdb->lb_count);
5153		byte2 = cdb->byte2;
5154		break;
5155	}
5156	case SYNCHRONIZE_CACHE_16: {
5157		struct scsi_sync_cache_16 *cdb;
5158		cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5159
5160		starting_lba = scsi_8btou64(cdb->begin_lba);
5161		block_count = scsi_4btoul(cdb->lb_count);
5162		byte2 = cdb->byte2;
5163		break;
5164	}
5165	default:
5166		ctl_set_invalid_opcode(ctsio);
5167		ctl_done((union ctl_io *)ctsio);
5168		goto bailout;
5169		break; /* NOTREACHED */
5170	}
5171
5172	/*
5173	 * We check the LBA and length, but don't do anything with them.
5174	 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5175	 * get flushed.  This check will just help satisfy anyone who wants
5176	 * to see an error for an out of range LBA.
5177	 */
5178	if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5179		ctl_set_lba_out_of_range(ctsio);
5180		ctl_done((union ctl_io *)ctsio);
5181		goto bailout;
5182	}
5183
5184	/*
5185	 * If this LUN has no backend, we can't flush the cache anyway.
5186	 */
5187	if (lun->backend == NULL) {
5188		ctl_set_invalid_opcode(ctsio);
5189		ctl_done((union ctl_io *)ctsio);
5190		goto bailout;
5191	}
5192
5193	lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5194	lbalen->lba = starting_lba;
5195	lbalen->len = block_count;
5196	lbalen->flags = byte2;
5197
5198	/*
5199	 * Check to see whether we're configured to send the SYNCHRONIZE
5200	 * CACHE command directly to the back end.
5201	 */
5202	mtx_lock(&lun->lun_lock);
5203	if ((softc->flags & CTL_FLAG_REAL_SYNC)
5204	 && (++(lun->sync_count) >= lun->sync_interval)) {
5205		lun->sync_count = 0;
5206		mtx_unlock(&lun->lun_lock);
5207		retval = lun->backend->config_write((union ctl_io *)ctsio);
5208	} else {
5209		mtx_unlock(&lun->lun_lock);
5210		ctl_set_success(ctsio);
5211		ctl_done((union ctl_io *)ctsio);
5212	}
5213
5214bailout:
5215
5216	return (retval);
5217}
5218
5219int
5220ctl_format(struct ctl_scsiio *ctsio)
5221{
5222	struct scsi_format *cdb;
5223	struct ctl_lun *lun;
5224	int length, defect_list_len;
5225
5226	CTL_DEBUG_PRINT(("ctl_format\n"));
5227
5228	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5229
5230	cdb = (struct scsi_format *)ctsio->cdb;
5231
5232	length = 0;
5233	if (cdb->byte2 & SF_FMTDATA) {
5234		if (cdb->byte2 & SF_LONGLIST)
5235			length = sizeof(struct scsi_format_header_long);
5236		else
5237			length = sizeof(struct scsi_format_header_short);
5238	}
5239
5240	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5241	 && (length > 0)) {
5242		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5243		ctsio->kern_data_len = length;
5244		ctsio->kern_total_len = length;
5245		ctsio->kern_data_resid = 0;
5246		ctsio->kern_rel_offset = 0;
5247		ctsio->kern_sg_entries = 0;
5248		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5249		ctsio->be_move_done = ctl_config_move_done;
5250		ctl_datamove((union ctl_io *)ctsio);
5251
5252		return (CTL_RETVAL_COMPLETE);
5253	}
5254
5255	defect_list_len = 0;
5256
5257	if (cdb->byte2 & SF_FMTDATA) {
5258		if (cdb->byte2 & SF_LONGLIST) {
5259			struct scsi_format_header_long *header;
5260
5261			header = (struct scsi_format_header_long *)
5262				ctsio->kern_data_ptr;
5263
5264			defect_list_len = scsi_4btoul(header->defect_list_len);
5265			if (defect_list_len != 0) {
5266				ctl_set_invalid_field(ctsio,
5267						      /*sks_valid*/ 1,
5268						      /*command*/ 0,
5269						      /*field*/ 2,
5270						      /*bit_valid*/ 0,
5271						      /*bit*/ 0);
5272				goto bailout;
5273			}
5274		} else {
5275			struct scsi_format_header_short *header;
5276
5277			header = (struct scsi_format_header_short *)
5278				ctsio->kern_data_ptr;
5279
5280			defect_list_len = scsi_2btoul(header->defect_list_len);
5281			if (defect_list_len != 0) {
5282				ctl_set_invalid_field(ctsio,
5283						      /*sks_valid*/ 1,
5284						      /*command*/ 0,
5285						      /*field*/ 2,
5286						      /*bit_valid*/ 0,
5287						      /*bit*/ 0);
5288				goto bailout;
5289			}
5290		}
5291	}
5292
5293	/*
5294	 * The format command will clear out the "Medium format corrupted"
5295	 * status if set by the configuration code.  That status is really
5296	 * just a way to notify the host that we have lost the media, and
5297	 * get them to issue a command that will basically make them think
5298	 * they're blowing away the media.
5299	 */
5300	mtx_lock(&lun->lun_lock);
5301	lun->flags &= ~CTL_LUN_INOPERABLE;
5302	mtx_unlock(&lun->lun_lock);
5303
5304	ctl_set_success(ctsio);
5305bailout:
5306
5307	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5308		free(ctsio->kern_data_ptr, M_CTL);
5309		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5310	}
5311
5312	ctl_done((union ctl_io *)ctsio);
5313	return (CTL_RETVAL_COMPLETE);
5314}
5315
5316int
5317ctl_read_buffer(struct ctl_scsiio *ctsio)
5318{
5319	struct scsi_read_buffer *cdb;
5320	struct ctl_lun *lun;
5321	int buffer_offset, len;
5322	static uint8_t descr[4];
5323	static uint8_t echo_descr[4] = { 0 };
5324
5325	CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5326
5327	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5328	cdb = (struct scsi_read_buffer *)ctsio->cdb;
5329
5330	if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5331	    (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5332	    (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5333		ctl_set_invalid_field(ctsio,
5334				      /*sks_valid*/ 1,
5335				      /*command*/ 1,
5336				      /*field*/ 1,
5337				      /*bit_valid*/ 1,
5338				      /*bit*/ 4);
5339		ctl_done((union ctl_io *)ctsio);
5340		return (CTL_RETVAL_COMPLETE);
5341	}
5342
5343	len = scsi_3btoul(cdb->length);
5344	buffer_offset = scsi_3btoul(cdb->offset);
5345
5346	if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5347		ctl_set_invalid_field(ctsio,
5348				      /*sks_valid*/ 1,
5349				      /*command*/ 1,
5350				      /*field*/ 6,
5351				      /*bit_valid*/ 0,
5352				      /*bit*/ 0);
5353		ctl_done((union ctl_io *)ctsio);
5354		return (CTL_RETVAL_COMPLETE);
5355	}
5356
5357	if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5358		descr[0] = 0;
5359		scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5360		ctsio->kern_data_ptr = descr;
5361		len = min(len, sizeof(descr));
5362	} else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5363		ctsio->kern_data_ptr = echo_descr;
5364		len = min(len, sizeof(echo_descr));
5365	} else {
5366		if (lun->write_buffer == NULL) {
5367			lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5368			    M_CTL, M_WAITOK);
5369		}
5370		ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5371	}
5372	ctsio->kern_data_len = len;
5373	ctsio->kern_total_len = len;
5374	ctsio->kern_data_resid = 0;
5375	ctsio->kern_rel_offset = 0;
5376	ctsio->kern_sg_entries = 0;
5377	ctl_set_success(ctsio);
5378	ctsio->be_move_done = ctl_config_move_done;
5379	ctl_datamove((union ctl_io *)ctsio);
5380	return (CTL_RETVAL_COMPLETE);
5381}
5382
5383int
5384ctl_write_buffer(struct ctl_scsiio *ctsio)
5385{
5386	struct scsi_write_buffer *cdb;
5387	struct ctl_lun *lun;
5388	int buffer_offset, len;
5389
5390	CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5391
5392	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5393	cdb = (struct scsi_write_buffer *)ctsio->cdb;
5394
5395	if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5396		ctl_set_invalid_field(ctsio,
5397				      /*sks_valid*/ 1,
5398				      /*command*/ 1,
5399				      /*field*/ 1,
5400				      /*bit_valid*/ 1,
5401				      /*bit*/ 4);
5402		ctl_done((union ctl_io *)ctsio);
5403		return (CTL_RETVAL_COMPLETE);
5404	}
5405
5406	len = scsi_3btoul(cdb->length);
5407	buffer_offset = scsi_3btoul(cdb->offset);
5408
5409	if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5410		ctl_set_invalid_field(ctsio,
5411				      /*sks_valid*/ 1,
5412				      /*command*/ 1,
5413				      /*field*/ 6,
5414				      /*bit_valid*/ 0,
5415				      /*bit*/ 0);
5416		ctl_done((union ctl_io *)ctsio);
5417		return (CTL_RETVAL_COMPLETE);
5418	}
5419
5420	/*
5421	 * If we've got a kernel request that hasn't been malloced yet,
5422	 * malloc it and tell the caller the data buffer is here.
5423	 */
5424	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5425		if (lun->write_buffer == NULL) {
5426			lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5427			    M_CTL, M_WAITOK);
5428		}
5429		ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5430		ctsio->kern_data_len = len;
5431		ctsio->kern_total_len = len;
5432		ctsio->kern_data_resid = 0;
5433		ctsio->kern_rel_offset = 0;
5434		ctsio->kern_sg_entries = 0;
5435		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5436		ctsio->be_move_done = ctl_config_move_done;
5437		ctl_datamove((union ctl_io *)ctsio);
5438
5439		return (CTL_RETVAL_COMPLETE);
5440	}
5441
5442	ctl_set_success(ctsio);
5443	ctl_done((union ctl_io *)ctsio);
5444	return (CTL_RETVAL_COMPLETE);
5445}
5446
5447int
5448ctl_write_same(struct ctl_scsiio *ctsio)
5449{
5450	struct ctl_lun *lun;
5451	struct ctl_lba_len_flags *lbalen;
5452	uint64_t lba;
5453	uint32_t num_blocks;
5454	int len, retval;
5455	uint8_t byte2;
5456
5457	retval = CTL_RETVAL_COMPLETE;
5458
5459	CTL_DEBUG_PRINT(("ctl_write_same\n"));
5460
5461	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5462
5463	switch (ctsio->cdb[0]) {
5464	case WRITE_SAME_10: {
5465		struct scsi_write_same_10 *cdb;
5466
5467		cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5468
5469		lba = scsi_4btoul(cdb->addr);
5470		num_blocks = scsi_2btoul(cdb->length);
5471		byte2 = cdb->byte2;
5472		break;
5473	}
5474	case WRITE_SAME_16: {
5475		struct scsi_write_same_16 *cdb;
5476
5477		cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5478
5479		lba = scsi_8btou64(cdb->addr);
5480		num_blocks = scsi_4btoul(cdb->length);
5481		byte2 = cdb->byte2;
5482		break;
5483	}
5484	default:
5485		/*
5486		 * We got a command we don't support.  This shouldn't
5487		 * happen, commands should be filtered out above us.
5488		 */
5489		ctl_set_invalid_opcode(ctsio);
5490		ctl_done((union ctl_io *)ctsio);
5491
5492		return (CTL_RETVAL_COMPLETE);
5493		break; /* NOTREACHED */
5494	}
5495
5496	/* NDOB and ANCHOR flags can be used only together with UNMAP */
5497	if ((byte2 & SWS_UNMAP) == 0 &&
5498	    (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5499		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5500		    /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5501		ctl_done((union ctl_io *)ctsio);
5502		return (CTL_RETVAL_COMPLETE);
5503	}
5504
5505	/*
5506	 * The first check is to make sure we're in bounds, the second
5507	 * check is to catch wrap-around problems.  If the lba + num blocks
5508	 * is less than the lba, then we've wrapped around and the block
5509	 * range is invalid anyway.
5510	 */
5511	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5512	 || ((lba + num_blocks) < lba)) {
5513		ctl_set_lba_out_of_range(ctsio);
5514		ctl_done((union ctl_io *)ctsio);
5515		return (CTL_RETVAL_COMPLETE);
5516	}
5517
5518	/* Zero number of blocks means "to the last logical block" */
5519	if (num_blocks == 0) {
5520		if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5521			ctl_set_invalid_field(ctsio,
5522					      /*sks_valid*/ 0,
5523					      /*command*/ 1,
5524					      /*field*/ 0,
5525					      /*bit_valid*/ 0,
5526					      /*bit*/ 0);
5527			ctl_done((union ctl_io *)ctsio);
5528			return (CTL_RETVAL_COMPLETE);
5529		}
5530		num_blocks = (lun->be_lun->maxlba + 1) - lba;
5531	}
5532
5533	len = lun->be_lun->blocksize;
5534
5535	/*
5536	 * If we've got a kernel request that hasn't been malloced yet,
5537	 * malloc it and tell the caller the data buffer is here.
5538	 */
5539	if ((byte2 & SWS_NDOB) == 0 &&
5540	    (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5541		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5542		ctsio->kern_data_len = len;
5543		ctsio->kern_total_len = len;
5544		ctsio->kern_data_resid = 0;
5545		ctsio->kern_rel_offset = 0;
5546		ctsio->kern_sg_entries = 0;
5547		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5548		ctsio->be_move_done = ctl_config_move_done;
5549		ctl_datamove((union ctl_io *)ctsio);
5550
5551		return (CTL_RETVAL_COMPLETE);
5552	}
5553
5554	lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5555	lbalen->lba = lba;
5556	lbalen->len = num_blocks;
5557	lbalen->flags = byte2;
5558	retval = lun->backend->config_write((union ctl_io *)ctsio);
5559
5560	return (retval);
5561}
5562
5563int
5564ctl_unmap(struct ctl_scsiio *ctsio)
5565{
5566	struct ctl_lun *lun;
5567	struct scsi_unmap *cdb;
5568	struct ctl_ptr_len_flags *ptrlen;
5569	struct scsi_unmap_header *hdr;
5570	struct scsi_unmap_desc *buf, *end, *endnz, *range;
5571	uint64_t lba;
5572	uint32_t num_blocks;
5573	int len, retval;
5574	uint8_t byte2;
5575
5576	retval = CTL_RETVAL_COMPLETE;
5577
5578	CTL_DEBUG_PRINT(("ctl_unmap\n"));
5579
5580	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5581	cdb = (struct scsi_unmap *)ctsio->cdb;
5582
5583	len = scsi_2btoul(cdb->length);
5584	byte2 = cdb->byte2;
5585
5586	/*
5587	 * If we've got a kernel request that hasn't been malloced yet,
5588	 * malloc it and tell the caller the data buffer is here.
5589	 */
5590	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5591		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5592		ctsio->kern_data_len = len;
5593		ctsio->kern_total_len = len;
5594		ctsio->kern_data_resid = 0;
5595		ctsio->kern_rel_offset = 0;
5596		ctsio->kern_sg_entries = 0;
5597		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5598		ctsio->be_move_done = ctl_config_move_done;
5599		ctl_datamove((union ctl_io *)ctsio);
5600
5601		return (CTL_RETVAL_COMPLETE);
5602	}
5603
5604	len = ctsio->kern_total_len - ctsio->kern_data_resid;
5605	hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5606	if (len < sizeof (*hdr) ||
5607	    len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5608	    len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5609	    scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5610		ctl_set_invalid_field(ctsio,
5611				      /*sks_valid*/ 0,
5612				      /*command*/ 0,
5613				      /*field*/ 0,
5614				      /*bit_valid*/ 0,
5615				      /*bit*/ 0);
5616		goto done;
5617	}
5618	len = scsi_2btoul(hdr->desc_length);
5619	buf = (struct scsi_unmap_desc *)(hdr + 1);
5620	end = buf + len / sizeof(*buf);
5621
5622	endnz = buf;
5623	for (range = buf; range < end; range++) {
5624		lba = scsi_8btou64(range->lba);
5625		num_blocks = scsi_4btoul(range->length);
5626		if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5627		 || ((lba + num_blocks) < lba)) {
5628			ctl_set_lba_out_of_range(ctsio);
5629			ctl_done((union ctl_io *)ctsio);
5630			return (CTL_RETVAL_COMPLETE);
5631		}
5632		if (num_blocks != 0)
5633			endnz = range + 1;
5634	}
5635
5636	/*
5637	 * Block backend can not handle zero last range.
5638	 * Filter it out and return if there is nothing left.
5639	 */
5640	len = (uint8_t *)endnz - (uint8_t *)buf;
5641	if (len == 0) {
5642		ctl_set_success(ctsio);
5643		goto done;
5644	}
5645
5646	mtx_lock(&lun->lun_lock);
5647	ptrlen = (struct ctl_ptr_len_flags *)
5648	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5649	ptrlen->ptr = (void *)buf;
5650	ptrlen->len = len;
5651	ptrlen->flags = byte2;
5652	ctl_check_blocked(lun);
5653	mtx_unlock(&lun->lun_lock);
5654
5655	retval = lun->backend->config_write((union ctl_io *)ctsio);
5656	return (retval);
5657
5658done:
5659	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5660		free(ctsio->kern_data_ptr, M_CTL);
5661		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5662	}
5663	ctl_done((union ctl_io *)ctsio);
5664	return (CTL_RETVAL_COMPLETE);
5665}
5666
5667/*
5668 * Note that this function currently doesn't actually do anything inside
5669 * CTL to enforce things if the DQue bit is turned on.
5670 *
5671 * Also note that this function can't be used in the default case, because
5672 * the DQue bit isn't set in the changeable mask for the control mode page
5673 * anyway.  This is just here as an example for how to implement a page
5674 * handler, and a placeholder in case we want to allow the user to turn
5675 * tagged queueing on and off.
5676 *
5677 * The D_SENSE bit handling is functional, however, and will turn
5678 * descriptor sense on and off for a given LUN.
5679 */
5680int
5681ctl_control_page_handler(struct ctl_scsiio *ctsio,
5682			 struct ctl_page_index *page_index, uint8_t *page_ptr)
5683{
5684	struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5685	struct ctl_lun *lun;
5686	int set_ua;
5687	uint32_t initidx;
5688
5689	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5690	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5691	set_ua = 0;
5692
5693	user_cp = (struct scsi_control_page *)page_ptr;
5694	current_cp = (struct scsi_control_page *)
5695		(page_index->page_data + (page_index->page_len *
5696		CTL_PAGE_CURRENT));
5697	saved_cp = (struct scsi_control_page *)
5698		(page_index->page_data + (page_index->page_len *
5699		CTL_PAGE_SAVED));
5700
5701	mtx_lock(&lun->lun_lock);
5702	if (((current_cp->rlec & SCP_DSENSE) == 0)
5703	 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5704		/*
5705		 * Descriptor sense is currently turned off and the user
5706		 * wants to turn it on.
5707		 */
5708		current_cp->rlec |= SCP_DSENSE;
5709		saved_cp->rlec |= SCP_DSENSE;
5710		lun->flags |= CTL_LUN_SENSE_DESC;
5711		set_ua = 1;
5712	} else if (((current_cp->rlec & SCP_DSENSE) != 0)
5713		&& ((user_cp->rlec & SCP_DSENSE) == 0)) {
5714		/*
5715		 * Descriptor sense is currently turned on, and the user
5716		 * wants to turn it off.
5717		 */
5718		current_cp->rlec &= ~SCP_DSENSE;
5719		saved_cp->rlec &= ~SCP_DSENSE;
5720		lun->flags &= ~CTL_LUN_SENSE_DESC;
5721		set_ua = 1;
5722	}
5723	if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5724	    (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5725		current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5726		current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5727		saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5728		saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5729		set_ua = 1;
5730	}
5731	if ((current_cp->eca_and_aen & SCP_SWP) !=
5732	    (user_cp->eca_and_aen & SCP_SWP)) {
5733		current_cp->eca_and_aen &= ~SCP_SWP;
5734		current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5735		saved_cp->eca_and_aen &= ~SCP_SWP;
5736		saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5737		set_ua = 1;
5738	}
5739	if (set_ua != 0)
5740		ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5741	mtx_unlock(&lun->lun_lock);
5742
5743	return (0);
5744}
5745
5746int
5747ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5748		     struct ctl_page_index *page_index, uint8_t *page_ptr)
5749{
5750	struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5751	struct ctl_lun *lun;
5752	int set_ua;
5753	uint32_t initidx;
5754
5755	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5756	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5757	set_ua = 0;
5758
5759	user_cp = (struct scsi_caching_page *)page_ptr;
5760	current_cp = (struct scsi_caching_page *)
5761		(page_index->page_data + (page_index->page_len *
5762		CTL_PAGE_CURRENT));
5763	saved_cp = (struct scsi_caching_page *)
5764		(page_index->page_data + (page_index->page_len *
5765		CTL_PAGE_SAVED));
5766
5767	mtx_lock(&lun->lun_lock);
5768	if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5769	    (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5770		current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5771		current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5772		saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5773		saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5774		set_ua = 1;
5775	}
5776	if (set_ua != 0)
5777		ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5778	mtx_unlock(&lun->lun_lock);
5779
5780	return (0);
5781}
5782
5783int
5784ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5785				struct ctl_page_index *page_index,
5786				uint8_t *page_ptr)
5787{
5788	uint8_t *c;
5789	int i;
5790
5791	c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5792	ctl_time_io_secs =
5793		(c[0] << 8) |
5794		(c[1] << 0) |
5795		0;
5796	CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5797	printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5798	printf("page data:");
5799	for (i=0; i<8; i++)
5800		printf(" %.2x",page_ptr[i]);
5801	printf("\n");
5802	return (0);
5803}
5804
5805int
5806ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5807			       struct ctl_page_index *page_index,
5808			       int pc)
5809{
5810	struct copan_debugconf_subpage *page;
5811
5812	page = (struct copan_debugconf_subpage *)page_index->page_data +
5813		(page_index->page_len * pc);
5814
5815	switch (pc) {
5816	case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5817	case SMS_PAGE_CTRL_DEFAULT >> 6:
5818	case SMS_PAGE_CTRL_SAVED >> 6:
5819		/*
5820		 * We don't update the changable or default bits for this page.
5821		 */
5822		break;
5823	case SMS_PAGE_CTRL_CURRENT >> 6:
5824		page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5825		page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5826		break;
5827	default:
5828#ifdef NEEDTOPORT
5829		EPRINT(0, "Invalid PC %d!!", pc);
5830#endif /* NEEDTOPORT */
5831		break;
5832	}
5833	return (0);
5834}
5835
5836
5837static int
5838ctl_do_mode_select(union ctl_io *io)
5839{
5840	struct scsi_mode_page_header *page_header;
5841	struct ctl_page_index *page_index;
5842	struct ctl_scsiio *ctsio;
5843	int control_dev, page_len;
5844	int page_len_offset, page_len_size;
5845	union ctl_modepage_info *modepage_info;
5846	struct ctl_lun *lun;
5847	int *len_left, *len_used;
5848	int retval, i;
5849
5850	ctsio = &io->scsiio;
5851	page_index = NULL;
5852	page_len = 0;
5853	retval = CTL_RETVAL_COMPLETE;
5854
5855	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5856
5857	if (lun->be_lun->lun_type != T_DIRECT)
5858		control_dev = 1;
5859	else
5860		control_dev = 0;
5861
5862	modepage_info = (union ctl_modepage_info *)
5863		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5864	len_left = &modepage_info->header.len_left;
5865	len_used = &modepage_info->header.len_used;
5866
5867do_next_page:
5868
5869	page_header = (struct scsi_mode_page_header *)
5870		(ctsio->kern_data_ptr + *len_used);
5871
5872	if (*len_left == 0) {
5873		free(ctsio->kern_data_ptr, M_CTL);
5874		ctl_set_success(ctsio);
5875		ctl_done((union ctl_io *)ctsio);
5876		return (CTL_RETVAL_COMPLETE);
5877	} else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5878
5879		free(ctsio->kern_data_ptr, M_CTL);
5880		ctl_set_param_len_error(ctsio);
5881		ctl_done((union ctl_io *)ctsio);
5882		return (CTL_RETVAL_COMPLETE);
5883
5884	} else if ((page_header->page_code & SMPH_SPF)
5885		&& (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5886
5887		free(ctsio->kern_data_ptr, M_CTL);
5888		ctl_set_param_len_error(ctsio);
5889		ctl_done((union ctl_io *)ctsio);
5890		return (CTL_RETVAL_COMPLETE);
5891	}
5892
5893
5894	/*
5895	 * XXX KDM should we do something with the block descriptor?
5896	 */
5897	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5898
5899		if ((control_dev != 0)
5900		 && (lun->mode_pages.index[i].page_flags &
5901		     CTL_PAGE_FLAG_DISK_ONLY))
5902			continue;
5903
5904		if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5905		    (page_header->page_code & SMPH_PC_MASK))
5906			continue;
5907
5908		/*
5909		 * If neither page has a subpage code, then we've got a
5910		 * match.
5911		 */
5912		if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
5913		 && ((page_header->page_code & SMPH_SPF) == 0)) {
5914			page_index = &lun->mode_pages.index[i];
5915			page_len = page_header->page_length;
5916			break;
5917		}
5918
5919		/*
5920		 * If both pages have subpages, then the subpage numbers
5921		 * have to match.
5922		 */
5923		if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
5924		  && (page_header->page_code & SMPH_SPF)) {
5925			struct scsi_mode_page_header_sp *sph;
5926
5927			sph = (struct scsi_mode_page_header_sp *)page_header;
5928
5929			if (lun->mode_pages.index[i].subpage ==
5930			    sph->subpage) {
5931				page_index = &lun->mode_pages.index[i];
5932				page_len = scsi_2btoul(sph->page_length);
5933				break;
5934			}
5935		}
5936	}
5937
5938	/*
5939	 * If we couldn't find the page, or if we don't have a mode select
5940	 * handler for it, send back an error to the user.
5941	 */
5942	if ((page_index == NULL)
5943	 || (page_index->select_handler == NULL)) {
5944		ctl_set_invalid_field(ctsio,
5945				      /*sks_valid*/ 1,
5946				      /*command*/ 0,
5947				      /*field*/ *len_used,
5948				      /*bit_valid*/ 0,
5949				      /*bit*/ 0);
5950		free(ctsio->kern_data_ptr, M_CTL);
5951		ctl_done((union ctl_io *)ctsio);
5952		return (CTL_RETVAL_COMPLETE);
5953	}
5954
5955	if (page_index->page_code & SMPH_SPF) {
5956		page_len_offset = 2;
5957		page_len_size = 2;
5958	} else {
5959		page_len_size = 1;
5960		page_len_offset = 1;
5961	}
5962
5963	/*
5964	 * If the length the initiator gives us isn't the one we specify in
5965	 * the mode page header, or if they didn't specify enough data in
5966	 * the CDB to avoid truncating this page, kick out the request.
5967	 */
5968	if ((page_len != (page_index->page_len - page_len_offset -
5969			  page_len_size))
5970	 || (*len_left < page_index->page_len)) {
5971
5972
5973		ctl_set_invalid_field(ctsio,
5974				      /*sks_valid*/ 1,
5975				      /*command*/ 0,
5976				      /*field*/ *len_used + page_len_offset,
5977				      /*bit_valid*/ 0,
5978				      /*bit*/ 0);
5979		free(ctsio->kern_data_ptr, M_CTL);
5980		ctl_done((union ctl_io *)ctsio);
5981		return (CTL_RETVAL_COMPLETE);
5982	}
5983
5984	/*
5985	 * Run through the mode page, checking to make sure that the bits
5986	 * the user changed are actually legal for him to change.
5987	 */
5988	for (i = 0; i < page_index->page_len; i++) {
5989		uint8_t *user_byte, *change_mask, *current_byte;
5990		int bad_bit;
5991		int j;
5992
5993		user_byte = (uint8_t *)page_header + i;
5994		change_mask = page_index->page_data +
5995			      (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
5996		current_byte = page_index->page_data +
5997			       (page_index->page_len * CTL_PAGE_CURRENT) + i;
5998
5999		/*
6000		 * Check to see whether the user set any bits in this byte
6001		 * that he is not allowed to set.
6002		 */
6003		if ((*user_byte & ~(*change_mask)) ==
6004		    (*current_byte & ~(*change_mask)))
6005			continue;
6006
6007		/*
6008		 * Go through bit by bit to determine which one is illegal.
6009		 */
6010		bad_bit = 0;
6011		for (j = 7; j >= 0; j--) {
6012			if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6013			    (((1 << i) & ~(*change_mask)) & *current_byte)) {
6014				bad_bit = i;
6015				break;
6016			}
6017		}
6018		ctl_set_invalid_field(ctsio,
6019				      /*sks_valid*/ 1,
6020				      /*command*/ 0,
6021				      /*field*/ *len_used + i,
6022				      /*bit_valid*/ 1,
6023				      /*bit*/ bad_bit);
6024		free(ctsio->kern_data_ptr, M_CTL);
6025		ctl_done((union ctl_io *)ctsio);
6026		return (CTL_RETVAL_COMPLETE);
6027	}
6028
6029	/*
6030	 * Decrement these before we call the page handler, since we may
6031	 * end up getting called back one way or another before the handler
6032	 * returns to this context.
6033	 */
6034	*len_left -= page_index->page_len;
6035	*len_used += page_index->page_len;
6036
6037	retval = page_index->select_handler(ctsio, page_index,
6038					    (uint8_t *)page_header);
6039
6040	/*
6041	 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6042	 * wait until this queued command completes to finish processing
6043	 * the mode page.  If it returns anything other than
6044	 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6045	 * already set the sense information, freed the data pointer, and
6046	 * completed the io for us.
6047	 */
6048	if (retval != CTL_RETVAL_COMPLETE)
6049		goto bailout_no_done;
6050
6051	/*
6052	 * If the initiator sent us more than one page, parse the next one.
6053	 */
6054	if (*len_left > 0)
6055		goto do_next_page;
6056
6057	ctl_set_success(ctsio);
6058	free(ctsio->kern_data_ptr, M_CTL);
6059	ctl_done((union ctl_io *)ctsio);
6060
6061bailout_no_done:
6062
6063	return (CTL_RETVAL_COMPLETE);
6064
6065}
6066
6067int
6068ctl_mode_select(struct ctl_scsiio *ctsio)
6069{
6070	int param_len, pf, sp;
6071	int header_size, bd_len;
6072	int len_left, len_used;
6073	struct ctl_page_index *page_index;
6074	struct ctl_lun *lun;
6075	int control_dev, page_len;
6076	union ctl_modepage_info *modepage_info;
6077	int retval;
6078
6079	pf = 0;
6080	sp = 0;
6081	page_len = 0;
6082	len_used = 0;
6083	len_left = 0;
6084	retval = 0;
6085	bd_len = 0;
6086	page_index = NULL;
6087
6088	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6089
6090	if (lun->be_lun->lun_type != T_DIRECT)
6091		control_dev = 1;
6092	else
6093		control_dev = 0;
6094
6095	switch (ctsio->cdb[0]) {
6096	case MODE_SELECT_6: {
6097		struct scsi_mode_select_6 *cdb;
6098
6099		cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6100
6101		pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6102		sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6103
6104		param_len = cdb->length;
6105		header_size = sizeof(struct scsi_mode_header_6);
6106		break;
6107	}
6108	case MODE_SELECT_10: {
6109		struct scsi_mode_select_10 *cdb;
6110
6111		cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6112
6113		pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6114		sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6115
6116		param_len = scsi_2btoul(cdb->length);
6117		header_size = sizeof(struct scsi_mode_header_10);
6118		break;
6119	}
6120	default:
6121		ctl_set_invalid_opcode(ctsio);
6122		ctl_done((union ctl_io *)ctsio);
6123		return (CTL_RETVAL_COMPLETE);
6124		break; /* NOTREACHED */
6125	}
6126
6127	/*
6128	 * From SPC-3:
6129	 * "A parameter list length of zero indicates that the Data-Out Buffer
6130	 * shall be empty. This condition shall not be considered as an error."
6131	 */
6132	if (param_len == 0) {
6133		ctl_set_success(ctsio);
6134		ctl_done((union ctl_io *)ctsio);
6135		return (CTL_RETVAL_COMPLETE);
6136	}
6137
6138	/*
6139	 * Since we'll hit this the first time through, prior to
6140	 * allocation, we don't need to free a data buffer here.
6141	 */
6142	if (param_len < header_size) {
6143		ctl_set_param_len_error(ctsio);
6144		ctl_done((union ctl_io *)ctsio);
6145		return (CTL_RETVAL_COMPLETE);
6146	}
6147
6148	/*
6149	 * Allocate the data buffer and grab the user's data.  In theory,
6150	 * we shouldn't have to sanity check the parameter list length here
6151	 * because the maximum size is 64K.  We should be able to malloc
6152	 * that much without too many problems.
6153	 */
6154	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6155		ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6156		ctsio->kern_data_len = param_len;
6157		ctsio->kern_total_len = param_len;
6158		ctsio->kern_data_resid = 0;
6159		ctsio->kern_rel_offset = 0;
6160		ctsio->kern_sg_entries = 0;
6161		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6162		ctsio->be_move_done = ctl_config_move_done;
6163		ctl_datamove((union ctl_io *)ctsio);
6164
6165		return (CTL_RETVAL_COMPLETE);
6166	}
6167
6168	switch (ctsio->cdb[0]) {
6169	case MODE_SELECT_6: {
6170		struct scsi_mode_header_6 *mh6;
6171
6172		mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6173		bd_len = mh6->blk_desc_len;
6174		break;
6175	}
6176	case MODE_SELECT_10: {
6177		struct scsi_mode_header_10 *mh10;
6178
6179		mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6180		bd_len = scsi_2btoul(mh10->blk_desc_len);
6181		break;
6182	}
6183	default:
6184		panic("Invalid CDB type %#x", ctsio->cdb[0]);
6185		break;
6186	}
6187
6188	if (param_len < (header_size + bd_len)) {
6189		free(ctsio->kern_data_ptr, M_CTL);
6190		ctl_set_param_len_error(ctsio);
6191		ctl_done((union ctl_io *)ctsio);
6192		return (CTL_RETVAL_COMPLETE);
6193	}
6194
6195	/*
6196	 * Set the IO_CONT flag, so that if this I/O gets passed to
6197	 * ctl_config_write_done(), it'll get passed back to
6198	 * ctl_do_mode_select() for further processing, or completion if
6199	 * we're all done.
6200	 */
6201	ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6202	ctsio->io_cont = ctl_do_mode_select;
6203
6204	modepage_info = (union ctl_modepage_info *)
6205		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6206
6207	memset(modepage_info, 0, sizeof(*modepage_info));
6208
6209	len_left = param_len - header_size - bd_len;
6210	len_used = header_size + bd_len;
6211
6212	modepage_info->header.len_left = len_left;
6213	modepage_info->header.len_used = len_used;
6214
6215	return (ctl_do_mode_select((union ctl_io *)ctsio));
6216}
6217
6218int
6219ctl_mode_sense(struct ctl_scsiio *ctsio)
6220{
6221	struct ctl_lun *lun;
6222	int pc, page_code, dbd, llba, subpage;
6223	int alloc_len, page_len, header_len, total_len;
6224	struct scsi_mode_block_descr *block_desc;
6225	struct ctl_page_index *page_index;
6226	int control_dev;
6227
6228	dbd = 0;
6229	llba = 0;
6230	block_desc = NULL;
6231	page_index = NULL;
6232
6233	CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6234
6235	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6236
6237	if (lun->be_lun->lun_type != T_DIRECT)
6238		control_dev = 1;
6239	else
6240		control_dev = 0;
6241
6242	switch (ctsio->cdb[0]) {
6243	case MODE_SENSE_6: {
6244		struct scsi_mode_sense_6 *cdb;
6245
6246		cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6247
6248		header_len = sizeof(struct scsi_mode_hdr_6);
6249		if (cdb->byte2 & SMS_DBD)
6250			dbd = 1;
6251		else
6252			header_len += sizeof(struct scsi_mode_block_descr);
6253
6254		pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6255		page_code = cdb->page & SMS_PAGE_CODE;
6256		subpage = cdb->subpage;
6257		alloc_len = cdb->length;
6258		break;
6259	}
6260	case MODE_SENSE_10: {
6261		struct scsi_mode_sense_10 *cdb;
6262
6263		cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6264
6265		header_len = sizeof(struct scsi_mode_hdr_10);
6266
6267		if (cdb->byte2 & SMS_DBD)
6268			dbd = 1;
6269		else
6270			header_len += sizeof(struct scsi_mode_block_descr);
6271		if (cdb->byte2 & SMS10_LLBAA)
6272			llba = 1;
6273		pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6274		page_code = cdb->page & SMS_PAGE_CODE;
6275		subpage = cdb->subpage;
6276		alloc_len = scsi_2btoul(cdb->length);
6277		break;
6278	}
6279	default:
6280		ctl_set_invalid_opcode(ctsio);
6281		ctl_done((union ctl_io *)ctsio);
6282		return (CTL_RETVAL_COMPLETE);
6283		break; /* NOTREACHED */
6284	}
6285
6286	/*
6287	 * We have to make a first pass through to calculate the size of
6288	 * the pages that match the user's query.  Then we allocate enough
6289	 * memory to hold it, and actually copy the data into the buffer.
6290	 */
6291	switch (page_code) {
6292	case SMS_ALL_PAGES_PAGE: {
6293		int i;
6294
6295		page_len = 0;
6296
6297		/*
6298		 * At the moment, values other than 0 and 0xff here are
6299		 * reserved according to SPC-3.
6300		 */
6301		if ((subpage != SMS_SUBPAGE_PAGE_0)
6302		 && (subpage != SMS_SUBPAGE_ALL)) {
6303			ctl_set_invalid_field(ctsio,
6304					      /*sks_valid*/ 1,
6305					      /*command*/ 1,
6306					      /*field*/ 3,
6307					      /*bit_valid*/ 0,
6308					      /*bit*/ 0);
6309			ctl_done((union ctl_io *)ctsio);
6310			return (CTL_RETVAL_COMPLETE);
6311		}
6312
6313		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6314			if ((control_dev != 0)
6315			 && (lun->mode_pages.index[i].page_flags &
6316			     CTL_PAGE_FLAG_DISK_ONLY))
6317				continue;
6318
6319			/*
6320			 * We don't use this subpage if the user didn't
6321			 * request all subpages.
6322			 */
6323			if ((lun->mode_pages.index[i].subpage != 0)
6324			 && (subpage == SMS_SUBPAGE_PAGE_0))
6325				continue;
6326
6327#if 0
6328			printf("found page %#x len %d\n",
6329			       lun->mode_pages.index[i].page_code &
6330			       SMPH_PC_MASK,
6331			       lun->mode_pages.index[i].page_len);
6332#endif
6333			page_len += lun->mode_pages.index[i].page_len;
6334		}
6335		break;
6336	}
6337	default: {
6338		int i;
6339
6340		page_len = 0;
6341
6342		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6343			/* Look for the right page code */
6344			if ((lun->mode_pages.index[i].page_code &
6345			     SMPH_PC_MASK) != page_code)
6346				continue;
6347
6348			/* Look for the right subpage or the subpage wildcard*/
6349			if ((lun->mode_pages.index[i].subpage != subpage)
6350			 && (subpage != SMS_SUBPAGE_ALL))
6351				continue;
6352
6353			/* Make sure the page is supported for this dev type */
6354			if ((control_dev != 0)
6355			 && (lun->mode_pages.index[i].page_flags &
6356			     CTL_PAGE_FLAG_DISK_ONLY))
6357				continue;
6358
6359#if 0
6360			printf("found page %#x len %d\n",
6361			       lun->mode_pages.index[i].page_code &
6362			       SMPH_PC_MASK,
6363			       lun->mode_pages.index[i].page_len);
6364#endif
6365
6366			page_len += lun->mode_pages.index[i].page_len;
6367		}
6368
6369		if (page_len == 0) {
6370			ctl_set_invalid_field(ctsio,
6371					      /*sks_valid*/ 1,
6372					      /*command*/ 1,
6373					      /*field*/ 2,
6374					      /*bit_valid*/ 1,
6375					      /*bit*/ 5);
6376			ctl_done((union ctl_io *)ctsio);
6377			return (CTL_RETVAL_COMPLETE);
6378		}
6379		break;
6380	}
6381	}
6382
6383	total_len = header_len + page_len;
6384#if 0
6385	printf("header_len = %d, page_len = %d, total_len = %d\n",
6386	       header_len, page_len, total_len);
6387#endif
6388
6389	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6390	ctsio->kern_sg_entries = 0;
6391	ctsio->kern_data_resid = 0;
6392	ctsio->kern_rel_offset = 0;
6393	if (total_len < alloc_len) {
6394		ctsio->residual = alloc_len - total_len;
6395		ctsio->kern_data_len = total_len;
6396		ctsio->kern_total_len = total_len;
6397	} else {
6398		ctsio->residual = 0;
6399		ctsio->kern_data_len = alloc_len;
6400		ctsio->kern_total_len = alloc_len;
6401	}
6402
6403	switch (ctsio->cdb[0]) {
6404	case MODE_SENSE_6: {
6405		struct scsi_mode_hdr_6 *header;
6406
6407		header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6408
6409		header->datalen = MIN(total_len - 1, 254);
6410		if (control_dev == 0) {
6411			header->dev_specific = 0x10; /* DPOFUA */
6412			if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6413			    (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6414			    .eca_and_aen & SCP_SWP) != 0)
6415				    header->dev_specific |= 0x80; /* WP */
6416		}
6417		if (dbd)
6418			header->block_descr_len = 0;
6419		else
6420			header->block_descr_len =
6421				sizeof(struct scsi_mode_block_descr);
6422		block_desc = (struct scsi_mode_block_descr *)&header[1];
6423		break;
6424	}
6425	case MODE_SENSE_10: {
6426		struct scsi_mode_hdr_10 *header;
6427		int datalen;
6428
6429		header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6430
6431		datalen = MIN(total_len - 2, 65533);
6432		scsi_ulto2b(datalen, header->datalen);
6433		if (control_dev == 0) {
6434			header->dev_specific = 0x10; /* DPOFUA */
6435			if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6436			    (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6437			    .eca_and_aen & SCP_SWP) != 0)
6438				    header->dev_specific |= 0x80; /* WP */
6439		}
6440		if (dbd)
6441			scsi_ulto2b(0, header->block_descr_len);
6442		else
6443			scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6444				    header->block_descr_len);
6445		block_desc = (struct scsi_mode_block_descr *)&header[1];
6446		break;
6447	}
6448	default:
6449		panic("invalid CDB type %#x", ctsio->cdb[0]);
6450		break; /* NOTREACHED */
6451	}
6452
6453	/*
6454	 * If we've got a disk, use its blocksize in the block
6455	 * descriptor.  Otherwise, just set it to 0.
6456	 */
6457	if (dbd == 0) {
6458		if (control_dev == 0)
6459			scsi_ulto3b(lun->be_lun->blocksize,
6460				    block_desc->block_len);
6461		else
6462			scsi_ulto3b(0, block_desc->block_len);
6463	}
6464
6465	switch (page_code) {
6466	case SMS_ALL_PAGES_PAGE: {
6467		int i, data_used;
6468
6469		data_used = header_len;
6470		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6471			struct ctl_page_index *page_index;
6472
6473			page_index = &lun->mode_pages.index[i];
6474
6475			if ((control_dev != 0)
6476			 && (page_index->page_flags &
6477			    CTL_PAGE_FLAG_DISK_ONLY))
6478				continue;
6479
6480			/*
6481			 * We don't use this subpage if the user didn't
6482			 * request all subpages.  We already checked (above)
6483			 * to make sure the user only specified a subpage
6484			 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6485			 */
6486			if ((page_index->subpage != 0)
6487			 && (subpage == SMS_SUBPAGE_PAGE_0))
6488				continue;
6489
6490			/*
6491			 * Call the handler, if it exists, to update the
6492			 * page to the latest values.
6493			 */
6494			if (page_index->sense_handler != NULL)
6495				page_index->sense_handler(ctsio, page_index,pc);
6496
6497			memcpy(ctsio->kern_data_ptr + data_used,
6498			       page_index->page_data +
6499			       (page_index->page_len * pc),
6500			       page_index->page_len);
6501			data_used += page_index->page_len;
6502		}
6503		break;
6504	}
6505	default: {
6506		int i, data_used;
6507
6508		data_used = header_len;
6509
6510		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6511			struct ctl_page_index *page_index;
6512
6513			page_index = &lun->mode_pages.index[i];
6514
6515			/* Look for the right page code */
6516			if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6517				continue;
6518
6519			/* Look for the right subpage or the subpage wildcard*/
6520			if ((page_index->subpage != subpage)
6521			 && (subpage != SMS_SUBPAGE_ALL))
6522				continue;
6523
6524			/* Make sure the page is supported for this dev type */
6525			if ((control_dev != 0)
6526			 && (page_index->page_flags &
6527			     CTL_PAGE_FLAG_DISK_ONLY))
6528				continue;
6529
6530			/*
6531			 * Call the handler, if it exists, to update the
6532			 * page to the latest values.
6533			 */
6534			if (page_index->sense_handler != NULL)
6535				page_index->sense_handler(ctsio, page_index,pc);
6536
6537			memcpy(ctsio->kern_data_ptr + data_used,
6538			       page_index->page_data +
6539			       (page_index->page_len * pc),
6540			       page_index->page_len);
6541			data_used += page_index->page_len;
6542		}
6543		break;
6544	}
6545	}
6546
6547	ctl_set_success(ctsio);
6548	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6549	ctsio->be_move_done = ctl_config_move_done;
6550	ctl_datamove((union ctl_io *)ctsio);
6551	return (CTL_RETVAL_COMPLETE);
6552}
6553
6554int
6555ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6556			       struct ctl_page_index *page_index,
6557			       int pc)
6558{
6559	struct ctl_lun *lun;
6560	struct scsi_log_param_header *phdr;
6561	uint8_t *data;
6562	uint64_t val;
6563
6564	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6565	data = page_index->page_data;
6566
6567	if (lun->backend->lun_attr != NULL &&
6568	    (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6569	     != UINT64_MAX) {
6570		phdr = (struct scsi_log_param_header *)data;
6571		scsi_ulto2b(0x0001, phdr->param_code);
6572		phdr->param_control = SLP_LBIN | SLP_LP;
6573		phdr->param_len = 8;
6574		data = (uint8_t *)(phdr + 1);
6575		scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6576		data[4] = 0x02; /* per-pool */
6577		data += phdr->param_len;
6578	}
6579
6580	if (lun->backend->lun_attr != NULL &&
6581	    (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6582	     != UINT64_MAX) {
6583		phdr = (struct scsi_log_param_header *)data;
6584		scsi_ulto2b(0x0002, phdr->param_code);
6585		phdr->param_control = SLP_LBIN | SLP_LP;
6586		phdr->param_len = 8;
6587		data = (uint8_t *)(phdr + 1);
6588		scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6589		data[4] = 0x01; /* per-LUN */
6590		data += phdr->param_len;
6591	}
6592
6593	if (lun->backend->lun_attr != NULL &&
6594	    (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6595	     != UINT64_MAX) {
6596		phdr = (struct scsi_log_param_header *)data;
6597		scsi_ulto2b(0x00f1, phdr->param_code);
6598		phdr->param_control = SLP_LBIN | SLP_LP;
6599		phdr->param_len = 8;
6600		data = (uint8_t *)(phdr + 1);
6601		scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6602		data[4] = 0x02; /* per-pool */
6603		data += phdr->param_len;
6604	}
6605
6606	if (lun->backend->lun_attr != NULL &&
6607	    (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6608	     != UINT64_MAX) {
6609		phdr = (struct scsi_log_param_header *)data;
6610		scsi_ulto2b(0x00f2, phdr->param_code);
6611		phdr->param_control = SLP_LBIN | SLP_LP;
6612		phdr->param_len = 8;
6613		data = (uint8_t *)(phdr + 1);
6614		scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6615		data[4] = 0x02; /* per-pool */
6616		data += phdr->param_len;
6617	}
6618
6619	page_index->page_len = data - page_index->page_data;
6620	return (0);
6621}
6622
6623int
6624ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6625			       struct ctl_page_index *page_index,
6626			       int pc)
6627{
6628	struct ctl_lun *lun;
6629	struct stat_page *data;
6630	uint64_t rn, wn, rb, wb;
6631	struct bintime rt, wt;
6632	int i;
6633
6634	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6635	data = (struct stat_page *)page_index->page_data;
6636
6637	scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6638	data->sap.hdr.param_control = SLP_LBIN;
6639	data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6640	    sizeof(struct scsi_log_param_header);
6641	rn = wn = rb = wb = 0;
6642	bintime_clear(&rt);
6643	bintime_clear(&wt);
6644	for (i = 0; i < CTL_MAX_PORTS; i++) {
6645		rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6646		wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6647		rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6648		wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6649		bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6650		bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6651	}
6652	scsi_u64to8b(rn, data->sap.read_num);
6653	scsi_u64to8b(wn, data->sap.write_num);
6654	if (lun->stats.blocksize > 0) {
6655		scsi_u64to8b(wb / lun->stats.blocksize,
6656		    data->sap.recvieved_lba);
6657		scsi_u64to8b(rb / lun->stats.blocksize,
6658		    data->sap.transmitted_lba);
6659	}
6660	scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6661	    data->sap.read_int);
6662	scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6663	    data->sap.write_int);
6664	scsi_u64to8b(0, data->sap.weighted_num);
6665	scsi_u64to8b(0, data->sap.weighted_int);
6666	scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6667	data->it.hdr.param_control = SLP_LBIN;
6668	data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6669	    sizeof(struct scsi_log_param_header);
6670#ifdef CTL_TIME_IO
6671	scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6672#endif
6673	scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6674	data->it.hdr.param_control = SLP_LBIN;
6675	data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6676	    sizeof(struct scsi_log_param_header);
6677	scsi_ulto4b(3, data->ti.exponent);
6678	scsi_ulto4b(1, data->ti.integer);
6679
6680	page_index->page_len = sizeof(*data);
6681	return (0);
6682}
6683
6684int
6685ctl_log_sense(struct ctl_scsiio *ctsio)
6686{
6687	struct ctl_lun *lun;
6688	int i, pc, page_code, subpage;
6689	int alloc_len, total_len;
6690	struct ctl_page_index *page_index;
6691	struct scsi_log_sense *cdb;
6692	struct scsi_log_header *header;
6693
6694	CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6695
6696	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6697	cdb = (struct scsi_log_sense *)ctsio->cdb;
6698	pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6699	page_code = cdb->page & SLS_PAGE_CODE;
6700	subpage = cdb->subpage;
6701	alloc_len = scsi_2btoul(cdb->length);
6702
6703	page_index = NULL;
6704	for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6705		page_index = &lun->log_pages.index[i];
6706
6707		/* Look for the right page code */
6708		if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6709			continue;
6710
6711		/* Look for the right subpage or the subpage wildcard*/
6712		if (page_index->subpage != subpage)
6713			continue;
6714
6715		break;
6716	}
6717	if (i >= CTL_NUM_LOG_PAGES) {
6718		ctl_set_invalid_field(ctsio,
6719				      /*sks_valid*/ 1,
6720				      /*command*/ 1,
6721				      /*field*/ 2,
6722				      /*bit_valid*/ 0,
6723				      /*bit*/ 0);
6724		ctl_done((union ctl_io *)ctsio);
6725		return (CTL_RETVAL_COMPLETE);
6726	}
6727
6728	total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6729
6730	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6731	ctsio->kern_sg_entries = 0;
6732	ctsio->kern_data_resid = 0;
6733	ctsio->kern_rel_offset = 0;
6734	if (total_len < alloc_len) {
6735		ctsio->residual = alloc_len - total_len;
6736		ctsio->kern_data_len = total_len;
6737		ctsio->kern_total_len = total_len;
6738	} else {
6739		ctsio->residual = 0;
6740		ctsio->kern_data_len = alloc_len;
6741		ctsio->kern_total_len = alloc_len;
6742	}
6743
6744	header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6745	header->page = page_index->page_code;
6746	if (page_index->subpage) {
6747		header->page |= SL_SPF;
6748		header->subpage = page_index->subpage;
6749	}
6750	scsi_ulto2b(page_index->page_len, header->datalen);
6751
6752	/*
6753	 * Call the handler, if it exists, to update the
6754	 * page to the latest values.
6755	 */
6756	if (page_index->sense_handler != NULL)
6757		page_index->sense_handler(ctsio, page_index, pc);
6758
6759	memcpy(header + 1, page_index->page_data, page_index->page_len);
6760
6761	ctl_set_success(ctsio);
6762	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6763	ctsio->be_move_done = ctl_config_move_done;
6764	ctl_datamove((union ctl_io *)ctsio);
6765	return (CTL_RETVAL_COMPLETE);
6766}
6767
6768int
6769ctl_read_capacity(struct ctl_scsiio *ctsio)
6770{
6771	struct scsi_read_capacity *cdb;
6772	struct scsi_read_capacity_data *data;
6773	struct ctl_lun *lun;
6774	uint32_t lba;
6775
6776	CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6777
6778	cdb = (struct scsi_read_capacity *)ctsio->cdb;
6779
6780	lba = scsi_4btoul(cdb->addr);
6781	if (((cdb->pmi & SRC_PMI) == 0)
6782	 && (lba != 0)) {
6783		ctl_set_invalid_field(/*ctsio*/ ctsio,
6784				      /*sks_valid*/ 1,
6785				      /*command*/ 1,
6786				      /*field*/ 2,
6787				      /*bit_valid*/ 0,
6788				      /*bit*/ 0);
6789		ctl_done((union ctl_io *)ctsio);
6790		return (CTL_RETVAL_COMPLETE);
6791	}
6792
6793	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6794
6795	ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6796	data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6797	ctsio->residual = 0;
6798	ctsio->kern_data_len = sizeof(*data);
6799	ctsio->kern_total_len = sizeof(*data);
6800	ctsio->kern_data_resid = 0;
6801	ctsio->kern_rel_offset = 0;
6802	ctsio->kern_sg_entries = 0;
6803
6804	/*
6805	 * If the maximum LBA is greater than 0xfffffffe, the user must
6806	 * issue a SERVICE ACTION IN (16) command, with the read capacity
6807	 * serivce action set.
6808	 */
6809	if (lun->be_lun->maxlba > 0xfffffffe)
6810		scsi_ulto4b(0xffffffff, data->addr);
6811	else
6812		scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6813
6814	/*
6815	 * XXX KDM this may not be 512 bytes...
6816	 */
6817	scsi_ulto4b(lun->be_lun->blocksize, data->length);
6818
6819	ctl_set_success(ctsio);
6820	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6821	ctsio->be_move_done = ctl_config_move_done;
6822	ctl_datamove((union ctl_io *)ctsio);
6823	return (CTL_RETVAL_COMPLETE);
6824}
6825
6826int
6827ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6828{
6829	struct scsi_read_capacity_16 *cdb;
6830	struct scsi_read_capacity_data_long *data;
6831	struct ctl_lun *lun;
6832	uint64_t lba;
6833	uint32_t alloc_len;
6834
6835	CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6836
6837	cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6838
6839	alloc_len = scsi_4btoul(cdb->alloc_len);
6840	lba = scsi_8btou64(cdb->addr);
6841
6842	if ((cdb->reladr & SRC16_PMI)
6843	 && (lba != 0)) {
6844		ctl_set_invalid_field(/*ctsio*/ ctsio,
6845				      /*sks_valid*/ 1,
6846				      /*command*/ 1,
6847				      /*field*/ 2,
6848				      /*bit_valid*/ 0,
6849				      /*bit*/ 0);
6850		ctl_done((union ctl_io *)ctsio);
6851		return (CTL_RETVAL_COMPLETE);
6852	}
6853
6854	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6855
6856	ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6857	data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6858
6859	if (sizeof(*data) < alloc_len) {
6860		ctsio->residual = alloc_len - sizeof(*data);
6861		ctsio->kern_data_len = sizeof(*data);
6862		ctsio->kern_total_len = sizeof(*data);
6863	} else {
6864		ctsio->residual = 0;
6865		ctsio->kern_data_len = alloc_len;
6866		ctsio->kern_total_len = alloc_len;
6867	}
6868	ctsio->kern_data_resid = 0;
6869	ctsio->kern_rel_offset = 0;
6870	ctsio->kern_sg_entries = 0;
6871
6872	scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6873	/* XXX KDM this may not be 512 bytes... */
6874	scsi_ulto4b(lun->be_lun->blocksize, data->length);
6875	data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6876	scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6877	if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6878		data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6879
6880	ctl_set_success(ctsio);
6881	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6882	ctsio->be_move_done = ctl_config_move_done;
6883	ctl_datamove((union ctl_io *)ctsio);
6884	return (CTL_RETVAL_COMPLETE);
6885}
6886
6887int
6888ctl_get_lba_status(struct ctl_scsiio *ctsio)
6889{
6890	struct scsi_get_lba_status *cdb;
6891	struct scsi_get_lba_status_data *data;
6892	struct ctl_lun *lun;
6893	struct ctl_lba_len_flags *lbalen;
6894	uint64_t lba;
6895	uint32_t alloc_len, total_len;
6896	int retval;
6897
6898	CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6899
6900	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6901	cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6902	lba = scsi_8btou64(cdb->addr);
6903	alloc_len = scsi_4btoul(cdb->alloc_len);
6904
6905	if (lba > lun->be_lun->maxlba) {
6906		ctl_set_lba_out_of_range(ctsio);
6907		ctl_done((union ctl_io *)ctsio);
6908		return (CTL_RETVAL_COMPLETE);
6909	}
6910
6911	total_len = sizeof(*data) + sizeof(data->descr[0]);
6912	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6913	data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6914
6915	if (total_len < alloc_len) {
6916		ctsio->residual = alloc_len - total_len;
6917		ctsio->kern_data_len = total_len;
6918		ctsio->kern_total_len = total_len;
6919	} else {
6920		ctsio->residual = 0;
6921		ctsio->kern_data_len = alloc_len;
6922		ctsio->kern_total_len = alloc_len;
6923	}
6924	ctsio->kern_data_resid = 0;
6925	ctsio->kern_rel_offset = 0;
6926	ctsio->kern_sg_entries = 0;
6927
6928	/* Fill dummy data in case backend can't tell anything. */
6929	scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6930	scsi_u64to8b(lba, data->descr[0].addr);
6931	scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
6932	    data->descr[0].length);
6933	data->descr[0].status = 0; /* Mapped or unknown. */
6934
6935	ctl_set_success(ctsio);
6936	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6937	ctsio->be_move_done = ctl_config_move_done;
6938
6939	lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6940	lbalen->lba = lba;
6941	lbalen->len = total_len;
6942	lbalen->flags = 0;
6943	retval = lun->backend->config_read((union ctl_io *)ctsio);
6944	return (CTL_RETVAL_COMPLETE);
6945}
6946
6947int
6948ctl_read_defect(struct ctl_scsiio *ctsio)
6949{
6950	struct scsi_read_defect_data_10 *ccb10;
6951	struct scsi_read_defect_data_12 *ccb12;
6952	struct scsi_read_defect_data_hdr_10 *data10;
6953	struct scsi_read_defect_data_hdr_12 *data12;
6954	uint32_t alloc_len, data_len;
6955	uint8_t format;
6956
6957	CTL_DEBUG_PRINT(("ctl_read_defect\n"));
6958
6959	if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
6960		ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
6961		format = ccb10->format;
6962		alloc_len = scsi_2btoul(ccb10->alloc_length);
6963		data_len = sizeof(*data10);
6964	} else {
6965		ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
6966		format = ccb12->format;
6967		alloc_len = scsi_4btoul(ccb12->alloc_length);
6968		data_len = sizeof(*data12);
6969	}
6970	if (alloc_len == 0) {
6971		ctl_set_success(ctsio);
6972		ctl_done((union ctl_io *)ctsio);
6973		return (CTL_RETVAL_COMPLETE);
6974	}
6975
6976	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
6977	if (data_len < alloc_len) {
6978		ctsio->residual = alloc_len - data_len;
6979		ctsio->kern_data_len = data_len;
6980		ctsio->kern_total_len = data_len;
6981	} else {
6982		ctsio->residual = 0;
6983		ctsio->kern_data_len = alloc_len;
6984		ctsio->kern_total_len = alloc_len;
6985	}
6986	ctsio->kern_data_resid = 0;
6987	ctsio->kern_rel_offset = 0;
6988	ctsio->kern_sg_entries = 0;
6989
6990	if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
6991		data10 = (struct scsi_read_defect_data_hdr_10 *)
6992		    ctsio->kern_data_ptr;
6993		data10->format = format;
6994		scsi_ulto2b(0, data10->length);
6995	} else {
6996		data12 = (struct scsi_read_defect_data_hdr_12 *)
6997		    ctsio->kern_data_ptr;
6998		data12->format = format;
6999		scsi_ulto2b(0, data12->generation);
7000		scsi_ulto4b(0, data12->length);
7001	}
7002
7003	ctl_set_success(ctsio);
7004	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7005	ctsio->be_move_done = ctl_config_move_done;
7006	ctl_datamove((union ctl_io *)ctsio);
7007	return (CTL_RETVAL_COMPLETE);
7008}
7009
7010int
7011ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7012{
7013	struct scsi_maintenance_in *cdb;
7014	int retval;
7015	int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7016	int num_target_port_groups, num_target_ports;
7017	struct ctl_lun *lun;
7018	struct ctl_softc *softc;
7019	struct ctl_port *port;
7020	struct scsi_target_group_data *rtg_ptr;
7021	struct scsi_target_group_data_extended *rtg_ext_ptr;
7022	struct scsi_target_port_group_descriptor *tpg_desc;
7023
7024	CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7025
7026	cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7027	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7028	softc = lun->ctl_softc;
7029
7030	retval = CTL_RETVAL_COMPLETE;
7031
7032	switch (cdb->byte2 & STG_PDF_MASK) {
7033	case STG_PDF_LENGTH:
7034		ext = 0;
7035		break;
7036	case STG_PDF_EXTENDED:
7037		ext = 1;
7038		break;
7039	default:
7040		ctl_set_invalid_field(/*ctsio*/ ctsio,
7041				      /*sks_valid*/ 1,
7042				      /*command*/ 1,
7043				      /*field*/ 2,
7044				      /*bit_valid*/ 1,
7045				      /*bit*/ 5);
7046		ctl_done((union ctl_io *)ctsio);
7047		return(retval);
7048	}
7049
7050	if (softc->is_single)
7051		num_target_port_groups = 1;
7052	else
7053		num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7054	num_target_ports = 0;
7055	mtx_lock(&softc->ctl_lock);
7056	STAILQ_FOREACH(port, &softc->port_list, links) {
7057		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7058			continue;
7059		if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7060			continue;
7061		num_target_ports++;
7062	}
7063	mtx_unlock(&softc->ctl_lock);
7064
7065	if (ext)
7066		total_len = sizeof(struct scsi_target_group_data_extended);
7067	else
7068		total_len = sizeof(struct scsi_target_group_data);
7069	total_len += sizeof(struct scsi_target_port_group_descriptor) *
7070		num_target_port_groups +
7071	    sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7072
7073	alloc_len = scsi_4btoul(cdb->length);
7074
7075	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7076
7077	ctsio->kern_sg_entries = 0;
7078
7079	if (total_len < alloc_len) {
7080		ctsio->residual = alloc_len - total_len;
7081		ctsio->kern_data_len = total_len;
7082		ctsio->kern_total_len = total_len;
7083	} else {
7084		ctsio->residual = 0;
7085		ctsio->kern_data_len = alloc_len;
7086		ctsio->kern_total_len = alloc_len;
7087	}
7088	ctsio->kern_data_resid = 0;
7089	ctsio->kern_rel_offset = 0;
7090
7091	if (ext) {
7092		rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7093		    ctsio->kern_data_ptr;
7094		scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7095		rtg_ext_ptr->format_type = 0x10;
7096		rtg_ext_ptr->implicit_transition_time = 0;
7097		tpg_desc = &rtg_ext_ptr->groups[0];
7098	} else {
7099		rtg_ptr = (struct scsi_target_group_data *)
7100		    ctsio->kern_data_ptr;
7101		scsi_ulto4b(total_len - 4, rtg_ptr->length);
7102		tpg_desc = &rtg_ptr->groups[0];
7103	}
7104
7105	mtx_lock(&softc->ctl_lock);
7106	pg = softc->port_min / softc->port_cnt;
7107	if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7108		gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7109	else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7110		gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7111	else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7112		gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7113	else
7114		gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7115	if (lun->flags & CTL_LUN_PRIMARY_SC) {
7116		os = gs;
7117		gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7118	} else
7119		os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7120	for (g = 0; g < num_target_port_groups; g++) {
7121		tpg_desc->pref_state = (g == pg) ? gs : os;
7122		tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7123		    TPG_U_SUP | TPG_T_SUP;
7124		scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7125		tpg_desc->status = TPG_IMPLICIT;
7126		pc = 0;
7127		STAILQ_FOREACH(port, &softc->port_list, links) {
7128			if (port->targ_port < g * softc->port_cnt ||
7129			    port->targ_port >= (g + 1) * softc->port_cnt)
7130				continue;
7131			if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7132				continue;
7133			if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7134				continue;
7135			scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7136			    relative_target_port_identifier);
7137			pc++;
7138		}
7139		tpg_desc->target_port_count = pc;
7140		tpg_desc = (struct scsi_target_port_group_descriptor *)
7141		    &tpg_desc->descriptors[pc];
7142	}
7143	mtx_unlock(&softc->ctl_lock);
7144
7145	ctl_set_success(ctsio);
7146	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7147	ctsio->be_move_done = ctl_config_move_done;
7148	ctl_datamove((union ctl_io *)ctsio);
7149	return(retval);
7150}
7151
7152int
7153ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7154{
7155	struct ctl_lun *lun;
7156	struct scsi_report_supported_opcodes *cdb;
7157	const struct ctl_cmd_entry *entry, *sentry;
7158	struct scsi_report_supported_opcodes_all *all;
7159	struct scsi_report_supported_opcodes_descr *descr;
7160	struct scsi_report_supported_opcodes_one *one;
7161	int retval;
7162	int alloc_len, total_len;
7163	int opcode, service_action, i, j, num;
7164
7165	CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7166
7167	cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7168	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7169
7170	retval = CTL_RETVAL_COMPLETE;
7171
7172	opcode = cdb->requested_opcode;
7173	service_action = scsi_2btoul(cdb->requested_service_action);
7174	switch (cdb->options & RSO_OPTIONS_MASK) {
7175	case RSO_OPTIONS_ALL:
7176		num = 0;
7177		for (i = 0; i < 256; i++) {
7178			entry = &ctl_cmd_table[i];
7179			if (entry->flags & CTL_CMD_FLAG_SA5) {
7180				for (j = 0; j < 32; j++) {
7181					sentry = &((const struct ctl_cmd_entry *)
7182					    entry->execute)[j];
7183					if (ctl_cmd_applicable(
7184					    lun->be_lun->lun_type, sentry))
7185						num++;
7186				}
7187			} else {
7188				if (ctl_cmd_applicable(lun->be_lun->lun_type,
7189				    entry))
7190					num++;
7191			}
7192		}
7193		total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7194		    num * sizeof(struct scsi_report_supported_opcodes_descr);
7195		break;
7196	case RSO_OPTIONS_OC:
7197		if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7198			ctl_set_invalid_field(/*ctsio*/ ctsio,
7199					      /*sks_valid*/ 1,
7200					      /*command*/ 1,
7201					      /*field*/ 2,
7202					      /*bit_valid*/ 1,
7203					      /*bit*/ 2);
7204			ctl_done((union ctl_io *)ctsio);
7205			return (CTL_RETVAL_COMPLETE);
7206		}
7207		total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7208		break;
7209	case RSO_OPTIONS_OC_SA:
7210		if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7211		    service_action >= 32) {
7212			ctl_set_invalid_field(/*ctsio*/ ctsio,
7213					      /*sks_valid*/ 1,
7214					      /*command*/ 1,
7215					      /*field*/ 2,
7216					      /*bit_valid*/ 1,
7217					      /*bit*/ 2);
7218			ctl_done((union ctl_io *)ctsio);
7219			return (CTL_RETVAL_COMPLETE);
7220		}
7221		total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7222		break;
7223	default:
7224		ctl_set_invalid_field(/*ctsio*/ ctsio,
7225				      /*sks_valid*/ 1,
7226				      /*command*/ 1,
7227				      /*field*/ 2,
7228				      /*bit_valid*/ 1,
7229				      /*bit*/ 2);
7230		ctl_done((union ctl_io *)ctsio);
7231		return (CTL_RETVAL_COMPLETE);
7232	}
7233
7234	alloc_len = scsi_4btoul(cdb->length);
7235
7236	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7237
7238	ctsio->kern_sg_entries = 0;
7239
7240	if (total_len < alloc_len) {
7241		ctsio->residual = alloc_len - total_len;
7242		ctsio->kern_data_len = total_len;
7243		ctsio->kern_total_len = total_len;
7244	} else {
7245		ctsio->residual = 0;
7246		ctsio->kern_data_len = alloc_len;
7247		ctsio->kern_total_len = alloc_len;
7248	}
7249	ctsio->kern_data_resid = 0;
7250	ctsio->kern_rel_offset = 0;
7251
7252	switch (cdb->options & RSO_OPTIONS_MASK) {
7253	case RSO_OPTIONS_ALL:
7254		all = (struct scsi_report_supported_opcodes_all *)
7255		    ctsio->kern_data_ptr;
7256		num = 0;
7257		for (i = 0; i < 256; i++) {
7258			entry = &ctl_cmd_table[i];
7259			if (entry->flags & CTL_CMD_FLAG_SA5) {
7260				for (j = 0; j < 32; j++) {
7261					sentry = &((const struct ctl_cmd_entry *)
7262					    entry->execute)[j];
7263					if (!ctl_cmd_applicable(
7264					    lun->be_lun->lun_type, sentry))
7265						continue;
7266					descr = &all->descr[num++];
7267					descr->opcode = i;
7268					scsi_ulto2b(j, descr->service_action);
7269					descr->flags = RSO_SERVACTV;
7270					scsi_ulto2b(sentry->length,
7271					    descr->cdb_length);
7272				}
7273			} else {
7274				if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7275				    entry))
7276					continue;
7277				descr = &all->descr[num++];
7278				descr->opcode = i;
7279				scsi_ulto2b(0, descr->service_action);
7280				descr->flags = 0;
7281				scsi_ulto2b(entry->length, descr->cdb_length);
7282			}
7283		}
7284		scsi_ulto4b(
7285		    num * sizeof(struct scsi_report_supported_opcodes_descr),
7286		    all->length);
7287		break;
7288	case RSO_OPTIONS_OC:
7289		one = (struct scsi_report_supported_opcodes_one *)
7290		    ctsio->kern_data_ptr;
7291		entry = &ctl_cmd_table[opcode];
7292		goto fill_one;
7293	case RSO_OPTIONS_OC_SA:
7294		one = (struct scsi_report_supported_opcodes_one *)
7295		    ctsio->kern_data_ptr;
7296		entry = &ctl_cmd_table[opcode];
7297		entry = &((const struct ctl_cmd_entry *)
7298		    entry->execute)[service_action];
7299fill_one:
7300		if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7301			one->support = 3;
7302			scsi_ulto2b(entry->length, one->cdb_length);
7303			one->cdb_usage[0] = opcode;
7304			memcpy(&one->cdb_usage[1], entry->usage,
7305			    entry->length - 1);
7306		} else
7307			one->support = 1;
7308		break;
7309	}
7310
7311	ctl_set_success(ctsio);
7312	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7313	ctsio->be_move_done = ctl_config_move_done;
7314	ctl_datamove((union ctl_io *)ctsio);
7315	return(retval);
7316}
7317
7318int
7319ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7320{
7321	struct scsi_report_supported_tmf *cdb;
7322	struct scsi_report_supported_tmf_data *data;
7323	int retval;
7324	int alloc_len, total_len;
7325
7326	CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7327
7328	cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7329
7330	retval = CTL_RETVAL_COMPLETE;
7331
7332	total_len = sizeof(struct scsi_report_supported_tmf_data);
7333	alloc_len = scsi_4btoul(cdb->length);
7334
7335	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7336
7337	ctsio->kern_sg_entries = 0;
7338
7339	if (total_len < alloc_len) {
7340		ctsio->residual = alloc_len - total_len;
7341		ctsio->kern_data_len = total_len;
7342		ctsio->kern_total_len = total_len;
7343	} else {
7344		ctsio->residual = 0;
7345		ctsio->kern_data_len = alloc_len;
7346		ctsio->kern_total_len = alloc_len;
7347	}
7348	ctsio->kern_data_resid = 0;
7349	ctsio->kern_rel_offset = 0;
7350
7351	data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7352	data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7353	data->byte2 |= RST_ITNRS;
7354
7355	ctl_set_success(ctsio);
7356	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7357	ctsio->be_move_done = ctl_config_move_done;
7358	ctl_datamove((union ctl_io *)ctsio);
7359	return (retval);
7360}
7361
7362int
7363ctl_report_timestamp(struct ctl_scsiio *ctsio)
7364{
7365	struct scsi_report_timestamp *cdb;
7366	struct scsi_report_timestamp_data *data;
7367	struct timeval tv;
7368	int64_t timestamp;
7369	int retval;
7370	int alloc_len, total_len;
7371
7372	CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7373
7374	cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7375
7376	retval = CTL_RETVAL_COMPLETE;
7377
7378	total_len = sizeof(struct scsi_report_timestamp_data);
7379	alloc_len = scsi_4btoul(cdb->length);
7380
7381	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7382
7383	ctsio->kern_sg_entries = 0;
7384
7385	if (total_len < alloc_len) {
7386		ctsio->residual = alloc_len - total_len;
7387		ctsio->kern_data_len = total_len;
7388		ctsio->kern_total_len = total_len;
7389	} else {
7390		ctsio->residual = 0;
7391		ctsio->kern_data_len = alloc_len;
7392		ctsio->kern_total_len = alloc_len;
7393	}
7394	ctsio->kern_data_resid = 0;
7395	ctsio->kern_rel_offset = 0;
7396
7397	data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7398	scsi_ulto2b(sizeof(*data) - 2, data->length);
7399	data->origin = RTS_ORIG_OUTSIDE;
7400	getmicrotime(&tv);
7401	timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7402	scsi_ulto4b(timestamp >> 16, data->timestamp);
7403	scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7404
7405	ctl_set_success(ctsio);
7406	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7407	ctsio->be_move_done = ctl_config_move_done;
7408	ctl_datamove((union ctl_io *)ctsio);
7409	return (retval);
7410}
7411
7412int
7413ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7414{
7415	struct scsi_per_res_in *cdb;
7416	int alloc_len, total_len = 0;
7417	/* struct scsi_per_res_in_rsrv in_data; */
7418	struct ctl_lun *lun;
7419	struct ctl_softc *softc;
7420	uint64_t key;
7421
7422	CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7423
7424	cdb = (struct scsi_per_res_in *)ctsio->cdb;
7425
7426	alloc_len = scsi_2btoul(cdb->length);
7427
7428	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7429	softc = lun->ctl_softc;
7430
7431retry:
7432	mtx_lock(&lun->lun_lock);
7433	switch (cdb->action) {
7434	case SPRI_RK: /* read keys */
7435		total_len = sizeof(struct scsi_per_res_in_keys) +
7436			lun->pr_key_count *
7437			sizeof(struct scsi_per_res_key);
7438		break;
7439	case SPRI_RR: /* read reservation */
7440		if (lun->flags & CTL_LUN_PR_RESERVED)
7441			total_len = sizeof(struct scsi_per_res_in_rsrv);
7442		else
7443			total_len = sizeof(struct scsi_per_res_in_header);
7444		break;
7445	case SPRI_RC: /* report capabilities */
7446		total_len = sizeof(struct scsi_per_res_cap);
7447		break;
7448	case SPRI_RS: /* read full status */
7449		total_len = sizeof(struct scsi_per_res_in_header) +
7450		    (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7451		    lun->pr_key_count;
7452		break;
7453	default:
7454		panic("Invalid PR type %x", cdb->action);
7455	}
7456	mtx_unlock(&lun->lun_lock);
7457
7458	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7459
7460	if (total_len < alloc_len) {
7461		ctsio->residual = alloc_len - total_len;
7462		ctsio->kern_data_len = total_len;
7463		ctsio->kern_total_len = total_len;
7464	} else {
7465		ctsio->residual = 0;
7466		ctsio->kern_data_len = alloc_len;
7467		ctsio->kern_total_len = alloc_len;
7468	}
7469
7470	ctsio->kern_data_resid = 0;
7471	ctsio->kern_rel_offset = 0;
7472	ctsio->kern_sg_entries = 0;
7473
7474	mtx_lock(&lun->lun_lock);
7475	switch (cdb->action) {
7476	case SPRI_RK: { // read keys
7477        struct scsi_per_res_in_keys *res_keys;
7478		int i, key_count;
7479
7480		res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7481
7482		/*
7483		 * We had to drop the lock to allocate our buffer, which
7484		 * leaves time for someone to come in with another
7485		 * persistent reservation.  (That is unlikely, though,
7486		 * since this should be the only persistent reservation
7487		 * command active right now.)
7488		 */
7489		if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7490		    (lun->pr_key_count *
7491		     sizeof(struct scsi_per_res_key)))){
7492			mtx_unlock(&lun->lun_lock);
7493			free(ctsio->kern_data_ptr, M_CTL);
7494			printf("%s: reservation length changed, retrying\n",
7495			       __func__);
7496			goto retry;
7497		}
7498
7499		scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7500
7501		scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7502			     lun->pr_key_count, res_keys->header.length);
7503
7504		for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7505			if ((key = ctl_get_prkey(lun, i)) == 0)
7506				continue;
7507
7508			/*
7509			 * We used lun->pr_key_count to calculate the
7510			 * size to allocate.  If it turns out the number of
7511			 * initiators with the registered flag set is
7512			 * larger than that (i.e. they haven't been kept in
7513			 * sync), we've got a problem.
7514			 */
7515			if (key_count >= lun->pr_key_count) {
7516#ifdef NEEDTOPORT
7517				csevent_log(CSC_CTL | CSC_SHELF_SW |
7518					    CTL_PR_ERROR,
7519					    csevent_LogType_Fault,
7520					    csevent_AlertLevel_Yellow,
7521					    csevent_FRU_ShelfController,
7522					    csevent_FRU_Firmware,
7523				        csevent_FRU_Unknown,
7524					    "registered keys %d >= key "
7525					    "count %d", key_count,
7526					    lun->pr_key_count);
7527#endif
7528				key_count++;
7529				continue;
7530			}
7531			scsi_u64to8b(key, res_keys->keys[key_count].key);
7532			key_count++;
7533		}
7534		break;
7535	}
7536	case SPRI_RR: { // read reservation
7537		struct scsi_per_res_in_rsrv *res;
7538		int tmp_len, header_only;
7539
7540		res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7541
7542		scsi_ulto4b(lun->PRGeneration, res->header.generation);
7543
7544		if (lun->flags & CTL_LUN_PR_RESERVED)
7545		{
7546			tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7547			scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7548				    res->header.length);
7549			header_only = 0;
7550		} else {
7551			tmp_len = sizeof(struct scsi_per_res_in_header);
7552			scsi_ulto4b(0, res->header.length);
7553			header_only = 1;
7554		}
7555
7556		/*
7557		 * We had to drop the lock to allocate our buffer, which
7558		 * leaves time for someone to come in with another
7559		 * persistent reservation.  (That is unlikely, though,
7560		 * since this should be the only persistent reservation
7561		 * command active right now.)
7562		 */
7563		if (tmp_len != total_len) {
7564			mtx_unlock(&lun->lun_lock);
7565			free(ctsio->kern_data_ptr, M_CTL);
7566			printf("%s: reservation status changed, retrying\n",
7567			       __func__);
7568			goto retry;
7569		}
7570
7571		/*
7572		 * No reservation held, so we're done.
7573		 */
7574		if (header_only != 0)
7575			break;
7576
7577		/*
7578		 * If the registration is an All Registrants type, the key
7579		 * is 0, since it doesn't really matter.
7580		 */
7581		if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7582			scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7583			    res->data.reservation);
7584		}
7585		res->data.scopetype = lun->res_type;
7586		break;
7587	}
7588	case SPRI_RC:     //report capabilities
7589	{
7590		struct scsi_per_res_cap *res_cap;
7591		uint16_t type_mask;
7592
7593		res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7594		scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7595		res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7596		type_mask = SPRI_TM_WR_EX_AR |
7597			    SPRI_TM_EX_AC_RO |
7598			    SPRI_TM_WR_EX_RO |
7599			    SPRI_TM_EX_AC |
7600			    SPRI_TM_WR_EX |
7601			    SPRI_TM_EX_AC_AR;
7602		scsi_ulto2b(type_mask, res_cap->type_mask);
7603		break;
7604	}
7605	case SPRI_RS: { // read full status
7606		struct scsi_per_res_in_full *res_status;
7607		struct scsi_per_res_in_full_desc *res_desc;
7608		struct ctl_port *port;
7609		int i, len;
7610
7611		res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7612
7613		/*
7614		 * We had to drop the lock to allocate our buffer, which
7615		 * leaves time for someone to come in with another
7616		 * persistent reservation.  (That is unlikely, though,
7617		 * since this should be the only persistent reservation
7618		 * command active right now.)
7619		 */
7620		if (total_len < (sizeof(struct scsi_per_res_in_header) +
7621		    (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7622		     lun->pr_key_count)){
7623			mtx_unlock(&lun->lun_lock);
7624			free(ctsio->kern_data_ptr, M_CTL);
7625			printf("%s: reservation length changed, retrying\n",
7626			       __func__);
7627			goto retry;
7628		}
7629
7630		scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7631
7632		res_desc = &res_status->desc[0];
7633		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7634			if ((key = ctl_get_prkey(lun, i)) == 0)
7635				continue;
7636
7637			scsi_u64to8b(key, res_desc->res_key.key);
7638			if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7639			    (lun->pr_res_idx == i ||
7640			     lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7641				res_desc->flags = SPRI_FULL_R_HOLDER;
7642				res_desc->scopetype = lun->res_type;
7643			}
7644			scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7645			    res_desc->rel_trgt_port_id);
7646			len = 0;
7647			port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7648			if (port != NULL)
7649				len = ctl_create_iid(port,
7650				    i % CTL_MAX_INIT_PER_PORT,
7651				    res_desc->transport_id);
7652			scsi_ulto4b(len, res_desc->additional_length);
7653			res_desc = (struct scsi_per_res_in_full_desc *)
7654			    &res_desc->transport_id[len];
7655		}
7656		scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7657		    res_status->header.length);
7658		break;
7659	}
7660	default:
7661		/*
7662		 * This is a bug, because we just checked for this above,
7663		 * and should have returned an error.
7664		 */
7665		panic("Invalid PR type %x", cdb->action);
7666		break; /* NOTREACHED */
7667	}
7668	mtx_unlock(&lun->lun_lock);
7669
7670	ctl_set_success(ctsio);
7671	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7672	ctsio->be_move_done = ctl_config_move_done;
7673	ctl_datamove((union ctl_io *)ctsio);
7674	return (CTL_RETVAL_COMPLETE);
7675}
7676
7677/*
7678 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7679 * it should return.
7680 */
7681static int
7682ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7683		uint64_t sa_res_key, uint8_t type, uint32_t residx,
7684		struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7685		struct scsi_per_res_out_parms* param)
7686{
7687	union ctl_ha_msg persis_io;
7688	int i;
7689
7690	mtx_lock(&lun->lun_lock);
7691	if (sa_res_key == 0) {
7692		if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7693			/* validate scope and type */
7694			if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7695			     SPR_LU_SCOPE) {
7696				mtx_unlock(&lun->lun_lock);
7697				ctl_set_invalid_field(/*ctsio*/ ctsio,
7698						      /*sks_valid*/ 1,
7699						      /*command*/ 1,
7700						      /*field*/ 2,
7701						      /*bit_valid*/ 1,
7702						      /*bit*/ 4);
7703				ctl_done((union ctl_io *)ctsio);
7704				return (1);
7705			}
7706
7707		        if (type>8 || type==2 || type==4 || type==0) {
7708				mtx_unlock(&lun->lun_lock);
7709				ctl_set_invalid_field(/*ctsio*/ ctsio,
7710       	           				      /*sks_valid*/ 1,
7711						      /*command*/ 1,
7712						      /*field*/ 2,
7713						      /*bit_valid*/ 1,
7714						      /*bit*/ 0);
7715				ctl_done((union ctl_io *)ctsio);
7716				return (1);
7717		        }
7718
7719			/*
7720			 * Unregister everybody else and build UA for
7721			 * them
7722			 */
7723			for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7724				if (i == residx || ctl_get_prkey(lun, i) == 0)
7725					continue;
7726
7727				ctl_clr_prkey(lun, i);
7728				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7729			}
7730			lun->pr_key_count = 1;
7731			lun->res_type = type;
7732			if (lun->res_type != SPR_TYPE_WR_EX_AR
7733			 && lun->res_type != SPR_TYPE_EX_AC_AR)
7734				lun->pr_res_idx = residx;
7735			lun->PRGeneration++;
7736			mtx_unlock(&lun->lun_lock);
7737
7738			/* send msg to other side */
7739			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7740			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7741			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7742			persis_io.pr.pr_info.residx = lun->pr_res_idx;
7743			persis_io.pr.pr_info.res_type = type;
7744			memcpy(persis_io.pr.pr_info.sa_res_key,
7745			       param->serv_act_res_key,
7746			       sizeof(param->serv_act_res_key));
7747			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7748			    sizeof(persis_io.pr), M_WAITOK);
7749		} else {
7750			/* not all registrants */
7751			mtx_unlock(&lun->lun_lock);
7752			free(ctsio->kern_data_ptr, M_CTL);
7753			ctl_set_invalid_field(ctsio,
7754					      /*sks_valid*/ 1,
7755					      /*command*/ 0,
7756					      /*field*/ 8,
7757					      /*bit_valid*/ 0,
7758					      /*bit*/ 0);
7759			ctl_done((union ctl_io *)ctsio);
7760			return (1);
7761		}
7762	} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7763		|| !(lun->flags & CTL_LUN_PR_RESERVED)) {
7764		int found = 0;
7765
7766		if (res_key == sa_res_key) {
7767			/* special case */
7768			/*
7769			 * The spec implies this is not good but doesn't
7770			 * say what to do. There are two choices either
7771			 * generate a res conflict or check condition
7772			 * with illegal field in parameter data. Since
7773			 * that is what is done when the sa_res_key is
7774			 * zero I'll take that approach since this has
7775			 * to do with the sa_res_key.
7776			 */
7777			mtx_unlock(&lun->lun_lock);
7778			free(ctsio->kern_data_ptr, M_CTL);
7779			ctl_set_invalid_field(ctsio,
7780					      /*sks_valid*/ 1,
7781					      /*command*/ 0,
7782					      /*field*/ 8,
7783					      /*bit_valid*/ 0,
7784					      /*bit*/ 0);
7785			ctl_done((union ctl_io *)ctsio);
7786			return (1);
7787		}
7788
7789		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7790			if (ctl_get_prkey(lun, i) != sa_res_key)
7791				continue;
7792
7793			found = 1;
7794			ctl_clr_prkey(lun, i);
7795			lun->pr_key_count--;
7796			ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7797		}
7798		if (!found) {
7799			mtx_unlock(&lun->lun_lock);
7800			free(ctsio->kern_data_ptr, M_CTL);
7801			ctl_set_reservation_conflict(ctsio);
7802			ctl_done((union ctl_io *)ctsio);
7803			return (CTL_RETVAL_COMPLETE);
7804		}
7805		lun->PRGeneration++;
7806		mtx_unlock(&lun->lun_lock);
7807
7808		/* send msg to other side */
7809		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7810		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7811		persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7812		persis_io.pr.pr_info.residx = lun->pr_res_idx;
7813		persis_io.pr.pr_info.res_type = type;
7814		memcpy(persis_io.pr.pr_info.sa_res_key,
7815		       param->serv_act_res_key,
7816		       sizeof(param->serv_act_res_key));
7817		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7818		    sizeof(persis_io.pr), M_WAITOK);
7819	} else {
7820		/* Reserved but not all registrants */
7821		/* sa_res_key is res holder */
7822		if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7823			/* validate scope and type */
7824			if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7825			     SPR_LU_SCOPE) {
7826				mtx_unlock(&lun->lun_lock);
7827				ctl_set_invalid_field(/*ctsio*/ ctsio,
7828						      /*sks_valid*/ 1,
7829						      /*command*/ 1,
7830						      /*field*/ 2,
7831						      /*bit_valid*/ 1,
7832						      /*bit*/ 4);
7833				ctl_done((union ctl_io *)ctsio);
7834				return (1);
7835			}
7836
7837			if (type>8 || type==2 || type==4 || type==0) {
7838				mtx_unlock(&lun->lun_lock);
7839				ctl_set_invalid_field(/*ctsio*/ ctsio,
7840						      /*sks_valid*/ 1,
7841						      /*command*/ 1,
7842						      /*field*/ 2,
7843						      /*bit_valid*/ 1,
7844						      /*bit*/ 0);
7845				ctl_done((union ctl_io *)ctsio);
7846				return (1);
7847			}
7848
7849			/*
7850			 * Do the following:
7851			 * if sa_res_key != res_key remove all
7852			 * registrants w/sa_res_key and generate UA
7853			 * for these registrants(Registrations
7854			 * Preempted) if it wasn't an exclusive
7855			 * reservation generate UA(Reservations
7856			 * Preempted) for all other registered nexuses
7857			 * if the type has changed. Establish the new
7858			 * reservation and holder. If res_key and
7859			 * sa_res_key are the same do the above
7860			 * except don't unregister the res holder.
7861			 */
7862
7863			for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7864				if (i == residx || ctl_get_prkey(lun, i) == 0)
7865					continue;
7866
7867				if (sa_res_key == ctl_get_prkey(lun, i)) {
7868					ctl_clr_prkey(lun, i);
7869					lun->pr_key_count--;
7870					ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7871				} else if (type != lun->res_type
7872					&& (lun->res_type == SPR_TYPE_WR_EX_RO
7873					 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7874					ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7875				}
7876			}
7877			lun->res_type = type;
7878			if (lun->res_type != SPR_TYPE_WR_EX_AR
7879			 && lun->res_type != SPR_TYPE_EX_AC_AR)
7880				lun->pr_res_idx = residx;
7881			else
7882				lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7883			lun->PRGeneration++;
7884			mtx_unlock(&lun->lun_lock);
7885
7886			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7887			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7888			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7889			persis_io.pr.pr_info.residx = lun->pr_res_idx;
7890			persis_io.pr.pr_info.res_type = type;
7891			memcpy(persis_io.pr.pr_info.sa_res_key,
7892			       param->serv_act_res_key,
7893			       sizeof(param->serv_act_res_key));
7894			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7895			    sizeof(persis_io.pr), M_WAITOK);
7896		} else {
7897			/*
7898			 * sa_res_key is not the res holder just
7899			 * remove registrants
7900			 */
7901			int found=0;
7902
7903			for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7904				if (sa_res_key != ctl_get_prkey(lun, i))
7905					continue;
7906
7907				found = 1;
7908				ctl_clr_prkey(lun, i);
7909				lun->pr_key_count--;
7910				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7911			}
7912
7913			if (!found) {
7914				mtx_unlock(&lun->lun_lock);
7915				free(ctsio->kern_data_ptr, M_CTL);
7916				ctl_set_reservation_conflict(ctsio);
7917				ctl_done((union ctl_io *)ctsio);
7918		        	return (1);
7919			}
7920			lun->PRGeneration++;
7921			mtx_unlock(&lun->lun_lock);
7922
7923			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7924			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7925			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7926			persis_io.pr.pr_info.residx = lun->pr_res_idx;
7927			persis_io.pr.pr_info.res_type = type;
7928			memcpy(persis_io.pr.pr_info.sa_res_key,
7929			       param->serv_act_res_key,
7930			       sizeof(param->serv_act_res_key));
7931			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7932			    sizeof(persis_io.pr), M_WAITOK);
7933		}
7934	}
7935	return (0);
7936}
7937
7938static void
7939ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7940{
7941	uint64_t sa_res_key;
7942	int i;
7943
7944	sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7945
7946	if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7947	 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7948	 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
7949		if (sa_res_key == 0) {
7950			/*
7951			 * Unregister everybody else and build UA for
7952			 * them
7953			 */
7954			for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7955				if (i == msg->pr.pr_info.residx ||
7956				    ctl_get_prkey(lun, i) == 0)
7957					continue;
7958
7959				ctl_clr_prkey(lun, i);
7960				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7961			}
7962
7963			lun->pr_key_count = 1;
7964			lun->res_type = msg->pr.pr_info.res_type;
7965			if (lun->res_type != SPR_TYPE_WR_EX_AR
7966			 && lun->res_type != SPR_TYPE_EX_AC_AR)
7967				lun->pr_res_idx = msg->pr.pr_info.residx;
7968		} else {
7969		        for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7970				if (sa_res_key == ctl_get_prkey(lun, i))
7971					continue;
7972
7973				ctl_clr_prkey(lun, i);
7974				lun->pr_key_count--;
7975				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7976			}
7977		}
7978	} else {
7979		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7980			if (i == msg->pr.pr_info.residx ||
7981			    ctl_get_prkey(lun, i) == 0)
7982				continue;
7983
7984			if (sa_res_key == ctl_get_prkey(lun, i)) {
7985				ctl_clr_prkey(lun, i);
7986				lun->pr_key_count--;
7987				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7988			} else if (msg->pr.pr_info.res_type != lun->res_type
7989				&& (lun->res_type == SPR_TYPE_WR_EX_RO
7990				 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7991				ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7992			}
7993		}
7994		lun->res_type = msg->pr.pr_info.res_type;
7995		if (lun->res_type != SPR_TYPE_WR_EX_AR
7996		 && lun->res_type != SPR_TYPE_EX_AC_AR)
7997			lun->pr_res_idx = msg->pr.pr_info.residx;
7998		else
7999			lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8000	}
8001	lun->PRGeneration++;
8002
8003}
8004
8005
8006int
8007ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8008{
8009	int retval;
8010	u_int32_t param_len;
8011	struct scsi_per_res_out *cdb;
8012	struct ctl_lun *lun;
8013	struct scsi_per_res_out_parms* param;
8014	struct ctl_softc *softc;
8015	uint32_t residx;
8016	uint64_t res_key, sa_res_key, key;
8017	uint8_t type;
8018	union ctl_ha_msg persis_io;
8019	int    i;
8020
8021	CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8022
8023	retval = CTL_RETVAL_COMPLETE;
8024
8025	cdb = (struct scsi_per_res_out *)ctsio->cdb;
8026	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8027	softc = lun->ctl_softc;
8028
8029	/*
8030	 * We only support whole-LUN scope.  The scope & type are ignored for
8031	 * register, register and ignore existing key and clear.
8032	 * We sometimes ignore scope and type on preempts too!!
8033	 * Verify reservation type here as well.
8034	 */
8035	type = cdb->scope_type & SPR_TYPE_MASK;
8036	if ((cdb->action == SPRO_RESERVE)
8037	 || (cdb->action == SPRO_RELEASE)) {
8038		if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8039			ctl_set_invalid_field(/*ctsio*/ ctsio,
8040					      /*sks_valid*/ 1,
8041					      /*command*/ 1,
8042					      /*field*/ 2,
8043					      /*bit_valid*/ 1,
8044					      /*bit*/ 4);
8045			ctl_done((union ctl_io *)ctsio);
8046			return (CTL_RETVAL_COMPLETE);
8047		}
8048
8049		if (type>8 || type==2 || type==4 || type==0) {
8050			ctl_set_invalid_field(/*ctsio*/ ctsio,
8051					      /*sks_valid*/ 1,
8052					      /*command*/ 1,
8053					      /*field*/ 2,
8054					      /*bit_valid*/ 1,
8055					      /*bit*/ 0);
8056			ctl_done((union ctl_io *)ctsio);
8057			return (CTL_RETVAL_COMPLETE);
8058		}
8059	}
8060
8061	param_len = scsi_4btoul(cdb->length);
8062
8063	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8064		ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8065		ctsio->kern_data_len = param_len;
8066		ctsio->kern_total_len = param_len;
8067		ctsio->kern_data_resid = 0;
8068		ctsio->kern_rel_offset = 0;
8069		ctsio->kern_sg_entries = 0;
8070		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8071		ctsio->be_move_done = ctl_config_move_done;
8072		ctl_datamove((union ctl_io *)ctsio);
8073
8074		return (CTL_RETVAL_COMPLETE);
8075	}
8076
8077	param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8078
8079	residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8080	res_key = scsi_8btou64(param->res_key.key);
8081	sa_res_key = scsi_8btou64(param->serv_act_res_key);
8082
8083	/*
8084	 * Validate the reservation key here except for SPRO_REG_IGNO
8085	 * This must be done for all other service actions
8086	 */
8087	if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8088		mtx_lock(&lun->lun_lock);
8089		if ((key = ctl_get_prkey(lun, residx)) != 0) {
8090			if (res_key != key) {
8091				/*
8092				 * The current key passed in doesn't match
8093				 * the one the initiator previously
8094				 * registered.
8095				 */
8096				mtx_unlock(&lun->lun_lock);
8097				free(ctsio->kern_data_ptr, M_CTL);
8098				ctl_set_reservation_conflict(ctsio);
8099				ctl_done((union ctl_io *)ctsio);
8100				return (CTL_RETVAL_COMPLETE);
8101			}
8102		} else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8103			/*
8104			 * We are not registered
8105			 */
8106			mtx_unlock(&lun->lun_lock);
8107			free(ctsio->kern_data_ptr, M_CTL);
8108			ctl_set_reservation_conflict(ctsio);
8109			ctl_done((union ctl_io *)ctsio);
8110			return (CTL_RETVAL_COMPLETE);
8111		} else if (res_key != 0) {
8112			/*
8113			 * We are not registered and trying to register but
8114			 * the register key isn't zero.
8115			 */
8116			mtx_unlock(&lun->lun_lock);
8117			free(ctsio->kern_data_ptr, M_CTL);
8118			ctl_set_reservation_conflict(ctsio);
8119			ctl_done((union ctl_io *)ctsio);
8120			return (CTL_RETVAL_COMPLETE);
8121		}
8122		mtx_unlock(&lun->lun_lock);
8123	}
8124
8125	switch (cdb->action & SPRO_ACTION_MASK) {
8126	case SPRO_REGISTER:
8127	case SPRO_REG_IGNO: {
8128
8129#if 0
8130		printf("Registration received\n");
8131#endif
8132
8133		/*
8134		 * We don't support any of these options, as we report in
8135		 * the read capabilities request (see
8136		 * ctl_persistent_reserve_in(), above).
8137		 */
8138		if ((param->flags & SPR_SPEC_I_PT)
8139		 || (param->flags & SPR_ALL_TG_PT)
8140		 || (param->flags & SPR_APTPL)) {
8141			int bit_ptr;
8142
8143			if (param->flags & SPR_APTPL)
8144				bit_ptr = 0;
8145			else if (param->flags & SPR_ALL_TG_PT)
8146				bit_ptr = 2;
8147			else /* SPR_SPEC_I_PT */
8148				bit_ptr = 3;
8149
8150			free(ctsio->kern_data_ptr, M_CTL);
8151			ctl_set_invalid_field(ctsio,
8152					      /*sks_valid*/ 1,
8153					      /*command*/ 0,
8154					      /*field*/ 20,
8155					      /*bit_valid*/ 1,
8156					      /*bit*/ bit_ptr);
8157			ctl_done((union ctl_io *)ctsio);
8158			return (CTL_RETVAL_COMPLETE);
8159		}
8160
8161		mtx_lock(&lun->lun_lock);
8162
8163		/*
8164		 * The initiator wants to clear the
8165		 * key/unregister.
8166		 */
8167		if (sa_res_key == 0) {
8168			if ((res_key == 0
8169			  && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8170			 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8171			  && ctl_get_prkey(lun, residx) == 0)) {
8172				mtx_unlock(&lun->lun_lock);
8173				goto done;
8174			}
8175
8176			ctl_clr_prkey(lun, residx);
8177			lun->pr_key_count--;
8178
8179			if (residx == lun->pr_res_idx) {
8180				lun->flags &= ~CTL_LUN_PR_RESERVED;
8181				lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8182
8183				if ((lun->res_type == SPR_TYPE_WR_EX_RO
8184				  || lun->res_type == SPR_TYPE_EX_AC_RO)
8185				 && lun->pr_key_count) {
8186					/*
8187					 * If the reservation is a registrants
8188					 * only type we need to generate a UA
8189					 * for other registered inits.  The
8190					 * sense code should be RESERVATIONS
8191					 * RELEASED
8192					 */
8193
8194					for (i = softc->init_min; i < softc->init_max; i++){
8195						if (ctl_get_prkey(lun, i) == 0)
8196							continue;
8197						ctl_est_ua(lun, i,
8198						    CTL_UA_RES_RELEASE);
8199					}
8200				}
8201				lun->res_type = 0;
8202			} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8203				if (lun->pr_key_count==0) {
8204					lun->flags &= ~CTL_LUN_PR_RESERVED;
8205					lun->res_type = 0;
8206					lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8207				}
8208			}
8209			lun->PRGeneration++;
8210			mtx_unlock(&lun->lun_lock);
8211
8212			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8213			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8214			persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8215			persis_io.pr.pr_info.residx = residx;
8216			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8217			    sizeof(persis_io.pr), M_WAITOK);
8218		} else /* sa_res_key != 0 */ {
8219
8220			/*
8221			 * If we aren't registered currently then increment
8222			 * the key count and set the registered flag.
8223			 */
8224			ctl_alloc_prkey(lun, residx);
8225			if (ctl_get_prkey(lun, residx) == 0)
8226				lun->pr_key_count++;
8227			ctl_set_prkey(lun, residx, sa_res_key);
8228			lun->PRGeneration++;
8229			mtx_unlock(&lun->lun_lock);
8230
8231			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8232			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8233			persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8234			persis_io.pr.pr_info.residx = residx;
8235			memcpy(persis_io.pr.pr_info.sa_res_key,
8236			       param->serv_act_res_key,
8237			       sizeof(param->serv_act_res_key));
8238			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8239			    sizeof(persis_io.pr), M_WAITOK);
8240		}
8241
8242		break;
8243	}
8244	case SPRO_RESERVE:
8245#if 0
8246                printf("Reserve executed type %d\n", type);
8247#endif
8248		mtx_lock(&lun->lun_lock);
8249		if (lun->flags & CTL_LUN_PR_RESERVED) {
8250			/*
8251			 * if this isn't the reservation holder and it's
8252			 * not a "all registrants" type or if the type is
8253			 * different then we have a conflict
8254			 */
8255			if ((lun->pr_res_idx != residx
8256			  && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8257			 || lun->res_type != type) {
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			mtx_unlock(&lun->lun_lock);
8265		} else /* create a reservation */ {
8266			/*
8267			 * If it's not an "all registrants" type record
8268			 * reservation holder
8269			 */
8270			if (type != SPR_TYPE_WR_EX_AR
8271			 && type != SPR_TYPE_EX_AC_AR)
8272				lun->pr_res_idx = residx; /* Res holder */
8273			else
8274				lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8275
8276			lun->flags |= CTL_LUN_PR_RESERVED;
8277			lun->res_type = type;
8278
8279			mtx_unlock(&lun->lun_lock);
8280
8281			/* send msg to other side */
8282			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8283			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8284			persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8285			persis_io.pr.pr_info.residx = lun->pr_res_idx;
8286			persis_io.pr.pr_info.res_type = type;
8287			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8288			    sizeof(persis_io.pr), M_WAITOK);
8289		}
8290		break;
8291
8292	case SPRO_RELEASE:
8293		mtx_lock(&lun->lun_lock);
8294		if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8295			/* No reservation exists return good status */
8296			mtx_unlock(&lun->lun_lock);
8297			goto done;
8298		}
8299		/*
8300		 * Is this nexus a reservation holder?
8301		 */
8302		if (lun->pr_res_idx != residx
8303		 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8304			/*
8305			 * not a res holder return good status but
8306			 * do nothing
8307			 */
8308			mtx_unlock(&lun->lun_lock);
8309			goto done;
8310		}
8311
8312		if (lun->res_type != type) {
8313			mtx_unlock(&lun->lun_lock);
8314			free(ctsio->kern_data_ptr, M_CTL);
8315			ctl_set_illegal_pr_release(ctsio);
8316			ctl_done((union ctl_io *)ctsio);
8317			return (CTL_RETVAL_COMPLETE);
8318		}
8319
8320		/* okay to release */
8321		lun->flags &= ~CTL_LUN_PR_RESERVED;
8322		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8323		lun->res_type = 0;
8324
8325		/*
8326		 * if this isn't an exclusive access
8327		 * res generate UA for all other
8328		 * registrants.
8329		 */
8330		if (type != SPR_TYPE_EX_AC
8331		 && type != SPR_TYPE_WR_EX) {
8332			for (i = softc->init_min; i < softc->init_max; i++) {
8333				if (i == residx || ctl_get_prkey(lun, i) == 0)
8334					continue;
8335				ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8336			}
8337		}
8338		mtx_unlock(&lun->lun_lock);
8339
8340		/* Send msg to other side */
8341		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8342		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8343		persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8344		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8345		     sizeof(persis_io.pr), M_WAITOK);
8346		break;
8347
8348	case SPRO_CLEAR:
8349		/* send msg to other side */
8350
8351		mtx_lock(&lun->lun_lock);
8352		lun->flags &= ~CTL_LUN_PR_RESERVED;
8353		lun->res_type = 0;
8354		lun->pr_key_count = 0;
8355		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8356
8357		ctl_clr_prkey(lun, residx);
8358		for (i = 0; i < CTL_MAX_INITIATORS; i++)
8359			if (ctl_get_prkey(lun, i) != 0) {
8360				ctl_clr_prkey(lun, i);
8361				ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8362			}
8363		lun->PRGeneration++;
8364		mtx_unlock(&lun->lun_lock);
8365
8366		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8367		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8368		persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8369		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8370		     sizeof(persis_io.pr), M_WAITOK);
8371		break;
8372
8373	case SPRO_PREEMPT:
8374	case SPRO_PRE_ABO: {
8375		int nretval;
8376
8377		nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8378					  residx, ctsio, cdb, param);
8379		if (nretval != 0)
8380			return (CTL_RETVAL_COMPLETE);
8381		break;
8382	}
8383	default:
8384		panic("Invalid PR type %x", cdb->action);
8385	}
8386
8387done:
8388	free(ctsio->kern_data_ptr, M_CTL);
8389	ctl_set_success(ctsio);
8390	ctl_done((union ctl_io *)ctsio);
8391
8392	return (retval);
8393}
8394
8395/*
8396 * This routine is for handling a message from the other SC pertaining to
8397 * persistent reserve out. All the error checking will have been done
8398 * so only perorming the action need be done here to keep the two
8399 * in sync.
8400 */
8401static void
8402ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8403{
8404	struct ctl_lun *lun;
8405	struct ctl_softc *softc;
8406	int i;
8407	uint32_t residx, targ_lun;
8408
8409	softc = control_softc;
8410	targ_lun = msg->hdr.nexus.targ_mapped_lun;
8411	mtx_lock(&softc->ctl_lock);
8412	if ((targ_lun >= CTL_MAX_LUNS) ||
8413	    ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8414		mtx_unlock(&softc->ctl_lock);
8415		return;
8416	}
8417	mtx_lock(&lun->lun_lock);
8418	mtx_unlock(&softc->ctl_lock);
8419	if (lun->flags & CTL_LUN_DISABLED) {
8420		mtx_unlock(&lun->lun_lock);
8421		return;
8422	}
8423	residx = ctl_get_initindex(&msg->hdr.nexus);
8424	switch(msg->pr.pr_info.action) {
8425	case CTL_PR_REG_KEY:
8426		ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8427		if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8428			lun->pr_key_count++;
8429		ctl_set_prkey(lun, msg->pr.pr_info.residx,
8430		    scsi_8btou64(msg->pr.pr_info.sa_res_key));
8431		lun->PRGeneration++;
8432		break;
8433
8434	case CTL_PR_UNREG_KEY:
8435		ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8436		lun->pr_key_count--;
8437
8438		/* XXX Need to see if the reservation has been released */
8439		/* if so do we need to generate UA? */
8440		if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8441			lun->flags &= ~CTL_LUN_PR_RESERVED;
8442			lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8443
8444			if ((lun->res_type == SPR_TYPE_WR_EX_RO
8445			  || lun->res_type == SPR_TYPE_EX_AC_RO)
8446			 && lun->pr_key_count) {
8447				/*
8448				 * If the reservation is a registrants
8449				 * only type we need to generate a UA
8450				 * for other registered inits.  The
8451				 * sense code should be RESERVATIONS
8452				 * RELEASED
8453				 */
8454
8455				for (i = softc->init_min; i < softc->init_max; i++) {
8456					if (ctl_get_prkey(lun, i) == 0)
8457						continue;
8458
8459					ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8460				}
8461			}
8462			lun->res_type = 0;
8463		} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8464			if (lun->pr_key_count==0) {
8465				lun->flags &= ~CTL_LUN_PR_RESERVED;
8466				lun->res_type = 0;
8467				lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8468			}
8469		}
8470		lun->PRGeneration++;
8471		break;
8472
8473	case CTL_PR_RESERVE:
8474		lun->flags |= CTL_LUN_PR_RESERVED;
8475		lun->res_type = msg->pr.pr_info.res_type;
8476		lun->pr_res_idx = msg->pr.pr_info.residx;
8477
8478		break;
8479
8480	case CTL_PR_RELEASE:
8481		/*
8482		 * if this isn't an exclusive access res generate UA for all
8483		 * other registrants.
8484		 */
8485		if (lun->res_type != SPR_TYPE_EX_AC
8486		 && lun->res_type != SPR_TYPE_WR_EX) {
8487			for (i = softc->init_min; i < softc->init_max; i++)
8488				if (i == residx || ctl_get_prkey(lun, i) == 0)
8489					continue;
8490				ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8491		}
8492
8493		lun->flags &= ~CTL_LUN_PR_RESERVED;
8494		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8495		lun->res_type = 0;
8496		break;
8497
8498	case CTL_PR_PREEMPT:
8499		ctl_pro_preempt_other(lun, msg);
8500		break;
8501	case CTL_PR_CLEAR:
8502		lun->flags &= ~CTL_LUN_PR_RESERVED;
8503		lun->res_type = 0;
8504		lun->pr_key_count = 0;
8505		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8506
8507		for (i=0; i < CTL_MAX_INITIATORS; i++) {
8508			if (ctl_get_prkey(lun, i) == 0)
8509				continue;
8510			ctl_clr_prkey(lun, i);
8511			ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8512		}
8513		lun->PRGeneration++;
8514		break;
8515	}
8516
8517	mtx_unlock(&lun->lun_lock);
8518}
8519
8520int
8521ctl_read_write(struct ctl_scsiio *ctsio)
8522{
8523	struct ctl_lun *lun;
8524	struct ctl_lba_len_flags *lbalen;
8525	uint64_t lba;
8526	uint32_t num_blocks;
8527	int flags, retval;
8528	int isread;
8529
8530	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8531
8532	CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8533
8534	flags = 0;
8535	retval = CTL_RETVAL_COMPLETE;
8536
8537	isread = ctsio->cdb[0] == READ_6  || ctsio->cdb[0] == READ_10
8538	      || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8539	switch (ctsio->cdb[0]) {
8540	case READ_6:
8541	case WRITE_6: {
8542		struct scsi_rw_6 *cdb;
8543
8544		cdb = (struct scsi_rw_6 *)ctsio->cdb;
8545
8546		lba = scsi_3btoul(cdb->addr);
8547		/* only 5 bits are valid in the most significant address byte */
8548		lba &= 0x1fffff;
8549		num_blocks = cdb->length;
8550		/*
8551		 * This is correct according to SBC-2.
8552		 */
8553		if (num_blocks == 0)
8554			num_blocks = 256;
8555		break;
8556	}
8557	case READ_10:
8558	case WRITE_10: {
8559		struct scsi_rw_10 *cdb;
8560
8561		cdb = (struct scsi_rw_10 *)ctsio->cdb;
8562		if (cdb->byte2 & SRW10_FUA)
8563			flags |= CTL_LLF_FUA;
8564		if (cdb->byte2 & SRW10_DPO)
8565			flags |= CTL_LLF_DPO;
8566		lba = scsi_4btoul(cdb->addr);
8567		num_blocks = scsi_2btoul(cdb->length);
8568		break;
8569	}
8570	case WRITE_VERIFY_10: {
8571		struct scsi_write_verify_10 *cdb;
8572
8573		cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8574		flags |= CTL_LLF_FUA;
8575		if (cdb->byte2 & SWV_DPO)
8576			flags |= CTL_LLF_DPO;
8577		lba = scsi_4btoul(cdb->addr);
8578		num_blocks = scsi_2btoul(cdb->length);
8579		break;
8580	}
8581	case READ_12:
8582	case WRITE_12: {
8583		struct scsi_rw_12 *cdb;
8584
8585		cdb = (struct scsi_rw_12 *)ctsio->cdb;
8586		if (cdb->byte2 & SRW12_FUA)
8587			flags |= CTL_LLF_FUA;
8588		if (cdb->byte2 & SRW12_DPO)
8589			flags |= CTL_LLF_DPO;
8590		lba = scsi_4btoul(cdb->addr);
8591		num_blocks = scsi_4btoul(cdb->length);
8592		break;
8593	}
8594	case WRITE_VERIFY_12: {
8595		struct scsi_write_verify_12 *cdb;
8596
8597		cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8598		flags |= CTL_LLF_FUA;
8599		if (cdb->byte2 & SWV_DPO)
8600			flags |= CTL_LLF_DPO;
8601		lba = scsi_4btoul(cdb->addr);
8602		num_blocks = scsi_4btoul(cdb->length);
8603		break;
8604	}
8605	case READ_16:
8606	case WRITE_16: {
8607		struct scsi_rw_16 *cdb;
8608
8609		cdb = (struct scsi_rw_16 *)ctsio->cdb;
8610		if (cdb->byte2 & SRW12_FUA)
8611			flags |= CTL_LLF_FUA;
8612		if (cdb->byte2 & SRW12_DPO)
8613			flags |= CTL_LLF_DPO;
8614		lba = scsi_8btou64(cdb->addr);
8615		num_blocks = scsi_4btoul(cdb->length);
8616		break;
8617	}
8618	case WRITE_ATOMIC_16: {
8619		struct scsi_rw_16 *cdb;
8620
8621		if (lun->be_lun->atomicblock == 0) {
8622			ctl_set_invalid_opcode(ctsio);
8623			ctl_done((union ctl_io *)ctsio);
8624			return (CTL_RETVAL_COMPLETE);
8625		}
8626
8627		cdb = (struct scsi_rw_16 *)ctsio->cdb;
8628		if (cdb->byte2 & SRW12_FUA)
8629			flags |= CTL_LLF_FUA;
8630		if (cdb->byte2 & SRW12_DPO)
8631			flags |= CTL_LLF_DPO;
8632		lba = scsi_8btou64(cdb->addr);
8633		num_blocks = scsi_4btoul(cdb->length);
8634		if (num_blocks > lun->be_lun->atomicblock) {
8635			ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8636			    /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8637			    /*bit*/ 0);
8638			ctl_done((union ctl_io *)ctsio);
8639			return (CTL_RETVAL_COMPLETE);
8640		}
8641		break;
8642	}
8643	case WRITE_VERIFY_16: {
8644		struct scsi_write_verify_16 *cdb;
8645
8646		cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8647		flags |= CTL_LLF_FUA;
8648		if (cdb->byte2 & SWV_DPO)
8649			flags |= CTL_LLF_DPO;
8650		lba = scsi_8btou64(cdb->addr);
8651		num_blocks = scsi_4btoul(cdb->length);
8652		break;
8653	}
8654	default:
8655		/*
8656		 * We got a command we don't support.  This shouldn't
8657		 * happen, commands should be filtered out above us.
8658		 */
8659		ctl_set_invalid_opcode(ctsio);
8660		ctl_done((union ctl_io *)ctsio);
8661
8662		return (CTL_RETVAL_COMPLETE);
8663		break; /* NOTREACHED */
8664	}
8665
8666	/*
8667	 * The first check is to make sure we're in bounds, the second
8668	 * check is to catch wrap-around problems.  If the lba + num blocks
8669	 * is less than the lba, then we've wrapped around and the block
8670	 * range is invalid anyway.
8671	 */
8672	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8673	 || ((lba + num_blocks) < lba)) {
8674		ctl_set_lba_out_of_range(ctsio);
8675		ctl_done((union ctl_io *)ctsio);
8676		return (CTL_RETVAL_COMPLETE);
8677	}
8678
8679	/*
8680	 * According to SBC-3, a transfer length of 0 is not an error.
8681	 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8682	 * translates to 256 blocks for those commands.
8683	 */
8684	if (num_blocks == 0) {
8685		ctl_set_success(ctsio);
8686		ctl_done((union ctl_io *)ctsio);
8687		return (CTL_RETVAL_COMPLETE);
8688	}
8689
8690	/* Set FUA and/or DPO if caches are disabled. */
8691	if (isread) {
8692		if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8693		    SCP_RCD) != 0)
8694			flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8695	} else {
8696		if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8697		    SCP_WCE) == 0)
8698			flags |= CTL_LLF_FUA;
8699	}
8700
8701	lbalen = (struct ctl_lba_len_flags *)
8702	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8703	lbalen->lba = lba;
8704	lbalen->len = num_blocks;
8705	lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8706
8707	ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8708	ctsio->kern_rel_offset = 0;
8709
8710	CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8711
8712	retval = lun->backend->data_submit((union ctl_io *)ctsio);
8713
8714	return (retval);
8715}
8716
8717static int
8718ctl_cnw_cont(union ctl_io *io)
8719{
8720	struct ctl_scsiio *ctsio;
8721	struct ctl_lun *lun;
8722	struct ctl_lba_len_flags *lbalen;
8723	int retval;
8724
8725	ctsio = &io->scsiio;
8726	ctsio->io_hdr.status = CTL_STATUS_NONE;
8727	ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8728	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8729	lbalen = (struct ctl_lba_len_flags *)
8730	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8731	lbalen->flags &= ~CTL_LLF_COMPARE;
8732	lbalen->flags |= CTL_LLF_WRITE;
8733
8734	CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8735	retval = lun->backend->data_submit((union ctl_io *)ctsio);
8736	return (retval);
8737}
8738
8739int
8740ctl_cnw(struct ctl_scsiio *ctsio)
8741{
8742	struct ctl_lun *lun;
8743	struct ctl_lba_len_flags *lbalen;
8744	uint64_t lba;
8745	uint32_t num_blocks;
8746	int flags, retval;
8747
8748	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8749
8750	CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8751
8752	flags = 0;
8753	retval = CTL_RETVAL_COMPLETE;
8754
8755	switch (ctsio->cdb[0]) {
8756	case COMPARE_AND_WRITE: {
8757		struct scsi_compare_and_write *cdb;
8758
8759		cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8760		if (cdb->byte2 & SRW10_FUA)
8761			flags |= CTL_LLF_FUA;
8762		if (cdb->byte2 & SRW10_DPO)
8763			flags |= CTL_LLF_DPO;
8764		lba = scsi_8btou64(cdb->addr);
8765		num_blocks = cdb->length;
8766		break;
8767	}
8768	default:
8769		/*
8770		 * We got a command we don't support.  This shouldn't
8771		 * happen, commands should be filtered out above us.
8772		 */
8773		ctl_set_invalid_opcode(ctsio);
8774		ctl_done((union ctl_io *)ctsio);
8775
8776		return (CTL_RETVAL_COMPLETE);
8777		break; /* NOTREACHED */
8778	}
8779
8780	/*
8781	 * The first check is to make sure we're in bounds, the second
8782	 * check is to catch wrap-around problems.  If the lba + num blocks
8783	 * is less than the lba, then we've wrapped around and the block
8784	 * range is invalid anyway.
8785	 */
8786	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8787	 || ((lba + num_blocks) < lba)) {
8788		ctl_set_lba_out_of_range(ctsio);
8789		ctl_done((union ctl_io *)ctsio);
8790		return (CTL_RETVAL_COMPLETE);
8791	}
8792
8793	/*
8794	 * According to SBC-3, a transfer length of 0 is not an error.
8795	 */
8796	if (num_blocks == 0) {
8797		ctl_set_success(ctsio);
8798		ctl_done((union ctl_io *)ctsio);
8799		return (CTL_RETVAL_COMPLETE);
8800	}
8801
8802	/* Set FUA if write cache is disabled. */
8803	if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8804	    SCP_WCE) == 0)
8805		flags |= CTL_LLF_FUA;
8806
8807	ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8808	ctsio->kern_rel_offset = 0;
8809
8810	/*
8811	 * Set the IO_CONT flag, so that if this I/O gets passed to
8812	 * ctl_data_submit_done(), it'll get passed back to
8813	 * ctl_ctl_cnw_cont() for further processing.
8814	 */
8815	ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8816	ctsio->io_cont = ctl_cnw_cont;
8817
8818	lbalen = (struct ctl_lba_len_flags *)
8819	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8820	lbalen->lba = lba;
8821	lbalen->len = num_blocks;
8822	lbalen->flags = CTL_LLF_COMPARE | flags;
8823
8824	CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8825	retval = lun->backend->data_submit((union ctl_io *)ctsio);
8826	return (retval);
8827}
8828
8829int
8830ctl_verify(struct ctl_scsiio *ctsio)
8831{
8832	struct ctl_lun *lun;
8833	struct ctl_lba_len_flags *lbalen;
8834	uint64_t lba;
8835	uint32_t num_blocks;
8836	int bytchk, flags;
8837	int retval;
8838
8839	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8840
8841	CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8842
8843	bytchk = 0;
8844	flags = CTL_LLF_FUA;
8845	retval = CTL_RETVAL_COMPLETE;
8846
8847	switch (ctsio->cdb[0]) {
8848	case VERIFY_10: {
8849		struct scsi_verify_10 *cdb;
8850
8851		cdb = (struct scsi_verify_10 *)ctsio->cdb;
8852		if (cdb->byte2 & SVFY_BYTCHK)
8853			bytchk = 1;
8854		if (cdb->byte2 & SVFY_DPO)
8855			flags |= CTL_LLF_DPO;
8856		lba = scsi_4btoul(cdb->addr);
8857		num_blocks = scsi_2btoul(cdb->length);
8858		break;
8859	}
8860	case VERIFY_12: {
8861		struct scsi_verify_12 *cdb;
8862
8863		cdb = (struct scsi_verify_12 *)ctsio->cdb;
8864		if (cdb->byte2 & SVFY_BYTCHK)
8865			bytchk = 1;
8866		if (cdb->byte2 & SVFY_DPO)
8867			flags |= CTL_LLF_DPO;
8868		lba = scsi_4btoul(cdb->addr);
8869		num_blocks = scsi_4btoul(cdb->length);
8870		break;
8871	}
8872	case VERIFY_16: {
8873		struct scsi_rw_16 *cdb;
8874
8875		cdb = (struct scsi_rw_16 *)ctsio->cdb;
8876		if (cdb->byte2 & SVFY_BYTCHK)
8877			bytchk = 1;
8878		if (cdb->byte2 & SVFY_DPO)
8879			flags |= CTL_LLF_DPO;
8880		lba = scsi_8btou64(cdb->addr);
8881		num_blocks = scsi_4btoul(cdb->length);
8882		break;
8883	}
8884	default:
8885		/*
8886		 * We got a command we don't support.  This shouldn't
8887		 * happen, commands should be filtered out above us.
8888		 */
8889		ctl_set_invalid_opcode(ctsio);
8890		ctl_done((union ctl_io *)ctsio);
8891		return (CTL_RETVAL_COMPLETE);
8892	}
8893
8894	/*
8895	 * The first check is to make sure we're in bounds, the second
8896	 * check is to catch wrap-around problems.  If the lba + num blocks
8897	 * is less than the lba, then we've wrapped around and the block
8898	 * range is invalid anyway.
8899	 */
8900	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8901	 || ((lba + num_blocks) < lba)) {
8902		ctl_set_lba_out_of_range(ctsio);
8903		ctl_done((union ctl_io *)ctsio);
8904		return (CTL_RETVAL_COMPLETE);
8905	}
8906
8907	/*
8908	 * According to SBC-3, a transfer length of 0 is not an error.
8909	 */
8910	if (num_blocks == 0) {
8911		ctl_set_success(ctsio);
8912		ctl_done((union ctl_io *)ctsio);
8913		return (CTL_RETVAL_COMPLETE);
8914	}
8915
8916	lbalen = (struct ctl_lba_len_flags *)
8917	    &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8918	lbalen->lba = lba;
8919	lbalen->len = num_blocks;
8920	if (bytchk) {
8921		lbalen->flags = CTL_LLF_COMPARE | flags;
8922		ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8923	} else {
8924		lbalen->flags = CTL_LLF_VERIFY | flags;
8925		ctsio->kern_total_len = 0;
8926	}
8927	ctsio->kern_rel_offset = 0;
8928
8929	CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8930	retval = lun->backend->data_submit((union ctl_io *)ctsio);
8931	return (retval);
8932}
8933
8934int
8935ctl_report_luns(struct ctl_scsiio *ctsio)
8936{
8937	struct ctl_softc *softc = control_softc;
8938	struct scsi_report_luns *cdb;
8939	struct scsi_report_luns_data *lun_data;
8940	struct ctl_lun *lun, *request_lun;
8941	struct ctl_port *port;
8942	int num_luns, retval;
8943	uint32_t alloc_len, lun_datalen;
8944	int num_filled, well_known;
8945	uint32_t initidx, targ_lun_id, lun_id;
8946
8947	retval = CTL_RETVAL_COMPLETE;
8948	well_known = 0;
8949
8950	cdb = (struct scsi_report_luns *)ctsio->cdb;
8951	port = ctl_io_port(&ctsio->io_hdr);
8952
8953	CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8954
8955	mtx_lock(&softc->ctl_lock);
8956	num_luns = 0;
8957	for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
8958		if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
8959			num_luns++;
8960	}
8961	mtx_unlock(&softc->ctl_lock);
8962
8963	switch (cdb->select_report) {
8964	case RPL_REPORT_DEFAULT:
8965	case RPL_REPORT_ALL:
8966		break;
8967	case RPL_REPORT_WELLKNOWN:
8968		well_known = 1;
8969		num_luns = 0;
8970		break;
8971	default:
8972		ctl_set_invalid_field(ctsio,
8973				      /*sks_valid*/ 1,
8974				      /*command*/ 1,
8975				      /*field*/ 2,
8976				      /*bit_valid*/ 0,
8977				      /*bit*/ 0);
8978		ctl_done((union ctl_io *)ctsio);
8979		return (retval);
8980		break; /* NOTREACHED */
8981	}
8982
8983	alloc_len = scsi_4btoul(cdb->length);
8984	/*
8985	 * The initiator has to allocate at least 16 bytes for this request,
8986	 * so he can at least get the header and the first LUN.  Otherwise
8987	 * we reject the request (per SPC-3 rev 14, section 6.21).
8988	 */
8989	if (alloc_len < (sizeof(struct scsi_report_luns_data) +
8990	    sizeof(struct scsi_report_luns_lundata))) {
8991		ctl_set_invalid_field(ctsio,
8992				      /*sks_valid*/ 1,
8993				      /*command*/ 1,
8994				      /*field*/ 6,
8995				      /*bit_valid*/ 0,
8996				      /*bit*/ 0);
8997		ctl_done((union ctl_io *)ctsio);
8998		return (retval);
8999	}
9000
9001	request_lun = (struct ctl_lun *)
9002		ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9003
9004	lun_datalen = sizeof(*lun_data) +
9005		(num_luns * sizeof(struct scsi_report_luns_lundata));
9006
9007	ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9008	lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9009	ctsio->kern_sg_entries = 0;
9010
9011	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9012
9013	mtx_lock(&softc->ctl_lock);
9014	for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9015		lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9016		if (lun_id >= CTL_MAX_LUNS)
9017			continue;
9018		lun = softc->ctl_luns[lun_id];
9019		if (lun == NULL)
9020			continue;
9021
9022		if (targ_lun_id <= 0xff) {
9023			/*
9024			 * Peripheral addressing method, bus number 0.
9025			 */
9026			lun_data->luns[num_filled].lundata[0] =
9027				RPL_LUNDATA_ATYP_PERIPH;
9028			lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9029			num_filled++;
9030		} else if (targ_lun_id <= 0x3fff) {
9031			/*
9032			 * Flat addressing method.
9033			 */
9034			lun_data->luns[num_filled].lundata[0] =
9035				RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9036			lun_data->luns[num_filled].lundata[1] =
9037				(targ_lun_id & 0xff);
9038			num_filled++;
9039		} else if (targ_lun_id <= 0xffffff) {
9040			/*
9041			 * Extended flat addressing method.
9042			 */
9043			lun_data->luns[num_filled].lundata[0] =
9044			    RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9045			scsi_ulto3b(targ_lun_id,
9046			    &lun_data->luns[num_filled].lundata[1]);
9047			num_filled++;
9048		} else {
9049			printf("ctl_report_luns: bogus LUN number %jd, "
9050			       "skipping\n", (intmax_t)targ_lun_id);
9051		}
9052		/*
9053		 * According to SPC-3, rev 14 section 6.21:
9054		 *
9055		 * "The execution of a REPORT LUNS command to any valid and
9056		 * installed logical unit shall clear the REPORTED LUNS DATA
9057		 * HAS CHANGED unit attention condition for all logical
9058		 * units of that target with respect to the requesting
9059		 * initiator. A valid and installed logical unit is one
9060		 * having a PERIPHERAL QUALIFIER of 000b in the standard
9061		 * INQUIRY data (see 6.4.2)."
9062		 *
9063		 * If request_lun is NULL, the LUN this report luns command
9064		 * was issued to is either disabled or doesn't exist. In that
9065		 * case, we shouldn't clear any pending lun change unit
9066		 * attention.
9067		 */
9068		if (request_lun != NULL) {
9069			mtx_lock(&lun->lun_lock);
9070			ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9071			mtx_unlock(&lun->lun_lock);
9072		}
9073	}
9074	mtx_unlock(&softc->ctl_lock);
9075
9076	/*
9077	 * It's quite possible that we've returned fewer LUNs than we allocated
9078	 * space for.  Trim it.
9079	 */
9080	lun_datalen = sizeof(*lun_data) +
9081		(num_filled * sizeof(struct scsi_report_luns_lundata));
9082
9083	if (lun_datalen < alloc_len) {
9084		ctsio->residual = alloc_len - lun_datalen;
9085		ctsio->kern_data_len = lun_datalen;
9086		ctsio->kern_total_len = lun_datalen;
9087	} else {
9088		ctsio->residual = 0;
9089		ctsio->kern_data_len = alloc_len;
9090		ctsio->kern_total_len = alloc_len;
9091	}
9092	ctsio->kern_data_resid = 0;
9093	ctsio->kern_rel_offset = 0;
9094	ctsio->kern_sg_entries = 0;
9095
9096	/*
9097	 * We set this to the actual data length, regardless of how much
9098	 * space we actually have to return results.  If the user looks at
9099	 * this value, he'll know whether or not he allocated enough space
9100	 * and reissue the command if necessary.  We don't support well
9101	 * known logical units, so if the user asks for that, return none.
9102	 */
9103	scsi_ulto4b(lun_datalen - 8, lun_data->length);
9104
9105	/*
9106	 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9107	 * this request.
9108	 */
9109	ctl_set_success(ctsio);
9110	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9111	ctsio->be_move_done = ctl_config_move_done;
9112	ctl_datamove((union ctl_io *)ctsio);
9113	return (retval);
9114}
9115
9116int
9117ctl_request_sense(struct ctl_scsiio *ctsio)
9118{
9119	struct scsi_request_sense *cdb;
9120	struct scsi_sense_data *sense_ptr;
9121	struct ctl_softc *ctl_softc;
9122	struct ctl_lun *lun;
9123	uint32_t initidx;
9124	int have_error;
9125	scsi_sense_data_type sense_format;
9126	ctl_ua_type ua_type;
9127
9128	cdb = (struct scsi_request_sense *)ctsio->cdb;
9129
9130	ctl_softc = control_softc;
9131	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9132
9133	CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9134
9135	/*
9136	 * Determine which sense format the user wants.
9137	 */
9138	if (cdb->byte2 & SRS_DESC)
9139		sense_format = SSD_TYPE_DESC;
9140	else
9141		sense_format = SSD_TYPE_FIXED;
9142
9143	ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9144	sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9145	ctsio->kern_sg_entries = 0;
9146
9147	/*
9148	 * struct scsi_sense_data, which is currently set to 256 bytes, is
9149	 * larger than the largest allowed value for the length field in the
9150	 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9151	 */
9152	ctsio->residual = 0;
9153	ctsio->kern_data_len = cdb->length;
9154	ctsio->kern_total_len = cdb->length;
9155
9156	ctsio->kern_data_resid = 0;
9157	ctsio->kern_rel_offset = 0;
9158	ctsio->kern_sg_entries = 0;
9159
9160	/*
9161	 * If we don't have a LUN, we don't have any pending sense.
9162	 */
9163	if (lun == NULL)
9164		goto no_sense;
9165
9166	have_error = 0;
9167	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9168	/*
9169	 * Check for pending sense, and then for pending unit attentions.
9170	 * Pending sense gets returned first, then pending unit attentions.
9171	 */
9172	mtx_lock(&lun->lun_lock);
9173#ifdef CTL_WITH_CA
9174	if (ctl_is_set(lun->have_ca, initidx)) {
9175		scsi_sense_data_type stored_format;
9176
9177		/*
9178		 * Check to see which sense format was used for the stored
9179		 * sense data.
9180		 */
9181		stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9182
9183		/*
9184		 * If the user requested a different sense format than the
9185		 * one we stored, then we need to convert it to the other
9186		 * format.  If we're going from descriptor to fixed format
9187		 * sense data, we may lose things in translation, depending
9188		 * on what options were used.
9189		 *
9190		 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9191		 * for some reason we'll just copy it out as-is.
9192		 */
9193		if ((stored_format == SSD_TYPE_FIXED)
9194		 && (sense_format == SSD_TYPE_DESC))
9195			ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9196			    &lun->pending_sense[initidx],
9197			    (struct scsi_sense_data_desc *)sense_ptr);
9198		else if ((stored_format == SSD_TYPE_DESC)
9199		      && (sense_format == SSD_TYPE_FIXED))
9200			ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9201			    &lun->pending_sense[initidx],
9202			    (struct scsi_sense_data_fixed *)sense_ptr);
9203		else
9204			memcpy(sense_ptr, &lun->pending_sense[initidx],
9205			       MIN(sizeof(*sense_ptr),
9206			       sizeof(lun->pending_sense[initidx])));
9207
9208		ctl_clear_mask(lun->have_ca, initidx);
9209		have_error = 1;
9210	} else
9211#endif
9212	{
9213		ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9214		if (ua_type != CTL_UA_NONE)
9215			have_error = 1;
9216		if (ua_type == CTL_UA_LUN_CHANGE) {
9217			mtx_unlock(&lun->lun_lock);
9218			mtx_lock(&ctl_softc->ctl_lock);
9219			ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9220			mtx_unlock(&ctl_softc->ctl_lock);
9221			mtx_lock(&lun->lun_lock);
9222		}
9223
9224	}
9225	mtx_unlock(&lun->lun_lock);
9226
9227	/*
9228	 * We already have a pending error, return it.
9229	 */
9230	if (have_error != 0) {
9231		/*
9232		 * We report the SCSI status as OK, since the status of the
9233		 * request sense command itself is OK.
9234		 * We report 0 for the sense length, because we aren't doing
9235		 * autosense in this case.  We're reporting sense as
9236		 * parameter data.
9237		 */
9238		ctl_set_success(ctsio);
9239		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9240		ctsio->be_move_done = ctl_config_move_done;
9241		ctl_datamove((union ctl_io *)ctsio);
9242		return (CTL_RETVAL_COMPLETE);
9243	}
9244
9245no_sense:
9246
9247	/*
9248	 * No sense information to report, so we report that everything is
9249	 * okay.
9250	 */
9251	ctl_set_sense_data(sense_ptr,
9252			   lun,
9253			   sense_format,
9254			   /*current_error*/ 1,
9255			   /*sense_key*/ SSD_KEY_NO_SENSE,
9256			   /*asc*/ 0x00,
9257			   /*ascq*/ 0x00,
9258			   SSD_ELEM_NONE);
9259
9260	/*
9261	 * We report 0 for the sense length, because we aren't doing
9262	 * autosense in this case.  We're reporting sense as parameter data.
9263	 */
9264	ctl_set_success(ctsio);
9265	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9266	ctsio->be_move_done = ctl_config_move_done;
9267	ctl_datamove((union ctl_io *)ctsio);
9268	return (CTL_RETVAL_COMPLETE);
9269}
9270
9271int
9272ctl_tur(struct ctl_scsiio *ctsio)
9273{
9274
9275	CTL_DEBUG_PRINT(("ctl_tur\n"));
9276
9277	ctl_set_success(ctsio);
9278	ctl_done((union ctl_io *)ctsio);
9279
9280	return (CTL_RETVAL_COMPLETE);
9281}
9282
9283/*
9284 * SCSI VPD page 0x00, the Supported VPD Pages page.
9285 */
9286static int
9287ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9288{
9289	struct scsi_vpd_supported_pages *pages;
9290	int sup_page_size;
9291	struct ctl_lun *lun;
9292	int p;
9293
9294	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9295
9296	sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9297	    SCSI_EVPD_NUM_SUPPORTED_PAGES;
9298	ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9299	pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9300	ctsio->kern_sg_entries = 0;
9301
9302	if (sup_page_size < alloc_len) {
9303		ctsio->residual = alloc_len - sup_page_size;
9304		ctsio->kern_data_len = sup_page_size;
9305		ctsio->kern_total_len = sup_page_size;
9306	} else {
9307		ctsio->residual = 0;
9308		ctsio->kern_data_len = alloc_len;
9309		ctsio->kern_total_len = alloc_len;
9310	}
9311	ctsio->kern_data_resid = 0;
9312	ctsio->kern_rel_offset = 0;
9313	ctsio->kern_sg_entries = 0;
9314
9315	/*
9316	 * The control device is always connected.  The disk device, on the
9317	 * other hand, may not be online all the time.  Need to change this
9318	 * to figure out whether the disk device is actually online or not.
9319	 */
9320	if (lun != NULL)
9321		pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9322				lun->be_lun->lun_type;
9323	else
9324		pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9325
9326	p = 0;
9327	/* Supported VPD pages */
9328	pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9329	/* Serial Number */
9330	pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9331	/* Device Identification */
9332	pages->page_list[p++] = SVPD_DEVICE_ID;
9333	/* Extended INQUIRY Data */
9334	pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9335	/* Mode Page Policy */
9336	pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9337	/* SCSI Ports */
9338	pages->page_list[p++] = SVPD_SCSI_PORTS;
9339	/* Third-party Copy */
9340	pages->page_list[p++] = SVPD_SCSI_TPC;
9341	if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9342		/* Block limits */
9343		pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9344		/* Block Device Characteristics */
9345		pages->page_list[p++] = SVPD_BDC;
9346		/* Logical Block Provisioning */
9347		pages->page_list[p++] = SVPD_LBP;
9348	}
9349	pages->length = p;
9350
9351	ctl_set_success(ctsio);
9352	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9353	ctsio->be_move_done = ctl_config_move_done;
9354	ctl_datamove((union ctl_io *)ctsio);
9355	return (CTL_RETVAL_COMPLETE);
9356}
9357
9358/*
9359 * SCSI VPD page 0x80, the Unit Serial Number page.
9360 */
9361static int
9362ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9363{
9364	struct scsi_vpd_unit_serial_number *sn_ptr;
9365	struct ctl_lun *lun;
9366	int data_len;
9367
9368	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9369
9370	data_len = 4 + CTL_SN_LEN;
9371	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9372	sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9373	if (data_len < alloc_len) {
9374		ctsio->residual = alloc_len - data_len;
9375		ctsio->kern_data_len = data_len;
9376		ctsio->kern_total_len = data_len;
9377	} else {
9378		ctsio->residual = 0;
9379		ctsio->kern_data_len = alloc_len;
9380		ctsio->kern_total_len = alloc_len;
9381	}
9382	ctsio->kern_data_resid = 0;
9383	ctsio->kern_rel_offset = 0;
9384	ctsio->kern_sg_entries = 0;
9385
9386	/*
9387	 * The control device is always connected.  The disk device, on the
9388	 * other hand, may not be online all the time.  Need to change this
9389	 * to figure out whether the disk device is actually online or not.
9390	 */
9391	if (lun != NULL)
9392		sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9393				  lun->be_lun->lun_type;
9394	else
9395		sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9396
9397	sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9398	sn_ptr->length = CTL_SN_LEN;
9399	/*
9400	 * If we don't have a LUN, we just leave the serial number as
9401	 * all spaces.
9402	 */
9403	if (lun != NULL) {
9404		strncpy((char *)sn_ptr->serial_num,
9405			(char *)lun->be_lun->serial_num, CTL_SN_LEN);
9406	} else
9407		memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9408
9409	ctl_set_success(ctsio);
9410	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9411	ctsio->be_move_done = ctl_config_move_done;
9412	ctl_datamove((union ctl_io *)ctsio);
9413	return (CTL_RETVAL_COMPLETE);
9414}
9415
9416
9417/*
9418 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9419 */
9420static int
9421ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9422{
9423	struct scsi_vpd_extended_inquiry_data *eid_ptr;
9424	struct ctl_lun *lun;
9425	int data_len;
9426
9427	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9428
9429	data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9430	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9431	eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9432	ctsio->kern_sg_entries = 0;
9433
9434	if (data_len < alloc_len) {
9435		ctsio->residual = alloc_len - data_len;
9436		ctsio->kern_data_len = data_len;
9437		ctsio->kern_total_len = data_len;
9438	} else {
9439		ctsio->residual = 0;
9440		ctsio->kern_data_len = alloc_len;
9441		ctsio->kern_total_len = alloc_len;
9442	}
9443	ctsio->kern_data_resid = 0;
9444	ctsio->kern_rel_offset = 0;
9445	ctsio->kern_sg_entries = 0;
9446
9447	/*
9448	 * The control device is always connected.  The disk device, on the
9449	 * other hand, may not be online all the time.
9450	 */
9451	if (lun != NULL)
9452		eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9453				     lun->be_lun->lun_type;
9454	else
9455		eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9456	eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9457	scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9458	/*
9459	 * We support head of queue, ordered and simple tags.
9460	 */
9461	eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9462	/*
9463	 * Volatile cache supported.
9464	 */
9465	eid_ptr->flags3 = SVPD_EID_V_SUP;
9466
9467	/*
9468	 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9469	 * attention for a particular IT nexus on all LUNs once we report
9470	 * it to that nexus once.  This bit is required as of SPC-4.
9471	 */
9472	eid_ptr->flags4 = SVPD_EID_LUICLT;
9473
9474	/*
9475	 * XXX KDM in order to correctly answer this, we would need
9476	 * information from the SIM to determine how much sense data it
9477	 * can send.  So this would really be a path inquiry field, most
9478	 * likely.  This can be set to a maximum of 252 according to SPC-4,
9479	 * but the hardware may or may not be able to support that much.
9480	 * 0 just means that the maximum sense data length is not reported.
9481	 */
9482	eid_ptr->max_sense_length = 0;
9483
9484	ctl_set_success(ctsio);
9485	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9486	ctsio->be_move_done = ctl_config_move_done;
9487	ctl_datamove((union ctl_io *)ctsio);
9488	return (CTL_RETVAL_COMPLETE);
9489}
9490
9491static int
9492ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9493{
9494	struct scsi_vpd_mode_page_policy *mpp_ptr;
9495	struct ctl_lun *lun;
9496	int data_len;
9497
9498	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9499
9500	data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9501	    sizeof(struct scsi_vpd_mode_page_policy_descr);
9502
9503	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9504	mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9505	ctsio->kern_sg_entries = 0;
9506
9507	if (data_len < alloc_len) {
9508		ctsio->residual = alloc_len - data_len;
9509		ctsio->kern_data_len = data_len;
9510		ctsio->kern_total_len = data_len;
9511	} else {
9512		ctsio->residual = 0;
9513		ctsio->kern_data_len = alloc_len;
9514		ctsio->kern_total_len = alloc_len;
9515	}
9516	ctsio->kern_data_resid = 0;
9517	ctsio->kern_rel_offset = 0;
9518	ctsio->kern_sg_entries = 0;
9519
9520	/*
9521	 * The control device is always connected.  The disk device, on the
9522	 * other hand, may not be online all the time.
9523	 */
9524	if (lun != NULL)
9525		mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9526				     lun->be_lun->lun_type;
9527	else
9528		mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9529	mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9530	scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9531	mpp_ptr->descr[0].page_code = 0x3f;
9532	mpp_ptr->descr[0].subpage_code = 0xff;
9533	mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9534
9535	ctl_set_success(ctsio);
9536	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9537	ctsio->be_move_done = ctl_config_move_done;
9538	ctl_datamove((union ctl_io *)ctsio);
9539	return (CTL_RETVAL_COMPLETE);
9540}
9541
9542/*
9543 * SCSI VPD page 0x83, the Device Identification page.
9544 */
9545static int
9546ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9547{
9548	struct scsi_vpd_device_id *devid_ptr;
9549	struct scsi_vpd_id_descriptor *desc;
9550	struct ctl_softc *softc;
9551	struct ctl_lun *lun;
9552	struct ctl_port *port;
9553	int data_len;
9554	uint8_t proto;
9555
9556	softc = control_softc;
9557
9558	port = ctl_io_port(&ctsio->io_hdr);
9559	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9560
9561	data_len = sizeof(struct scsi_vpd_device_id) +
9562	    sizeof(struct scsi_vpd_id_descriptor) +
9563		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9564	    sizeof(struct scsi_vpd_id_descriptor) +
9565		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9566	if (lun && lun->lun_devid)
9567		data_len += lun->lun_devid->len;
9568	if (port && port->port_devid)
9569		data_len += port->port_devid->len;
9570	if (port && port->target_devid)
9571		data_len += port->target_devid->len;
9572
9573	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9574	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9575	ctsio->kern_sg_entries = 0;
9576
9577	if (data_len < alloc_len) {
9578		ctsio->residual = alloc_len - data_len;
9579		ctsio->kern_data_len = data_len;
9580		ctsio->kern_total_len = data_len;
9581	} else {
9582		ctsio->residual = 0;
9583		ctsio->kern_data_len = alloc_len;
9584		ctsio->kern_total_len = alloc_len;
9585	}
9586	ctsio->kern_data_resid = 0;
9587	ctsio->kern_rel_offset = 0;
9588	ctsio->kern_sg_entries = 0;
9589
9590	/*
9591	 * The control device is always connected.  The disk device, on the
9592	 * other hand, may not be online all the time.
9593	 */
9594	if (lun != NULL)
9595		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9596				     lun->be_lun->lun_type;
9597	else
9598		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9599	devid_ptr->page_code = SVPD_DEVICE_ID;
9600	scsi_ulto2b(data_len - 4, devid_ptr->length);
9601
9602	if (port && port->port_type == CTL_PORT_FC)
9603		proto = SCSI_PROTO_FC << 4;
9604	else if (port && port->port_type == CTL_PORT_ISCSI)
9605		proto = SCSI_PROTO_ISCSI << 4;
9606	else
9607		proto = SCSI_PROTO_SPI << 4;
9608	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9609
9610	/*
9611	 * We're using a LUN association here.  i.e., this device ID is a
9612	 * per-LUN identifier.
9613	 */
9614	if (lun && lun->lun_devid) {
9615		memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9616		desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9617		    lun->lun_devid->len);
9618	}
9619
9620	/*
9621	 * This is for the WWPN which is a port association.
9622	 */
9623	if (port && port->port_devid) {
9624		memcpy(desc, port->port_devid->data, port->port_devid->len);
9625		desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9626		    port->port_devid->len);
9627	}
9628
9629	/*
9630	 * This is for the Relative Target Port(type 4h) identifier
9631	 */
9632	desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9633	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9634	    SVPD_ID_TYPE_RELTARG;
9635	desc->length = 4;
9636	scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9637	desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9638	    sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9639
9640	/*
9641	 * This is for the Target Port Group(type 5h) identifier
9642	 */
9643	desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9644	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9645	    SVPD_ID_TYPE_TPORTGRP;
9646	desc->length = 4;
9647	scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9648	    &desc->identifier[2]);
9649	desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9650	    sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9651
9652	/*
9653	 * This is for the Target identifier
9654	 */
9655	if (port && port->target_devid) {
9656		memcpy(desc, port->target_devid->data, port->target_devid->len);
9657	}
9658
9659	ctl_set_success(ctsio);
9660	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9661	ctsio->be_move_done = ctl_config_move_done;
9662	ctl_datamove((union ctl_io *)ctsio);
9663	return (CTL_RETVAL_COMPLETE);
9664}
9665
9666static int
9667ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9668{
9669	struct ctl_softc *softc = control_softc;
9670	struct scsi_vpd_scsi_ports *sp;
9671	struct scsi_vpd_port_designation *pd;
9672	struct scsi_vpd_port_designation_cont *pdc;
9673	struct ctl_lun *lun;
9674	struct ctl_port *port;
9675	int data_len, num_target_ports, iid_len, id_len;
9676
9677	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9678
9679	num_target_ports = 0;
9680	iid_len = 0;
9681	id_len = 0;
9682	mtx_lock(&softc->ctl_lock);
9683	STAILQ_FOREACH(port, &softc->port_list, links) {
9684		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9685			continue;
9686		if (lun != NULL &&
9687		    ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9688			continue;
9689		num_target_ports++;
9690		if (port->init_devid)
9691			iid_len += port->init_devid->len;
9692		if (port->port_devid)
9693			id_len += port->port_devid->len;
9694	}
9695	mtx_unlock(&softc->ctl_lock);
9696
9697	data_len = sizeof(struct scsi_vpd_scsi_ports) +
9698	    num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9699	     sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9700	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9701	sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9702	ctsio->kern_sg_entries = 0;
9703
9704	if (data_len < alloc_len) {
9705		ctsio->residual = alloc_len - data_len;
9706		ctsio->kern_data_len = data_len;
9707		ctsio->kern_total_len = data_len;
9708	} else {
9709		ctsio->residual = 0;
9710		ctsio->kern_data_len = alloc_len;
9711		ctsio->kern_total_len = alloc_len;
9712	}
9713	ctsio->kern_data_resid = 0;
9714	ctsio->kern_rel_offset = 0;
9715	ctsio->kern_sg_entries = 0;
9716
9717	/*
9718	 * The control device is always connected.  The disk device, on the
9719	 * other hand, may not be online all the time.  Need to change this
9720	 * to figure out whether the disk device is actually online or not.
9721	 */
9722	if (lun != NULL)
9723		sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9724				  lun->be_lun->lun_type;
9725	else
9726		sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9727
9728	sp->page_code = SVPD_SCSI_PORTS;
9729	scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9730	    sp->page_length);
9731	pd = &sp->design[0];
9732
9733	mtx_lock(&softc->ctl_lock);
9734	STAILQ_FOREACH(port, &softc->port_list, links) {
9735		if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9736			continue;
9737		if (lun != NULL &&
9738		    ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9739			continue;
9740		scsi_ulto2b(port->targ_port, pd->relative_port_id);
9741		if (port->init_devid) {
9742			iid_len = port->init_devid->len;
9743			memcpy(pd->initiator_transportid,
9744			    port->init_devid->data, port->init_devid->len);
9745		} else
9746			iid_len = 0;
9747		scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9748		pdc = (struct scsi_vpd_port_designation_cont *)
9749		    (&pd->initiator_transportid[iid_len]);
9750		if (port->port_devid) {
9751			id_len = port->port_devid->len;
9752			memcpy(pdc->target_port_descriptors,
9753			    port->port_devid->data, port->port_devid->len);
9754		} else
9755			id_len = 0;
9756		scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9757		pd = (struct scsi_vpd_port_designation *)
9758		    ((uint8_t *)pdc->target_port_descriptors + id_len);
9759	}
9760	mtx_unlock(&softc->ctl_lock);
9761
9762	ctl_set_success(ctsio);
9763	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9764	ctsio->be_move_done = ctl_config_move_done;
9765	ctl_datamove((union ctl_io *)ctsio);
9766	return (CTL_RETVAL_COMPLETE);
9767}
9768
9769static int
9770ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9771{
9772	struct scsi_vpd_block_limits *bl_ptr;
9773	struct ctl_lun *lun;
9774	int bs;
9775
9776	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9777
9778	ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9779	bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9780	ctsio->kern_sg_entries = 0;
9781
9782	if (sizeof(*bl_ptr) < alloc_len) {
9783		ctsio->residual = alloc_len - sizeof(*bl_ptr);
9784		ctsio->kern_data_len = sizeof(*bl_ptr);
9785		ctsio->kern_total_len = sizeof(*bl_ptr);
9786	} else {
9787		ctsio->residual = 0;
9788		ctsio->kern_data_len = alloc_len;
9789		ctsio->kern_total_len = alloc_len;
9790	}
9791	ctsio->kern_data_resid = 0;
9792	ctsio->kern_rel_offset = 0;
9793	ctsio->kern_sg_entries = 0;
9794
9795	/*
9796	 * The control device is always connected.  The disk device, on the
9797	 * other hand, may not be online all the time.  Need to change this
9798	 * to figure out whether the disk device is actually online or not.
9799	 */
9800	if (lun != NULL)
9801		bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9802				  lun->be_lun->lun_type;
9803	else
9804		bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9805
9806	bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9807	scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9808	bl_ptr->max_cmp_write_len = 0xff;
9809	scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9810	if (lun != NULL) {
9811		bs = lun->be_lun->blocksize;
9812		scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9813		if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9814			scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9815			scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9816			if (lun->be_lun->ublockexp != 0) {
9817				scsi_ulto4b((1 << lun->be_lun->ublockexp),
9818				    bl_ptr->opt_unmap_grain);
9819				scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9820				    bl_ptr->unmap_grain_align);
9821			}
9822		}
9823		scsi_ulto4b(lun->be_lun->atomicblock,
9824		    bl_ptr->max_atomic_transfer_length);
9825		scsi_ulto4b(0, bl_ptr->atomic_alignment);
9826		scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9827	}
9828	scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9829
9830	ctl_set_success(ctsio);
9831	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9832	ctsio->be_move_done = ctl_config_move_done;
9833	ctl_datamove((union ctl_io *)ctsio);
9834	return (CTL_RETVAL_COMPLETE);
9835}
9836
9837static int
9838ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9839{
9840	struct scsi_vpd_block_device_characteristics *bdc_ptr;
9841	struct ctl_lun *lun;
9842	const char *value;
9843	u_int i;
9844
9845	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9846
9847	ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9848	bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9849	ctsio->kern_sg_entries = 0;
9850
9851	if (sizeof(*bdc_ptr) < alloc_len) {
9852		ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9853		ctsio->kern_data_len = sizeof(*bdc_ptr);
9854		ctsio->kern_total_len = sizeof(*bdc_ptr);
9855	} else {
9856		ctsio->residual = 0;
9857		ctsio->kern_data_len = alloc_len;
9858		ctsio->kern_total_len = alloc_len;
9859	}
9860	ctsio->kern_data_resid = 0;
9861	ctsio->kern_rel_offset = 0;
9862	ctsio->kern_sg_entries = 0;
9863
9864	/*
9865	 * The control device is always connected.  The disk device, on the
9866	 * other hand, may not be online all the time.  Need to change this
9867	 * to figure out whether the disk device is actually online or not.
9868	 */
9869	if (lun != NULL)
9870		bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9871				  lun->be_lun->lun_type;
9872	else
9873		bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9874	bdc_ptr->page_code = SVPD_BDC;
9875	scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9876	if (lun != NULL &&
9877	    (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9878		i = strtol(value, NULL, 0);
9879	else
9880		i = CTL_DEFAULT_ROTATION_RATE;
9881	scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9882	if (lun != NULL &&
9883	    (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9884		i = strtol(value, NULL, 0);
9885	else
9886		i = 0;
9887	bdc_ptr->wab_wac_ff = (i & 0x0f);
9888	bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9889
9890	ctl_set_success(ctsio);
9891	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9892	ctsio->be_move_done = ctl_config_move_done;
9893	ctl_datamove((union ctl_io *)ctsio);
9894	return (CTL_RETVAL_COMPLETE);
9895}
9896
9897static int
9898ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9899{
9900	struct scsi_vpd_logical_block_prov *lbp_ptr;
9901	struct ctl_lun *lun;
9902
9903	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9904
9905	ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9906	lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9907	ctsio->kern_sg_entries = 0;
9908
9909	if (sizeof(*lbp_ptr) < alloc_len) {
9910		ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9911		ctsio->kern_data_len = sizeof(*lbp_ptr);
9912		ctsio->kern_total_len = sizeof(*lbp_ptr);
9913	} else {
9914		ctsio->residual = 0;
9915		ctsio->kern_data_len = alloc_len;
9916		ctsio->kern_total_len = alloc_len;
9917	}
9918	ctsio->kern_data_resid = 0;
9919	ctsio->kern_rel_offset = 0;
9920	ctsio->kern_sg_entries = 0;
9921
9922	/*
9923	 * The control device is always connected.  The disk device, on the
9924	 * other hand, may not be online all the time.  Need to change this
9925	 * to figure out whether the disk device is actually online or not.
9926	 */
9927	if (lun != NULL)
9928		lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9929				  lun->be_lun->lun_type;
9930	else
9931		lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9932
9933	lbp_ptr->page_code = SVPD_LBP;
9934	scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9935	lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9936	if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9937		lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9938		    SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9939		lbp_ptr->prov_type = SVPD_LBP_THIN;
9940	}
9941
9942	ctl_set_success(ctsio);
9943	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9944	ctsio->be_move_done = ctl_config_move_done;
9945	ctl_datamove((union ctl_io *)ctsio);
9946	return (CTL_RETVAL_COMPLETE);
9947}
9948
9949/*
9950 * INQUIRY with the EVPD bit set.
9951 */
9952static int
9953ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9954{
9955	struct ctl_lun *lun;
9956	struct scsi_inquiry *cdb;
9957	int alloc_len, retval;
9958
9959	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9960	cdb = (struct scsi_inquiry *)ctsio->cdb;
9961	alloc_len = scsi_2btoul(cdb->length);
9962
9963	switch (cdb->page_code) {
9964	case SVPD_SUPPORTED_PAGES:
9965		retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9966		break;
9967	case SVPD_UNIT_SERIAL_NUMBER:
9968		retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9969		break;
9970	case SVPD_DEVICE_ID:
9971		retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9972		break;
9973	case SVPD_EXTENDED_INQUIRY_DATA:
9974		retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9975		break;
9976	case SVPD_MODE_PAGE_POLICY:
9977		retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9978		break;
9979	case SVPD_SCSI_PORTS:
9980		retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9981		break;
9982	case SVPD_SCSI_TPC:
9983		retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9984		break;
9985	case SVPD_BLOCK_LIMITS:
9986		if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9987			goto err;
9988		retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9989		break;
9990	case SVPD_BDC:
9991		if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9992			goto err;
9993		retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9994		break;
9995	case SVPD_LBP:
9996		if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9997			goto err;
9998		retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9999		break;
10000	default:
10001err:
10002		ctl_set_invalid_field(ctsio,
10003				      /*sks_valid*/ 1,
10004				      /*command*/ 1,
10005				      /*field*/ 2,
10006				      /*bit_valid*/ 0,
10007				      /*bit*/ 0);
10008		ctl_done((union ctl_io *)ctsio);
10009		retval = CTL_RETVAL_COMPLETE;
10010		break;
10011	}
10012
10013	return (retval);
10014}
10015
10016/*
10017 * Standard INQUIRY data.
10018 */
10019static int
10020ctl_inquiry_std(struct ctl_scsiio *ctsio)
10021{
10022	struct scsi_inquiry_data *inq_ptr;
10023	struct scsi_inquiry *cdb;
10024	struct ctl_softc *softc;
10025	struct ctl_port *port;
10026	struct ctl_lun *lun;
10027	char *val;
10028	uint32_t alloc_len, data_len;
10029	ctl_port_type port_type;
10030
10031	softc = control_softc;
10032
10033	/*
10034	 * Figure out whether we're talking to a Fibre Channel port or not.
10035	 * We treat the ioctl front end, and any SCSI adapters, as packetized
10036	 * SCSI front ends.
10037	 */
10038	port = ctl_io_port(&ctsio->io_hdr);
10039	if (port != NULL)
10040		port_type = port->port_type;
10041	else
10042		port_type = CTL_PORT_SCSI;
10043	if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10044		port_type = CTL_PORT_SCSI;
10045
10046	lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10047	cdb = (struct scsi_inquiry *)ctsio->cdb;
10048	alloc_len = scsi_2btoul(cdb->length);
10049
10050	/*
10051	 * We malloc the full inquiry data size here and fill it
10052	 * in.  If the user only asks for less, we'll give him
10053	 * that much.
10054	 */
10055	data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10056	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10057	inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10058	ctsio->kern_sg_entries = 0;
10059	ctsio->kern_data_resid = 0;
10060	ctsio->kern_rel_offset = 0;
10061
10062	if (data_len < alloc_len) {
10063		ctsio->residual = alloc_len - data_len;
10064		ctsio->kern_data_len = data_len;
10065		ctsio->kern_total_len = data_len;
10066	} else {
10067		ctsio->residual = 0;
10068		ctsio->kern_data_len = alloc_len;
10069		ctsio->kern_total_len = alloc_len;
10070	}
10071
10072	if (lun != NULL) {
10073		if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10074		    softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10075			inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10076			    lun->be_lun->lun_type;
10077		} else {
10078			inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10079			    lun->be_lun->lun_type;
10080		}
10081	} else
10082		inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10083
10084	/* RMB in byte 2 is 0 */
10085	inq_ptr->version = SCSI_REV_SPC4;
10086
10087	/*
10088	 * According to SAM-3, even if a device only supports a single
10089	 * level of LUN addressing, it should still set the HISUP bit:
10090	 *
10091	 * 4.9.1 Logical unit numbers overview
10092	 *
10093	 * All logical unit number formats described in this standard are
10094	 * hierarchical in structure even when only a single level in that
10095	 * hierarchy is used. The HISUP bit shall be set to one in the
10096	 * standard INQUIRY data (see SPC-2) when any logical unit number
10097	 * format described in this standard is used.  Non-hierarchical
10098	 * formats are outside the scope of this standard.
10099	 *
10100	 * Therefore we set the HiSup bit here.
10101	 *
10102	 * The reponse format is 2, per SPC-3.
10103	 */
10104	inq_ptr->response_format = SID_HiSup | 2;
10105
10106	inq_ptr->additional_length = data_len -
10107	    (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10108	CTL_DEBUG_PRINT(("additional_length = %d\n",
10109			 inq_ptr->additional_length));
10110
10111	inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10112	/* 16 bit addressing */
10113	if (port_type == CTL_PORT_SCSI)
10114		inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10115	/* XXX set the SID_MultiP bit here if we're actually going to
10116	   respond on multiple ports */
10117	inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10118
10119	/* 16 bit data bus, synchronous transfers */
10120	if (port_type == CTL_PORT_SCSI)
10121		inq_ptr->flags = SID_WBus16 | SID_Sync;
10122	/*
10123	 * XXX KDM do we want to support tagged queueing on the control
10124	 * device at all?
10125	 */
10126	if ((lun == NULL)
10127	 || (lun->be_lun->lun_type != T_PROCESSOR))
10128		inq_ptr->flags |= SID_CmdQue;
10129	/*
10130	 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10131	 * We have 8 bytes for the vendor name, and 16 bytes for the device
10132	 * name and 4 bytes for the revision.
10133	 */
10134	if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10135	    "vendor")) == NULL) {
10136		strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10137	} else {
10138		memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10139		strncpy(inq_ptr->vendor, val,
10140		    min(sizeof(inq_ptr->vendor), strlen(val)));
10141	}
10142	if (lun == NULL) {
10143		strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10144		    sizeof(inq_ptr->product));
10145	} else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10146		switch (lun->be_lun->lun_type) {
10147		case T_DIRECT:
10148			strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10149			    sizeof(inq_ptr->product));
10150			break;
10151		case T_PROCESSOR:
10152			strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10153			    sizeof(inq_ptr->product));
10154			break;
10155		default:
10156			strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10157			    sizeof(inq_ptr->product));
10158			break;
10159		}
10160	} else {
10161		memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10162		strncpy(inq_ptr->product, val,
10163		    min(sizeof(inq_ptr->product), strlen(val)));
10164	}
10165
10166	/*
10167	 * XXX make this a macro somewhere so it automatically gets
10168	 * incremented when we make changes.
10169	 */
10170	if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10171	    "revision")) == NULL) {
10172		strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10173	} else {
10174		memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10175		strncpy(inq_ptr->revision, val,
10176		    min(sizeof(inq_ptr->revision), strlen(val)));
10177	}
10178
10179	/*
10180	 * For parallel SCSI, we support double transition and single
10181	 * transition clocking.  We also support QAS (Quick Arbitration
10182	 * and Selection) and Information Unit transfers on both the
10183	 * control and array devices.
10184	 */
10185	if (port_type == CTL_PORT_SCSI)
10186		inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10187				    SID_SPI_IUS;
10188
10189	/* SAM-5 (no version claimed) */
10190	scsi_ulto2b(0x00A0, inq_ptr->version1);
10191	/* SPC-4 (no version claimed) */
10192	scsi_ulto2b(0x0460, inq_ptr->version2);
10193	if (port_type == CTL_PORT_FC) {
10194		/* FCP-2 ANSI INCITS.350:2003 */
10195		scsi_ulto2b(0x0917, inq_ptr->version3);
10196	} else if (port_type == CTL_PORT_SCSI) {
10197		/* SPI-4 ANSI INCITS.362:200x */
10198		scsi_ulto2b(0x0B56, inq_ptr->version3);
10199	} else if (port_type == CTL_PORT_ISCSI) {
10200		/* iSCSI (no version claimed) */
10201		scsi_ulto2b(0x0960, inq_ptr->version3);
10202	} else if (port_type == CTL_PORT_SAS) {
10203		/* SAS (no version claimed) */
10204		scsi_ulto2b(0x0BE0, inq_ptr->version3);
10205	}
10206
10207	if (lun == NULL) {
10208		/* SBC-4 (no version claimed) */
10209		scsi_ulto2b(0x0600, inq_ptr->version4);
10210	} else {
10211		switch (lun->be_lun->lun_type) {
10212		case T_DIRECT:
10213			/* SBC-4 (no version claimed) */
10214			scsi_ulto2b(0x0600, inq_ptr->version4);
10215			break;
10216		case T_PROCESSOR:
10217		default:
10218			break;
10219		}
10220	}
10221
10222	ctl_set_success(ctsio);
10223	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10224	ctsio->be_move_done = ctl_config_move_done;
10225	ctl_datamove((union ctl_io *)ctsio);
10226	return (CTL_RETVAL_COMPLETE);
10227}
10228
10229int
10230ctl_inquiry(struct ctl_scsiio *ctsio)
10231{
10232	struct scsi_inquiry *cdb;
10233	int retval;
10234
10235	CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10236
10237	cdb = (struct scsi_inquiry *)ctsio->cdb;
10238	if (cdb->byte2 & SI_EVPD)
10239		retval = ctl_inquiry_evpd(ctsio);
10240	else if (cdb->page_code == 0)
10241		retval = ctl_inquiry_std(ctsio);
10242	else {
10243		ctl_set_invalid_field(ctsio,
10244				      /*sks_valid*/ 1,
10245				      /*command*/ 1,
10246				      /*field*/ 2,
10247				      /*bit_valid*/ 0,
10248				      /*bit*/ 0);
10249		ctl_done((union ctl_io *)ctsio);
10250		return (CTL_RETVAL_COMPLETE);
10251	}
10252
10253	return (retval);
10254}
10255
10256/*
10257 * For known CDB types, parse the LBA and length.
10258 */
10259static int
10260ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10261{
10262	if (io->io_hdr.io_type != CTL_IO_SCSI)
10263		return (1);
10264
10265	switch (io->scsiio.cdb[0]) {
10266	case COMPARE_AND_WRITE: {
10267		struct scsi_compare_and_write *cdb;
10268
10269		cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10270
10271		*lba = scsi_8btou64(cdb->addr);
10272		*len = cdb->length;
10273		break;
10274	}
10275	case READ_6:
10276	case WRITE_6: {
10277		struct scsi_rw_6 *cdb;
10278
10279		cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10280
10281		*lba = scsi_3btoul(cdb->addr);
10282		/* only 5 bits are valid in the most significant address byte */
10283		*lba &= 0x1fffff;
10284		*len = cdb->length;
10285		break;
10286	}
10287	case READ_10:
10288	case WRITE_10: {
10289		struct scsi_rw_10 *cdb;
10290
10291		cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10292
10293		*lba = scsi_4btoul(cdb->addr);
10294		*len = scsi_2btoul(cdb->length);
10295		break;
10296	}
10297	case WRITE_VERIFY_10: {
10298		struct scsi_write_verify_10 *cdb;
10299
10300		cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10301
10302		*lba = scsi_4btoul(cdb->addr);
10303		*len = scsi_2btoul(cdb->length);
10304		break;
10305	}
10306	case READ_12:
10307	case WRITE_12: {
10308		struct scsi_rw_12 *cdb;
10309
10310		cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10311
10312		*lba = scsi_4btoul(cdb->addr);
10313		*len = scsi_4btoul(cdb->length);
10314		break;
10315	}
10316	case WRITE_VERIFY_12: {
10317		struct scsi_write_verify_12 *cdb;
10318
10319		cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10320
10321		*lba = scsi_4btoul(cdb->addr);
10322		*len = scsi_4btoul(cdb->length);
10323		break;
10324	}
10325	case READ_16:
10326	case WRITE_16:
10327	case WRITE_ATOMIC_16: {
10328		struct scsi_rw_16 *cdb;
10329
10330		cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10331
10332		*lba = scsi_8btou64(cdb->addr);
10333		*len = scsi_4btoul(cdb->length);
10334		break;
10335	}
10336	case WRITE_VERIFY_16: {
10337		struct scsi_write_verify_16 *cdb;
10338
10339		cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10340
10341		*lba = scsi_8btou64(cdb->addr);
10342		*len = scsi_4btoul(cdb->length);
10343		break;
10344	}
10345	case WRITE_SAME_10: {
10346		struct scsi_write_same_10 *cdb;
10347
10348		cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10349
10350		*lba = scsi_4btoul(cdb->addr);
10351		*len = scsi_2btoul(cdb->length);
10352		break;
10353	}
10354	case WRITE_SAME_16: {
10355		struct scsi_write_same_16 *cdb;
10356
10357		cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10358
10359		*lba = scsi_8btou64(cdb->addr);
10360		*len = scsi_4btoul(cdb->length);
10361		break;
10362	}
10363	case VERIFY_10: {
10364		struct scsi_verify_10 *cdb;
10365
10366		cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10367
10368		*lba = scsi_4btoul(cdb->addr);
10369		*len = scsi_2btoul(cdb->length);
10370		break;
10371	}
10372	case VERIFY_12: {
10373		struct scsi_verify_12 *cdb;
10374
10375		cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10376
10377		*lba = scsi_4btoul(cdb->addr);
10378		*len = scsi_4btoul(cdb->length);
10379		break;
10380	}
10381	case VERIFY_16: {
10382		struct scsi_verify_16 *cdb;
10383
10384		cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10385
10386		*lba = scsi_8btou64(cdb->addr);
10387		*len = scsi_4btoul(cdb->length);
10388		break;
10389	}
10390	case UNMAP: {
10391		*lba = 0;
10392		*len = UINT64_MAX;
10393		break;
10394	}
10395	case SERVICE_ACTION_IN: {	/* GET LBA STATUS */
10396		struct scsi_get_lba_status *cdb;
10397
10398		cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10399		*lba = scsi_8btou64(cdb->addr);
10400		*len = UINT32_MAX;
10401		break;
10402	}
10403	default:
10404		return (1);
10405		break; /* NOTREACHED */
10406	}
10407
10408	return (0);
10409}
10410
10411static ctl_action
10412ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10413    bool seq)
10414{
10415	uint64_t endlba1, endlba2;
10416
10417	endlba1 = lba1 + len1 - (seq ? 0 : 1);
10418	endlba2 = lba2 + len2 - 1;
10419
10420	if ((endlba1 < lba2) || (endlba2 < lba1))
10421		return (CTL_ACTION_PASS);
10422	else
10423		return (CTL_ACTION_BLOCK);
10424}
10425
10426static int
10427ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10428{
10429	struct ctl_ptr_len_flags *ptrlen;
10430	struct scsi_unmap_desc *buf, *end, *range;
10431	uint64_t lba;
10432	uint32_t len;
10433
10434	/* If not UNMAP -- go other way. */
10435	if (io->io_hdr.io_type != CTL_IO_SCSI ||
10436	    io->scsiio.cdb[0] != UNMAP)
10437		return (CTL_ACTION_ERROR);
10438
10439	/* If UNMAP without data -- block and wait for data. */
10440	ptrlen = (struct ctl_ptr_len_flags *)
10441	    &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10442	if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10443	    ptrlen->ptr == NULL)
10444		return (CTL_ACTION_BLOCK);
10445
10446	/* UNMAP with data -- check for collision. */
10447	buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10448	end = buf + ptrlen->len / sizeof(*buf);
10449	for (range = buf; range < end; range++) {
10450		lba = scsi_8btou64(range->lba);
10451		len = scsi_4btoul(range->length);
10452		if ((lba < lba2 + len2) && (lba + len > lba2))
10453			return (CTL_ACTION_BLOCK);
10454	}
10455	return (CTL_ACTION_PASS);
10456}
10457
10458static ctl_action
10459ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10460{
10461	uint64_t lba1, lba2;
10462	uint64_t len1, len2;
10463	int retval;
10464
10465	if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10466		return (CTL_ACTION_ERROR);
10467
10468	retval = ctl_extent_check_unmap(io1, lba2, len2);
10469	if (retval != CTL_ACTION_ERROR)
10470		return (retval);
10471
10472	if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10473		return (CTL_ACTION_ERROR);
10474
10475	return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10476}
10477
10478static ctl_action
10479ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10480{
10481	uint64_t lba1, lba2;
10482	uint64_t len1, len2;
10483
10484	if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10485		return (CTL_ACTION_ERROR);
10486	if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10487		return (CTL_ACTION_ERROR);
10488
10489	if (lba1 + len1 == lba2)
10490		return (CTL_ACTION_BLOCK);
10491	return (CTL_ACTION_PASS);
10492}
10493
10494static ctl_action
10495ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10496    union ctl_io *ooa_io)
10497{
10498	const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10499	ctl_serialize_action *serialize_row;
10500
10501	/*
10502	 * The initiator attempted multiple untagged commands at the same
10503	 * time.  Can't do that.
10504	 */
10505	if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10506	 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10507	 && ((pending_io->io_hdr.nexus.targ_port ==
10508	      ooa_io->io_hdr.nexus.targ_port)
10509	  && (pending_io->io_hdr.nexus.initid ==
10510	      ooa_io->io_hdr.nexus.initid))
10511	 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10512	      CTL_FLAG_STATUS_SENT)) == 0))
10513		return (CTL_ACTION_OVERLAP);
10514
10515	/*
10516	 * The initiator attempted to send multiple tagged commands with
10517	 * the same ID.  (It's fine if different initiators have the same
10518	 * tag ID.)
10519	 *
10520	 * Even if all of those conditions are true, we don't kill the I/O
10521	 * if the command ahead of us has been aborted.  We won't end up
10522	 * sending it to the FETD, and it's perfectly legal to resend a
10523	 * command with the same tag number as long as the previous
10524	 * instance of this tag number has been aborted somehow.
10525	 */
10526	if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10527	 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10528	 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10529	 && ((pending_io->io_hdr.nexus.targ_port ==
10530	      ooa_io->io_hdr.nexus.targ_port)
10531	  && (pending_io->io_hdr.nexus.initid ==
10532	      ooa_io->io_hdr.nexus.initid))
10533	 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10534	      CTL_FLAG_STATUS_SENT)) == 0))
10535		return (CTL_ACTION_OVERLAP_TAG);
10536
10537	/*
10538	 * If we get a head of queue tag, SAM-3 says that we should
10539	 * immediately execute it.
10540	 *
10541	 * What happens if this command would normally block for some other
10542	 * reason?  e.g. a request sense with a head of queue tag
10543	 * immediately after a write.  Normally that would block, but this
10544	 * will result in its getting executed immediately...
10545	 *
10546	 * We currently return "pass" instead of "skip", so we'll end up
10547	 * going through the rest of the queue to check for overlapped tags.
10548	 *
10549	 * XXX KDM check for other types of blockage first??
10550	 */
10551	if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10552		return (CTL_ACTION_PASS);
10553
10554	/*
10555	 * Ordered tags have to block until all items ahead of them
10556	 * have completed.  If we get called with an ordered tag, we always
10557	 * block, if something else is ahead of us in the queue.
10558	 */
10559	if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10560		return (CTL_ACTION_BLOCK);
10561
10562	/*
10563	 * Simple tags get blocked until all head of queue and ordered tags
10564	 * ahead of them have completed.  I'm lumping untagged commands in
10565	 * with simple tags here.  XXX KDM is that the right thing to do?
10566	 */
10567	if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10568	  || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10569	 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10570	  || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10571		return (CTL_ACTION_BLOCK);
10572
10573	pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10574	ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10575
10576	serialize_row = ctl_serialize_table[ooa_entry->seridx];
10577
10578	switch (serialize_row[pending_entry->seridx]) {
10579	case CTL_SER_BLOCK:
10580		return (CTL_ACTION_BLOCK);
10581	case CTL_SER_EXTENT:
10582		return (ctl_extent_check(ooa_io, pending_io,
10583		    (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10584	case CTL_SER_EXTENTOPT:
10585		if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10586		    & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10587			return (ctl_extent_check(ooa_io, pending_io,
10588			    (lun->be_lun &&
10589			     lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10590		return (CTL_ACTION_PASS);
10591	case CTL_SER_EXTENTSEQ:
10592		if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10593			return (ctl_extent_check_seq(ooa_io, pending_io));
10594		return (CTL_ACTION_PASS);
10595	case CTL_SER_PASS:
10596		return (CTL_ACTION_PASS);
10597	case CTL_SER_BLOCKOPT:
10598		if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10599		    & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10600			return (CTL_ACTION_BLOCK);
10601		return (CTL_ACTION_PASS);
10602	case CTL_SER_SKIP:
10603		return (CTL_ACTION_SKIP);
10604	default:
10605		panic("invalid serialization value %d",
10606		      serialize_row[pending_entry->seridx]);
10607	}
10608
10609	return (CTL_ACTION_ERROR);
10610}
10611
10612/*
10613 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10614 * Assumptions:
10615 * - pending_io is generally either incoming, or on the blocked queue
10616 * - starting I/O is the I/O we want to start the check with.
10617 */
10618static ctl_action
10619ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10620	      union ctl_io *starting_io)
10621{
10622	union ctl_io *ooa_io;
10623	ctl_action action;
10624
10625	mtx_assert(&lun->lun_lock, MA_OWNED);
10626
10627	/*
10628	 * Run back along the OOA queue, starting with the current
10629	 * blocked I/O and going through every I/O before it on the
10630	 * queue.  If starting_io is NULL, we'll just end up returning
10631	 * CTL_ACTION_PASS.
10632	 */
10633	for (ooa_io = starting_io; ooa_io != NULL;
10634	     ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10635	     ooa_links)){
10636
10637		/*
10638		 * This routine just checks to see whether
10639		 * cur_blocked is blocked by ooa_io, which is ahead
10640		 * of it in the queue.  It doesn't queue/dequeue
10641		 * cur_blocked.
10642		 */
10643		action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10644		switch (action) {
10645		case CTL_ACTION_BLOCK:
10646		case CTL_ACTION_OVERLAP:
10647		case CTL_ACTION_OVERLAP_TAG:
10648		case CTL_ACTION_SKIP:
10649		case CTL_ACTION_ERROR:
10650			return (action);
10651			break; /* NOTREACHED */
10652		case CTL_ACTION_PASS:
10653			break;
10654		default:
10655			panic("invalid action %d", action);
10656			break;  /* NOTREACHED */
10657		}
10658	}
10659
10660	return (CTL_ACTION_PASS);
10661}
10662
10663/*
10664 * Assumptions:
10665 * - An I/O has just completed, and has been removed from the per-LUN OOA
10666 *   queue, so some items on the blocked queue may now be unblocked.
10667 */
10668static int
10669ctl_check_blocked(struct ctl_lun *lun)
10670{
10671	struct ctl_softc *softc = lun->ctl_softc;
10672	union ctl_io *cur_blocked, *next_blocked;
10673
10674	mtx_assert(&lun->lun_lock, MA_OWNED);
10675
10676	/*
10677	 * Run forward from the head of the blocked queue, checking each
10678	 * entry against the I/Os prior to it on the OOA queue to see if
10679	 * there is still any blockage.
10680	 *
10681	 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10682	 * with our removing a variable on it while it is traversing the
10683	 * list.
10684	 */
10685	for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10686	     cur_blocked != NULL; cur_blocked = next_blocked) {
10687		union ctl_io *prev_ooa;
10688		ctl_action action;
10689
10690		next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10691							  blocked_links);
10692
10693		prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10694						      ctl_ooaq, ooa_links);
10695
10696		/*
10697		 * If cur_blocked happens to be the first item in the OOA
10698		 * queue now, prev_ooa will be NULL, and the action
10699		 * returned will just be CTL_ACTION_PASS.
10700		 */
10701		action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10702
10703		switch (action) {
10704		case CTL_ACTION_BLOCK:
10705			/* Nothing to do here, still blocked */
10706			break;
10707		case CTL_ACTION_OVERLAP:
10708		case CTL_ACTION_OVERLAP_TAG:
10709			/*
10710			 * This shouldn't happen!  In theory we've already
10711			 * checked this command for overlap...
10712			 */
10713			break;
10714		case CTL_ACTION_PASS:
10715		case CTL_ACTION_SKIP: {
10716			const struct ctl_cmd_entry *entry;
10717
10718			/*
10719			 * The skip case shouldn't happen, this transaction
10720			 * should have never made it onto the blocked queue.
10721			 */
10722			/*
10723			 * This I/O is no longer blocked, we can remove it
10724			 * from the blocked queue.  Since this is a TAILQ
10725			 * (doubly linked list), we can do O(1) removals
10726			 * from any place on the list.
10727			 */
10728			TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10729				     blocked_links);
10730			cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10731
10732			if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
10733			    (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
10734				/*
10735				 * Need to send IO back to original side to
10736				 * run
10737				 */
10738				union ctl_ha_msg msg_info;
10739
10740				cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10741				msg_info.hdr.original_sc =
10742					cur_blocked->io_hdr.original_sc;
10743				msg_info.hdr.serializing_sc = cur_blocked;
10744				msg_info.hdr.msg_type = CTL_MSG_R2R;
10745				ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10746				    sizeof(msg_info.hdr), M_NOWAIT);
10747				break;
10748			}
10749			entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10750
10751			/*
10752			 * Check this I/O for LUN state changes that may
10753			 * have happened while this command was blocked.
10754			 * The LUN state may have been changed by a command
10755			 * ahead of us in the queue, so we need to re-check
10756			 * for any states that can be caused by SCSI
10757			 * commands.
10758			 */
10759			if (ctl_scsiio_lun_check(lun, entry,
10760						 &cur_blocked->scsiio) == 0) {
10761				cur_blocked->io_hdr.flags |=
10762				                      CTL_FLAG_IS_WAS_ON_RTR;
10763				ctl_enqueue_rtr(cur_blocked);
10764			} else
10765				ctl_done(cur_blocked);
10766			break;
10767		}
10768		default:
10769			/*
10770			 * This probably shouldn't happen -- we shouldn't
10771			 * get CTL_ACTION_ERROR, or anything else.
10772			 */
10773			break;
10774		}
10775	}
10776
10777	return (CTL_RETVAL_COMPLETE);
10778}
10779
10780/*
10781 * This routine (with one exception) checks LUN flags that can be set by
10782 * commands ahead of us in the OOA queue.  These flags have to be checked
10783 * when a command initially comes in, and when we pull a command off the
10784 * blocked queue and are preparing to execute it.  The reason we have to
10785 * check these flags for commands on the blocked queue is that the LUN
10786 * state may have been changed by a command ahead of us while we're on the
10787 * blocked queue.
10788 *
10789 * Ordering is somewhat important with these checks, so please pay
10790 * careful attention to the placement of any new checks.
10791 */
10792static int
10793ctl_scsiio_lun_check(struct ctl_lun *lun,
10794    const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10795{
10796	struct ctl_softc *softc = lun->ctl_softc;
10797	int retval;
10798	uint32_t residx;
10799
10800	retval = 0;
10801
10802	mtx_assert(&lun->lun_lock, MA_OWNED);
10803
10804	/*
10805	 * If this shelf is a secondary shelf controller, we may have to
10806	 * reject some commands disallowed by HA mode and link state.
10807	 */
10808	if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10809		if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
10810		    (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10811			ctl_set_lun_unavail(ctsio);
10812			retval = 1;
10813			goto bailout;
10814		}
10815		if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
10816		    (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10817			ctl_set_lun_transit(ctsio);
10818			retval = 1;
10819			goto bailout;
10820		}
10821		if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
10822		    (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
10823			ctl_set_lun_standby(ctsio);
10824			retval = 1;
10825			goto bailout;
10826		}
10827
10828		/* The rest of checks are only done on executing side */
10829		if (softc->ha_mode == CTL_HA_MODE_XFER)
10830			goto bailout;
10831	}
10832
10833	if (entry->pattern & CTL_LUN_PAT_WRITE) {
10834		if (lun->be_lun &&
10835		    lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
10836			ctl_set_sense(ctsio, /*current_error*/ 1,
10837			    /*sense_key*/ SSD_KEY_DATA_PROTECT,
10838			    /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
10839			retval = 1;
10840			goto bailout;
10841		}
10842		if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
10843		    .eca_and_aen & SCP_SWP) != 0) {
10844			ctl_set_sense(ctsio, /*current_error*/ 1,
10845			    /*sense_key*/ SSD_KEY_DATA_PROTECT,
10846			    /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
10847			retval = 1;
10848			goto bailout;
10849		}
10850	}
10851
10852	/*
10853	 * Check for a reservation conflict.  If this command isn't allowed
10854	 * even on reserved LUNs, and if this initiator isn't the one who
10855	 * reserved us, reject the command with a reservation conflict.
10856	 */
10857	residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10858	if ((lun->flags & CTL_LUN_RESERVED)
10859	 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10860		if (lun->res_idx != residx) {
10861			ctl_set_reservation_conflict(ctsio);
10862			retval = 1;
10863			goto bailout;
10864		}
10865	}
10866
10867	if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
10868	    (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
10869		/* No reservation or command is allowed. */;
10870	} else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
10871	    (lun->res_type == SPR_TYPE_WR_EX ||
10872	     lun->res_type == SPR_TYPE_WR_EX_RO ||
10873	     lun->res_type == SPR_TYPE_WR_EX_AR)) {
10874		/* The command is allowed for Write Exclusive resv. */;
10875	} else {
10876		/*
10877		 * if we aren't registered or it's a res holder type
10878		 * reservation and this isn't the res holder then set a
10879		 * conflict.
10880		 */
10881		if (ctl_get_prkey(lun, residx) == 0
10882		 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10883			ctl_set_reservation_conflict(ctsio);
10884			retval = 1;
10885			goto bailout;
10886		}
10887	}
10888
10889	if ((lun->flags & CTL_LUN_OFFLINE)
10890	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) {
10891		ctl_set_lun_not_ready(ctsio);
10892		retval = 1;
10893		goto bailout;
10894	}
10895
10896	if ((lun->flags & CTL_LUN_STOPPED)
10897	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10898		/* "Logical unit not ready, initializing cmd. required" */
10899		ctl_set_lun_stopped(ctsio);
10900		retval = 1;
10901		goto bailout;
10902	}
10903
10904	if ((lun->flags & CTL_LUN_INOPERABLE)
10905	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10906		/* "Medium format corrupted" */
10907		ctl_set_medium_format_corrupted(ctsio);
10908		retval = 1;
10909		goto bailout;
10910	}
10911
10912bailout:
10913	return (retval);
10914}
10915
10916static void
10917ctl_failover_io(union ctl_io *io, int have_lock)
10918{
10919	ctl_set_busy(&io->scsiio);
10920	ctl_done(io);
10921}
10922
10923static void
10924ctl_failover_lun(struct ctl_lun *lun)
10925{
10926	struct ctl_softc *softc = lun->ctl_softc;
10927	struct ctl_io_hdr *io, *next_io;
10928
10929	CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun));
10930	if (softc->ha_mode == CTL_HA_MODE_XFER) {
10931		TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10932			/* We are master */
10933			if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10934				if (io->flags & CTL_FLAG_IO_ACTIVE) {
10935					io->flags |= CTL_FLAG_ABORT;
10936				} else { /* This can be only due to DATAMOVE */
10937					io->msg_type = CTL_MSG_DATAMOVE_DONE;
10938					io->flags |= CTL_FLAG_IO_ACTIVE;
10939					io->port_status = 31340;
10940					ctl_enqueue_isc((union ctl_io *)io);
10941				}
10942			}
10943			/* We are slave */
10944			if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10945				io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10946				if (io->flags & CTL_FLAG_IO_ACTIVE) {
10947					io->flags |= CTL_FLAG_FAILOVER;
10948				} else {
10949					ctl_set_busy(&((union ctl_io *)io)->
10950					    scsiio);
10951					ctl_done((union ctl_io *)io);
10952				}
10953			}
10954		}
10955	} else { /* SERIALIZE modes */
10956		TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
10957		    next_io) {
10958			/* We are master */
10959			if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10960				TAILQ_REMOVE(&lun->blocked_queue, io,
10961				    blocked_links);
10962				io->flags &= ~CTL_FLAG_BLOCKED;
10963				TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10964				ctl_free_io((union ctl_io *)io);
10965			}
10966		}
10967		TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10968			/* We are master */
10969			if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10970				TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10971				ctl_free_io((union ctl_io *)io);
10972			}
10973			/* We are slave */
10974			if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10975				io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10976				if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
10977					ctl_set_busy(&((union ctl_io *)io)->
10978					    scsiio);
10979					ctl_done((union ctl_io *)io);
10980				}
10981			}
10982		}
10983		ctl_check_blocked(lun);
10984	}
10985}
10986
10987static int
10988ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
10989{
10990	struct ctl_lun *lun;
10991	const struct ctl_cmd_entry *entry;
10992	uint32_t initidx, targ_lun;
10993	int retval;
10994
10995	retval = 0;
10996
10997	lun = NULL;
10998
10999	targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11000	if ((targ_lun < CTL_MAX_LUNS)
11001	 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11002		/*
11003		 * If the LUN is invalid, pretend that it doesn't exist.
11004		 * It will go away as soon as all pending I/O has been
11005		 * completed.
11006		 */
11007		mtx_lock(&lun->lun_lock);
11008		if (lun->flags & CTL_LUN_DISABLED) {
11009			mtx_unlock(&lun->lun_lock);
11010			lun = NULL;
11011			ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11012			ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11013		} else {
11014			ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11015			ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11016				lun->be_lun;
11017
11018			/*
11019			 * Every I/O goes into the OOA queue for a
11020			 * particular LUN, and stays there until completion.
11021			 */
11022#ifdef CTL_TIME_IO
11023			if (TAILQ_EMPTY(&lun->ooa_queue)) {
11024				lun->idle_time += getsbinuptime() -
11025				    lun->last_busy;
11026			}
11027#endif
11028			TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11029			    ooa_links);
11030		}
11031	} else {
11032		ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11033		ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11034	}
11035
11036	/* Get command entry and return error if it is unsuppotyed. */
11037	entry = ctl_validate_command(ctsio);
11038	if (entry == NULL) {
11039		if (lun)
11040			mtx_unlock(&lun->lun_lock);
11041		return (retval);
11042	}
11043
11044	ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11045	ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11046
11047	/*
11048	 * Check to see whether we can send this command to LUNs that don't
11049	 * exist.  This should pretty much only be the case for inquiry
11050	 * and request sense.  Further checks, below, really require having
11051	 * a LUN, so we can't really check the command anymore.  Just put
11052	 * it on the rtr queue.
11053	 */
11054	if (lun == NULL) {
11055		if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11056			ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11057			ctl_enqueue_rtr((union ctl_io *)ctsio);
11058			return (retval);
11059		}
11060
11061		ctl_set_unsupported_lun(ctsio);
11062		ctl_done((union ctl_io *)ctsio);
11063		CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11064		return (retval);
11065	} else {
11066		/*
11067		 * Make sure we support this particular command on this LUN.
11068		 * e.g., we don't support writes to the control LUN.
11069		 */
11070		if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11071			mtx_unlock(&lun->lun_lock);
11072			ctl_set_invalid_opcode(ctsio);
11073			ctl_done((union ctl_io *)ctsio);
11074			return (retval);
11075		}
11076	}
11077
11078	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11079
11080#ifdef CTL_WITH_CA
11081	/*
11082	 * If we've got a request sense, it'll clear the contingent
11083	 * allegiance condition.  Otherwise, if we have a CA condition for
11084	 * this initiator, clear it, because it sent down a command other
11085	 * than request sense.
11086	 */
11087	if ((ctsio->cdb[0] != REQUEST_SENSE)
11088	 && (ctl_is_set(lun->have_ca, initidx)))
11089		ctl_clear_mask(lun->have_ca, initidx);
11090#endif
11091
11092	/*
11093	 * If the command has this flag set, it handles its own unit
11094	 * attention reporting, we shouldn't do anything.  Otherwise we
11095	 * check for any pending unit attentions, and send them back to the
11096	 * initiator.  We only do this when a command initially comes in,
11097	 * not when we pull it off the blocked queue.
11098	 *
11099	 * According to SAM-3, section 5.3.2, the order that things get
11100	 * presented back to the host is basically unit attentions caused
11101	 * by some sort of reset event, busy status, reservation conflicts
11102	 * or task set full, and finally any other status.
11103	 *
11104	 * One issue here is that some of the unit attentions we report
11105	 * don't fall into the "reset" category (e.g. "reported luns data
11106	 * has changed").  So reporting it here, before the reservation
11107	 * check, may be technically wrong.  I guess the only thing to do
11108	 * would be to check for and report the reset events here, and then
11109	 * check for the other unit attention types after we check for a
11110	 * reservation conflict.
11111	 *
11112	 * XXX KDM need to fix this
11113	 */
11114	if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11115		ctl_ua_type ua_type;
11116		scsi_sense_data_type sense_format;
11117
11118		if (lun->flags & CTL_LUN_SENSE_DESC)
11119			sense_format = SSD_TYPE_DESC;
11120		else
11121			sense_format = SSD_TYPE_FIXED;
11122
11123		ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11124		    sense_format);
11125		if (ua_type != CTL_UA_NONE) {
11126			mtx_unlock(&lun->lun_lock);
11127			ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11128			ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11129			ctsio->sense_len = SSD_FULL_SIZE;
11130			ctl_done((union ctl_io *)ctsio);
11131			return (retval);
11132		}
11133	}
11134
11135
11136	if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11137		mtx_unlock(&lun->lun_lock);
11138		ctl_done((union ctl_io *)ctsio);
11139		return (retval);
11140	}
11141
11142	/*
11143	 * XXX CHD this is where we want to send IO to other side if
11144	 * this LUN is secondary on this SC. We will need to make a copy
11145	 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11146	 * the copy we send as FROM_OTHER.
11147	 * We also need to stuff the address of the original IO so we can
11148	 * find it easily. Something similar will need be done on the other
11149	 * side so when we are done we can find the copy.
11150	 */
11151	if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11152	    (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0) {
11153		union ctl_ha_msg msg_info;
11154		int isc_retval;
11155
11156		ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11157		ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11158		mtx_unlock(&lun->lun_lock);
11159
11160		msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11161		msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11162		msg_info.hdr.serializing_sc = NULL;
11163		msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11164		msg_info.scsi.tag_num = ctsio->tag_num;
11165		msg_info.scsi.tag_type = ctsio->tag_type;
11166		msg_info.scsi.cdb_len = ctsio->cdb_len;
11167		memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11168
11169		if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11170		    sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11171		    M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11172			ctl_set_busy(ctsio);
11173			ctl_done((union ctl_io *)ctsio);
11174			return (retval);
11175		}
11176		return (retval);
11177	}
11178
11179	switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11180			      (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11181			      ctl_ooaq, ooa_links))) {
11182	case CTL_ACTION_BLOCK:
11183		ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11184		TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11185				  blocked_links);
11186		mtx_unlock(&lun->lun_lock);
11187		return (retval);
11188	case CTL_ACTION_PASS:
11189	case CTL_ACTION_SKIP:
11190		ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11191		mtx_unlock(&lun->lun_lock);
11192		ctl_enqueue_rtr((union ctl_io *)ctsio);
11193		break;
11194	case CTL_ACTION_OVERLAP:
11195		mtx_unlock(&lun->lun_lock);
11196		ctl_set_overlapped_cmd(ctsio);
11197		ctl_done((union ctl_io *)ctsio);
11198		break;
11199	case CTL_ACTION_OVERLAP_TAG:
11200		mtx_unlock(&lun->lun_lock);
11201		ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11202		ctl_done((union ctl_io *)ctsio);
11203		break;
11204	case CTL_ACTION_ERROR:
11205	default:
11206		mtx_unlock(&lun->lun_lock);
11207		ctl_set_internal_failure(ctsio,
11208					 /*sks_valid*/ 0,
11209					 /*retry_count*/ 0);
11210		ctl_done((union ctl_io *)ctsio);
11211		break;
11212	}
11213	return (retval);
11214}
11215
11216const struct ctl_cmd_entry *
11217ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11218{
11219	const struct ctl_cmd_entry *entry;
11220	int service_action;
11221
11222	entry = &ctl_cmd_table[ctsio->cdb[0]];
11223	if (sa)
11224		*sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11225	if (entry->flags & CTL_CMD_FLAG_SA5) {
11226		service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11227		entry = &((const struct ctl_cmd_entry *)
11228		    entry->execute)[service_action];
11229	}
11230	return (entry);
11231}
11232
11233const struct ctl_cmd_entry *
11234ctl_validate_command(struct ctl_scsiio *ctsio)
11235{
11236	const struct ctl_cmd_entry *entry;
11237	int i, sa;
11238	uint8_t diff;
11239
11240	entry = ctl_get_cmd_entry(ctsio, &sa);
11241	if (entry->execute == NULL) {
11242		if (sa)
11243			ctl_set_invalid_field(ctsio,
11244					      /*sks_valid*/ 1,
11245					      /*command*/ 1,
11246					      /*field*/ 1,
11247					      /*bit_valid*/ 1,
11248					      /*bit*/ 4);
11249		else
11250			ctl_set_invalid_opcode(ctsio);
11251		ctl_done((union ctl_io *)ctsio);
11252		return (NULL);
11253	}
11254	KASSERT(entry->length > 0,
11255	    ("Not defined length for command 0x%02x/0x%02x",
11256	     ctsio->cdb[0], ctsio->cdb[1]));
11257	for (i = 1; i < entry->length; i++) {
11258		diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11259		if (diff == 0)
11260			continue;
11261		ctl_set_invalid_field(ctsio,
11262				      /*sks_valid*/ 1,
11263				      /*command*/ 1,
11264				      /*field*/ i,
11265				      /*bit_valid*/ 1,
11266				      /*bit*/ fls(diff) - 1);
11267		ctl_done((union ctl_io *)ctsio);
11268		return (NULL);
11269	}
11270	return (entry);
11271}
11272
11273static int
11274ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11275{
11276
11277	switch (lun_type) {
11278	case T_PROCESSOR:
11279		if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11280		    ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11281			return (0);
11282		break;
11283	case T_DIRECT:
11284		if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11285		    ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11286			return (0);
11287		break;
11288	default:
11289		return (0);
11290	}
11291	return (1);
11292}
11293
11294static int
11295ctl_scsiio(struct ctl_scsiio *ctsio)
11296{
11297	int retval;
11298	const struct ctl_cmd_entry *entry;
11299
11300	retval = CTL_RETVAL_COMPLETE;
11301
11302	CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11303
11304	entry = ctl_get_cmd_entry(ctsio, NULL);
11305
11306	/*
11307	 * If this I/O has been aborted, just send it straight to
11308	 * ctl_done() without executing it.
11309	 */
11310	if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11311		ctl_done((union ctl_io *)ctsio);
11312		goto bailout;
11313	}
11314
11315	/*
11316	 * All the checks should have been handled by ctl_scsiio_precheck().
11317	 * We should be clear now to just execute the I/O.
11318	 */
11319	retval = entry->execute(ctsio);
11320
11321bailout:
11322	return (retval);
11323}
11324
11325/*
11326 * Since we only implement one target right now, a bus reset simply resets
11327 * our single target.
11328 */
11329static int
11330ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11331{
11332	return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11333}
11334
11335static int
11336ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11337		 ctl_ua_type ua_type)
11338{
11339	struct ctl_lun *lun;
11340	int retval;
11341
11342	if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11343		union ctl_ha_msg msg_info;
11344
11345		msg_info.hdr.nexus = io->io_hdr.nexus;
11346		if (ua_type==CTL_UA_TARG_RESET)
11347			msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11348		else
11349			msg_info.task.task_action = CTL_TASK_BUS_RESET;
11350		msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11351		msg_info.hdr.original_sc = NULL;
11352		msg_info.hdr.serializing_sc = NULL;
11353		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11354		    sizeof(msg_info.task), M_WAITOK);
11355	}
11356	retval = 0;
11357
11358	mtx_lock(&softc->ctl_lock);
11359	STAILQ_FOREACH(lun, &softc->lun_list, links)
11360		retval += ctl_lun_reset(lun, io, ua_type);
11361	mtx_unlock(&softc->ctl_lock);
11362
11363	return (retval);
11364}
11365
11366/*
11367 * The LUN should always be set.  The I/O is optional, and is used to
11368 * distinguish between I/Os sent by this initiator, and by other
11369 * initiators.  We set unit attention for initiators other than this one.
11370 * SAM-3 is vague on this point.  It does say that a unit attention should
11371 * be established for other initiators when a LUN is reset (see section
11372 * 5.7.3), but it doesn't specifically say that the unit attention should
11373 * be established for this particular initiator when a LUN is reset.  Here
11374 * is the relevant text, from SAM-3 rev 8:
11375 *
11376 * 5.7.2 When a SCSI initiator port aborts its own tasks
11377 *
11378 * When a SCSI initiator port causes its own task(s) to be aborted, no
11379 * notification that the task(s) have been aborted shall be returned to
11380 * the SCSI initiator port other than the completion response for the
11381 * command or task management function action that caused the task(s) to
11382 * be aborted and notification(s) associated with related effects of the
11383 * action (e.g., a reset unit attention condition).
11384 *
11385 * XXX KDM for now, we're setting unit attention for all initiators.
11386 */
11387static int
11388ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11389{
11390	union ctl_io *xio;
11391#if 0
11392	uint32_t initidx;
11393#endif
11394#ifdef CTL_WITH_CA
11395	int i;
11396#endif
11397
11398	mtx_lock(&lun->lun_lock);
11399	/*
11400	 * Run through the OOA queue and abort each I/O.
11401	 */
11402	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11403	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11404		xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11405	}
11406
11407	/*
11408	 * This version sets unit attention for every
11409	 */
11410#if 0
11411	initidx = ctl_get_initindex(&io->io_hdr.nexus);
11412	ctl_est_ua_all(lun, initidx, ua_type);
11413#else
11414	ctl_est_ua_all(lun, -1, ua_type);
11415#endif
11416
11417	/*
11418	 * A reset (any kind, really) clears reservations established with
11419	 * RESERVE/RELEASE.  It does not clear reservations established
11420	 * with PERSISTENT RESERVE OUT, but we don't support that at the
11421	 * moment anyway.  See SPC-2, section 5.6.  SPC-3 doesn't address
11422	 * reservations made with the RESERVE/RELEASE commands, because
11423	 * those commands are obsolete in SPC-3.
11424	 */
11425	lun->flags &= ~CTL_LUN_RESERVED;
11426
11427#ifdef CTL_WITH_CA
11428	for (i = 0; i < CTL_MAX_INITIATORS; i++)
11429		ctl_clear_mask(lun->have_ca, i);
11430#endif
11431	mtx_unlock(&lun->lun_lock);
11432
11433	return (0);
11434}
11435
11436static void
11437ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11438    int other_sc)
11439{
11440	union ctl_io *xio;
11441
11442	mtx_assert(&lun->lun_lock, MA_OWNED);
11443
11444	/*
11445	 * Run through the OOA queue and attempt to find the given I/O.
11446	 * The target port, initiator ID, tag type and tag number have to
11447	 * match the values that we got from the initiator.  If we have an
11448	 * untagged command to abort, simply abort the first untagged command
11449	 * we come to.  We only allow one untagged command at a time of course.
11450	 */
11451	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11452	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11453
11454		if ((targ_port == UINT32_MAX ||
11455		     targ_port == xio->io_hdr.nexus.targ_port) &&
11456		    (init_id == UINT32_MAX ||
11457		     init_id == xio->io_hdr.nexus.initid)) {
11458			if (targ_port != xio->io_hdr.nexus.targ_port ||
11459			    init_id != xio->io_hdr.nexus.initid)
11460				xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11461			xio->io_hdr.flags |= CTL_FLAG_ABORT;
11462			if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11463				union ctl_ha_msg msg_info;
11464
11465				msg_info.hdr.nexus = xio->io_hdr.nexus;
11466				msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11467				msg_info.task.tag_num = xio->scsiio.tag_num;
11468				msg_info.task.tag_type = xio->scsiio.tag_type;
11469				msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11470				msg_info.hdr.original_sc = NULL;
11471				msg_info.hdr.serializing_sc = NULL;
11472				ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11473				    sizeof(msg_info.task), M_NOWAIT);
11474			}
11475		}
11476	}
11477}
11478
11479static int
11480ctl_abort_task_set(union ctl_io *io)
11481{
11482	struct ctl_softc *softc = control_softc;
11483	struct ctl_lun *lun;
11484	uint32_t targ_lun;
11485
11486	/*
11487	 * Look up the LUN.
11488	 */
11489	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11490	mtx_lock(&softc->ctl_lock);
11491	if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11492		lun = softc->ctl_luns[targ_lun];
11493	else {
11494		mtx_unlock(&softc->ctl_lock);
11495		return (1);
11496	}
11497
11498	mtx_lock(&lun->lun_lock);
11499	mtx_unlock(&softc->ctl_lock);
11500	if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11501		ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11502		    io->io_hdr.nexus.initid,
11503		    (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11504	} else { /* CTL_TASK_CLEAR_TASK_SET */
11505		ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11506		    (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11507	}
11508	mtx_unlock(&lun->lun_lock);
11509	return (0);
11510}
11511
11512static int
11513ctl_i_t_nexus_reset(union ctl_io *io)
11514{
11515	struct ctl_softc *softc = control_softc;
11516	struct ctl_lun *lun;
11517	uint32_t initidx;
11518
11519	initidx = ctl_get_initindex(&io->io_hdr.nexus);
11520	mtx_lock(&softc->ctl_lock);
11521	STAILQ_FOREACH(lun, &softc->lun_list, links) {
11522		mtx_lock(&lun->lun_lock);
11523		ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11524		    io->io_hdr.nexus.initid,
11525		    (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11526#ifdef CTL_WITH_CA
11527		ctl_clear_mask(lun->have_ca, initidx);
11528#endif
11529		if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11530			lun->flags &= ~CTL_LUN_RESERVED;
11531		ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11532		mtx_unlock(&lun->lun_lock);
11533	}
11534	mtx_unlock(&softc->ctl_lock);
11535	return (0);
11536}
11537
11538static int
11539ctl_abort_task(union ctl_io *io)
11540{
11541	union ctl_io *xio;
11542	struct ctl_lun *lun;
11543	struct ctl_softc *softc;
11544#if 0
11545	struct sbuf sb;
11546	char printbuf[128];
11547#endif
11548	int found;
11549	uint32_t targ_lun;
11550
11551	softc = control_softc;
11552	found = 0;
11553
11554	/*
11555	 * Look up the LUN.
11556	 */
11557	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11558	mtx_lock(&softc->ctl_lock);
11559	if ((targ_lun < CTL_MAX_LUNS)
11560	 && (softc->ctl_luns[targ_lun] != NULL))
11561		lun = softc->ctl_luns[targ_lun];
11562	else {
11563		mtx_unlock(&softc->ctl_lock);
11564		return (1);
11565	}
11566
11567#if 0
11568	printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11569	       lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11570#endif
11571
11572	mtx_lock(&lun->lun_lock);
11573	mtx_unlock(&softc->ctl_lock);
11574	/*
11575	 * Run through the OOA queue and attempt to find the given I/O.
11576	 * The target port, initiator ID, tag type and tag number have to
11577	 * match the values that we got from the initiator.  If we have an
11578	 * untagged command to abort, simply abort the first untagged command
11579	 * we come to.  We only allow one untagged command at a time of course.
11580	 */
11581	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11582	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11583#if 0
11584		sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11585
11586		sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11587			    lun->lun, xio->scsiio.tag_num,
11588			    xio->scsiio.tag_type,
11589			    (xio->io_hdr.blocked_links.tqe_prev
11590			    == NULL) ? "" : " BLOCKED",
11591			    (xio->io_hdr.flags &
11592			    CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11593			    (xio->io_hdr.flags &
11594			    CTL_FLAG_ABORT) ? " ABORT" : "",
11595			    (xio->io_hdr.flags &
11596			    CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11597		ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11598		sbuf_finish(&sb);
11599		printf("%s\n", sbuf_data(&sb));
11600#endif
11601
11602		if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11603		 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11604		 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11605			continue;
11606
11607		/*
11608		 * If the abort says that the task is untagged, the
11609		 * task in the queue must be untagged.  Otherwise,
11610		 * we just check to see whether the tag numbers
11611		 * match.  This is because the QLogic firmware
11612		 * doesn't pass back the tag type in an abort
11613		 * request.
11614		 */
11615#if 0
11616		if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11617		  && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11618		 || (xio->scsiio.tag_num == io->taskio.tag_num))
11619#endif
11620		/*
11621		 * XXX KDM we've got problems with FC, because it
11622		 * doesn't send down a tag type with aborts.  So we
11623		 * can only really go by the tag number...
11624		 * This may cause problems with parallel SCSI.
11625		 * Need to figure that out!!
11626		 */
11627		if (xio->scsiio.tag_num == io->taskio.tag_num) {
11628			xio->io_hdr.flags |= CTL_FLAG_ABORT;
11629			found = 1;
11630			if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11631			    !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11632				union ctl_ha_msg msg_info;
11633
11634				msg_info.hdr.nexus = io->io_hdr.nexus;
11635				msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11636				msg_info.task.tag_num = io->taskio.tag_num;
11637				msg_info.task.tag_type = io->taskio.tag_type;
11638				msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11639				msg_info.hdr.original_sc = NULL;
11640				msg_info.hdr.serializing_sc = NULL;
11641#if 0
11642				printf("Sent Abort to other side\n");
11643#endif
11644				ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11645				    sizeof(msg_info.task), M_NOWAIT);
11646			}
11647#if 0
11648			printf("ctl_abort_task: found I/O to abort\n");
11649#endif
11650		}
11651	}
11652	mtx_unlock(&lun->lun_lock);
11653
11654	if (found == 0) {
11655		/*
11656		 * This isn't really an error.  It's entirely possible for
11657		 * the abort and command completion to cross on the wire.
11658		 * This is more of an informative/diagnostic error.
11659		 */
11660#if 0
11661		printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11662		       "%u:%u:%u tag %d type %d\n",
11663		       io->io_hdr.nexus.initid,
11664		       io->io_hdr.nexus.targ_port,
11665		       io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11666		       io->taskio.tag_type);
11667#endif
11668	}
11669	return (0);
11670}
11671
11672static void
11673ctl_run_task(union ctl_io *io)
11674{
11675	struct ctl_softc *softc = control_softc;
11676	int retval = 1;
11677	const char *task_desc;
11678
11679	CTL_DEBUG_PRINT(("ctl_run_task\n"));
11680
11681	KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11682	    ("ctl_run_task: Unextected io_type %d\n",
11683	     io->io_hdr.io_type));
11684
11685	task_desc = ctl_scsi_task_string(&io->taskio);
11686	if (task_desc != NULL) {
11687#ifdef NEEDTOPORT
11688		csevent_log(CSC_CTL | CSC_SHELF_SW |
11689			    CTL_TASK_REPORT,
11690			    csevent_LogType_Trace,
11691			    csevent_Severity_Information,
11692			    csevent_AlertLevel_Green,
11693			    csevent_FRU_Firmware,
11694			    csevent_FRU_Unknown,
11695			    "CTL: received task: %s",task_desc);
11696#endif
11697	} else {
11698#ifdef NEEDTOPORT
11699		csevent_log(CSC_CTL | CSC_SHELF_SW |
11700			    CTL_TASK_REPORT,
11701			    csevent_LogType_Trace,
11702			    csevent_Severity_Information,
11703			    csevent_AlertLevel_Green,
11704			    csevent_FRU_Firmware,
11705			    csevent_FRU_Unknown,
11706			    "CTL: received unknown task "
11707			    "type: %d (%#x)",
11708			    io->taskio.task_action,
11709			    io->taskio.task_action);
11710#endif
11711	}
11712	switch (io->taskio.task_action) {
11713	case CTL_TASK_ABORT_TASK:
11714		retval = ctl_abort_task(io);
11715		break;
11716	case CTL_TASK_ABORT_TASK_SET:
11717	case CTL_TASK_CLEAR_TASK_SET:
11718		retval = ctl_abort_task_set(io);
11719		break;
11720	case CTL_TASK_CLEAR_ACA:
11721		break;
11722	case CTL_TASK_I_T_NEXUS_RESET:
11723		retval = ctl_i_t_nexus_reset(io);
11724		break;
11725	case CTL_TASK_LUN_RESET: {
11726		struct ctl_lun *lun;
11727		uint32_t targ_lun;
11728
11729		targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11730		mtx_lock(&softc->ctl_lock);
11731		if ((targ_lun < CTL_MAX_LUNS)
11732		 && (softc->ctl_luns[targ_lun] != NULL))
11733			lun = softc->ctl_luns[targ_lun];
11734		else {
11735			mtx_unlock(&softc->ctl_lock);
11736			retval = 1;
11737			break;
11738		}
11739		retval = ctl_lun_reset(lun, io, CTL_UA_LUN_RESET);
11740		mtx_unlock(&softc->ctl_lock);
11741
11742		if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11743			union ctl_ha_msg msg_info;
11744
11745			msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11746			msg_info.hdr.nexus = io->io_hdr.nexus;
11747			msg_info.task.task_action = CTL_TASK_LUN_RESET;
11748			msg_info.hdr.original_sc = NULL;
11749			msg_info.hdr.serializing_sc = NULL;
11750			ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11751			    sizeof(msg_info.task), M_WAITOK);
11752		}
11753		break;
11754	}
11755	case CTL_TASK_TARGET_RESET:
11756		retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
11757		break;
11758	case CTL_TASK_BUS_RESET:
11759		retval = ctl_bus_reset(softc, io);
11760		break;
11761	case CTL_TASK_PORT_LOGIN:
11762		break;
11763	case CTL_TASK_PORT_LOGOUT:
11764		break;
11765	default:
11766		printf("ctl_run_task: got unknown task management event %d\n",
11767		       io->taskio.task_action);
11768		break;
11769	}
11770	if (retval == 0)
11771		io->io_hdr.status = CTL_SUCCESS;
11772	else
11773		io->io_hdr.status = CTL_ERROR;
11774	ctl_done(io);
11775}
11776
11777/*
11778 * For HA operation.  Handle commands that come in from the other
11779 * controller.
11780 */
11781static void
11782ctl_handle_isc(union ctl_io *io)
11783{
11784	int free_io;
11785	struct ctl_lun *lun;
11786	struct ctl_softc *softc;
11787	uint32_t targ_lun;
11788
11789	softc = control_softc;
11790
11791	targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11792	lun = softc->ctl_luns[targ_lun];
11793
11794	switch (io->io_hdr.msg_type) {
11795	case CTL_MSG_SERIALIZE:
11796		free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11797		break;
11798	case CTL_MSG_R2R: {
11799		const struct ctl_cmd_entry *entry;
11800
11801		/*
11802		 * This is only used in SER_ONLY mode.
11803		 */
11804		free_io = 0;
11805		entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11806		mtx_lock(&lun->lun_lock);
11807		if (ctl_scsiio_lun_check(lun,
11808		    entry, (struct ctl_scsiio *)io) != 0) {
11809			mtx_unlock(&lun->lun_lock);
11810			ctl_done(io);
11811			break;
11812		}
11813		io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11814		mtx_unlock(&lun->lun_lock);
11815		ctl_enqueue_rtr(io);
11816		break;
11817	}
11818	case CTL_MSG_FINISH_IO:
11819		if (softc->ha_mode == CTL_HA_MODE_XFER) {
11820			free_io = 0;
11821			ctl_done(io);
11822		} else {
11823			free_io = 1;
11824			mtx_lock(&lun->lun_lock);
11825			TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11826				     ooa_links);
11827			ctl_check_blocked(lun);
11828			mtx_unlock(&lun->lun_lock);
11829		}
11830		break;
11831	case CTL_MSG_PERS_ACTION:
11832		ctl_hndl_per_res_out_on_other_sc(
11833			(union ctl_ha_msg *)&io->presio.pr_msg);
11834		free_io = 1;
11835		break;
11836	case CTL_MSG_BAD_JUJU:
11837		free_io = 0;
11838		ctl_done(io);
11839		break;
11840	case CTL_MSG_DATAMOVE:
11841		/* Only used in XFER mode */
11842		free_io = 0;
11843		ctl_datamove_remote(io);
11844		break;
11845	case CTL_MSG_DATAMOVE_DONE:
11846		/* Only used in XFER mode */
11847		free_io = 0;
11848		io->scsiio.be_move_done(io);
11849		break;
11850	case CTL_MSG_FAILOVER:
11851		mtx_lock(&lun->lun_lock);
11852		ctl_failover_lun(lun);
11853		mtx_unlock(&lun->lun_lock);
11854		free_io = 1;
11855		break;
11856	default:
11857		free_io = 1;
11858		printf("%s: Invalid message type %d\n",
11859		       __func__, io->io_hdr.msg_type);
11860		break;
11861	}
11862	if (free_io)
11863		ctl_free_io(io);
11864
11865}
11866
11867
11868/*
11869 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11870 * there is no match.
11871 */
11872static ctl_lun_error_pattern
11873ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11874{
11875	const struct ctl_cmd_entry *entry;
11876	ctl_lun_error_pattern filtered_pattern, pattern;
11877
11878	pattern = desc->error_pattern;
11879
11880	/*
11881	 * XXX KDM we need more data passed into this function to match a
11882	 * custom pattern, and we actually need to implement custom pattern
11883	 * matching.
11884	 */
11885	if (pattern & CTL_LUN_PAT_CMD)
11886		return (CTL_LUN_PAT_CMD);
11887
11888	if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11889		return (CTL_LUN_PAT_ANY);
11890
11891	entry = ctl_get_cmd_entry(ctsio, NULL);
11892
11893	filtered_pattern = entry->pattern & pattern;
11894
11895	/*
11896	 * If the user requested specific flags in the pattern (e.g.
11897	 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11898	 * flags.
11899	 *
11900	 * If the user did not specify any flags, it doesn't matter whether
11901	 * or not the command supports the flags.
11902	 */
11903	if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11904	     (pattern & ~CTL_LUN_PAT_MASK))
11905		return (CTL_LUN_PAT_NONE);
11906
11907	/*
11908	 * If the user asked for a range check, see if the requested LBA
11909	 * range overlaps with this command's LBA range.
11910	 */
11911	if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11912		uint64_t lba1;
11913		uint64_t len1;
11914		ctl_action action;
11915		int retval;
11916
11917		retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11918		if (retval != 0)
11919			return (CTL_LUN_PAT_NONE);
11920
11921		action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11922					      desc->lba_range.len, FALSE);
11923		/*
11924		 * A "pass" means that the LBA ranges don't overlap, so
11925		 * this doesn't match the user's range criteria.
11926		 */
11927		if (action == CTL_ACTION_PASS)
11928			return (CTL_LUN_PAT_NONE);
11929	}
11930
11931	return (filtered_pattern);
11932}
11933
11934static void
11935ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11936{
11937	struct ctl_error_desc *desc, *desc2;
11938
11939	mtx_assert(&lun->lun_lock, MA_OWNED);
11940
11941	STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11942		ctl_lun_error_pattern pattern;
11943		/*
11944		 * Check to see whether this particular command matches
11945		 * the pattern in the descriptor.
11946		 */
11947		pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11948		if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11949			continue;
11950
11951		switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11952		case CTL_LUN_INJ_ABORTED:
11953			ctl_set_aborted(&io->scsiio);
11954			break;
11955		case CTL_LUN_INJ_MEDIUM_ERR:
11956			ctl_set_medium_error(&io->scsiio);
11957			break;
11958		case CTL_LUN_INJ_UA:
11959			/* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET
11960			 * OCCURRED */
11961			ctl_set_ua(&io->scsiio, 0x29, 0x00);
11962			break;
11963		case CTL_LUN_INJ_CUSTOM:
11964			/*
11965			 * We're assuming the user knows what he is doing.
11966			 * Just copy the sense information without doing
11967			 * checks.
11968			 */
11969			bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11970			      MIN(sizeof(desc->custom_sense),
11971				  sizeof(io->scsiio.sense_data)));
11972			io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11973			io->scsiio.sense_len = SSD_FULL_SIZE;
11974			io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11975			break;
11976		case CTL_LUN_INJ_NONE:
11977		default:
11978			/*
11979			 * If this is an error injection type we don't know
11980			 * about, clear the continuous flag (if it is set)
11981			 * so it will get deleted below.
11982			 */
11983			desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11984			break;
11985		}
11986		/*
11987		 * By default, each error injection action is a one-shot
11988		 */
11989		if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11990			continue;
11991
11992		STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11993
11994		free(desc, M_CTL);
11995	}
11996}
11997
11998#ifdef CTL_IO_DELAY
11999static void
12000ctl_datamove_timer_wakeup(void *arg)
12001{
12002	union ctl_io *io;
12003
12004	io = (union ctl_io *)arg;
12005
12006	ctl_datamove(io);
12007}
12008#endif /* CTL_IO_DELAY */
12009
12010void
12011ctl_datamove(union ctl_io *io)
12012{
12013	void (*fe_datamove)(union ctl_io *io);
12014
12015	mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12016
12017	CTL_DEBUG_PRINT(("ctl_datamove\n"));
12018
12019#ifdef CTL_TIME_IO
12020	if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12021		char str[256];
12022		char path_str[64];
12023		struct sbuf sb;
12024
12025		ctl_scsi_path_string(io, path_str, sizeof(path_str));
12026		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12027
12028		sbuf_cat(&sb, path_str);
12029		switch (io->io_hdr.io_type) {
12030		case CTL_IO_SCSI:
12031			ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12032			sbuf_printf(&sb, "\n");
12033			sbuf_cat(&sb, path_str);
12034			sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12035				    io->scsiio.tag_num, io->scsiio.tag_type);
12036			break;
12037		case CTL_IO_TASK:
12038			sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12039				    "Tag Type: %d\n", io->taskio.task_action,
12040				    io->taskio.tag_num, io->taskio.tag_type);
12041			break;
12042		default:
12043			printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12044			panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12045			break;
12046		}
12047		sbuf_cat(&sb, path_str);
12048		sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12049			    (intmax_t)time_uptime - io->io_hdr.start_time);
12050		sbuf_finish(&sb);
12051		printf("%s", sbuf_data(&sb));
12052	}
12053#endif /* CTL_TIME_IO */
12054
12055#ifdef CTL_IO_DELAY
12056	if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12057		io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12058	} else {
12059		struct ctl_lun *lun;
12060
12061		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12062		if ((lun != NULL)
12063		 && (lun->delay_info.datamove_delay > 0)) {
12064
12065			callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12066			io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12067			callout_reset(&io->io_hdr.delay_callout,
12068				      lun->delay_info.datamove_delay * hz,
12069				      ctl_datamove_timer_wakeup, io);
12070			if (lun->delay_info.datamove_type ==
12071			    CTL_DELAY_TYPE_ONESHOT)
12072				lun->delay_info.datamove_delay = 0;
12073			return;
12074		}
12075	}
12076#endif
12077
12078	/*
12079	 * This command has been aborted.  Set the port status, so we fail
12080	 * the data move.
12081	 */
12082	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12083		printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12084		       io->scsiio.tag_num, io->io_hdr.nexus.initid,
12085		       io->io_hdr.nexus.targ_port,
12086		       io->io_hdr.nexus.targ_lun);
12087		io->io_hdr.port_status = 31337;
12088		/*
12089		 * Note that the backend, in this case, will get the
12090		 * callback in its context.  In other cases it may get
12091		 * called in the frontend's interrupt thread context.
12092		 */
12093		io->scsiio.be_move_done(io);
12094		return;
12095	}
12096
12097	/* Don't confuse frontend with zero length data move. */
12098	if (io->scsiio.kern_data_len == 0) {
12099		io->scsiio.be_move_done(io);
12100		return;
12101	}
12102
12103	/*
12104	 * If we're in XFER mode and this I/O is from the other shelf
12105	 * controller, we need to send the DMA to the other side to
12106	 * actually transfer the data to/from the host.  In serialize only
12107	 * mode the transfer happens below CTL and ctl_datamove() is only
12108	 * called on the machine that originally received the I/O.
12109	 */
12110	if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12111	 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12112		union ctl_ha_msg msg;
12113		uint32_t sg_entries_sent;
12114		int do_sg_copy;
12115		int i;
12116
12117		memset(&msg, 0, sizeof(msg));
12118		msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12119		msg.hdr.original_sc = io->io_hdr.original_sc;
12120		msg.hdr.serializing_sc = io;
12121		msg.hdr.nexus = io->io_hdr.nexus;
12122		msg.dt.flags = io->io_hdr.flags;
12123		/*
12124		 * We convert everything into a S/G list here.  We can't
12125		 * pass by reference, only by value between controllers.
12126		 * So we can't pass a pointer to the S/G list, only as many
12127		 * S/G entries as we can fit in here.  If it's possible for
12128		 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12129		 * then we need to break this up into multiple transfers.
12130		 */
12131		if (io->scsiio.kern_sg_entries == 0) {
12132			msg.dt.kern_sg_entries = 1;
12133#if 0
12134			/*
12135			 * Convert to a physical address if this is a
12136			 * virtual address.
12137			 */
12138			if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12139				msg.dt.sg_list[0].addr =
12140					io->scsiio.kern_data_ptr;
12141			} else {
12142				/*
12143				 * XXX KDM use busdma here!
12144				 */
12145				msg.dt.sg_list[0].addr = (void *)
12146					vtophys(io->scsiio.kern_data_ptr);
12147			}
12148#else
12149			KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12150			    ("HA does not support BUS_ADDR"));
12151			msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
12152#endif
12153
12154			msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12155			do_sg_copy = 0;
12156		} else {
12157			msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12158			do_sg_copy = 1;
12159		}
12160
12161		msg.dt.kern_data_len = io->scsiio.kern_data_len;
12162		msg.dt.kern_total_len = io->scsiio.kern_total_len;
12163		msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12164		msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12165		msg.dt.sg_sequence = 0;
12166
12167		/*
12168		 * Loop until we've sent all of the S/G entries.  On the
12169		 * other end, we'll recompose these S/G entries into one
12170		 * contiguous list before passing it to the
12171		 */
12172		for (sg_entries_sent = 0; sg_entries_sent <
12173		     msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12174			msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12175				sizeof(msg.dt.sg_list[0])),
12176				msg.dt.kern_sg_entries - sg_entries_sent);
12177
12178			if (do_sg_copy != 0) {
12179				struct ctl_sg_entry *sgl;
12180				int j;
12181
12182				sgl = (struct ctl_sg_entry *)
12183					io->scsiio.kern_data_ptr;
12184				/*
12185				 * If this is in cached memory, flush the cache
12186				 * before we send the DMA request to the other
12187				 * controller.  We want to do this in either
12188				 * the * read or the write case.  The read
12189				 * case is straightforward.  In the write
12190				 * case, we want to make sure nothing is
12191				 * in the local cache that could overwrite
12192				 * the DMAed data.
12193				 */
12194
12195				for (i = sg_entries_sent, j = 0;
12196				     i < msg.dt.cur_sg_entries; i++, j++) {
12197#if 0
12198					if ((io->io_hdr.flags &
12199					     CTL_FLAG_BUS_ADDR) == 0) {
12200						/*
12201						 * XXX KDM use busdma.
12202						 */
12203						msg.dt.sg_list[j].addr =(void *)
12204						       vtophys(sgl[i].addr);
12205					} else {
12206						msg.dt.sg_list[j].addr =
12207							sgl[i].addr;
12208					}
12209#else
12210					KASSERT((io->io_hdr.flags &
12211					    CTL_FLAG_BUS_ADDR) == 0,
12212					    ("HA does not support BUS_ADDR"));
12213					msg.dt.sg_list[j].addr = sgl[i].addr;
12214#endif
12215					msg.dt.sg_list[j].len = sgl[i].len;
12216				}
12217			}
12218
12219			sg_entries_sent += msg.dt.cur_sg_entries;
12220			if (sg_entries_sent >= msg.dt.kern_sg_entries)
12221				msg.dt.sg_last = 1;
12222			else
12223				msg.dt.sg_last = 0;
12224
12225			if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12226			    sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
12227			    sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries,
12228			    M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
12229				io->io_hdr.port_status = 31341;
12230				io->scsiio.be_move_done(io);
12231				return;
12232			}
12233
12234			msg.dt.sent_sg_entries = sg_entries_sent;
12235		}
12236		io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12237	} else {
12238
12239		/*
12240		 * Lookup the fe_datamove() function for this particular
12241		 * front end.
12242		 */
12243		fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12244
12245		fe_datamove(io);
12246	}
12247}
12248
12249static void
12250ctl_send_datamove_done(union ctl_io *io, int have_lock)
12251{
12252	union ctl_ha_msg msg;
12253
12254	memset(&msg, 0, sizeof(msg));
12255
12256	msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12257	msg.hdr.original_sc = io;
12258	msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12259	msg.hdr.nexus = io->io_hdr.nexus;
12260	msg.hdr.status = io->io_hdr.status;
12261	msg.scsi.tag_num = io->scsiio.tag_num;
12262	msg.scsi.tag_type = io->scsiio.tag_type;
12263	msg.scsi.scsi_status = io->scsiio.scsi_status;
12264	memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12265	       io->scsiio.sense_len);
12266	msg.scsi.sense_len = io->scsiio.sense_len;
12267	msg.scsi.sense_residual = io->scsiio.sense_residual;
12268	msg.scsi.fetd_status = io->io_hdr.port_status;
12269	msg.scsi.residual = io->scsiio.residual;
12270	io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12271
12272	if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12273		ctl_failover_io(io, /*have_lock*/ have_lock);
12274		return;
12275	}
12276
12277	ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12278	    sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12279	    msg.scsi.sense_len, M_WAITOK);
12280}
12281
12282/*
12283 * The DMA to the remote side is done, now we need to tell the other side
12284 * we're done so it can continue with its data movement.
12285 */
12286static void
12287ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12288{
12289	union ctl_io *io;
12290	int i;
12291
12292	io = rq->context;
12293
12294	if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12295		printf("%s: ISC DMA write failed with error %d", __func__,
12296		       rq->ret);
12297		ctl_set_internal_failure(&io->scsiio,
12298					 /*sks_valid*/ 1,
12299					 /*retry_count*/ rq->ret);
12300	}
12301
12302	ctl_dt_req_free(rq);
12303
12304	for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12305		free(io->io_hdr.local_sglist[i].addr, M_CTL);
12306	free(io->io_hdr.remote_sglist, M_CTL);
12307	io->io_hdr.remote_sglist = NULL;
12308	io->io_hdr.local_sglist = NULL;
12309
12310	/*
12311	 * The data is in local and remote memory, so now we need to send
12312	 * status (good or back) back to the other side.
12313	 */
12314	ctl_send_datamove_done(io, /*have_lock*/ 0);
12315}
12316
12317/*
12318 * We've moved the data from the host/controller into local memory.  Now we
12319 * need to push it over to the remote controller's memory.
12320 */
12321static int
12322ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12323{
12324	int retval;
12325
12326	retval = 0;
12327
12328	retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12329					  ctl_datamove_remote_write_cb);
12330
12331	return (retval);
12332}
12333
12334static void
12335ctl_datamove_remote_write(union ctl_io *io)
12336{
12337	int retval;
12338	void (*fe_datamove)(union ctl_io *io);
12339
12340	/*
12341	 * - Get the data from the host/HBA into local memory.
12342	 * - DMA memory from the local controller to the remote controller.
12343	 * - Send status back to the remote controller.
12344	 */
12345
12346	retval = ctl_datamove_remote_sgl_setup(io);
12347	if (retval != 0)
12348		return;
12349
12350	/* Switch the pointer over so the FETD knows what to do */
12351	io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12352
12353	/*
12354	 * Use a custom move done callback, since we need to send completion
12355	 * back to the other controller, not to the backend on this side.
12356	 */
12357	io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12358
12359	fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12360
12361	fe_datamove(io);
12362
12363	return;
12364
12365}
12366
12367static int
12368ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12369{
12370#if 0
12371	char str[256];
12372	char path_str[64];
12373	struct sbuf sb;
12374#endif
12375	int i;
12376
12377	for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12378		free(io->io_hdr.local_sglist[i].addr, M_CTL);
12379	free(io->io_hdr.remote_sglist, M_CTL);
12380	io->io_hdr.remote_sglist = NULL;
12381	io->io_hdr.local_sglist = NULL;
12382
12383#if 0
12384	scsi_path_string(io, path_str, sizeof(path_str));
12385	sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12386	sbuf_cat(&sb, path_str);
12387	scsi_command_string(&io->scsiio, NULL, &sb);
12388	sbuf_printf(&sb, "\n");
12389	sbuf_cat(&sb, path_str);
12390	sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12391		    io->scsiio.tag_num, io->scsiio.tag_type);
12392	sbuf_cat(&sb, path_str);
12393	sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12394		    io->io_hdr.flags, io->io_hdr.status);
12395	sbuf_finish(&sb);
12396	printk("%s", sbuf_data(&sb));
12397#endif
12398
12399
12400	/*
12401	 * The read is done, now we need to send status (good or bad) back
12402	 * to the other side.
12403	 */
12404	ctl_send_datamove_done(io, /*have_lock*/ 0);
12405
12406	return (0);
12407}
12408
12409static void
12410ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12411{
12412	union ctl_io *io;
12413	void (*fe_datamove)(union ctl_io *io);
12414
12415	io = rq->context;
12416
12417	if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12418		printf("%s: ISC DMA read failed with error %d\n", __func__,
12419		       rq->ret);
12420		ctl_set_internal_failure(&io->scsiio,
12421					 /*sks_valid*/ 1,
12422					 /*retry_count*/ rq->ret);
12423	}
12424
12425	ctl_dt_req_free(rq);
12426
12427	/* Switch the pointer over so the FETD knows what to do */
12428	io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12429
12430	/*
12431	 * Use a custom move done callback, since we need to send completion
12432	 * back to the other controller, not to the backend on this side.
12433	 */
12434	io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12435
12436	/* XXX KDM add checks like the ones in ctl_datamove? */
12437
12438	fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12439
12440	fe_datamove(io);
12441}
12442
12443static int
12444ctl_datamove_remote_sgl_setup(union ctl_io *io)
12445{
12446	struct ctl_sg_entry *local_sglist, *remote_sglist;
12447	struct ctl_softc *softc;
12448	uint32_t len_to_go;
12449	int retval;
12450	int i;
12451
12452	retval = 0;
12453	softc = control_softc;
12454	local_sglist = io->io_hdr.local_sglist;
12455	remote_sglist = io->io_hdr.remote_sglist;
12456	len_to_go = io->scsiio.kern_data_len;
12457
12458	/*
12459	 * The difficult thing here is that the size of the various
12460	 * S/G segments may be different than the size from the
12461	 * remote controller.  That'll make it harder when DMAing
12462	 * the data back to the other side.
12463	 */
12464	for (i = 0; len_to_go > 0; i++) {
12465		local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12466		local_sglist[i].addr =
12467		    malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12468
12469		len_to_go -= local_sglist[i].len;
12470	}
12471	/*
12472	 * Reset the number of S/G entries accordingly.  The original
12473	 * number of S/G entries is available in rem_sg_entries.
12474	 */
12475	io->scsiio.kern_sg_entries = i;
12476
12477#if 0
12478	printf("%s: kern_sg_entries = %d\n", __func__,
12479	       io->scsiio.kern_sg_entries);
12480	for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12481		printf("%s: sg[%d] = %p, %d\n", __func__, i,
12482		       local_sglist[i].addr, local_sglist[i].len);
12483#endif
12484
12485	return (retval);
12486}
12487
12488static int
12489ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12490			 ctl_ha_dt_cb callback)
12491{
12492	struct ctl_ha_dt_req *rq;
12493	struct ctl_sg_entry *remote_sglist, *local_sglist;
12494	uint32_t local_used, remote_used, total_used;
12495	int i, j, isc_ret;
12496
12497	rq = ctl_dt_req_alloc();
12498
12499	/*
12500	 * If we failed to allocate the request, and if the DMA didn't fail
12501	 * anyway, set busy status.  This is just a resource allocation
12502	 * failure.
12503	 */
12504	if ((rq == NULL)
12505	 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12506		ctl_set_busy(&io->scsiio);
12507
12508	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12509
12510		if (rq != NULL)
12511			ctl_dt_req_free(rq);
12512
12513		/*
12514		 * The data move failed.  We need to return status back
12515		 * to the other controller.  No point in trying to DMA
12516		 * data to the remote controller.
12517		 */
12518
12519		ctl_send_datamove_done(io, /*have_lock*/ 0);
12520
12521		return (1);
12522	}
12523
12524	local_sglist = io->io_hdr.local_sglist;
12525	remote_sglist = io->io_hdr.remote_sglist;
12526	local_used = 0;
12527	remote_used = 0;
12528	total_used = 0;
12529
12530	/*
12531	 * Pull/push the data over the wire from/to the other controller.
12532	 * This takes into account the possibility that the local and
12533	 * remote sglists may not be identical in terms of the size of
12534	 * the elements and the number of elements.
12535	 *
12536	 * One fundamental assumption here is that the length allocated for
12537	 * both the local and remote sglists is identical.  Otherwise, we've
12538	 * essentially got a coding error of some sort.
12539	 */
12540	isc_ret = CTL_HA_STATUS_SUCCESS;
12541	for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12542		uint32_t cur_len;
12543		uint8_t *tmp_ptr;
12544
12545		rq->command = command;
12546		rq->context = io;
12547
12548		/*
12549		 * Both pointers should be aligned.  But it is possible
12550		 * that the allocation length is not.  They should both
12551		 * also have enough slack left over at the end, though,
12552		 * to round up to the next 8 byte boundary.
12553		 */
12554		cur_len = MIN(local_sglist[i].len - local_used,
12555			      remote_sglist[j].len - remote_used);
12556		rq->size = cur_len;
12557
12558		tmp_ptr = (uint8_t *)local_sglist[i].addr;
12559		tmp_ptr += local_used;
12560
12561#if 0
12562		/* Use physical addresses when talking to ISC hardware */
12563		if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12564			/* XXX KDM use busdma */
12565			rq->local = vtophys(tmp_ptr);
12566		} else
12567			rq->local = tmp_ptr;
12568#else
12569		KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12570		    ("HA does not support BUS_ADDR"));
12571		rq->local = tmp_ptr;
12572#endif
12573
12574		tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12575		tmp_ptr += remote_used;
12576		rq->remote = tmp_ptr;
12577
12578		rq->callback = NULL;
12579
12580		local_used += cur_len;
12581		if (local_used >= local_sglist[i].len) {
12582			i++;
12583			local_used = 0;
12584		}
12585
12586		remote_used += cur_len;
12587		if (remote_used >= remote_sglist[j].len) {
12588			j++;
12589			remote_used = 0;
12590		}
12591		total_used += cur_len;
12592
12593		if (total_used >= io->scsiio.kern_data_len)
12594			rq->callback = callback;
12595
12596#if 0
12597		printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12598		       (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12599		       rq->local, rq->remote, rq->size);
12600#endif
12601
12602		isc_ret = ctl_dt_single(rq);
12603		if (isc_ret > CTL_HA_STATUS_SUCCESS)
12604			break;
12605	}
12606	if (isc_ret != CTL_HA_STATUS_WAIT) {
12607		rq->ret = isc_ret;
12608		callback(rq);
12609	}
12610
12611	return (0);
12612}
12613
12614static void
12615ctl_datamove_remote_read(union ctl_io *io)
12616{
12617	int retval;
12618	int i;
12619
12620	/*
12621	 * This will send an error to the other controller in the case of a
12622	 * failure.
12623	 */
12624	retval = ctl_datamove_remote_sgl_setup(io);
12625	if (retval != 0)
12626		return;
12627
12628	retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12629					  ctl_datamove_remote_read_cb);
12630	if (retval != 0) {
12631		/*
12632		 * Make sure we free memory if there was an error..  The
12633		 * ctl_datamove_remote_xfer() function will send the
12634		 * datamove done message, or call the callback with an
12635		 * error if there is a problem.
12636		 */
12637		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12638			free(io->io_hdr.local_sglist[i].addr, M_CTL);
12639		free(io->io_hdr.remote_sglist, M_CTL);
12640		io->io_hdr.remote_sglist = NULL;
12641		io->io_hdr.local_sglist = NULL;
12642	}
12643
12644	return;
12645}
12646
12647/*
12648 * Process a datamove request from the other controller.  This is used for
12649 * XFER mode only, not SER_ONLY mode.  For writes, we DMA into local memory
12650 * first.  Once that is complete, the data gets DMAed into the remote
12651 * controller's memory.  For reads, we DMA from the remote controller's
12652 * memory into our memory first, and then move it out to the FETD.
12653 */
12654static void
12655ctl_datamove_remote(union ctl_io *io)
12656{
12657
12658	mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12659
12660	if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12661		ctl_failover_io(io, /*have_lock*/ 0);
12662		return;
12663	}
12664
12665	/*
12666	 * Note that we look for an aborted I/O here, but don't do some of
12667	 * the other checks that ctl_datamove() normally does.
12668	 * We don't need to run the datamove delay code, since that should
12669	 * have been done if need be on the other controller.
12670	 */
12671	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12672		printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12673		       io->scsiio.tag_num, io->io_hdr.nexus.initid,
12674		       io->io_hdr.nexus.targ_port,
12675		       io->io_hdr.nexus.targ_lun);
12676		io->io_hdr.port_status = 31338;
12677		ctl_send_datamove_done(io, /*have_lock*/ 0);
12678		return;
12679	}
12680
12681	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12682		ctl_datamove_remote_write(io);
12683	else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12684		ctl_datamove_remote_read(io);
12685	else {
12686		io->io_hdr.port_status = 31339;
12687		ctl_send_datamove_done(io, /*have_lock*/ 0);
12688	}
12689}
12690
12691static int
12692ctl_process_done(union ctl_io *io)
12693{
12694	struct ctl_lun *lun;
12695	struct ctl_softc *softc = control_softc;
12696	void (*fe_done)(union ctl_io *io);
12697	union ctl_ha_msg msg;
12698	uint32_t targ_port = io->io_hdr.nexus.targ_port;
12699
12700	CTL_DEBUG_PRINT(("ctl_process_done\n"));
12701
12702	if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0)
12703		fe_done = softc->ctl_ports[targ_port]->fe_done;
12704	else
12705		fe_done = NULL;
12706
12707#ifdef CTL_TIME_IO
12708	if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12709		char str[256];
12710		char path_str[64];
12711		struct sbuf sb;
12712
12713		ctl_scsi_path_string(io, path_str, sizeof(path_str));
12714		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12715
12716		sbuf_cat(&sb, path_str);
12717		switch (io->io_hdr.io_type) {
12718		case CTL_IO_SCSI:
12719			ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12720			sbuf_printf(&sb, "\n");
12721			sbuf_cat(&sb, path_str);
12722			sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12723				    io->scsiio.tag_num, io->scsiio.tag_type);
12724			break;
12725		case CTL_IO_TASK:
12726			sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12727				    "Tag Type: %d\n", io->taskio.task_action,
12728				    io->taskio.tag_num, io->taskio.tag_type);
12729			break;
12730		default:
12731			printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12732			panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12733			break;
12734		}
12735		sbuf_cat(&sb, path_str);
12736		sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12737			    (intmax_t)time_uptime - io->io_hdr.start_time);
12738		sbuf_finish(&sb);
12739		printf("%s", sbuf_data(&sb));
12740	}
12741#endif /* CTL_TIME_IO */
12742
12743	switch (io->io_hdr.io_type) {
12744	case CTL_IO_SCSI:
12745		break;
12746	case CTL_IO_TASK:
12747		if (ctl_debug & CTL_DEBUG_INFO)
12748			ctl_io_error_print(io, NULL);
12749		if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12750			ctl_free_io(io);
12751		else
12752			fe_done(io);
12753		return (CTL_RETVAL_COMPLETE);
12754	default:
12755		panic("ctl_process_done: invalid io type %d\n",
12756		      io->io_hdr.io_type);
12757		break; /* NOTREACHED */
12758	}
12759
12760	lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12761	if (lun == NULL) {
12762		CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12763				 io->io_hdr.nexus.targ_mapped_lun));
12764		goto bailout;
12765	}
12766
12767	mtx_lock(&lun->lun_lock);
12768
12769	/*
12770	 * Check to see if we have any errors to inject here.  We only
12771	 * inject errors for commands that don't already have errors set.
12772	 */
12773	if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
12774	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12775	    ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12776		ctl_inject_error(lun, io);
12777
12778	/*
12779	 * XXX KDM how do we treat commands that aren't completed
12780	 * successfully?
12781	 *
12782	 * XXX KDM should we also track I/O latency?
12783	 */
12784	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12785	    io->io_hdr.io_type == CTL_IO_SCSI) {
12786#ifdef CTL_TIME_IO
12787		struct bintime cur_bt;
12788#endif
12789		int type;
12790
12791		if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12792		    CTL_FLAG_DATA_IN)
12793			type = CTL_STATS_READ;
12794		else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12795		    CTL_FLAG_DATA_OUT)
12796			type = CTL_STATS_WRITE;
12797		else
12798			type = CTL_STATS_NO_IO;
12799
12800		lun->stats.ports[targ_port].bytes[type] +=
12801		    io->scsiio.kern_total_len;
12802		lun->stats.ports[targ_port].operations[type]++;
12803#ifdef CTL_TIME_IO
12804		bintime_add(&lun->stats.ports[targ_port].dma_time[type],
12805		   &io->io_hdr.dma_bt);
12806		lun->stats.ports[targ_port].num_dmas[type] +=
12807		    io->io_hdr.num_dmas;
12808		getbintime(&cur_bt);
12809		bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12810		bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
12811#endif
12812	}
12813
12814	/*
12815	 * Remove this from the OOA queue.
12816	 */
12817	TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12818#ifdef CTL_TIME_IO
12819	if (TAILQ_EMPTY(&lun->ooa_queue))
12820		lun->last_busy = getsbinuptime();
12821#endif
12822
12823	/*
12824	 * Run through the blocked queue on this LUN and see if anything
12825	 * has become unblocked, now that this transaction is done.
12826	 */
12827	ctl_check_blocked(lun);
12828
12829	/*
12830	 * If the LUN has been invalidated, free it if there is nothing
12831	 * left on its OOA queue.
12832	 */
12833	if ((lun->flags & CTL_LUN_INVALID)
12834	 && TAILQ_EMPTY(&lun->ooa_queue)) {
12835		mtx_unlock(&lun->lun_lock);
12836		mtx_lock(&softc->ctl_lock);
12837		ctl_free_lun(lun);
12838		mtx_unlock(&softc->ctl_lock);
12839	} else
12840		mtx_unlock(&lun->lun_lock);
12841
12842bailout:
12843
12844	/*
12845	 * If this command has been aborted, make sure we set the status
12846	 * properly.  The FETD is responsible for freeing the I/O and doing
12847	 * whatever it needs to do to clean up its state.
12848	 */
12849	if (io->io_hdr.flags & CTL_FLAG_ABORT)
12850		ctl_set_task_aborted(&io->scsiio);
12851
12852	/*
12853	 * If enabled, print command error status.
12854	 */
12855	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
12856	    (ctl_debug & CTL_DEBUG_INFO) != 0)
12857		ctl_io_error_print(io, NULL);
12858
12859	/*
12860	 * Tell the FETD or the other shelf controller we're done with this
12861	 * command.  Note that only SCSI commands get to this point.  Task
12862	 * management commands are completed above.
12863	 */
12864	if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
12865	    (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
12866		memset(&msg, 0, sizeof(msg));
12867		msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12868		msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12869		msg.hdr.nexus = io->io_hdr.nexus;
12870		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12871		    sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
12872		    M_WAITOK);
12873	}
12874	if ((softc->ha_mode == CTL_HA_MODE_XFER)
12875	 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12876		memset(&msg, 0, sizeof(msg));
12877		msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12878		msg.hdr.original_sc = io->io_hdr.original_sc;
12879		msg.hdr.nexus = io->io_hdr.nexus;
12880		msg.hdr.status = io->io_hdr.status;
12881		msg.scsi.scsi_status = io->scsiio.scsi_status;
12882		msg.scsi.tag_num = io->scsiio.tag_num;
12883		msg.scsi.tag_type = io->scsiio.tag_type;
12884		msg.scsi.sense_len = io->scsiio.sense_len;
12885		msg.scsi.sense_residual = io->scsiio.sense_residual;
12886		msg.scsi.residual = io->scsiio.residual;
12887		memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12888		       io->scsiio.sense_len);
12889		/*
12890		 * We copy this whether or not this is an I/O-related
12891		 * command.  Otherwise, we'd have to go and check to see
12892		 * whether it's a read/write command, and it really isn't
12893		 * worth it.
12894		 */
12895		memcpy(&msg.scsi.lbalen,
12896		       &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
12897		       sizeof(msg.scsi.lbalen));
12898
12899		ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12900		    sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12901		    msg.scsi.sense_len, M_WAITOK);
12902		ctl_free_io(io);
12903	} else
12904		fe_done(io);
12905
12906	return (CTL_RETVAL_COMPLETE);
12907}
12908
12909#ifdef CTL_WITH_CA
12910/*
12911 * Front end should call this if it doesn't do autosense.  When the request
12912 * sense comes back in from the initiator, we'll dequeue this and send it.
12913 */
12914int
12915ctl_queue_sense(union ctl_io *io)
12916{
12917	struct ctl_lun *lun;
12918	struct ctl_port *port;
12919	struct ctl_softc *softc;
12920	uint32_t initidx, targ_lun;
12921
12922	softc = control_softc;
12923
12924	CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
12925
12926	/*
12927	 * LUN lookup will likely move to the ctl_work_thread() once we
12928	 * have our new queueing infrastructure (that doesn't put things on
12929	 * a per-LUN queue initially).  That is so that we can handle
12930	 * things like an INQUIRY to a LUN that we don't have enabled.  We
12931	 * can't deal with that right now.
12932	 */
12933	mtx_lock(&softc->ctl_lock);
12934
12935	/*
12936	 * If we don't have a LUN for this, just toss the sense
12937	 * information.
12938	 */
12939	port = ctl_io_port(&ctsio->io_hdr);
12940	targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
12941	if ((targ_lun < CTL_MAX_LUNS)
12942	 && (softc->ctl_luns[targ_lun] != NULL))
12943		lun = softc->ctl_luns[targ_lun];
12944	else
12945		goto bailout;
12946
12947	initidx = ctl_get_initindex(&io->io_hdr.nexus);
12948
12949	mtx_lock(&lun->lun_lock);
12950	/*
12951	 * Already have CA set for this LUN...toss the sense information.
12952	 */
12953	if (ctl_is_set(lun->have_ca, initidx)) {
12954		mtx_unlock(&lun->lun_lock);
12955		goto bailout;
12956	}
12957
12958	memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
12959	       MIN(sizeof(lun->pending_sense[initidx]),
12960	       sizeof(io->scsiio.sense_data)));
12961	ctl_set_mask(lun->have_ca, initidx);
12962	mtx_unlock(&lun->lun_lock);
12963
12964bailout:
12965	mtx_unlock(&softc->ctl_lock);
12966
12967	ctl_free_io(io);
12968
12969	return (CTL_RETVAL_COMPLETE);
12970}
12971#endif
12972
12973/*
12974 * Primary command inlet from frontend ports.  All SCSI and task I/O
12975 * requests must go through this function.
12976 */
12977int
12978ctl_queue(union ctl_io *io)
12979{
12980	struct ctl_port *port;
12981
12982	CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
12983
12984#ifdef CTL_TIME_IO
12985	io->io_hdr.start_time = time_uptime;
12986	getbintime(&io->io_hdr.start_bt);
12987#endif /* CTL_TIME_IO */
12988
12989	/* Map FE-specific LUN ID into global one. */
12990	port = ctl_io_port(&io->io_hdr);
12991	io->io_hdr.nexus.targ_mapped_lun =
12992	    ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
12993
12994	switch (io->io_hdr.io_type) {
12995	case CTL_IO_SCSI:
12996	case CTL_IO_TASK:
12997		if (ctl_debug & CTL_DEBUG_CDB)
12998			ctl_io_print(io);
12999		ctl_enqueue_incoming(io);
13000		break;
13001	default:
13002		printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13003		return (EINVAL);
13004	}
13005
13006	return (CTL_RETVAL_COMPLETE);
13007}
13008
13009#ifdef CTL_IO_DELAY
13010static void
13011ctl_done_timer_wakeup(void *arg)
13012{
13013	union ctl_io *io;
13014
13015	io = (union ctl_io *)arg;
13016	ctl_done(io);
13017}
13018#endif /* CTL_IO_DELAY */
13019
13020void
13021ctl_done(union ctl_io *io)
13022{
13023
13024	/*
13025	 * Enable this to catch duplicate completion issues.
13026	 */
13027#if 0
13028	if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13029		printf("%s: type %d msg %d cdb %x iptl: "
13030		       "%u:%u:%u tag 0x%04x "
13031		       "flag %#x status %x\n",
13032			__func__,
13033			io->io_hdr.io_type,
13034			io->io_hdr.msg_type,
13035			io->scsiio.cdb[0],
13036			io->io_hdr.nexus.initid,
13037			io->io_hdr.nexus.targ_port,
13038			io->io_hdr.nexus.targ_lun,
13039			(io->io_hdr.io_type ==
13040			CTL_IO_TASK) ?
13041			io->taskio.tag_num :
13042			io->scsiio.tag_num,
13043		        io->io_hdr.flags,
13044			io->io_hdr.status);
13045	} else
13046		io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13047#endif
13048
13049	/*
13050	 * This is an internal copy of an I/O, and should not go through
13051	 * the normal done processing logic.
13052	 */
13053	if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13054		return;
13055
13056#ifdef CTL_IO_DELAY
13057	if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13058		struct ctl_lun *lun;
13059
13060		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13061
13062		io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13063	} else {
13064		struct ctl_lun *lun;
13065
13066		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13067
13068		if ((lun != NULL)
13069		 && (lun->delay_info.done_delay > 0)) {
13070
13071			callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13072			io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13073			callout_reset(&io->io_hdr.delay_callout,
13074				      lun->delay_info.done_delay * hz,
13075				      ctl_done_timer_wakeup, io);
13076			if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13077				lun->delay_info.done_delay = 0;
13078			return;
13079		}
13080	}
13081#endif /* CTL_IO_DELAY */
13082
13083	ctl_enqueue_done(io);
13084}
13085
13086static void
13087ctl_work_thread(void *arg)
13088{
13089	struct ctl_thread *thr = (struct ctl_thread *)arg;
13090	struct ctl_softc *softc = thr->ctl_softc;
13091	union ctl_io *io;
13092	int retval;
13093
13094	CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13095
13096	for (;;) {
13097		retval = 0;
13098
13099		/*
13100		 * We handle the queues in this order:
13101		 * - ISC
13102		 * - done queue (to free up resources, unblock other commands)
13103		 * - RtR queue
13104		 * - incoming queue
13105		 *
13106		 * If those queues are empty, we break out of the loop and
13107		 * go to sleep.
13108		 */
13109		mtx_lock(&thr->queue_lock);
13110		io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13111		if (io != NULL) {
13112			STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13113			mtx_unlock(&thr->queue_lock);
13114			ctl_handle_isc(io);
13115			continue;
13116		}
13117		io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13118		if (io != NULL) {
13119			STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13120			/* clear any blocked commands, call fe_done */
13121			mtx_unlock(&thr->queue_lock);
13122			retval = ctl_process_done(io);
13123			continue;
13124		}
13125		io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13126		if (io != NULL) {
13127			STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13128			mtx_unlock(&thr->queue_lock);
13129			if (io->io_hdr.io_type == CTL_IO_TASK)
13130				ctl_run_task(io);
13131			else
13132				ctl_scsiio_precheck(softc, &io->scsiio);
13133			continue;
13134		}
13135		io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13136		if (io != NULL) {
13137			STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13138			mtx_unlock(&thr->queue_lock);
13139			retval = ctl_scsiio(&io->scsiio);
13140			if (retval != CTL_RETVAL_COMPLETE)
13141				CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13142			continue;
13143		}
13144
13145		/* Sleep until we have something to do. */
13146		mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13147	}
13148}
13149
13150static void
13151ctl_lun_thread(void *arg)
13152{
13153	struct ctl_softc *softc = (struct ctl_softc *)arg;
13154	struct ctl_be_lun *be_lun;
13155	int retval;
13156
13157	CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13158
13159	for (;;) {
13160		retval = 0;
13161		mtx_lock(&softc->ctl_lock);
13162		be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13163		if (be_lun != NULL) {
13164			STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13165			mtx_unlock(&softc->ctl_lock);
13166			ctl_create_lun(be_lun);
13167			continue;
13168		}
13169
13170		/* Sleep until we have something to do. */
13171		mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13172		    PDROP | PRIBIO, "-", 0);
13173	}
13174}
13175
13176static void
13177ctl_thresh_thread(void *arg)
13178{
13179	struct ctl_softc *softc = (struct ctl_softc *)arg;
13180	struct ctl_lun *lun;
13181	struct ctl_be_lun *be_lun;
13182	struct scsi_da_rw_recovery_page *rwpage;
13183	struct ctl_logical_block_provisioning_page *page;
13184	const char *attr;
13185	union ctl_ha_msg msg;
13186	uint64_t thres, val;
13187	int i, e, set;
13188
13189	CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13190
13191	for (;;) {
13192		mtx_lock(&softc->ctl_lock);
13193		STAILQ_FOREACH(lun, &softc->lun_list, links) {
13194			be_lun = lun->be_lun;
13195			if ((lun->flags & CTL_LUN_DISABLED) ||
13196			    (lun->flags & CTL_LUN_OFFLINE) ||
13197			    lun->backend->lun_attr == NULL)
13198				continue;
13199			if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13200			    softc->ha_mode == CTL_HA_MODE_XFER)
13201				continue;
13202			rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13203			if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13204				continue;
13205			e = 0;
13206			page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13207			for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13208				if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13209					continue;
13210				thres = scsi_4btoul(page->descr[i].count);
13211				thres <<= CTL_LBP_EXPONENT;
13212				switch (page->descr[i].resource) {
13213				case 0x01:
13214					attr = "blocksavail";
13215					break;
13216				case 0x02:
13217					attr = "blocksused";
13218					break;
13219				case 0xf1:
13220					attr = "poolblocksavail";
13221					break;
13222				case 0xf2:
13223					attr = "poolblocksused";
13224					break;
13225				default:
13226					continue;
13227				}
13228				mtx_unlock(&softc->ctl_lock); // XXX
13229				val = lun->backend->lun_attr(
13230				    lun->be_lun->be_lun, attr);
13231				mtx_lock(&softc->ctl_lock);
13232				if (val == UINT64_MAX)
13233					continue;
13234				if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13235				    == SLBPPD_ARMING_INC)
13236					e |= (val >= thres);
13237				else
13238					e |= (val <= thres);
13239			}
13240			mtx_lock(&lun->lun_lock);
13241			if (e) {
13242				if (lun->lasttpt == 0 ||
13243				    time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13244					lun->lasttpt = time_uptime;
13245					ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13246					set = 1;
13247				} else
13248					set = 0;
13249			} else {
13250				lun->lasttpt = 0;
13251				ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13252				set = -1;
13253			}
13254			mtx_unlock(&lun->lun_lock);
13255			if (set != 0 &&
13256			    lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13257				/* Send msg to other side. */
13258				bzero(&msg.ua, sizeof(msg.ua));
13259				msg.hdr.msg_type = CTL_MSG_UA;
13260				msg.hdr.nexus.initid = -1;
13261				msg.hdr.nexus.targ_port = -1;
13262				msg.hdr.nexus.targ_lun = lun->lun;
13263				msg.hdr.nexus.targ_mapped_lun = lun->lun;
13264				msg.ua.ua_all = 1;
13265				msg.ua.ua_set = (set > 0);
13266				msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13267				mtx_unlock(&softc->ctl_lock); // XXX
13268				ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13269				    sizeof(msg.ua), M_WAITOK);
13270				mtx_lock(&softc->ctl_lock);
13271			}
13272		}
13273		mtx_unlock(&softc->ctl_lock);
13274		pause("-", CTL_LBP_PERIOD * hz);
13275	}
13276}
13277
13278static void
13279ctl_enqueue_incoming(union ctl_io *io)
13280{
13281	struct ctl_softc *softc = control_softc;
13282	struct ctl_thread *thr;
13283	u_int idx;
13284
13285	idx = (io->io_hdr.nexus.targ_port * 127 +
13286	       io->io_hdr.nexus.initid) % worker_threads;
13287	thr = &softc->threads[idx];
13288	mtx_lock(&thr->queue_lock);
13289	STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13290	mtx_unlock(&thr->queue_lock);
13291	wakeup(thr);
13292}
13293
13294static void
13295ctl_enqueue_rtr(union ctl_io *io)
13296{
13297	struct ctl_softc *softc = control_softc;
13298	struct ctl_thread *thr;
13299
13300	thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13301	mtx_lock(&thr->queue_lock);
13302	STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13303	mtx_unlock(&thr->queue_lock);
13304	wakeup(thr);
13305}
13306
13307static void
13308ctl_enqueue_done(union ctl_io *io)
13309{
13310	struct ctl_softc *softc = control_softc;
13311	struct ctl_thread *thr;
13312
13313	thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13314	mtx_lock(&thr->queue_lock);
13315	STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13316	mtx_unlock(&thr->queue_lock);
13317	wakeup(thr);
13318}
13319
13320static void
13321ctl_enqueue_isc(union ctl_io *io)
13322{
13323	struct ctl_softc *softc = control_softc;
13324	struct ctl_thread *thr;
13325
13326	thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13327	mtx_lock(&thr->queue_lock);
13328	STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13329	mtx_unlock(&thr->queue_lock);
13330	wakeup(thr);
13331}
13332
13333/*
13334 *  vim: ts=8
13335 */
13336