ctl.c revision 265492
1/*-
2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions, and the following disclaimer,
14 *    without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 *    substantially similar to the "NO WARRANTY" disclaimer below
17 *    ("Disclaimer") and any redistribution must be conditioned upon
18 *    including a substantially similar Disclaimer requirement for further
19 *    binary redistribution.
20 *
21 * NO WARRANTY
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
33 *
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
35 */
36/*
37 * CAM Target Layer, a SCSI device emulation subsystem.
38 *
39 * Author: Ken Merry <ken@FreeBSD.org>
40 */
41
42#define _CTL_C
43
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 265492 2014-05-07 06:22:51Z trasz $");
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/types.h>
51#include <sys/kthread.h>
52#include <sys/bio.h>
53#include <sys/fcntl.h>
54#include <sys/lock.h>
55#include <sys/module.h>
56#include <sys/mutex.h>
57#include <sys/condvar.h>
58#include <sys/malloc.h>
59#include <sys/conf.h>
60#include <sys/ioccom.h>
61#include <sys/queue.h>
62#include <sys/sbuf.h>
63#include <sys/smp.h>
64#include <sys/endian.h>
65#include <sys/sysctl.h>
66
67#include <cam/cam.h>
68#include <cam/scsi/scsi_all.h>
69#include <cam/scsi/scsi_da.h>
70#include <cam/ctl/ctl_io.h>
71#include <cam/ctl/ctl.h>
72#include <cam/ctl/ctl_frontend.h>
73#include <cam/ctl/ctl_frontend_internal.h>
74#include <cam/ctl/ctl_util.h>
75#include <cam/ctl/ctl_backend.h>
76#include <cam/ctl/ctl_ioctl.h>
77#include <cam/ctl/ctl_ha.h>
78#include <cam/ctl/ctl_private.h>
79#include <cam/ctl/ctl_debug.h>
80#include <cam/ctl/ctl_scsi_all.h>
81#include <cam/ctl/ctl_error.h>
82
83struct ctl_softc *control_softc = NULL;
84
85/*
86 * The default is to run with CTL_DONE_THREAD turned on.  Completed
87 * transactions are queued for processing by the CTL work thread.  When
88 * CTL_DONE_THREAD is not defined, completed transactions are processed in
89 * the caller's context.
90 */
91#define CTL_DONE_THREAD
92
93/*
94 * Use the serial number and device ID provided by the backend, rather than
95 * making up our own.
96 */
97#define CTL_USE_BACKEND_SN
98
99/*
100 * Size and alignment macros needed for Copan-specific HA hardware.  These
101 * can go away when the HA code is re-written, and uses busdma for any
102 * hardware.
103 */
104#define	CTL_ALIGN_8B(target, source, type)				\
105	if (((uint32_t)source & 0x7) != 0)				\
106		target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
107	else								\
108		target = (type)source;
109
110#define	CTL_SIZE_8B(target, size)					\
111	if ((size & 0x7) != 0)						\
112		target = size + (0x8 - (size & 0x7));			\
113	else								\
114		target = size;
115
116#define CTL_ALIGN_8B_MARGIN	16
117
118/*
119 * Template mode pages.
120 */
121
122/*
123 * Note that these are default values only.  The actual values will be
124 * filled in when the user does a mode sense.
125 */
126static struct copan_power_subpage power_page_default = {
127	/*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
128	/*subpage*/ PWR_SUBPAGE_CODE,
129	/*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
130			 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
131	/*page_version*/ PWR_VERSION,
132	/* total_luns */ 26,
133	/* max_active_luns*/ PWR_DFLT_MAX_LUNS,
134	/*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
135		      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
136		      0, 0, 0, 0, 0, 0}
137};
138
139static struct copan_power_subpage power_page_changeable = {
140	/*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
141	/*subpage*/ PWR_SUBPAGE_CODE,
142	/*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
143			 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
144	/*page_version*/ 0,
145	/* total_luns */ 0,
146	/* max_active_luns*/ 0,
147	/*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
148		      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
149		      0, 0, 0, 0, 0, 0}
150};
151
152static struct copan_aps_subpage aps_page_default = {
153	APS_PAGE_CODE | SMPH_SPF, //page_code
154	APS_SUBPAGE_CODE, //subpage
155	{(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
156	 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
157	APS_VERSION, //page_version
158	0, //lock_active
159	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161	0, 0, 0, 0, 0} //reserved
162};
163
164static struct copan_aps_subpage aps_page_changeable = {
165	APS_PAGE_CODE | SMPH_SPF, //page_code
166	APS_SUBPAGE_CODE, //subpage
167	{(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
168	 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
169	0, //page_version
170	0, //lock_active
171	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173	0, 0, 0, 0, 0} //reserved
174};
175
176static struct copan_debugconf_subpage debugconf_page_default = {
177	DBGCNF_PAGE_CODE | SMPH_SPF,	/* page_code */
178	DBGCNF_SUBPAGE_CODE,		/* subpage */
179	{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
180	 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
181	DBGCNF_VERSION,			/* page_version */
182	{CTL_TIME_IO_DEFAULT_SECS>>8,
183	 CTL_TIME_IO_DEFAULT_SECS>>0},	/* ctl_time_io_secs */
184};
185
186static struct copan_debugconf_subpage debugconf_page_changeable = {
187	DBGCNF_PAGE_CODE | SMPH_SPF,	/* page_code */
188	DBGCNF_SUBPAGE_CODE,		/* subpage */
189	{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
190	 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
191	0,				/* page_version */
192	{0xff,0xff},			/* ctl_time_io_secs */
193};
194
195static struct scsi_format_page format_page_default = {
196	/*page_code*/SMS_FORMAT_DEVICE_PAGE,
197	/*page_length*/sizeof(struct scsi_format_page) - 2,
198	/*tracks_per_zone*/ {0, 0},
199	/*alt_sectors_per_zone*/ {0, 0},
200	/*alt_tracks_per_zone*/ {0, 0},
201	/*alt_tracks_per_lun*/ {0, 0},
202	/*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
203			        CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
204	/*bytes_per_sector*/ {0, 0},
205	/*interleave*/ {0, 0},
206	/*track_skew*/ {0, 0},
207	/*cylinder_skew*/ {0, 0},
208	/*flags*/ SFP_HSEC,
209	/*reserved*/ {0, 0, 0}
210};
211
212static struct scsi_format_page format_page_changeable = {
213	/*page_code*/SMS_FORMAT_DEVICE_PAGE,
214	/*page_length*/sizeof(struct scsi_format_page) - 2,
215	/*tracks_per_zone*/ {0, 0},
216	/*alt_sectors_per_zone*/ {0, 0},
217	/*alt_tracks_per_zone*/ {0, 0},
218	/*alt_tracks_per_lun*/ {0, 0},
219	/*sectors_per_track*/ {0, 0},
220	/*bytes_per_sector*/ {0, 0},
221	/*interleave*/ {0, 0},
222	/*track_skew*/ {0, 0},
223	/*cylinder_skew*/ {0, 0},
224	/*flags*/ 0,
225	/*reserved*/ {0, 0, 0}
226};
227
228static struct scsi_rigid_disk_page rigid_disk_page_default = {
229	/*page_code*/SMS_RIGID_DISK_PAGE,
230	/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
231	/*cylinders*/ {0, 0, 0},
232	/*heads*/ CTL_DEFAULT_HEADS,
233	/*start_write_precomp*/ {0, 0, 0},
234	/*start_reduced_current*/ {0, 0, 0},
235	/*step_rate*/ {0, 0},
236	/*landing_zone_cylinder*/ {0, 0, 0},
237	/*rpl*/ SRDP_RPL_DISABLED,
238	/*rotational_offset*/ 0,
239	/*reserved1*/ 0,
240	/*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
241			   CTL_DEFAULT_ROTATION_RATE & 0xff},
242	/*reserved2*/ {0, 0}
243};
244
245static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
246	/*page_code*/SMS_RIGID_DISK_PAGE,
247	/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
248	/*cylinders*/ {0, 0, 0},
249	/*heads*/ 0,
250	/*start_write_precomp*/ {0, 0, 0},
251	/*start_reduced_current*/ {0, 0, 0},
252	/*step_rate*/ {0, 0},
253	/*landing_zone_cylinder*/ {0, 0, 0},
254	/*rpl*/ 0,
255	/*rotational_offset*/ 0,
256	/*reserved1*/ 0,
257	/*rotation_rate*/ {0, 0},
258	/*reserved2*/ {0, 0}
259};
260
261static struct scsi_caching_page caching_page_default = {
262	/*page_code*/SMS_CACHING_PAGE,
263	/*page_length*/sizeof(struct scsi_caching_page) - 2,
264	/*flags1*/ SCP_DISC | SCP_WCE,
265	/*ret_priority*/ 0,
266	/*disable_pf_transfer_len*/ {0xff, 0xff},
267	/*min_prefetch*/ {0, 0},
268	/*max_prefetch*/ {0xff, 0xff},
269	/*max_pf_ceiling*/ {0xff, 0xff},
270	/*flags2*/ 0,
271	/*cache_segments*/ 0,
272	/*cache_seg_size*/ {0, 0},
273	/*reserved*/ 0,
274	/*non_cache_seg_size*/ {0, 0, 0}
275};
276
277static struct scsi_caching_page caching_page_changeable = {
278	/*page_code*/SMS_CACHING_PAGE,
279	/*page_length*/sizeof(struct scsi_caching_page) - 2,
280	/*flags1*/ 0,
281	/*ret_priority*/ 0,
282	/*disable_pf_transfer_len*/ {0, 0},
283	/*min_prefetch*/ {0, 0},
284	/*max_prefetch*/ {0, 0},
285	/*max_pf_ceiling*/ {0, 0},
286	/*flags2*/ 0,
287	/*cache_segments*/ 0,
288	/*cache_seg_size*/ {0, 0},
289	/*reserved*/ 0,
290	/*non_cache_seg_size*/ {0, 0, 0}
291};
292
293static struct scsi_control_page control_page_default = {
294	/*page_code*/SMS_CONTROL_MODE_PAGE,
295	/*page_length*/sizeof(struct scsi_control_page) - 2,
296	/*rlec*/0,
297	/*queue_flags*/0,
298	/*eca_and_aen*/0,
299	/*reserved*/0,
300	/*aen_holdoff_period*/{0, 0}
301};
302
303static struct scsi_control_page control_page_changeable = {
304	/*page_code*/SMS_CONTROL_MODE_PAGE,
305	/*page_length*/sizeof(struct scsi_control_page) - 2,
306	/*rlec*/SCP_DSENSE,
307	/*queue_flags*/0,
308	/*eca_and_aen*/0,
309	/*reserved*/0,
310	/*aen_holdoff_period*/{0, 0}
311};
312
313
314/*
315 * XXX KDM move these into the softc.
316 */
317static int rcv_sync_msg;
318static int persis_offset;
319static uint8_t ctl_pause_rtr;
320static int     ctl_is_single = 1;
321static int     index_to_aps_page;
322
323SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
324static int worker_threads = 1;
325TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
326SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
327    &worker_threads, 1, "Number of worker threads");
328
329/*
330 * Serial number (0x80), device id (0x83), and supported pages (0x00)
331 */
332#define SCSI_EVPD_NUM_SUPPORTED_PAGES	3
333
334static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
335				  int param);
336static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
337static int ctl_init(void);
338void ctl_shutdown(void);
339static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
340static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
341static void ctl_ioctl_online(void *arg);
342static void ctl_ioctl_offline(void *arg);
343static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
344static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
345static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
346static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
347static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
348static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
349static int ctl_ioctl_submit_wait(union ctl_io *io);
350static void ctl_ioctl_datamove(union ctl_io *io);
351static void ctl_ioctl_done(union ctl_io *io);
352static void ctl_ioctl_hard_startstop_callback(void *arg,
353					      struct cfi_metatask *metatask);
354static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
355static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
356			      struct ctl_ooa *ooa_hdr,
357			      struct ctl_ooa_entry *kern_entries);
358static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
359		     struct thread *td);
360uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
361uint32_t ctl_port_idx(int port_num);
362#ifdef unused
363static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
364				   uint32_t targ_target, uint32_t targ_lun,
365				   int can_wait);
366static void ctl_kfree_io(union ctl_io *io);
367#endif /* unused */
368static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
369			 struct ctl_be_lun *be_lun, struct ctl_id target_id);
370static int ctl_free_lun(struct ctl_lun *lun);
371static void ctl_create_lun(struct ctl_be_lun *be_lun);
372/**
373static void ctl_failover_change_pages(struct ctl_softc *softc,
374				      struct ctl_scsiio *ctsio, int master);
375**/
376
377static int ctl_do_mode_select(union ctl_io *io);
378static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
379			   uint64_t res_key, uint64_t sa_res_key,
380			   uint8_t type, uint32_t residx,
381			   struct ctl_scsiio *ctsio,
382			   struct scsi_per_res_out *cdb,
383			   struct scsi_per_res_out_parms* param);
384static void ctl_pro_preempt_other(struct ctl_lun *lun,
385				  union ctl_ha_msg *msg);
386static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
387static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
388static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
389static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
390static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
391static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
392static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
393static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
394static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
395					 union ctl_io *ooa_io);
396static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
397				union ctl_io *starting_io);
398static int ctl_check_blocked(struct ctl_lun *lun);
399static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
400				struct ctl_lun *lun,
401				struct ctl_cmd_entry *entry,
402				struct ctl_scsiio *ctsio);
403//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
404static void ctl_failover(void);
405static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
406			       struct ctl_scsiio *ctsio);
407static int ctl_scsiio(struct ctl_scsiio *ctsio);
408
409static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
410static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
411			    ctl_ua_type ua_type);
412static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
413			 ctl_ua_type ua_type);
414static int ctl_abort_task(union ctl_io *io);
415static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
416#ifdef CTL_IO_DELAY
417static void ctl_datamove_timer_wakeup(void *arg);
418static void ctl_done_timer_wakeup(void *arg);
419#endif /* CTL_IO_DELAY */
420
421static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
422static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
423static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
424static void ctl_datamove_remote_write(union ctl_io *io);
425static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
426static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
427static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
428static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
429				    ctl_ha_dt_cb callback);
430static void ctl_datamove_remote_read(union ctl_io *io);
431static void ctl_datamove_remote(union ctl_io *io);
432static int ctl_process_done(union ctl_io *io, int have_lock);
433static void ctl_work_thread(void *arg);
434
435/*
436 * Load the serialization table.  This isn't very pretty, but is probably
437 * the easiest way to do it.
438 */
439#include "ctl_ser_table.c"
440
441/*
442 * We only need to define open, close and ioctl routines for this driver.
443 */
444static struct cdevsw ctl_cdevsw = {
445	.d_version =	D_VERSION,
446	.d_flags =	0,
447	.d_open =	ctl_open,
448	.d_close =	ctl_close,
449	.d_ioctl =	ctl_ioctl,
450	.d_name =	"ctl",
451};
452
453
454MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
455
456static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
457
458static moduledata_t ctl_moduledata = {
459	"ctl",
460	ctl_module_event_handler,
461	NULL
462};
463
464DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
465MODULE_VERSION(ctl, 1);
466
467static void
468ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
469			    union ctl_ha_msg *msg_info)
470{
471	struct ctl_scsiio *ctsio;
472
473	if (msg_info->hdr.original_sc == NULL) {
474		printf("%s: original_sc == NULL!\n", __func__);
475		/* XXX KDM now what? */
476		return;
477	}
478
479	ctsio = &msg_info->hdr.original_sc->scsiio;
480	ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
481	ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
482	ctsio->io_hdr.status = msg_info->hdr.status;
483	ctsio->scsi_status = msg_info->scsi.scsi_status;
484	ctsio->sense_len = msg_info->scsi.sense_len;
485	ctsio->sense_residual = msg_info->scsi.sense_residual;
486	ctsio->residual = msg_info->scsi.residual;
487	memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
488	       sizeof(ctsio->sense_data));
489	memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
490	       &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
491	STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
492	ctl_wakeup_thread();
493}
494
495static void
496ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
497				union ctl_ha_msg *msg_info)
498{
499	struct ctl_scsiio *ctsio;
500
501	if (msg_info->hdr.serializing_sc == NULL) {
502		printf("%s: serializing_sc == NULL!\n", __func__);
503		/* XXX KDM now what? */
504		return;
505	}
506
507	ctsio = &msg_info->hdr.serializing_sc->scsiio;
508#if 0
509	/*
510	 * Attempt to catch the situation where an I/O has
511	 * been freed, and we're using it again.
512	 */
513	if (ctsio->io_hdr.io_type == 0xff) {
514		union ctl_io *tmp_io;
515		tmp_io = (union ctl_io *)ctsio;
516		printf("%s: %p use after free!\n", __func__,
517		       ctsio);
518		printf("%s: type %d msg %d cdb %x iptl: "
519		       "%d:%d:%d:%d tag 0x%04x "
520		       "flag %#x status %x\n",
521			__func__,
522			tmp_io->io_hdr.io_type,
523			tmp_io->io_hdr.msg_type,
524			tmp_io->scsiio.cdb[0],
525			tmp_io->io_hdr.nexus.initid.id,
526			tmp_io->io_hdr.nexus.targ_port,
527			tmp_io->io_hdr.nexus.targ_target.id,
528			tmp_io->io_hdr.nexus.targ_lun,
529			(tmp_io->io_hdr.io_type ==
530			CTL_IO_TASK) ?
531			tmp_io->taskio.tag_num :
532			tmp_io->scsiio.tag_num,
533		        tmp_io->io_hdr.flags,
534			tmp_io->io_hdr.status);
535	}
536#endif
537	ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
538	STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
539	ctl_wakeup_thread();
540}
541
542/*
543 * ISC (Inter Shelf Communication) event handler.  Events from the HA
544 * subsystem come in here.
545 */
546static void
547ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
548{
549	struct ctl_softc *ctl_softc;
550	union ctl_io *io;
551	struct ctl_prio *presio;
552	ctl_ha_status isc_status;
553
554	ctl_softc = control_softc;
555	io = NULL;
556
557
558#if 0
559	printf("CTL: Isc Msg event %d\n", event);
560#endif
561	if (event == CTL_HA_EVT_MSG_RECV) {
562		union ctl_ha_msg msg_info;
563
564		isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
565					     sizeof(msg_info), /*wait*/ 0);
566#if 0
567		printf("CTL: msg_type %d\n", msg_info.msg_type);
568#endif
569		if (isc_status != 0) {
570			printf("Error receiving message, status = %d\n",
571			       isc_status);
572			return;
573		}
574		mtx_lock(&ctl_softc->ctl_lock);
575
576		switch (msg_info.hdr.msg_type) {
577		case CTL_MSG_SERIALIZE:
578#if 0
579			printf("Serialize\n");
580#endif
581			io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
582			if (io == NULL) {
583				printf("ctl_isc_event_handler: can't allocate "
584				       "ctl_io!\n");
585				/* Bad Juju */
586				/* Need to set busy and send msg back */
587				mtx_unlock(&ctl_softc->ctl_lock);
588				msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
589				msg_info.hdr.status = CTL_SCSI_ERROR;
590				msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
591				msg_info.scsi.sense_len = 0;
592			        if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
593				    sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
594				}
595				goto bailout;
596			}
597			ctl_zero_io(io);
598			// populate ctsio from msg_info
599			io->io_hdr.io_type = CTL_IO_SCSI;
600			io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
601			io->io_hdr.original_sc = msg_info.hdr.original_sc;
602#if 0
603			printf("pOrig %x\n", (int)msg_info.original_sc);
604#endif
605			io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
606					    CTL_FLAG_IO_ACTIVE;
607			/*
608			 * If we're in serialization-only mode, we don't
609			 * want to go through full done processing.  Thus
610			 * the COPY flag.
611			 *
612			 * XXX KDM add another flag that is more specific.
613			 */
614			if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
615				io->io_hdr.flags |= CTL_FLAG_INT_COPY;
616			io->io_hdr.nexus = msg_info.hdr.nexus;
617#if 0
618			printf("targ %d, port %d, iid %d, lun %d\n",
619			       io->io_hdr.nexus.targ_target.id,
620			       io->io_hdr.nexus.targ_port,
621			       io->io_hdr.nexus.initid.id,
622			       io->io_hdr.nexus.targ_lun);
623#endif
624			io->scsiio.tag_num = msg_info.scsi.tag_num;
625			io->scsiio.tag_type = msg_info.scsi.tag_type;
626			memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
627			       CTL_MAX_CDBLEN);
628			if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
629				struct ctl_cmd_entry *entry;
630				uint8_t opcode;
631
632				opcode = io->scsiio.cdb[0];
633				entry = &ctl_cmd_table[opcode];
634				io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
635				io->io_hdr.flags |=
636					entry->flags & CTL_FLAG_DATA_MASK;
637			}
638			STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
639					   &io->io_hdr, links);
640			ctl_wakeup_thread();
641			break;
642
643		/* Performed on the Originating SC, XFER mode only */
644		case CTL_MSG_DATAMOVE: {
645			struct ctl_sg_entry *sgl;
646			int i, j;
647
648			io = msg_info.hdr.original_sc;
649			if (io == NULL) {
650				printf("%s: original_sc == NULL!\n", __func__);
651				/* XXX KDM do something here */
652				break;
653			}
654			io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
655			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
656			/*
657			 * Keep track of this, we need to send it back over
658			 * when the datamove is complete.
659			 */
660			io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
661
662			if (msg_info.dt.sg_sequence == 0) {
663				/*
664				 * XXX KDM we use the preallocated S/G list
665				 * here, but we'll need to change this to
666				 * dynamic allocation if we need larger S/G
667				 * lists.
668				 */
669				if (msg_info.dt.kern_sg_entries >
670				    sizeof(io->io_hdr.remote_sglist) /
671				    sizeof(io->io_hdr.remote_sglist[0])) {
672					printf("%s: number of S/G entries "
673					    "needed %u > allocated num %zd\n",
674					    __func__,
675					    msg_info.dt.kern_sg_entries,
676					    sizeof(io->io_hdr.remote_sglist)/
677					    sizeof(io->io_hdr.remote_sglist[0]));
678
679					/*
680					 * XXX KDM send a message back to
681					 * the other side to shut down the
682					 * DMA.  The error will come back
683					 * through via the normal channel.
684					 */
685					break;
686				}
687				sgl = io->io_hdr.remote_sglist;
688				memset(sgl, 0,
689				       sizeof(io->io_hdr.remote_sglist));
690
691				io->scsiio.kern_data_ptr = (uint8_t *)sgl;
692
693				io->scsiio.kern_sg_entries =
694					msg_info.dt.kern_sg_entries;
695				io->scsiio.rem_sg_entries =
696					msg_info.dt.kern_sg_entries;
697				io->scsiio.kern_data_len =
698					msg_info.dt.kern_data_len;
699				io->scsiio.kern_total_len =
700					msg_info.dt.kern_total_len;
701				io->scsiio.kern_data_resid =
702					msg_info.dt.kern_data_resid;
703				io->scsiio.kern_rel_offset =
704					msg_info.dt.kern_rel_offset;
705				/*
706				 * Clear out per-DMA flags.
707				 */
708				io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
709				/*
710				 * Add per-DMA flags that are set for this
711				 * particular DMA request.
712				 */
713				io->io_hdr.flags |= msg_info.dt.flags &
714						    CTL_FLAG_RDMA_MASK;
715			} else
716				sgl = (struct ctl_sg_entry *)
717					io->scsiio.kern_data_ptr;
718
719			for (i = msg_info.dt.sent_sg_entries, j = 0;
720			     i < (msg_info.dt.sent_sg_entries +
721			     msg_info.dt.cur_sg_entries); i++, j++) {
722				sgl[i].addr = msg_info.dt.sg_list[j].addr;
723				sgl[i].len = msg_info.dt.sg_list[j].len;
724
725#if 0
726				printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
727				       __func__,
728				       msg_info.dt.sg_list[j].addr,
729				       msg_info.dt.sg_list[j].len,
730				       sgl[i].addr, sgl[i].len, j, i);
731#endif
732			}
733#if 0
734			memcpy(&sgl[msg_info.dt.sent_sg_entries],
735			       msg_info.dt.sg_list,
736			       sizeof(*sgl) * msg_info.dt.cur_sg_entries);
737#endif
738
739			/*
740			 * If this is the last piece of the I/O, we've got
741			 * the full S/G list.  Queue processing in the thread.
742			 * Otherwise wait for the next piece.
743			 */
744			if (msg_info.dt.sg_last != 0) {
745				STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
746						   &io->io_hdr, links);
747				ctl_wakeup_thread();
748			}
749			break;
750		}
751		/* Performed on the Serializing (primary) SC, XFER mode only */
752		case CTL_MSG_DATAMOVE_DONE: {
753			if (msg_info.hdr.serializing_sc == NULL) {
754				printf("%s: serializing_sc == NULL!\n",
755				       __func__);
756				/* XXX KDM now what? */
757				break;
758			}
759			/*
760			 * We grab the sense information here in case
761			 * there was a failure, so we can return status
762			 * back to the initiator.
763			 */
764			io = msg_info.hdr.serializing_sc;
765			io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
766			io->io_hdr.status = msg_info.hdr.status;
767			io->scsiio.scsi_status = msg_info.scsi.scsi_status;
768			io->scsiio.sense_len = msg_info.scsi.sense_len;
769			io->scsiio.sense_residual =msg_info.scsi.sense_residual;
770			io->io_hdr.port_status = msg_info.scsi.fetd_status;
771			io->scsiio.residual = msg_info.scsi.residual;
772			memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
773			       sizeof(io->scsiio.sense_data));
774
775			STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
776					   &io->io_hdr, links);
777			ctl_wakeup_thread();
778			break;
779		}
780
781		/* Preformed on Originating SC, SER_ONLY mode */
782		case CTL_MSG_R2R:
783			io = msg_info.hdr.original_sc;
784			if (io == NULL) {
785				printf("%s: Major Bummer\n", __func__);
786				mtx_unlock(&ctl_softc->ctl_lock);
787				return;
788			} else {
789#if 0
790				printf("pOrig %x\n",(int) ctsio);
791#endif
792			}
793			io->io_hdr.msg_type = CTL_MSG_R2R;
794			io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
795			STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
796					   &io->io_hdr, links);
797			ctl_wakeup_thread();
798			break;
799
800		/*
801		 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
802		 * mode.
803		 * Performed on the Originating (i.e. secondary) SC in XFER
804		 * mode
805		 */
806		case CTL_MSG_FINISH_IO:
807			if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
808				ctl_isc_handler_finish_xfer(ctl_softc,
809							    &msg_info);
810			else
811				ctl_isc_handler_finish_ser_only(ctl_softc,
812								&msg_info);
813			break;
814
815		/* Preformed on Originating SC */
816		case CTL_MSG_BAD_JUJU:
817			io = msg_info.hdr.original_sc;
818			if (io == NULL) {
819				printf("%s: Bad JUJU!, original_sc is NULL!\n",
820				       __func__);
821				break;
822			}
823			ctl_copy_sense_data(&msg_info, io);
824			/*
825			 * IO should have already been cleaned up on other
826			 * SC so clear this flag so we won't send a message
827			 * back to finish the IO there.
828			 */
829			io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
830			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
831
832			/* io = msg_info.hdr.serializing_sc; */
833			io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
834		        STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
835					   &io->io_hdr, links);
836			ctl_wakeup_thread();
837			break;
838
839		/* Handle resets sent from the other side */
840		case CTL_MSG_MANAGE_TASKS: {
841			struct ctl_taskio *taskio;
842			taskio = (struct ctl_taskio *)ctl_alloc_io(
843				(void *)ctl_softc->othersc_pool);
844			if (taskio == NULL) {
845				printf("ctl_isc_event_handler: can't allocate "
846				       "ctl_io!\n");
847				/* Bad Juju */
848				/* should I just call the proper reset func
849				   here??? */
850				mtx_unlock(&ctl_softc->ctl_lock);
851				goto bailout;
852			}
853			ctl_zero_io((union ctl_io *)taskio);
854			taskio->io_hdr.io_type = CTL_IO_TASK;
855			taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
856			taskio->io_hdr.nexus = msg_info.hdr.nexus;
857			taskio->task_action = msg_info.task.task_action;
858			taskio->tag_num = msg_info.task.tag_num;
859			taskio->tag_type = msg_info.task.tag_type;
860#ifdef CTL_TIME_IO
861			taskio->io_hdr.start_time = time_uptime;
862			getbintime(&taskio->io_hdr.start_bt);
863#if 0
864			cs_prof_gettime(&taskio->io_hdr.start_ticks);
865#endif
866#endif /* CTL_TIME_IO */
867		        STAILQ_INSERT_TAIL(&ctl_softc->task_queue,
868					   &taskio->io_hdr, links);
869			ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
870			ctl_wakeup_thread();
871			break;
872		}
873		/* Persistent Reserve action which needs attention */
874		case CTL_MSG_PERS_ACTION:
875			presio = (struct ctl_prio *)ctl_alloc_io(
876				(void *)ctl_softc->othersc_pool);
877			if (presio == NULL) {
878				printf("ctl_isc_event_handler: can't allocate "
879				       "ctl_io!\n");
880				/* Bad Juju */
881				/* Need to set busy and send msg back */
882				mtx_unlock(&ctl_softc->ctl_lock);
883				goto bailout;
884			}
885			ctl_zero_io((union ctl_io *)presio);
886			presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
887			presio->pr_msg = msg_info.pr;
888		        STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
889					   &presio->io_hdr, links);
890			ctl_wakeup_thread();
891			break;
892		case CTL_MSG_SYNC_FE:
893			rcv_sync_msg = 1;
894			break;
895		case CTL_MSG_APS_LOCK: {
896			// It's quicker to execute this then to
897			// queue it.
898			struct ctl_lun *lun;
899			struct ctl_page_index *page_index;
900			struct copan_aps_subpage *current_sp;
901			uint32_t targ_lun;
902
903			targ_lun = msg_info.hdr.nexus.targ_lun;
904			if (msg_info.hdr.nexus.lun_map_fn != NULL)
905				targ_lun = msg_info.hdr.nexus.lun_map_fn(msg_info.hdr.nexus.lun_map_arg, targ_lun);
906
907			lun = ctl_softc->ctl_luns[targ_lun];
908			page_index = &lun->mode_pages.index[index_to_aps_page];
909			current_sp = (struct copan_aps_subpage *)
910				     (page_index->page_data +
911				     (page_index->page_len * CTL_PAGE_CURRENT));
912
913			current_sp->lock_active = msg_info.aps.lock_flag;
914		        break;
915		}
916		default:
917		        printf("How did I get here?\n");
918		}
919		mtx_unlock(&ctl_softc->ctl_lock);
920	} else if (event == CTL_HA_EVT_MSG_SENT) {
921		if (param != CTL_HA_STATUS_SUCCESS) {
922			printf("Bad status from ctl_ha_msg_send status %d\n",
923			       param);
924		}
925		return;
926	} else if (event == CTL_HA_EVT_DISCONNECT) {
927		printf("CTL: Got a disconnect from Isc\n");
928		return;
929	} else {
930		printf("ctl_isc_event_handler: Unknown event %d\n", event);
931		return;
932	}
933
934bailout:
935	return;
936}
937
938static void
939ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
940{
941	struct scsi_sense_data *sense;
942
943	sense = &dest->scsiio.sense_data;
944	bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
945	dest->scsiio.scsi_status = src->scsi.scsi_status;
946	dest->scsiio.sense_len = src->scsi.sense_len;
947	dest->io_hdr.status = src->hdr.status;
948}
949
950static int
951ctl_init(void)
952{
953	struct ctl_softc *softc;
954	struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
955	struct ctl_frontend *fe;
956	struct ctl_lun *lun;
957        uint8_t sc_id =0;
958	int i, error, retval;
959	//int isc_retval;
960
961	retval = 0;
962	ctl_pause_rtr = 0;
963        rcv_sync_msg = 0;
964
965	control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
966			       M_WAITOK | M_ZERO);
967	softc = control_softc;
968
969	softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
970			      "cam/ctl");
971
972	softc->dev->si_drv1 = softc;
973
974	/*
975	 * By default, return a "bad LUN" peripheral qualifier for unknown
976	 * LUNs.  The user can override this default using the tunable or
977	 * sysctl.  See the comment in ctl_inquiry_std() for more details.
978	 */
979	softc->inquiry_pq_no_lun = 1;
980	TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
981			  &softc->inquiry_pq_no_lun);
982	sysctl_ctx_init(&softc->sysctl_ctx);
983	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
984		SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
985		CTLFLAG_RD, 0, "CAM Target Layer");
986
987	if (softc->sysctl_tree == NULL) {
988		printf("%s: unable to allocate sysctl tree\n", __func__);
989		destroy_dev(softc->dev);
990		free(control_softc, M_DEVBUF);
991		control_softc = NULL;
992		return (ENOMEM);
993	}
994
995	SYSCTL_ADD_INT(&softc->sysctl_ctx,
996		       SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
997		       "inquiry_pq_no_lun", CTLFLAG_RW,
998		       &softc->inquiry_pq_no_lun, 0,
999		       "Report no lun possible for invalid LUNs");
1000
1001	mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1002	mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
1003	softc->open_count = 0;
1004
1005	/*
1006	 * Default to actually sending a SYNCHRONIZE CACHE command down to
1007	 * the drive.
1008	 */
1009	softc->flags = CTL_FLAG_REAL_SYNC;
1010
1011	/*
1012	 * In Copan's HA scheme, the "master" and "slave" roles are
1013	 * figured out through the slot the controller is in.  Although it
1014	 * is an active/active system, someone has to be in charge.
1015 	 */
1016#ifdef NEEDTOPORT
1017        scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1018#endif
1019
1020        if (sc_id == 0) {
1021		softc->flags |= CTL_FLAG_MASTER_SHELF;
1022		persis_offset = 0;
1023	} else
1024		persis_offset = CTL_MAX_INITIATORS;
1025
1026	/*
1027	 * XXX KDM need to figure out where we want to get our target ID
1028	 * and WWID.  Is it different on each port?
1029	 */
1030	softc->target.id = 0;
1031	softc->target.wwid[0] = 0x12345678;
1032	softc->target.wwid[1] = 0x87654321;
1033	STAILQ_INIT(&softc->lun_list);
1034	STAILQ_INIT(&softc->pending_lun_queue);
1035	STAILQ_INIT(&softc->task_queue);
1036	STAILQ_INIT(&softc->incoming_queue);
1037	STAILQ_INIT(&softc->rtr_queue);
1038	STAILQ_INIT(&softc->done_queue);
1039	STAILQ_INIT(&softc->isc_queue);
1040	STAILQ_INIT(&softc->fe_list);
1041	STAILQ_INIT(&softc->be_list);
1042	STAILQ_INIT(&softc->io_pools);
1043
1044	lun = &softc->lun;
1045
1046	/*
1047	 * We don't bother calling these with ctl_lock held here, because,
1048	 * in theory, no one else can try to do anything while we're in our
1049	 * module init routine.
1050	 */
1051	if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1052			    &internal_pool)!= 0){
1053		printf("ctl: can't allocate %d entry internal pool, "
1054		       "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1055		return (ENOMEM);
1056	}
1057
1058	if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1059			    CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1060		printf("ctl: can't allocate %d entry emergency pool, "
1061		       "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1062		ctl_pool_free(internal_pool);
1063		return (ENOMEM);
1064	}
1065
1066	if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1067	                    &other_pool) != 0)
1068	{
1069		printf("ctl: can't allocate %d entry other SC pool, "
1070		       "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1071		ctl_pool_free(internal_pool);
1072		ctl_pool_free(emergency_pool);
1073		return (ENOMEM);
1074	}
1075
1076	softc->internal_pool = internal_pool;
1077	softc->emergency_pool = emergency_pool;
1078	softc->othersc_pool = other_pool;
1079
1080	/*
1081	 * We used to allocate a processor LUN here.  The new scheme is to
1082	 * just let the user allocate LUNs as he sees fit.
1083	 */
1084#if 0
1085	mtx_lock(&softc->ctl_lock);
1086	ctl_alloc_lun(softc, lun, /*be_lun*/NULL, /*target*/softc->target);
1087	mtx_unlock(&softc->ctl_lock);
1088#endif
1089
1090	if (worker_threads > MAXCPU || worker_threads == 0) {
1091		printf("invalid kern.cam.ctl.worker_threads value; "
1092		    "setting to 1");
1093		worker_threads = 1;
1094	} else if (worker_threads < 0) {
1095		if (mp_ncpus > 2) {
1096			/*
1097			 * Using more than two worker threads actually hurts
1098			 * performance due to lock contention.
1099			 */
1100			worker_threads = 2;
1101		} else {
1102			worker_threads = 1;
1103		}
1104	}
1105
1106	for (i = 0; i < worker_threads; i++) {
1107		error = kproc_create(ctl_work_thread, softc, &softc->work_thread, 0, 0,
1108				"ctl_thrd%d", i);
1109		if (error != 0) {
1110			printf("error creating CTL work thread!\n");
1111			mtx_lock(&softc->ctl_lock);
1112			ctl_free_lun(lun);
1113			mtx_unlock(&softc->ctl_lock);
1114			ctl_pool_free(internal_pool);
1115			ctl_pool_free(emergency_pool);
1116			ctl_pool_free(other_pool);
1117			return (error);
1118		}
1119	}
1120	if (bootverbose)
1121		printf("ctl: CAM Target Layer loaded\n");
1122
1123	/*
1124	 * Initialize the initiator and portname mappings
1125	 */
1126	memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1127
1128	/*
1129	 * Initialize the ioctl front end.
1130	 */
1131	fe = &softc->ioctl_info.fe;
1132	sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1133	fe->port_type = CTL_PORT_IOCTL;
1134	fe->num_requested_ctl_io = 100;
1135	fe->port_name = softc->ioctl_info.port_name;
1136	fe->port_online = ctl_ioctl_online;
1137	fe->port_offline = ctl_ioctl_offline;
1138	fe->onoff_arg = &softc->ioctl_info;
1139	fe->targ_enable = ctl_ioctl_targ_enable;
1140	fe->targ_disable = ctl_ioctl_targ_disable;
1141	fe->lun_enable = ctl_ioctl_lun_enable;
1142	fe->lun_disable = ctl_ioctl_lun_disable;
1143	fe->targ_lun_arg = &softc->ioctl_info;
1144	fe->fe_datamove = ctl_ioctl_datamove;
1145	fe->fe_done = ctl_ioctl_done;
1146	fe->max_targets = 15;
1147	fe->max_target_id = 15;
1148
1149	if (ctl_frontend_register(&softc->ioctl_info.fe,
1150	                  (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1151		printf("ctl: ioctl front end registration failed, will "
1152		       "continue anyway\n");
1153	}
1154
1155#ifdef CTL_IO_DELAY
1156	if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1157		printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1158		       sizeof(struct callout), CTL_TIMER_BYTES);
1159		return (EINVAL);
1160	}
1161#endif /* CTL_IO_DELAY */
1162
1163	return (0);
1164}
1165
1166void
1167ctl_shutdown(void)
1168{
1169	struct ctl_softc *softc;
1170	struct ctl_lun *lun, *next_lun;
1171	struct ctl_io_pool *pool;
1172
1173	softc = (struct ctl_softc *)control_softc;
1174
1175	if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1176		printf("ctl: ioctl front end deregistration failed\n");
1177
1178	mtx_lock(&softc->ctl_lock);
1179
1180	/*
1181	 * Free up each LUN.
1182	 */
1183	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1184		next_lun = STAILQ_NEXT(lun, links);
1185		ctl_free_lun(lun);
1186	}
1187
1188	mtx_unlock(&softc->ctl_lock);
1189
1190	/*
1191	 * This will rip the rug out from under any FETDs or anyone else
1192	 * that has a pool allocated.  Since we increment our module
1193	 * refcount any time someone outside the main CTL module allocates
1194	 * a pool, we shouldn't have any problems here.  The user won't be
1195	 * able to unload the CTL module until client modules have
1196	 * successfully unloaded.
1197	 */
1198	while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1199		ctl_pool_free(pool);
1200
1201#if 0
1202	ctl_shutdown_thread(softc->work_thread);
1203#endif
1204
1205	mtx_destroy(&softc->pool_lock);
1206	mtx_destroy(&softc->ctl_lock);
1207
1208	destroy_dev(softc->dev);
1209
1210	sysctl_ctx_free(&softc->sysctl_ctx);
1211
1212	free(control_softc, M_DEVBUF);
1213	control_softc = NULL;
1214
1215	if (bootverbose)
1216		printf("ctl: CAM Target Layer unloaded\n");
1217}
1218
1219static int
1220ctl_module_event_handler(module_t mod, int what, void *arg)
1221{
1222
1223	switch (what) {
1224	case MOD_LOAD:
1225		return (ctl_init());
1226	case MOD_UNLOAD:
1227		return (EBUSY);
1228	default:
1229		return (EOPNOTSUPP);
1230	}
1231}
1232
1233/*
1234 * XXX KDM should we do some access checks here?  Bump a reference count to
1235 * prevent a CTL module from being unloaded while someone has it open?
1236 */
1237static int
1238ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1239{
1240	return (0);
1241}
1242
1243static int
1244ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1245{
1246	return (0);
1247}
1248
1249int
1250ctl_port_enable(ctl_port_type port_type)
1251{
1252	struct ctl_softc *softc;
1253	struct ctl_frontend *fe;
1254
1255	if (ctl_is_single == 0) {
1256		union ctl_ha_msg msg_info;
1257		int isc_retval;
1258
1259#if 0
1260		printf("%s: HA mode, synchronizing frontend enable\n",
1261		        __func__);
1262#endif
1263		msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1264	        if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1265		        sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1266			printf("Sync msg send error retval %d\n", isc_retval);
1267		}
1268		if (!rcv_sync_msg) {
1269			isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1270			        sizeof(msg_info), 1);
1271		}
1272#if 0
1273        	printf("CTL:Frontend Enable\n");
1274	} else {
1275		printf("%s: single mode, skipping frontend synchronization\n",
1276		        __func__);
1277#endif
1278	}
1279
1280	softc = control_softc;
1281
1282	STAILQ_FOREACH(fe, &softc->fe_list, links) {
1283		if (port_type & fe->port_type)
1284		{
1285#if 0
1286			printf("port %d\n", fe->targ_port);
1287#endif
1288			ctl_frontend_online(fe);
1289		}
1290	}
1291
1292	return (0);
1293}
1294
1295int
1296ctl_port_disable(ctl_port_type port_type)
1297{
1298	struct ctl_softc *softc;
1299	struct ctl_frontend *fe;
1300
1301	softc = control_softc;
1302
1303	STAILQ_FOREACH(fe, &softc->fe_list, links) {
1304		if (port_type & fe->port_type)
1305			ctl_frontend_offline(fe);
1306	}
1307
1308	return (0);
1309}
1310
1311/*
1312 * Returns 0 for success, 1 for failure.
1313 * Currently the only failure mode is if there aren't enough entries
1314 * allocated.  So, in case of a failure, look at num_entries_dropped,
1315 * reallocate and try again.
1316 */
1317int
1318ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1319	      int *num_entries_filled, int *num_entries_dropped,
1320	      ctl_port_type port_type, int no_virtual)
1321{
1322	struct ctl_softc *softc;
1323	struct ctl_frontend *fe;
1324	int entries_dropped, entries_filled;
1325	int retval;
1326	int i;
1327
1328	softc = control_softc;
1329
1330	retval = 0;
1331	entries_filled = 0;
1332	entries_dropped = 0;
1333
1334	i = 0;
1335	mtx_lock(&softc->ctl_lock);
1336	STAILQ_FOREACH(fe, &softc->fe_list, links) {
1337		struct ctl_port_entry *entry;
1338
1339		if ((fe->port_type & port_type) == 0)
1340			continue;
1341
1342		if ((no_virtual != 0)
1343		 && (fe->virtual_port != 0))
1344			continue;
1345
1346		if (entries_filled >= num_entries_alloced) {
1347			entries_dropped++;
1348			continue;
1349		}
1350		entry = &entries[i];
1351
1352		entry->port_type = fe->port_type;
1353		strlcpy(entry->port_name, fe->port_name,
1354			sizeof(entry->port_name));
1355		entry->physical_port = fe->physical_port;
1356		entry->virtual_port = fe->virtual_port;
1357		entry->wwnn = fe->wwnn;
1358		entry->wwpn = fe->wwpn;
1359
1360		i++;
1361		entries_filled++;
1362	}
1363
1364	mtx_unlock(&softc->ctl_lock);
1365
1366	if (entries_dropped > 0)
1367		retval = 1;
1368
1369	*num_entries_dropped = entries_dropped;
1370	*num_entries_filled = entries_filled;
1371
1372	return (retval);
1373}
1374
1375static void
1376ctl_ioctl_online(void *arg)
1377{
1378	struct ctl_ioctl_info *ioctl_info;
1379
1380	ioctl_info = (struct ctl_ioctl_info *)arg;
1381
1382	ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1383}
1384
1385static void
1386ctl_ioctl_offline(void *arg)
1387{
1388	struct ctl_ioctl_info *ioctl_info;
1389
1390	ioctl_info = (struct ctl_ioctl_info *)arg;
1391
1392	ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1393}
1394
1395/*
1396 * Remove an initiator by port number and initiator ID.
1397 * Returns 0 for success, 1 for failure.
1398 */
1399int
1400ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1401{
1402	struct ctl_softc *softc;
1403
1404	softc = control_softc;
1405
1406	mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1407
1408	if ((targ_port < 0)
1409	 || (targ_port > CTL_MAX_PORTS)) {
1410		printf("%s: invalid port number %d\n", __func__, targ_port);
1411		return (1);
1412	}
1413	if (iid > CTL_MAX_INIT_PER_PORT) {
1414		printf("%s: initiator ID %u > maximun %u!\n",
1415		       __func__, iid, CTL_MAX_INIT_PER_PORT);
1416		return (1);
1417	}
1418
1419	mtx_lock(&softc->ctl_lock);
1420
1421	softc->wwpn_iid[targ_port][iid].in_use = 0;
1422
1423	mtx_unlock(&softc->ctl_lock);
1424
1425	return (0);
1426}
1427
1428/*
1429 * Add an initiator to the initiator map.
1430 * Returns 0 for success, 1 for failure.
1431 */
1432int
1433ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1434{
1435	struct ctl_softc *softc;
1436	int retval;
1437
1438	softc = control_softc;
1439
1440	mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1441
1442	retval = 0;
1443
1444	if ((targ_port < 0)
1445	 || (targ_port > CTL_MAX_PORTS)) {
1446		printf("%s: invalid port number %d\n", __func__, targ_port);
1447		return (1);
1448	}
1449	if (iid > CTL_MAX_INIT_PER_PORT) {
1450		printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1451		       __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1452		return (1);
1453	}
1454
1455	mtx_lock(&softc->ctl_lock);
1456
1457	if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1458		/*
1459		 * We don't treat this as an error.
1460		 */
1461		if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1462			printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1463			       __func__, targ_port, iid, (uintmax_t)wwpn);
1464			goto bailout;
1465		}
1466
1467		/*
1468		 * This is an error, but what do we do about it?  The
1469		 * driver is telling us we have a new WWPN for this
1470		 * initiator ID, so we pretty much need to use it.
1471		 */
1472		printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1473		       "still at that address\n", __func__, targ_port, iid,
1474		       (uintmax_t)wwpn,
1475		       (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1476
1477		/*
1478		 * XXX KDM clear have_ca and ua_pending on each LUN for
1479		 * this initiator.
1480		 */
1481	}
1482	softc->wwpn_iid[targ_port][iid].in_use = 1;
1483	softc->wwpn_iid[targ_port][iid].iid = iid;
1484	softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1485	softc->wwpn_iid[targ_port][iid].port = targ_port;
1486
1487bailout:
1488
1489	mtx_unlock(&softc->ctl_lock);
1490
1491	return (retval);
1492}
1493
1494/*
1495 * XXX KDM should we pretend to do something in the target/lun
1496 * enable/disable functions?
1497 */
1498static int
1499ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1500{
1501	return (0);
1502}
1503
1504static int
1505ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1506{
1507	return (0);
1508}
1509
1510static int
1511ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1512{
1513	return (0);
1514}
1515
1516static int
1517ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1518{
1519	return (0);
1520}
1521
1522/*
1523 * Data movement routine for the CTL ioctl frontend port.
1524 */
1525static int
1526ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1527{
1528	struct ctl_sg_entry *ext_sglist, *kern_sglist;
1529	struct ctl_sg_entry ext_entry, kern_entry;
1530	int ext_sglen, ext_sg_entries, kern_sg_entries;
1531	int ext_sg_start, ext_offset;
1532	int len_to_copy, len_copied;
1533	int kern_watermark, ext_watermark;
1534	int ext_sglist_malloced;
1535	int i, j;
1536
1537	ext_sglist_malloced = 0;
1538	ext_sg_start = 0;
1539	ext_offset = 0;
1540
1541	CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1542
1543	/*
1544	 * If this flag is set, fake the data transfer.
1545	 */
1546	if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1547		ctsio->ext_data_filled = ctsio->ext_data_len;
1548		goto bailout;
1549	}
1550
1551	/*
1552	 * To simplify things here, if we have a single buffer, stick it in
1553	 * a S/G entry and just make it a single entry S/G list.
1554	 */
1555	if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1556		int len_seen;
1557
1558		ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1559
1560		ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1561							   M_WAITOK);
1562		ext_sglist_malloced = 1;
1563		if (copyin(ctsio->ext_data_ptr, ext_sglist,
1564				   ext_sglen) != 0) {
1565			ctl_set_internal_failure(ctsio,
1566						 /*sks_valid*/ 0,
1567						 /*retry_count*/ 0);
1568			goto bailout;
1569		}
1570		ext_sg_entries = ctsio->ext_sg_entries;
1571		len_seen = 0;
1572		for (i = 0; i < ext_sg_entries; i++) {
1573			if ((len_seen + ext_sglist[i].len) >=
1574			     ctsio->ext_data_filled) {
1575				ext_sg_start = i;
1576				ext_offset = ctsio->ext_data_filled - len_seen;
1577				break;
1578			}
1579			len_seen += ext_sglist[i].len;
1580		}
1581	} else {
1582		ext_sglist = &ext_entry;
1583		ext_sglist->addr = ctsio->ext_data_ptr;
1584		ext_sglist->len = ctsio->ext_data_len;
1585		ext_sg_entries = 1;
1586		ext_sg_start = 0;
1587		ext_offset = ctsio->ext_data_filled;
1588	}
1589
1590	if (ctsio->kern_sg_entries > 0) {
1591		kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1592		kern_sg_entries = ctsio->kern_sg_entries;
1593	} else {
1594		kern_sglist = &kern_entry;
1595		kern_sglist->addr = ctsio->kern_data_ptr;
1596		kern_sglist->len = ctsio->kern_data_len;
1597		kern_sg_entries = 1;
1598	}
1599
1600
1601	kern_watermark = 0;
1602	ext_watermark = ext_offset;
1603	len_copied = 0;
1604	for (i = ext_sg_start, j = 0;
1605	     i < ext_sg_entries && j < kern_sg_entries;) {
1606		uint8_t *ext_ptr, *kern_ptr;
1607
1608		len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1609				      kern_sglist[j].len - kern_watermark);
1610
1611		ext_ptr = (uint8_t *)ext_sglist[i].addr;
1612		ext_ptr = ext_ptr + ext_watermark;
1613		if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1614			/*
1615			 * XXX KDM fix this!
1616			 */
1617			panic("need to implement bus address support");
1618#if 0
1619			kern_ptr = bus_to_virt(kern_sglist[j].addr);
1620#endif
1621		} else
1622			kern_ptr = (uint8_t *)kern_sglist[j].addr;
1623		kern_ptr = kern_ptr + kern_watermark;
1624
1625		kern_watermark += len_to_copy;
1626		ext_watermark += len_to_copy;
1627
1628		if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1629		     CTL_FLAG_DATA_IN) {
1630			CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1631					 "bytes to user\n", len_to_copy));
1632			CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1633					 "to %p\n", kern_ptr, ext_ptr));
1634			if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1635				ctl_set_internal_failure(ctsio,
1636							 /*sks_valid*/ 0,
1637							 /*retry_count*/ 0);
1638				goto bailout;
1639			}
1640		} else {
1641			CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1642					 "bytes from user\n", len_to_copy));
1643			CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1644					 "to %p\n", ext_ptr, kern_ptr));
1645			if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1646				ctl_set_internal_failure(ctsio,
1647							 /*sks_valid*/ 0,
1648							 /*retry_count*/0);
1649				goto bailout;
1650			}
1651		}
1652
1653		len_copied += len_to_copy;
1654
1655		if (ext_sglist[i].len == ext_watermark) {
1656			i++;
1657			ext_watermark = 0;
1658		}
1659
1660		if (kern_sglist[j].len == kern_watermark) {
1661			j++;
1662			kern_watermark = 0;
1663		}
1664	}
1665
1666	ctsio->ext_data_filled += len_copied;
1667
1668	CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1669			 "kern_sg_entries: %d\n", ext_sg_entries,
1670			 kern_sg_entries));
1671	CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1672			 "kern_data_len = %d\n", ctsio->ext_data_len,
1673			 ctsio->kern_data_len));
1674
1675
1676	/* XXX KDM set residual?? */
1677bailout:
1678
1679	if (ext_sglist_malloced != 0)
1680		free(ext_sglist, M_CTL);
1681
1682	return (CTL_RETVAL_COMPLETE);
1683}
1684
1685/*
1686 * Serialize a command that went down the "wrong" side, and so was sent to
1687 * this controller for execution.  The logic is a little different than the
1688 * standard case in ctl_scsiio_precheck().  Errors in this case need to get
1689 * sent back to the other side, but in the success case, we execute the
1690 * command on this side (XFER mode) or tell the other side to execute it
1691 * (SER_ONLY mode).
1692 */
1693static int
1694ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1695{
1696	struct ctl_softc *ctl_softc;
1697	union ctl_ha_msg msg_info;
1698	struct ctl_lun *lun;
1699	int retval = 0;
1700	uint32_t targ_lun;
1701
1702	ctl_softc = control_softc;
1703	if (have_lock == 0)
1704		mtx_lock(&ctl_softc->ctl_lock);
1705
1706	targ_lun = ctsio->io_hdr.nexus.targ_lun;
1707	if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
1708		targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
1709	lun = ctl_softc->ctl_luns[targ_lun];
1710	if (lun==NULL)
1711	{
1712		/*
1713		 * Why isn't LUN defined? The other side wouldn't
1714		 * send a cmd if the LUN is undefined.
1715		 */
1716		printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1717
1718		/* "Logical unit not supported" */
1719		ctl_set_sense_data(&msg_info.scsi.sense_data,
1720				   lun,
1721				   /*sense_format*/SSD_TYPE_NONE,
1722				   /*current_error*/ 1,
1723				   /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1724				   /*asc*/ 0x25,
1725				   /*ascq*/ 0x00,
1726				   SSD_ELEM_NONE);
1727
1728		msg_info.scsi.sense_len = SSD_FULL_SIZE;
1729		msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1730		msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1731		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1732		msg_info.hdr.serializing_sc = NULL;
1733		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1734	        if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1735				sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1736		}
1737		if (have_lock == 0)
1738			mtx_unlock(&ctl_softc->ctl_lock);
1739		return(1);
1740
1741	}
1742
1743    	TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1744
1745	switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1746		(union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1747		 ooa_links))) {
1748	case CTL_ACTION_BLOCK:
1749		ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1750		TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1751				  blocked_links);
1752		break;
1753	case CTL_ACTION_PASS:
1754	case CTL_ACTION_SKIP:
1755		if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1756			ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1757			STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1758					   &ctsio->io_hdr, links);
1759		} else {
1760
1761			/* send msg back to other side */
1762			msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1763			msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1764			msg_info.hdr.msg_type = CTL_MSG_R2R;
1765#if 0
1766			printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1767#endif
1768		        if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1769			    sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1770			}
1771		}
1772		break;
1773	case CTL_ACTION_OVERLAP:
1774		/* OVERLAPPED COMMANDS ATTEMPTED */
1775		ctl_set_sense_data(&msg_info.scsi.sense_data,
1776				   lun,
1777				   /*sense_format*/SSD_TYPE_NONE,
1778				   /*current_error*/ 1,
1779				   /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1780				   /*asc*/ 0x4E,
1781				   /*ascq*/ 0x00,
1782				   SSD_ELEM_NONE);
1783
1784		msg_info.scsi.sense_len = SSD_FULL_SIZE;
1785		msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1786		msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1787		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1788		msg_info.hdr.serializing_sc = NULL;
1789		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1790#if 0
1791		printf("BAD JUJU:Major Bummer Overlap\n");
1792#endif
1793		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1794		retval = 1;
1795		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1796		    sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1797		}
1798		break;
1799	case CTL_ACTION_OVERLAP_TAG:
1800		/* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1801		ctl_set_sense_data(&msg_info.scsi.sense_data,
1802				   lun,
1803				   /*sense_format*/SSD_TYPE_NONE,
1804				   /*current_error*/ 1,
1805				   /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1806				   /*asc*/ 0x4D,
1807				   /*ascq*/ ctsio->tag_num & 0xff,
1808				   SSD_ELEM_NONE);
1809
1810		msg_info.scsi.sense_len = SSD_FULL_SIZE;
1811		msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1812		msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1813		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1814		msg_info.hdr.serializing_sc = NULL;
1815		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1816#if 0
1817		printf("BAD JUJU:Major Bummer Overlap Tag\n");
1818#endif
1819		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1820		retval = 1;
1821		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1822		    sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1823		}
1824		break;
1825	case CTL_ACTION_ERROR:
1826	default:
1827		/* "Internal target failure" */
1828		ctl_set_sense_data(&msg_info.scsi.sense_data,
1829				   lun,
1830				   /*sense_format*/SSD_TYPE_NONE,
1831				   /*current_error*/ 1,
1832				   /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1833				   /*asc*/ 0x44,
1834				   /*ascq*/ 0x00,
1835				   SSD_ELEM_NONE);
1836
1837		msg_info.scsi.sense_len = SSD_FULL_SIZE;
1838		msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1839		msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1840		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1841		msg_info.hdr.serializing_sc = NULL;
1842		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1843#if 0
1844		printf("BAD JUJU:Major Bummer HW Error\n");
1845#endif
1846		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1847		retval = 1;
1848		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1849		    sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1850		}
1851		break;
1852	}
1853	if (have_lock == 0)
1854		mtx_unlock(&ctl_softc->ctl_lock);
1855	return (retval);
1856}
1857
1858static int
1859ctl_ioctl_submit_wait(union ctl_io *io)
1860{
1861	struct ctl_fe_ioctl_params params;
1862	ctl_fe_ioctl_state last_state;
1863	int done, retval;
1864
1865	retval = 0;
1866
1867	bzero(&params, sizeof(params));
1868
1869	mtx_init(&params.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1870	cv_init(&params.sem, "ctlioccv");
1871	params.state = CTL_IOCTL_INPROG;
1872	last_state = params.state;
1873
1874	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = &params;
1875
1876	CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1877
1878	/* This shouldn't happen */
1879	if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1880		return (retval);
1881
1882	done = 0;
1883
1884	do {
1885		mtx_lock(&params.ioctl_mtx);
1886		/*
1887		 * Check the state here, and don't sleep if the state has
1888		 * already changed (i.e. wakeup has already occured, but we
1889		 * weren't waiting yet).
1890		 */
1891		if (params.state == last_state) {
1892			/* XXX KDM cv_wait_sig instead? */
1893			cv_wait(&params.sem, &params.ioctl_mtx);
1894		}
1895		last_state = params.state;
1896
1897		switch (params.state) {
1898		case CTL_IOCTL_INPROG:
1899			/* Why did we wake up? */
1900			/* XXX KDM error here? */
1901			mtx_unlock(&params.ioctl_mtx);
1902			break;
1903		case CTL_IOCTL_DATAMOVE:
1904			CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1905
1906			/*
1907			 * change last_state back to INPROG to avoid
1908			 * deadlock on subsequent data moves.
1909			 */
1910			params.state = last_state = CTL_IOCTL_INPROG;
1911
1912			mtx_unlock(&params.ioctl_mtx);
1913			ctl_ioctl_do_datamove(&io->scsiio);
1914			/*
1915			 * Note that in some cases, most notably writes,
1916			 * this will queue the I/O and call us back later.
1917			 * In other cases, generally reads, this routine
1918			 * will immediately call back and wake us up,
1919			 * probably using our own context.
1920			 */
1921			io->scsiio.be_move_done(io);
1922			break;
1923		case CTL_IOCTL_DONE:
1924			mtx_unlock(&params.ioctl_mtx);
1925			CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1926			done = 1;
1927			break;
1928		default:
1929			mtx_unlock(&params.ioctl_mtx);
1930			/* XXX KDM error here? */
1931			break;
1932		}
1933	} while (done == 0);
1934
1935	mtx_destroy(&params.ioctl_mtx);
1936	cv_destroy(&params.sem);
1937
1938	return (CTL_RETVAL_COMPLETE);
1939}
1940
1941static void
1942ctl_ioctl_datamove(union ctl_io *io)
1943{
1944	struct ctl_fe_ioctl_params *params;
1945
1946	params = (struct ctl_fe_ioctl_params *)
1947		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1948
1949	mtx_lock(&params->ioctl_mtx);
1950	params->state = CTL_IOCTL_DATAMOVE;
1951	cv_broadcast(&params->sem);
1952	mtx_unlock(&params->ioctl_mtx);
1953}
1954
1955static void
1956ctl_ioctl_done(union ctl_io *io)
1957{
1958	struct ctl_fe_ioctl_params *params;
1959
1960	params = (struct ctl_fe_ioctl_params *)
1961		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1962
1963	mtx_lock(&params->ioctl_mtx);
1964	params->state = CTL_IOCTL_DONE;
1965	cv_broadcast(&params->sem);
1966	mtx_unlock(&params->ioctl_mtx);
1967}
1968
1969static void
1970ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1971{
1972	struct ctl_fe_ioctl_startstop_info *sd_info;
1973
1974	sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1975
1976	sd_info->hs_info.status = metatask->status;
1977	sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1978	sd_info->hs_info.luns_complete =
1979		metatask->taskinfo.startstop.luns_complete;
1980	sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1981
1982	cv_broadcast(&sd_info->sem);
1983}
1984
1985static void
1986ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1987{
1988	struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1989
1990	fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1991
1992	mtx_lock(fe_bbr_info->lock);
1993	fe_bbr_info->bbr_info->status = metatask->status;
1994	fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1995	fe_bbr_info->wakeup_done = 1;
1996	mtx_unlock(fe_bbr_info->lock);
1997
1998	cv_broadcast(&fe_bbr_info->sem);
1999}
2000
2001/*
2002 * Returns 0 for success, errno for failure.
2003 */
2004static int
2005ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2006		   struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2007{
2008	union ctl_io *io;
2009	int retval;
2010
2011	retval = 0;
2012
2013	mtx_assert(&control_softc->ctl_lock, MA_OWNED);
2014
2015	for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2016	     (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2017	     ooa_links)) {
2018		struct ctl_ooa_entry *entry;
2019
2020		/*
2021		 * If we've got more than we can fit, just count the
2022		 * remaining entries.
2023		 */
2024		if (*cur_fill_num >= ooa_hdr->alloc_num)
2025			continue;
2026
2027		entry = &kern_entries[*cur_fill_num];
2028
2029		entry->tag_num = io->scsiio.tag_num;
2030		entry->lun_num = lun->lun;
2031#ifdef CTL_TIME_IO
2032		entry->start_bt = io->io_hdr.start_bt;
2033#endif
2034		bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2035		entry->cdb_len = io->scsiio.cdb_len;
2036		if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2037			entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2038
2039		if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2040			entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2041
2042		if (io->io_hdr.flags & CTL_FLAG_ABORT)
2043			entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2044
2045		if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2046			entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2047
2048		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2049			entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2050	}
2051
2052	return (retval);
2053}
2054
2055static void *
2056ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2057		 size_t error_str_len)
2058{
2059	void *kptr;
2060
2061	kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2062
2063	if (copyin(user_addr, kptr, len) != 0) {
2064		snprintf(error_str, error_str_len, "Error copying %d bytes "
2065			 "from user address %p to kernel address %p", len,
2066			 user_addr, kptr);
2067		free(kptr, M_CTL);
2068		return (NULL);
2069	}
2070
2071	return (kptr);
2072}
2073
2074static void
2075ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2076{
2077	int i;
2078
2079	if (be_args == NULL)
2080		return;
2081
2082	for (i = 0; i < num_be_args; i++) {
2083		free(be_args[i].kname, M_CTL);
2084		free(be_args[i].kvalue, M_CTL);
2085	}
2086
2087	free(be_args, M_CTL);
2088}
2089
2090static struct ctl_be_arg *
2091ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2092		char *error_str, size_t error_str_len)
2093{
2094	struct ctl_be_arg *args;
2095	int i;
2096
2097	args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2098				error_str, error_str_len);
2099
2100	if (args == NULL)
2101		goto bailout;
2102
2103	for (i = 0; i < num_be_args; i++) {
2104		args[i].kname = NULL;
2105		args[i].kvalue = NULL;
2106	}
2107
2108	for (i = 0; i < num_be_args; i++) {
2109		uint8_t *tmpptr;
2110
2111		args[i].kname = ctl_copyin_alloc(args[i].name,
2112			args[i].namelen, error_str, error_str_len);
2113		if (args[i].kname == NULL)
2114			goto bailout;
2115
2116		if (args[i].kname[args[i].namelen - 1] != '\0') {
2117			snprintf(error_str, error_str_len, "Argument %d "
2118				 "name is not NUL-terminated", i);
2119			goto bailout;
2120		}
2121
2122		args[i].kvalue = NULL;
2123
2124		tmpptr = ctl_copyin_alloc(args[i].value,
2125			args[i].vallen, error_str, error_str_len);
2126		if (tmpptr == NULL)
2127			goto bailout;
2128
2129		args[i].kvalue = tmpptr;
2130
2131		if ((args[i].flags & CTL_BEARG_ASCII)
2132		 && (tmpptr[args[i].vallen - 1] != '\0')) {
2133			snprintf(error_str, error_str_len, "Argument %d "
2134				 "value is not NUL-terminated", i);
2135			goto bailout;
2136		}
2137	}
2138
2139	return (args);
2140bailout:
2141
2142	ctl_free_args(num_be_args, args);
2143
2144	return (NULL);
2145}
2146
2147/*
2148 * Escape characters that are illegal or not recommended in XML.
2149 */
2150int
2151ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2152{
2153	int retval;
2154
2155	retval = 0;
2156
2157	for (; *str; str++) {
2158		switch (*str) {
2159		case '&':
2160			retval = sbuf_printf(sb, "&amp;");
2161			break;
2162		case '>':
2163			retval = sbuf_printf(sb, "&gt;");
2164			break;
2165		case '<':
2166			retval = sbuf_printf(sb, "&lt;");
2167			break;
2168		default:
2169			retval = sbuf_putc(sb, *str);
2170			break;
2171		}
2172
2173		if (retval != 0)
2174			break;
2175
2176	}
2177
2178	return (retval);
2179}
2180
2181static int
2182ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2183	  struct thread *td)
2184{
2185	struct ctl_softc *softc;
2186	int retval;
2187
2188	softc = control_softc;
2189
2190	retval = 0;
2191
2192	switch (cmd) {
2193	case CTL_IO: {
2194		union ctl_io *io;
2195		void *pool_tmp;
2196
2197		/*
2198		 * If we haven't been "enabled", don't allow any SCSI I/O
2199		 * to this FETD.
2200		 */
2201		if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2202			retval = -EPERM;
2203			break;
2204		}
2205
2206		io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2207		if (io == NULL) {
2208			printf("ctl_ioctl: can't allocate ctl_io!\n");
2209			retval = -ENOSPC;
2210			break;
2211		}
2212
2213		/*
2214		 * Need to save the pool reference so it doesn't get
2215		 * spammed by the user's ctl_io.
2216		 */
2217		pool_tmp = io->io_hdr.pool;
2218
2219		memcpy(io, (void *)addr, sizeof(*io));
2220
2221		io->io_hdr.pool = pool_tmp;
2222		/*
2223		 * No status yet, so make sure the status is set properly.
2224		 */
2225		io->io_hdr.status = CTL_STATUS_NONE;
2226
2227		/*
2228		 * The user sets the initiator ID, target and LUN IDs.
2229		 */
2230		io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2231		io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2232		if ((io->io_hdr.io_type == CTL_IO_SCSI)
2233		 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2234			io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2235
2236		retval = ctl_ioctl_submit_wait(io);
2237
2238		if (retval != 0) {
2239			ctl_free_io(io);
2240			break;
2241		}
2242
2243		memcpy((void *)addr, io, sizeof(*io));
2244
2245		/* return this to our pool */
2246		ctl_free_io(io);
2247
2248		break;
2249	}
2250	case CTL_ENABLE_PORT:
2251	case CTL_DISABLE_PORT:
2252	case CTL_SET_PORT_WWNS: {
2253		struct ctl_frontend *fe;
2254		struct ctl_port_entry *entry;
2255
2256		entry = (struct ctl_port_entry *)addr;
2257
2258		mtx_lock(&softc->ctl_lock);
2259		STAILQ_FOREACH(fe, &softc->fe_list, links) {
2260			int action, done;
2261
2262			action = 0;
2263			done = 0;
2264
2265			if ((entry->port_type == CTL_PORT_NONE)
2266			 && (entry->targ_port == fe->targ_port)) {
2267				/*
2268				 * If the user only wants to enable or
2269				 * disable or set WWNs on a specific port,
2270				 * do the operation and we're done.
2271				 */
2272				action = 1;
2273				done = 1;
2274			} else if (entry->port_type & fe->port_type) {
2275				/*
2276				 * Compare the user's type mask with the
2277				 * particular frontend type to see if we
2278				 * have a match.
2279				 */
2280				action = 1;
2281				done = 0;
2282
2283				/*
2284				 * Make sure the user isn't trying to set
2285				 * WWNs on multiple ports at the same time.
2286				 */
2287				if (cmd == CTL_SET_PORT_WWNS) {
2288					printf("%s: Can't set WWNs on "
2289					       "multiple ports\n", __func__);
2290					retval = EINVAL;
2291					break;
2292				}
2293			}
2294			if (action != 0) {
2295				/*
2296				 * XXX KDM we have to drop the lock here,
2297				 * because the online/offline operations
2298				 * can potentially block.  We need to
2299				 * reference count the frontends so they
2300				 * can't go away,
2301				 */
2302				mtx_unlock(&softc->ctl_lock);
2303
2304				if (cmd == CTL_ENABLE_PORT) {
2305					struct ctl_lun *lun;
2306
2307					STAILQ_FOREACH(lun, &softc->lun_list,
2308						       links) {
2309						fe->lun_enable(fe->targ_lun_arg,
2310						    lun->target,
2311						    lun->lun);
2312					}
2313
2314					ctl_frontend_online(fe);
2315				} else if (cmd == CTL_DISABLE_PORT) {
2316					struct ctl_lun *lun;
2317
2318					ctl_frontend_offline(fe);
2319
2320					STAILQ_FOREACH(lun, &softc->lun_list,
2321						       links) {
2322						fe->lun_disable(
2323						    fe->targ_lun_arg,
2324						    lun->target,
2325						    lun->lun);
2326					}
2327				}
2328
2329				mtx_lock(&softc->ctl_lock);
2330
2331				if (cmd == CTL_SET_PORT_WWNS)
2332					ctl_frontend_set_wwns(fe,
2333					    (entry->flags & CTL_PORT_WWNN_VALID) ?
2334					    1 : 0, entry->wwnn,
2335					    (entry->flags & CTL_PORT_WWPN_VALID) ?
2336					    1 : 0, entry->wwpn);
2337			}
2338			if (done != 0)
2339				break;
2340		}
2341		mtx_unlock(&softc->ctl_lock);
2342		break;
2343	}
2344	case CTL_GET_PORT_LIST: {
2345		struct ctl_frontend *fe;
2346		struct ctl_port_list *list;
2347		int i;
2348
2349		list = (struct ctl_port_list *)addr;
2350
2351		if (list->alloc_len != (list->alloc_num *
2352		    sizeof(struct ctl_port_entry))) {
2353			printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2354			       "alloc_num %u * sizeof(struct ctl_port_entry) "
2355			       "%zu\n", __func__, list->alloc_len,
2356			       list->alloc_num, sizeof(struct ctl_port_entry));
2357			retval = EINVAL;
2358			break;
2359		}
2360		list->fill_len = 0;
2361		list->fill_num = 0;
2362		list->dropped_num = 0;
2363		i = 0;
2364		mtx_lock(&softc->ctl_lock);
2365		STAILQ_FOREACH(fe, &softc->fe_list, links) {
2366			struct ctl_port_entry entry, *list_entry;
2367
2368			if (list->fill_num >= list->alloc_num) {
2369				list->dropped_num++;
2370				continue;
2371			}
2372
2373			entry.port_type = fe->port_type;
2374			strlcpy(entry.port_name, fe->port_name,
2375				sizeof(entry.port_name));
2376			entry.targ_port = fe->targ_port;
2377			entry.physical_port = fe->physical_port;
2378			entry.virtual_port = fe->virtual_port;
2379			entry.wwnn = fe->wwnn;
2380			entry.wwpn = fe->wwpn;
2381			if (fe->status & CTL_PORT_STATUS_ONLINE)
2382				entry.online = 1;
2383			else
2384				entry.online = 0;
2385
2386			list_entry = &list->entries[i];
2387
2388			retval = copyout(&entry, list_entry, sizeof(entry));
2389			if (retval != 0) {
2390				printf("%s: CTL_GET_PORT_LIST: copyout "
2391				       "returned %d\n", __func__, retval);
2392				break;
2393			}
2394			i++;
2395			list->fill_num++;
2396			list->fill_len += sizeof(entry);
2397		}
2398		mtx_unlock(&softc->ctl_lock);
2399
2400		/*
2401		 * If this is non-zero, we had a copyout fault, so there's
2402		 * probably no point in attempting to set the status inside
2403		 * the structure.
2404		 */
2405		if (retval != 0)
2406			break;
2407
2408		if (list->dropped_num > 0)
2409			list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2410		else
2411			list->status = CTL_PORT_LIST_OK;
2412		break;
2413	}
2414	case CTL_DUMP_OOA: {
2415		struct ctl_lun *lun;
2416		union ctl_io *io;
2417		char printbuf[128];
2418		struct sbuf sb;
2419
2420		mtx_lock(&softc->ctl_lock);
2421		printf("Dumping OOA queues:\n");
2422		STAILQ_FOREACH(lun, &softc->lun_list, links) {
2423			for (io = (union ctl_io *)TAILQ_FIRST(
2424			     &lun->ooa_queue); io != NULL;
2425			     io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2426			     ooa_links)) {
2427				sbuf_new(&sb, printbuf, sizeof(printbuf),
2428					 SBUF_FIXEDLEN);
2429				sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2430					    (intmax_t)lun->lun,
2431					    io->scsiio.tag_num,
2432					    (io->io_hdr.flags &
2433					    CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2434					    (io->io_hdr.flags &
2435					    CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2436					    (io->io_hdr.flags &
2437					    CTL_FLAG_ABORT) ? " ABORT" : "",
2438			                    (io->io_hdr.flags &
2439		                        CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2440				ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2441				sbuf_finish(&sb);
2442				printf("%s\n", sbuf_data(&sb));
2443			}
2444		}
2445		printf("OOA queues dump done\n");
2446		mtx_unlock(&softc->ctl_lock);
2447		break;
2448	}
2449	case CTL_GET_OOA: {
2450		struct ctl_lun *lun;
2451		struct ctl_ooa *ooa_hdr;
2452		struct ctl_ooa_entry *entries;
2453		uint32_t cur_fill_num;
2454
2455		ooa_hdr = (struct ctl_ooa *)addr;
2456
2457		if ((ooa_hdr->alloc_len == 0)
2458		 || (ooa_hdr->alloc_num == 0)) {
2459			printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2460			       "must be non-zero\n", __func__,
2461			       ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2462			retval = EINVAL;
2463			break;
2464		}
2465
2466		if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2467		    sizeof(struct ctl_ooa_entry))) {
2468			printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2469			       "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2470			       __func__, ooa_hdr->alloc_len,
2471			       ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2472			retval = EINVAL;
2473			break;
2474		}
2475
2476		entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2477		if (entries == NULL) {
2478			printf("%s: could not allocate %d bytes for OOA "
2479			       "dump\n", __func__, ooa_hdr->alloc_len);
2480			retval = ENOMEM;
2481			break;
2482		}
2483
2484		mtx_lock(&softc->ctl_lock);
2485		if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2486		 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2487		  || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2488			mtx_unlock(&softc->ctl_lock);
2489			free(entries, M_CTL);
2490			printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2491			       __func__, (uintmax_t)ooa_hdr->lun_num);
2492			retval = EINVAL;
2493			break;
2494		}
2495
2496		cur_fill_num = 0;
2497
2498		if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2499			STAILQ_FOREACH(lun, &softc->lun_list, links) {
2500				retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2501					ooa_hdr, entries);
2502				if (retval != 0)
2503					break;
2504			}
2505			if (retval != 0) {
2506				mtx_unlock(&softc->ctl_lock);
2507				free(entries, M_CTL);
2508				break;
2509			}
2510		} else {
2511			lun = softc->ctl_luns[ooa_hdr->lun_num];
2512
2513			retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2514						    entries);
2515		}
2516		mtx_unlock(&softc->ctl_lock);
2517
2518		ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2519		ooa_hdr->fill_len = ooa_hdr->fill_num *
2520			sizeof(struct ctl_ooa_entry);
2521		retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2522		if (retval != 0) {
2523			printf("%s: error copying out %d bytes for OOA dump\n",
2524			       __func__, ooa_hdr->fill_len);
2525		}
2526
2527		getbintime(&ooa_hdr->cur_bt);
2528
2529		if (cur_fill_num > ooa_hdr->alloc_num) {
2530			ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2531			ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2532		} else {
2533			ooa_hdr->dropped_num = 0;
2534			ooa_hdr->status = CTL_OOA_OK;
2535		}
2536
2537		free(entries, M_CTL);
2538		break;
2539	}
2540	case CTL_CHECK_OOA: {
2541		union ctl_io *io;
2542		struct ctl_lun *lun;
2543		struct ctl_ooa_info *ooa_info;
2544
2545
2546		ooa_info = (struct ctl_ooa_info *)addr;
2547
2548		if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2549			ooa_info->status = CTL_OOA_INVALID_LUN;
2550			break;
2551		}
2552		mtx_lock(&softc->ctl_lock);
2553		lun = softc->ctl_luns[ooa_info->lun_id];
2554		if (lun == NULL) {
2555			mtx_unlock(&softc->ctl_lock);
2556			ooa_info->status = CTL_OOA_INVALID_LUN;
2557			break;
2558		}
2559
2560		ooa_info->num_entries = 0;
2561		for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2562		     io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2563		     &io->io_hdr, ooa_links)) {
2564			ooa_info->num_entries++;
2565		}
2566
2567		mtx_unlock(&softc->ctl_lock);
2568		ooa_info->status = CTL_OOA_SUCCESS;
2569
2570		break;
2571	}
2572	case CTL_HARD_START:
2573	case CTL_HARD_STOP: {
2574		struct ctl_fe_ioctl_startstop_info ss_info;
2575		struct cfi_metatask *metatask;
2576		struct mtx hs_mtx;
2577
2578		mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2579
2580		cv_init(&ss_info.sem, "hard start/stop cv" );
2581
2582		metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2583		if (metatask == NULL) {
2584			retval = ENOMEM;
2585			mtx_destroy(&hs_mtx);
2586			break;
2587		}
2588
2589		if (cmd == CTL_HARD_START)
2590			metatask->tasktype = CFI_TASK_STARTUP;
2591		else
2592			metatask->tasktype = CFI_TASK_SHUTDOWN;
2593
2594		metatask->callback = ctl_ioctl_hard_startstop_callback;
2595		metatask->callback_arg = &ss_info;
2596
2597		cfi_action(metatask);
2598
2599		/* Wait for the callback */
2600		mtx_lock(&hs_mtx);
2601		cv_wait_sig(&ss_info.sem, &hs_mtx);
2602		mtx_unlock(&hs_mtx);
2603
2604		/*
2605		 * All information has been copied from the metatask by the
2606		 * time cv_broadcast() is called, so we free the metatask here.
2607		 */
2608		cfi_free_metatask(metatask);
2609
2610		memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2611
2612		mtx_destroy(&hs_mtx);
2613		break;
2614	}
2615	case CTL_BBRREAD: {
2616		struct ctl_bbrread_info *bbr_info;
2617		struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2618		struct mtx bbr_mtx;
2619		struct cfi_metatask *metatask;
2620
2621		bbr_info = (struct ctl_bbrread_info *)addr;
2622
2623		bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2624
2625		bzero(&bbr_mtx, sizeof(bbr_mtx));
2626		mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2627
2628		fe_bbr_info.bbr_info = bbr_info;
2629		fe_bbr_info.lock = &bbr_mtx;
2630
2631		cv_init(&fe_bbr_info.sem, "BBR read cv");
2632		metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2633
2634		if (metatask == NULL) {
2635			mtx_destroy(&bbr_mtx);
2636			cv_destroy(&fe_bbr_info.sem);
2637			retval = ENOMEM;
2638			break;
2639		}
2640		metatask->tasktype = CFI_TASK_BBRREAD;
2641		metatask->callback = ctl_ioctl_bbrread_callback;
2642		metatask->callback_arg = &fe_bbr_info;
2643		metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2644		metatask->taskinfo.bbrread.lba = bbr_info->lba;
2645		metatask->taskinfo.bbrread.len = bbr_info->len;
2646
2647		cfi_action(metatask);
2648
2649		mtx_lock(&bbr_mtx);
2650		while (fe_bbr_info.wakeup_done == 0)
2651			cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2652		mtx_unlock(&bbr_mtx);
2653
2654		bbr_info->status = metatask->status;
2655		bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2656		bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2657		memcpy(&bbr_info->sense_data,
2658		       &metatask->taskinfo.bbrread.sense_data,
2659		       ctl_min(sizeof(bbr_info->sense_data),
2660			       sizeof(metatask->taskinfo.bbrread.sense_data)));
2661
2662		cfi_free_metatask(metatask);
2663
2664		mtx_destroy(&bbr_mtx);
2665		cv_destroy(&fe_bbr_info.sem);
2666
2667		break;
2668	}
2669	case CTL_DELAY_IO: {
2670		struct ctl_io_delay_info *delay_info;
2671#ifdef CTL_IO_DELAY
2672		struct ctl_lun *lun;
2673#endif /* CTL_IO_DELAY */
2674
2675		delay_info = (struct ctl_io_delay_info *)addr;
2676
2677#ifdef CTL_IO_DELAY
2678		mtx_lock(&softc->ctl_lock);
2679
2680		if ((delay_info->lun_id > CTL_MAX_LUNS)
2681		 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2682			delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2683		} else {
2684			lun = softc->ctl_luns[delay_info->lun_id];
2685
2686			delay_info->status = CTL_DELAY_STATUS_OK;
2687
2688			switch (delay_info->delay_type) {
2689			case CTL_DELAY_TYPE_CONT:
2690				break;
2691			case CTL_DELAY_TYPE_ONESHOT:
2692				break;
2693			default:
2694				delay_info->status =
2695					CTL_DELAY_STATUS_INVALID_TYPE;
2696				break;
2697			}
2698
2699			switch (delay_info->delay_loc) {
2700			case CTL_DELAY_LOC_DATAMOVE:
2701				lun->delay_info.datamove_type =
2702					delay_info->delay_type;
2703				lun->delay_info.datamove_delay =
2704					delay_info->delay_secs;
2705				break;
2706			case CTL_DELAY_LOC_DONE:
2707				lun->delay_info.done_type =
2708					delay_info->delay_type;
2709				lun->delay_info.done_delay =
2710					delay_info->delay_secs;
2711				break;
2712			default:
2713				delay_info->status =
2714					CTL_DELAY_STATUS_INVALID_LOC;
2715				break;
2716			}
2717		}
2718
2719		mtx_unlock(&softc->ctl_lock);
2720#else
2721		delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2722#endif /* CTL_IO_DELAY */
2723		break;
2724	}
2725	case CTL_REALSYNC_SET: {
2726		int *syncstate;
2727
2728		syncstate = (int *)addr;
2729
2730		mtx_lock(&softc->ctl_lock);
2731		switch (*syncstate) {
2732		case 0:
2733			softc->flags &= ~CTL_FLAG_REAL_SYNC;
2734			break;
2735		case 1:
2736			softc->flags |= CTL_FLAG_REAL_SYNC;
2737			break;
2738		default:
2739			retval = -EINVAL;
2740			break;
2741		}
2742		mtx_unlock(&softc->ctl_lock);
2743		break;
2744	}
2745	case CTL_REALSYNC_GET: {
2746		int *syncstate;
2747
2748		syncstate = (int*)addr;
2749
2750		mtx_lock(&softc->ctl_lock);
2751		if (softc->flags & CTL_FLAG_REAL_SYNC)
2752			*syncstate = 1;
2753		else
2754			*syncstate = 0;
2755		mtx_unlock(&softc->ctl_lock);
2756
2757		break;
2758	}
2759	case CTL_SETSYNC:
2760	case CTL_GETSYNC: {
2761		struct ctl_sync_info *sync_info;
2762		struct ctl_lun *lun;
2763
2764		sync_info = (struct ctl_sync_info *)addr;
2765
2766		mtx_lock(&softc->ctl_lock);
2767		lun = softc->ctl_luns[sync_info->lun_id];
2768		if (lun == NULL) {
2769			mtx_unlock(&softc->ctl_lock);
2770			sync_info->status = CTL_GS_SYNC_NO_LUN;
2771		}
2772		/*
2773		 * Get or set the sync interval.  We're not bounds checking
2774		 * in the set case, hopefully the user won't do something
2775		 * silly.
2776		 */
2777		if (cmd == CTL_GETSYNC)
2778			sync_info->sync_interval = lun->sync_interval;
2779		else
2780			lun->sync_interval = sync_info->sync_interval;
2781
2782		mtx_unlock(&softc->ctl_lock);
2783
2784		sync_info->status = CTL_GS_SYNC_OK;
2785
2786		break;
2787	}
2788	case CTL_GETSTATS: {
2789		struct ctl_stats *stats;
2790		struct ctl_lun *lun;
2791		int i;
2792
2793		stats = (struct ctl_stats *)addr;
2794
2795		if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2796		     stats->alloc_len) {
2797			stats->status = CTL_SS_NEED_MORE_SPACE;
2798			stats->num_luns = softc->num_luns;
2799			break;
2800		}
2801		/*
2802		 * XXX KDM no locking here.  If the LUN list changes,
2803		 * things can blow up.
2804		 */
2805		for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2806		     i++, lun = STAILQ_NEXT(lun, links)) {
2807			retval = copyout(&lun->stats, &stats->lun_stats[i],
2808					 sizeof(lun->stats));
2809			if (retval != 0)
2810				break;
2811		}
2812		stats->num_luns = softc->num_luns;
2813		stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2814				 softc->num_luns;
2815		stats->status = CTL_SS_OK;
2816#ifdef CTL_TIME_IO
2817		stats->flags = CTL_STATS_FLAG_TIME_VALID;
2818#else
2819		stats->flags = CTL_STATS_FLAG_NONE;
2820#endif
2821		getnanouptime(&stats->timestamp);
2822		break;
2823	}
2824	case CTL_ERROR_INJECT: {
2825		struct ctl_error_desc *err_desc, *new_err_desc;
2826		struct ctl_lun *lun;
2827
2828		err_desc = (struct ctl_error_desc *)addr;
2829
2830		new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2831				      M_WAITOK | M_ZERO);
2832		bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2833
2834		mtx_lock(&softc->ctl_lock);
2835		lun = softc->ctl_luns[err_desc->lun_id];
2836		if (lun == NULL) {
2837			mtx_unlock(&softc->ctl_lock);
2838			printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2839			       __func__, (uintmax_t)err_desc->lun_id);
2840			retval = EINVAL;
2841			break;
2842		}
2843
2844		/*
2845		 * We could do some checking here to verify the validity
2846		 * of the request, but given the complexity of error
2847		 * injection requests, the checking logic would be fairly
2848		 * complex.
2849		 *
2850		 * For now, if the request is invalid, it just won't get
2851		 * executed and might get deleted.
2852		 */
2853		STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2854
2855		/*
2856		 * XXX KDM check to make sure the serial number is unique,
2857		 * in case we somehow manage to wrap.  That shouldn't
2858		 * happen for a very long time, but it's the right thing to
2859		 * do.
2860		 */
2861		new_err_desc->serial = lun->error_serial;
2862		err_desc->serial = lun->error_serial;
2863		lun->error_serial++;
2864
2865		mtx_unlock(&softc->ctl_lock);
2866		break;
2867	}
2868	case CTL_ERROR_INJECT_DELETE: {
2869		struct ctl_error_desc *delete_desc, *desc, *desc2;
2870		struct ctl_lun *lun;
2871		int delete_done;
2872
2873		delete_desc = (struct ctl_error_desc *)addr;
2874		delete_done = 0;
2875
2876		mtx_lock(&softc->ctl_lock);
2877		lun = softc->ctl_luns[delete_desc->lun_id];
2878		if (lun == NULL) {
2879			mtx_unlock(&softc->ctl_lock);
2880			printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2881			       __func__, (uintmax_t)delete_desc->lun_id);
2882			retval = EINVAL;
2883			break;
2884		}
2885		STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2886			if (desc->serial != delete_desc->serial)
2887				continue;
2888
2889			STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2890				      links);
2891			free(desc, M_CTL);
2892			delete_done = 1;
2893		}
2894		mtx_unlock(&softc->ctl_lock);
2895		if (delete_done == 0) {
2896			printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2897			       "error serial %ju on LUN %u\n", __func__,
2898			       delete_desc->serial, delete_desc->lun_id);
2899			retval = EINVAL;
2900			break;
2901		}
2902		break;
2903	}
2904	case CTL_DUMP_STRUCTS: {
2905		int i, j, k;
2906		struct ctl_frontend *fe;
2907
2908		printf("CTL IID to WWPN map start:\n");
2909		for (i = 0; i < CTL_MAX_PORTS; i++) {
2910			for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2911				if (softc->wwpn_iid[i][j].in_use == 0)
2912					continue;
2913
2914				printf("port %d iid %u WWPN %#jx\n",
2915				       softc->wwpn_iid[i][j].port,
2916				       softc->wwpn_iid[i][j].iid,
2917				       (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2918			}
2919		}
2920		printf("CTL IID to WWPN map end\n");
2921		printf("CTL Persistent Reservation information start:\n");
2922		for (i = 0; i < CTL_MAX_LUNS; i++) {
2923			struct ctl_lun *lun;
2924
2925			lun = softc->ctl_luns[i];
2926
2927			if ((lun == NULL)
2928			 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2929				continue;
2930
2931			for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2932				for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2933					if (lun->per_res[j+k].registered == 0)
2934						continue;
2935					printf("LUN %d port %d iid %d key "
2936					       "%#jx\n", i, j, k,
2937					       (uintmax_t)scsi_8btou64(
2938					       lun->per_res[j+k].res_key.key));
2939				}
2940			}
2941		}
2942		printf("CTL Persistent Reservation information end\n");
2943		printf("CTL Frontends:\n");
2944		/*
2945		 * XXX KDM calling this without a lock.  We'd likely want
2946		 * to drop the lock before calling the frontend's dump
2947		 * routine anyway.
2948		 */
2949		STAILQ_FOREACH(fe, &softc->fe_list, links) {
2950			printf("Frontend %s Type %u pport %d vport %d WWNN "
2951			       "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2952			       fe->physical_port, fe->virtual_port,
2953			       (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2954
2955			/*
2956			 * Frontends are not required to support the dump
2957			 * routine.
2958			 */
2959			if (fe->fe_dump == NULL)
2960				continue;
2961
2962			fe->fe_dump();
2963		}
2964		printf("CTL Frontend information end\n");
2965		break;
2966	}
2967	case CTL_LUN_REQ: {
2968		struct ctl_lun_req *lun_req;
2969		struct ctl_backend_driver *backend;
2970
2971		lun_req = (struct ctl_lun_req *)addr;
2972
2973		backend = ctl_backend_find(lun_req->backend);
2974		if (backend == NULL) {
2975			lun_req->status = CTL_LUN_ERROR;
2976			snprintf(lun_req->error_str,
2977				 sizeof(lun_req->error_str),
2978				 "Backend \"%s\" not found.",
2979				 lun_req->backend);
2980			break;
2981		}
2982		if (lun_req->num_be_args > 0) {
2983			lun_req->kern_be_args = ctl_copyin_args(
2984				lun_req->num_be_args,
2985				lun_req->be_args,
2986				lun_req->error_str,
2987				sizeof(lun_req->error_str));
2988			if (lun_req->kern_be_args == NULL) {
2989				lun_req->status = CTL_LUN_ERROR;
2990				break;
2991			}
2992		}
2993
2994		retval = backend->ioctl(dev, cmd, addr, flag, td);
2995
2996		if (lun_req->num_be_args > 0) {
2997			ctl_free_args(lun_req->num_be_args,
2998				      lun_req->kern_be_args);
2999		}
3000		break;
3001	}
3002	case CTL_LUN_LIST: {
3003		struct sbuf *sb;
3004		struct ctl_lun *lun;
3005		struct ctl_lun_list *list;
3006		struct ctl_be_lun_option *opt;
3007
3008		list = (struct ctl_lun_list *)addr;
3009
3010		/*
3011		 * Allocate a fixed length sbuf here, based on the length
3012		 * of the user's buffer.  We could allocate an auto-extending
3013		 * buffer, and then tell the user how much larger our
3014		 * amount of data is than his buffer, but that presents
3015		 * some problems:
3016		 *
3017		 * 1.  The sbuf(9) routines use a blocking malloc, and so
3018		 *     we can't hold a lock while calling them with an
3019		 *     auto-extending buffer.
3020 		 *
3021		 * 2.  There is not currently a LUN reference counting
3022		 *     mechanism, outside of outstanding transactions on
3023		 *     the LUN's OOA queue.  So a LUN could go away on us
3024		 *     while we're getting the LUN number, backend-specific
3025		 *     information, etc.  Thus, given the way things
3026		 *     currently work, we need to hold the CTL lock while
3027		 *     grabbing LUN information.
3028		 *
3029		 * So, from the user's standpoint, the best thing to do is
3030		 * allocate what he thinks is a reasonable buffer length,
3031		 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3032		 * double the buffer length and try again.  (And repeat
3033		 * that until he succeeds.)
3034		 */
3035		sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3036		if (sb == NULL) {
3037			list->status = CTL_LUN_LIST_ERROR;
3038			snprintf(list->error_str, sizeof(list->error_str),
3039				 "Unable to allocate %d bytes for LUN list",
3040				 list->alloc_len);
3041			break;
3042		}
3043
3044		sbuf_printf(sb, "<ctllunlist>\n");
3045
3046		mtx_lock(&softc->ctl_lock);
3047
3048		STAILQ_FOREACH(lun, &softc->lun_list, links) {
3049			retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3050					     (uintmax_t)lun->lun);
3051
3052			/*
3053			 * Bail out as soon as we see that we've overfilled
3054			 * the buffer.
3055			 */
3056			if (retval != 0)
3057				break;
3058
3059			retval = sbuf_printf(sb, "<backend_type>%s"
3060					     "</backend_type>\n",
3061					     (lun->backend == NULL) ?  "none" :
3062					     lun->backend->name);
3063
3064			if (retval != 0)
3065				break;
3066
3067			retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3068					     lun->be_lun->lun_type);
3069
3070			if (retval != 0)
3071				break;
3072
3073			if (lun->backend == NULL) {
3074				retval = sbuf_printf(sb, "</lun>\n");
3075				if (retval != 0)
3076					break;
3077				continue;
3078			}
3079
3080			retval = sbuf_printf(sb, "<size>%ju</size>\n",
3081					     (lun->be_lun->maxlba > 0) ?
3082					     lun->be_lun->maxlba + 1 : 0);
3083
3084			if (retval != 0)
3085				break;
3086
3087			retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3088					     lun->be_lun->blocksize);
3089
3090			if (retval != 0)
3091				break;
3092
3093			retval = sbuf_printf(sb, "<serial_number>");
3094
3095			if (retval != 0)
3096				break;
3097
3098			retval = ctl_sbuf_printf_esc(sb,
3099						     lun->be_lun->serial_num);
3100
3101			if (retval != 0)
3102				break;
3103
3104			retval = sbuf_printf(sb, "</serial_number>\n");
3105
3106			if (retval != 0)
3107				break;
3108
3109			retval = sbuf_printf(sb, "<device_id>");
3110
3111			if (retval != 0)
3112				break;
3113
3114			retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3115
3116			if (retval != 0)
3117				break;
3118
3119			retval = sbuf_printf(sb, "</device_id>\n");
3120
3121			if (retval != 0)
3122				break;
3123
3124			if (lun->backend->lun_info != NULL) {
3125				retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3126				if (retval != 0)
3127					break;
3128			}
3129			STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3130				retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name);
3131				if (retval != 0)
3132					break;
3133			}
3134
3135			retval = sbuf_printf(sb, "</lun>\n");
3136
3137			if (retval != 0)
3138				break;
3139		}
3140		mtx_unlock(&softc->ctl_lock);
3141
3142		if ((retval != 0)
3143		 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3144			retval = 0;
3145			sbuf_delete(sb);
3146			list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3147			snprintf(list->error_str, sizeof(list->error_str),
3148				 "Out of space, %d bytes is too small",
3149				 list->alloc_len);
3150			break;
3151		}
3152
3153		sbuf_finish(sb);
3154
3155		retval = copyout(sbuf_data(sb), list->lun_xml,
3156				 sbuf_len(sb) + 1);
3157
3158		list->fill_len = sbuf_len(sb) + 1;
3159		list->status = CTL_LUN_LIST_OK;
3160		sbuf_delete(sb);
3161		break;
3162	}
3163	case CTL_ISCSI: {
3164		struct ctl_iscsi *ci;
3165		struct ctl_frontend *fe;
3166
3167		ci = (struct ctl_iscsi *)addr;
3168
3169		mtx_lock(&softc->ctl_lock);
3170		STAILQ_FOREACH(fe, &softc->fe_list, links) {
3171			if (strcmp(fe->port_name, "iscsi") == 0)
3172				break;
3173		}
3174		mtx_unlock(&softc->ctl_lock);
3175
3176		if (fe == NULL) {
3177			ci->status = CTL_ISCSI_ERROR;
3178			snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found.");
3179			break;
3180		}
3181
3182		retval = fe->ioctl(dev, cmd, addr, flag, td);
3183		break;
3184	}
3185	default: {
3186		/* XXX KDM should we fix this? */
3187#if 0
3188		struct ctl_backend_driver *backend;
3189		unsigned int type;
3190		int found;
3191
3192		found = 0;
3193
3194		/*
3195		 * We encode the backend type as the ioctl type for backend
3196		 * ioctls.  So parse it out here, and then search for a
3197		 * backend of this type.
3198		 */
3199		type = _IOC_TYPE(cmd);
3200
3201		STAILQ_FOREACH(backend, &softc->be_list, links) {
3202			if (backend->type == type) {
3203				found = 1;
3204				break;
3205			}
3206		}
3207		if (found == 0) {
3208			printf("ctl: unknown ioctl command %#lx or backend "
3209			       "%d\n", cmd, type);
3210			retval = -EINVAL;
3211			break;
3212		}
3213		retval = backend->ioctl(dev, cmd, addr, flag, td);
3214#endif
3215		retval = ENOTTY;
3216		break;
3217	}
3218	}
3219	return (retval);
3220}
3221
3222uint32_t
3223ctl_get_initindex(struct ctl_nexus *nexus)
3224{
3225	if (nexus->targ_port < CTL_MAX_PORTS)
3226		return (nexus->initid.id +
3227			(nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3228	else
3229		return (nexus->initid.id +
3230		       ((nexus->targ_port - CTL_MAX_PORTS) *
3231			CTL_MAX_INIT_PER_PORT));
3232}
3233
3234uint32_t
3235ctl_get_resindex(struct ctl_nexus *nexus)
3236{
3237	return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3238}
3239
3240uint32_t
3241ctl_port_idx(int port_num)
3242{
3243	if (port_num < CTL_MAX_PORTS)
3244		return(port_num);
3245	else
3246		return(port_num - CTL_MAX_PORTS);
3247}
3248
3249/*
3250 * Note:  This only works for bitmask sizes that are at least 32 bits, and
3251 * that are a power of 2.
3252 */
3253int
3254ctl_ffz(uint32_t *mask, uint32_t size)
3255{
3256	uint32_t num_chunks, num_pieces;
3257	int i, j;
3258
3259	num_chunks = (size >> 5);
3260	if (num_chunks == 0)
3261		num_chunks++;
3262	num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3263
3264	for (i = 0; i < num_chunks; i++) {
3265		for (j = 0; j < num_pieces; j++) {
3266			if ((mask[i] & (1 << j)) == 0)
3267				return ((i << 5) + j);
3268		}
3269	}
3270
3271	return (-1);
3272}
3273
3274int
3275ctl_set_mask(uint32_t *mask, uint32_t bit)
3276{
3277	uint32_t chunk, piece;
3278
3279	chunk = bit >> 5;
3280	piece = bit % (sizeof(uint32_t) * 8);
3281
3282	if ((mask[chunk] & (1 << piece)) != 0)
3283		return (-1);
3284	else
3285		mask[chunk] |= (1 << piece);
3286
3287	return (0);
3288}
3289
3290int
3291ctl_clear_mask(uint32_t *mask, uint32_t bit)
3292{
3293	uint32_t chunk, piece;
3294
3295	chunk = bit >> 5;
3296	piece = bit % (sizeof(uint32_t) * 8);
3297
3298	if ((mask[chunk] & (1 << piece)) == 0)
3299		return (-1);
3300	else
3301		mask[chunk] &= ~(1 << piece);
3302
3303	return (0);
3304}
3305
3306int
3307ctl_is_set(uint32_t *mask, uint32_t bit)
3308{
3309	uint32_t chunk, piece;
3310
3311	chunk = bit >> 5;
3312	piece = bit % (sizeof(uint32_t) * 8);
3313
3314	if ((mask[chunk] & (1 << piece)) == 0)
3315		return (0);
3316	else
3317		return (1);
3318}
3319
3320#ifdef unused
3321/*
3322 * The bus, target and lun are optional, they can be filled in later.
3323 * can_wait is used to determine whether we can wait on the malloc or not.
3324 */
3325union ctl_io*
3326ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3327	      uint32_t targ_lun, int can_wait)
3328{
3329	union ctl_io *io;
3330
3331	if (can_wait)
3332		io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3333	else
3334		io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3335
3336	if (io != NULL) {
3337		io->io_hdr.io_type = io_type;
3338		io->io_hdr.targ_port = targ_port;
3339		/*
3340		 * XXX KDM this needs to change/go away.  We need to move
3341		 * to a preallocated pool of ctl_scsiio structures.
3342		 */
3343		io->io_hdr.nexus.targ_target.id = targ_target;
3344		io->io_hdr.nexus.targ_lun = targ_lun;
3345	}
3346
3347	return (io);
3348}
3349
3350void
3351ctl_kfree_io(union ctl_io *io)
3352{
3353	free(io, M_CTL);
3354}
3355#endif /* unused */
3356
3357/*
3358 * ctl_softc, pool_type, total_ctl_io are passed in.
3359 * npool is passed out.
3360 */
3361int
3362ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3363		uint32_t total_ctl_io, struct ctl_io_pool **npool)
3364{
3365	uint32_t i;
3366	union ctl_io *cur_io, *next_io;
3367	struct ctl_io_pool *pool;
3368	int retval;
3369
3370	retval = 0;
3371
3372	pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3373					    M_NOWAIT | M_ZERO);
3374	if (pool == NULL) {
3375		retval = -ENOMEM;
3376		goto bailout;
3377	}
3378
3379	pool->type = pool_type;
3380	pool->ctl_softc = ctl_softc;
3381
3382	mtx_lock(&ctl_softc->pool_lock);
3383	pool->id = ctl_softc->cur_pool_id++;
3384	mtx_unlock(&ctl_softc->pool_lock);
3385
3386	pool->flags = CTL_POOL_FLAG_NONE;
3387	pool->refcount = 1;		/* Reference for validity. */
3388	STAILQ_INIT(&pool->free_queue);
3389
3390	/*
3391	 * XXX KDM other options here:
3392	 * - allocate a page at a time
3393	 * - allocate one big chunk of memory.
3394	 * Page allocation might work well, but would take a little more
3395	 * tracking.
3396	 */
3397	for (i = 0; i < total_ctl_io; i++) {
3398		cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3399						M_NOWAIT);
3400		if (cur_io == NULL) {
3401			retval = ENOMEM;
3402			break;
3403		}
3404		cur_io->io_hdr.pool = pool;
3405		STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3406		pool->total_ctl_io++;
3407		pool->free_ctl_io++;
3408	}
3409
3410	if (retval != 0) {
3411		for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3412		     cur_io != NULL; cur_io = next_io) {
3413			next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3414							      links);
3415			STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3416				      ctl_io_hdr, links);
3417			free(cur_io, M_CTL);
3418		}
3419
3420		free(pool, M_CTL);
3421		goto bailout;
3422	}
3423	mtx_lock(&ctl_softc->pool_lock);
3424	ctl_softc->num_pools++;
3425	STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3426	/*
3427	 * Increment our usage count if this is an external consumer, so we
3428	 * can't get unloaded until the external consumer (most likely a
3429	 * FETD) unloads and frees his pool.
3430	 *
3431	 * XXX KDM will this increment the caller's module use count, or
3432	 * mine?
3433	 */
3434#if 0
3435	if ((pool_type != CTL_POOL_EMERGENCY)
3436	 && (pool_type != CTL_POOL_INTERNAL)
3437	 && (pool_type != CTL_POOL_IOCTL)
3438	 && (pool_type != CTL_POOL_4OTHERSC))
3439		MOD_INC_USE_COUNT;
3440#endif
3441
3442	mtx_unlock(&ctl_softc->pool_lock);
3443
3444	*npool = pool;
3445
3446bailout:
3447
3448	return (retval);
3449}
3450
3451static int
3452ctl_pool_acquire(struct ctl_io_pool *pool)
3453{
3454
3455	mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3456
3457	if (pool->flags & CTL_POOL_FLAG_INVALID)
3458		return (-EINVAL);
3459
3460	pool->refcount++;
3461
3462	return (0);
3463}
3464
3465static void
3466ctl_pool_release(struct ctl_io_pool *pool)
3467{
3468	struct ctl_softc *ctl_softc = pool->ctl_softc;
3469	union ctl_io *io;
3470
3471	mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3472
3473	if (--pool->refcount != 0)
3474		return;
3475
3476	while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3477		STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3478			      links);
3479		free(io, M_CTL);
3480	}
3481
3482	STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3483	ctl_softc->num_pools--;
3484
3485	/*
3486	 * XXX KDM will this decrement the caller's usage count or mine?
3487	 */
3488#if 0
3489	if ((pool->type != CTL_POOL_EMERGENCY)
3490	 && (pool->type != CTL_POOL_INTERNAL)
3491	 && (pool->type != CTL_POOL_IOCTL))
3492		MOD_DEC_USE_COUNT;
3493#endif
3494
3495	free(pool, M_CTL);
3496}
3497
3498void
3499ctl_pool_free(struct ctl_io_pool *pool)
3500{
3501	struct ctl_softc *ctl_softc;
3502
3503	if (pool == NULL)
3504		return;
3505
3506	ctl_softc = pool->ctl_softc;
3507	mtx_lock(&ctl_softc->pool_lock);
3508	pool->flags |= CTL_POOL_FLAG_INVALID;
3509	ctl_pool_release(pool);
3510	mtx_unlock(&ctl_softc->pool_lock);
3511}
3512
3513/*
3514 * This routine does not block (except for spinlocks of course).
3515 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3516 * possible.
3517 */
3518union ctl_io *
3519ctl_alloc_io(void *pool_ref)
3520{
3521	union ctl_io *io;
3522	struct ctl_softc *ctl_softc;
3523	struct ctl_io_pool *pool, *npool;
3524	struct ctl_io_pool *emergency_pool;
3525
3526	pool = (struct ctl_io_pool *)pool_ref;
3527
3528	if (pool == NULL) {
3529		printf("%s: pool is NULL\n", __func__);
3530		return (NULL);
3531	}
3532
3533	emergency_pool = NULL;
3534
3535	ctl_softc = pool->ctl_softc;
3536
3537	mtx_lock(&ctl_softc->pool_lock);
3538	/*
3539	 * First, try to get the io structure from the user's pool.
3540	 */
3541	if (ctl_pool_acquire(pool) == 0) {
3542		io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3543		if (io != NULL) {
3544			STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3545			pool->total_allocated++;
3546			pool->free_ctl_io--;
3547			mtx_unlock(&ctl_softc->pool_lock);
3548			return (io);
3549		} else
3550			ctl_pool_release(pool);
3551	}
3552	/*
3553	 * If he doesn't have any io structures left, search for an
3554	 * emergency pool and grab one from there.
3555	 */
3556	STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3557		if (npool->type != CTL_POOL_EMERGENCY)
3558			continue;
3559
3560		if (ctl_pool_acquire(npool) != 0)
3561			continue;
3562
3563		emergency_pool = npool;
3564
3565		io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3566		if (io != NULL) {
3567			STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3568			npool->total_allocated++;
3569			npool->free_ctl_io--;
3570			mtx_unlock(&ctl_softc->pool_lock);
3571			return (io);
3572		} else
3573			ctl_pool_release(npool);
3574	}
3575
3576	/* Drop the spinlock before we malloc */
3577	mtx_unlock(&ctl_softc->pool_lock);
3578
3579	/*
3580	 * The emergency pool (if it exists) didn't have one, so try an
3581	 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3582	 */
3583	io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3584	if (io != NULL) {
3585		/*
3586		 * If the emergency pool exists but is empty, add this
3587		 * ctl_io to its list when it gets freed.
3588		 */
3589		if (emergency_pool != NULL) {
3590			mtx_lock(&ctl_softc->pool_lock);
3591			if (ctl_pool_acquire(emergency_pool) == 0) {
3592				io->io_hdr.pool = emergency_pool;
3593				emergency_pool->total_ctl_io++;
3594				/*
3595				 * Need to bump this, otherwise
3596				 * total_allocated and total_freed won't
3597				 * match when we no longer have anything
3598				 * outstanding.
3599				 */
3600				emergency_pool->total_allocated++;
3601			}
3602			mtx_unlock(&ctl_softc->pool_lock);
3603		} else
3604			io->io_hdr.pool = NULL;
3605	}
3606
3607	return (io);
3608}
3609
3610void
3611ctl_free_io(union ctl_io *io)
3612{
3613	if (io == NULL)
3614		return;
3615
3616	/*
3617	 * If this ctl_io has a pool, return it to that pool.
3618	 */
3619	if (io->io_hdr.pool != NULL) {
3620		struct ctl_io_pool *pool;
3621#if 0
3622		struct ctl_softc *ctl_softc;
3623		union ctl_io *tmp_io;
3624		unsigned long xflags;
3625		int i;
3626
3627		ctl_softc = control_softc;
3628#endif
3629
3630		pool = (struct ctl_io_pool *)io->io_hdr.pool;
3631
3632		mtx_lock(&pool->ctl_softc->pool_lock);
3633#if 0
3634		save_flags(xflags);
3635
3636		for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3637		     &ctl_softc->task_queue); tmp_io != NULL; i++,
3638		     tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3639		     links)) {
3640			if (tmp_io == io) {
3641				printf("%s: %p is still on the task queue!\n",
3642				       __func__, tmp_io);
3643				printf("%s: (%d): type %d "
3644				       "msg %d cdb %x iptl: "
3645				       "%d:%d:%d:%d tag 0x%04x "
3646				       "flg %#lx\n",
3647					__func__, i,
3648					tmp_io->io_hdr.io_type,
3649					tmp_io->io_hdr.msg_type,
3650					tmp_io->scsiio.cdb[0],
3651					tmp_io->io_hdr.nexus.initid.id,
3652					tmp_io->io_hdr.nexus.targ_port,
3653					tmp_io->io_hdr.nexus.targ_target.id,
3654					tmp_io->io_hdr.nexus.targ_lun,
3655					(tmp_io->io_hdr.io_type ==
3656					CTL_IO_TASK) ?
3657					tmp_io->taskio.tag_num :
3658					tmp_io->scsiio.tag_num,
3659					xflags);
3660				panic("I/O still on the task queue!");
3661			}
3662		}
3663#endif
3664		io->io_hdr.io_type = 0xff;
3665		STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3666		pool->total_freed++;
3667		pool->free_ctl_io++;
3668		ctl_pool_release(pool);
3669		mtx_unlock(&pool->ctl_softc->pool_lock);
3670	} else {
3671		/*
3672		 * Otherwise, just free it.  We probably malloced it and
3673		 * the emergency pool wasn't available.
3674		 */
3675		free(io, M_CTL);
3676	}
3677
3678}
3679
3680void
3681ctl_zero_io(union ctl_io *io)
3682{
3683	void *pool_ref;
3684
3685	if (io == NULL)
3686		return;
3687
3688	/*
3689	 * May need to preserve linked list pointers at some point too.
3690	 */
3691	pool_ref = io->io_hdr.pool;
3692
3693	memset(io, 0, sizeof(*io));
3694
3695	io->io_hdr.pool = pool_ref;
3696}
3697
3698/*
3699 * This routine is currently used for internal copies of ctl_ios that need
3700 * to persist for some reason after we've already returned status to the
3701 * FETD.  (Thus the flag set.)
3702 *
3703 * XXX XXX
3704 * Note that this makes a blind copy of all fields in the ctl_io, except
3705 * for the pool reference.  This includes any memory that has been
3706 * allocated!  That memory will no longer be valid after done has been
3707 * called, so this would be VERY DANGEROUS for command that actually does
3708 * any reads or writes.  Right now (11/7/2005), this is only used for immediate
3709 * start and stop commands, which don't transfer any data, so this is not a
3710 * problem.  If it is used for anything else, the caller would also need to
3711 * allocate data buffer space and this routine would need to be modified to
3712 * copy the data buffer(s) as well.
3713 */
3714void
3715ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3716{
3717	void *pool_ref;
3718
3719	if ((src == NULL)
3720	 || (dest == NULL))
3721		return;
3722
3723	/*
3724	 * May need to preserve linked list pointers at some point too.
3725	 */
3726	pool_ref = dest->io_hdr.pool;
3727
3728	memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3729
3730	dest->io_hdr.pool = pool_ref;
3731	/*
3732	 * We need to know that this is an internal copy, and doesn't need
3733	 * to get passed back to the FETD that allocated it.
3734	 */
3735	dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3736}
3737
3738#ifdef NEEDTOPORT
3739static void
3740ctl_update_power_subpage(struct copan_power_subpage *page)
3741{
3742	int num_luns, num_partitions, config_type;
3743	struct ctl_softc *softc;
3744	cs_BOOL_t aor_present, shelf_50pct_power;
3745	cs_raidset_personality_t rs_type;
3746	int max_active_luns;
3747
3748	softc = control_softc;
3749
3750	/* subtract out the processor LUN */
3751	num_luns = softc->num_luns - 1;
3752	/*
3753	 * Default to 7 LUNs active, which was the only number we allowed
3754	 * in the past.
3755	 */
3756	max_active_luns = 7;
3757
3758	num_partitions = config_GetRsPartitionInfo();
3759	config_type = config_GetConfigType();
3760	shelf_50pct_power = config_GetShelfPowerMode();
3761	aor_present = config_IsAorRsPresent();
3762
3763	rs_type = ddb_GetRsRaidType(1);
3764	if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3765	 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3766		EPRINT(0, "Unsupported RS type %d!", rs_type);
3767	}
3768
3769
3770	page->total_luns = num_luns;
3771
3772	switch (config_type) {
3773	case 40:
3774		/*
3775		 * In a 40 drive configuration, it doesn't matter what DC
3776		 * cards we have, whether we have AOR enabled or not,
3777		 * partitioning or not, or what type of RAIDset we have.
3778		 * In that scenario, we can power up every LUN we present
3779		 * to the user.
3780		 */
3781		max_active_luns = num_luns;
3782
3783		break;
3784	case 64:
3785		if (shelf_50pct_power == CS_FALSE) {
3786			/* 25% power */
3787			if (aor_present == CS_TRUE) {
3788				if (rs_type ==
3789				     CS_RAIDSET_PERSONALITY_RAID5) {
3790					max_active_luns = 7;
3791				} else if (rs_type ==
3792					 CS_RAIDSET_PERSONALITY_RAID1){
3793					max_active_luns = 14;
3794				} else {
3795					/* XXX KDM now what?? */
3796				}
3797			} else {
3798				if (rs_type ==
3799				     CS_RAIDSET_PERSONALITY_RAID5) {
3800					max_active_luns = 8;
3801				} else if (rs_type ==
3802					 CS_RAIDSET_PERSONALITY_RAID1){
3803					max_active_luns = 16;
3804				} else {
3805					/* XXX KDM now what?? */
3806				}
3807			}
3808		} else {
3809			/* 50% power */
3810			/*
3811			 * With 50% power in a 64 drive configuration, we
3812			 * can power all LUNs we present.
3813			 */
3814			max_active_luns = num_luns;
3815		}
3816		break;
3817	case 112:
3818		if (shelf_50pct_power == CS_FALSE) {
3819			/* 25% power */
3820			if (aor_present == CS_TRUE) {
3821				if (rs_type ==
3822				     CS_RAIDSET_PERSONALITY_RAID5) {
3823					max_active_luns = 7;
3824				} else if (rs_type ==
3825					 CS_RAIDSET_PERSONALITY_RAID1){
3826					max_active_luns = 14;
3827				} else {
3828					/* XXX KDM now what?? */
3829				}
3830			} else {
3831				if (rs_type ==
3832				     CS_RAIDSET_PERSONALITY_RAID5) {
3833					max_active_luns = 8;
3834				} else if (rs_type ==
3835					 CS_RAIDSET_PERSONALITY_RAID1){
3836					max_active_luns = 16;
3837				} else {
3838					/* XXX KDM now what?? */
3839				}
3840			}
3841		} else {
3842			/* 50% power */
3843			if (aor_present == CS_TRUE) {
3844				if (rs_type ==
3845				     CS_RAIDSET_PERSONALITY_RAID5) {
3846					max_active_luns = 14;
3847				} else if (rs_type ==
3848					 CS_RAIDSET_PERSONALITY_RAID1){
3849					/*
3850					 * We're assuming here that disk
3851					 * caching is enabled, and so we're
3852					 * able to power up half of each
3853					 * LUN, and cache all writes.
3854					 */
3855					max_active_luns = num_luns;
3856				} else {
3857					/* XXX KDM now what?? */
3858				}
3859			} else {
3860				if (rs_type ==
3861				     CS_RAIDSET_PERSONALITY_RAID5) {
3862					max_active_luns = 15;
3863				} else if (rs_type ==
3864					 CS_RAIDSET_PERSONALITY_RAID1){
3865					max_active_luns = 30;
3866				} else {
3867					/* XXX KDM now what?? */
3868				}
3869			}
3870		}
3871		break;
3872	default:
3873		/*
3874		 * In this case, we have an unknown configuration, so we
3875		 * just use the default from above.
3876		 */
3877		break;
3878	}
3879
3880	page->max_active_luns = max_active_luns;
3881#if 0
3882	printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3883	       page->total_luns, page->max_active_luns);
3884#endif
3885}
3886#endif /* NEEDTOPORT */
3887
3888/*
3889 * This routine could be used in the future to load default and/or saved
3890 * mode page parameters for a particuar lun.
3891 */
3892static int
3893ctl_init_page_index(struct ctl_lun *lun)
3894{
3895	int i;
3896	struct ctl_page_index *page_index;
3897	struct ctl_softc *softc;
3898
3899	memcpy(&lun->mode_pages.index, page_index_template,
3900	       sizeof(page_index_template));
3901
3902	softc = lun->ctl_softc;
3903
3904	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3905
3906		page_index = &lun->mode_pages.index[i];
3907		/*
3908		 * If this is a disk-only mode page, there's no point in
3909		 * setting it up.  For some pages, we have to have some
3910		 * basic information about the disk in order to calculate the
3911		 * mode page data.
3912		 */
3913		if ((lun->be_lun->lun_type != T_DIRECT)
3914		 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3915			continue;
3916
3917		switch (page_index->page_code & SMPH_PC_MASK) {
3918		case SMS_FORMAT_DEVICE_PAGE: {
3919			struct scsi_format_page *format_page;
3920
3921			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3922				panic("subpage is incorrect!");
3923
3924			/*
3925			 * Sectors per track are set above.  Bytes per
3926			 * sector need to be set here on a per-LUN basis.
3927			 */
3928			memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3929			       &format_page_default,
3930			       sizeof(format_page_default));
3931			memcpy(&lun->mode_pages.format_page[
3932			       CTL_PAGE_CHANGEABLE], &format_page_changeable,
3933			       sizeof(format_page_changeable));
3934			memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3935			       &format_page_default,
3936			       sizeof(format_page_default));
3937			memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3938			       &format_page_default,
3939			       sizeof(format_page_default));
3940
3941			format_page = &lun->mode_pages.format_page[
3942				CTL_PAGE_CURRENT];
3943			scsi_ulto2b(lun->be_lun->blocksize,
3944				    format_page->bytes_per_sector);
3945
3946			format_page = &lun->mode_pages.format_page[
3947				CTL_PAGE_DEFAULT];
3948			scsi_ulto2b(lun->be_lun->blocksize,
3949				    format_page->bytes_per_sector);
3950
3951			format_page = &lun->mode_pages.format_page[
3952				CTL_PAGE_SAVED];
3953			scsi_ulto2b(lun->be_lun->blocksize,
3954				    format_page->bytes_per_sector);
3955
3956			page_index->page_data =
3957				(uint8_t *)lun->mode_pages.format_page;
3958			break;
3959		}
3960		case SMS_RIGID_DISK_PAGE: {
3961			struct scsi_rigid_disk_page *rigid_disk_page;
3962			uint32_t sectors_per_cylinder;
3963			uint64_t cylinders;
3964#ifndef	__XSCALE__
3965			int shift;
3966#endif /* !__XSCALE__ */
3967
3968			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3969				panic("invalid subpage value %d",
3970				      page_index->subpage);
3971
3972			/*
3973			 * Rotation rate and sectors per track are set
3974			 * above.  We calculate the cylinders here based on
3975			 * capacity.  Due to the number of heads and
3976			 * sectors per track we're using, smaller arrays
3977			 * may turn out to have 0 cylinders.  Linux and
3978			 * FreeBSD don't pay attention to these mode pages
3979			 * to figure out capacity, but Solaris does.  It
3980			 * seems to deal with 0 cylinders just fine, and
3981			 * works out a fake geometry based on the capacity.
3982			 */
3983			memcpy(&lun->mode_pages.rigid_disk_page[
3984			       CTL_PAGE_CURRENT], &rigid_disk_page_default,
3985			       sizeof(rigid_disk_page_default));
3986			memcpy(&lun->mode_pages.rigid_disk_page[
3987			       CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3988			       sizeof(rigid_disk_page_changeable));
3989			memcpy(&lun->mode_pages.rigid_disk_page[
3990			       CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3991			       sizeof(rigid_disk_page_default));
3992			memcpy(&lun->mode_pages.rigid_disk_page[
3993			       CTL_PAGE_SAVED], &rigid_disk_page_default,
3994			       sizeof(rigid_disk_page_default));
3995
3996			sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3997				CTL_DEFAULT_HEADS;
3998
3999			/*
4000			 * The divide method here will be more accurate,
4001			 * probably, but results in floating point being
4002			 * used in the kernel on i386 (__udivdi3()).  On the
4003			 * XScale, though, __udivdi3() is implemented in
4004			 * software.
4005			 *
4006			 * The shift method for cylinder calculation is
4007			 * accurate if sectors_per_cylinder is a power of
4008			 * 2.  Otherwise it might be slightly off -- you
4009			 * might have a bit of a truncation problem.
4010			 */
4011#ifdef	__XSCALE__
4012			cylinders = (lun->be_lun->maxlba + 1) /
4013				sectors_per_cylinder;
4014#else
4015			for (shift = 31; shift > 0; shift--) {
4016				if (sectors_per_cylinder & (1 << shift))
4017					break;
4018			}
4019			cylinders = (lun->be_lun->maxlba + 1) >> shift;
4020#endif
4021
4022			/*
4023			 * We've basically got 3 bytes, or 24 bits for the
4024			 * cylinder size in the mode page.  If we're over,
4025			 * just round down to 2^24.
4026			 */
4027			if (cylinders > 0xffffff)
4028				cylinders = 0xffffff;
4029
4030			rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4031				CTL_PAGE_CURRENT];
4032			scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4033
4034			rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4035				CTL_PAGE_DEFAULT];
4036			scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4037
4038			rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4039				CTL_PAGE_SAVED];
4040			scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4041
4042			page_index->page_data =
4043				(uint8_t *)lun->mode_pages.rigid_disk_page;
4044			break;
4045		}
4046		case SMS_CACHING_PAGE: {
4047
4048			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4049				panic("invalid subpage value %d",
4050				      page_index->subpage);
4051			/*
4052			 * Defaults should be okay here, no calculations
4053			 * needed.
4054			 */
4055			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4056			       &caching_page_default,
4057			       sizeof(caching_page_default));
4058			memcpy(&lun->mode_pages.caching_page[
4059			       CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4060			       sizeof(caching_page_changeable));
4061			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4062			       &caching_page_default,
4063			       sizeof(caching_page_default));
4064			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4065			       &caching_page_default,
4066			       sizeof(caching_page_default));
4067			page_index->page_data =
4068				(uint8_t *)lun->mode_pages.caching_page;
4069			break;
4070		}
4071		case SMS_CONTROL_MODE_PAGE: {
4072
4073			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4074				panic("invalid subpage value %d",
4075				      page_index->subpage);
4076
4077			/*
4078			 * Defaults should be okay here, no calculations
4079			 * needed.
4080			 */
4081			memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4082			       &control_page_default,
4083			       sizeof(control_page_default));
4084			memcpy(&lun->mode_pages.control_page[
4085			       CTL_PAGE_CHANGEABLE], &control_page_changeable,
4086			       sizeof(control_page_changeable));
4087			memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4088			       &control_page_default,
4089			       sizeof(control_page_default));
4090			memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4091			       &control_page_default,
4092			       sizeof(control_page_default));
4093			page_index->page_data =
4094				(uint8_t *)lun->mode_pages.control_page;
4095			break;
4096
4097		}
4098		case SMS_VENDOR_SPECIFIC_PAGE:{
4099			switch (page_index->subpage) {
4100			case PWR_SUBPAGE_CODE: {
4101				struct copan_power_subpage *current_page,
4102							   *saved_page;
4103
4104				memcpy(&lun->mode_pages.power_subpage[
4105				       CTL_PAGE_CURRENT],
4106				       &power_page_default,
4107				       sizeof(power_page_default));
4108				memcpy(&lun->mode_pages.power_subpage[
4109				       CTL_PAGE_CHANGEABLE],
4110				       &power_page_changeable,
4111				       sizeof(power_page_changeable));
4112				memcpy(&lun->mode_pages.power_subpage[
4113				       CTL_PAGE_DEFAULT],
4114				       &power_page_default,
4115				       sizeof(power_page_default));
4116				memcpy(&lun->mode_pages.power_subpage[
4117				       CTL_PAGE_SAVED],
4118				       &power_page_default,
4119				       sizeof(power_page_default));
4120				page_index->page_data =
4121				    (uint8_t *)lun->mode_pages.power_subpage;
4122
4123				current_page = (struct copan_power_subpage *)
4124					(page_index->page_data +
4125					 (page_index->page_len *
4126					  CTL_PAGE_CURRENT));
4127			        saved_page = (struct copan_power_subpage *)
4128				        (page_index->page_data +
4129					 (page_index->page_len *
4130					  CTL_PAGE_SAVED));
4131				break;
4132			}
4133			case APS_SUBPAGE_CODE: {
4134				struct copan_aps_subpage *current_page,
4135							 *saved_page;
4136
4137				// This gets set multiple times but
4138				// it should always be the same. It's
4139				// only done during init so who cares.
4140				index_to_aps_page = i;
4141
4142				memcpy(&lun->mode_pages.aps_subpage[
4143				       CTL_PAGE_CURRENT],
4144				       &aps_page_default,
4145				       sizeof(aps_page_default));
4146				memcpy(&lun->mode_pages.aps_subpage[
4147				       CTL_PAGE_CHANGEABLE],
4148				       &aps_page_changeable,
4149				       sizeof(aps_page_changeable));
4150				memcpy(&lun->mode_pages.aps_subpage[
4151				       CTL_PAGE_DEFAULT],
4152				       &aps_page_default,
4153				       sizeof(aps_page_default));
4154				memcpy(&lun->mode_pages.aps_subpage[
4155				       CTL_PAGE_SAVED],
4156				       &aps_page_default,
4157				       sizeof(aps_page_default));
4158				page_index->page_data =
4159					(uint8_t *)lun->mode_pages.aps_subpage;
4160
4161				current_page = (struct copan_aps_subpage *)
4162					(page_index->page_data +
4163					 (page_index->page_len *
4164					  CTL_PAGE_CURRENT));
4165				saved_page = (struct copan_aps_subpage *)
4166					(page_index->page_data +
4167					 (page_index->page_len *
4168					  CTL_PAGE_SAVED));
4169				break;
4170			}
4171			case DBGCNF_SUBPAGE_CODE: {
4172				struct copan_debugconf_subpage *current_page,
4173							       *saved_page;
4174
4175				memcpy(&lun->mode_pages.debugconf_subpage[
4176				       CTL_PAGE_CURRENT],
4177				       &debugconf_page_default,
4178				       sizeof(debugconf_page_default));
4179				memcpy(&lun->mode_pages.debugconf_subpage[
4180				       CTL_PAGE_CHANGEABLE],
4181				       &debugconf_page_changeable,
4182				       sizeof(debugconf_page_changeable));
4183				memcpy(&lun->mode_pages.debugconf_subpage[
4184				       CTL_PAGE_DEFAULT],
4185				       &debugconf_page_default,
4186				       sizeof(debugconf_page_default));
4187				memcpy(&lun->mode_pages.debugconf_subpage[
4188				       CTL_PAGE_SAVED],
4189				       &debugconf_page_default,
4190				       sizeof(debugconf_page_default));
4191				page_index->page_data =
4192					(uint8_t *)lun->mode_pages.debugconf_subpage;
4193
4194				current_page = (struct copan_debugconf_subpage *)
4195					(page_index->page_data +
4196					 (page_index->page_len *
4197					  CTL_PAGE_CURRENT));
4198				saved_page = (struct copan_debugconf_subpage *)
4199					(page_index->page_data +
4200					 (page_index->page_len *
4201					  CTL_PAGE_SAVED));
4202				break;
4203			}
4204			default:
4205				panic("invalid subpage value %d",
4206				      page_index->subpage);
4207				break;
4208			}
4209   			break;
4210		}
4211		default:
4212			panic("invalid page value %d",
4213			      page_index->page_code & SMPH_PC_MASK);
4214			break;
4215    	}
4216	}
4217
4218	return (CTL_RETVAL_COMPLETE);
4219}
4220
4221/*
4222 * LUN allocation.
4223 *
4224 * Requirements:
4225 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4226 *   wants us to allocate the LUN and he can block.
4227 * - ctl_softc is always set
4228 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4229 *
4230 * Returns 0 for success, non-zero (errno) for failure.
4231 */
4232static int
4233ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4234	      struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4235{
4236	struct ctl_lun *nlun, *lun;
4237	struct ctl_frontend *fe;
4238	int lun_number, i, lun_malloced;
4239
4240	if (be_lun == NULL)
4241		return (EINVAL);
4242
4243	/*
4244	 * We currently only support Direct Access or Processor LUN types.
4245	 */
4246	switch (be_lun->lun_type) {
4247	case T_DIRECT:
4248		break;
4249	case T_PROCESSOR:
4250		break;
4251	case T_SEQUENTIAL:
4252	case T_CHANGER:
4253	default:
4254		be_lun->lun_config_status(be_lun->be_lun,
4255					  CTL_LUN_CONFIG_FAILURE);
4256		break;
4257	}
4258	if (ctl_lun == NULL) {
4259		lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4260		lun_malloced = 1;
4261	} else {
4262		lun_malloced = 0;
4263		lun = ctl_lun;
4264	}
4265
4266	memset(lun, 0, sizeof(*lun));
4267	if (lun_malloced)
4268		lun->flags = CTL_LUN_MALLOCED;
4269
4270	mtx_lock(&ctl_softc->ctl_lock);
4271	/*
4272	 * See if the caller requested a particular LUN number.  If so, see
4273	 * if it is available.  Otherwise, allocate the first available LUN.
4274	 */
4275	if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4276		if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4277		 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4278			mtx_unlock(&ctl_softc->ctl_lock);
4279			if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4280				printf("ctl: requested LUN ID %d is higher "
4281				       "than CTL_MAX_LUNS - 1 (%d)\n",
4282				       be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4283			} else {
4284				/*
4285				 * XXX KDM return an error, or just assign
4286				 * another LUN ID in this case??
4287				 */
4288				printf("ctl: requested LUN ID %d is already "
4289				       "in use\n", be_lun->req_lun_id);
4290			}
4291			if (lun->flags & CTL_LUN_MALLOCED)
4292				free(lun, M_CTL);
4293			be_lun->lun_config_status(be_lun->be_lun,
4294						  CTL_LUN_CONFIG_FAILURE);
4295			return (ENOSPC);
4296		}
4297		lun_number = be_lun->req_lun_id;
4298	} else {
4299		lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4300		if (lun_number == -1) {
4301			mtx_unlock(&ctl_softc->ctl_lock);
4302			printf("ctl: can't allocate LUN on target %ju, out of "
4303			       "LUNs\n", (uintmax_t)target_id.id);
4304			if (lun->flags & CTL_LUN_MALLOCED)
4305				free(lun, M_CTL);
4306			be_lun->lun_config_status(be_lun->be_lun,
4307						  CTL_LUN_CONFIG_FAILURE);
4308			return (ENOSPC);
4309		}
4310	}
4311	ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4312
4313	lun->target = target_id;
4314	lun->lun = lun_number;
4315	lun->be_lun = be_lun;
4316	/*
4317	 * The processor LUN is always enabled.  Disk LUNs come on line
4318	 * disabled, and must be enabled by the backend.
4319	 */
4320	lun->flags |= CTL_LUN_DISABLED;
4321	lun->backend = be_lun->be;
4322	be_lun->ctl_lun = lun;
4323	be_lun->lun_id = lun_number;
4324	atomic_add_int(&be_lun->be->num_luns, 1);
4325	if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4326		lun->flags |= CTL_LUN_STOPPED;
4327
4328	if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4329		lun->flags |= CTL_LUN_INOPERABLE;
4330
4331	if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4332		lun->flags |= CTL_LUN_PRIMARY_SC;
4333
4334	lun->ctl_softc = ctl_softc;
4335	TAILQ_INIT(&lun->ooa_queue);
4336	TAILQ_INIT(&lun->blocked_queue);
4337	STAILQ_INIT(&lun->error_list);
4338
4339	/*
4340	 * Initialize the mode page index.
4341	 */
4342	ctl_init_page_index(lun);
4343
4344	/*
4345	 * Set the poweron UA for all initiators on this LUN only.
4346	 */
4347	for (i = 0; i < CTL_MAX_INITIATORS; i++)
4348		lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4349
4350	/*
4351	 * Now, before we insert this lun on the lun list, set the lun
4352	 * inventory changed UA for all other luns.
4353	 */
4354	STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4355		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4356			nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4357		}
4358	}
4359
4360	STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4361
4362	ctl_softc->ctl_luns[lun_number] = lun;
4363
4364	ctl_softc->num_luns++;
4365
4366	/* Setup statistics gathering */
4367	lun->stats.device_type = be_lun->lun_type;
4368	lun->stats.lun_number = lun_number;
4369	if (lun->stats.device_type == T_DIRECT)
4370		lun->stats.blocksize = be_lun->blocksize;
4371	else
4372		lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4373	for (i = 0;i < CTL_MAX_PORTS;i++)
4374		lun->stats.ports[i].targ_port = i;
4375
4376	mtx_unlock(&ctl_softc->ctl_lock);
4377
4378	lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4379
4380	/*
4381	 * Run through each registered FETD and bring it online if it isn't
4382	 * already.  Enable the target ID if it hasn't been enabled, and
4383	 * enable this particular LUN.
4384	 */
4385	STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4386		int retval;
4387
4388		/*
4389		 * XXX KDM this only works for ONE TARGET ID.  We'll need
4390		 * to do things differently if we go to a multiple target
4391		 * ID scheme.
4392		 */
4393		if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4394
4395			retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4396			if (retval != 0) {
4397				printf("ctl_alloc_lun: FETD %s port %d "
4398				       "returned error %d for targ_enable on "
4399				       "target %ju\n", fe->port_name,
4400				       fe->targ_port, retval,
4401				       (uintmax_t)target_id.id);
4402			} else
4403				fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4404		}
4405
4406		retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4407		if (retval != 0) {
4408			printf("ctl_alloc_lun: FETD %s port %d returned error "
4409			       "%d for lun_enable on target %ju lun %d\n",
4410			       fe->port_name, fe->targ_port, retval,
4411			       (uintmax_t)target_id.id, lun_number);
4412		} else
4413			fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4414	}
4415	return (0);
4416}
4417
4418/*
4419 * Delete a LUN.
4420 * Assumptions:
4421 * - LUN has already been marked invalid and any pending I/O has been taken
4422 *   care of.
4423 */
4424static int
4425ctl_free_lun(struct ctl_lun *lun)
4426{
4427	struct ctl_softc *softc;
4428#if 0
4429	struct ctl_frontend *fe;
4430#endif
4431	struct ctl_lun *nlun;
4432	union ctl_io *io, *next_io;
4433	int i;
4434
4435	softc = lun->ctl_softc;
4436
4437	mtx_assert(&softc->ctl_lock, MA_OWNED);
4438
4439	STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4440
4441	ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4442
4443	softc->ctl_luns[lun->lun] = NULL;
4444
4445	if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4446		printf("ctl_free_lun: aieee!! freeing a LUN with "
4447		       "outstanding I/O!!\n");
4448	}
4449
4450	/*
4451	 * If we have anything pending on the RtR queue, remove it.
4452	 */
4453	for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4454	     io = next_io) {
4455		uint32_t targ_lun;
4456
4457		next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4458		targ_lun = io->io_hdr.nexus.targ_lun;
4459		if (io->io_hdr.nexus.lun_map_fn != NULL)
4460			targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
4461		if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4462		 && (targ_lun == lun->lun))
4463			STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4464				      ctl_io_hdr, links);
4465	}
4466
4467	/*
4468	 * Then remove everything from the blocked queue.
4469	 */
4470	for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4471	     io = next_io) {
4472		next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4473		TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4474		io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4475	}
4476
4477	/*
4478	 * Now clear out the OOA queue, and free all the I/O.
4479	 * XXX KDM should we notify the FETD here?  We probably need to
4480	 * quiesce the LUN before deleting it.
4481	 */
4482	for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4483	     io = next_io) {
4484		next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4485		TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4486		ctl_free_io(io);
4487	}
4488
4489	softc->num_luns--;
4490
4491	/*
4492	 * XXX KDM this scheme only works for a single target/multiple LUN
4493	 * setup.  It needs to be revamped for a multiple target scheme.
4494	 *
4495	 * XXX KDM this results in fe->lun_disable() getting called twice,
4496	 * once when ctl_disable_lun() is called, and a second time here.
4497	 * We really need to re-think the LUN disable semantics.  There
4498	 * should probably be several steps/levels to LUN removal:
4499	 *  - disable
4500	 *  - invalidate
4501	 *  - free
4502 	 *
4503	 * Right now we only have a disable method when communicating to
4504	 * the front end ports, at least for individual LUNs.
4505	 */
4506#if 0
4507	STAILQ_FOREACH(fe, &softc->fe_list, links) {
4508		int retval;
4509
4510		retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4511					 lun->lun);
4512		if (retval != 0) {
4513			printf("ctl_free_lun: FETD %s port %d returned error "
4514			       "%d for lun_disable on target %ju lun %jd\n",
4515			       fe->port_name, fe->targ_port, retval,
4516			       (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4517		}
4518
4519		if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4520			fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4521
4522			retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4523			if (retval != 0) {
4524				printf("ctl_free_lun: FETD %s port %d "
4525				       "returned error %d for targ_disable on "
4526				       "target %ju\n", fe->port_name,
4527				       fe->targ_port, retval,
4528				       (uintmax_t)lun->target.id);
4529			} else
4530				fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4531
4532			if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4533				continue;
4534
4535#if 0
4536			fe->port_offline(fe->onoff_arg);
4537			fe->status &= ~CTL_PORT_STATUS_ONLINE;
4538#endif
4539		}
4540	}
4541#endif
4542
4543	/*
4544	 * Tell the backend to free resources, if this LUN has a backend.
4545	 */
4546	atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4547	lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4548
4549	if (lun->flags & CTL_LUN_MALLOCED)
4550		free(lun, M_CTL);
4551
4552	STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4553		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4554			nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4555		}
4556	}
4557
4558	return (0);
4559}
4560
4561static void
4562ctl_create_lun(struct ctl_be_lun *be_lun)
4563{
4564	struct ctl_softc *ctl_softc;
4565
4566	ctl_softc = control_softc;
4567
4568	/*
4569	 * ctl_alloc_lun() should handle all potential failure cases.
4570	 */
4571	ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4572}
4573
4574int
4575ctl_add_lun(struct ctl_be_lun *be_lun)
4576{
4577	struct ctl_softc *ctl_softc;
4578
4579	ctl_softc = control_softc;
4580
4581	mtx_lock(&ctl_softc->ctl_lock);
4582	STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4583	mtx_unlock(&ctl_softc->ctl_lock);
4584
4585	ctl_wakeup_thread();
4586
4587	return (0);
4588}
4589
4590int
4591ctl_enable_lun(struct ctl_be_lun *be_lun)
4592{
4593	struct ctl_softc *ctl_softc;
4594	struct ctl_frontend *fe, *nfe;
4595	struct ctl_lun *lun;
4596	int retval;
4597
4598	ctl_softc = control_softc;
4599
4600	lun = (struct ctl_lun *)be_lun->ctl_lun;
4601
4602	mtx_lock(&ctl_softc->ctl_lock);
4603	if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4604		/*
4605		 * eh?  Why did we get called if the LUN is already
4606		 * enabled?
4607		 */
4608		mtx_unlock(&ctl_softc->ctl_lock);
4609		return (0);
4610	}
4611	lun->flags &= ~CTL_LUN_DISABLED;
4612
4613	for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4614		nfe = STAILQ_NEXT(fe, links);
4615
4616		/*
4617		 * Drop the lock while we call the FETD's enable routine.
4618		 * This can lead to a callback into CTL (at least in the
4619		 * case of the internal initiator frontend.
4620		 */
4621		mtx_unlock(&ctl_softc->ctl_lock);
4622		retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4623		mtx_lock(&ctl_softc->ctl_lock);
4624		if (retval != 0) {
4625			printf("%s: FETD %s port %d returned error "
4626			       "%d for lun_enable on target %ju lun %jd\n",
4627			       __func__, fe->port_name, fe->targ_port, retval,
4628			       (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4629		}
4630#if 0
4631		 else {
4632            /* NOTE:  TODO:  why does lun enable affect port status? */
4633			fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4634		}
4635#endif
4636	}
4637
4638	mtx_unlock(&ctl_softc->ctl_lock);
4639
4640	return (0);
4641}
4642
4643int
4644ctl_disable_lun(struct ctl_be_lun *be_lun)
4645{
4646	struct ctl_softc *ctl_softc;
4647	struct ctl_frontend *fe;
4648	struct ctl_lun *lun;
4649	int retval;
4650
4651	ctl_softc = control_softc;
4652
4653	lun = (struct ctl_lun *)be_lun->ctl_lun;
4654
4655	mtx_lock(&ctl_softc->ctl_lock);
4656
4657	if (lun->flags & CTL_LUN_DISABLED) {
4658		mtx_unlock(&ctl_softc->ctl_lock);
4659		return (0);
4660	}
4661	lun->flags |= CTL_LUN_DISABLED;
4662
4663	STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4664		mtx_unlock(&ctl_softc->ctl_lock);
4665		/*
4666		 * Drop the lock before we call the frontend's disable
4667		 * routine, to avoid lock order reversals.
4668		 *
4669		 * XXX KDM what happens if the frontend list changes while
4670		 * we're traversing it?  It's unlikely, but should be handled.
4671		 */
4672		retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4673					 lun->lun);
4674		mtx_lock(&ctl_softc->ctl_lock);
4675		if (retval != 0) {
4676			printf("ctl_alloc_lun: FETD %s port %d returned error "
4677			       "%d for lun_disable on target %ju lun %jd\n",
4678			       fe->port_name, fe->targ_port, retval,
4679			       (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4680		}
4681	}
4682
4683	mtx_unlock(&ctl_softc->ctl_lock);
4684
4685	return (0);
4686}
4687
4688int
4689ctl_start_lun(struct ctl_be_lun *be_lun)
4690{
4691	struct ctl_softc *ctl_softc;
4692	struct ctl_lun *lun;
4693
4694	ctl_softc = control_softc;
4695
4696	lun = (struct ctl_lun *)be_lun->ctl_lun;
4697
4698	mtx_lock(&ctl_softc->ctl_lock);
4699	lun->flags &= ~CTL_LUN_STOPPED;
4700	mtx_unlock(&ctl_softc->ctl_lock);
4701
4702	return (0);
4703}
4704
4705int
4706ctl_stop_lun(struct ctl_be_lun *be_lun)
4707{
4708	struct ctl_softc *ctl_softc;
4709	struct ctl_lun *lun;
4710
4711	ctl_softc = control_softc;
4712
4713	lun = (struct ctl_lun *)be_lun->ctl_lun;
4714
4715	mtx_lock(&ctl_softc->ctl_lock);
4716	lun->flags |= CTL_LUN_STOPPED;
4717	mtx_unlock(&ctl_softc->ctl_lock);
4718
4719	return (0);
4720}
4721
4722int
4723ctl_lun_offline(struct ctl_be_lun *be_lun)
4724{
4725	struct ctl_softc *ctl_softc;
4726	struct ctl_lun *lun;
4727
4728	ctl_softc = control_softc;
4729
4730	lun = (struct ctl_lun *)be_lun->ctl_lun;
4731
4732	mtx_lock(&ctl_softc->ctl_lock);
4733	lun->flags |= CTL_LUN_OFFLINE;
4734	mtx_unlock(&ctl_softc->ctl_lock);
4735
4736	return (0);
4737}
4738
4739int
4740ctl_lun_online(struct ctl_be_lun *be_lun)
4741{
4742	struct ctl_softc *ctl_softc;
4743	struct ctl_lun *lun;
4744
4745	ctl_softc = control_softc;
4746
4747	lun = (struct ctl_lun *)be_lun->ctl_lun;
4748
4749	mtx_lock(&ctl_softc->ctl_lock);
4750	lun->flags &= ~CTL_LUN_OFFLINE;
4751	mtx_unlock(&ctl_softc->ctl_lock);
4752
4753	return (0);
4754}
4755
4756int
4757ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4758{
4759	struct ctl_softc *ctl_softc;
4760	struct ctl_lun *lun;
4761
4762	ctl_softc = control_softc;
4763
4764	lun = (struct ctl_lun *)be_lun->ctl_lun;
4765
4766	mtx_lock(&ctl_softc->ctl_lock);
4767
4768	/*
4769	 * The LUN needs to be disabled before it can be marked invalid.
4770	 */
4771	if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4772		mtx_unlock(&ctl_softc->ctl_lock);
4773		return (-1);
4774	}
4775	/*
4776	 * Mark the LUN invalid.
4777	 */
4778	lun->flags |= CTL_LUN_INVALID;
4779
4780	/*
4781	 * If there is nothing in the OOA queue, go ahead and free the LUN.
4782	 * If we have something in the OOA queue, we'll free it when the
4783	 * last I/O completes.
4784	 */
4785	if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4786		ctl_free_lun(lun);
4787	mtx_unlock(&ctl_softc->ctl_lock);
4788
4789	return (0);
4790}
4791
4792int
4793ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4794{
4795	struct ctl_softc *ctl_softc;
4796	struct ctl_lun *lun;
4797
4798	ctl_softc = control_softc;
4799	lun = (struct ctl_lun *)be_lun->ctl_lun;
4800
4801	mtx_lock(&ctl_softc->ctl_lock);
4802	lun->flags |= CTL_LUN_INOPERABLE;
4803	mtx_unlock(&ctl_softc->ctl_lock);
4804
4805	return (0);
4806}
4807
4808int
4809ctl_lun_operable(struct ctl_be_lun *be_lun)
4810{
4811	struct ctl_softc *ctl_softc;
4812	struct ctl_lun *lun;
4813
4814	ctl_softc = control_softc;
4815	lun = (struct ctl_lun *)be_lun->ctl_lun;
4816
4817	mtx_lock(&ctl_softc->ctl_lock);
4818	lun->flags &= ~CTL_LUN_INOPERABLE;
4819	mtx_unlock(&ctl_softc->ctl_lock);
4820
4821	return (0);
4822}
4823
4824int
4825ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4826		   int lock)
4827{
4828	struct ctl_softc *softc;
4829	struct ctl_lun *lun;
4830	struct copan_aps_subpage *current_sp;
4831	struct ctl_page_index *page_index;
4832	int i;
4833
4834	softc = control_softc;
4835
4836	mtx_lock(&softc->ctl_lock);
4837
4838	lun = (struct ctl_lun *)be_lun->ctl_lun;
4839
4840	page_index = NULL;
4841	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4842		if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4843		     APS_PAGE_CODE)
4844			continue;
4845
4846		if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4847			continue;
4848		page_index = &lun->mode_pages.index[i];
4849	}
4850
4851	if (page_index == NULL) {
4852		mtx_unlock(&softc->ctl_lock);
4853		printf("%s: APS subpage not found for lun %ju!\n", __func__,
4854		       (uintmax_t)lun->lun);
4855		return (1);
4856	}
4857#if 0
4858	if ((softc->aps_locked_lun != 0)
4859	 && (softc->aps_locked_lun != lun->lun)) {
4860		printf("%s: attempt to lock LUN %llu when %llu is already "
4861		       "locked\n");
4862		mtx_unlock(&softc->ctl_lock);
4863		return (1);
4864	}
4865#endif
4866
4867	current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4868		(page_index->page_len * CTL_PAGE_CURRENT));
4869
4870	if (lock != 0) {
4871		current_sp->lock_active = APS_LOCK_ACTIVE;
4872		softc->aps_locked_lun = lun->lun;
4873	} else {
4874		current_sp->lock_active = 0;
4875		softc->aps_locked_lun = 0;
4876	}
4877
4878
4879	/*
4880	 * If we're in HA mode, try to send the lock message to the other
4881	 * side.
4882	 */
4883	if (ctl_is_single == 0) {
4884		int isc_retval;
4885		union ctl_ha_msg lock_msg;
4886
4887		lock_msg.hdr.nexus = *nexus;
4888		lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4889		if (lock != 0)
4890			lock_msg.aps.lock_flag = 1;
4891		else
4892			lock_msg.aps.lock_flag = 0;
4893		isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4894					 sizeof(lock_msg), 0);
4895		if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4896			printf("%s: APS (lock=%d) error returned from "
4897			       "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4898			mtx_unlock(&softc->ctl_lock);
4899			return (1);
4900		}
4901	}
4902
4903	mtx_unlock(&softc->ctl_lock);
4904
4905	return (0);
4906}
4907
4908void
4909ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4910{
4911	struct ctl_lun *lun;
4912	struct ctl_softc *softc;
4913	int i;
4914
4915	softc = control_softc;
4916
4917	mtx_lock(&softc->ctl_lock);
4918
4919	lun = (struct ctl_lun *)be_lun->ctl_lun;
4920
4921	for (i = 0; i < CTL_MAX_INITIATORS; i++)
4922		lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4923
4924	mtx_unlock(&softc->ctl_lock);
4925}
4926
4927/*
4928 * Backend "memory move is complete" callback for requests that never
4929 * make it down to say RAIDCore's configuration code.
4930 */
4931int
4932ctl_config_move_done(union ctl_io *io)
4933{
4934	int retval;
4935
4936	retval = CTL_RETVAL_COMPLETE;
4937
4938
4939	CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4940	/*
4941	 * XXX KDM this shouldn't happen, but what if it does?
4942	 */
4943	if (io->io_hdr.io_type != CTL_IO_SCSI)
4944		panic("I/O type isn't CTL_IO_SCSI!");
4945
4946	if ((io->io_hdr.port_status == 0)
4947	 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4948	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4949		io->io_hdr.status = CTL_SUCCESS;
4950	else if ((io->io_hdr.port_status != 0)
4951	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4952	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4953		/*
4954		 * For hardware error sense keys, the sense key
4955		 * specific value is defined to be a retry count,
4956		 * but we use it to pass back an internal FETD
4957		 * error code.  XXX KDM  Hopefully the FETD is only
4958		 * using 16 bits for an error code, since that's
4959		 * all the space we have in the sks field.
4960		 */
4961		ctl_set_internal_failure(&io->scsiio,
4962					 /*sks_valid*/ 1,
4963					 /*retry_count*/
4964					 io->io_hdr.port_status);
4965		free(io->scsiio.kern_data_ptr, M_CTL);
4966		ctl_done(io);
4967		goto bailout;
4968	}
4969
4970	if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4971	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4972	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4973		/*
4974		 * XXX KDM just assuming a single pointer here, and not a
4975		 * S/G list.  If we start using S/G lists for config data,
4976		 * we'll need to know how to clean them up here as well.
4977		 */
4978		free(io->scsiio.kern_data_ptr, M_CTL);
4979		/* Hopefully the user has already set the status... */
4980		ctl_done(io);
4981	} else {
4982		/*
4983		 * XXX KDM now we need to continue data movement.  Some
4984		 * options:
4985		 * - call ctl_scsiio() again?  We don't do this for data
4986		 *   writes, because for those at least we know ahead of
4987		 *   time where the write will go and how long it is.  For
4988		 *   config writes, though, that information is largely
4989		 *   contained within the write itself, thus we need to
4990		 *   parse out the data again.
4991		 *
4992		 * - Call some other function once the data is in?
4993		 */
4994
4995		/*
4996		 * XXX KDM call ctl_scsiio() again for now, and check flag
4997		 * bits to see whether we're allocated or not.
4998		 */
4999		retval = ctl_scsiio(&io->scsiio);
5000	}
5001bailout:
5002	return (retval);
5003}
5004
5005/*
5006 * This gets called by a backend driver when it is done with a
5007 * configuration write.
5008 */
5009void
5010ctl_config_write_done(union ctl_io *io)
5011{
5012	/*
5013	 * If the IO_CONT flag is set, we need to call the supplied
5014	 * function to continue processing the I/O, instead of completing
5015	 * the I/O just yet.
5016	 *
5017	 * If there is an error, though, we don't want to keep processing.
5018	 * Instead, just send status back to the initiator.
5019	 */
5020	if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5021	 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5022	  || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5023		io->scsiio.io_cont(io);
5024		return;
5025	}
5026	/*
5027	 * Since a configuration write can be done for commands that actually
5028	 * have data allocated, like write buffer, and commands that have
5029	 * no data, like start/stop unit, we need to check here.
5030	 */
5031	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5032		free(io->scsiio.kern_data_ptr, M_CTL);
5033	ctl_done(io);
5034}
5035
5036/*
5037 * SCSI release command.
5038 */
5039int
5040ctl_scsi_release(struct ctl_scsiio *ctsio)
5041{
5042	int length, longid, thirdparty_id, resv_id;
5043	struct ctl_softc *ctl_softc;
5044	struct ctl_lun *lun;
5045
5046	length = 0;
5047	resv_id = 0;
5048
5049	CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5050
5051	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5052	ctl_softc = control_softc;
5053
5054	switch (ctsio->cdb[0]) {
5055	case RELEASE: {
5056		struct scsi_release *cdb;
5057
5058		cdb = (struct scsi_release *)ctsio->cdb;
5059		if ((cdb->byte2 & 0x1f) != 0) {
5060			ctl_set_invalid_field(ctsio,
5061					      /*sks_valid*/ 1,
5062					      /*command*/ 1,
5063					      /*field*/ 1,
5064					      /*bit_valid*/ 0,
5065					      /*bit*/ 0);
5066			ctl_done((union ctl_io *)ctsio);
5067			return (CTL_RETVAL_COMPLETE);
5068		}
5069		break;
5070	}
5071	case RELEASE_10: {
5072		struct scsi_release_10 *cdb;
5073
5074		cdb = (struct scsi_release_10 *)ctsio->cdb;
5075
5076		if ((cdb->byte2 & SR10_EXTENT) != 0) {
5077			ctl_set_invalid_field(ctsio,
5078					      /*sks_valid*/ 1,
5079					      /*command*/ 1,
5080					      /*field*/ 1,
5081					      /*bit_valid*/ 1,
5082					      /*bit*/ 0);
5083			ctl_done((union ctl_io *)ctsio);
5084			return (CTL_RETVAL_COMPLETE);
5085
5086		}
5087
5088		if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5089			ctl_set_invalid_field(ctsio,
5090					      /*sks_valid*/ 1,
5091					      /*command*/ 1,
5092					      /*field*/ 1,
5093					      /*bit_valid*/ 1,
5094					      /*bit*/ 4);
5095			ctl_done((union ctl_io *)ctsio);
5096			return (CTL_RETVAL_COMPLETE);
5097		}
5098
5099		if (cdb->byte2 & SR10_LONGID)
5100			longid = 1;
5101		else
5102			thirdparty_id = cdb->thirdparty_id;
5103
5104		resv_id = cdb->resv_id;
5105		length = scsi_2btoul(cdb->length);
5106		break;
5107	}
5108	}
5109
5110
5111	/*
5112	 * XXX KDM right now, we only support LUN reservation.  We don't
5113	 * support 3rd party reservations, or extent reservations, which
5114	 * might actually need the parameter list.  If we've gotten this
5115	 * far, we've got a LUN reservation.  Anything else got kicked out
5116	 * above.  So, according to SPC, ignore the length.
5117	 */
5118	length = 0;
5119
5120	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5121	 && (length > 0)) {
5122		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5123		ctsio->kern_data_len = length;
5124		ctsio->kern_total_len = length;
5125		ctsio->kern_data_resid = 0;
5126		ctsio->kern_rel_offset = 0;
5127		ctsio->kern_sg_entries = 0;
5128		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5129		ctsio->be_move_done = ctl_config_move_done;
5130		ctl_datamove((union ctl_io *)ctsio);
5131
5132		return (CTL_RETVAL_COMPLETE);
5133	}
5134
5135	if (length > 0)
5136		thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5137
5138	mtx_lock(&ctl_softc->ctl_lock);
5139
5140	/*
5141	 * According to SPC, it is not an error for an intiator to attempt
5142	 * to release a reservation on a LUN that isn't reserved, or that
5143	 * is reserved by another initiator.  The reservation can only be
5144	 * released, though, by the initiator who made it or by one of
5145	 * several reset type events.
5146	 */
5147	if (lun->flags & CTL_LUN_RESERVED) {
5148		if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5149		 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5150		 && (ctsio->io_hdr.nexus.targ_target.id ==
5151		     lun->rsv_nexus.targ_target.id)) {
5152			lun->flags &= ~CTL_LUN_RESERVED;
5153		}
5154	}
5155
5156	ctsio->scsi_status = SCSI_STATUS_OK;
5157	ctsio->io_hdr.status = CTL_SUCCESS;
5158
5159	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5160		free(ctsio->kern_data_ptr, M_CTL);
5161		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5162	}
5163
5164	mtx_unlock(&ctl_softc->ctl_lock);
5165
5166	ctl_done((union ctl_io *)ctsio);
5167	return (CTL_RETVAL_COMPLETE);
5168}
5169
5170int
5171ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5172{
5173	int extent, thirdparty, longid;
5174	int resv_id, length;
5175	uint64_t thirdparty_id;
5176	struct ctl_softc *ctl_softc;
5177	struct ctl_lun *lun;
5178
5179	extent = 0;
5180	thirdparty = 0;
5181	longid = 0;
5182	resv_id = 0;
5183	length = 0;
5184	thirdparty_id = 0;
5185
5186	CTL_DEBUG_PRINT(("ctl_reserve\n"));
5187
5188	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5189	ctl_softc = control_softc;
5190
5191	switch (ctsio->cdb[0]) {
5192	case RESERVE: {
5193		struct scsi_reserve *cdb;
5194
5195		cdb = (struct scsi_reserve *)ctsio->cdb;
5196		if ((cdb->byte2 & 0x1f) != 0) {
5197			ctl_set_invalid_field(ctsio,
5198					      /*sks_valid*/ 1,
5199					      /*command*/ 1,
5200					      /*field*/ 1,
5201					      /*bit_valid*/ 0,
5202					      /*bit*/ 0);
5203			ctl_done((union ctl_io *)ctsio);
5204			return (CTL_RETVAL_COMPLETE);
5205		}
5206		resv_id = cdb->resv_id;
5207		length = scsi_2btoul(cdb->length);
5208		break;
5209	}
5210	case RESERVE_10: {
5211		struct scsi_reserve_10 *cdb;
5212
5213		cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5214
5215		if ((cdb->byte2 & SR10_EXTENT) != 0) {
5216			ctl_set_invalid_field(ctsio,
5217					      /*sks_valid*/ 1,
5218					      /*command*/ 1,
5219					      /*field*/ 1,
5220					      /*bit_valid*/ 1,
5221					      /*bit*/ 0);
5222			ctl_done((union ctl_io *)ctsio);
5223			return (CTL_RETVAL_COMPLETE);
5224		}
5225		if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5226			ctl_set_invalid_field(ctsio,
5227					      /*sks_valid*/ 1,
5228					      /*command*/ 1,
5229					      /*field*/ 1,
5230					      /*bit_valid*/ 1,
5231					      /*bit*/ 4);
5232			ctl_done((union ctl_io *)ctsio);
5233			return (CTL_RETVAL_COMPLETE);
5234		}
5235		if (cdb->byte2 & SR10_LONGID)
5236			longid = 1;
5237		else
5238			thirdparty_id = cdb->thirdparty_id;
5239
5240		resv_id = cdb->resv_id;
5241		length = scsi_2btoul(cdb->length);
5242		break;
5243	}
5244	}
5245
5246	/*
5247	 * XXX KDM right now, we only support LUN reservation.  We don't
5248	 * support 3rd party reservations, or extent reservations, which
5249	 * might actually need the parameter list.  If we've gotten this
5250	 * far, we've got a LUN reservation.  Anything else got kicked out
5251	 * above.  So, according to SPC, ignore the length.
5252	 */
5253	length = 0;
5254
5255	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5256	 && (length > 0)) {
5257		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5258		ctsio->kern_data_len = length;
5259		ctsio->kern_total_len = length;
5260		ctsio->kern_data_resid = 0;
5261		ctsio->kern_rel_offset = 0;
5262		ctsio->kern_sg_entries = 0;
5263		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5264		ctsio->be_move_done = ctl_config_move_done;
5265		ctl_datamove((union ctl_io *)ctsio);
5266
5267		return (CTL_RETVAL_COMPLETE);
5268	}
5269
5270	if (length > 0)
5271		thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5272
5273	mtx_lock(&ctl_softc->ctl_lock);
5274	if (lun->flags & CTL_LUN_RESERVED) {
5275		if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5276		 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5277		 || (ctsio->io_hdr.nexus.targ_target.id !=
5278		     lun->rsv_nexus.targ_target.id)) {
5279			ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5280			ctsio->io_hdr.status = CTL_SCSI_ERROR;
5281			goto bailout;
5282		}
5283	}
5284
5285	lun->flags |= CTL_LUN_RESERVED;
5286	lun->rsv_nexus = ctsio->io_hdr.nexus;
5287
5288	ctsio->scsi_status = SCSI_STATUS_OK;
5289	ctsio->io_hdr.status = CTL_SUCCESS;
5290
5291bailout:
5292	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5293		free(ctsio->kern_data_ptr, M_CTL);
5294		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5295	}
5296
5297	mtx_unlock(&ctl_softc->ctl_lock);
5298
5299	ctl_done((union ctl_io *)ctsio);
5300	return (CTL_RETVAL_COMPLETE);
5301}
5302
5303int
5304ctl_start_stop(struct ctl_scsiio *ctsio)
5305{
5306	struct scsi_start_stop_unit *cdb;
5307	struct ctl_lun *lun;
5308	struct ctl_softc *ctl_softc;
5309	int retval;
5310
5311	CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5312
5313	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5314	ctl_softc = control_softc;
5315	retval = 0;
5316
5317	cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5318
5319	/*
5320	 * XXX KDM
5321	 * We don't support the immediate bit on a stop unit.  In order to
5322	 * do that, we would need to code up a way to know that a stop is
5323	 * pending, and hold off any new commands until it completes, one
5324	 * way or another.  Then we could accept or reject those commands
5325	 * depending on its status.  We would almost need to do the reverse
5326	 * of what we do below for an immediate start -- return the copy of
5327	 * the ctl_io to the FETD with status to send to the host (and to
5328	 * free the copy!) and then free the original I/O once the stop
5329	 * actually completes.  That way, the OOA queue mechanism can work
5330	 * to block commands that shouldn't proceed.  Another alternative
5331	 * would be to put the copy in the queue in place of the original,
5332	 * and return the original back to the caller.  That could be
5333	 * slightly safer..
5334	 */
5335	if ((cdb->byte2 & SSS_IMMED)
5336	 && ((cdb->how & SSS_START) == 0)) {
5337		ctl_set_invalid_field(ctsio,
5338				      /*sks_valid*/ 1,
5339				      /*command*/ 1,
5340				      /*field*/ 1,
5341				      /*bit_valid*/ 1,
5342				      /*bit*/ 0);
5343		ctl_done((union ctl_io *)ctsio);
5344		return (CTL_RETVAL_COMPLETE);
5345	}
5346
5347	/*
5348	 * We don't support the power conditions field.  We need to check
5349	 * this prior to checking the load/eject and start/stop bits.
5350	 */
5351	if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5352		ctl_set_invalid_field(ctsio,
5353				      /*sks_valid*/ 1,
5354				      /*command*/ 1,
5355				      /*field*/ 4,
5356				      /*bit_valid*/ 1,
5357				      /*bit*/ 4);
5358		ctl_done((union ctl_io *)ctsio);
5359		return (CTL_RETVAL_COMPLETE);
5360	}
5361
5362	/*
5363	 * Media isn't removable, so we can't load or eject it.
5364	 */
5365	if ((cdb->how & SSS_LOEJ) != 0) {
5366		ctl_set_invalid_field(ctsio,
5367				      /*sks_valid*/ 1,
5368				      /*command*/ 1,
5369				      /*field*/ 4,
5370				      /*bit_valid*/ 1,
5371				      /*bit*/ 1);
5372		ctl_done((union ctl_io *)ctsio);
5373		return (CTL_RETVAL_COMPLETE);
5374	}
5375
5376	if ((lun->flags & CTL_LUN_PR_RESERVED)
5377	 && ((cdb->how & SSS_START)==0)) {
5378		uint32_t residx;
5379
5380		residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5381		if (!lun->per_res[residx].registered
5382		 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5383
5384			ctl_set_reservation_conflict(ctsio);
5385			ctl_done((union ctl_io *)ctsio);
5386			return (CTL_RETVAL_COMPLETE);
5387		}
5388	}
5389
5390	/*
5391	 * If there is no backend on this device, we can't start or stop
5392	 * it.  In theory we shouldn't get any start/stop commands in the
5393	 * first place at this level if the LUN doesn't have a backend.
5394	 * That should get stopped by the command decode code.
5395	 */
5396	if (lun->backend == NULL) {
5397		ctl_set_invalid_opcode(ctsio);
5398		ctl_done((union ctl_io *)ctsio);
5399		return (CTL_RETVAL_COMPLETE);
5400	}
5401
5402	/*
5403	 * XXX KDM Copan-specific offline behavior.
5404	 * Figure out a reasonable way to port this?
5405	 */
5406#ifdef NEEDTOPORT
5407	mtx_lock(&ctl_softc->ctl_lock);
5408
5409	if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5410	 && (lun->flags & CTL_LUN_OFFLINE)) {
5411		/*
5412		 * If the LUN is offline, and the on/offline bit isn't set,
5413		 * reject the start or stop.  Otherwise, let it through.
5414		 */
5415		mtx_unlock(&ctl_softc->ctl_lock);
5416		ctl_set_lun_not_ready(ctsio);
5417		ctl_done((union ctl_io *)ctsio);
5418	} else {
5419		mtx_unlock(&ctl_softc->ctl_lock);
5420#endif /* NEEDTOPORT */
5421		/*
5422		 * This could be a start or a stop when we're online,
5423		 * or a stop/offline or start/online.  A start or stop when
5424		 * we're offline is covered in the case above.
5425		 */
5426		/*
5427		 * In the non-immediate case, we send the request to
5428		 * the backend and return status to the user when
5429		 * it is done.
5430		 *
5431		 * In the immediate case, we allocate a new ctl_io
5432		 * to hold a copy of the request, and send that to
5433		 * the backend.  We then set good status on the
5434		 * user's request and return it immediately.
5435		 */
5436		if (cdb->byte2 & SSS_IMMED) {
5437			union ctl_io *new_io;
5438
5439			new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5440			if (new_io == NULL) {
5441				ctl_set_busy(ctsio);
5442				ctl_done((union ctl_io *)ctsio);
5443			} else {
5444				ctl_copy_io((union ctl_io *)ctsio,
5445					    new_io);
5446				retval = lun->backend->config_write(new_io);
5447				ctl_set_success(ctsio);
5448				ctl_done((union ctl_io *)ctsio);
5449			}
5450		} else {
5451			retval = lun->backend->config_write(
5452				(union ctl_io *)ctsio);
5453		}
5454#ifdef NEEDTOPORT
5455	}
5456#endif
5457	return (retval);
5458}
5459
5460/*
5461 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5462 * we don't really do anything with the LBA and length fields if the user
5463 * passes them in.  Instead we'll just flush out the cache for the entire
5464 * LUN.
5465 */
5466int
5467ctl_sync_cache(struct ctl_scsiio *ctsio)
5468{
5469	struct ctl_lun *lun;
5470	struct ctl_softc *ctl_softc;
5471	uint64_t starting_lba;
5472	uint32_t block_count;
5473	int reladr, immed;
5474	int retval;
5475
5476	CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5477
5478	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5479	ctl_softc = control_softc;
5480	retval = 0;
5481	reladr = 0;
5482	immed = 0;
5483
5484	switch (ctsio->cdb[0]) {
5485	case SYNCHRONIZE_CACHE: {
5486		struct scsi_sync_cache *cdb;
5487		cdb = (struct scsi_sync_cache *)ctsio->cdb;
5488
5489		if (cdb->byte2 & SSC_RELADR)
5490			reladr = 1;
5491
5492		if (cdb->byte2 & SSC_IMMED)
5493			immed = 1;
5494
5495		starting_lba = scsi_4btoul(cdb->begin_lba);
5496		block_count = scsi_2btoul(cdb->lb_count);
5497		break;
5498	}
5499	case SYNCHRONIZE_CACHE_16: {
5500		struct scsi_sync_cache_16 *cdb;
5501		cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5502
5503		if (cdb->byte2 & SSC_RELADR)
5504			reladr = 1;
5505
5506		if (cdb->byte2 & SSC_IMMED)
5507			immed = 1;
5508
5509		starting_lba = scsi_8btou64(cdb->begin_lba);
5510		block_count = scsi_4btoul(cdb->lb_count);
5511		break;
5512	}
5513	default:
5514		ctl_set_invalid_opcode(ctsio);
5515		ctl_done((union ctl_io *)ctsio);
5516		goto bailout;
5517		break; /* NOTREACHED */
5518	}
5519
5520	if (immed) {
5521		/*
5522		 * We don't support the immediate bit.  Since it's in the
5523		 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5524		 * commands, we can just return the same error in either
5525		 * case.
5526		 */
5527		ctl_set_invalid_field(ctsio,
5528				      /*sks_valid*/ 1,
5529				      /*command*/ 1,
5530				      /*field*/ 1,
5531				      /*bit_valid*/ 1,
5532				      /*bit*/ 1);
5533		ctl_done((union ctl_io *)ctsio);
5534		goto bailout;
5535	}
5536
5537	if (reladr) {
5538		/*
5539		 * We don't support the reladr bit either.  It can only be
5540		 * used with linked commands, and we don't support linked
5541		 * commands.  Since the bit is in the same place for the
5542		 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5543		 * just return the same error in either case.
5544		 */
5545		ctl_set_invalid_field(ctsio,
5546				      /*sks_valid*/ 1,
5547				      /*command*/ 1,
5548				      /*field*/ 1,
5549				      /*bit_valid*/ 1,
5550				      /*bit*/ 0);
5551		ctl_done((union ctl_io *)ctsio);
5552		goto bailout;
5553	}
5554
5555	/*
5556	 * We check the LBA and length, but don't do anything with them.
5557	 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5558	 * get flushed.  This check will just help satisfy anyone who wants
5559	 * to see an error for an out of range LBA.
5560	 */
5561	if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5562		ctl_set_lba_out_of_range(ctsio);
5563		ctl_done((union ctl_io *)ctsio);
5564		goto bailout;
5565	}
5566
5567	/*
5568	 * If this LUN has no backend, we can't flush the cache anyway.
5569	 */
5570	if (lun->backend == NULL) {
5571		ctl_set_invalid_opcode(ctsio);
5572		ctl_done((union ctl_io *)ctsio);
5573		goto bailout;
5574	}
5575
5576	/*
5577	 * Check to see whether we're configured to send the SYNCHRONIZE
5578	 * CACHE command directly to the back end.
5579	 */
5580	mtx_lock(&ctl_softc->ctl_lock);
5581	if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5582	 && (++(lun->sync_count) >= lun->sync_interval)) {
5583		lun->sync_count = 0;
5584		mtx_unlock(&ctl_softc->ctl_lock);
5585		retval = lun->backend->config_write((union ctl_io *)ctsio);
5586	} else {
5587		mtx_unlock(&ctl_softc->ctl_lock);
5588		ctl_set_success(ctsio);
5589		ctl_done((union ctl_io *)ctsio);
5590	}
5591
5592bailout:
5593
5594	return (retval);
5595}
5596
5597int
5598ctl_format(struct ctl_scsiio *ctsio)
5599{
5600	struct scsi_format *cdb;
5601	struct ctl_lun *lun;
5602	struct ctl_softc *ctl_softc;
5603	int length, defect_list_len;
5604
5605	CTL_DEBUG_PRINT(("ctl_format\n"));
5606
5607	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5608	ctl_softc = control_softc;
5609
5610	cdb = (struct scsi_format *)ctsio->cdb;
5611
5612	length = 0;
5613	if (cdb->byte2 & SF_FMTDATA) {
5614		if (cdb->byte2 & SF_LONGLIST)
5615			length = sizeof(struct scsi_format_header_long);
5616		else
5617			length = sizeof(struct scsi_format_header_short);
5618	}
5619
5620	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5621	 && (length > 0)) {
5622		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5623		ctsio->kern_data_len = length;
5624		ctsio->kern_total_len = length;
5625		ctsio->kern_data_resid = 0;
5626		ctsio->kern_rel_offset = 0;
5627		ctsio->kern_sg_entries = 0;
5628		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5629		ctsio->be_move_done = ctl_config_move_done;
5630		ctl_datamove((union ctl_io *)ctsio);
5631
5632		return (CTL_RETVAL_COMPLETE);
5633	}
5634
5635	defect_list_len = 0;
5636
5637	if (cdb->byte2 & SF_FMTDATA) {
5638		if (cdb->byte2 & SF_LONGLIST) {
5639			struct scsi_format_header_long *header;
5640
5641			header = (struct scsi_format_header_long *)
5642				ctsio->kern_data_ptr;
5643
5644			defect_list_len = scsi_4btoul(header->defect_list_len);
5645			if (defect_list_len != 0) {
5646				ctl_set_invalid_field(ctsio,
5647						      /*sks_valid*/ 1,
5648						      /*command*/ 0,
5649						      /*field*/ 2,
5650						      /*bit_valid*/ 0,
5651						      /*bit*/ 0);
5652				goto bailout;
5653			}
5654		} else {
5655			struct scsi_format_header_short *header;
5656
5657			header = (struct scsi_format_header_short *)
5658				ctsio->kern_data_ptr;
5659
5660			defect_list_len = scsi_2btoul(header->defect_list_len);
5661			if (defect_list_len != 0) {
5662				ctl_set_invalid_field(ctsio,
5663						      /*sks_valid*/ 1,
5664						      /*command*/ 0,
5665						      /*field*/ 2,
5666						      /*bit_valid*/ 0,
5667						      /*bit*/ 0);
5668				goto bailout;
5669			}
5670		}
5671	}
5672
5673	/*
5674	 * The format command will clear out the "Medium format corrupted"
5675	 * status if set by the configuration code.  That status is really
5676	 * just a way to notify the host that we have lost the media, and
5677	 * get them to issue a command that will basically make them think
5678	 * they're blowing away the media.
5679	 */
5680	mtx_lock(&ctl_softc->ctl_lock);
5681	lun->flags &= ~CTL_LUN_INOPERABLE;
5682	mtx_unlock(&ctl_softc->ctl_lock);
5683
5684	ctsio->scsi_status = SCSI_STATUS_OK;
5685	ctsio->io_hdr.status = CTL_SUCCESS;
5686bailout:
5687
5688	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5689		free(ctsio->kern_data_ptr, M_CTL);
5690		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5691	}
5692
5693	ctl_done((union ctl_io *)ctsio);
5694	return (CTL_RETVAL_COMPLETE);
5695}
5696
5697int
5698ctl_write_buffer(struct ctl_scsiio *ctsio)
5699{
5700	struct scsi_write_buffer *cdb;
5701	struct copan_page_header *header;
5702	struct ctl_lun *lun;
5703	struct ctl_softc *ctl_softc;
5704	int buffer_offset, len;
5705	int retval;
5706
5707	header = NULL;
5708
5709	retval = CTL_RETVAL_COMPLETE;
5710
5711	CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5712
5713	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5714	ctl_softc = control_softc;
5715	cdb = (struct scsi_write_buffer *)ctsio->cdb;
5716
5717	if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5718		ctl_set_invalid_field(ctsio,
5719				      /*sks_valid*/ 1,
5720				      /*command*/ 1,
5721				      /*field*/ 1,
5722				      /*bit_valid*/ 1,
5723				      /*bit*/ 4);
5724		ctl_done((union ctl_io *)ctsio);
5725		return (CTL_RETVAL_COMPLETE);
5726	}
5727	if (cdb->buffer_id != 0) {
5728		ctl_set_invalid_field(ctsio,
5729				      /*sks_valid*/ 1,
5730				      /*command*/ 1,
5731				      /*field*/ 2,
5732				      /*bit_valid*/ 0,
5733				      /*bit*/ 0);
5734		ctl_done((union ctl_io *)ctsio);
5735		return (CTL_RETVAL_COMPLETE);
5736	}
5737
5738	len = scsi_3btoul(cdb->length);
5739	buffer_offset = scsi_3btoul(cdb->offset);
5740
5741	if (len > sizeof(lun->write_buffer)) {
5742		ctl_set_invalid_field(ctsio,
5743				      /*sks_valid*/ 1,
5744				      /*command*/ 1,
5745				      /*field*/ 6,
5746				      /*bit_valid*/ 0,
5747				      /*bit*/ 0);
5748		ctl_done((union ctl_io *)ctsio);
5749		return (CTL_RETVAL_COMPLETE);
5750	}
5751
5752	if (buffer_offset != 0) {
5753		ctl_set_invalid_field(ctsio,
5754				      /*sks_valid*/ 1,
5755				      /*command*/ 1,
5756				      /*field*/ 3,
5757				      /*bit_valid*/ 0,
5758				      /*bit*/ 0);
5759		ctl_done((union ctl_io *)ctsio);
5760		return (CTL_RETVAL_COMPLETE);
5761	}
5762
5763	/*
5764	 * If we've got a kernel request that hasn't been malloced yet,
5765	 * malloc it and tell the caller the data buffer is here.
5766	 */
5767	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5768		ctsio->kern_data_ptr = lun->write_buffer;
5769		ctsio->kern_data_len = len;
5770		ctsio->kern_total_len = len;
5771		ctsio->kern_data_resid = 0;
5772		ctsio->kern_rel_offset = 0;
5773		ctsio->kern_sg_entries = 0;
5774		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5775		ctsio->be_move_done = ctl_config_move_done;
5776		ctl_datamove((union ctl_io *)ctsio);
5777
5778		return (CTL_RETVAL_COMPLETE);
5779	}
5780
5781	ctl_done((union ctl_io *)ctsio);
5782
5783	return (CTL_RETVAL_COMPLETE);
5784}
5785
5786/*
5787 * Note that this function currently doesn't actually do anything inside
5788 * CTL to enforce things if the DQue bit is turned on.
5789 *
5790 * Also note that this function can't be used in the default case, because
5791 * the DQue bit isn't set in the changeable mask for the control mode page
5792 * anyway.  This is just here as an example for how to implement a page
5793 * handler, and a placeholder in case we want to allow the user to turn
5794 * tagged queueing on and off.
5795 *
5796 * The D_SENSE bit handling is functional, however, and will turn
5797 * descriptor sense on and off for a given LUN.
5798 */
5799int
5800ctl_control_page_handler(struct ctl_scsiio *ctsio,
5801			 struct ctl_page_index *page_index, uint8_t *page_ptr)
5802{
5803	struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5804	struct ctl_lun *lun;
5805	struct ctl_softc *softc;
5806	int set_ua;
5807	uint32_t initidx;
5808
5809	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5810	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5811	set_ua = 0;
5812
5813	user_cp = (struct scsi_control_page *)page_ptr;
5814	current_cp = (struct scsi_control_page *)
5815		(page_index->page_data + (page_index->page_len *
5816		CTL_PAGE_CURRENT));
5817	saved_cp = (struct scsi_control_page *)
5818		(page_index->page_data + (page_index->page_len *
5819		CTL_PAGE_SAVED));
5820
5821	softc = control_softc;
5822
5823	mtx_lock(&softc->ctl_lock);
5824	if (((current_cp->rlec & SCP_DSENSE) == 0)
5825	 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5826		/*
5827		 * Descriptor sense is currently turned off and the user
5828		 * wants to turn it on.
5829		 */
5830		current_cp->rlec |= SCP_DSENSE;
5831		saved_cp->rlec |= SCP_DSENSE;
5832		lun->flags |= CTL_LUN_SENSE_DESC;
5833		set_ua = 1;
5834	} else if (((current_cp->rlec & SCP_DSENSE) != 0)
5835		&& ((user_cp->rlec & SCP_DSENSE) == 0)) {
5836		/*
5837		 * Descriptor sense is currently turned on, and the user
5838		 * wants to turn it off.
5839		 */
5840		current_cp->rlec &= ~SCP_DSENSE;
5841		saved_cp->rlec &= ~SCP_DSENSE;
5842		lun->flags &= ~CTL_LUN_SENSE_DESC;
5843		set_ua = 1;
5844	}
5845	if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5846		if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5847#ifdef NEEDTOPORT
5848			csevent_log(CSC_CTL | CSC_SHELF_SW |
5849				    CTL_UNTAG_TO_UNTAG,
5850				    csevent_LogType_Trace,
5851				    csevent_Severity_Information,
5852				    csevent_AlertLevel_Green,
5853				    csevent_FRU_Firmware,
5854				    csevent_FRU_Unknown,
5855				    "Received untagged to untagged transition");
5856#endif /* NEEDTOPORT */
5857		} else {
5858#ifdef NEEDTOPORT
5859			csevent_log(CSC_CTL | CSC_SHELF_SW |
5860				    CTL_UNTAG_TO_TAG,
5861				    csevent_LogType_ConfigChange,
5862				    csevent_Severity_Information,
5863				    csevent_AlertLevel_Green,
5864				    csevent_FRU_Firmware,
5865				    csevent_FRU_Unknown,
5866				    "Received untagged to tagged "
5867				    "queueing transition");
5868#endif /* NEEDTOPORT */
5869
5870			current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5871			saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5872			set_ua = 1;
5873		}
5874	} else {
5875		if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5876#ifdef NEEDTOPORT
5877			csevent_log(CSC_CTL | CSC_SHELF_SW |
5878				    CTL_TAG_TO_UNTAG,
5879				    csevent_LogType_ConfigChange,
5880				    csevent_Severity_Warning,
5881				    csevent_AlertLevel_Yellow,
5882				    csevent_FRU_Firmware,
5883				    csevent_FRU_Unknown,
5884				    "Received tagged queueing to untagged "
5885				    "transition");
5886#endif /* NEEDTOPORT */
5887
5888			current_cp->queue_flags |= SCP_QUEUE_DQUE;
5889			saved_cp->queue_flags |= SCP_QUEUE_DQUE;
5890			set_ua = 1;
5891		} else {
5892#ifdef NEEDTOPORT
5893			csevent_log(CSC_CTL | CSC_SHELF_SW |
5894				    CTL_TAG_TO_TAG,
5895				    csevent_LogType_Trace,
5896				    csevent_Severity_Information,
5897				    csevent_AlertLevel_Green,
5898				    csevent_FRU_Firmware,
5899				    csevent_FRU_Unknown,
5900				    "Received tagged queueing to tagged "
5901				    "queueing transition");
5902#endif /* NEEDTOPORT */
5903		}
5904	}
5905	if (set_ua != 0) {
5906		int i;
5907		/*
5908		 * Let other initiators know that the mode
5909		 * parameters for this LUN have changed.
5910		 */
5911		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
5912			if (i == initidx)
5913				continue;
5914
5915			lun->pending_sense[i].ua_pending |=
5916				CTL_UA_MODE_CHANGE;
5917		}
5918	}
5919	mtx_unlock(&softc->ctl_lock);
5920
5921	return (0);
5922}
5923
5924int
5925ctl_power_sp_handler(struct ctl_scsiio *ctsio,
5926		     struct ctl_page_index *page_index, uint8_t *page_ptr)
5927{
5928	return (0);
5929}
5930
5931int
5932ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
5933			   struct ctl_page_index *page_index, int pc)
5934{
5935	struct copan_power_subpage *page;
5936
5937	page = (struct copan_power_subpage *)page_index->page_data +
5938		(page_index->page_len * pc);
5939
5940	switch (pc) {
5941	case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5942		/*
5943		 * We don't update the changable bits for this page.
5944		 */
5945		break;
5946	case SMS_PAGE_CTRL_CURRENT >> 6:
5947	case SMS_PAGE_CTRL_DEFAULT >> 6:
5948	case SMS_PAGE_CTRL_SAVED >> 6:
5949#ifdef NEEDTOPORT
5950		ctl_update_power_subpage(page);
5951#endif
5952		break;
5953	default:
5954#ifdef NEEDTOPORT
5955		EPRINT(0, "Invalid PC %d!!", pc);
5956#endif
5957		break;
5958	}
5959	return (0);
5960}
5961
5962
5963int
5964ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
5965		   struct ctl_page_index *page_index, uint8_t *page_ptr)
5966{
5967	struct copan_aps_subpage *user_sp;
5968	struct copan_aps_subpage *current_sp;
5969	union ctl_modepage_info *modepage_info;
5970	struct ctl_softc *softc;
5971	struct ctl_lun *lun;
5972	int retval;
5973
5974	retval = CTL_RETVAL_COMPLETE;
5975	current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5976		     (page_index->page_len * CTL_PAGE_CURRENT));
5977	softc = control_softc;
5978	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5979
5980	user_sp = (struct copan_aps_subpage *)page_ptr;
5981
5982	modepage_info = (union ctl_modepage_info *)
5983		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5984
5985	modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
5986	modepage_info->header.subpage = page_index->subpage;
5987	modepage_info->aps.lock_active = user_sp->lock_active;
5988
5989	mtx_lock(&softc->ctl_lock);
5990
5991	/*
5992	 * If there is a request to lock the LUN and another LUN is locked
5993	 * this is an error. If the requested LUN is already locked ignore
5994	 * the request. If no LUN is locked attempt to lock it.
5995	 * if there is a request to unlock the LUN and the LUN is currently
5996	 * locked attempt to unlock it. Otherwise ignore the request. i.e.
5997	 * if another LUN is locked or no LUN is locked.
5998	 */
5999	if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6000		if (softc->aps_locked_lun == lun->lun) {
6001			/*
6002			 * This LUN is already locked, so we're done.
6003			 */
6004			retval = CTL_RETVAL_COMPLETE;
6005		} else if (softc->aps_locked_lun == 0) {
6006			/*
6007			 * No one has the lock, pass the request to the
6008			 * backend.
6009			 */
6010			retval = lun->backend->config_write(
6011				(union ctl_io *)ctsio);
6012		} else {
6013			/*
6014			 * Someone else has the lock, throw out the request.
6015			 */
6016			ctl_set_already_locked(ctsio);
6017			free(ctsio->kern_data_ptr, M_CTL);
6018			ctl_done((union ctl_io *)ctsio);
6019
6020			/*
6021			 * Set the return value so that ctl_do_mode_select()
6022			 * won't try to complete the command.  We already
6023			 * completed it here.
6024			 */
6025			retval = CTL_RETVAL_ERROR;
6026		}
6027	} else if (softc->aps_locked_lun == lun->lun) {
6028		/*
6029		 * This LUN is locked, so pass the unlock request to the
6030		 * backend.
6031		 */
6032		retval = lun->backend->config_write((union ctl_io *)ctsio);
6033	}
6034	mtx_unlock(&softc->ctl_lock);
6035
6036	return (retval);
6037}
6038
6039int
6040ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6041				struct ctl_page_index *page_index,
6042				uint8_t *page_ptr)
6043{
6044	uint8_t *c;
6045	int i;
6046
6047	c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6048	ctl_time_io_secs =
6049		(c[0] << 8) |
6050		(c[1] << 0) |
6051		0;
6052	CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6053	printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6054	printf("page data:");
6055	for (i=0; i<8; i++)
6056		printf(" %.2x",page_ptr[i]);
6057	printf("\n");
6058	return (0);
6059}
6060
6061int
6062ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6063			       struct ctl_page_index *page_index,
6064			       int pc)
6065{
6066	struct copan_debugconf_subpage *page;
6067
6068	page = (struct copan_debugconf_subpage *)page_index->page_data +
6069		(page_index->page_len * pc);
6070
6071	switch (pc) {
6072	case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6073	case SMS_PAGE_CTRL_DEFAULT >> 6:
6074	case SMS_PAGE_CTRL_SAVED >> 6:
6075		/*
6076		 * We don't update the changable or default bits for this page.
6077		 */
6078		break;
6079	case SMS_PAGE_CTRL_CURRENT >> 6:
6080		page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6081		page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6082		break;
6083	default:
6084#ifdef NEEDTOPORT
6085		EPRINT(0, "Invalid PC %d!!", pc);
6086#endif /* NEEDTOPORT */
6087		break;
6088	}
6089	return (0);
6090}
6091
6092
6093static int
6094ctl_do_mode_select(union ctl_io *io)
6095{
6096	struct scsi_mode_page_header *page_header;
6097	struct ctl_page_index *page_index;
6098	struct ctl_scsiio *ctsio;
6099	int control_dev, page_len;
6100	int page_len_offset, page_len_size;
6101	union ctl_modepage_info *modepage_info;
6102	struct ctl_lun *lun;
6103	int *len_left, *len_used;
6104	int retval, i;
6105
6106	ctsio = &io->scsiio;
6107	page_index = NULL;
6108	page_len = 0;
6109	retval = CTL_RETVAL_COMPLETE;
6110
6111	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6112
6113	if (lun->be_lun->lun_type != T_DIRECT)
6114		control_dev = 1;
6115	else
6116		control_dev = 0;
6117
6118	modepage_info = (union ctl_modepage_info *)
6119		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6120	len_left = &modepage_info->header.len_left;
6121	len_used = &modepage_info->header.len_used;
6122
6123do_next_page:
6124
6125	page_header = (struct scsi_mode_page_header *)
6126		(ctsio->kern_data_ptr + *len_used);
6127
6128	if (*len_left == 0) {
6129		free(ctsio->kern_data_ptr, M_CTL);
6130		ctl_set_success(ctsio);
6131		ctl_done((union ctl_io *)ctsio);
6132		return (CTL_RETVAL_COMPLETE);
6133	} else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6134
6135		free(ctsio->kern_data_ptr, M_CTL);
6136		ctl_set_param_len_error(ctsio);
6137		ctl_done((union ctl_io *)ctsio);
6138		return (CTL_RETVAL_COMPLETE);
6139
6140	} else if ((page_header->page_code & SMPH_SPF)
6141		&& (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6142
6143		free(ctsio->kern_data_ptr, M_CTL);
6144		ctl_set_param_len_error(ctsio);
6145		ctl_done((union ctl_io *)ctsio);
6146		return (CTL_RETVAL_COMPLETE);
6147	}
6148
6149
6150	/*
6151	 * XXX KDM should we do something with the block descriptor?
6152	 */
6153	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6154
6155		if ((control_dev != 0)
6156		 && (lun->mode_pages.index[i].page_flags &
6157		     CTL_PAGE_FLAG_DISK_ONLY))
6158			continue;
6159
6160		if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6161		    (page_header->page_code & SMPH_PC_MASK))
6162			continue;
6163
6164		/*
6165		 * If neither page has a subpage code, then we've got a
6166		 * match.
6167		 */
6168		if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6169		 && ((page_header->page_code & SMPH_SPF) == 0)) {
6170			page_index = &lun->mode_pages.index[i];
6171			page_len = page_header->page_length;
6172			break;
6173		}
6174
6175		/*
6176		 * If both pages have subpages, then the subpage numbers
6177		 * have to match.
6178		 */
6179		if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6180		  && (page_header->page_code & SMPH_SPF)) {
6181			struct scsi_mode_page_header_sp *sph;
6182
6183			sph = (struct scsi_mode_page_header_sp *)page_header;
6184
6185			if (lun->mode_pages.index[i].subpage ==
6186			    sph->subpage) {
6187				page_index = &lun->mode_pages.index[i];
6188				page_len = scsi_2btoul(sph->page_length);
6189				break;
6190			}
6191		}
6192	}
6193
6194	/*
6195	 * If we couldn't find the page, or if we don't have a mode select
6196	 * handler for it, send back an error to the user.
6197	 */
6198	if ((page_index == NULL)
6199	 || (page_index->select_handler == NULL)) {
6200		ctl_set_invalid_field(ctsio,
6201				      /*sks_valid*/ 1,
6202				      /*command*/ 0,
6203				      /*field*/ *len_used,
6204				      /*bit_valid*/ 0,
6205				      /*bit*/ 0);
6206		free(ctsio->kern_data_ptr, M_CTL);
6207		ctl_done((union ctl_io *)ctsio);
6208		return (CTL_RETVAL_COMPLETE);
6209	}
6210
6211	if (page_index->page_code & SMPH_SPF) {
6212		page_len_offset = 2;
6213		page_len_size = 2;
6214	} else {
6215		page_len_size = 1;
6216		page_len_offset = 1;
6217	}
6218
6219	/*
6220	 * If the length the initiator gives us isn't the one we specify in
6221	 * the mode page header, or if they didn't specify enough data in
6222	 * the CDB to avoid truncating this page, kick out the request.
6223	 */
6224	if ((page_len != (page_index->page_len - page_len_offset -
6225			  page_len_size))
6226	 || (*len_left < page_index->page_len)) {
6227
6228
6229		ctl_set_invalid_field(ctsio,
6230				      /*sks_valid*/ 1,
6231				      /*command*/ 0,
6232				      /*field*/ *len_used + page_len_offset,
6233				      /*bit_valid*/ 0,
6234				      /*bit*/ 0);
6235		free(ctsio->kern_data_ptr, M_CTL);
6236		ctl_done((union ctl_io *)ctsio);
6237		return (CTL_RETVAL_COMPLETE);
6238	}
6239
6240	/*
6241	 * Run through the mode page, checking to make sure that the bits
6242	 * the user changed are actually legal for him to change.
6243	 */
6244	for (i = 0; i < page_index->page_len; i++) {
6245		uint8_t *user_byte, *change_mask, *current_byte;
6246		int bad_bit;
6247		int j;
6248
6249		user_byte = (uint8_t *)page_header + i;
6250		change_mask = page_index->page_data +
6251			      (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6252		current_byte = page_index->page_data +
6253			       (page_index->page_len * CTL_PAGE_CURRENT) + i;
6254
6255		/*
6256		 * Check to see whether the user set any bits in this byte
6257		 * that he is not allowed to set.
6258		 */
6259		if ((*user_byte & ~(*change_mask)) ==
6260		    (*current_byte & ~(*change_mask)))
6261			continue;
6262
6263		/*
6264		 * Go through bit by bit to determine which one is illegal.
6265		 */
6266		bad_bit = 0;
6267		for (j = 7; j >= 0; j--) {
6268			if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6269			    (((1 << i) & ~(*change_mask)) & *current_byte)) {
6270				bad_bit = i;
6271				break;
6272			}
6273		}
6274		ctl_set_invalid_field(ctsio,
6275				      /*sks_valid*/ 1,
6276				      /*command*/ 0,
6277				      /*field*/ *len_used + i,
6278				      /*bit_valid*/ 1,
6279				      /*bit*/ bad_bit);
6280		free(ctsio->kern_data_ptr, M_CTL);
6281		ctl_done((union ctl_io *)ctsio);
6282		return (CTL_RETVAL_COMPLETE);
6283	}
6284
6285	/*
6286	 * Decrement these before we call the page handler, since we may
6287	 * end up getting called back one way or another before the handler
6288	 * returns to this context.
6289	 */
6290	*len_left -= page_index->page_len;
6291	*len_used += page_index->page_len;
6292
6293	retval = page_index->select_handler(ctsio, page_index,
6294					    (uint8_t *)page_header);
6295
6296	/*
6297	 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6298	 * wait until this queued command completes to finish processing
6299	 * the mode page.  If it returns anything other than
6300	 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6301	 * already set the sense information, freed the data pointer, and
6302	 * completed the io for us.
6303	 */
6304	if (retval != CTL_RETVAL_COMPLETE)
6305		goto bailout_no_done;
6306
6307	/*
6308	 * If the initiator sent us more than one page, parse the next one.
6309	 */
6310	if (*len_left > 0)
6311		goto do_next_page;
6312
6313	ctl_set_success(ctsio);
6314	free(ctsio->kern_data_ptr, M_CTL);
6315	ctl_done((union ctl_io *)ctsio);
6316
6317bailout_no_done:
6318
6319	return (CTL_RETVAL_COMPLETE);
6320
6321}
6322
6323int
6324ctl_mode_select(struct ctl_scsiio *ctsio)
6325{
6326	int param_len, pf, sp;
6327	int header_size, bd_len;
6328	int len_left, len_used;
6329	struct ctl_page_index *page_index;
6330	struct ctl_lun *lun;
6331	int control_dev, page_len;
6332	union ctl_modepage_info *modepage_info;
6333	int retval;
6334
6335	pf = 0;
6336	sp = 0;
6337	page_len = 0;
6338	len_used = 0;
6339	len_left = 0;
6340	retval = 0;
6341	bd_len = 0;
6342	page_index = NULL;
6343
6344	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6345
6346	if (lun->be_lun->lun_type != T_DIRECT)
6347		control_dev = 1;
6348	else
6349		control_dev = 0;
6350
6351	switch (ctsio->cdb[0]) {
6352	case MODE_SELECT_6: {
6353		struct scsi_mode_select_6 *cdb;
6354
6355		cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6356
6357		pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6358		sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6359
6360		param_len = cdb->length;
6361		header_size = sizeof(struct scsi_mode_header_6);
6362		break;
6363	}
6364	case MODE_SELECT_10: {
6365		struct scsi_mode_select_10 *cdb;
6366
6367		cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6368
6369		pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6370		sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6371
6372		param_len = scsi_2btoul(cdb->length);
6373		header_size = sizeof(struct scsi_mode_header_10);
6374		break;
6375	}
6376	default:
6377		ctl_set_invalid_opcode(ctsio);
6378		ctl_done((union ctl_io *)ctsio);
6379		return (CTL_RETVAL_COMPLETE);
6380		break; /* NOTREACHED */
6381	}
6382
6383	/*
6384	 * From SPC-3:
6385	 * "A parameter list length of zero indicates that the Data-Out Buffer
6386	 * shall be empty. This condition shall not be considered as an error."
6387	 */
6388	if (param_len == 0) {
6389		ctl_set_success(ctsio);
6390		ctl_done((union ctl_io *)ctsio);
6391		return (CTL_RETVAL_COMPLETE);
6392	}
6393
6394	/*
6395	 * Since we'll hit this the first time through, prior to
6396	 * allocation, we don't need to free a data buffer here.
6397	 */
6398	if (param_len < header_size) {
6399		ctl_set_param_len_error(ctsio);
6400		ctl_done((union ctl_io *)ctsio);
6401		return (CTL_RETVAL_COMPLETE);
6402	}
6403
6404	/*
6405	 * Allocate the data buffer and grab the user's data.  In theory,
6406	 * we shouldn't have to sanity check the parameter list length here
6407	 * because the maximum size is 64K.  We should be able to malloc
6408	 * that much without too many problems.
6409	 */
6410	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6411		ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6412		ctsio->kern_data_len = param_len;
6413		ctsio->kern_total_len = param_len;
6414		ctsio->kern_data_resid = 0;
6415		ctsio->kern_rel_offset = 0;
6416		ctsio->kern_sg_entries = 0;
6417		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6418		ctsio->be_move_done = ctl_config_move_done;
6419		ctl_datamove((union ctl_io *)ctsio);
6420
6421		return (CTL_RETVAL_COMPLETE);
6422	}
6423
6424	switch (ctsio->cdb[0]) {
6425	case MODE_SELECT_6: {
6426		struct scsi_mode_header_6 *mh6;
6427
6428		mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6429		bd_len = mh6->blk_desc_len;
6430		break;
6431	}
6432	case MODE_SELECT_10: {
6433		struct scsi_mode_header_10 *mh10;
6434
6435		mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6436		bd_len = scsi_2btoul(mh10->blk_desc_len);
6437		break;
6438	}
6439	default:
6440		panic("Invalid CDB type %#x", ctsio->cdb[0]);
6441		break;
6442	}
6443
6444	if (param_len < (header_size + bd_len)) {
6445		free(ctsio->kern_data_ptr, M_CTL);
6446		ctl_set_param_len_error(ctsio);
6447		ctl_done((union ctl_io *)ctsio);
6448		return (CTL_RETVAL_COMPLETE);
6449	}
6450
6451	/*
6452	 * Set the IO_CONT flag, so that if this I/O gets passed to
6453	 * ctl_config_write_done(), it'll get passed back to
6454	 * ctl_do_mode_select() for further processing, or completion if
6455	 * we're all done.
6456	 */
6457	ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6458	ctsio->io_cont = ctl_do_mode_select;
6459
6460	modepage_info = (union ctl_modepage_info *)
6461		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6462
6463	memset(modepage_info, 0, sizeof(*modepage_info));
6464
6465	len_left = param_len - header_size - bd_len;
6466	len_used = header_size + bd_len;
6467
6468	modepage_info->header.len_left = len_left;
6469	modepage_info->header.len_used = len_used;
6470
6471	return (ctl_do_mode_select((union ctl_io *)ctsio));
6472}
6473
6474int
6475ctl_mode_sense(struct ctl_scsiio *ctsio)
6476{
6477	struct ctl_lun *lun;
6478	int pc, page_code, dbd, llba, subpage;
6479	int alloc_len, page_len, header_len, total_len;
6480	struct scsi_mode_block_descr *block_desc;
6481	struct ctl_page_index *page_index;
6482	int control_dev;
6483
6484	dbd = 0;
6485	llba = 0;
6486	block_desc = NULL;
6487	page_index = NULL;
6488
6489	CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6490
6491	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6492
6493	if (lun->be_lun->lun_type != T_DIRECT)
6494		control_dev = 1;
6495	else
6496		control_dev = 0;
6497
6498	switch (ctsio->cdb[0]) {
6499	case MODE_SENSE_6: {
6500		struct scsi_mode_sense_6 *cdb;
6501
6502		cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6503
6504		header_len = sizeof(struct scsi_mode_hdr_6);
6505		if (cdb->byte2 & SMS_DBD)
6506			dbd = 1;
6507		else
6508			header_len += sizeof(struct scsi_mode_block_descr);
6509
6510		pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6511		page_code = cdb->page & SMS_PAGE_CODE;
6512		subpage = cdb->subpage;
6513		alloc_len = cdb->length;
6514		break;
6515	}
6516	case MODE_SENSE_10: {
6517		struct scsi_mode_sense_10 *cdb;
6518
6519		cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6520
6521		header_len = sizeof(struct scsi_mode_hdr_10);
6522
6523		if (cdb->byte2 & SMS_DBD)
6524			dbd = 1;
6525		else
6526			header_len += sizeof(struct scsi_mode_block_descr);
6527		if (cdb->byte2 & SMS10_LLBAA)
6528			llba = 1;
6529		pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6530		page_code = cdb->page & SMS_PAGE_CODE;
6531		subpage = cdb->subpage;
6532		alloc_len = scsi_2btoul(cdb->length);
6533		break;
6534	}
6535	default:
6536		ctl_set_invalid_opcode(ctsio);
6537		ctl_done((union ctl_io *)ctsio);
6538		return (CTL_RETVAL_COMPLETE);
6539		break; /* NOTREACHED */
6540	}
6541
6542	/*
6543	 * We have to make a first pass through to calculate the size of
6544	 * the pages that match the user's query.  Then we allocate enough
6545	 * memory to hold it, and actually copy the data into the buffer.
6546	 */
6547	switch (page_code) {
6548	case SMS_ALL_PAGES_PAGE: {
6549		int i;
6550
6551		page_len = 0;
6552
6553		/*
6554		 * At the moment, values other than 0 and 0xff here are
6555		 * reserved according to SPC-3.
6556		 */
6557		if ((subpage != SMS_SUBPAGE_PAGE_0)
6558		 && (subpage != SMS_SUBPAGE_ALL)) {
6559			ctl_set_invalid_field(ctsio,
6560					      /*sks_valid*/ 1,
6561					      /*command*/ 1,
6562					      /*field*/ 3,
6563					      /*bit_valid*/ 0,
6564					      /*bit*/ 0);
6565			ctl_done((union ctl_io *)ctsio);
6566			return (CTL_RETVAL_COMPLETE);
6567		}
6568
6569		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6570			if ((control_dev != 0)
6571			 && (lun->mode_pages.index[i].page_flags &
6572			     CTL_PAGE_FLAG_DISK_ONLY))
6573				continue;
6574
6575			/*
6576			 * We don't use this subpage if the user didn't
6577			 * request all subpages.
6578			 */
6579			if ((lun->mode_pages.index[i].subpage != 0)
6580			 && (subpage == SMS_SUBPAGE_PAGE_0))
6581				continue;
6582
6583#if 0
6584			printf("found page %#x len %d\n",
6585			       lun->mode_pages.index[i].page_code &
6586			       SMPH_PC_MASK,
6587			       lun->mode_pages.index[i].page_len);
6588#endif
6589			page_len += lun->mode_pages.index[i].page_len;
6590		}
6591		break;
6592	}
6593	default: {
6594		int i;
6595
6596		page_len = 0;
6597
6598		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6599			/* Look for the right page code */
6600			if ((lun->mode_pages.index[i].page_code &
6601			     SMPH_PC_MASK) != page_code)
6602				continue;
6603
6604			/* Look for the right subpage or the subpage wildcard*/
6605			if ((lun->mode_pages.index[i].subpage != subpage)
6606			 && (subpage != SMS_SUBPAGE_ALL))
6607				continue;
6608
6609			/* Make sure the page is supported for this dev type */
6610			if ((control_dev != 0)
6611			 && (lun->mode_pages.index[i].page_flags &
6612			     CTL_PAGE_FLAG_DISK_ONLY))
6613				continue;
6614
6615#if 0
6616			printf("found page %#x len %d\n",
6617			       lun->mode_pages.index[i].page_code &
6618			       SMPH_PC_MASK,
6619			       lun->mode_pages.index[i].page_len);
6620#endif
6621
6622			page_len += lun->mode_pages.index[i].page_len;
6623		}
6624
6625		if (page_len == 0) {
6626			ctl_set_invalid_field(ctsio,
6627					      /*sks_valid*/ 1,
6628					      /*command*/ 1,
6629					      /*field*/ 2,
6630					      /*bit_valid*/ 1,
6631					      /*bit*/ 5);
6632			ctl_done((union ctl_io *)ctsio);
6633			return (CTL_RETVAL_COMPLETE);
6634		}
6635		break;
6636	}
6637	}
6638
6639	total_len = header_len + page_len;
6640#if 0
6641	printf("header_len = %d, page_len = %d, total_len = %d\n",
6642	       header_len, page_len, total_len);
6643#endif
6644
6645	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6646	ctsio->kern_sg_entries = 0;
6647	ctsio->kern_data_resid = 0;
6648	ctsio->kern_rel_offset = 0;
6649	if (total_len < alloc_len) {
6650		ctsio->residual = alloc_len - total_len;
6651		ctsio->kern_data_len = total_len;
6652		ctsio->kern_total_len = total_len;
6653	} else {
6654		ctsio->residual = 0;
6655		ctsio->kern_data_len = alloc_len;
6656		ctsio->kern_total_len = alloc_len;
6657	}
6658
6659	switch (ctsio->cdb[0]) {
6660	case MODE_SENSE_6: {
6661		struct scsi_mode_hdr_6 *header;
6662
6663		header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6664
6665		header->datalen = ctl_min(total_len - 1, 254);
6666
6667		if (dbd)
6668			header->block_descr_len = 0;
6669		else
6670			header->block_descr_len =
6671				sizeof(struct scsi_mode_block_descr);
6672		block_desc = (struct scsi_mode_block_descr *)&header[1];
6673		break;
6674	}
6675	case MODE_SENSE_10: {
6676		struct scsi_mode_hdr_10 *header;
6677		int datalen;
6678
6679		header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6680
6681		datalen = ctl_min(total_len - 2, 65533);
6682		scsi_ulto2b(datalen, header->datalen);
6683		if (dbd)
6684			scsi_ulto2b(0, header->block_descr_len);
6685		else
6686			scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6687				    header->block_descr_len);
6688		block_desc = (struct scsi_mode_block_descr *)&header[1];
6689		break;
6690	}
6691	default:
6692		panic("invalid CDB type %#x", ctsio->cdb[0]);
6693		break; /* NOTREACHED */
6694	}
6695
6696	/*
6697	 * If we've got a disk, use its blocksize in the block
6698	 * descriptor.  Otherwise, just set it to 0.
6699	 */
6700	if (dbd == 0) {
6701		if (control_dev != 0)
6702			scsi_ulto3b(lun->be_lun->blocksize,
6703				    block_desc->block_len);
6704		else
6705			scsi_ulto3b(0, block_desc->block_len);
6706	}
6707
6708	switch (page_code) {
6709	case SMS_ALL_PAGES_PAGE: {
6710		int i, data_used;
6711
6712		data_used = header_len;
6713		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6714			struct ctl_page_index *page_index;
6715
6716			page_index = &lun->mode_pages.index[i];
6717
6718			if ((control_dev != 0)
6719			 && (page_index->page_flags &
6720			    CTL_PAGE_FLAG_DISK_ONLY))
6721				continue;
6722
6723			/*
6724			 * We don't use this subpage if the user didn't
6725			 * request all subpages.  We already checked (above)
6726			 * to make sure the user only specified a subpage
6727			 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6728			 */
6729			if ((page_index->subpage != 0)
6730			 && (subpage == SMS_SUBPAGE_PAGE_0))
6731				continue;
6732
6733			/*
6734			 * Call the handler, if it exists, to update the
6735			 * page to the latest values.
6736			 */
6737			if (page_index->sense_handler != NULL)
6738				page_index->sense_handler(ctsio, page_index,pc);
6739
6740			memcpy(ctsio->kern_data_ptr + data_used,
6741			       page_index->page_data +
6742			       (page_index->page_len * pc),
6743			       page_index->page_len);
6744			data_used += page_index->page_len;
6745		}
6746		break;
6747	}
6748	default: {
6749		int i, data_used;
6750
6751		data_used = header_len;
6752
6753		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6754			struct ctl_page_index *page_index;
6755
6756			page_index = &lun->mode_pages.index[i];
6757
6758			/* Look for the right page code */
6759			if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6760				continue;
6761
6762			/* Look for the right subpage or the subpage wildcard*/
6763			if ((page_index->subpage != subpage)
6764			 && (subpage != SMS_SUBPAGE_ALL))
6765				continue;
6766
6767			/* Make sure the page is supported for this dev type */
6768			if ((control_dev != 0)
6769			 && (page_index->page_flags &
6770			     CTL_PAGE_FLAG_DISK_ONLY))
6771				continue;
6772
6773			/*
6774			 * Call the handler, if it exists, to update the
6775			 * page to the latest values.
6776			 */
6777			if (page_index->sense_handler != NULL)
6778				page_index->sense_handler(ctsio, page_index,pc);
6779
6780			memcpy(ctsio->kern_data_ptr + data_used,
6781			       page_index->page_data +
6782			       (page_index->page_len * pc),
6783			       page_index->page_len);
6784			data_used += page_index->page_len;
6785		}
6786		break;
6787	}
6788	}
6789
6790	ctsio->scsi_status = SCSI_STATUS_OK;
6791
6792	ctsio->be_move_done = ctl_config_move_done;
6793	ctl_datamove((union ctl_io *)ctsio);
6794
6795	return (CTL_RETVAL_COMPLETE);
6796}
6797
6798int
6799ctl_read_capacity(struct ctl_scsiio *ctsio)
6800{
6801	struct scsi_read_capacity *cdb;
6802	struct scsi_read_capacity_data *data;
6803	struct ctl_lun *lun;
6804	uint32_t lba;
6805
6806	CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6807
6808	cdb = (struct scsi_read_capacity *)ctsio->cdb;
6809
6810	lba = scsi_4btoul(cdb->addr);
6811	if (((cdb->pmi & SRC_PMI) == 0)
6812	 && (lba != 0)) {
6813		ctl_set_invalid_field(/*ctsio*/ ctsio,
6814				      /*sks_valid*/ 1,
6815				      /*command*/ 1,
6816				      /*field*/ 2,
6817				      /*bit_valid*/ 0,
6818				      /*bit*/ 0);
6819		ctl_done((union ctl_io *)ctsio);
6820		return (CTL_RETVAL_COMPLETE);
6821	}
6822
6823	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6824
6825	ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6826	data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6827	ctsio->residual = 0;
6828	ctsio->kern_data_len = sizeof(*data);
6829	ctsio->kern_total_len = sizeof(*data);
6830	ctsio->kern_data_resid = 0;
6831	ctsio->kern_rel_offset = 0;
6832	ctsio->kern_sg_entries = 0;
6833
6834	/*
6835	 * If the maximum LBA is greater than 0xfffffffe, the user must
6836	 * issue a SERVICE ACTION IN (16) command, with the read capacity
6837	 * serivce action set.
6838	 */
6839	if (lun->be_lun->maxlba > 0xfffffffe)
6840		scsi_ulto4b(0xffffffff, data->addr);
6841	else
6842		scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6843
6844	/*
6845	 * XXX KDM this may not be 512 bytes...
6846	 */
6847	scsi_ulto4b(lun->be_lun->blocksize, data->length);
6848
6849	ctsio->scsi_status = SCSI_STATUS_OK;
6850
6851	ctsio->be_move_done = ctl_config_move_done;
6852	ctl_datamove((union ctl_io *)ctsio);
6853
6854	return (CTL_RETVAL_COMPLETE);
6855}
6856
6857static int
6858ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6859{
6860	struct scsi_read_capacity_16 *cdb;
6861	struct scsi_read_capacity_data_long *data;
6862	struct ctl_lun *lun;
6863	uint64_t lba;
6864	uint32_t alloc_len;
6865
6866	CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6867
6868	cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6869
6870	alloc_len = scsi_4btoul(cdb->alloc_len);
6871	lba = scsi_8btou64(cdb->addr);
6872
6873	if ((cdb->reladr & SRC16_PMI)
6874	 && (lba != 0)) {
6875		ctl_set_invalid_field(/*ctsio*/ ctsio,
6876				      /*sks_valid*/ 1,
6877				      /*command*/ 1,
6878				      /*field*/ 2,
6879				      /*bit_valid*/ 0,
6880				      /*bit*/ 0);
6881		ctl_done((union ctl_io *)ctsio);
6882		return (CTL_RETVAL_COMPLETE);
6883	}
6884
6885	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6886
6887	ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6888	data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6889
6890	if (sizeof(*data) < alloc_len) {
6891		ctsio->residual = alloc_len - sizeof(*data);
6892		ctsio->kern_data_len = sizeof(*data);
6893		ctsio->kern_total_len = sizeof(*data);
6894	} else {
6895		ctsio->residual = 0;
6896		ctsio->kern_data_len = alloc_len;
6897		ctsio->kern_total_len = alloc_len;
6898	}
6899	ctsio->kern_data_resid = 0;
6900	ctsio->kern_rel_offset = 0;
6901	ctsio->kern_sg_entries = 0;
6902
6903	scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6904	/* XXX KDM this may not be 512 bytes... */
6905	scsi_ulto4b(lun->be_lun->blocksize, data->length);
6906	data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6907	scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6908
6909	ctsio->scsi_status = SCSI_STATUS_OK;
6910
6911	ctsio->be_move_done = ctl_config_move_done;
6912	ctl_datamove((union ctl_io *)ctsio);
6913
6914	return (CTL_RETVAL_COMPLETE);
6915}
6916
6917int
6918ctl_service_action_in(struct ctl_scsiio *ctsio)
6919{
6920	struct scsi_service_action_in *cdb;
6921	int retval;
6922
6923	CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
6924
6925	cdb = (struct scsi_service_action_in *)ctsio->cdb;
6926
6927	retval = CTL_RETVAL_COMPLETE;
6928
6929	switch (cdb->service_action) {
6930	case SRC16_SERVICE_ACTION:
6931		retval = ctl_read_capacity_16(ctsio);
6932		break;
6933	default:
6934		ctl_set_invalid_field(/*ctsio*/ ctsio,
6935				      /*sks_valid*/ 1,
6936				      /*command*/ 1,
6937				      /*field*/ 1,
6938				      /*bit_valid*/ 1,
6939				      /*bit*/ 4);
6940		ctl_done((union ctl_io *)ctsio);
6941		break;
6942	}
6943
6944	return (retval);
6945}
6946
6947int
6948ctl_maintenance_in(struct ctl_scsiio *ctsio)
6949{
6950	struct scsi_maintenance_in *cdb;
6951	int retval;
6952	int alloc_len, total_len = 0;
6953	int num_target_port_groups, single;
6954	struct ctl_lun *lun;
6955	struct ctl_softc *softc;
6956	struct scsi_target_group_data *rtg_ptr;
6957	struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
6958	struct scsi_target_port_descriptor  *tp_desc_ptr1_1, *tp_desc_ptr1_2,
6959	                                    *tp_desc_ptr2_1, *tp_desc_ptr2_2;
6960
6961	CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
6962
6963	cdb = (struct scsi_maintenance_in *)ctsio->cdb;
6964	softc = control_softc;
6965	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6966
6967	retval = CTL_RETVAL_COMPLETE;
6968
6969	if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
6970		ctl_set_invalid_field(/*ctsio*/ ctsio,
6971				      /*sks_valid*/ 1,
6972				      /*command*/ 1,
6973				      /*field*/ 1,
6974				      /*bit_valid*/ 1,
6975				      /*bit*/ 4);
6976		ctl_done((union ctl_io *)ctsio);
6977		return(retval);
6978	}
6979
6980	mtx_lock(&softc->ctl_lock);
6981	single = ctl_is_single;
6982	mtx_unlock(&softc->ctl_lock);
6983
6984	if (single)
6985        	num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
6986	else
6987        	num_target_port_groups = NUM_TARGET_PORT_GROUPS;
6988
6989	total_len = sizeof(struct scsi_target_group_data) +
6990		sizeof(struct scsi_target_port_group_descriptor) *
6991		num_target_port_groups +
6992		sizeof(struct scsi_target_port_descriptor) *
6993		NUM_PORTS_PER_GRP * num_target_port_groups;
6994
6995	alloc_len = scsi_4btoul(cdb->length);
6996
6997	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6998
6999	ctsio->kern_sg_entries = 0;
7000
7001	if (total_len < alloc_len) {
7002		ctsio->residual = alloc_len - total_len;
7003		ctsio->kern_data_len = total_len;
7004		ctsio->kern_total_len = total_len;
7005	} else {
7006		ctsio->residual = 0;
7007		ctsio->kern_data_len = alloc_len;
7008		ctsio->kern_total_len = alloc_len;
7009	}
7010	ctsio->kern_data_resid = 0;
7011	ctsio->kern_rel_offset = 0;
7012
7013	rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
7014
7015	tpg_desc_ptr1 = &rtg_ptr->groups[0];
7016	tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
7017	tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7018	        &tp_desc_ptr1_1->desc_list[0];
7019
7020	if (single == 0) {
7021		tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7022	                &tp_desc_ptr1_2->desc_list[0];
7023		tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7024		tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7025	        	&tp_desc_ptr2_1->desc_list[0];
7026        } else {
7027		tpg_desc_ptr2 = NULL;
7028		tp_desc_ptr2_1 = NULL;
7029		tp_desc_ptr2_2 = NULL;
7030	}
7031
7032	scsi_ulto4b(total_len - 4, rtg_ptr->length);
7033	if (single == 0) {
7034        	if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7035			if (lun->flags & CTL_LUN_PRIMARY_SC) {
7036				tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7037				tpg_desc_ptr2->pref_state =
7038					TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7039			} else {
7040				tpg_desc_ptr1->pref_state =
7041					TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7042				tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7043			}
7044		} else {
7045			if (lun->flags & CTL_LUN_PRIMARY_SC) {
7046				tpg_desc_ptr1->pref_state =
7047					TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7048				tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7049			} else {
7050				tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7051				tpg_desc_ptr2->pref_state =
7052					TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7053			}
7054		}
7055	} else {
7056		tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7057	}
7058	tpg_desc_ptr1->support = 0;
7059	tpg_desc_ptr1->target_port_group[1] = 1;
7060	tpg_desc_ptr1->status = TPG_IMPLICIT;
7061	tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7062
7063	if (single == 0) {
7064		tpg_desc_ptr2->support = 0;
7065		tpg_desc_ptr2->target_port_group[1] = 2;
7066		tpg_desc_ptr2->status = TPG_IMPLICIT;
7067		tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7068
7069		tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7070		tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7071
7072		tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7073		tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7074	} else {
7075        	if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7076			tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7077			tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7078		} else {
7079			tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7080			tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7081		}
7082	}
7083
7084	ctsio->be_move_done = ctl_config_move_done;
7085
7086	CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7087			 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7088			 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7089			 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7090			 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7091
7092	ctl_datamove((union ctl_io *)ctsio);
7093	return(retval);
7094}
7095
7096int
7097ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7098{
7099	struct scsi_per_res_in *cdb;
7100	int alloc_len, total_len = 0;
7101	/* struct scsi_per_res_in_rsrv in_data; */
7102	struct ctl_lun *lun;
7103	struct ctl_softc *softc;
7104
7105	CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7106
7107	softc = control_softc;
7108
7109	cdb = (struct scsi_per_res_in *)ctsio->cdb;
7110
7111	alloc_len = scsi_2btoul(cdb->length);
7112
7113	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7114
7115retry:
7116	mtx_lock(&softc->ctl_lock);
7117	switch (cdb->action) {
7118	case SPRI_RK: /* read keys */
7119		total_len = sizeof(struct scsi_per_res_in_keys) +
7120			lun->pr_key_count *
7121			sizeof(struct scsi_per_res_key);
7122		break;
7123	case SPRI_RR: /* read reservation */
7124		if (lun->flags & CTL_LUN_PR_RESERVED)
7125			total_len = sizeof(struct scsi_per_res_in_rsrv);
7126		else
7127			total_len = sizeof(struct scsi_per_res_in_header);
7128		break;
7129	case SPRI_RC: /* report capabilities */
7130		total_len = sizeof(struct scsi_per_res_cap);
7131		break;
7132	case SPRI_RS: /* read full status */
7133	default:
7134		mtx_unlock(&softc->ctl_lock);
7135		ctl_set_invalid_field(ctsio,
7136				      /*sks_valid*/ 1,
7137				      /*command*/ 1,
7138				      /*field*/ 1,
7139				      /*bit_valid*/ 1,
7140				      /*bit*/ 0);
7141		ctl_done((union ctl_io *)ctsio);
7142		return (CTL_RETVAL_COMPLETE);
7143		break; /* NOTREACHED */
7144	}
7145	mtx_unlock(&softc->ctl_lock);
7146
7147	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7148
7149	if (total_len < alloc_len) {
7150		ctsio->residual = alloc_len - total_len;
7151		ctsio->kern_data_len = total_len;
7152		ctsio->kern_total_len = total_len;
7153	} else {
7154		ctsio->residual = 0;
7155		ctsio->kern_data_len = alloc_len;
7156		ctsio->kern_total_len = alloc_len;
7157	}
7158
7159	ctsio->kern_data_resid = 0;
7160	ctsio->kern_rel_offset = 0;
7161	ctsio->kern_sg_entries = 0;
7162
7163	mtx_lock(&softc->ctl_lock);
7164	switch (cdb->action) {
7165	case SPRI_RK: { // read keys
7166        struct scsi_per_res_in_keys *res_keys;
7167		int i, key_count;
7168
7169		res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7170
7171		/*
7172		 * We had to drop the lock to allocate our buffer, which
7173		 * leaves time for someone to come in with another
7174		 * persistent reservation.  (That is unlikely, though,
7175		 * since this should be the only persistent reservation
7176		 * command active right now.)
7177		 */
7178		if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7179		    (lun->pr_key_count *
7180		     sizeof(struct scsi_per_res_key)))){
7181			mtx_unlock(&softc->ctl_lock);
7182			free(ctsio->kern_data_ptr, M_CTL);
7183			printf("%s: reservation length changed, retrying\n",
7184			       __func__);
7185			goto retry;
7186		}
7187
7188		scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7189
7190		scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7191			     lun->pr_key_count, res_keys->header.length);
7192
7193		for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7194			if (!lun->per_res[i].registered)
7195				continue;
7196
7197			/*
7198			 * We used lun->pr_key_count to calculate the
7199			 * size to allocate.  If it turns out the number of
7200			 * initiators with the registered flag set is
7201			 * larger than that (i.e. they haven't been kept in
7202			 * sync), we've got a problem.
7203			 */
7204			if (key_count >= lun->pr_key_count) {
7205#ifdef NEEDTOPORT
7206				csevent_log(CSC_CTL | CSC_SHELF_SW |
7207					    CTL_PR_ERROR,
7208					    csevent_LogType_Fault,
7209					    csevent_AlertLevel_Yellow,
7210					    csevent_FRU_ShelfController,
7211					    csevent_FRU_Firmware,
7212				        csevent_FRU_Unknown,
7213					    "registered keys %d >= key "
7214					    "count %d", key_count,
7215					    lun->pr_key_count);
7216#endif
7217				key_count++;
7218				continue;
7219			}
7220			memcpy(res_keys->keys[key_count].key,
7221			       lun->per_res[i].res_key.key,
7222			       ctl_min(sizeof(res_keys->keys[key_count].key),
7223			       sizeof(lun->per_res[i].res_key)));
7224			key_count++;
7225		}
7226		break;
7227	}
7228	case SPRI_RR: { // read reservation
7229		struct scsi_per_res_in_rsrv *res;
7230		int tmp_len, header_only;
7231
7232		res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7233
7234		scsi_ulto4b(lun->PRGeneration, res->header.generation);
7235
7236		if (lun->flags & CTL_LUN_PR_RESERVED)
7237		{
7238			tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7239			scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7240				    res->header.length);
7241			header_only = 0;
7242		} else {
7243			tmp_len = sizeof(struct scsi_per_res_in_header);
7244			scsi_ulto4b(0, res->header.length);
7245			header_only = 1;
7246		}
7247
7248		/*
7249		 * We had to drop the lock to allocate our buffer, which
7250		 * leaves time for someone to come in with another
7251		 * persistent reservation.  (That is unlikely, though,
7252		 * since this should be the only persistent reservation
7253		 * command active right now.)
7254		 */
7255		if (tmp_len != total_len) {
7256			mtx_unlock(&softc->ctl_lock);
7257			free(ctsio->kern_data_ptr, M_CTL);
7258			printf("%s: reservation status changed, retrying\n",
7259			       __func__);
7260			goto retry;
7261		}
7262
7263		/*
7264		 * No reservation held, so we're done.
7265		 */
7266		if (header_only != 0)
7267			break;
7268
7269		/*
7270		 * If the registration is an All Registrants type, the key
7271		 * is 0, since it doesn't really matter.
7272		 */
7273		if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7274			memcpy(res->data.reservation,
7275			       &lun->per_res[lun->pr_res_idx].res_key,
7276			       sizeof(struct scsi_per_res_key));
7277		}
7278		res->data.scopetype = lun->res_type;
7279		break;
7280	}
7281	case SPRI_RC:     //report capabilities
7282	{
7283		struct scsi_per_res_cap *res_cap;
7284		uint16_t type_mask;
7285
7286		res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7287		scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7288		res_cap->flags2 |= SPRI_TMV;
7289		type_mask = SPRI_TM_WR_EX_AR |
7290			    SPRI_TM_EX_AC_RO |
7291			    SPRI_TM_WR_EX_RO |
7292			    SPRI_TM_EX_AC |
7293			    SPRI_TM_WR_EX |
7294			    SPRI_TM_EX_AC_AR;
7295		scsi_ulto2b(type_mask, res_cap->type_mask);
7296		break;
7297	}
7298	case SPRI_RS: //read full status
7299	default:
7300		/*
7301		 * This is a bug, because we just checked for this above,
7302		 * and should have returned an error.
7303		 */
7304		panic("Invalid PR type %x", cdb->action);
7305		break; /* NOTREACHED */
7306	}
7307	mtx_unlock(&softc->ctl_lock);
7308
7309	ctsio->be_move_done = ctl_config_move_done;
7310
7311	CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7312			 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7313			 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7314			 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7315			 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7316
7317	ctl_datamove((union ctl_io *)ctsio);
7318
7319	return (CTL_RETVAL_COMPLETE);
7320}
7321
7322/*
7323 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7324 * it should return.
7325 */
7326static int
7327ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7328		uint64_t sa_res_key, uint8_t type, uint32_t residx,
7329		struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7330		struct scsi_per_res_out_parms* param)
7331{
7332	union ctl_ha_msg persis_io;
7333	int retval, i;
7334	int isc_retval;
7335
7336	retval = 0;
7337
7338	if (sa_res_key == 0) {
7339		mtx_lock(&softc->ctl_lock);
7340		if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7341			/* validate scope and type */
7342			if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7343			     SPR_LU_SCOPE) {
7344				mtx_unlock(&softc->ctl_lock);
7345				ctl_set_invalid_field(/*ctsio*/ ctsio,
7346						      /*sks_valid*/ 1,
7347						      /*command*/ 1,
7348						      /*field*/ 2,
7349						      /*bit_valid*/ 1,
7350						      /*bit*/ 4);
7351				ctl_done((union ctl_io *)ctsio);
7352				return (1);
7353			}
7354
7355		        if (type>8 || type==2 || type==4 || type==0) {
7356				mtx_unlock(&softc->ctl_lock);
7357				ctl_set_invalid_field(/*ctsio*/ ctsio,
7358       	           				      /*sks_valid*/ 1,
7359						      /*command*/ 1,
7360						      /*field*/ 2,
7361						      /*bit_valid*/ 1,
7362						      /*bit*/ 0);
7363				ctl_done((union ctl_io *)ctsio);
7364				return (1);
7365		        }
7366
7367			/* temporarily unregister this nexus */
7368			lun->per_res[residx].registered = 0;
7369
7370			/*
7371			 * Unregister everybody else and build UA for
7372			 * them
7373			 */
7374			for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7375				if (lun->per_res[i].registered == 0)
7376					continue;
7377
7378				if (!persis_offset
7379				 && i <CTL_MAX_INITIATORS)
7380					lun->pending_sense[i].ua_pending |=
7381						CTL_UA_REG_PREEMPT;
7382				else if (persis_offset
7383				      && i >= persis_offset)
7384					lun->pending_sense[i-persis_offset
7385						].ua_pending |=
7386						CTL_UA_REG_PREEMPT;
7387				lun->per_res[i].registered = 0;
7388				memset(&lun->per_res[i].res_key, 0,
7389				       sizeof(struct scsi_per_res_key));
7390			}
7391			lun->per_res[residx].registered = 1;
7392			lun->pr_key_count = 1;
7393			lun->res_type = type;
7394			if (lun->res_type != SPR_TYPE_WR_EX_AR
7395			 && lun->res_type != SPR_TYPE_EX_AC_AR)
7396				lun->pr_res_idx = residx;
7397
7398			mtx_unlock(&softc->ctl_lock);
7399			/* send msg to other side */
7400			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7401			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7402			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7403			persis_io.pr.pr_info.residx = lun->pr_res_idx;
7404			persis_io.pr.pr_info.res_type = type;
7405			memcpy(persis_io.pr.pr_info.sa_res_key,
7406			       param->serv_act_res_key,
7407			       sizeof(param->serv_act_res_key));
7408			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7409			     &persis_io, sizeof(persis_io), 0)) >
7410			     CTL_HA_STATUS_SUCCESS) {
7411				printf("CTL:Persis Out error returned "
7412				       "from ctl_ha_msg_send %d\n",
7413				       isc_retval);
7414			}
7415		} else {
7416			/* not all registrants */
7417			mtx_unlock(&softc->ctl_lock);
7418			free(ctsio->kern_data_ptr, M_CTL);
7419			ctl_set_invalid_field(ctsio,
7420					      /*sks_valid*/ 1,
7421					      /*command*/ 0,
7422					      /*field*/ 8,
7423					      /*bit_valid*/ 0,
7424					      /*bit*/ 0);
7425			ctl_done((union ctl_io *)ctsio);
7426			return (1);
7427		}
7428	} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7429		|| !(lun->flags & CTL_LUN_PR_RESERVED)) {
7430		int found = 0;
7431
7432		mtx_lock(&softc->ctl_lock);
7433		if (res_key == sa_res_key) {
7434			/* special case */
7435			/*
7436			 * The spec implies this is not good but doesn't
7437			 * say what to do. There are two choices either
7438			 * generate a res conflict or check condition
7439			 * with illegal field in parameter data. Since
7440			 * that is what is done when the sa_res_key is
7441			 * zero I'll take that approach since this has
7442			 * to do with the sa_res_key.
7443			 */
7444			mtx_unlock(&softc->ctl_lock);
7445			free(ctsio->kern_data_ptr, M_CTL);
7446			ctl_set_invalid_field(ctsio,
7447					      /*sks_valid*/ 1,
7448					      /*command*/ 0,
7449					      /*field*/ 8,
7450					      /*bit_valid*/ 0,
7451					      /*bit*/ 0);
7452			ctl_done((union ctl_io *)ctsio);
7453			return (1);
7454		}
7455
7456		for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7457			if (lun->per_res[i].registered
7458			 && memcmp(param->serv_act_res_key,
7459			    lun->per_res[i].res_key.key,
7460			    sizeof(struct scsi_per_res_key)) != 0)
7461				continue;
7462
7463			found = 1;
7464			lun->per_res[i].registered = 0;
7465			memset(&lun->per_res[i].res_key, 0,
7466			       sizeof(struct scsi_per_res_key));
7467			lun->pr_key_count--;
7468
7469			if (!persis_offset
7470			 && i < CTL_MAX_INITIATORS)
7471				lun->pending_sense[i].ua_pending |=
7472					CTL_UA_REG_PREEMPT;
7473			else if (persis_offset
7474			      && i >= persis_offset)
7475				lun->pending_sense[i-persis_offset].ua_pending|=
7476					CTL_UA_REG_PREEMPT;
7477		}
7478		mtx_unlock(&softc->ctl_lock);
7479		if (!found) {
7480			free(ctsio->kern_data_ptr, M_CTL);
7481			ctl_set_reservation_conflict(ctsio);
7482			ctl_done((union ctl_io *)ctsio);
7483			return (CTL_RETVAL_COMPLETE);
7484		}
7485		/* send msg to other side */
7486		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7487		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7488		persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7489		persis_io.pr.pr_info.residx = lun->pr_res_idx;
7490		persis_io.pr.pr_info.res_type = type;
7491		memcpy(persis_io.pr.pr_info.sa_res_key,
7492		       param->serv_act_res_key,
7493		       sizeof(param->serv_act_res_key));
7494		if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7495		     &persis_io, sizeof(persis_io), 0)) >
7496		     CTL_HA_STATUS_SUCCESS) {
7497			printf("CTL:Persis Out error returned from "
7498			       "ctl_ha_msg_send %d\n", isc_retval);
7499		}
7500	} else {
7501		/* Reserved but not all registrants */
7502		/* sa_res_key is res holder */
7503		if (memcmp(param->serv_act_res_key,
7504                   lun->per_res[lun->pr_res_idx].res_key.key,
7505                   sizeof(struct scsi_per_res_key)) == 0) {
7506			/* validate scope and type */
7507			if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7508			     SPR_LU_SCOPE) {
7509				ctl_set_invalid_field(/*ctsio*/ ctsio,
7510						      /*sks_valid*/ 1,
7511						      /*command*/ 1,
7512						      /*field*/ 2,
7513						      /*bit_valid*/ 1,
7514						      /*bit*/ 4);
7515				ctl_done((union ctl_io *)ctsio);
7516				return (1);
7517			}
7518
7519			if (type>8 || type==2 || type==4 || type==0) {
7520				ctl_set_invalid_field(/*ctsio*/ ctsio,
7521						      /*sks_valid*/ 1,
7522						      /*command*/ 1,
7523						      /*field*/ 2,
7524						      /*bit_valid*/ 1,
7525						      /*bit*/ 0);
7526				ctl_done((union ctl_io *)ctsio);
7527				return (1);
7528			}
7529
7530			/*
7531			 * Do the following:
7532			 * if sa_res_key != res_key remove all
7533			 * registrants w/sa_res_key and generate UA
7534			 * for these registrants(Registrations
7535			 * Preempted) if it wasn't an exclusive
7536			 * reservation generate UA(Reservations
7537			 * Preempted) for all other registered nexuses
7538			 * if the type has changed. Establish the new
7539			 * reservation and holder. If res_key and
7540			 * sa_res_key are the same do the above
7541			 * except don't unregister the res holder.
7542			 */
7543
7544			/*
7545			 * Temporarily unregister so it won't get
7546			 * removed or UA generated
7547			 */
7548			lun->per_res[residx].registered = 0;
7549			for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7550				if (lun->per_res[i].registered == 0)
7551					continue;
7552
7553				if (memcmp(param->serv_act_res_key,
7554				    lun->per_res[i].res_key.key,
7555				    sizeof(struct scsi_per_res_key)) == 0) {
7556					lun->per_res[i].registered = 0;
7557					memset(&lun->per_res[i].res_key,
7558					       0,
7559					       sizeof(struct scsi_per_res_key));
7560					lun->pr_key_count--;
7561
7562					if (!persis_offset
7563					 && i < CTL_MAX_INITIATORS)
7564						lun->pending_sense[i
7565							].ua_pending |=
7566							CTL_UA_REG_PREEMPT;
7567					else if (persis_offset
7568					      && i >= persis_offset)
7569						lun->pending_sense[
7570						  i-persis_offset].ua_pending |=
7571						  CTL_UA_REG_PREEMPT;
7572				} else if (type != lun->res_type
7573					&& (lun->res_type == SPR_TYPE_WR_EX_RO
7574					 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7575						if (!persis_offset
7576						 && i < CTL_MAX_INITIATORS)
7577							lun->pending_sense[i
7578							].ua_pending |=
7579							CTL_UA_RES_RELEASE;
7580						else if (persis_offset
7581						      && i >= persis_offset)
7582							lun->pending_sense[
7583							i-persis_offset
7584							].ua_pending |=
7585							CTL_UA_RES_RELEASE;
7586				}
7587			}
7588			lun->per_res[residx].registered = 1;
7589			lun->res_type = type;
7590			if (lun->res_type != SPR_TYPE_WR_EX_AR
7591			 && lun->res_type != SPR_TYPE_EX_AC_AR)
7592				lun->pr_res_idx = residx;
7593			else
7594				lun->pr_res_idx =
7595					CTL_PR_ALL_REGISTRANTS;
7596
7597			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7598			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7599			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7600			persis_io.pr.pr_info.residx = lun->pr_res_idx;
7601			persis_io.pr.pr_info.res_type = type;
7602			memcpy(persis_io.pr.pr_info.sa_res_key,
7603			       param->serv_act_res_key,
7604			       sizeof(param->serv_act_res_key));
7605			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7606			     &persis_io, sizeof(persis_io), 0)) >
7607			     CTL_HA_STATUS_SUCCESS) {
7608				printf("CTL:Persis Out error returned "
7609				       "from ctl_ha_msg_send %d\n",
7610				       isc_retval);
7611			}
7612		} else {
7613			/*
7614			 * sa_res_key is not the res holder just
7615			 * remove registrants
7616			 */
7617			int found=0;
7618			mtx_lock(&softc->ctl_lock);
7619
7620			for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7621				if (memcmp(param->serv_act_res_key,
7622				    lun->per_res[i].res_key.key,
7623				    sizeof(struct scsi_per_res_key)) != 0)
7624					continue;
7625
7626				found = 1;
7627				lun->per_res[i].registered = 0;
7628				memset(&lun->per_res[i].res_key, 0,
7629				       sizeof(struct scsi_per_res_key));
7630				lun->pr_key_count--;
7631
7632				if (!persis_offset
7633				 && i < CTL_MAX_INITIATORS)
7634					lun->pending_sense[i].ua_pending |=
7635						CTL_UA_REG_PREEMPT;
7636				else if (persis_offset
7637				      && i >= persis_offset)
7638					lun->pending_sense[
7639						i-persis_offset].ua_pending |=
7640						CTL_UA_REG_PREEMPT;
7641			}
7642
7643			if (!found) {
7644				mtx_unlock(&softc->ctl_lock);
7645				free(ctsio->kern_data_ptr, M_CTL);
7646				ctl_set_reservation_conflict(ctsio);
7647				ctl_done((union ctl_io *)ctsio);
7648		        	return (1);
7649			}
7650			mtx_unlock(&softc->ctl_lock);
7651			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7652			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7653			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7654			persis_io.pr.pr_info.residx = lun->pr_res_idx;
7655			persis_io.pr.pr_info.res_type = type;
7656			memcpy(persis_io.pr.pr_info.sa_res_key,
7657			       param->serv_act_res_key,
7658			       sizeof(param->serv_act_res_key));
7659			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7660			     &persis_io, sizeof(persis_io), 0)) >
7661			     CTL_HA_STATUS_SUCCESS) {
7662				printf("CTL:Persis Out error returned "
7663				       "from ctl_ha_msg_send %d\n",
7664				isc_retval);
7665			}
7666		}
7667	}
7668
7669	lun->PRGeneration++;
7670
7671	return (retval);
7672}
7673
7674static void
7675ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7676{
7677	int i;
7678
7679	if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7680	 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7681	 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7682		   msg->pr.pr_info.sa_res_key,
7683		   sizeof(struct scsi_per_res_key)) != 0) {
7684		uint64_t sa_res_key;
7685		sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7686
7687		if (sa_res_key == 0) {
7688			/* temporarily unregister this nexus */
7689			lun->per_res[msg->pr.pr_info.residx].registered = 0;
7690
7691			/*
7692			 * Unregister everybody else and build UA for
7693			 * them
7694			 */
7695			for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7696				if (lun->per_res[i].registered == 0)
7697					continue;
7698
7699				if (!persis_offset
7700				 && i < CTL_MAX_INITIATORS)
7701					lun->pending_sense[i].ua_pending |=
7702						CTL_UA_REG_PREEMPT;
7703				else if (persis_offset && i >= persis_offset)
7704					lun->pending_sense[i -
7705						persis_offset].ua_pending |=
7706						CTL_UA_REG_PREEMPT;
7707				lun->per_res[i].registered = 0;
7708				memset(&lun->per_res[i].res_key, 0,
7709				       sizeof(struct scsi_per_res_key));
7710			}
7711
7712			lun->per_res[msg->pr.pr_info.residx].registered = 1;
7713			lun->pr_key_count = 1;
7714			lun->res_type = msg->pr.pr_info.res_type;
7715			if (lun->res_type != SPR_TYPE_WR_EX_AR
7716			 && lun->res_type != SPR_TYPE_EX_AC_AR)
7717				lun->pr_res_idx = msg->pr.pr_info.residx;
7718		} else {
7719		        for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7720				if (memcmp(msg->pr.pr_info.sa_res_key,
7721		                   lun->per_res[i].res_key.key,
7722		                   sizeof(struct scsi_per_res_key)) != 0)
7723					continue;
7724
7725				lun->per_res[i].registered = 0;
7726				memset(&lun->per_res[i].res_key, 0,
7727				       sizeof(struct scsi_per_res_key));
7728				lun->pr_key_count--;
7729
7730				if (!persis_offset
7731				 && i < persis_offset)
7732					lun->pending_sense[i].ua_pending |=
7733						CTL_UA_REG_PREEMPT;
7734				else if (persis_offset
7735				      && i >= persis_offset)
7736					lun->pending_sense[i -
7737						persis_offset].ua_pending |=
7738						CTL_UA_REG_PREEMPT;
7739			}
7740		}
7741	} else {
7742		/*
7743		 * Temporarily unregister so it won't get removed
7744		 * or UA generated
7745		 */
7746		lun->per_res[msg->pr.pr_info.residx].registered = 0;
7747		for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7748			if (lun->per_res[i].registered == 0)
7749				continue;
7750
7751			if (memcmp(msg->pr.pr_info.sa_res_key,
7752	                   lun->per_res[i].res_key.key,
7753	                   sizeof(struct scsi_per_res_key)) == 0) {
7754				lun->per_res[i].registered = 0;
7755				memset(&lun->per_res[i].res_key, 0,
7756				       sizeof(struct scsi_per_res_key));
7757				lun->pr_key_count--;
7758				if (!persis_offset
7759				 && i < CTL_MAX_INITIATORS)
7760					lun->pending_sense[i].ua_pending |=
7761						CTL_UA_REG_PREEMPT;
7762				else if (persis_offset
7763				      && i >= persis_offset)
7764					lun->pending_sense[i -
7765						persis_offset].ua_pending |=
7766						CTL_UA_REG_PREEMPT;
7767			} else if (msg->pr.pr_info.res_type != lun->res_type
7768				&& (lun->res_type == SPR_TYPE_WR_EX_RO
7769				 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7770					if (!persis_offset
7771					 && i < persis_offset)
7772						lun->pending_sense[i
7773							].ua_pending |=
7774							CTL_UA_RES_RELEASE;
7775					else if (persis_offset
7776					      && i >= persis_offset)
7777					lun->pending_sense[i -
7778						persis_offset].ua_pending |=
7779						CTL_UA_RES_RELEASE;
7780			}
7781		}
7782		lun->per_res[msg->pr.pr_info.residx].registered = 1;
7783		lun->res_type = msg->pr.pr_info.res_type;
7784		if (lun->res_type != SPR_TYPE_WR_EX_AR
7785		 && lun->res_type != SPR_TYPE_EX_AC_AR)
7786			lun->pr_res_idx = msg->pr.pr_info.residx;
7787		else
7788			lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7789	}
7790	lun->PRGeneration++;
7791
7792}
7793
7794
7795int
7796ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
7797{
7798	int retval;
7799	int isc_retval;
7800	u_int32_t param_len;
7801	struct scsi_per_res_out *cdb;
7802	struct ctl_lun *lun;
7803	struct scsi_per_res_out_parms* param;
7804	struct ctl_softc *softc;
7805	uint32_t residx;
7806	uint64_t res_key, sa_res_key;
7807	uint8_t type;
7808	union ctl_ha_msg persis_io;
7809	int    i;
7810
7811	CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
7812
7813	retval = CTL_RETVAL_COMPLETE;
7814
7815	softc = control_softc;
7816
7817	cdb = (struct scsi_per_res_out *)ctsio->cdb;
7818	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7819
7820	/*
7821	 * We only support whole-LUN scope.  The scope & type are ignored for
7822	 * register, register and ignore existing key and clear.
7823	 * We sometimes ignore scope and type on preempts too!!
7824	 * Verify reservation type here as well.
7825	 */
7826	type = cdb->scope_type & SPR_TYPE_MASK;
7827	if ((cdb->action == SPRO_RESERVE)
7828	 || (cdb->action == SPRO_RELEASE)) {
7829		if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
7830			ctl_set_invalid_field(/*ctsio*/ ctsio,
7831					      /*sks_valid*/ 1,
7832					      /*command*/ 1,
7833					      /*field*/ 2,
7834					      /*bit_valid*/ 1,
7835					      /*bit*/ 4);
7836			ctl_done((union ctl_io *)ctsio);
7837			return (CTL_RETVAL_COMPLETE);
7838		}
7839
7840		if (type>8 || type==2 || type==4 || type==0) {
7841			ctl_set_invalid_field(/*ctsio*/ ctsio,
7842					      /*sks_valid*/ 1,
7843					      /*command*/ 1,
7844					      /*field*/ 2,
7845					      /*bit_valid*/ 1,
7846					      /*bit*/ 0);
7847			ctl_done((union ctl_io *)ctsio);
7848			return (CTL_RETVAL_COMPLETE);
7849		}
7850	}
7851
7852	switch (cdb->action & SPRO_ACTION_MASK) {
7853	case SPRO_REGISTER:
7854	case SPRO_RESERVE:
7855	case SPRO_RELEASE:
7856	case SPRO_CLEAR:
7857	case SPRO_PREEMPT:
7858	case SPRO_REG_IGNO:
7859		break;
7860	case SPRO_REG_MOVE:
7861	case SPRO_PRE_ABO:
7862	default:
7863		ctl_set_invalid_field(/*ctsio*/ ctsio,
7864				      /*sks_valid*/ 1,
7865				      /*command*/ 1,
7866				      /*field*/ 1,
7867				      /*bit_valid*/ 1,
7868				      /*bit*/ 0);
7869		ctl_done((union ctl_io *)ctsio);
7870		return (CTL_RETVAL_COMPLETE);
7871		break; /* NOTREACHED */
7872	}
7873
7874	param_len = scsi_4btoul(cdb->length);
7875
7876	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
7877		ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
7878		ctsio->kern_data_len = param_len;
7879		ctsio->kern_total_len = param_len;
7880		ctsio->kern_data_resid = 0;
7881		ctsio->kern_rel_offset = 0;
7882		ctsio->kern_sg_entries = 0;
7883		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7884		ctsio->be_move_done = ctl_config_move_done;
7885		ctl_datamove((union ctl_io *)ctsio);
7886
7887		return (CTL_RETVAL_COMPLETE);
7888	}
7889
7890	param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
7891
7892	residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
7893	res_key = scsi_8btou64(param->res_key.key);
7894	sa_res_key = scsi_8btou64(param->serv_act_res_key);
7895
7896	/*
7897	 * Validate the reservation key here except for SPRO_REG_IGNO
7898	 * This must be done for all other service actions
7899	 */
7900	if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
7901		mtx_lock(&softc->ctl_lock);
7902		if (lun->per_res[residx].registered) {
7903		    if (memcmp(param->res_key.key,
7904			       lun->per_res[residx].res_key.key,
7905			       ctl_min(sizeof(param->res_key),
7906			       sizeof(lun->per_res[residx].res_key))) != 0) {
7907				/*
7908				 * The current key passed in doesn't match
7909				 * the one the initiator previously
7910				 * registered.
7911				 */
7912				mtx_unlock(&softc->ctl_lock);
7913				free(ctsio->kern_data_ptr, M_CTL);
7914				ctl_set_reservation_conflict(ctsio);
7915				ctl_done((union ctl_io *)ctsio);
7916				return (CTL_RETVAL_COMPLETE);
7917			}
7918		} else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
7919			/*
7920			 * We are not registered
7921			 */
7922			mtx_unlock(&softc->ctl_lock);
7923			free(ctsio->kern_data_ptr, M_CTL);
7924			ctl_set_reservation_conflict(ctsio);
7925			ctl_done((union ctl_io *)ctsio);
7926			return (CTL_RETVAL_COMPLETE);
7927		} else if (res_key != 0) {
7928			/*
7929			 * We are not registered and trying to register but
7930			 * the register key isn't zero.
7931			 */
7932			mtx_unlock(&softc->ctl_lock);
7933			free(ctsio->kern_data_ptr, M_CTL);
7934			ctl_set_reservation_conflict(ctsio);
7935			ctl_done((union ctl_io *)ctsio);
7936			return (CTL_RETVAL_COMPLETE);
7937		}
7938		mtx_unlock(&softc->ctl_lock);
7939	}
7940
7941	switch (cdb->action & SPRO_ACTION_MASK) {
7942	case SPRO_REGISTER:
7943	case SPRO_REG_IGNO: {
7944
7945#if 0
7946		printf("Registration received\n");
7947#endif
7948
7949		/*
7950		 * We don't support any of these options, as we report in
7951		 * the read capabilities request (see
7952		 * ctl_persistent_reserve_in(), above).
7953		 */
7954		if ((param->flags & SPR_SPEC_I_PT)
7955		 || (param->flags & SPR_ALL_TG_PT)
7956		 || (param->flags & SPR_APTPL)) {
7957			int bit_ptr;
7958
7959			if (param->flags & SPR_APTPL)
7960				bit_ptr = 0;
7961			else if (param->flags & SPR_ALL_TG_PT)
7962				bit_ptr = 2;
7963			else /* SPR_SPEC_I_PT */
7964				bit_ptr = 3;
7965
7966			free(ctsio->kern_data_ptr, M_CTL);
7967			ctl_set_invalid_field(ctsio,
7968					      /*sks_valid*/ 1,
7969					      /*command*/ 0,
7970					      /*field*/ 20,
7971					      /*bit_valid*/ 1,
7972					      /*bit*/ bit_ptr);
7973			ctl_done((union ctl_io *)ctsio);
7974			return (CTL_RETVAL_COMPLETE);
7975		}
7976
7977		mtx_lock(&softc->ctl_lock);
7978
7979		/*
7980		 * The initiator wants to clear the
7981		 * key/unregister.
7982		 */
7983		if (sa_res_key == 0) {
7984			if ((res_key == 0
7985			  && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
7986			 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
7987			  && !lun->per_res[residx].registered)) {
7988				mtx_unlock(&softc->ctl_lock);
7989				goto done;
7990			}
7991
7992			lun->per_res[residx].registered = 0;
7993			memset(&lun->per_res[residx].res_key,
7994			       0, sizeof(lun->per_res[residx].res_key));
7995			lun->pr_key_count--;
7996
7997			if (residx == lun->pr_res_idx) {
7998				lun->flags &= ~CTL_LUN_PR_RESERVED;
7999				lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8000
8001				if ((lun->res_type == SPR_TYPE_WR_EX_RO
8002				  || lun->res_type == SPR_TYPE_EX_AC_RO)
8003				 && lun->pr_key_count) {
8004					/*
8005					 * If the reservation is a registrants
8006					 * only type we need to generate a UA
8007					 * for other registered inits.  The
8008					 * sense code should be RESERVATIONS
8009					 * RELEASED
8010					 */
8011
8012					for (i = 0; i < CTL_MAX_INITIATORS;i++){
8013						if (lun->per_res[
8014						    i+persis_offset].registered
8015						    == 0)
8016							continue;
8017						lun->pending_sense[i
8018							].ua_pending |=
8019							CTL_UA_RES_RELEASE;
8020					}
8021				}
8022				lun->res_type = 0;
8023			} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8024				if (lun->pr_key_count==0) {
8025					lun->flags &= ~CTL_LUN_PR_RESERVED;
8026					lun->res_type = 0;
8027					lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8028				}
8029			}
8030			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8031			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8032			persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8033			persis_io.pr.pr_info.residx = residx;
8034			if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8035			     &persis_io, sizeof(persis_io), 0 )) >
8036			     CTL_HA_STATUS_SUCCESS) {
8037				printf("CTL:Persis Out error returned from "
8038				       "ctl_ha_msg_send %d\n", isc_retval);
8039			}
8040			mtx_unlock(&softc->ctl_lock);
8041		} else /* sa_res_key != 0 */ {
8042
8043			/*
8044			 * If we aren't registered currently then increment
8045			 * the key count and set the registered flag.
8046			 */
8047			if (!lun->per_res[residx].registered) {
8048				lun->pr_key_count++;
8049				lun->per_res[residx].registered = 1;
8050			}
8051
8052			memcpy(&lun->per_res[residx].res_key,
8053			       param->serv_act_res_key,
8054			       ctl_min(sizeof(param->serv_act_res_key),
8055			       sizeof(lun->per_res[residx].res_key)));
8056
8057			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8058			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8059			persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8060			persis_io.pr.pr_info.residx = residx;
8061			memcpy(persis_io.pr.pr_info.sa_res_key,
8062			       param->serv_act_res_key,
8063			       sizeof(param->serv_act_res_key));
8064			mtx_unlock(&softc->ctl_lock);
8065			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8066			     &persis_io, sizeof(persis_io), 0)) >
8067			     CTL_HA_STATUS_SUCCESS) {
8068				printf("CTL:Persis Out error returned from "
8069				       "ctl_ha_msg_send %d\n", isc_retval);
8070			}
8071		}
8072		lun->PRGeneration++;
8073
8074		break;
8075	}
8076	case SPRO_RESERVE:
8077#if 0
8078                printf("Reserve executed type %d\n", type);
8079#endif
8080		mtx_lock(&softc->ctl_lock);
8081		if (lun->flags & CTL_LUN_PR_RESERVED) {
8082			/*
8083			 * if this isn't the reservation holder and it's
8084			 * not a "all registrants" type or if the type is
8085			 * different then we have a conflict
8086			 */
8087			if ((lun->pr_res_idx != residx
8088			  && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8089			 || lun->res_type != type) {
8090				mtx_unlock(&softc->ctl_lock);
8091				free(ctsio->kern_data_ptr, M_CTL);
8092				ctl_set_reservation_conflict(ctsio);
8093				ctl_done((union ctl_io *)ctsio);
8094				return (CTL_RETVAL_COMPLETE);
8095			}
8096			mtx_unlock(&softc->ctl_lock);
8097		} else /* create a reservation */ {
8098			/*
8099			 * If it's not an "all registrants" type record
8100			 * reservation holder
8101			 */
8102			if (type != SPR_TYPE_WR_EX_AR
8103			 && type != SPR_TYPE_EX_AC_AR)
8104				lun->pr_res_idx = residx; /* Res holder */
8105			else
8106				lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8107
8108			lun->flags |= CTL_LUN_PR_RESERVED;
8109			lun->res_type = type;
8110
8111			mtx_unlock(&softc->ctl_lock);
8112
8113			/* send msg to other side */
8114			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8115			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8116			persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8117			persis_io.pr.pr_info.residx = lun->pr_res_idx;
8118			persis_io.pr.pr_info.res_type = type;
8119			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8120			     &persis_io, sizeof(persis_io), 0)) >
8121			     CTL_HA_STATUS_SUCCESS) {
8122				printf("CTL:Persis Out error returned from "
8123				       "ctl_ha_msg_send %d\n", isc_retval);
8124			}
8125		}
8126		break;
8127
8128	case SPRO_RELEASE:
8129		mtx_lock(&softc->ctl_lock);
8130		if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8131			/* No reservation exists return good status */
8132			mtx_unlock(&softc->ctl_lock);
8133			goto done;
8134		}
8135		/*
8136		 * Is this nexus a reservation holder?
8137		 */
8138		if (lun->pr_res_idx != residx
8139		 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8140			/*
8141			 * not a res holder return good status but
8142			 * do nothing
8143			 */
8144			mtx_unlock(&softc->ctl_lock);
8145			goto done;
8146		}
8147
8148		if (lun->res_type != type) {
8149			mtx_unlock(&softc->ctl_lock);
8150			free(ctsio->kern_data_ptr, M_CTL);
8151			ctl_set_illegal_pr_release(ctsio);
8152			ctl_done((union ctl_io *)ctsio);
8153			return (CTL_RETVAL_COMPLETE);
8154		}
8155
8156		/* okay to release */
8157		lun->flags &= ~CTL_LUN_PR_RESERVED;
8158		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8159		lun->res_type = 0;
8160
8161		/*
8162		 * if this isn't an exclusive access
8163		 * res generate UA for all other
8164		 * registrants.
8165		 */
8166		if (type != SPR_TYPE_EX_AC
8167		 && type != SPR_TYPE_WR_EX) {
8168			/*
8169			 * temporarily unregister so we don't generate UA
8170			 */
8171			lun->per_res[residx].registered = 0;
8172
8173			for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8174				if (lun->per_res[i+persis_offset].registered
8175				    == 0)
8176					continue;
8177				lun->pending_sense[i].ua_pending |=
8178					CTL_UA_RES_RELEASE;
8179			}
8180
8181			lun->per_res[residx].registered = 1;
8182		}
8183		mtx_unlock(&softc->ctl_lock);
8184		/* Send msg to other side */
8185		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8186		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8187		persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8188		if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8189		     sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8190			printf("CTL:Persis Out error returned from "
8191			       "ctl_ha_msg_send %d\n", isc_retval);
8192		}
8193		break;
8194
8195	case SPRO_CLEAR:
8196		/* send msg to other side */
8197
8198		mtx_lock(&softc->ctl_lock);
8199		lun->flags &= ~CTL_LUN_PR_RESERVED;
8200		lun->res_type = 0;
8201		lun->pr_key_count = 0;
8202		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8203
8204
8205		memset(&lun->per_res[residx].res_key,
8206		       0, sizeof(lun->per_res[residx].res_key));
8207		lun->per_res[residx].registered = 0;
8208
8209		for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8210			if (lun->per_res[i].registered) {
8211				if (!persis_offset && i < CTL_MAX_INITIATORS)
8212					lun->pending_sense[i].ua_pending |=
8213						CTL_UA_RES_PREEMPT;
8214				else if (persis_offset && i >= persis_offset)
8215					lun->pending_sense[i-persis_offset
8216					    ].ua_pending |= CTL_UA_RES_PREEMPT;
8217
8218				memset(&lun->per_res[i].res_key,
8219				       0, sizeof(struct scsi_per_res_key));
8220				lun->per_res[i].registered = 0;
8221			}
8222		lun->PRGeneration++;
8223		mtx_unlock(&softc->ctl_lock);
8224		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8225		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8226		persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8227		if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8228		     sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8229			printf("CTL:Persis Out error returned from "
8230			       "ctl_ha_msg_send %d\n", isc_retval);
8231		}
8232		break;
8233
8234	case SPRO_PREEMPT: {
8235		int nretval;
8236
8237		nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8238					  residx, ctsio, cdb, param);
8239		if (nretval != 0)
8240			return (CTL_RETVAL_COMPLETE);
8241		break;
8242	}
8243	case SPRO_REG_MOVE:
8244	case SPRO_PRE_ABO:
8245	default:
8246		free(ctsio->kern_data_ptr, M_CTL);
8247		ctl_set_invalid_field(/*ctsio*/ ctsio,
8248				      /*sks_valid*/ 1,
8249				      /*command*/ 1,
8250				      /*field*/ 1,
8251				      /*bit_valid*/ 1,
8252				      /*bit*/ 0);
8253		ctl_done((union ctl_io *)ctsio);
8254		return (CTL_RETVAL_COMPLETE);
8255		break; /* NOTREACHED */
8256	}
8257
8258done:
8259	free(ctsio->kern_data_ptr, M_CTL);
8260	ctl_set_success(ctsio);
8261	ctl_done((union ctl_io *)ctsio);
8262
8263	return (retval);
8264}
8265
8266/*
8267 * This routine is for handling a message from the other SC pertaining to
8268 * persistent reserve out. All the error checking will have been done
8269 * so only perorming the action need be done here to keep the two
8270 * in sync.
8271 */
8272static void
8273ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8274{
8275	struct ctl_lun *lun;
8276	struct ctl_softc *softc;
8277	int i;
8278	uint32_t targ_lun;
8279
8280	softc = control_softc;
8281
8282	mtx_lock(&softc->ctl_lock);
8283
8284	targ_lun = msg->hdr.nexus.targ_lun;
8285	if (msg->hdr.nexus.lun_map_fn != NULL)
8286		targ_lun = msg->hdr.nexus.lun_map_fn(msg->hdr.nexus.lun_map_arg, targ_lun);
8287	lun = softc->ctl_luns[targ_lun];
8288	switch(msg->pr.pr_info.action) {
8289	case CTL_PR_REG_KEY:
8290		if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8291			lun->per_res[msg->pr.pr_info.residx].registered = 1;
8292			lun->pr_key_count++;
8293		}
8294		lun->PRGeneration++;
8295		memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8296		       msg->pr.pr_info.sa_res_key,
8297		       sizeof(struct scsi_per_res_key));
8298		break;
8299
8300	case CTL_PR_UNREG_KEY:
8301		lun->per_res[msg->pr.pr_info.residx].registered = 0;
8302		memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8303		       0, sizeof(struct scsi_per_res_key));
8304		lun->pr_key_count--;
8305
8306		/* XXX Need to see if the reservation has been released */
8307		/* if so do we need to generate UA? */
8308		if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8309			lun->flags &= ~CTL_LUN_PR_RESERVED;
8310			lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8311
8312			if ((lun->res_type == SPR_TYPE_WR_EX_RO
8313			  || lun->res_type == SPR_TYPE_EX_AC_RO)
8314			 && lun->pr_key_count) {
8315				/*
8316				 * If the reservation is a registrants
8317				 * only type we need to generate a UA
8318				 * for other registered inits.  The
8319				 * sense code should be RESERVATIONS
8320				 * RELEASED
8321				 */
8322
8323				for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8324					if (lun->per_res[i+
8325					    persis_offset].registered == 0)
8326						continue;
8327
8328					lun->pending_sense[i
8329						].ua_pending |=
8330						CTL_UA_RES_RELEASE;
8331				}
8332			}
8333			lun->res_type = 0;
8334		} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8335			if (lun->pr_key_count==0) {
8336				lun->flags &= ~CTL_LUN_PR_RESERVED;
8337				lun->res_type = 0;
8338				lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8339			}
8340		}
8341		lun->PRGeneration++;
8342		break;
8343
8344	case CTL_PR_RESERVE:
8345		lun->flags |= CTL_LUN_PR_RESERVED;
8346		lun->res_type = msg->pr.pr_info.res_type;
8347		lun->pr_res_idx = msg->pr.pr_info.residx;
8348
8349		break;
8350
8351	case CTL_PR_RELEASE:
8352		/*
8353		 * if this isn't an exclusive access res generate UA for all
8354		 * other registrants.
8355		 */
8356		if (lun->res_type != SPR_TYPE_EX_AC
8357		 && lun->res_type != SPR_TYPE_WR_EX) {
8358			for (i = 0; i < CTL_MAX_INITIATORS; i++)
8359				if (lun->per_res[i+persis_offset].registered)
8360					lun->pending_sense[i].ua_pending |=
8361						CTL_UA_RES_RELEASE;
8362		}
8363
8364		lun->flags &= ~CTL_LUN_PR_RESERVED;
8365		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8366		lun->res_type = 0;
8367		break;
8368
8369	case CTL_PR_PREEMPT:
8370		ctl_pro_preempt_other(lun, msg);
8371		break;
8372	case CTL_PR_CLEAR:
8373		lun->flags &= ~CTL_LUN_PR_RESERVED;
8374		lun->res_type = 0;
8375		lun->pr_key_count = 0;
8376		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8377
8378		for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8379			if (lun->per_res[i].registered == 0)
8380				continue;
8381			if (!persis_offset
8382			 && i < CTL_MAX_INITIATORS)
8383				lun->pending_sense[i].ua_pending |=
8384					CTL_UA_RES_PREEMPT;
8385			else if (persis_offset
8386			      && i >= persis_offset)
8387   				lun->pending_sense[i-persis_offset].ua_pending|=
8388					CTL_UA_RES_PREEMPT;
8389			memset(&lun->per_res[i].res_key, 0,
8390			       sizeof(struct scsi_per_res_key));
8391			lun->per_res[i].registered = 0;
8392		}
8393		lun->PRGeneration++;
8394		break;
8395	}
8396
8397	mtx_unlock(&softc->ctl_lock);
8398}
8399
8400int
8401ctl_read_write(struct ctl_scsiio *ctsio)
8402{
8403	struct ctl_lun *lun;
8404	struct ctl_lba_len lbalen;
8405	uint64_t lba;
8406	uint32_t num_blocks;
8407	int reladdr, fua, dpo, ebp;
8408	int retval;
8409	int isread;
8410
8411	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8412
8413	CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8414
8415	reladdr = 0;
8416	fua = 0;
8417	dpo = 0;
8418	ebp = 0;
8419
8420	retval = CTL_RETVAL_COMPLETE;
8421
8422	isread = ctsio->cdb[0] == READ_6  || ctsio->cdb[0] == READ_10
8423	      || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8424	if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8425		uint32_t residx;
8426
8427		/*
8428		 * XXX KDM need a lock here.
8429		 */
8430		residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8431		if ((lun->res_type == SPR_TYPE_EX_AC
8432		  && residx != lun->pr_res_idx)
8433		 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8434		   || lun->res_type == SPR_TYPE_EX_AC_AR)
8435		  && !lun->per_res[residx].registered)) {
8436			ctl_set_reservation_conflict(ctsio);
8437			ctl_done((union ctl_io *)ctsio);
8438			return (CTL_RETVAL_COMPLETE);
8439	        }
8440	}
8441
8442	switch (ctsio->cdb[0]) {
8443	case READ_6:
8444	case WRITE_6: {
8445		struct scsi_rw_6 *cdb;
8446
8447		cdb = (struct scsi_rw_6 *)ctsio->cdb;
8448
8449		lba = scsi_3btoul(cdb->addr);
8450		/* only 5 bits are valid in the most significant address byte */
8451		lba &= 0x1fffff;
8452		num_blocks = cdb->length;
8453		/*
8454		 * This is correct according to SBC-2.
8455		 */
8456		if (num_blocks == 0)
8457			num_blocks = 256;
8458		break;
8459	}
8460	case READ_10:
8461	case WRITE_10: {
8462		struct scsi_rw_10 *cdb;
8463
8464		cdb = (struct scsi_rw_10 *)ctsio->cdb;
8465
8466		if (cdb->byte2 & SRW10_RELADDR)
8467			reladdr = 1;
8468		if (cdb->byte2 & SRW10_FUA)
8469			fua = 1;
8470		if (cdb->byte2 & SRW10_DPO)
8471			dpo = 1;
8472
8473		if ((cdb->opcode == WRITE_10)
8474		 && (cdb->byte2 & SRW10_EBP))
8475			ebp = 1;
8476
8477		lba = scsi_4btoul(cdb->addr);
8478		num_blocks = scsi_2btoul(cdb->length);
8479		break;
8480	}
8481	case WRITE_VERIFY_10: {
8482		struct scsi_write_verify_10 *cdb;
8483
8484		cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8485
8486		/*
8487		 * XXX KDM we should do actual write verify support at some
8488		 * point.  This is obviously fake, we're just translating
8489		 * things to a write.  So we don't even bother checking the
8490		 * BYTCHK field, since we don't do any verification.  If
8491		 * the user asks for it, we'll just pretend we did it.
8492		 */
8493		if (cdb->byte2 & SWV_DPO)
8494			dpo = 1;
8495
8496		lba = scsi_4btoul(cdb->addr);
8497		num_blocks = scsi_2btoul(cdb->length);
8498		break;
8499	}
8500	case READ_12:
8501	case WRITE_12: {
8502		struct scsi_rw_12 *cdb;
8503
8504		cdb = (struct scsi_rw_12 *)ctsio->cdb;
8505
8506		if (cdb->byte2 & SRW12_RELADDR)
8507			reladdr = 1;
8508		if (cdb->byte2 & SRW12_FUA)
8509			fua = 1;
8510		if (cdb->byte2 & SRW12_DPO)
8511			dpo = 1;
8512		lba = scsi_4btoul(cdb->addr);
8513		num_blocks = scsi_4btoul(cdb->length);
8514		break;
8515	}
8516	case WRITE_VERIFY_12: {
8517		struct scsi_write_verify_12 *cdb;
8518
8519		cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8520
8521		if (cdb->byte2 & SWV_DPO)
8522			dpo = 1;
8523
8524		lba = scsi_4btoul(cdb->addr);
8525		num_blocks = scsi_4btoul(cdb->length);
8526
8527		break;
8528	}
8529	case READ_16:
8530	case WRITE_16: {
8531		struct scsi_rw_16 *cdb;
8532
8533		cdb = (struct scsi_rw_16 *)ctsio->cdb;
8534
8535		if (cdb->byte2 & SRW12_RELADDR)
8536			reladdr = 1;
8537		if (cdb->byte2 & SRW12_FUA)
8538			fua = 1;
8539		if (cdb->byte2 & SRW12_DPO)
8540			dpo = 1;
8541
8542		lba = scsi_8btou64(cdb->addr);
8543		num_blocks = scsi_4btoul(cdb->length);
8544		break;
8545	}
8546	case WRITE_VERIFY_16: {
8547		struct scsi_write_verify_16 *cdb;
8548
8549		cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8550
8551		if (cdb->byte2 & SWV_DPO)
8552			dpo = 1;
8553
8554		lba = scsi_8btou64(cdb->addr);
8555		num_blocks = scsi_4btoul(cdb->length);
8556		break;
8557	}
8558	default:
8559		/*
8560		 * We got a command we don't support.  This shouldn't
8561		 * happen, commands should be filtered out above us.
8562		 */
8563		ctl_set_invalid_opcode(ctsio);
8564		ctl_done((union ctl_io *)ctsio);
8565
8566		return (CTL_RETVAL_COMPLETE);
8567		break; /* NOTREACHED */
8568	}
8569
8570	/*
8571	 * XXX KDM what do we do with the DPO and FUA bits?  FUA might be
8572	 * interesting for us, but if RAIDCore is in write-back mode,
8573	 * getting it to do write-through for a particular transaction may
8574	 * not be possible.
8575	 */
8576	/*
8577	 * We don't support relative addressing.  That also requires
8578	 * supporting linked commands, which we don't do.
8579	 */
8580	if (reladdr != 0) {
8581		ctl_set_invalid_field(ctsio,
8582				      /*sks_valid*/ 1,
8583				      /*command*/ 1,
8584				      /*field*/ 1,
8585				      /*bit_valid*/ 1,
8586				      /*bit*/ 0);
8587		ctl_done((union ctl_io *)ctsio);
8588		return (CTL_RETVAL_COMPLETE);
8589	}
8590
8591	/*
8592	 * The first check is to make sure we're in bounds, the second
8593	 * check is to catch wrap-around problems.  If the lba + num blocks
8594	 * is less than the lba, then we've wrapped around and the block
8595	 * range is invalid anyway.
8596	 */
8597	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8598	 || ((lba + num_blocks) < lba)) {
8599		ctl_set_lba_out_of_range(ctsio);
8600		ctl_done((union ctl_io *)ctsio);
8601		return (CTL_RETVAL_COMPLETE);
8602	}
8603
8604	/*
8605	 * According to SBC-3, a transfer length of 0 is not an error.
8606	 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8607	 * translates to 256 blocks for those commands.
8608	 */
8609	if (num_blocks == 0) {
8610		ctl_set_success(ctsio);
8611		ctl_done((union ctl_io *)ctsio);
8612		return (CTL_RETVAL_COMPLETE);
8613	}
8614
8615	lbalen.lba = lba;
8616	lbalen.len = num_blocks;
8617	memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
8618	       sizeof(lbalen));
8619
8620	CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8621
8622	retval = lun->backend->data_submit((union ctl_io *)ctsio);
8623
8624	return (retval);
8625}
8626
8627int
8628ctl_report_luns(struct ctl_scsiio *ctsio)
8629{
8630	struct scsi_report_luns *cdb;
8631	struct scsi_report_luns_data *lun_data;
8632	struct ctl_lun *lun, *request_lun;
8633	int num_luns, retval;
8634	uint32_t alloc_len, lun_datalen;
8635	int num_filled, well_known;
8636	uint32_t initidx, targ_lun_id, lun_id;
8637
8638	retval = CTL_RETVAL_COMPLETE;
8639	well_known = 0;
8640
8641	cdb = (struct scsi_report_luns *)ctsio->cdb;
8642
8643	CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8644
8645	mtx_lock(&control_softc->ctl_lock);
8646	num_luns = control_softc->num_luns;
8647	mtx_unlock(&control_softc->ctl_lock);
8648
8649	switch (cdb->select_report) {
8650	case RPL_REPORT_DEFAULT:
8651	case RPL_REPORT_ALL:
8652		break;
8653	case RPL_REPORT_WELLKNOWN:
8654		well_known = 1;
8655		num_luns = 0;
8656		break;
8657	default:
8658		ctl_set_invalid_field(ctsio,
8659				      /*sks_valid*/ 1,
8660				      /*command*/ 1,
8661				      /*field*/ 2,
8662				      /*bit_valid*/ 0,
8663				      /*bit*/ 0);
8664		ctl_done((union ctl_io *)ctsio);
8665		return (retval);
8666		break; /* NOTREACHED */
8667	}
8668
8669	alloc_len = scsi_4btoul(cdb->length);
8670	/*
8671	 * The initiator has to allocate at least 16 bytes for this request,
8672	 * so he can at least get the header and the first LUN.  Otherwise
8673	 * we reject the request (per SPC-3 rev 14, section 6.21).
8674	 */
8675	if (alloc_len < (sizeof(struct scsi_report_luns_data) +
8676	    sizeof(struct scsi_report_luns_lundata))) {
8677		ctl_set_invalid_field(ctsio,
8678				      /*sks_valid*/ 1,
8679				      /*command*/ 1,
8680				      /*field*/ 6,
8681				      /*bit_valid*/ 0,
8682				      /*bit*/ 0);
8683		ctl_done((union ctl_io *)ctsio);
8684		return (retval);
8685	}
8686
8687	request_lun = (struct ctl_lun *)
8688		ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8689
8690	lun_datalen = sizeof(*lun_data) +
8691		(num_luns * sizeof(struct scsi_report_luns_lundata));
8692
8693	ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
8694	lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
8695	ctsio->kern_sg_entries = 0;
8696
8697	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8698
8699	mtx_lock(&control_softc->ctl_lock);
8700	for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
8701		lun_id = targ_lun_id;
8702		if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
8703			lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
8704		if (lun_id >= CTL_MAX_LUNS)
8705			continue;
8706		lun = control_softc->ctl_luns[lun_id];
8707		if (lun == NULL)
8708			continue;
8709
8710		if (targ_lun_id <= 0xff) {
8711			/*
8712			 * Peripheral addressing method, bus number 0.
8713			 */
8714			lun_data->luns[num_filled].lundata[0] =
8715				RPL_LUNDATA_ATYP_PERIPH;
8716			lun_data->luns[num_filled].lundata[1] = targ_lun_id;
8717			num_filled++;
8718		} else if (targ_lun_id <= 0x3fff) {
8719			/*
8720			 * Flat addressing method.
8721			 */
8722			lun_data->luns[num_filled].lundata[0] =
8723				RPL_LUNDATA_ATYP_FLAT |
8724				(targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
8725#ifdef OLDCTLHEADERS
8726				(SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
8727				(targ_lun_id & SRLD_BUS_LUN_MASK);
8728#endif
8729			lun_data->luns[num_filled].lundata[1] =
8730#ifdef OLDCTLHEADERS
8731				targ_lun_id >> SRLD_BUS_LUN_BITS;
8732#endif
8733				targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
8734			num_filled++;
8735		} else {
8736			printf("ctl_report_luns: bogus LUN number %jd, "
8737			       "skipping\n", (intmax_t)targ_lun_id);
8738		}
8739		/*
8740		 * According to SPC-3, rev 14 section 6.21:
8741		 *
8742		 * "The execution of a REPORT LUNS command to any valid and
8743		 * installed logical unit shall clear the REPORTED LUNS DATA
8744		 * HAS CHANGED unit attention condition for all logical
8745		 * units of that target with respect to the requesting
8746		 * initiator. A valid and installed logical unit is one
8747		 * having a PERIPHERAL QUALIFIER of 000b in the standard
8748		 * INQUIRY data (see 6.4.2)."
8749		 *
8750		 * If request_lun is NULL, the LUN this report luns command
8751		 * was issued to is either disabled or doesn't exist. In that
8752		 * case, we shouldn't clear any pending lun change unit
8753		 * attention.
8754		 */
8755		if (request_lun != NULL)
8756			lun->pending_sense[initidx].ua_pending &=
8757				~CTL_UA_LUN_CHANGE;
8758	}
8759	mtx_unlock(&control_softc->ctl_lock);
8760
8761	/*
8762	 * It's quite possible that we've returned fewer LUNs than we allocated
8763	 * space for.  Trim it.
8764	 */
8765	lun_datalen = sizeof(*lun_data) +
8766		(num_filled * sizeof(struct scsi_report_luns_lundata));
8767
8768	if (lun_datalen < alloc_len) {
8769		ctsio->residual = alloc_len - lun_datalen;
8770		ctsio->kern_data_len = lun_datalen;
8771		ctsio->kern_total_len = lun_datalen;
8772	} else {
8773		ctsio->residual = 0;
8774		ctsio->kern_data_len = alloc_len;
8775		ctsio->kern_total_len = alloc_len;
8776	}
8777	ctsio->kern_data_resid = 0;
8778	ctsio->kern_rel_offset = 0;
8779	ctsio->kern_sg_entries = 0;
8780
8781	/*
8782	 * We set this to the actual data length, regardless of how much
8783	 * space we actually have to return results.  If the user looks at
8784	 * this value, he'll know whether or not he allocated enough space
8785	 * and reissue the command if necessary.  We don't support well
8786	 * known logical units, so if the user asks for that, return none.
8787	 */
8788	scsi_ulto4b(lun_datalen - 8, lun_data->length);
8789
8790	/*
8791	 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
8792	 * this request.
8793	 */
8794	ctsio->scsi_status = SCSI_STATUS_OK;
8795
8796	ctsio->be_move_done = ctl_config_move_done;
8797	ctl_datamove((union ctl_io *)ctsio);
8798
8799	return (retval);
8800}
8801
8802int
8803ctl_request_sense(struct ctl_scsiio *ctsio)
8804{
8805	struct scsi_request_sense *cdb;
8806	struct scsi_sense_data *sense_ptr;
8807	struct ctl_lun *lun;
8808	uint32_t initidx;
8809	int have_error;
8810	scsi_sense_data_type sense_format;
8811
8812	cdb = (struct scsi_request_sense *)ctsio->cdb;
8813
8814	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8815
8816	CTL_DEBUG_PRINT(("ctl_request_sense\n"));
8817
8818	/*
8819	 * Determine which sense format the user wants.
8820	 */
8821	if (cdb->byte2 & SRS_DESC)
8822		sense_format = SSD_TYPE_DESC;
8823	else
8824		sense_format = SSD_TYPE_FIXED;
8825
8826	ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
8827	sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
8828	ctsio->kern_sg_entries = 0;
8829
8830	/*
8831	 * struct scsi_sense_data, which is currently set to 256 bytes, is
8832	 * larger than the largest allowed value for the length field in the
8833	 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
8834	 */
8835	ctsio->residual = 0;
8836	ctsio->kern_data_len = cdb->length;
8837	ctsio->kern_total_len = cdb->length;
8838
8839	ctsio->kern_data_resid = 0;
8840	ctsio->kern_rel_offset = 0;
8841	ctsio->kern_sg_entries = 0;
8842
8843	/*
8844	 * If we don't have a LUN, we don't have any pending sense.
8845	 */
8846	if (lun == NULL)
8847		goto no_sense;
8848
8849	have_error = 0;
8850	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8851	/*
8852	 * Check for pending sense, and then for pending unit attentions.
8853	 * Pending sense gets returned first, then pending unit attentions.
8854	 */
8855	mtx_lock(&lun->ctl_softc->ctl_lock);
8856	if (ctl_is_set(lun->have_ca, initidx)) {
8857		scsi_sense_data_type stored_format;
8858
8859		/*
8860		 * Check to see which sense format was used for the stored
8861		 * sense data.
8862		 */
8863		stored_format = scsi_sense_type(
8864		    &lun->pending_sense[initidx].sense);
8865
8866		/*
8867		 * If the user requested a different sense format than the
8868		 * one we stored, then we need to convert it to the other
8869		 * format.  If we're going from descriptor to fixed format
8870		 * sense data, we may lose things in translation, depending
8871		 * on what options were used.
8872		 *
8873		 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
8874		 * for some reason we'll just copy it out as-is.
8875		 */
8876		if ((stored_format == SSD_TYPE_FIXED)
8877		 && (sense_format == SSD_TYPE_DESC))
8878			ctl_sense_to_desc((struct scsi_sense_data_fixed *)
8879			    &lun->pending_sense[initidx].sense,
8880			    (struct scsi_sense_data_desc *)sense_ptr);
8881		else if ((stored_format == SSD_TYPE_DESC)
8882		      && (sense_format == SSD_TYPE_FIXED))
8883			ctl_sense_to_fixed((struct scsi_sense_data_desc *)
8884			    &lun->pending_sense[initidx].sense,
8885			    (struct scsi_sense_data_fixed *)sense_ptr);
8886		else
8887			memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
8888			       ctl_min(sizeof(*sense_ptr),
8889			       sizeof(lun->pending_sense[initidx].sense)));
8890
8891		ctl_clear_mask(lun->have_ca, initidx);
8892		have_error = 1;
8893	} else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
8894		ctl_ua_type ua_type;
8895
8896		ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
8897				       sense_ptr, sense_format);
8898		if (ua_type != CTL_UA_NONE) {
8899			have_error = 1;
8900			/* We're reporting this UA, so clear it */
8901			lun->pending_sense[initidx].ua_pending &= ~ua_type;
8902		}
8903	}
8904	mtx_unlock(&lun->ctl_softc->ctl_lock);
8905
8906	/*
8907	 * We already have a pending error, return it.
8908	 */
8909	if (have_error != 0) {
8910		/*
8911		 * We report the SCSI status as OK, since the status of the
8912		 * request sense command itself is OK.
8913		 */
8914		ctsio->scsi_status = SCSI_STATUS_OK;
8915
8916		/*
8917		 * We report 0 for the sense length, because we aren't doing
8918		 * autosense in this case.  We're reporting sense as
8919		 * parameter data.
8920		 */
8921		ctsio->sense_len = 0;
8922
8923		ctsio->be_move_done = ctl_config_move_done;
8924		ctl_datamove((union ctl_io *)ctsio);
8925
8926		return (CTL_RETVAL_COMPLETE);
8927	}
8928
8929no_sense:
8930
8931	/*
8932	 * No sense information to report, so we report that everything is
8933	 * okay.
8934	 */
8935	ctl_set_sense_data(sense_ptr,
8936			   lun,
8937			   sense_format,
8938			   /*current_error*/ 1,
8939			   /*sense_key*/ SSD_KEY_NO_SENSE,
8940			   /*asc*/ 0x00,
8941			   /*ascq*/ 0x00,
8942			   SSD_ELEM_NONE);
8943
8944	ctsio->scsi_status = SCSI_STATUS_OK;
8945
8946	/*
8947	 * We report 0 for the sense length, because we aren't doing
8948	 * autosense in this case.  We're reporting sense as parameter data.
8949	 */
8950	ctsio->sense_len = 0;
8951	ctsio->be_move_done = ctl_config_move_done;
8952	ctl_datamove((union ctl_io *)ctsio);
8953
8954	return (CTL_RETVAL_COMPLETE);
8955}
8956
8957int
8958ctl_tur(struct ctl_scsiio *ctsio)
8959{
8960	struct ctl_lun *lun;
8961
8962	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8963
8964	CTL_DEBUG_PRINT(("ctl_tur\n"));
8965
8966	if (lun == NULL)
8967		return (-EINVAL);
8968
8969	ctsio->scsi_status = SCSI_STATUS_OK;
8970	ctsio->io_hdr.status = CTL_SUCCESS;
8971
8972	ctl_done((union ctl_io *)ctsio);
8973
8974	return (CTL_RETVAL_COMPLETE);
8975}
8976
8977#ifdef notyet
8978static int
8979ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
8980{
8981
8982}
8983#endif
8984
8985static int
8986ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
8987{
8988	struct scsi_vpd_supported_pages *pages;
8989	int sup_page_size;
8990	struct ctl_lun *lun;
8991
8992	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8993
8994	sup_page_size = sizeof(struct scsi_vpd_supported_pages) +
8995		SCSI_EVPD_NUM_SUPPORTED_PAGES;
8996	ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
8997	pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
8998	ctsio->kern_sg_entries = 0;
8999
9000	if (sup_page_size < alloc_len) {
9001		ctsio->residual = alloc_len - sup_page_size;
9002		ctsio->kern_data_len = sup_page_size;
9003		ctsio->kern_total_len = sup_page_size;
9004	} else {
9005		ctsio->residual = 0;
9006		ctsio->kern_data_len = alloc_len;
9007		ctsio->kern_total_len = alloc_len;
9008	}
9009	ctsio->kern_data_resid = 0;
9010	ctsio->kern_rel_offset = 0;
9011	ctsio->kern_sg_entries = 0;
9012
9013	/*
9014	 * The control device is always connected.  The disk device, on the
9015	 * other hand, may not be online all the time.  Need to change this
9016	 * to figure out whether the disk device is actually online or not.
9017	 */
9018	if (lun != NULL)
9019		pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9020				lun->be_lun->lun_type;
9021	else
9022		pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9023
9024	pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9025	/* Supported VPD pages */
9026	pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9027	/* Serial Number */
9028	pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9029	/* Device Identification */
9030	pages->page_list[2] = SVPD_DEVICE_ID;
9031
9032	ctsio->scsi_status = SCSI_STATUS_OK;
9033
9034	ctsio->be_move_done = ctl_config_move_done;
9035	ctl_datamove((union ctl_io *)ctsio);
9036
9037	return (CTL_RETVAL_COMPLETE);
9038}
9039
9040static int
9041ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9042{
9043	struct scsi_vpd_unit_serial_number *sn_ptr;
9044	struct ctl_lun *lun;
9045#ifndef CTL_USE_BACKEND_SN
9046	char tmpstr[32];
9047#endif
9048
9049	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9050
9051	ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9052	sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9053	ctsio->kern_sg_entries = 0;
9054
9055	if (sizeof(*sn_ptr) < alloc_len) {
9056		ctsio->residual = alloc_len - sizeof(*sn_ptr);
9057		ctsio->kern_data_len = sizeof(*sn_ptr);
9058		ctsio->kern_total_len = sizeof(*sn_ptr);
9059	} else {
9060		ctsio->residual = 0;
9061		ctsio->kern_data_len = alloc_len;
9062		ctsio->kern_total_len = alloc_len;
9063	}
9064	ctsio->kern_data_resid = 0;
9065	ctsio->kern_rel_offset = 0;
9066	ctsio->kern_sg_entries = 0;
9067
9068	/*
9069	 * The control device is always connected.  The disk device, on the
9070	 * other hand, may not be online all the time.  Need to change this
9071	 * to figure out whether the disk device is actually online or not.
9072	 */
9073	if (lun != NULL)
9074		sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9075				  lun->be_lun->lun_type;
9076	else
9077		sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9078
9079	sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9080	sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9081#ifdef CTL_USE_BACKEND_SN
9082	/*
9083	 * If we don't have a LUN, we just leave the serial number as
9084	 * all spaces.
9085	 */
9086	memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9087	if (lun != NULL) {
9088		strncpy((char *)sn_ptr->serial_num,
9089			(char *)lun->be_lun->serial_num, CTL_SN_LEN);
9090	}
9091#else
9092	/*
9093	 * Note that we're using a non-unique serial number here,
9094	 */
9095	snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9096	memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9097	strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9098		ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9099#endif
9100	ctsio->scsi_status = SCSI_STATUS_OK;
9101
9102	ctsio->be_move_done = ctl_config_move_done;
9103	ctl_datamove((union ctl_io *)ctsio);
9104
9105	return (CTL_RETVAL_COMPLETE);
9106}
9107
9108
9109static int
9110ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9111{
9112	struct scsi_vpd_device_id *devid_ptr;
9113	struct scsi_vpd_id_descriptor *desc, *desc1;
9114	struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9115	struct scsi_vpd_id_t10 *t10id;
9116	struct ctl_softc *ctl_softc;
9117	struct ctl_lun *lun;
9118	struct ctl_frontend *fe;
9119#ifndef CTL_USE_BACKEND_SN
9120	char tmpstr[32];
9121#endif /* CTL_USE_BACKEND_SN */
9122	int devid_len;
9123
9124	ctl_softc = control_softc;
9125
9126	mtx_lock(&ctl_softc->ctl_lock);
9127	fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9128	mtx_unlock(&ctl_softc->ctl_lock);
9129
9130	if (fe->devid != NULL)
9131		return ((fe->devid)(ctsio, alloc_len));
9132
9133	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9134
9135	devid_len = sizeof(struct scsi_vpd_device_id) +
9136		sizeof(struct scsi_vpd_id_descriptor) +
9137		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9138		sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9139		sizeof(struct scsi_vpd_id_descriptor) +
9140		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9141		sizeof(struct scsi_vpd_id_descriptor) +
9142		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9143
9144	ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
9145	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9146	ctsio->kern_sg_entries = 0;
9147
9148	if (devid_len < alloc_len) {
9149		ctsio->residual = alloc_len - devid_len;
9150		ctsio->kern_data_len = devid_len;
9151		ctsio->kern_total_len = devid_len;
9152	} else {
9153		ctsio->residual = 0;
9154		ctsio->kern_data_len = alloc_len;
9155		ctsio->kern_total_len = alloc_len;
9156	}
9157	ctsio->kern_data_resid = 0;
9158	ctsio->kern_rel_offset = 0;
9159	ctsio->kern_sg_entries = 0;
9160
9161	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9162	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9163	desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9164		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9165	desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9166	          CTL_WWPN_LEN);
9167	desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9168	         sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9169
9170	/*
9171	 * The control device is always connected.  The disk device, on the
9172	 * other hand, may not be online all the time.
9173	 */
9174	if (lun != NULL)
9175		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9176				     lun->be_lun->lun_type;
9177	else
9178		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9179
9180	devid_ptr->page_code = SVPD_DEVICE_ID;
9181
9182	scsi_ulto2b(devid_len - 4, devid_ptr->length);
9183
9184	mtx_lock(&ctl_softc->ctl_lock);
9185
9186	/*
9187	 * For Fibre channel,
9188	 */
9189	if (fe->port_type == CTL_PORT_FC)
9190	{
9191		desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9192				      SVPD_ID_CODESET_ASCII;
9193        	desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9194		              SVPD_ID_CODESET_BINARY;
9195	}
9196	else
9197	{
9198		desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9199				      SVPD_ID_CODESET_ASCII;
9200        	desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9201		              SVPD_ID_CODESET_BINARY;
9202	}
9203	desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9204	mtx_unlock(&ctl_softc->ctl_lock);
9205
9206	/*
9207	 * We're using a LUN association here.  i.e., this device ID is a
9208	 * per-LUN identifier.
9209	 */
9210	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9211	desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9212	strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9213
9214	/*
9215	 * desc1 is for the WWPN which is a port asscociation.
9216	 */
9217	desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9218	desc1->length = CTL_WWPN_LEN;
9219	/* XXX Call Reggie's get_WWNN func here then add port # to the end */
9220	/* For testing just create the WWPN */
9221#if 0
9222	ddb_GetWWNN((char *)desc1->identifier);
9223
9224	/* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9225	/* This is so Copancontrol will return something sane */
9226	if (ctsio->io_hdr.nexus.targ_port!=0 &&
9227	    ctsio->io_hdr.nexus.targ_port!=8)
9228		desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9229	else
9230		desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9231#endif
9232
9233	be64enc(desc1->identifier, fe->wwpn);
9234
9235	/*
9236	 * desc2 is for the Relative Target Port(type 4h) identifier
9237	 */
9238	desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9239	                 | SVPD_ID_TYPE_RELTARG;
9240	desc2->length = 4;
9241//#if 0
9242	/* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9243	/* This is so Copancontrol will return something sane */
9244	if (ctsio->io_hdr.nexus.targ_port!=0 &&
9245	    ctsio->io_hdr.nexus.targ_port!=8)
9246		desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9247	else
9248	        desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9249//#endif
9250
9251	/*
9252	 * desc3 is for the Target Port Group(type 5h) identifier
9253	 */
9254	desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9255	                 | SVPD_ID_TYPE_TPORTGRP;
9256	desc3->length = 4;
9257	if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9258		desc3->identifier[3] = 1;
9259	else
9260		desc3->identifier[3] = 2;
9261
9262#ifdef CTL_USE_BACKEND_SN
9263	/*
9264	 * If we've actually got a backend, copy the device id from the
9265	 * per-LUN data.  Otherwise, set it to all spaces.
9266	 */
9267	if (lun != NULL) {
9268		/*
9269		 * Copy the backend's LUN ID.
9270		 */
9271		strncpy((char *)t10id->vendor_spec_id,
9272			(char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9273	} else {
9274		/*
9275		 * No backend, set this to spaces.
9276		 */
9277		memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9278	}
9279#else
9280	snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9281		 (lun != NULL) ?  (int)lun->lun : 0);
9282	strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9283		sizeof(tmpstr)));
9284#endif
9285
9286	ctsio->scsi_status = SCSI_STATUS_OK;
9287
9288	ctsio->be_move_done = ctl_config_move_done;
9289	ctl_datamove((union ctl_io *)ctsio);
9290
9291	return (CTL_RETVAL_COMPLETE);
9292}
9293
9294static int
9295ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9296{
9297	struct scsi_inquiry *cdb;
9298	int alloc_len, retval;
9299
9300	cdb = (struct scsi_inquiry *)ctsio->cdb;
9301
9302	retval = CTL_RETVAL_COMPLETE;
9303
9304	alloc_len = scsi_2btoul(cdb->length);
9305
9306	switch (cdb->page_code) {
9307	case SVPD_SUPPORTED_PAGES:
9308		retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9309		break;
9310	case SVPD_UNIT_SERIAL_NUMBER:
9311		retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9312		break;
9313	case SVPD_DEVICE_ID:
9314		retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9315		break;
9316	default:
9317		ctl_set_invalid_field(ctsio,
9318				      /*sks_valid*/ 1,
9319				      /*command*/ 1,
9320				      /*field*/ 2,
9321				      /*bit_valid*/ 0,
9322				      /*bit*/ 0);
9323		ctl_done((union ctl_io *)ctsio);
9324		retval = CTL_RETVAL_COMPLETE;
9325		break;
9326	}
9327
9328	return (retval);
9329}
9330
9331static int
9332ctl_inquiry_std(struct ctl_scsiio *ctsio)
9333{
9334	struct scsi_inquiry_data *inq_ptr;
9335	struct scsi_inquiry *cdb;
9336	struct ctl_softc *ctl_softc;
9337	struct ctl_lun *lun;
9338	uint32_t alloc_len;
9339	int is_fc;
9340
9341	ctl_softc = control_softc;
9342
9343	/*
9344	 * Figure out whether we're talking to a Fibre Channel port or not.
9345	 * We treat the ioctl front end, and any SCSI adapters, as packetized
9346	 * SCSI front ends.
9347	 */
9348	mtx_lock(&ctl_softc->ctl_lock);
9349	if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9350	    CTL_PORT_FC)
9351		is_fc = 0;
9352	else
9353		is_fc = 1;
9354	mtx_unlock(&ctl_softc->ctl_lock);
9355
9356	lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9357	cdb = (struct scsi_inquiry *)ctsio->cdb;
9358	alloc_len = scsi_2btoul(cdb->length);
9359
9360	/*
9361	 * We malloc the full inquiry data size here and fill it
9362	 * in.  If the user only asks for less, we'll give him
9363	 * that much.
9364	 */
9365	ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9366	inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9367	ctsio->kern_sg_entries = 0;
9368	ctsio->kern_data_resid = 0;
9369	ctsio->kern_rel_offset = 0;
9370
9371	if (sizeof(*inq_ptr) < alloc_len) {
9372		ctsio->residual = alloc_len - sizeof(*inq_ptr);
9373		ctsio->kern_data_len = sizeof(*inq_ptr);
9374		ctsio->kern_total_len = sizeof(*inq_ptr);
9375	} else {
9376		ctsio->residual = 0;
9377		ctsio->kern_data_len = alloc_len;
9378		ctsio->kern_total_len = alloc_len;
9379	}
9380
9381	/*
9382	 * If we have a LUN configured, report it as connected.  Otherwise,
9383	 * report that it is offline or no device is supported, depending
9384	 * on the value of inquiry_pq_no_lun.
9385	 *
9386	 * According to the spec (SPC-4 r34), the peripheral qualifier
9387	 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9388	 *
9389	 * "A peripheral device having the specified peripheral device type
9390	 * is not connected to this logical unit. However, the device
9391	 * server is capable of supporting the specified peripheral device
9392	 * type on this logical unit."
9393	 *
9394	 * According to the same spec, the peripheral qualifier
9395	 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9396	 *
9397	 * "The device server is not capable of supporting a peripheral
9398	 * device on this logical unit. For this peripheral qualifier the
9399	 * peripheral device type shall be set to 1Fh. All other peripheral
9400	 * device type values are reserved for this peripheral qualifier."
9401	 *
9402	 * Given the text, it would seem that we probably want to report that
9403	 * the LUN is offline here.  There is no LUN connected, but we can
9404	 * support a LUN at the given LUN number.
9405	 *
9406	 * In the real world, though, it sounds like things are a little
9407	 * different:
9408	 *
9409	 * - Linux, when presented with a LUN with the offline peripheral
9410	 *   qualifier, will create an sg driver instance for it.  So when
9411	 *   you attach it to CTL, you wind up with a ton of sg driver
9412	 *   instances.  (One for every LUN that Linux bothered to probe.)
9413	 *   Linux does this despite the fact that it issues a REPORT LUNs
9414	 *   to LUN 0 to get the inventory of supported LUNs.
9415	 *
9416	 * - There is other anecdotal evidence (from Emulex folks) about
9417	 *   arrays that use the offline peripheral qualifier for LUNs that
9418	 *   are on the "passive" path in an active/passive array.
9419	 *
9420	 * So the solution is provide a hopefully reasonable default
9421	 * (return bad/no LUN) and allow the user to change the behavior
9422	 * with a tunable/sysctl variable.
9423	 */
9424	if (lun != NULL)
9425		inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9426				  lun->be_lun->lun_type;
9427	else if (ctl_softc->inquiry_pq_no_lun == 0)
9428		inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9429	else
9430		inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9431
9432	/* RMB in byte 2 is 0 */
9433	inq_ptr->version = SCSI_REV_SPC3;
9434
9435	/*
9436	 * According to SAM-3, even if a device only supports a single
9437	 * level of LUN addressing, it should still set the HISUP bit:
9438	 *
9439	 * 4.9.1 Logical unit numbers overview
9440	 *
9441	 * All logical unit number formats described in this standard are
9442	 * hierarchical in structure even when only a single level in that
9443	 * hierarchy is used. The HISUP bit shall be set to one in the
9444	 * standard INQUIRY data (see SPC-2) when any logical unit number
9445	 * format described in this standard is used.  Non-hierarchical
9446	 * formats are outside the scope of this standard.
9447	 *
9448	 * Therefore we set the HiSup bit here.
9449	 *
9450	 * The reponse format is 2, per SPC-3.
9451	 */
9452	inq_ptr->response_format = SID_HiSup | 2;
9453
9454	inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9455	CTL_DEBUG_PRINT(("additional_length = %d\n",
9456			 inq_ptr->additional_length));
9457
9458	inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9459	/* 16 bit addressing */
9460	if (is_fc == 0)
9461		inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9462	/* XXX set the SID_MultiP bit here if we're actually going to
9463	   respond on multiple ports */
9464	inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9465
9466	/* 16 bit data bus, synchronous transfers */
9467	/* XXX these flags don't apply for FC */
9468	if (is_fc == 0)
9469		inq_ptr->flags = SID_WBus16 | SID_Sync;
9470	/*
9471	 * XXX KDM do we want to support tagged queueing on the control
9472	 * device at all?
9473	 */
9474	if ((lun == NULL)
9475	 || (lun->be_lun->lun_type != T_PROCESSOR))
9476		inq_ptr->flags |= SID_CmdQue;
9477	/*
9478	 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9479	 * We have 8 bytes for the vendor name, and 16 bytes for the device
9480	 * name and 4 bytes for the revision.
9481	 */
9482	strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
9483	if (lun == NULL) {
9484		strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9485	} else {
9486		switch (lun->be_lun->lun_type) {
9487		case T_DIRECT:
9488			strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9489			break;
9490		case T_PROCESSOR:
9491			strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
9492			break;
9493		default:
9494			strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
9495			break;
9496		}
9497	}
9498
9499	/*
9500	 * XXX make this a macro somewhere so it automatically gets
9501	 * incremented when we make changes.
9502	 */
9503	strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
9504
9505	/*
9506	 * For parallel SCSI, we support double transition and single
9507	 * transition clocking.  We also support QAS (Quick Arbitration
9508	 * and Selection) and Information Unit transfers on both the
9509	 * control and array devices.
9510	 */
9511	if (is_fc == 0)
9512		inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
9513				    SID_SPI_IUS;
9514
9515	/* SAM-3 */
9516	scsi_ulto2b(0x0060, inq_ptr->version1);
9517	/* SPC-3 (no version claimed) XXX should we claim a version? */
9518	scsi_ulto2b(0x0300, inq_ptr->version2);
9519	if (is_fc) {
9520		/* FCP-2 ANSI INCITS.350:2003 */
9521		scsi_ulto2b(0x0917, inq_ptr->version3);
9522	} else {
9523		/* SPI-4 ANSI INCITS.362:200x */
9524		scsi_ulto2b(0x0B56, inq_ptr->version3);
9525	}
9526
9527	if (lun == NULL) {
9528		/* SBC-2 (no version claimed) XXX should we claim a version? */
9529		scsi_ulto2b(0x0320, inq_ptr->version4);
9530	} else {
9531		switch (lun->be_lun->lun_type) {
9532		case T_DIRECT:
9533			/*
9534			 * SBC-2 (no version claimed) XXX should we claim a
9535			 * version?
9536			 */
9537			scsi_ulto2b(0x0320, inq_ptr->version4);
9538			break;
9539		case T_PROCESSOR:
9540		default:
9541			break;
9542		}
9543	}
9544
9545	ctsio->scsi_status = SCSI_STATUS_OK;
9546	if (ctsio->kern_data_len > 0) {
9547		ctsio->be_move_done = ctl_config_move_done;
9548		ctl_datamove((union ctl_io *)ctsio);
9549	} else {
9550		ctsio->io_hdr.status = CTL_SUCCESS;
9551		ctl_done((union ctl_io *)ctsio);
9552	}
9553
9554	return (CTL_RETVAL_COMPLETE);
9555}
9556
9557int
9558ctl_inquiry(struct ctl_scsiio *ctsio)
9559{
9560	struct scsi_inquiry *cdb;
9561	int retval;
9562
9563	cdb = (struct scsi_inquiry *)ctsio->cdb;
9564
9565	retval = 0;
9566
9567	CTL_DEBUG_PRINT(("ctl_inquiry\n"));
9568
9569	/*
9570	 * Right now, we don't support the CmdDt inquiry information.
9571	 * This would be nice to support in the future.  When we do
9572	 * support it, we should change this test so that it checks to make
9573	 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
9574	 */
9575#ifdef notyet
9576	if (((cdb->byte2 & SI_EVPD)
9577	 && (cdb->byte2 & SI_CMDDT)))
9578#endif
9579	if (cdb->byte2 & SI_CMDDT) {
9580		/*
9581		 * Point to the SI_CMDDT bit.  We might change this
9582		 * when we support SI_CMDDT, but since both bits would be
9583		 * "wrong", this should probably just stay as-is then.
9584		 */
9585		ctl_set_invalid_field(ctsio,
9586				      /*sks_valid*/ 1,
9587				      /*command*/ 1,
9588				      /*field*/ 1,
9589				      /*bit_valid*/ 1,
9590				      /*bit*/ 1);
9591		ctl_done((union ctl_io *)ctsio);
9592		return (CTL_RETVAL_COMPLETE);
9593	}
9594	if (cdb->byte2 & SI_EVPD)
9595		retval = ctl_inquiry_evpd(ctsio);
9596#ifdef notyet
9597	else if (cdb->byte2 & SI_CMDDT)
9598		retval = ctl_inquiry_cmddt(ctsio);
9599#endif
9600	else
9601		retval = ctl_inquiry_std(ctsio);
9602
9603	return (retval);
9604}
9605
9606/*
9607 * For known CDB types, parse the LBA and length.
9608 */
9609static int
9610ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
9611{
9612	if (io->io_hdr.io_type != CTL_IO_SCSI)
9613		return (1);
9614
9615	switch (io->scsiio.cdb[0]) {
9616	case READ_6:
9617	case WRITE_6: {
9618		struct scsi_rw_6 *cdb;
9619
9620		cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
9621
9622		*lba = scsi_3btoul(cdb->addr);
9623		/* only 5 bits are valid in the most significant address byte */
9624		*lba &= 0x1fffff;
9625		*len = cdb->length;
9626		break;
9627	}
9628	case READ_10:
9629	case WRITE_10: {
9630		struct scsi_rw_10 *cdb;
9631
9632		cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
9633
9634		*lba = scsi_4btoul(cdb->addr);
9635		*len = scsi_2btoul(cdb->length);
9636		break;
9637	}
9638	case WRITE_VERIFY_10: {
9639		struct scsi_write_verify_10 *cdb;
9640
9641		cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
9642
9643		*lba = scsi_4btoul(cdb->addr);
9644		*len = scsi_2btoul(cdb->length);
9645		break;
9646	}
9647	case READ_12:
9648	case WRITE_12: {
9649		struct scsi_rw_12 *cdb;
9650
9651		cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
9652
9653		*lba = scsi_4btoul(cdb->addr);
9654		*len = scsi_4btoul(cdb->length);
9655		break;
9656	}
9657	case WRITE_VERIFY_12: {
9658		struct scsi_write_verify_12 *cdb;
9659
9660		cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
9661
9662		*lba = scsi_4btoul(cdb->addr);
9663		*len = scsi_4btoul(cdb->length);
9664		break;
9665	}
9666	case READ_16:
9667	case WRITE_16: {
9668		struct scsi_rw_16 *cdb;
9669
9670		cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
9671
9672		*lba = scsi_8btou64(cdb->addr);
9673		*len = scsi_4btoul(cdb->length);
9674		break;
9675	}
9676	case WRITE_VERIFY_16: {
9677		struct scsi_write_verify_16 *cdb;
9678
9679		cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
9680
9681
9682		*lba = scsi_8btou64(cdb->addr);
9683		*len = scsi_4btoul(cdb->length);
9684		break;
9685	}
9686	default:
9687		return (1);
9688		break; /* NOTREACHED */
9689	}
9690
9691	return (0);
9692}
9693
9694static ctl_action
9695ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
9696{
9697	uint64_t endlba1, endlba2;
9698
9699	endlba1 = lba1 + len1 - 1;
9700	endlba2 = lba2 + len2 - 1;
9701
9702	if ((endlba1 < lba2)
9703	 || (endlba2 < lba1))
9704		return (CTL_ACTION_PASS);
9705	else
9706		return (CTL_ACTION_BLOCK);
9707}
9708
9709static ctl_action
9710ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
9711{
9712	uint64_t lba1, lba2;
9713	uint32_t len1, len2;
9714	int retval;
9715
9716	retval = ctl_get_lba_len(io1, &lba1, &len1);
9717	if (retval != 0)
9718		return (CTL_ACTION_ERROR);
9719
9720	retval = ctl_get_lba_len(io2, &lba2, &len2);
9721	if (retval != 0)
9722		return (CTL_ACTION_ERROR);
9723
9724	return (ctl_extent_check_lba(lba1, len1, lba2, len2));
9725}
9726
9727static ctl_action
9728ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
9729{
9730	struct ctl_cmd_entry *pending_entry, *ooa_entry;
9731	ctl_serialize_action *serialize_row;
9732
9733	/*
9734	 * The initiator attempted multiple untagged commands at the same
9735	 * time.  Can't do that.
9736	 */
9737	if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9738	 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9739	 && ((pending_io->io_hdr.nexus.targ_port ==
9740	      ooa_io->io_hdr.nexus.targ_port)
9741	  && (pending_io->io_hdr.nexus.initid.id ==
9742	      ooa_io->io_hdr.nexus.initid.id))
9743	 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9744		return (CTL_ACTION_OVERLAP);
9745
9746	/*
9747	 * The initiator attempted to send multiple tagged commands with
9748	 * the same ID.  (It's fine if different initiators have the same
9749	 * tag ID.)
9750	 *
9751	 * Even if all of those conditions are true, we don't kill the I/O
9752	 * if the command ahead of us has been aborted.  We won't end up
9753	 * sending it to the FETD, and it's perfectly legal to resend a
9754	 * command with the same tag number as long as the previous
9755	 * instance of this tag number has been aborted somehow.
9756	 */
9757	if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9758	 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9759	 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
9760	 && ((pending_io->io_hdr.nexus.targ_port ==
9761	      ooa_io->io_hdr.nexus.targ_port)
9762	  && (pending_io->io_hdr.nexus.initid.id ==
9763	      ooa_io->io_hdr.nexus.initid.id))
9764	 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9765		return (CTL_ACTION_OVERLAP_TAG);
9766
9767	/*
9768	 * If we get a head of queue tag, SAM-3 says that we should
9769	 * immediately execute it.
9770	 *
9771	 * What happens if this command would normally block for some other
9772	 * reason?  e.g. a request sense with a head of queue tag
9773	 * immediately after a write.  Normally that would block, but this
9774	 * will result in its getting executed immediately...
9775	 *
9776	 * We currently return "pass" instead of "skip", so we'll end up
9777	 * going through the rest of the queue to check for overlapped tags.
9778	 *
9779	 * XXX KDM check for other types of blockage first??
9780	 */
9781	if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9782		return (CTL_ACTION_PASS);
9783
9784	/*
9785	 * Ordered tags have to block until all items ahead of them
9786	 * have completed.  If we get called with an ordered tag, we always
9787	 * block, if something else is ahead of us in the queue.
9788	 */
9789	if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
9790		return (CTL_ACTION_BLOCK);
9791
9792	/*
9793	 * Simple tags get blocked until all head of queue and ordered tags
9794	 * ahead of them have completed.  I'm lumping untagged commands in
9795	 * with simple tags here.  XXX KDM is that the right thing to do?
9796	 */
9797	if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9798	  || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
9799	 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9800	  || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
9801		return (CTL_ACTION_BLOCK);
9802
9803	pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
9804	ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
9805
9806	serialize_row = ctl_serialize_table[ooa_entry->seridx];
9807
9808	switch (serialize_row[pending_entry->seridx]) {
9809	case CTL_SER_BLOCK:
9810		return (CTL_ACTION_BLOCK);
9811		break; /* NOTREACHED */
9812	case CTL_SER_EXTENT:
9813		return (ctl_extent_check(pending_io, ooa_io));
9814		break; /* NOTREACHED */
9815	case CTL_SER_PASS:
9816		return (CTL_ACTION_PASS);
9817		break; /* NOTREACHED */
9818	case CTL_SER_SKIP:
9819		return (CTL_ACTION_SKIP);
9820		break;
9821	default:
9822		panic("invalid serialization value %d",
9823		      serialize_row[pending_entry->seridx]);
9824		break; /* NOTREACHED */
9825	}
9826
9827	return (CTL_ACTION_ERROR);
9828}
9829
9830/*
9831 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
9832 * Assumptions:
9833 * - pending_io is generally either incoming, or on the blocked queue
9834 * - starting I/O is the I/O we want to start the check with.
9835 */
9836static ctl_action
9837ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
9838	      union ctl_io *starting_io)
9839{
9840	union ctl_io *ooa_io;
9841	ctl_action action;
9842
9843	mtx_assert(&control_softc->ctl_lock, MA_OWNED);
9844
9845	/*
9846	 * Run back along the OOA queue, starting with the current
9847	 * blocked I/O and going through every I/O before it on the
9848	 * queue.  If starting_io is NULL, we'll just end up returning
9849	 * CTL_ACTION_PASS.
9850	 */
9851	for (ooa_io = starting_io; ooa_io != NULL;
9852	     ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
9853	     ooa_links)){
9854
9855		/*
9856		 * This routine just checks to see whether
9857		 * cur_blocked is blocked by ooa_io, which is ahead
9858		 * of it in the queue.  It doesn't queue/dequeue
9859		 * cur_blocked.
9860		 */
9861		action = ctl_check_for_blockage(pending_io, ooa_io);
9862		switch (action) {
9863		case CTL_ACTION_BLOCK:
9864		case CTL_ACTION_OVERLAP:
9865		case CTL_ACTION_OVERLAP_TAG:
9866		case CTL_ACTION_SKIP:
9867		case CTL_ACTION_ERROR:
9868			return (action);
9869			break; /* NOTREACHED */
9870		case CTL_ACTION_PASS:
9871			break;
9872		default:
9873			panic("invalid action %d", action);
9874			break;  /* NOTREACHED */
9875		}
9876	}
9877
9878	return (CTL_ACTION_PASS);
9879}
9880
9881/*
9882 * Assumptions:
9883 * - An I/O has just completed, and has been removed from the per-LUN OOA
9884 *   queue, so some items on the blocked queue may now be unblocked.
9885 */
9886static int
9887ctl_check_blocked(struct ctl_lun *lun)
9888{
9889	union ctl_io *cur_blocked, *next_blocked;
9890
9891	mtx_assert(&control_softc->ctl_lock, MA_OWNED);
9892
9893	/*
9894	 * Run forward from the head of the blocked queue, checking each
9895	 * entry against the I/Os prior to it on the OOA queue to see if
9896	 * there is still any blockage.
9897	 *
9898	 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
9899	 * with our removing a variable on it while it is traversing the
9900	 * list.
9901	 */
9902	for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
9903	     cur_blocked != NULL; cur_blocked = next_blocked) {
9904		union ctl_io *prev_ooa;
9905		ctl_action action;
9906
9907		next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
9908							  blocked_links);
9909
9910		prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
9911						      ctl_ooaq, ooa_links);
9912
9913		/*
9914		 * If cur_blocked happens to be the first item in the OOA
9915		 * queue now, prev_ooa will be NULL, and the action
9916		 * returned will just be CTL_ACTION_PASS.
9917		 */
9918		action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
9919
9920		switch (action) {
9921		case CTL_ACTION_BLOCK:
9922			/* Nothing to do here, still blocked */
9923			break;
9924		case CTL_ACTION_OVERLAP:
9925		case CTL_ACTION_OVERLAP_TAG:
9926			/*
9927			 * This shouldn't happen!  In theory we've already
9928			 * checked this command for overlap...
9929			 */
9930			break;
9931		case CTL_ACTION_PASS:
9932		case CTL_ACTION_SKIP: {
9933			struct ctl_softc *softc;
9934			struct ctl_cmd_entry *entry;
9935			uint32_t initidx;
9936			uint8_t opcode;
9937			int isc_retval;
9938
9939			/*
9940			 * The skip case shouldn't happen, this transaction
9941			 * should have never made it onto the blocked queue.
9942			 */
9943			/*
9944			 * This I/O is no longer blocked, we can remove it
9945			 * from the blocked queue.  Since this is a TAILQ
9946			 * (doubly linked list), we can do O(1) removals
9947			 * from any place on the list.
9948			 */
9949			TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
9950				     blocked_links);
9951			cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
9952
9953			if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
9954				/*
9955				 * Need to send IO back to original side to
9956				 * run
9957				 */
9958				union ctl_ha_msg msg_info;
9959
9960				msg_info.hdr.original_sc =
9961					cur_blocked->io_hdr.original_sc;
9962				msg_info.hdr.serializing_sc = cur_blocked;
9963				msg_info.hdr.msg_type = CTL_MSG_R2R;
9964				if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
9965				     &msg_info, sizeof(msg_info), 0)) >
9966				     CTL_HA_STATUS_SUCCESS) {
9967					printf("CTL:Check Blocked error from "
9968					       "ctl_ha_msg_send %d\n",
9969					       isc_retval);
9970				}
9971				break;
9972			}
9973			opcode = cur_blocked->scsiio.cdb[0];
9974			entry = &ctl_cmd_table[opcode];
9975			softc = control_softc;
9976
9977			initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
9978
9979			/*
9980			 * Check this I/O for LUN state changes that may
9981			 * have happened while this command was blocked.
9982			 * The LUN state may have been changed by a command
9983			 * ahead of us in the queue, so we need to re-check
9984			 * for any states that can be caused by SCSI
9985			 * commands.
9986			 */
9987			if (ctl_scsiio_lun_check(softc, lun, entry,
9988						 &cur_blocked->scsiio) == 0) {
9989				cur_blocked->io_hdr.flags |=
9990				                      CTL_FLAG_IS_WAS_ON_RTR;
9991				STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
9992						   &cur_blocked->io_hdr, links);
9993				/*
9994				 * In the non CTL_DONE_THREAD case, we need
9995				 * to wake up the work thread here.  When
9996				 * we're processing completed requests from
9997				 * the work thread context, we'll pop back
9998				 * around and end up pulling things off the
9999				 * RtR queue.  When we aren't processing
10000				 * things from the work thread context,
10001				 * though, we won't ever check the RtR queue.
10002				 * So we need to wake up the thread to clear
10003				 * things off the queue.  Otherwise this
10004				 * transaction will just sit on the RtR queue
10005				 * until a new I/O comes in.  (Which may or
10006				 * may not happen...)
10007				 */
10008#ifndef CTL_DONE_THREAD
10009				ctl_wakeup_thread();
10010#endif
10011			} else
10012				ctl_done_lock(cur_blocked, /*have_lock*/ 1);
10013			break;
10014		}
10015		default:
10016			/*
10017			 * This probably shouldn't happen -- we shouldn't
10018			 * get CTL_ACTION_ERROR, or anything else.
10019			 */
10020			break;
10021		}
10022	}
10023
10024	return (CTL_RETVAL_COMPLETE);
10025}
10026
10027/*
10028 * This routine (with one exception) checks LUN flags that can be set by
10029 * commands ahead of us in the OOA queue.  These flags have to be checked
10030 * when a command initially comes in, and when we pull a command off the
10031 * blocked queue and are preparing to execute it.  The reason we have to
10032 * check these flags for commands on the blocked queue is that the LUN
10033 * state may have been changed by a command ahead of us while we're on the
10034 * blocked queue.
10035 *
10036 * Ordering is somewhat important with these checks, so please pay
10037 * careful attention to the placement of any new checks.
10038 */
10039static int
10040ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10041		     struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10042{
10043	int retval;
10044
10045	retval = 0;
10046
10047	/*
10048	 * If this shelf is a secondary shelf controller, we have to reject
10049	 * any media access commands.
10050	 */
10051#if 0
10052	/* No longer needed for HA */
10053	if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10054	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10055		ctl_set_lun_standby(ctsio);
10056		retval = 1;
10057		goto bailout;
10058	}
10059#endif
10060
10061	/*
10062	 * Check for a reservation conflict.  If this command isn't allowed
10063	 * even on reserved LUNs, and if this initiator isn't the one who
10064	 * reserved us, reject the command with a reservation conflict.
10065	 */
10066	if ((lun->flags & CTL_LUN_RESERVED)
10067	 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10068		if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10069		 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10070		 || (ctsio->io_hdr.nexus.targ_target.id !=
10071		     lun->rsv_nexus.targ_target.id)) {
10072			ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10073			ctsio->io_hdr.status = CTL_SCSI_ERROR;
10074			retval = 1;
10075			goto bailout;
10076		}
10077	}
10078
10079	if ( (lun->flags & CTL_LUN_PR_RESERVED)
10080	 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10081		uint32_t residx;
10082
10083		residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10084		/*
10085		 * if we aren't registered or it's a res holder type
10086		 * reservation and this isn't the res holder then set a
10087		 * conflict.
10088		 * NOTE: Commands which might be allowed on write exclusive
10089		 * type reservations are checked in the particular command
10090		 * for a conflict. Read and SSU are the only ones.
10091		 */
10092		if (!lun->per_res[residx].registered
10093		 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10094			ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10095			ctsio->io_hdr.status = CTL_SCSI_ERROR;
10096			retval = 1;
10097			goto bailout;
10098		}
10099
10100	}
10101
10102	if ((lun->flags & CTL_LUN_OFFLINE)
10103	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10104		ctl_set_lun_not_ready(ctsio);
10105		retval = 1;
10106		goto bailout;
10107	}
10108
10109	/*
10110	 * If the LUN is stopped, see if this particular command is allowed
10111	 * for a stopped lun.  Otherwise, reject it with 0x04,0x02.
10112	 */
10113	if ((lun->flags & CTL_LUN_STOPPED)
10114	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10115		/* "Logical unit not ready, initializing cmd. required" */
10116		ctl_set_lun_stopped(ctsio);
10117		retval = 1;
10118		goto bailout;
10119	}
10120
10121	if ((lun->flags & CTL_LUN_INOPERABLE)
10122	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10123		/* "Medium format corrupted" */
10124		ctl_set_medium_format_corrupted(ctsio);
10125		retval = 1;
10126		goto bailout;
10127	}
10128
10129bailout:
10130	return (retval);
10131
10132}
10133
10134static void
10135ctl_failover_io(union ctl_io *io, int have_lock)
10136{
10137	ctl_set_busy(&io->scsiio);
10138	ctl_done_lock(io, have_lock);
10139}
10140
10141static void
10142ctl_failover(void)
10143{
10144	struct ctl_lun *lun;
10145	struct ctl_softc *ctl_softc;
10146	union ctl_io *next_io, *pending_io;
10147	union ctl_io *io;
10148	int lun_idx;
10149	int i;
10150
10151	ctl_softc = control_softc;
10152
10153	mtx_lock(&ctl_softc->ctl_lock);
10154	/*
10155	 * Remove any cmds from the other SC from the rtr queue.  These
10156	 * will obviously only be for LUNs for which we're the primary.
10157	 * We can't send status or get/send data for these commands.
10158	 * Since they haven't been executed yet, we can just remove them.
10159	 * We'll either abort them or delete them below, depending on
10160	 * which HA mode we're in.
10161	 */
10162	for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10163	     io != NULL; io = next_io) {
10164		next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10165		if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10166			STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10167				      ctl_io_hdr, links);
10168	}
10169
10170	for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10171		lun = ctl_softc->ctl_luns[lun_idx];
10172		if (lun==NULL)
10173			continue;
10174
10175		/*
10176		 * Processor LUNs are primary on both sides.
10177		 * XXX will this always be true?
10178		 */
10179		if (lun->be_lun->lun_type == T_PROCESSOR)
10180			continue;
10181
10182		if ((lun->flags & CTL_LUN_PRIMARY_SC)
10183		 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10184			printf("FAILOVER: primary lun %d\n", lun_idx);
10185		        /*
10186			 * Remove all commands from the other SC. First from the
10187			 * blocked queue then from the ooa queue. Once we have
10188			 * removed them. Call ctl_check_blocked to see if there
10189			 * is anything that can run.
10190			 */
10191			for (io = (union ctl_io *)TAILQ_FIRST(
10192			     &lun->blocked_queue); io != NULL; io = next_io) {
10193
10194		        	next_io = (union ctl_io *)TAILQ_NEXT(
10195				    &io->io_hdr, blocked_links);
10196
10197				if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10198					TAILQ_REMOVE(&lun->blocked_queue,
10199						     &io->io_hdr,blocked_links);
10200					io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10201					TAILQ_REMOVE(&lun->ooa_queue,
10202						     &io->io_hdr, ooa_links);
10203
10204					ctl_free_io(io);
10205				}
10206			}
10207
10208			for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10209	     		     io != NULL; io = next_io) {
10210
10211		        	next_io = (union ctl_io *)TAILQ_NEXT(
10212				    &io->io_hdr, ooa_links);
10213
10214				if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10215
10216					TAILQ_REMOVE(&lun->ooa_queue,
10217						&io->io_hdr,
10218					     	ooa_links);
10219
10220					ctl_free_io(io);
10221				}
10222			}
10223			ctl_check_blocked(lun);
10224		} else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10225			&& (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10226
10227			printf("FAILOVER: primary lun %d\n", lun_idx);
10228			/*
10229			 * Abort all commands from the other SC.  We can't
10230			 * send status back for them now.  These should get
10231			 * cleaned up when they are completed or come out
10232			 * for a datamove operation.
10233			 */
10234			for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10235	     		     io != NULL; io = next_io) {
10236		        	next_io = (union ctl_io *)TAILQ_NEXT(
10237					&io->io_hdr, ooa_links);
10238
10239				if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10240					io->io_hdr.flags |= CTL_FLAG_ABORT;
10241			}
10242		} else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10243			&& (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10244
10245			printf("FAILOVER: secondary lun %d\n", lun_idx);
10246
10247			lun->flags |= CTL_LUN_PRIMARY_SC;
10248
10249			/*
10250			 * We send all I/O that was sent to this controller
10251			 * and redirected to the other side back with
10252			 * busy status, and have the initiator retry it.
10253			 * Figuring out how much data has been transferred,
10254			 * etc. and picking up where we left off would be
10255			 * very tricky.
10256			 *
10257			 * XXX KDM need to remove I/O from the blocked
10258			 * queue as well!
10259			 */
10260			for (pending_io = (union ctl_io *)TAILQ_FIRST(
10261			     &lun->ooa_queue); pending_io != NULL;
10262			     pending_io = next_io) {
10263
10264				next_io =  (union ctl_io *)TAILQ_NEXT(
10265					&pending_io->io_hdr, ooa_links);
10266
10267				pending_io->io_hdr.flags &=
10268					~CTL_FLAG_SENT_2OTHER_SC;
10269
10270				if (pending_io->io_hdr.flags &
10271				    CTL_FLAG_IO_ACTIVE) {
10272					pending_io->io_hdr.flags |=
10273						CTL_FLAG_FAILOVER;
10274				} else {
10275					ctl_set_busy(&pending_io->scsiio);
10276					ctl_done_lock(pending_io,
10277						      /*have_lock*/1);
10278				}
10279			}
10280
10281			/*
10282			 * Build Unit Attention
10283			 */
10284			for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10285				lun->pending_sense[i].ua_pending |=
10286				                     CTL_UA_ASYM_ACC_CHANGE;
10287			}
10288		} else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10289			&& (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10290			printf("FAILOVER: secondary lun %d\n", lun_idx);
10291			/*
10292			 * if the first io on the OOA is not on the RtR queue
10293			 * add it.
10294			 */
10295			lun->flags |= CTL_LUN_PRIMARY_SC;
10296
10297			pending_io = (union ctl_io *)TAILQ_FIRST(
10298			    &lun->ooa_queue);
10299			if (pending_io==NULL) {
10300				printf("Nothing on OOA queue\n");
10301				continue;
10302			}
10303
10304			pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10305			if ((pending_io->io_hdr.flags &
10306			     CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10307				pending_io->io_hdr.flags |=
10308				    CTL_FLAG_IS_WAS_ON_RTR;
10309				STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
10310						   &pending_io->io_hdr, links);
10311			}
10312#if 0
10313			else
10314			{
10315				printf("Tag 0x%04x is running\n",
10316				      pending_io->scsiio.tag_num);
10317			}
10318#endif
10319
10320			next_io = (union ctl_io *)TAILQ_NEXT(
10321			    &pending_io->io_hdr, ooa_links);
10322			for (pending_io=next_io; pending_io != NULL;
10323			     pending_io = next_io) {
10324				pending_io->io_hdr.flags &=
10325				    ~CTL_FLAG_SENT_2OTHER_SC;
10326				next_io = (union ctl_io *)TAILQ_NEXT(
10327					&pending_io->io_hdr, ooa_links);
10328				if (pending_io->io_hdr.flags &
10329				    CTL_FLAG_IS_WAS_ON_RTR) {
10330#if 0
10331				        printf("Tag 0x%04x is running\n",
10332				      		pending_io->scsiio.tag_num);
10333#endif
10334					continue;
10335				}
10336
10337				switch (ctl_check_ooa(lun, pending_io,
10338			            (union ctl_io *)TAILQ_PREV(
10339				    &pending_io->io_hdr, ctl_ooaq,
10340				    ooa_links))) {
10341
10342				case CTL_ACTION_BLOCK:
10343					TAILQ_INSERT_TAIL(&lun->blocked_queue,
10344							  &pending_io->io_hdr,
10345							  blocked_links);
10346					pending_io->io_hdr.flags |=
10347					    CTL_FLAG_BLOCKED;
10348					break;
10349				case CTL_ACTION_PASS:
10350				case CTL_ACTION_SKIP:
10351					pending_io->io_hdr.flags |=
10352					    CTL_FLAG_IS_WAS_ON_RTR;
10353					STAILQ_INSERT_TAIL(
10354					    &ctl_softc->rtr_queue,
10355					    &pending_io->io_hdr, links);
10356					break;
10357				case CTL_ACTION_OVERLAP:
10358					ctl_set_overlapped_cmd(
10359					    (struct ctl_scsiio *)pending_io);
10360					ctl_done_lock(pending_io,
10361						      /*have_lock*/ 1);
10362					break;
10363				case CTL_ACTION_OVERLAP_TAG:
10364					ctl_set_overlapped_tag(
10365					    (struct ctl_scsiio *)pending_io,
10366					    pending_io->scsiio.tag_num & 0xff);
10367					ctl_done_lock(pending_io,
10368						      /*have_lock*/ 1);
10369					break;
10370				case CTL_ACTION_ERROR:
10371				default:
10372					ctl_set_internal_failure(
10373						(struct ctl_scsiio *)pending_io,
10374						0,  // sks_valid
10375						0); //retry count
10376					ctl_done_lock(pending_io,
10377						      /*have_lock*/ 1);
10378					break;
10379				}
10380			}
10381
10382			/*
10383			 * Build Unit Attention
10384			 */
10385			for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10386				lun->pending_sense[i].ua_pending |=
10387				                     CTL_UA_ASYM_ACC_CHANGE;
10388			}
10389		} else {
10390			panic("Unhandled HA mode failover, LUN flags = %#x, "
10391			      "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10392		}
10393	}
10394	ctl_pause_rtr = 0;
10395	mtx_unlock(&ctl_softc->ctl_lock);
10396}
10397
10398static int
10399ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10400{
10401	struct ctl_lun *lun;
10402	struct ctl_cmd_entry *entry;
10403	uint8_t opcode;
10404	uint32_t initidx, targ_lun;
10405	int retval;
10406
10407	retval = 0;
10408
10409	lun = NULL;
10410
10411	opcode = ctsio->cdb[0];
10412
10413	mtx_lock(&ctl_softc->ctl_lock);
10414
10415	targ_lun = ctsio->io_hdr.nexus.targ_lun;
10416	if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
10417		targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
10418	if ((targ_lun < CTL_MAX_LUNS)
10419	 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
10420		lun = ctl_softc->ctl_luns[targ_lun];
10421		/*
10422		 * If the LUN is invalid, pretend that it doesn't exist.
10423		 * It will go away as soon as all pending I/O has been
10424		 * completed.
10425		 */
10426		if (lun->flags & CTL_LUN_DISABLED) {
10427			lun = NULL;
10428		} else {
10429			ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10430			ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10431				lun->be_lun;
10432			if (lun->be_lun->lun_type == T_PROCESSOR) {
10433				ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10434			}
10435		}
10436	} else {
10437		ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10438		ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
10439	}
10440
10441	entry = &ctl_cmd_table[opcode];
10442
10443	ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
10444	ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
10445
10446	/*
10447	 * Check to see whether we can send this command to LUNs that don't
10448	 * exist.  This should pretty much only be the case for inquiry
10449	 * and request sense.  Further checks, below, really require having
10450	 * a LUN, so we can't really check the command anymore.  Just put
10451	 * it on the rtr queue.
10452	 */
10453	if (lun == NULL) {
10454		if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10455			goto queue_rtr;
10456
10457		ctl_set_unsupported_lun(ctsio);
10458		mtx_unlock(&ctl_softc->ctl_lock);
10459		ctl_done((union ctl_io *)ctsio);
10460		CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
10461		goto bailout;
10462	} else {
10463		/*
10464		 * Every I/O goes into the OOA queue for a particular LUN, and
10465		 * stays there until completion.
10466		 */
10467		TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
10468
10469		/*
10470		 * Make sure we support this particular command on this LUN.
10471		 * e.g., we don't support writes to the control LUN.
10472		 */
10473		switch (lun->be_lun->lun_type) {
10474		case T_PROCESSOR:
10475		 	if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
10476			 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10477			      == 0)) {
10478				ctl_set_invalid_opcode(ctsio);
10479				mtx_unlock(&ctl_softc->ctl_lock);
10480				ctl_done((union ctl_io *)ctsio);
10481				goto bailout;
10482			}
10483			break;
10484		case T_DIRECT:
10485			if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
10486			 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10487			      == 0)){
10488				ctl_set_invalid_opcode(ctsio);
10489				mtx_unlock(&ctl_softc->ctl_lock);
10490				ctl_done((union ctl_io *)ctsio);
10491				goto bailout;
10492			}
10493			break;
10494		default:
10495			printf("Unsupported CTL LUN type %d\n",
10496			       lun->be_lun->lun_type);
10497			panic("Unsupported CTL LUN type %d\n",
10498			      lun->be_lun->lun_type);
10499			break; /* NOTREACHED */
10500		}
10501	}
10502
10503	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10504
10505	/*
10506	 * If we've got a request sense, it'll clear the contingent
10507	 * allegiance condition.  Otherwise, if we have a CA condition for
10508	 * this initiator, clear it, because it sent down a command other
10509	 * than request sense.
10510	 */
10511	if ((opcode != REQUEST_SENSE)
10512	 && (ctl_is_set(lun->have_ca, initidx)))
10513		ctl_clear_mask(lun->have_ca, initidx);
10514
10515	/*
10516	 * If the command has this flag set, it handles its own unit
10517	 * attention reporting, we shouldn't do anything.  Otherwise we
10518	 * check for any pending unit attentions, and send them back to the
10519	 * initiator.  We only do this when a command initially comes in,
10520	 * not when we pull it off the blocked queue.
10521	 *
10522	 * According to SAM-3, section 5.3.2, the order that things get
10523	 * presented back to the host is basically unit attentions caused
10524	 * by some sort of reset event, busy status, reservation conflicts
10525	 * or task set full, and finally any other status.
10526	 *
10527	 * One issue here is that some of the unit attentions we report
10528	 * don't fall into the "reset" category (e.g. "reported luns data
10529	 * has changed").  So reporting it here, before the reservation
10530	 * check, may be technically wrong.  I guess the only thing to do
10531	 * would be to check for and report the reset events here, and then
10532	 * check for the other unit attention types after we check for a
10533	 * reservation conflict.
10534	 *
10535	 * XXX KDM need to fix this
10536	 */
10537	if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
10538		ctl_ua_type ua_type;
10539
10540		ua_type = lun->pending_sense[initidx].ua_pending;
10541		if (ua_type != CTL_UA_NONE) {
10542			scsi_sense_data_type sense_format;
10543
10544			if (lun != NULL)
10545				sense_format = (lun->flags &
10546				    CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
10547				    SSD_TYPE_FIXED;
10548			else
10549				sense_format = SSD_TYPE_FIXED;
10550
10551			ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
10552					       sense_format);
10553			if (ua_type != CTL_UA_NONE) {
10554				ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
10555				ctsio->io_hdr.status = CTL_SCSI_ERROR |
10556						       CTL_AUTOSENSE;
10557				ctsio->sense_len = SSD_FULL_SIZE;
10558				lun->pending_sense[initidx].ua_pending &=
10559					~ua_type;
10560				mtx_unlock(&ctl_softc->ctl_lock);
10561				ctl_done((union ctl_io *)ctsio);
10562				goto bailout;
10563			}
10564		}
10565	}
10566
10567
10568	if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
10569		mtx_unlock(&ctl_softc->ctl_lock);
10570		ctl_done((union ctl_io *)ctsio);
10571		goto bailout;
10572	}
10573
10574	/*
10575	 * XXX CHD this is where we want to send IO to other side if
10576	 * this LUN is secondary on this SC. We will need to make a copy
10577	 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
10578	 * the copy we send as FROM_OTHER.
10579	 * We also need to stuff the address of the original IO so we can
10580	 * find it easily. Something similar will need be done on the other
10581	 * side so when we are done we can find the copy.
10582	 */
10583	if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10584		union ctl_ha_msg msg_info;
10585		int isc_retval;
10586
10587		ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10588
10589		msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
10590		msg_info.hdr.original_sc = (union ctl_io *)ctsio;
10591#if 0
10592		printf("1. ctsio %p\n", ctsio);
10593#endif
10594		msg_info.hdr.serializing_sc = NULL;
10595		msg_info.hdr.nexus = ctsio->io_hdr.nexus;
10596		msg_info.scsi.tag_num = ctsio->tag_num;
10597		msg_info.scsi.tag_type = ctsio->tag_type;
10598		memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
10599
10600		ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10601
10602		if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10603		    (void *)&msg_info, sizeof(msg_info), 0)) >
10604		    CTL_HA_STATUS_SUCCESS) {
10605			printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
10606			       isc_retval);
10607			printf("CTL:opcode is %x\n",opcode);
10608		} else {
10609#if 0
10610			printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
10611#endif
10612		}
10613
10614		/*
10615		 * XXX KDM this I/O is off the incoming queue, but hasn't
10616		 * been inserted on any other queue.  We may need to come
10617		 * up with a holding queue while we wait for serialization
10618		 * so that we have an idea of what we're waiting for from
10619		 * the other side.
10620		 */
10621		goto bailout_unlock;
10622	}
10623
10624	switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
10625			      (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
10626			      ctl_ooaq, ooa_links))) {
10627	case CTL_ACTION_BLOCK:
10628		ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
10629		TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
10630				  blocked_links);
10631		goto bailout_unlock;
10632		break; /* NOTREACHED */
10633	case CTL_ACTION_PASS:
10634	case CTL_ACTION_SKIP:
10635		goto queue_rtr;
10636		break; /* NOTREACHED */
10637	case CTL_ACTION_OVERLAP:
10638		ctl_set_overlapped_cmd(ctsio);
10639		mtx_unlock(&ctl_softc->ctl_lock);
10640		ctl_done((union ctl_io *)ctsio);
10641		goto bailout;
10642		break; /* NOTREACHED */
10643	case CTL_ACTION_OVERLAP_TAG:
10644		ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
10645		mtx_unlock(&ctl_softc->ctl_lock);
10646		ctl_done((union ctl_io *)ctsio);
10647		goto bailout;
10648		break; /* NOTREACHED */
10649	case CTL_ACTION_ERROR:
10650	default:
10651		ctl_set_internal_failure(ctsio,
10652					 /*sks_valid*/ 0,
10653					 /*retry_count*/ 0);
10654		mtx_unlock(&ctl_softc->ctl_lock);
10655		ctl_done((union ctl_io *)ctsio);
10656		goto bailout;
10657		break; /* NOTREACHED */
10658	}
10659
10660	goto bailout_unlock;
10661
10662queue_rtr:
10663	ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
10664	STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
10665
10666bailout_unlock:
10667	mtx_unlock(&ctl_softc->ctl_lock);
10668
10669bailout:
10670	return (retval);
10671}
10672
10673static int
10674ctl_scsiio(struct ctl_scsiio *ctsio)
10675{
10676	int retval;
10677	struct ctl_cmd_entry *entry;
10678
10679	retval = CTL_RETVAL_COMPLETE;
10680
10681	CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
10682
10683	entry = &ctl_cmd_table[ctsio->cdb[0]];
10684
10685	/*
10686	 * If this I/O has been aborted, just send it straight to
10687	 * ctl_done() without executing it.
10688	 */
10689	if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
10690		ctl_done((union ctl_io *)ctsio);
10691		goto bailout;
10692	}
10693
10694	/*
10695	 * All the checks should have been handled by ctl_scsiio_precheck().
10696	 * We should be clear now to just execute the I/O.
10697	 */
10698	retval = entry->execute(ctsio);
10699
10700bailout:
10701	return (retval);
10702}
10703
10704/*
10705 * Since we only implement one target right now, a bus reset simply resets
10706 * our single target.
10707 */
10708static int
10709ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
10710{
10711	return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
10712}
10713
10714static int
10715ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
10716		 ctl_ua_type ua_type)
10717{
10718	struct ctl_lun *lun;
10719	int retval;
10720
10721	if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
10722		union ctl_ha_msg msg_info;
10723
10724		io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10725		msg_info.hdr.nexus = io->io_hdr.nexus;
10726		if (ua_type==CTL_UA_TARG_RESET)
10727			msg_info.task.task_action = CTL_TASK_TARGET_RESET;
10728		else
10729			msg_info.task.task_action = CTL_TASK_BUS_RESET;
10730		msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
10731		msg_info.hdr.original_sc = NULL;
10732		msg_info.hdr.serializing_sc = NULL;
10733		if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10734		    (void *)&msg_info, sizeof(msg_info), 0)) {
10735		}
10736	}
10737	retval = 0;
10738
10739	STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
10740		retval += ctl_lun_reset(lun, io, ua_type);
10741
10742	return (retval);
10743}
10744
10745/*
10746 * The LUN should always be set.  The I/O is optional, and is used to
10747 * distinguish between I/Os sent by this initiator, and by other
10748 * initiators.  We set unit attention for initiators other than this one.
10749 * SAM-3 is vague on this point.  It does say that a unit attention should
10750 * be established for other initiators when a LUN is reset (see section
10751 * 5.7.3), but it doesn't specifically say that the unit attention should
10752 * be established for this particular initiator when a LUN is reset.  Here
10753 * is the relevant text, from SAM-3 rev 8:
10754 *
10755 * 5.7.2 When a SCSI initiator port aborts its own tasks
10756 *
10757 * When a SCSI initiator port causes its own task(s) to be aborted, no
10758 * notification that the task(s) have been aborted shall be returned to
10759 * the SCSI initiator port other than the completion response for the
10760 * command or task management function action that caused the task(s) to
10761 * be aborted and notification(s) associated with related effects of the
10762 * action (e.g., a reset unit attention condition).
10763 *
10764 * XXX KDM for now, we're setting unit attention for all initiators.
10765 */
10766static int
10767ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
10768{
10769	union ctl_io *xio;
10770#if 0
10771	uint32_t initindex;
10772#endif
10773	int i;
10774
10775	/*
10776	 * Run through the OOA queue and abort each I/O.
10777	 */
10778#if 0
10779	TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10780#endif
10781	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10782	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10783		xio->io_hdr.flags |= CTL_FLAG_ABORT;
10784	}
10785
10786	/*
10787	 * This version sets unit attention for every
10788	 */
10789#if 0
10790	initindex = ctl_get_initindex(&io->io_hdr.nexus);
10791	for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10792		if (initindex == i)
10793			continue;
10794		lun->pending_sense[i].ua_pending |= ua_type;
10795	}
10796#endif
10797
10798	/*
10799	 * A reset (any kind, really) clears reservations established with
10800	 * RESERVE/RELEASE.  It does not clear reservations established
10801	 * with PERSISTENT RESERVE OUT, but we don't support that at the
10802	 * moment anyway.  See SPC-2, section 5.6.  SPC-3 doesn't address
10803	 * reservations made with the RESERVE/RELEASE commands, because
10804	 * those commands are obsolete in SPC-3.
10805	 */
10806	lun->flags &= ~CTL_LUN_RESERVED;
10807
10808	for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10809		ctl_clear_mask(lun->have_ca, i);
10810		lun->pending_sense[i].ua_pending |= ua_type;
10811	}
10812
10813	return (0);
10814}
10815
10816static int
10817ctl_abort_task(union ctl_io *io)
10818{
10819	union ctl_io *xio;
10820	struct ctl_lun *lun;
10821	struct ctl_softc *ctl_softc;
10822#if 0
10823	struct sbuf sb;
10824	char printbuf[128];
10825#endif
10826	int found;
10827	uint32_t targ_lun;
10828
10829	ctl_softc = control_softc;
10830	found = 0;
10831
10832	/*
10833	 * Look up the LUN.
10834	 */
10835	targ_lun = io->io_hdr.nexus.targ_lun;
10836	if (io->io_hdr.nexus.lun_map_fn != NULL)
10837		targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
10838	if ((targ_lun < CTL_MAX_LUNS)
10839	 && (ctl_softc->ctl_luns[targ_lun] != NULL))
10840		lun = ctl_softc->ctl_luns[targ_lun];
10841	else
10842		goto bailout;
10843
10844#if 0
10845	printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
10846	       lun->lun, io->taskio.tag_num, io->taskio.tag_type);
10847#endif
10848
10849	/*
10850	 * Run through the OOA queue and attempt to find the given I/O.
10851	 * The target port, initiator ID, tag type and tag number have to
10852	 * match the values that we got from the initiator.  If we have an
10853	 * untagged command to abort, simply abort the first untagged command
10854	 * we come to.  We only allow one untagged command at a time of course.
10855	 */
10856#if 0
10857	TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10858#endif
10859	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10860	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10861#if 0
10862		sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
10863
10864		sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
10865			    lun->lun, xio->scsiio.tag_num,
10866			    xio->scsiio.tag_type,
10867			    (xio->io_hdr.blocked_links.tqe_prev
10868			    == NULL) ? "" : " BLOCKED",
10869			    (xio->io_hdr.flags &
10870			    CTL_FLAG_DMA_INPROG) ? " DMA" : "",
10871			    (xio->io_hdr.flags &
10872			    CTL_FLAG_ABORT) ? " ABORT" : "",
10873			    (xio->io_hdr.flags &
10874			    CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
10875		ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
10876		sbuf_finish(&sb);
10877		printf("%s\n", sbuf_data(&sb));
10878#endif
10879
10880		if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
10881		 && (xio->io_hdr.nexus.initid.id ==
10882		     io->io_hdr.nexus.initid.id)) {
10883			/*
10884			 * If the abort says that the task is untagged, the
10885			 * task in the queue must be untagged.  Otherwise,
10886			 * we just check to see whether the tag numbers
10887			 * match.  This is because the QLogic firmware
10888			 * doesn't pass back the tag type in an abort
10889			 * request.
10890			 */
10891#if 0
10892			if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
10893			  && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
10894			 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
10895#endif
10896			/*
10897			 * XXX KDM we've got problems with FC, because it
10898			 * doesn't send down a tag type with aborts.  So we
10899			 * can only really go by the tag number...
10900			 * This may cause problems with parallel SCSI.
10901			 * Need to figure that out!!
10902			 */
10903			if (xio->scsiio.tag_num == io->taskio.tag_num) {
10904				xio->io_hdr.flags |= CTL_FLAG_ABORT;
10905				found = 1;
10906				if ((io->io_hdr.flags &
10907				     CTL_FLAG_FROM_OTHER_SC) == 0 &&
10908				    !(lun->flags & CTL_LUN_PRIMARY_SC)) {
10909					union ctl_ha_msg msg_info;
10910
10911					io->io_hdr.flags |=
10912					                CTL_FLAG_SENT_2OTHER_SC;
10913					msg_info.hdr.nexus = io->io_hdr.nexus;
10914					msg_info.task.task_action =
10915						CTL_TASK_ABORT_TASK;
10916					msg_info.task.tag_num =
10917						io->taskio.tag_num;
10918					msg_info.task.tag_type =
10919						io->taskio.tag_type;
10920					msg_info.hdr.msg_type =
10921						CTL_MSG_MANAGE_TASKS;
10922					msg_info.hdr.original_sc = NULL;
10923					msg_info.hdr.serializing_sc = NULL;
10924#if 0
10925					printf("Sent Abort to other side\n");
10926#endif
10927					if (CTL_HA_STATUS_SUCCESS !=
10928					        ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10929		    				(void *)&msg_info,
10930						sizeof(msg_info), 0)) {
10931					}
10932				}
10933#if 0
10934				printf("ctl_abort_task: found I/O to abort\n");
10935#endif
10936				break;
10937			}
10938		}
10939	}
10940
10941bailout:
10942
10943	if (found == 0) {
10944		/*
10945		 * This isn't really an error.  It's entirely possible for
10946		 * the abort and command completion to cross on the wire.
10947		 * This is more of an informative/diagnostic error.
10948		 */
10949#if 0
10950		printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
10951		       "%d:%d:%d:%d tag %d type %d\n",
10952		       io->io_hdr.nexus.initid.id,
10953		       io->io_hdr.nexus.targ_port,
10954		       io->io_hdr.nexus.targ_target.id,
10955		       io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
10956		       io->taskio.tag_type);
10957#endif
10958		return (1);
10959	} else
10960		return (0);
10961}
10962
10963/*
10964 * This routine cannot block!  It must be callable from an interrupt
10965 * handler as well as from the work thread.
10966 */
10967static void
10968ctl_run_task_queue(struct ctl_softc *ctl_softc)
10969{
10970	union ctl_io *io, *next_io;
10971
10972	mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
10973
10974	CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
10975
10976	for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
10977	     io != NULL; io = next_io) {
10978		int retval;
10979		const char *task_desc;
10980
10981		next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10982
10983		retval = 0;
10984
10985		switch (io->io_hdr.io_type) {
10986		case CTL_IO_TASK: {
10987			task_desc = ctl_scsi_task_string(&io->taskio);
10988			if (task_desc != NULL) {
10989#ifdef NEEDTOPORT
10990				csevent_log(CSC_CTL | CSC_SHELF_SW |
10991					    CTL_TASK_REPORT,
10992					    csevent_LogType_Trace,
10993					    csevent_Severity_Information,
10994					    csevent_AlertLevel_Green,
10995					    csevent_FRU_Firmware,
10996					    csevent_FRU_Unknown,
10997					    "CTL: received task: %s",task_desc);
10998#endif
10999			} else {
11000#ifdef NEEDTOPORT
11001				csevent_log(CSC_CTL | CSC_SHELF_SW |
11002					    CTL_TASK_REPORT,
11003					    csevent_LogType_Trace,
11004					    csevent_Severity_Information,
11005					    csevent_AlertLevel_Green,
11006					    csevent_FRU_Firmware,
11007					    csevent_FRU_Unknown,
11008					    "CTL: received unknown task "
11009					    "type: %d (%#x)",
11010					    io->taskio.task_action,
11011					    io->taskio.task_action);
11012#endif
11013			}
11014			switch (io->taskio.task_action) {
11015			case CTL_TASK_ABORT_TASK:
11016				retval = ctl_abort_task(io);
11017				break;
11018			case CTL_TASK_ABORT_TASK_SET:
11019				break;
11020			case CTL_TASK_CLEAR_ACA:
11021				break;
11022			case CTL_TASK_CLEAR_TASK_SET:
11023				break;
11024			case CTL_TASK_LUN_RESET: {
11025				struct ctl_lun *lun;
11026				uint32_t targ_lun;
11027				int retval;
11028
11029				targ_lun = io->io_hdr.nexus.targ_lun;
11030				if (io->io_hdr.nexus.lun_map_fn != NULL)
11031					targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11032
11033				if ((targ_lun < CTL_MAX_LUNS)
11034				 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11035					lun = ctl_softc->ctl_luns[targ_lun];
11036				else {
11037					retval = 1;
11038					break;
11039				}
11040
11041				if (!(io->io_hdr.flags &
11042				    CTL_FLAG_FROM_OTHER_SC)) {
11043					union ctl_ha_msg msg_info;
11044
11045					io->io_hdr.flags |=
11046						CTL_FLAG_SENT_2OTHER_SC;
11047					msg_info.hdr.msg_type =
11048						CTL_MSG_MANAGE_TASKS;
11049					msg_info.hdr.nexus = io->io_hdr.nexus;
11050					msg_info.task.task_action =
11051						CTL_TASK_LUN_RESET;
11052					msg_info.hdr.original_sc = NULL;
11053					msg_info.hdr.serializing_sc = NULL;
11054					if (CTL_HA_STATUS_SUCCESS !=
11055					    ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11056					    (void *)&msg_info,
11057					    sizeof(msg_info), 0)) {
11058					}
11059				}
11060
11061				retval = ctl_lun_reset(lun, io,
11062						       CTL_UA_LUN_RESET);
11063				break;
11064			}
11065			case CTL_TASK_TARGET_RESET:
11066				retval = ctl_target_reset(ctl_softc, io,
11067							  CTL_UA_TARG_RESET);
11068				break;
11069			case CTL_TASK_BUS_RESET:
11070				retval = ctl_bus_reset(ctl_softc, io);
11071				break;
11072			case CTL_TASK_PORT_LOGIN:
11073				break;
11074			case CTL_TASK_PORT_LOGOUT:
11075				break;
11076			default:
11077				printf("ctl_run_task_queue: got unknown task "
11078				       "management event %d\n",
11079				       io->taskio.task_action);
11080				break;
11081			}
11082			if (retval == 0)
11083				io->io_hdr.status = CTL_SUCCESS;
11084			else
11085				io->io_hdr.status = CTL_ERROR;
11086
11087			STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11088				      ctl_io_hdr, links);
11089			/*
11090			 * This will queue this I/O to the done queue, but the
11091			 * work thread won't be able to process it until we
11092			 * return and the lock is released.
11093			 */
11094			ctl_done_lock(io, /*have_lock*/ 1);
11095			break;
11096		}
11097		default: {
11098
11099			printf("%s: invalid I/O type %d msg %d cdb %x"
11100			       " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11101			       __func__, io->io_hdr.io_type,
11102			       io->io_hdr.msg_type, io->scsiio.cdb[0],
11103			       (uintmax_t)io->io_hdr.nexus.initid.id,
11104			       io->io_hdr.nexus.targ_port,
11105			       (uintmax_t)io->io_hdr.nexus.targ_target.id,
11106			       io->io_hdr.nexus.targ_lun /* XXX */,
11107			       (io->io_hdr.io_type == CTL_IO_TASK) ?
11108			       io->taskio.tag_num : io->scsiio.tag_num);
11109			STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11110				      ctl_io_hdr, links);
11111			ctl_free_io(io);
11112			break;
11113		}
11114		}
11115	}
11116
11117	ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11118}
11119
11120/*
11121 * For HA operation.  Handle commands that come in from the other
11122 * controller.
11123 */
11124static void
11125ctl_handle_isc(union ctl_io *io)
11126{
11127	int free_io;
11128	struct ctl_lun *lun;
11129	struct ctl_softc *ctl_softc;
11130	uint32_t targ_lun;
11131
11132	ctl_softc = control_softc;
11133
11134	targ_lun = io->io_hdr.nexus.targ_lun;
11135	if (io->io_hdr.nexus.lun_map_fn != NULL)
11136		targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11137	lun = ctl_softc->ctl_luns[targ_lun];
11138
11139	switch (io->io_hdr.msg_type) {
11140	case CTL_MSG_SERIALIZE:
11141		free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11142						     /*have_lock*/ 0);
11143		break;
11144	case CTL_MSG_R2R: {
11145		uint8_t opcode;
11146		struct ctl_cmd_entry *entry;
11147
11148		/*
11149		 * This is only used in SER_ONLY mode.
11150		 */
11151		free_io = 0;
11152		opcode = io->scsiio.cdb[0];
11153		entry = &ctl_cmd_table[opcode];
11154		mtx_lock(&ctl_softc->ctl_lock);
11155		if (ctl_scsiio_lun_check(ctl_softc, lun,
11156		    entry, (struct ctl_scsiio *)io) != 0) {
11157			ctl_done_lock(io, /*have_lock*/ 1);
11158			mtx_unlock(&ctl_softc->ctl_lock);
11159			break;
11160		}
11161		io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11162		STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11163				   &io->io_hdr, links);
11164		mtx_unlock(&ctl_softc->ctl_lock);
11165		break;
11166	}
11167	case CTL_MSG_FINISH_IO:
11168		if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11169			free_io = 0;
11170			ctl_done_lock(io, /*have_lock*/ 0);
11171		} else {
11172			free_io = 1;
11173			mtx_lock(&ctl_softc->ctl_lock);
11174			TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11175				     ooa_links);
11176			STAILQ_REMOVE(&ctl_softc->task_queue,
11177				      &io->io_hdr, ctl_io_hdr, links);
11178			ctl_check_blocked(lun);
11179			mtx_unlock(&ctl_softc->ctl_lock);
11180		}
11181		break;
11182	case CTL_MSG_PERS_ACTION:
11183		ctl_hndl_per_res_out_on_other_sc(
11184			(union ctl_ha_msg *)&io->presio.pr_msg);
11185		free_io = 1;
11186		break;
11187	case CTL_MSG_BAD_JUJU:
11188		free_io = 0;
11189		ctl_done_lock(io, /*have_lock*/ 0);
11190		break;
11191	case CTL_MSG_DATAMOVE:
11192		/* Only used in XFER mode */
11193		free_io = 0;
11194		ctl_datamove_remote(io);
11195		break;
11196	case CTL_MSG_DATAMOVE_DONE:
11197		/* Only used in XFER mode */
11198		free_io = 0;
11199		io->scsiio.be_move_done(io);
11200		break;
11201	default:
11202		free_io = 1;
11203		printf("%s: Invalid message type %d\n",
11204		       __func__, io->io_hdr.msg_type);
11205		break;
11206	}
11207	if (free_io)
11208		ctl_free_io(io);
11209
11210}
11211
11212
11213/*
11214 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11215 * there is no match.
11216 */
11217static ctl_lun_error_pattern
11218ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11219{
11220	struct ctl_cmd_entry *entry;
11221	ctl_lun_error_pattern filtered_pattern, pattern;
11222	uint8_t opcode;
11223
11224	pattern = desc->error_pattern;
11225
11226	/*
11227	 * XXX KDM we need more data passed into this function to match a
11228	 * custom pattern, and we actually need to implement custom pattern
11229	 * matching.
11230	 */
11231	if (pattern & CTL_LUN_PAT_CMD)
11232		return (CTL_LUN_PAT_CMD);
11233
11234	if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11235		return (CTL_LUN_PAT_ANY);
11236
11237	opcode = ctsio->cdb[0];
11238	entry = &ctl_cmd_table[opcode];
11239
11240	filtered_pattern = entry->pattern & pattern;
11241
11242	/*
11243	 * If the user requested specific flags in the pattern (e.g.
11244	 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11245	 * flags.
11246	 *
11247	 * If the user did not specify any flags, it doesn't matter whether
11248	 * or not the command supports the flags.
11249	 */
11250	if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11251	     (pattern & ~CTL_LUN_PAT_MASK))
11252		return (CTL_LUN_PAT_NONE);
11253
11254	/*
11255	 * If the user asked for a range check, see if the requested LBA
11256	 * range overlaps with this command's LBA range.
11257	 */
11258	if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11259		uint64_t lba1;
11260		uint32_t len1;
11261		ctl_action action;
11262		int retval;
11263
11264		retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11265		if (retval != 0)
11266			return (CTL_LUN_PAT_NONE);
11267
11268		action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11269					      desc->lba_range.len);
11270		/*
11271		 * A "pass" means that the LBA ranges don't overlap, so
11272		 * this doesn't match the user's range criteria.
11273		 */
11274		if (action == CTL_ACTION_PASS)
11275			return (CTL_LUN_PAT_NONE);
11276	}
11277
11278	return (filtered_pattern);
11279}
11280
11281static void
11282ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11283{
11284	struct ctl_error_desc *desc, *desc2;
11285
11286	mtx_assert(&control_softc->ctl_lock, MA_OWNED);
11287
11288	STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11289		ctl_lun_error_pattern pattern;
11290		/*
11291		 * Check to see whether this particular command matches
11292		 * the pattern in the descriptor.
11293		 */
11294		pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11295		if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11296			continue;
11297
11298		switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11299		case CTL_LUN_INJ_ABORTED:
11300			ctl_set_aborted(&io->scsiio);
11301			break;
11302		case CTL_LUN_INJ_MEDIUM_ERR:
11303			ctl_set_medium_error(&io->scsiio);
11304			break;
11305		case CTL_LUN_INJ_UA:
11306			/* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET
11307			 * OCCURRED */
11308			ctl_set_ua(&io->scsiio, 0x29, 0x00);
11309			break;
11310		case CTL_LUN_INJ_CUSTOM:
11311			/*
11312			 * We're assuming the user knows what he is doing.
11313			 * Just copy the sense information without doing
11314			 * checks.
11315			 */
11316			bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11317			      ctl_min(sizeof(desc->custom_sense),
11318				      sizeof(io->scsiio.sense_data)));
11319			io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11320			io->scsiio.sense_len = SSD_FULL_SIZE;
11321			io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11322			break;
11323		case CTL_LUN_INJ_NONE:
11324		default:
11325			/*
11326			 * If this is an error injection type we don't know
11327			 * about, clear the continuous flag (if it is set)
11328			 * so it will get deleted below.
11329			 */
11330			desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11331			break;
11332		}
11333		/*
11334		 * By default, each error injection action is a one-shot
11335		 */
11336		if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11337			continue;
11338
11339		STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11340
11341		free(desc, M_CTL);
11342	}
11343}
11344
11345#ifdef CTL_IO_DELAY
11346static void
11347ctl_datamove_timer_wakeup(void *arg)
11348{
11349	union ctl_io *io;
11350
11351	io = (union ctl_io *)arg;
11352
11353	ctl_datamove(io);
11354}
11355#endif /* CTL_IO_DELAY */
11356
11357void
11358ctl_datamove(union ctl_io *io)
11359{
11360	void (*fe_datamove)(union ctl_io *io);
11361
11362	mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11363
11364	CTL_DEBUG_PRINT(("ctl_datamove\n"));
11365
11366#ifdef CTL_TIME_IO
11367	if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11368		char str[256];
11369		char path_str[64];
11370		struct sbuf sb;
11371
11372		ctl_scsi_path_string(io, path_str, sizeof(path_str));
11373		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11374
11375		sbuf_cat(&sb, path_str);
11376		switch (io->io_hdr.io_type) {
11377		case CTL_IO_SCSI:
11378			ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11379			sbuf_printf(&sb, "\n");
11380			sbuf_cat(&sb, path_str);
11381			sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11382				    io->scsiio.tag_num, io->scsiio.tag_type);
11383			break;
11384		case CTL_IO_TASK:
11385			sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11386				    "Tag Type: %d\n", io->taskio.task_action,
11387				    io->taskio.tag_num, io->taskio.tag_type);
11388			break;
11389		default:
11390			printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11391			panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11392			break;
11393		}
11394		sbuf_cat(&sb, path_str);
11395		sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11396			    (intmax_t)time_uptime - io->io_hdr.start_time);
11397		sbuf_finish(&sb);
11398		printf("%s", sbuf_data(&sb));
11399	}
11400#endif /* CTL_TIME_IO */
11401
11402	mtx_lock(&control_softc->ctl_lock);
11403#ifdef CTL_IO_DELAY
11404	if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11405		struct ctl_lun *lun;
11406
11407		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11408
11409		io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11410	} else {
11411		struct ctl_lun *lun;
11412
11413		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11414		if ((lun != NULL)
11415		 && (lun->delay_info.datamove_delay > 0)) {
11416			struct callout *callout;
11417
11418			callout = (struct callout *)&io->io_hdr.timer_bytes;
11419			callout_init(callout, /*mpsafe*/ 1);
11420			io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11421			callout_reset(callout,
11422				      lun->delay_info.datamove_delay * hz,
11423				      ctl_datamove_timer_wakeup, io);
11424			if (lun->delay_info.datamove_type ==
11425			    CTL_DELAY_TYPE_ONESHOT)
11426				lun->delay_info.datamove_delay = 0;
11427			mtx_unlock(&control_softc->ctl_lock);
11428			return;
11429		}
11430	}
11431#endif
11432	/*
11433	 * If we have any pending task management commands, process them
11434	 * first.  This is necessary to eliminate a race condition with the
11435	 * FETD:
11436	 *
11437	 * - FETD submits a task management command, like an abort.
11438	 * - Back end calls fe_datamove() to move the data for the aborted
11439	 *   command.  The FETD can't really accept it, but if it did, it
11440	 *   would end up transmitting data for a command that the initiator
11441	 *   told us to abort.
11442	 *
11443	 * We close the race by processing all pending task management
11444	 * commands here (we can't block!), and then check this I/O to see
11445	 * if it has been aborted.  If so, return it to the back end with
11446	 * bad status, so the back end can say return an error to the back end
11447	 * and then when the back end returns an error, we can return the
11448	 * aborted command to the FETD, so it can clean up its resources.
11449	 */
11450	if (control_softc->flags & CTL_FLAG_TASK_PENDING)
11451		ctl_run_task_queue(control_softc);
11452
11453	/*
11454	 * This command has been aborted.  Set the port status, so we fail
11455	 * the data move.
11456	 */
11457	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
11458		printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
11459		       io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
11460		       io->io_hdr.nexus.targ_port,
11461		       (uintmax_t)io->io_hdr.nexus.targ_target.id,
11462		       io->io_hdr.nexus.targ_lun);
11463		io->io_hdr.status = CTL_CMD_ABORTED;
11464		io->io_hdr.port_status = 31337;
11465		mtx_unlock(&control_softc->ctl_lock);
11466		/*
11467		 * Note that the backend, in this case, will get the
11468		 * callback in its context.  In other cases it may get
11469		 * called in the frontend's interrupt thread context.
11470		 */
11471		io->scsiio.be_move_done(io);
11472		return;
11473	}
11474
11475	/*
11476	 * If we're in XFER mode and this I/O is from the other shelf
11477	 * controller, we need to send the DMA to the other side to
11478	 * actually transfer the data to/from the host.  In serialize only
11479	 * mode the transfer happens below CTL and ctl_datamove() is only
11480	 * called on the machine that originally received the I/O.
11481	 */
11482	if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
11483	 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11484		union ctl_ha_msg msg;
11485		uint32_t sg_entries_sent;
11486		int do_sg_copy;
11487		int i;
11488
11489		memset(&msg, 0, sizeof(msg));
11490		msg.hdr.msg_type = CTL_MSG_DATAMOVE;
11491		msg.hdr.original_sc = io->io_hdr.original_sc;
11492		msg.hdr.serializing_sc = io;
11493		msg.hdr.nexus = io->io_hdr.nexus;
11494		msg.dt.flags = io->io_hdr.flags;
11495		/*
11496		 * We convert everything into a S/G list here.  We can't
11497		 * pass by reference, only by value between controllers.
11498		 * So we can't pass a pointer to the S/G list, only as many
11499		 * S/G entries as we can fit in here.  If it's possible for
11500		 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
11501		 * then we need to break this up into multiple transfers.
11502		 */
11503		if (io->scsiio.kern_sg_entries == 0) {
11504			msg.dt.kern_sg_entries = 1;
11505			/*
11506			 * If this is in cached memory, flush the cache
11507			 * before we send the DMA request to the other
11508			 * controller.  We want to do this in either the
11509			 * read or the write case.  The read case is
11510			 * straightforward.  In the write case, we want to
11511			 * make sure nothing is in the local cache that
11512			 * could overwrite the DMAed data.
11513			 */
11514			if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11515				/*
11516				 * XXX KDM use bus_dmamap_sync() here.
11517				 */
11518			}
11519
11520			/*
11521			 * Convert to a physical address if this is a
11522			 * virtual address.
11523			 */
11524			if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
11525				msg.dt.sg_list[0].addr =
11526					io->scsiio.kern_data_ptr;
11527			} else {
11528				/*
11529				 * XXX KDM use busdma here!
11530				 */
11531#if 0
11532				msg.dt.sg_list[0].addr = (void *)
11533					vtophys(io->scsiio.kern_data_ptr);
11534#endif
11535			}
11536
11537			msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
11538			do_sg_copy = 0;
11539		} else {
11540			struct ctl_sg_entry *sgl;
11541
11542			do_sg_copy = 1;
11543			msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
11544			sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
11545			if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11546				/*
11547				 * XXX KDM use bus_dmamap_sync() here.
11548				 */
11549			}
11550		}
11551
11552		msg.dt.kern_data_len = io->scsiio.kern_data_len;
11553		msg.dt.kern_total_len = io->scsiio.kern_total_len;
11554		msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
11555		msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
11556		msg.dt.sg_sequence = 0;
11557
11558		/*
11559		 * Loop until we've sent all of the S/G entries.  On the
11560		 * other end, we'll recompose these S/G entries into one
11561		 * contiguous list before passing it to the
11562		 */
11563		for (sg_entries_sent = 0; sg_entries_sent <
11564		     msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
11565			msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
11566				sizeof(msg.dt.sg_list[0])),
11567				msg.dt.kern_sg_entries - sg_entries_sent);
11568
11569			if (do_sg_copy != 0) {
11570				struct ctl_sg_entry *sgl;
11571				int j;
11572
11573				sgl = (struct ctl_sg_entry *)
11574					io->scsiio.kern_data_ptr;
11575				/*
11576				 * If this is in cached memory, flush the cache
11577				 * before we send the DMA request to the other
11578				 * controller.  We want to do this in either
11579				 * the * read or the write case.  The read
11580				 * case is straightforward.  In the write
11581				 * case, we want to make sure nothing is
11582				 * in the local cache that could overwrite
11583				 * the DMAed data.
11584				 */
11585
11586				for (i = sg_entries_sent, j = 0;
11587				     i < msg.dt.cur_sg_entries; i++, j++) {
11588					if ((io->io_hdr.flags &
11589					     CTL_FLAG_NO_DATASYNC) == 0) {
11590						/*
11591						 * XXX KDM use bus_dmamap_sync()
11592						 */
11593					}
11594					if ((io->io_hdr.flags &
11595					     CTL_FLAG_BUS_ADDR) == 0) {
11596						/*
11597						 * XXX KDM use busdma.
11598						 */
11599#if 0
11600						msg.dt.sg_list[j].addr =(void *)
11601						       vtophys(sgl[i].addr);
11602#endif
11603					} else {
11604						msg.dt.sg_list[j].addr =
11605							sgl[i].addr;
11606					}
11607					msg.dt.sg_list[j].len = sgl[i].len;
11608				}
11609			}
11610
11611			sg_entries_sent += msg.dt.cur_sg_entries;
11612			if (sg_entries_sent >= msg.dt.kern_sg_entries)
11613				msg.dt.sg_last = 1;
11614			else
11615				msg.dt.sg_last = 0;
11616
11617			/*
11618			 * XXX KDM drop and reacquire the lock here?
11619			 */
11620			if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
11621			    sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
11622				/*
11623				 * XXX do something here.
11624				 */
11625			}
11626
11627			msg.dt.sent_sg_entries = sg_entries_sent;
11628		}
11629		io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11630		if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
11631			ctl_failover_io(io, /*have_lock*/ 1);
11632
11633	} else {
11634
11635		/*
11636		 * Lookup the fe_datamove() function for this particular
11637		 * front end.
11638		 */
11639		fe_datamove =
11640		    control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11641		mtx_unlock(&control_softc->ctl_lock);
11642
11643		fe_datamove(io);
11644	}
11645}
11646
11647static void
11648ctl_send_datamove_done(union ctl_io *io, int have_lock)
11649{
11650	union ctl_ha_msg msg;
11651	int isc_status;
11652
11653	memset(&msg, 0, sizeof(msg));
11654
11655	msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
11656	msg.hdr.original_sc = io;
11657	msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
11658	msg.hdr.nexus = io->io_hdr.nexus;
11659	msg.hdr.status = io->io_hdr.status;
11660	msg.scsi.tag_num = io->scsiio.tag_num;
11661	msg.scsi.tag_type = io->scsiio.tag_type;
11662	msg.scsi.scsi_status = io->scsiio.scsi_status;
11663	memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
11664	       sizeof(io->scsiio.sense_data));
11665	msg.scsi.sense_len = io->scsiio.sense_len;
11666	msg.scsi.sense_residual = io->scsiio.sense_residual;
11667	msg.scsi.fetd_status = io->io_hdr.port_status;
11668	msg.scsi.residual = io->scsiio.residual;
11669	io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11670
11671	if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
11672		ctl_failover_io(io, /*have_lock*/ have_lock);
11673		return;
11674	}
11675
11676	isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
11677	if (isc_status > CTL_HA_STATUS_SUCCESS) {
11678		/* XXX do something if this fails */
11679	}
11680
11681}
11682
11683/*
11684 * The DMA to the remote side is done, now we need to tell the other side
11685 * we're done so it can continue with its data movement.
11686 */
11687static void
11688ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
11689{
11690	union ctl_io *io;
11691
11692	io = rq->context;
11693
11694	if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11695		printf("%s: ISC DMA write failed with error %d", __func__,
11696		       rq->ret);
11697		ctl_set_internal_failure(&io->scsiio,
11698					 /*sks_valid*/ 1,
11699					 /*retry_count*/ rq->ret);
11700	}
11701
11702	ctl_dt_req_free(rq);
11703
11704	/*
11705	 * In this case, we had to malloc the memory locally.  Free it.
11706	 */
11707	if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11708		int i;
11709		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11710			free(io->io_hdr.local_sglist[i].addr, M_CTL);
11711	}
11712	/*
11713	 * The data is in local and remote memory, so now we need to send
11714	 * status (good or back) back to the other side.
11715	 */
11716	ctl_send_datamove_done(io, /*have_lock*/ 0);
11717}
11718
11719/*
11720 * We've moved the data from the host/controller into local memory.  Now we
11721 * need to push it over to the remote controller's memory.
11722 */
11723static int
11724ctl_datamove_remote_dm_write_cb(union ctl_io *io)
11725{
11726	int retval;
11727
11728	retval = 0;
11729
11730	retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
11731					  ctl_datamove_remote_write_cb);
11732
11733	return (retval);
11734}
11735
11736static void
11737ctl_datamove_remote_write(union ctl_io *io)
11738{
11739	int retval;
11740	void (*fe_datamove)(union ctl_io *io);
11741
11742	/*
11743	 * - Get the data from the host/HBA into local memory.
11744	 * - DMA memory from the local controller to the remote controller.
11745	 * - Send status back to the remote controller.
11746	 */
11747
11748	retval = ctl_datamove_remote_sgl_setup(io);
11749	if (retval != 0)
11750		return;
11751
11752	/* Switch the pointer over so the FETD knows what to do */
11753	io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11754
11755	/*
11756	 * Use a custom move done callback, since we need to send completion
11757	 * back to the other controller, not to the backend on this side.
11758	 */
11759	io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
11760
11761	fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11762
11763	fe_datamove(io);
11764
11765	return;
11766
11767}
11768
11769static int
11770ctl_datamove_remote_dm_read_cb(union ctl_io *io)
11771{
11772#if 0
11773	char str[256];
11774	char path_str[64];
11775	struct sbuf sb;
11776#endif
11777
11778	/*
11779	 * In this case, we had to malloc the memory locally.  Free it.
11780	 */
11781	if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11782		int i;
11783		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11784			free(io->io_hdr.local_sglist[i].addr, M_CTL);
11785	}
11786
11787#if 0
11788	scsi_path_string(io, path_str, sizeof(path_str));
11789	sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11790	sbuf_cat(&sb, path_str);
11791	scsi_command_string(&io->scsiio, NULL, &sb);
11792	sbuf_printf(&sb, "\n");
11793	sbuf_cat(&sb, path_str);
11794	sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11795		    io->scsiio.tag_num, io->scsiio.tag_type);
11796	sbuf_cat(&sb, path_str);
11797	sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
11798		    io->io_hdr.flags, io->io_hdr.status);
11799	sbuf_finish(&sb);
11800	printk("%s", sbuf_data(&sb));
11801#endif
11802
11803
11804	/*
11805	 * The read is done, now we need to send status (good or bad) back
11806	 * to the other side.
11807	 */
11808	ctl_send_datamove_done(io, /*have_lock*/ 0);
11809
11810	return (0);
11811}
11812
11813static void
11814ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
11815{
11816	union ctl_io *io;
11817	void (*fe_datamove)(union ctl_io *io);
11818
11819	io = rq->context;
11820
11821	if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11822		printf("%s: ISC DMA read failed with error %d", __func__,
11823		       rq->ret);
11824		ctl_set_internal_failure(&io->scsiio,
11825					 /*sks_valid*/ 1,
11826					 /*retry_count*/ rq->ret);
11827	}
11828
11829	ctl_dt_req_free(rq);
11830
11831	/* Switch the pointer over so the FETD knows what to do */
11832	io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11833
11834	/*
11835	 * Use a custom move done callback, since we need to send completion
11836	 * back to the other controller, not to the backend on this side.
11837	 */
11838	io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
11839
11840	/* XXX KDM add checks like the ones in ctl_datamove? */
11841
11842	fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11843
11844	fe_datamove(io);
11845}
11846
11847static int
11848ctl_datamove_remote_sgl_setup(union ctl_io *io)
11849{
11850	struct ctl_sg_entry *local_sglist, *remote_sglist;
11851	struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
11852	struct ctl_softc *softc;
11853	int retval;
11854	int i;
11855
11856	retval = 0;
11857	softc = control_softc;
11858
11859	local_sglist = io->io_hdr.local_sglist;
11860	local_dma_sglist = io->io_hdr.local_dma_sglist;
11861	remote_sglist = io->io_hdr.remote_sglist;
11862	remote_dma_sglist = io->io_hdr.remote_dma_sglist;
11863
11864	if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
11865		for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
11866			local_sglist[i].len = remote_sglist[i].len;
11867
11868			/*
11869			 * XXX Detect the situation where the RS-level I/O
11870			 * redirector on the other side has already read the
11871			 * data off of the AOR RS on this side, and
11872			 * transferred it to remote (mirror) memory on the
11873			 * other side.  Since we already have the data in
11874			 * memory here, we just need to use it.
11875			 *
11876			 * XXX KDM this can probably be removed once we
11877			 * get the cache device code in and take the
11878			 * current AOR implementation out.
11879			 */
11880#ifdef NEEDTOPORT
11881			if ((remote_sglist[i].addr >=
11882			     (void *)vtophys(softc->mirr->addr))
11883			 && (remote_sglist[i].addr <
11884			     ((void *)vtophys(softc->mirr->addr) +
11885			     CacheMirrorOffset))) {
11886				local_sglist[i].addr = remote_sglist[i].addr -
11887					CacheMirrorOffset;
11888				if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
11889				     CTL_FLAG_DATA_IN)
11890					io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
11891			} else {
11892				local_sglist[i].addr = remote_sglist[i].addr +
11893					CacheMirrorOffset;
11894			}
11895#endif
11896#if 0
11897			printf("%s: local %p, remote %p, len %d\n",
11898			       __func__, local_sglist[i].addr,
11899			       remote_sglist[i].addr, local_sglist[i].len);
11900#endif
11901		}
11902	} else {
11903		uint32_t len_to_go;
11904
11905		/*
11906		 * In this case, we don't have automatically allocated
11907		 * memory for this I/O on this controller.  This typically
11908		 * happens with internal CTL I/O -- e.g. inquiry, mode
11909		 * sense, etc.  Anything coming from RAIDCore will have
11910		 * a mirror area available.
11911		 */
11912		len_to_go = io->scsiio.kern_data_len;
11913
11914		/*
11915		 * Clear the no datasync flag, we have to use malloced
11916		 * buffers.
11917		 */
11918		io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
11919
11920		/*
11921		 * The difficult thing here is that the size of the various
11922		 * S/G segments may be different than the size from the
11923		 * remote controller.  That'll make it harder when DMAing
11924		 * the data back to the other side.
11925		 */
11926		for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
11927		     sizeof(io->io_hdr.remote_sglist[0])) &&
11928		     (len_to_go > 0); i++) {
11929			local_sglist[i].len = ctl_min(len_to_go, 131072);
11930			CTL_SIZE_8B(local_dma_sglist[i].len,
11931				    local_sglist[i].len);
11932			local_sglist[i].addr =
11933				malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
11934
11935			local_dma_sglist[i].addr = local_sglist[i].addr;
11936
11937			if (local_sglist[i].addr == NULL) {
11938				int j;
11939
11940				printf("malloc failed for %zd bytes!",
11941				       local_dma_sglist[i].len);
11942				for (j = 0; j < i; j++) {
11943					free(local_sglist[j].addr, M_CTL);
11944				}
11945				ctl_set_internal_failure(&io->scsiio,
11946							 /*sks_valid*/ 1,
11947							 /*retry_count*/ 4857);
11948				retval = 1;
11949				goto bailout_error;
11950
11951			}
11952			/* XXX KDM do we need a sync here? */
11953
11954			len_to_go -= local_sglist[i].len;
11955		}
11956		/*
11957		 * Reset the number of S/G entries accordingly.  The
11958		 * original number of S/G entries is available in
11959		 * rem_sg_entries.
11960		 */
11961		io->scsiio.kern_sg_entries = i;
11962
11963#if 0
11964		printf("%s: kern_sg_entries = %d\n", __func__,
11965		       io->scsiio.kern_sg_entries);
11966		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11967			printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
11968			       local_sglist[i].addr, local_sglist[i].len,
11969			       local_dma_sglist[i].len);
11970#endif
11971	}
11972
11973
11974	return (retval);
11975
11976bailout_error:
11977
11978	ctl_send_datamove_done(io, /*have_lock*/ 0);
11979
11980	return (retval);
11981}
11982
11983static int
11984ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
11985			 ctl_ha_dt_cb callback)
11986{
11987	struct ctl_ha_dt_req *rq;
11988	struct ctl_sg_entry *remote_sglist, *local_sglist;
11989	struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
11990	uint32_t local_used, remote_used, total_used;
11991	int retval;
11992	int i, j;
11993
11994	retval = 0;
11995
11996	rq = ctl_dt_req_alloc();
11997
11998	/*
11999	 * If we failed to allocate the request, and if the DMA didn't fail
12000	 * anyway, set busy status.  This is just a resource allocation
12001	 * failure.
12002	 */
12003	if ((rq == NULL)
12004	 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12005		ctl_set_busy(&io->scsiio);
12006
12007	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12008
12009		if (rq != NULL)
12010			ctl_dt_req_free(rq);
12011
12012		/*
12013		 * The data move failed.  We need to return status back
12014		 * to the other controller.  No point in trying to DMA
12015		 * data to the remote controller.
12016		 */
12017
12018		ctl_send_datamove_done(io, /*have_lock*/ 0);
12019
12020		retval = 1;
12021
12022		goto bailout;
12023	}
12024
12025	local_sglist = io->io_hdr.local_sglist;
12026	local_dma_sglist = io->io_hdr.local_dma_sglist;
12027	remote_sglist = io->io_hdr.remote_sglist;
12028	remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12029	local_used = 0;
12030	remote_used = 0;
12031	total_used = 0;
12032
12033	if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12034		rq->ret = CTL_HA_STATUS_SUCCESS;
12035		rq->context = io;
12036		callback(rq);
12037		goto bailout;
12038	}
12039
12040	/*
12041	 * Pull/push the data over the wire from/to the other controller.
12042	 * This takes into account the possibility that the local and
12043	 * remote sglists may not be identical in terms of the size of
12044	 * the elements and the number of elements.
12045	 *
12046	 * One fundamental assumption here is that the length allocated for
12047	 * both the local and remote sglists is identical.  Otherwise, we've
12048	 * essentially got a coding error of some sort.
12049	 */
12050	for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12051		int isc_ret;
12052		uint32_t cur_len, dma_length;
12053		uint8_t *tmp_ptr;
12054
12055		rq->id = CTL_HA_DATA_CTL;
12056		rq->command = command;
12057		rq->context = io;
12058
12059		/*
12060		 * Both pointers should be aligned.  But it is possible
12061		 * that the allocation length is not.  They should both
12062		 * also have enough slack left over at the end, though,
12063		 * to round up to the next 8 byte boundary.
12064		 */
12065		cur_len = ctl_min(local_sglist[i].len - local_used,
12066				  remote_sglist[j].len - remote_used);
12067
12068		/*
12069		 * In this case, we have a size issue and need to decrease
12070		 * the size, except in the case where we actually have less
12071		 * than 8 bytes left.  In that case, we need to increase
12072		 * the DMA length to get the last bit.
12073		 */
12074		if ((cur_len & 0x7) != 0) {
12075			if (cur_len > 0x7) {
12076				cur_len = cur_len - (cur_len & 0x7);
12077				dma_length = cur_len;
12078			} else {
12079				CTL_SIZE_8B(dma_length, cur_len);
12080			}
12081
12082		} else
12083			dma_length = cur_len;
12084
12085		/*
12086		 * If we had to allocate memory for this I/O, instead of using
12087		 * the non-cached mirror memory, we'll need to flush the cache
12088		 * before trying to DMA to the other controller.
12089		 *
12090		 * We could end up doing this multiple times for the same
12091		 * segment if we have a larger local segment than remote
12092		 * segment.  That shouldn't be an issue.
12093		 */
12094		if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12095			/*
12096			 * XXX KDM use bus_dmamap_sync() here.
12097			 */
12098		}
12099
12100		rq->size = dma_length;
12101
12102		tmp_ptr = (uint8_t *)local_sglist[i].addr;
12103		tmp_ptr += local_used;
12104
12105		/* Use physical addresses when talking to ISC hardware */
12106		if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12107			/* XXX KDM use busdma */
12108#if 0
12109			rq->local = vtophys(tmp_ptr);
12110#endif
12111		} else
12112			rq->local = tmp_ptr;
12113
12114		tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12115		tmp_ptr += remote_used;
12116		rq->remote = tmp_ptr;
12117
12118		rq->callback = NULL;
12119
12120		local_used += cur_len;
12121		if (local_used >= local_sglist[i].len) {
12122			i++;
12123			local_used = 0;
12124		}
12125
12126		remote_used += cur_len;
12127		if (remote_used >= remote_sglist[j].len) {
12128			j++;
12129			remote_used = 0;
12130		}
12131		total_used += cur_len;
12132
12133		if (total_used >= io->scsiio.kern_data_len)
12134			rq->callback = callback;
12135
12136		if ((rq->size & 0x7) != 0) {
12137			printf("%s: warning: size %d is not on 8b boundary\n",
12138			       __func__, rq->size);
12139		}
12140		if (((uintptr_t)rq->local & 0x7) != 0) {
12141			printf("%s: warning: local %p not on 8b boundary\n",
12142			       __func__, rq->local);
12143		}
12144		if (((uintptr_t)rq->remote & 0x7) != 0) {
12145			printf("%s: warning: remote %p not on 8b boundary\n",
12146			       __func__, rq->local);
12147		}
12148#if 0
12149		printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12150		       (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12151		       rq->local, rq->remote, rq->size);
12152#endif
12153
12154		isc_ret = ctl_dt_single(rq);
12155		if (isc_ret == CTL_HA_STATUS_WAIT)
12156			continue;
12157
12158		if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12159			rq->ret = CTL_HA_STATUS_SUCCESS;
12160		} else {
12161			rq->ret = isc_ret;
12162		}
12163		callback(rq);
12164		goto bailout;
12165	}
12166
12167bailout:
12168	return (retval);
12169
12170}
12171
12172static void
12173ctl_datamove_remote_read(union ctl_io *io)
12174{
12175	int retval;
12176	int i;
12177
12178	/*
12179	 * This will send an error to the other controller in the case of a
12180	 * failure.
12181	 */
12182	retval = ctl_datamove_remote_sgl_setup(io);
12183	if (retval != 0)
12184		return;
12185
12186	retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12187					  ctl_datamove_remote_read_cb);
12188	if ((retval != 0)
12189	 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12190		/*
12191		 * Make sure we free memory if there was an error..  The
12192		 * ctl_datamove_remote_xfer() function will send the
12193		 * datamove done message, or call the callback with an
12194		 * error if there is a problem.
12195		 */
12196		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12197			free(io->io_hdr.local_sglist[i].addr, M_CTL);
12198	}
12199
12200	return;
12201}
12202
12203/*
12204 * Process a datamove request from the other controller.  This is used for
12205 * XFER mode only, not SER_ONLY mode.  For writes, we DMA into local memory
12206 * first.  Once that is complete, the data gets DMAed into the remote
12207 * controller's memory.  For reads, we DMA from the remote controller's
12208 * memory into our memory first, and then move it out to the FETD.
12209 */
12210static void
12211ctl_datamove_remote(union ctl_io *io)
12212{
12213	struct ctl_softc *softc;
12214
12215	softc = control_softc;
12216
12217	mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12218
12219	/*
12220	 * Note that we look for an aborted I/O here, but don't do some of
12221	 * the other checks that ctl_datamove() normally does.  We don't
12222	 * need to run the task queue, because this I/O is on the ISC
12223	 * queue, which is executed by the work thread after the task queue.
12224	 * We don't need to run the datamove delay code, since that should
12225	 * have been done if need be on the other controller.
12226	 */
12227	mtx_lock(&softc->ctl_lock);
12228
12229	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12230
12231		printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12232		       io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12233		       io->io_hdr.nexus.targ_port,
12234		       io->io_hdr.nexus.targ_target.id,
12235		       io->io_hdr.nexus.targ_lun);
12236		io->io_hdr.status = CTL_CMD_ABORTED;
12237		io->io_hdr.port_status = 31338;
12238
12239		mtx_unlock(&softc->ctl_lock);
12240
12241		ctl_send_datamove_done(io, /*have_lock*/ 0);
12242
12243		return;
12244	}
12245
12246	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12247		mtx_unlock(&softc->ctl_lock);
12248		ctl_datamove_remote_write(io);
12249	} else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12250		mtx_unlock(&softc->ctl_lock);
12251		ctl_datamove_remote_read(io);
12252	} else {
12253		union ctl_ha_msg msg;
12254		struct scsi_sense_data *sense;
12255		uint8_t sks[3];
12256		int retry_count;
12257
12258		memset(&msg, 0, sizeof(msg));
12259
12260		msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12261		msg.hdr.status = CTL_SCSI_ERROR;
12262		msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12263
12264		retry_count = 4243;
12265
12266		sense = &msg.scsi.sense_data;
12267		sks[0] = SSD_SCS_VALID;
12268		sks[1] = (retry_count >> 8) & 0xff;
12269		sks[2] = retry_count & 0xff;
12270
12271		/* "Internal target failure" */
12272		scsi_set_sense_data(sense,
12273				    /*sense_format*/ SSD_TYPE_NONE,
12274				    /*current_error*/ 1,
12275				    /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12276				    /*asc*/ 0x44,
12277				    /*ascq*/ 0x00,
12278				    /*type*/ SSD_ELEM_SKS,
12279				    /*size*/ sizeof(sks),
12280				    /*data*/ sks,
12281				    SSD_ELEM_NONE);
12282
12283		io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12284		if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12285			ctl_failover_io(io, /*have_lock*/ 1);
12286			mtx_unlock(&softc->ctl_lock);
12287			return;
12288		}
12289
12290		mtx_unlock(&softc->ctl_lock);
12291
12292		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12293		    CTL_HA_STATUS_SUCCESS) {
12294			/* XXX KDM what to do if this fails? */
12295		}
12296		return;
12297	}
12298
12299}
12300
12301static int
12302ctl_process_done(union ctl_io *io, int have_lock)
12303{
12304	struct ctl_lun *lun;
12305	struct ctl_softc *ctl_softc;
12306	void (*fe_done)(union ctl_io *io);
12307	uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12308
12309	CTL_DEBUG_PRINT(("ctl_process_done\n"));
12310
12311	fe_done =
12312	    control_softc->ctl_ports[targ_port]->fe_done;
12313
12314#ifdef CTL_TIME_IO
12315	if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12316		char str[256];
12317		char path_str[64];
12318		struct sbuf sb;
12319
12320		ctl_scsi_path_string(io, path_str, sizeof(path_str));
12321		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12322
12323		sbuf_cat(&sb, path_str);
12324		switch (io->io_hdr.io_type) {
12325		case CTL_IO_SCSI:
12326			ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12327			sbuf_printf(&sb, "\n");
12328			sbuf_cat(&sb, path_str);
12329			sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12330				    io->scsiio.tag_num, io->scsiio.tag_type);
12331			break;
12332		case CTL_IO_TASK:
12333			sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12334				    "Tag Type: %d\n", io->taskio.task_action,
12335				    io->taskio.tag_num, io->taskio.tag_type);
12336			break;
12337		default:
12338			printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12339			panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12340			break;
12341		}
12342		sbuf_cat(&sb, path_str);
12343		sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12344			    (intmax_t)time_uptime - io->io_hdr.start_time);
12345		sbuf_finish(&sb);
12346		printf("%s", sbuf_data(&sb));
12347	}
12348#endif /* CTL_TIME_IO */
12349
12350	switch (io->io_hdr.io_type) {
12351	case CTL_IO_SCSI:
12352		break;
12353	case CTL_IO_TASK:
12354		ctl_io_error_print(io, NULL);
12355		if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12356			ctl_free_io(io);
12357		else
12358			fe_done(io);
12359		return (CTL_RETVAL_COMPLETE);
12360		break;
12361	default:
12362		printf("ctl_process_done: invalid io type %d\n",
12363		       io->io_hdr.io_type);
12364		panic("ctl_process_done: invalid io type %d\n",
12365		      io->io_hdr.io_type);
12366		break; /* NOTREACHED */
12367	}
12368
12369	lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12370	if (lun == NULL) {
12371		CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12372				 io->io_hdr.nexus.targ_lun));
12373		fe_done(io);
12374		goto bailout;
12375	}
12376	ctl_softc = lun->ctl_softc;
12377
12378	/*
12379	 * Remove this from the OOA queue.
12380	 */
12381	if (have_lock == 0)
12382		mtx_lock(&ctl_softc->ctl_lock);
12383
12384	/*
12385	 * Check to see if we have any errors to inject here.  We only
12386	 * inject errors for commands that don't already have errors set.
12387	 */
12388	if ((STAILQ_FIRST(&lun->error_list) != NULL)
12389	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12390		ctl_inject_error(lun, io);
12391
12392	/*
12393	 * XXX KDM how do we treat commands that aren't completed
12394	 * successfully?
12395	 *
12396	 * XXX KDM should we also track I/O latency?
12397	 */
12398	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
12399		uint32_t blocksize;
12400#ifdef CTL_TIME_IO
12401		struct bintime cur_bt;
12402#endif
12403
12404		if ((lun->be_lun != NULL)
12405		 && (lun->be_lun->blocksize != 0))
12406			blocksize = lun->be_lun->blocksize;
12407		else
12408			blocksize = 512;
12409
12410		switch (io->io_hdr.io_type) {
12411		case CTL_IO_SCSI: {
12412			int isread;
12413			struct ctl_lba_len lbalen;
12414
12415			isread = 0;
12416			switch (io->scsiio.cdb[0]) {
12417			case READ_6:
12418			case READ_10:
12419			case READ_12:
12420			case READ_16:
12421				isread = 1;
12422				/* FALLTHROUGH */
12423			case WRITE_6:
12424			case WRITE_10:
12425			case WRITE_12:
12426			case WRITE_16:
12427			case WRITE_VERIFY_10:
12428			case WRITE_VERIFY_12:
12429			case WRITE_VERIFY_16:
12430				memcpy(&lbalen, io->io_hdr.ctl_private[
12431				       CTL_PRIV_LBA_LEN].bytes, sizeof(lbalen));
12432
12433				if (isread) {
12434					lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
12435						lbalen.len * blocksize;
12436					lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
12437
12438#ifdef CTL_TIME_IO
12439					bintime_add(
12440					   &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
12441					   &io->io_hdr.dma_bt);
12442					lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
12443						io->io_hdr.num_dmas;
12444					getbintime(&cur_bt);
12445					bintime_sub(&cur_bt,
12446						    &io->io_hdr.start_bt);
12447
12448					bintime_add(
12449					    &lun->stats.ports[targ_port].time[CTL_STATS_READ],
12450					    &cur_bt);
12451
12452#if 0
12453					cs_prof_gettime(&cur_ticks);
12454					lun->stats.time[CTL_STATS_READ] +=
12455						cur_ticks -
12456						io->io_hdr.start_ticks;
12457#endif
12458#if 0
12459					lun->stats.time[CTL_STATS_READ] +=
12460						jiffies - io->io_hdr.start_time;
12461#endif
12462#endif /* CTL_TIME_IO */
12463				} else {
12464					lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
12465						lbalen.len * blocksize;
12466					lun->stats.ports[targ_port].operations[
12467						CTL_STATS_WRITE]++;
12468
12469#ifdef CTL_TIME_IO
12470					bintime_add(
12471					  &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
12472					  &io->io_hdr.dma_bt);
12473					lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
12474						io->io_hdr.num_dmas;
12475					getbintime(&cur_bt);
12476					bintime_sub(&cur_bt,
12477						    &io->io_hdr.start_bt);
12478
12479					bintime_add(
12480					    &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
12481					    &cur_bt);
12482#if 0
12483					cs_prof_gettime(&cur_ticks);
12484					lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12485						cur_ticks -
12486						io->io_hdr.start_ticks;
12487					lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12488						jiffies - io->io_hdr.start_time;
12489#endif
12490#endif /* CTL_TIME_IO */
12491				}
12492				break;
12493			default:
12494				lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
12495
12496#ifdef CTL_TIME_IO
12497				bintime_add(
12498				  &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
12499				  &io->io_hdr.dma_bt);
12500				lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
12501					io->io_hdr.num_dmas;
12502				getbintime(&cur_bt);
12503				bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12504
12505				bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
12506					    &cur_bt);
12507
12508#if 0
12509				cs_prof_gettime(&cur_ticks);
12510				lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12511					cur_ticks -
12512					io->io_hdr.start_ticks;
12513				lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12514					jiffies - io->io_hdr.start_time;
12515#endif
12516#endif /* CTL_TIME_IO */
12517				break;
12518			}
12519			break;
12520		}
12521		default:
12522			break;
12523		}
12524	}
12525
12526	TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12527
12528	/*
12529	 * Run through the blocked queue on this LUN and see if anything
12530	 * has become unblocked, now that this transaction is done.
12531	 */
12532	ctl_check_blocked(lun);
12533
12534	/*
12535	 * If the LUN has been invalidated, free it if there is nothing
12536	 * left on its OOA queue.
12537	 */
12538	if ((lun->flags & CTL_LUN_INVALID)
12539	 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
12540		ctl_free_lun(lun);
12541
12542	/*
12543	 * If this command has been aborted, make sure we set the status
12544	 * properly.  The FETD is responsible for freeing the I/O and doing
12545	 * whatever it needs to do to clean up its state.
12546	 */
12547	if (io->io_hdr.flags & CTL_FLAG_ABORT)
12548		io->io_hdr.status = CTL_CMD_ABORTED;
12549
12550	/*
12551	 * We print out status for every task management command.  For SCSI
12552	 * commands, we filter out any unit attention errors; they happen
12553	 * on every boot, and would clutter up the log.  Note:  task
12554	 * management commands aren't printed here, they are printed above,
12555	 * since they should never even make it down here.
12556	 */
12557	switch (io->io_hdr.io_type) {
12558	case CTL_IO_SCSI: {
12559		int error_code, sense_key, asc, ascq;
12560
12561		sense_key = 0;
12562
12563		if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
12564		 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
12565			/*
12566			 * Since this is just for printing, no need to
12567			 * show errors here.
12568			 */
12569			scsi_extract_sense_len(&io->scsiio.sense_data,
12570					       io->scsiio.sense_len,
12571					       &error_code,
12572					       &sense_key,
12573					       &asc,
12574					       &ascq,
12575					       /*show_errors*/ 0);
12576		}
12577
12578		if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
12579		 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
12580		  || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
12581		  || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
12582
12583			if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
12584				ctl_softc->skipped_prints++;
12585				if (have_lock == 0)
12586					mtx_unlock(&ctl_softc->ctl_lock);
12587			} else {
12588				uint32_t skipped_prints;
12589
12590				skipped_prints = ctl_softc->skipped_prints;
12591
12592				ctl_softc->skipped_prints = 0;
12593				ctl_softc->last_print_jiffies = time_uptime;
12594
12595				if (have_lock == 0)
12596					mtx_unlock(&ctl_softc->ctl_lock);
12597				if (skipped_prints > 0) {
12598#ifdef NEEDTOPORT
12599					csevent_log(CSC_CTL | CSC_SHELF_SW |
12600					    CTL_ERROR_REPORT,
12601					    csevent_LogType_Trace,
12602					    csevent_Severity_Information,
12603					    csevent_AlertLevel_Green,
12604					    csevent_FRU_Firmware,
12605					    csevent_FRU_Unknown,
12606					    "High CTL error volume, %d prints "
12607					    "skipped", skipped_prints);
12608#endif
12609				}
12610				ctl_io_error_print(io, NULL);
12611			}
12612		} else {
12613			if (have_lock == 0)
12614				mtx_unlock(&ctl_softc->ctl_lock);
12615		}
12616		break;
12617	}
12618	case CTL_IO_TASK:
12619		if (have_lock == 0)
12620			mtx_unlock(&ctl_softc->ctl_lock);
12621		ctl_io_error_print(io, NULL);
12622		break;
12623	default:
12624		if (have_lock == 0)
12625			mtx_unlock(&ctl_softc->ctl_lock);
12626		break;
12627	}
12628
12629	/*
12630	 * Tell the FETD or the other shelf controller we're done with this
12631	 * command.  Note that only SCSI commands get to this point.  Task
12632	 * management commands are completed above.
12633	 *
12634	 * We only send status to the other controller if we're in XFER
12635	 * mode.  In SER_ONLY mode, the I/O is done on the controller that
12636	 * received the I/O (from CTL's perspective), and so the status is
12637	 * generated there.
12638	 *
12639	 * XXX KDM if we hold the lock here, we could cause a deadlock
12640	 * if the frontend comes back in in this context to queue
12641	 * something.
12642	 */
12643	if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
12644	 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12645		union ctl_ha_msg msg;
12646
12647		memset(&msg, 0, sizeof(msg));
12648		msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12649		msg.hdr.original_sc = io->io_hdr.original_sc;
12650		msg.hdr.nexus = io->io_hdr.nexus;
12651		msg.hdr.status = io->io_hdr.status;
12652		msg.scsi.scsi_status = io->scsiio.scsi_status;
12653		msg.scsi.tag_num = io->scsiio.tag_num;
12654		msg.scsi.tag_type = io->scsiio.tag_type;
12655		msg.scsi.sense_len = io->scsiio.sense_len;
12656		msg.scsi.sense_residual = io->scsiio.sense_residual;
12657		msg.scsi.residual = io->scsiio.residual;
12658		memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12659		       sizeof(io->scsiio.sense_data));
12660		/*
12661		 * We copy this whether or not this is an I/O-related
12662		 * command.  Otherwise, we'd have to go and check to see
12663		 * whether it's a read/write command, and it really isn't
12664		 * worth it.
12665		 */
12666		memcpy(&msg.scsi.lbalen,
12667		       &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
12668		       sizeof(msg.scsi.lbalen));
12669
12670		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12671				sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12672			/* XXX do something here */
12673		}
12674
12675		ctl_free_io(io);
12676	} else
12677		fe_done(io);
12678
12679bailout:
12680
12681	return (CTL_RETVAL_COMPLETE);
12682}
12683
12684/*
12685 * Front end should call this if it doesn't do autosense.  When the request
12686 * sense comes back in from the initiator, we'll dequeue this and send it.
12687 */
12688int
12689ctl_queue_sense(union ctl_io *io)
12690{
12691	struct ctl_lun *lun;
12692	struct ctl_softc *ctl_softc;
12693	uint32_t initidx, targ_lun;
12694
12695	ctl_softc = control_softc;
12696
12697	CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
12698
12699	/*
12700	 * LUN lookup will likely move to the ctl_work_thread() once we
12701	 * have our new queueing infrastructure (that doesn't put things on
12702	 * a per-LUN queue initially).  That is so that we can handle
12703	 * things like an INQUIRY to a LUN that we don't have enabled.  We
12704	 * can't deal with that right now.
12705	 */
12706	mtx_lock(&ctl_softc->ctl_lock);
12707
12708	/*
12709	 * If we don't have a LUN for this, just toss the sense
12710	 * information.
12711	 */
12712	targ_lun = io->io_hdr.nexus.targ_lun;
12713	if (io->io_hdr.nexus.lun_map_fn != NULL)
12714		targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
12715	if ((targ_lun < CTL_MAX_LUNS)
12716	 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12717		lun = ctl_softc->ctl_luns[targ_lun];
12718	else
12719		goto bailout;
12720
12721	initidx = ctl_get_initindex(&io->io_hdr.nexus);
12722
12723	/*
12724	 * Already have CA set for this LUN...toss the sense information.
12725	 */
12726	if (ctl_is_set(lun->have_ca, initidx))
12727		goto bailout;
12728
12729	memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
12730	       ctl_min(sizeof(lun->pending_sense[initidx].sense),
12731	       sizeof(io->scsiio.sense_data)));
12732	ctl_set_mask(lun->have_ca, initidx);
12733
12734bailout:
12735	mtx_unlock(&ctl_softc->ctl_lock);
12736
12737	ctl_free_io(io);
12738
12739	return (CTL_RETVAL_COMPLETE);
12740}
12741
12742/*
12743 * Primary command inlet from frontend ports.  All SCSI and task I/O
12744 * requests must go through this function.
12745 */
12746int
12747ctl_queue(union ctl_io *io)
12748{
12749	struct ctl_softc *ctl_softc;
12750
12751	CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
12752
12753	ctl_softc = control_softc;
12754
12755#ifdef CTL_TIME_IO
12756	io->io_hdr.start_time = time_uptime;
12757	getbintime(&io->io_hdr.start_bt);
12758#endif /* CTL_TIME_IO */
12759
12760	mtx_lock(&ctl_softc->ctl_lock);
12761
12762	switch (io->io_hdr.io_type) {
12763	case CTL_IO_SCSI:
12764		STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
12765				   links);
12766		break;
12767	case CTL_IO_TASK:
12768		STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
12769		/*
12770		 * Set the task pending flag.  This is necessary to close a
12771		 * race condition with the FETD:
12772		 *
12773		 * - FETD submits a task management command, like an abort.
12774		 * - Back end calls fe_datamove() to move the data for the
12775		 *   aborted command.  The FETD can't really accept it, but
12776		 *   if it did, it would end up transmitting data for a
12777		 *   command that the initiator told us to abort.
12778		 *
12779		 * We close the race condition by setting the flag here,
12780		 * and checking it in ctl_datamove(), before calling the
12781		 * FETD's fe_datamove routine.  If we've got a task
12782		 * pending, we run the task queue and then check to see
12783		 * whether our particular I/O has been aborted.
12784		 */
12785		ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
12786		break;
12787	default:
12788		mtx_unlock(&ctl_softc->ctl_lock);
12789		printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
12790		return (-EINVAL);
12791		break; /* NOTREACHED */
12792	}
12793	mtx_unlock(&ctl_softc->ctl_lock);
12794
12795	ctl_wakeup_thread();
12796
12797	return (CTL_RETVAL_COMPLETE);
12798}
12799
12800#ifdef CTL_IO_DELAY
12801static void
12802ctl_done_timer_wakeup(void *arg)
12803{
12804	union ctl_io *io;
12805
12806	io = (union ctl_io *)arg;
12807	ctl_done_lock(io, /*have_lock*/ 0);
12808}
12809#endif /* CTL_IO_DELAY */
12810
12811void
12812ctl_done_lock(union ctl_io *io, int have_lock)
12813{
12814	struct ctl_softc *ctl_softc;
12815#ifndef CTL_DONE_THREAD
12816	union ctl_io *xio;
12817#endif /* !CTL_DONE_THREAD */
12818
12819	ctl_softc = control_softc;
12820
12821	if (have_lock == 0)
12822		mtx_lock(&ctl_softc->ctl_lock);
12823
12824	/*
12825	 * Enable this to catch duplicate completion issues.
12826	 */
12827#if 0
12828	if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
12829		printf("%s: type %d msg %d cdb %x iptl: "
12830		       "%d:%d:%d:%d tag 0x%04x "
12831		       "flag %#x status %x\n",
12832			__func__,
12833			io->io_hdr.io_type,
12834			io->io_hdr.msg_type,
12835			io->scsiio.cdb[0],
12836			io->io_hdr.nexus.initid.id,
12837			io->io_hdr.nexus.targ_port,
12838			io->io_hdr.nexus.targ_target.id,
12839			io->io_hdr.nexus.targ_lun,
12840			(io->io_hdr.io_type ==
12841			CTL_IO_TASK) ?
12842			io->taskio.tag_num :
12843			io->scsiio.tag_num,
12844		        io->io_hdr.flags,
12845			io->io_hdr.status);
12846	} else
12847		io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
12848#endif
12849
12850	/*
12851	 * This is an internal copy of an I/O, and should not go through
12852	 * the normal done processing logic.
12853	 */
12854	if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
12855		if (have_lock == 0)
12856			mtx_unlock(&ctl_softc->ctl_lock);
12857		return;
12858	}
12859
12860	/*
12861	 * We need to send a msg to the serializing shelf to finish the IO
12862	 * as well.  We don't send a finish message to the other shelf if
12863	 * this is a task management command.  Task management commands
12864	 * aren't serialized in the OOA queue, but rather just executed on
12865	 * both shelf controllers for commands that originated on that
12866	 * controller.
12867	 */
12868	if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
12869	 && (io->io_hdr.io_type != CTL_IO_TASK)) {
12870		union ctl_ha_msg msg_io;
12871
12872		msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
12873		msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
12874		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
12875		    sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
12876		}
12877		/* continue on to finish IO */
12878	}
12879#ifdef CTL_IO_DELAY
12880	if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12881		struct ctl_lun *lun;
12882
12883		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12884
12885		io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12886	} else {
12887		struct ctl_lun *lun;
12888
12889		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12890
12891		if ((lun != NULL)
12892		 && (lun->delay_info.done_delay > 0)) {
12893			struct callout *callout;
12894
12895			callout = (struct callout *)&io->io_hdr.timer_bytes;
12896			callout_init(callout, /*mpsafe*/ 1);
12897			io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12898			callout_reset(callout,
12899				      lun->delay_info.done_delay * hz,
12900				      ctl_done_timer_wakeup, io);
12901			if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
12902				lun->delay_info.done_delay = 0;
12903			if (have_lock == 0)
12904				mtx_unlock(&ctl_softc->ctl_lock);
12905			return;
12906		}
12907	}
12908#endif /* CTL_IO_DELAY */
12909
12910	STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
12911
12912#ifdef CTL_DONE_THREAD
12913	if (have_lock == 0)
12914		mtx_unlock(&ctl_softc->ctl_lock);
12915
12916	ctl_wakeup_thread();
12917#else /* CTL_DONE_THREAD */
12918	for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
12919	     xio != NULL;
12920	     xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
12921
12922		STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
12923
12924		ctl_process_done(xio, /*have_lock*/ 1);
12925	}
12926	if (have_lock == 0)
12927		mtx_unlock(&ctl_softc->ctl_lock);
12928#endif /* CTL_DONE_THREAD */
12929}
12930
12931void
12932ctl_done(union ctl_io *io)
12933{
12934	ctl_done_lock(io, /*have_lock*/ 0);
12935}
12936
12937int
12938ctl_isc(struct ctl_scsiio *ctsio)
12939{
12940	struct ctl_lun *lun;
12941	int retval;
12942
12943	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12944
12945	CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
12946
12947	CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
12948
12949	retval = lun->backend->data_submit((union ctl_io *)ctsio);
12950
12951	return (retval);
12952}
12953
12954
12955static void
12956ctl_work_thread(void *arg)
12957{
12958	struct ctl_softc *softc;
12959	union ctl_io *io;
12960	struct ctl_be_lun *be_lun;
12961	int retval;
12962
12963	CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
12964
12965	softc = (struct ctl_softc *)arg;
12966	if (softc == NULL)
12967		return;
12968
12969	mtx_lock(&softc->ctl_lock);
12970	for (;;) {
12971		retval = 0;
12972
12973		/*
12974		 * We handle the queues in this order:
12975		 * - task management
12976		 * - ISC
12977		 * - done queue (to free up resources, unblock other commands)
12978		 * - RtR queue
12979		 * - incoming queue
12980		 *
12981		 * If those queues are empty, we break out of the loop and
12982		 * go to sleep.
12983		 */
12984		io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
12985		if (io != NULL) {
12986			ctl_run_task_queue(softc);
12987			continue;
12988		}
12989		io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
12990		if (io != NULL) {
12991			STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
12992			ctl_handle_isc(io);
12993			continue;
12994		}
12995		io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
12996		if (io != NULL) {
12997			STAILQ_REMOVE_HEAD(&softc->done_queue, links);
12998			/* clear any blocked commands, call fe_done */
12999			mtx_unlock(&softc->ctl_lock);
13000			/*
13001			 * XXX KDM
13002			 * Call this without a lock for now.  This will
13003			 * depend on whether there is any way the FETD can
13004			 * sleep or deadlock if called with the CTL lock
13005			 * held.
13006			 */
13007			retval = ctl_process_done(io, /*have_lock*/ 0);
13008			mtx_lock(&softc->ctl_lock);
13009			continue;
13010		}
13011		if (!ctl_pause_rtr) {
13012			io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
13013			if (io != NULL) {
13014				STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
13015				mtx_unlock(&softc->ctl_lock);
13016				retval = ctl_scsiio(&io->scsiio);
13017				if (retval != CTL_RETVAL_COMPLETE)
13018					CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13019				mtx_lock(&softc->ctl_lock);
13020				continue;
13021			}
13022		}
13023		io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
13024		if (io != NULL) {
13025			STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
13026			mtx_unlock(&softc->ctl_lock);
13027			ctl_scsiio_precheck(softc, &io->scsiio);
13028			mtx_lock(&softc->ctl_lock);
13029			continue;
13030		}
13031		/*
13032		 * We might want to move this to a separate thread, so that
13033		 * configuration requests (in this case LUN creations)
13034		 * won't impact the I/O path.
13035		 */
13036		be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13037		if (be_lun != NULL) {
13038			STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13039			mtx_unlock(&softc->ctl_lock);
13040			ctl_create_lun(be_lun);
13041			mtx_lock(&softc->ctl_lock);
13042			continue;
13043		}
13044
13045		/* XXX KDM use the PDROP flag?? */
13046		/* Sleep until we have something to do. */
13047		mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "ctl_work", 0);
13048
13049		/* Back to the top of the loop to see what woke us up. */
13050		continue;
13051	}
13052}
13053
13054void
13055ctl_wakeup_thread()
13056{
13057	struct ctl_softc *softc;
13058
13059	softc = control_softc;
13060
13061	wakeup(softc);
13062}
13063
13064/* Initialization and failover */
13065
13066void
13067ctl_init_isc_msg(void)
13068{
13069	printf("CTL: Still calling this thing\n");
13070}
13071
13072/*
13073 * Init component
13074 * 	Initializes component into configuration defined by bootMode
13075 *	(see hasc-sv.c)
13076 *  	returns hasc_Status:
13077 * 		OK
13078 *		ERROR - fatal error
13079 */
13080static ctl_ha_comp_status
13081ctl_isc_init(struct ctl_ha_component *c)
13082{
13083	ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13084
13085	c->status = ret;
13086	return ret;
13087}
13088
13089/* Start component
13090 * 	Starts component in state requested. If component starts successfully,
13091 *	it must set its own state to the requestrd state
13092 *	When requested state is HASC_STATE_HA, the component may refine it
13093 * 	by adding _SLAVE or _MASTER flags.
13094 *	Currently allowed state transitions are:
13095 *	UNKNOWN->HA		- initial startup
13096 *	UNKNOWN->SINGLE - initial startup when no parter detected
13097 *	HA->SINGLE		- failover
13098 * returns ctl_ha_comp_status:
13099 * 		OK	- component successfully started in requested state
13100 *		FAILED  - could not start the requested state, failover may
13101 * 			  be possible
13102 *		ERROR	- fatal error detected, no future startup possible
13103 */
13104static ctl_ha_comp_status
13105ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13106{
13107	ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13108
13109	printf("%s: go\n", __func__);
13110
13111	// UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13112	if (c->state == CTL_HA_STATE_UNKNOWN ) {
13113		ctl_is_single = 0;
13114		if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13115		    != CTL_HA_STATUS_SUCCESS) {
13116			printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13117			ret = CTL_HA_COMP_STATUS_ERROR;
13118		}
13119	} else if (CTL_HA_STATE_IS_HA(c->state)
13120		&& CTL_HA_STATE_IS_SINGLE(state)){
13121		// HA->SINGLE transition
13122	        ctl_failover();
13123		ctl_is_single = 1;
13124	} else {
13125		printf("ctl_isc_start:Invalid state transition %X->%X\n",
13126		       c->state, state);
13127		ret = CTL_HA_COMP_STATUS_ERROR;
13128	}
13129	if (CTL_HA_STATE_IS_SINGLE(state))
13130		ctl_is_single = 1;
13131
13132	c->state = state;
13133	c->status = ret;
13134	return ret;
13135}
13136
13137/*
13138 * Quiesce component
13139 * The component must clear any error conditions (set status to OK) and
13140 * prepare itself to another Start call
13141 * returns ctl_ha_comp_status:
13142 * 	OK
13143 *	ERROR
13144 */
13145static ctl_ha_comp_status
13146ctl_isc_quiesce(struct ctl_ha_component *c)
13147{
13148	int ret = CTL_HA_COMP_STATUS_OK;
13149
13150	ctl_pause_rtr = 1;
13151	c->status = ret;
13152	return ret;
13153}
13154
13155struct ctl_ha_component ctl_ha_component_ctlisc =
13156{
13157	.name = "CTL ISC",
13158	.state = CTL_HA_STATE_UNKNOWN,
13159	.init = ctl_isc_init,
13160	.start = ctl_isc_start,
13161	.quiesce = ctl_isc_quiesce
13162};
13163
13164/*
13165 *  vim: ts=8
13166 */
13167