]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/ctl/ctl.c
LinuxKPI: Expand ktime functionality.
[FreeBSD/FreeBSD.git] / sys / cam / ctl / ctl.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003-2009 Silicon Graphics International Corp.
5  * Copyright (c) 2012 The FreeBSD Foundation
6  * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
7  * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org>
8  * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org>
9  * All rights reserved.
10  *
11  * Portions of this software were developed by Edward Tomasz Napierala
12  * under sponsorship from the FreeBSD Foundation.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions, and the following disclaimer,
19  *    without modification.
20  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
21  *    substantially similar to the "NO WARRANTY" disclaimer below
22  *    ("Disclaimer") and any redistribution must be conditioned upon
23  *    including a substantially similar Disclaimer requirement for further
24  *    binary redistribution.
25  *
26  * NO WARRANTY
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGES.
38  *
39  * $Id$
40  */
41 /*
42  * CAM Target Layer, a SCSI device emulation subsystem.
43  *
44  * Author: Ken Merry <ken@FreeBSD.org>
45  */
46
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/ctype.h>
53 #include <sys/kernel.h>
54 #include <sys/types.h>
55 #include <sys/kthread.h>
56 #include <sys/bio.h>
57 #include <sys/fcntl.h>
58 #include <sys/lock.h>
59 #include <sys/module.h>
60 #include <sys/mutex.h>
61 #include <sys/condvar.h>
62 #include <sys/malloc.h>
63 #include <sys/conf.h>
64 #include <sys/ioccom.h>
65 #include <sys/queue.h>
66 #include <sys/sbuf.h>
67 #include <sys/smp.h>
68 #include <sys/endian.h>
69 #include <sys/proc.h>
70 #include <sys/sched.h>
71 #include <sys/sysctl.h>
72 #include <sys/nv.h>
73 #include <sys/dnv.h>
74 #include <vm/uma.h>
75
76 #include <cam/cam.h>
77 #include <cam/scsi/scsi_all.h>
78 #include <cam/scsi/scsi_cd.h>
79 #include <cam/scsi/scsi_da.h>
80 #include <cam/ctl/ctl_io.h>
81 #include <cam/ctl/ctl.h>
82 #include <cam/ctl/ctl_frontend.h>
83 #include <cam/ctl/ctl_util.h>
84 #include <cam/ctl/ctl_backend.h>
85 #include <cam/ctl/ctl_ioctl.h>
86 #include <cam/ctl/ctl_ha.h>
87 #include <cam/ctl/ctl_private.h>
88 #include <cam/ctl/ctl_debug.h>
89 #include <cam/ctl/ctl_scsi_all.h>
90 #include <cam/ctl/ctl_error.h>
91
92 struct ctl_softc *control_softc = NULL;
93
94 /*
95  * Template mode pages.
96  */
97
98 /*
99  * Note that these are default values only.  The actual values will be
100  * filled in when the user does a mode sense.
101  */
102 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
103         /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
104         /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
105         /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
106         /*read_retry_count*/0,
107         /*correction_span*/0,
108         /*head_offset_count*/0,
109         /*data_strobe_offset_cnt*/0,
110         /*byte8*/SMS_RWER_LBPERE,
111         /*write_retry_count*/0,
112         /*reserved2*/0,
113         /*recovery_time_limit*/{0, 0},
114 };
115
116 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
117         /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
118         /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
119         /*byte3*/SMS_RWER_PER,
120         /*read_retry_count*/0,
121         /*correction_span*/0,
122         /*head_offset_count*/0,
123         /*data_strobe_offset_cnt*/0,
124         /*byte8*/SMS_RWER_LBPERE,
125         /*write_retry_count*/0,
126         /*reserved2*/0,
127         /*recovery_time_limit*/{0, 0},
128 };
129
130 const static struct scsi_format_page format_page_default = {
131         /*page_code*/SMS_FORMAT_DEVICE_PAGE,
132         /*page_length*/sizeof(struct scsi_format_page) - 2,
133         /*tracks_per_zone*/ {0, 0},
134         /*alt_sectors_per_zone*/ {0, 0},
135         /*alt_tracks_per_zone*/ {0, 0},
136         /*alt_tracks_per_lun*/ {0, 0},
137         /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
138                                 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
139         /*bytes_per_sector*/ {0, 0},
140         /*interleave*/ {0, 0},
141         /*track_skew*/ {0, 0},
142         /*cylinder_skew*/ {0, 0},
143         /*flags*/ SFP_HSEC,
144         /*reserved*/ {0, 0, 0}
145 };
146
147 const static struct scsi_format_page format_page_changeable = {
148         /*page_code*/SMS_FORMAT_DEVICE_PAGE,
149         /*page_length*/sizeof(struct scsi_format_page) - 2,
150         /*tracks_per_zone*/ {0, 0},
151         /*alt_sectors_per_zone*/ {0, 0},
152         /*alt_tracks_per_zone*/ {0, 0},
153         /*alt_tracks_per_lun*/ {0, 0},
154         /*sectors_per_track*/ {0, 0},
155         /*bytes_per_sector*/ {0, 0},
156         /*interleave*/ {0, 0},
157         /*track_skew*/ {0, 0},
158         /*cylinder_skew*/ {0, 0},
159         /*flags*/ 0,
160         /*reserved*/ {0, 0, 0}
161 };
162
163 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
164         /*page_code*/SMS_RIGID_DISK_PAGE,
165         /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
166         /*cylinders*/ {0, 0, 0},
167         /*heads*/ CTL_DEFAULT_HEADS,
168         /*start_write_precomp*/ {0, 0, 0},
169         /*start_reduced_current*/ {0, 0, 0},
170         /*step_rate*/ {0, 0},
171         /*landing_zone_cylinder*/ {0, 0, 0},
172         /*rpl*/ SRDP_RPL_DISABLED,
173         /*rotational_offset*/ 0,
174         /*reserved1*/ 0,
175         /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
176                            CTL_DEFAULT_ROTATION_RATE & 0xff},
177         /*reserved2*/ {0, 0}
178 };
179
180 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
181         /*page_code*/SMS_RIGID_DISK_PAGE,
182         /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
183         /*cylinders*/ {0, 0, 0},
184         /*heads*/ 0,
185         /*start_write_precomp*/ {0, 0, 0},
186         /*start_reduced_current*/ {0, 0, 0},
187         /*step_rate*/ {0, 0},
188         /*landing_zone_cylinder*/ {0, 0, 0},
189         /*rpl*/ 0,
190         /*rotational_offset*/ 0,
191         /*reserved1*/ 0,
192         /*rotation_rate*/ {0, 0},
193         /*reserved2*/ {0, 0}
194 };
195
196 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
197         /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
198         /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
199         /*byte3*/0,
200         /*read_retry_count*/0,
201         /*reserved*/{ 0, 0, 0, 0, 0, 0 },
202         /*recovery_time_limit*/{0, 0},
203 };
204
205 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
206         /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
207         /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
208         /*byte3*/SMS_VER_PER,
209         /*read_retry_count*/0,
210         /*reserved*/{ 0, 0, 0, 0, 0, 0 },
211         /*recovery_time_limit*/{0, 0},
212 };
213
214 const static struct scsi_caching_page caching_page_default = {
215         /*page_code*/SMS_CACHING_PAGE,
216         /*page_length*/sizeof(struct scsi_caching_page) - 2,
217         /*flags1*/ SCP_DISC | SCP_WCE,
218         /*ret_priority*/ 0,
219         /*disable_pf_transfer_len*/ {0xff, 0xff},
220         /*min_prefetch*/ {0, 0},
221         /*max_prefetch*/ {0xff, 0xff},
222         /*max_pf_ceiling*/ {0xff, 0xff},
223         /*flags2*/ 0,
224         /*cache_segments*/ 0,
225         /*cache_seg_size*/ {0, 0},
226         /*reserved*/ 0,
227         /*non_cache_seg_size*/ {0, 0, 0}
228 };
229
230 const static struct scsi_caching_page caching_page_changeable = {
231         /*page_code*/SMS_CACHING_PAGE,
232         /*page_length*/sizeof(struct scsi_caching_page) - 2,
233         /*flags1*/ SCP_WCE | SCP_RCD,
234         /*ret_priority*/ 0,
235         /*disable_pf_transfer_len*/ {0, 0},
236         /*min_prefetch*/ {0, 0},
237         /*max_prefetch*/ {0, 0},
238         /*max_pf_ceiling*/ {0, 0},
239         /*flags2*/ 0,
240         /*cache_segments*/ 0,
241         /*cache_seg_size*/ {0, 0},
242         /*reserved*/ 0,
243         /*non_cache_seg_size*/ {0, 0, 0}
244 };
245
246 const static struct scsi_control_page control_page_default = {
247         /*page_code*/SMS_CONTROL_MODE_PAGE,
248         /*page_length*/sizeof(struct scsi_control_page) - 2,
249         /*rlec*/0,
250         /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
251         /*eca_and_aen*/0,
252         /*flags4*/SCP_TAS,
253         /*aen_holdoff_period*/{0, 0},
254         /*busy_timeout_period*/{0, 0},
255         /*extended_selftest_completion_time*/{0, 0}
256 };
257
258 const static struct scsi_control_page control_page_changeable = {
259         /*page_code*/SMS_CONTROL_MODE_PAGE,
260         /*page_length*/sizeof(struct scsi_control_page) - 2,
261         /*rlec*/SCP_DSENSE,
262         /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
263         /*eca_and_aen*/SCP_SWP,
264         /*flags4*/0,
265         /*aen_holdoff_period*/{0, 0},
266         /*busy_timeout_period*/{0, 0},
267         /*extended_selftest_completion_time*/{0, 0}
268 };
269
270 #define CTL_CEM_LEN     (sizeof(struct scsi_control_ext_page) - 4)
271
272 const static struct scsi_control_ext_page control_ext_page_default = {
273         /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
274         /*subpage_code*/0x01,
275         /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
276         /*flags*/0,
277         /*prio*/0,
278         /*max_sense*/0
279 };
280
281 const static struct scsi_control_ext_page control_ext_page_changeable = {
282         /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
283         /*subpage_code*/0x01,
284         /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
285         /*flags*/0,
286         /*prio*/0,
287         /*max_sense*/0xff
288 };
289
290 const static struct scsi_info_exceptions_page ie_page_default = {
291         /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
292         /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
293         /*info_flags*/SIEP_FLAGS_EWASC,
294         /*mrie*/SIEP_MRIE_NO,
295         /*interval_timer*/{0, 0, 0, 0},
296         /*report_count*/{0, 0, 0, 1}
297 };
298
299 const static struct scsi_info_exceptions_page ie_page_changeable = {
300         /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
301         /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
302         /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
303             SIEP_FLAGS_LOGERR,
304         /*mrie*/0x0f,
305         /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
306         /*report_count*/{0xff, 0xff, 0xff, 0xff}
307 };
308
309 #define CTL_LBPM_LEN    (sizeof(struct ctl_logical_block_provisioning_page) - 4)
310
311 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
312         /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
313         /*subpage_code*/0x02,
314         /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
315         /*flags*/0,
316         /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
317         /*descr*/{}},
318         {{/*flags*/0,
319           /*resource*/0x01,
320           /*reserved*/{0, 0},
321           /*count*/{0, 0, 0, 0}},
322          {/*flags*/0,
323           /*resource*/0x02,
324           /*reserved*/{0, 0},
325           /*count*/{0, 0, 0, 0}},
326          {/*flags*/0,
327           /*resource*/0xf1,
328           /*reserved*/{0, 0},
329           /*count*/{0, 0, 0, 0}},
330          {/*flags*/0,
331           /*resource*/0xf2,
332           /*reserved*/{0, 0},
333           /*count*/{0, 0, 0, 0}}
334         }
335 };
336
337 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
338         /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
339         /*subpage_code*/0x02,
340         /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
341         /*flags*/SLBPP_SITUA,
342         /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
343         /*descr*/{}},
344         {{/*flags*/0,
345           /*resource*/0,
346           /*reserved*/{0, 0},
347           /*count*/{0, 0, 0, 0}},
348          {/*flags*/0,
349           /*resource*/0,
350           /*reserved*/{0, 0},
351           /*count*/{0, 0, 0, 0}},
352          {/*flags*/0,
353           /*resource*/0,
354           /*reserved*/{0, 0},
355           /*count*/{0, 0, 0, 0}},
356          {/*flags*/0,
357           /*resource*/0,
358           /*reserved*/{0, 0},
359           /*count*/{0, 0, 0, 0}}
360         }
361 };
362
363 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
364         /*page_code*/SMS_CDDVD_CAPS_PAGE,
365         /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
366         /*caps1*/0x3f,
367         /*caps2*/0x00,
368         /*caps3*/0xf0,
369         /*caps4*/0x00,
370         /*caps5*/0x29,
371         /*caps6*/0x00,
372         /*obsolete*/{0, 0},
373         /*nvol_levels*/{0, 0},
374         /*buffer_size*/{8, 0},
375         /*obsolete2*/{0, 0},
376         /*reserved*/0,
377         /*digital*/0,
378         /*obsolete3*/0,
379         /*copy_management*/0,
380         /*reserved2*/0,
381         /*rotation_control*/0,
382         /*cur_write_speed*/0,
383         /*num_speed_descr*/0,
384 };
385
386 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
387         /*page_code*/SMS_CDDVD_CAPS_PAGE,
388         /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
389         /*caps1*/0,
390         /*caps2*/0,
391         /*caps3*/0,
392         /*caps4*/0,
393         /*caps5*/0,
394         /*caps6*/0,
395         /*obsolete*/{0, 0},
396         /*nvol_levels*/{0, 0},
397         /*buffer_size*/{0, 0},
398         /*obsolete2*/{0, 0},
399         /*reserved*/0,
400         /*digital*/0,
401         /*obsolete3*/0,
402         /*copy_management*/0,
403         /*reserved2*/0,
404         /*rotation_control*/0,
405         /*cur_write_speed*/0,
406         /*num_speed_descr*/0,
407 };
408
409 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
410 static int worker_threads = -1;
411 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
412     &worker_threads, 1, "Number of worker threads");
413 static int ctl_debug = CTL_DEBUG_NONE;
414 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
415     &ctl_debug, 0, "Enabled debug flags");
416 static int ctl_lun_map_size = 1024;
417 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
418     &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
419 #ifdef  CTL_TIME_IO
420 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
421 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
422     &ctl_time_io_secs, 0, "Log requests taking more seconds");
423 #endif
424
425 /*
426  * Maximum number of LUNs we support.  MUST be a power of 2.
427  */
428 #define CTL_DEFAULT_MAX_LUNS    1024
429 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
430 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
431 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
432     &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
433
434 /*
435  * Maximum number of ports registered at one time.
436  */
437 #define CTL_DEFAULT_MAX_PORTS           256
438 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
439 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
440 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
441     &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
442
443 /*
444  * Maximum number of initiators we support.
445  */
446 #define CTL_MAX_INITIATORS      (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
447
448 /*
449  * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
450  * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
451  * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
452  * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
453  */
454 #define SCSI_EVPD_NUM_SUPPORTED_PAGES   10
455
456 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
457                                   int param);
458 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
459 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
460 static int ctl_init(void);
461 static int ctl_shutdown(void);
462 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
463 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
464 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
465 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
466                               struct ctl_ooa *ooa_hdr,
467                               struct ctl_ooa_entry *kern_entries);
468 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
469                      struct thread *td);
470 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
471                          struct ctl_be_lun *be_lun);
472 static int ctl_free_lun(struct ctl_lun *lun);
473 static void ctl_create_lun(struct ctl_be_lun *be_lun);
474
475 static int ctl_do_mode_select(union ctl_io *io);
476 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
477                            uint64_t res_key, uint64_t sa_res_key,
478                            uint8_t type, uint32_t residx,
479                            struct ctl_scsiio *ctsio,
480                            struct scsi_per_res_out *cdb,
481                            struct scsi_per_res_out_parms* param);
482 static void ctl_pro_preempt_other(struct ctl_lun *lun,
483                                   union ctl_ha_msg *msg);
484 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
485 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
486 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
487 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
489 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
490 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
491                                          int alloc_len);
492 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
493                                          int alloc_len);
494 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
495 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
496 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
497 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
498 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
499 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
500     bool seq);
501 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
502 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
503     union ctl_io *pending_io, union ctl_io *ooa_io);
504 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
505                                 union ctl_io **starting_io);
506 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io,
507     bool skip);
508 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io,
509     bool skip);
510 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
511                                 const struct ctl_cmd_entry *entry,
512                                 struct ctl_scsiio *ctsio);
513 static void ctl_failover_lun(union ctl_io *io);
514 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
515                                struct ctl_scsiio *ctsio);
516 static int ctl_scsiio(struct ctl_scsiio *ctsio);
517
518 static int ctl_target_reset(union ctl_io *io);
519 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
520                          ctl_ua_type ua_type);
521 static int ctl_lun_reset(union ctl_io *io);
522 static int ctl_abort_task(union ctl_io *io);
523 static int ctl_abort_task_set(union ctl_io *io);
524 static int ctl_query_task(union ctl_io *io, int task_set);
525 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
526                               ctl_ua_type ua_type);
527 static int ctl_i_t_nexus_reset(union ctl_io *io);
528 static int ctl_query_async_event(union ctl_io *io);
529 static void ctl_run_task(union ctl_io *io);
530 #ifdef CTL_IO_DELAY
531 static void ctl_datamove_timer_wakeup(void *arg);
532 static void ctl_done_timer_wakeup(void *arg);
533 #endif /* CTL_IO_DELAY */
534
535 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
536 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
537 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
538 static void ctl_datamove_remote_write(union ctl_io *io);
539 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
540 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
541 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
542 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
543                                     ctl_ha_dt_cb callback);
544 static void ctl_datamove_remote_read(union ctl_io *io);
545 static void ctl_datamove_remote(union ctl_io *io);
546 static void ctl_process_done(union ctl_io *io);
547 static void ctl_lun_thread(void *arg);
548 static void ctl_thresh_thread(void *arg);
549 static void ctl_work_thread(void *arg);
550 static void ctl_enqueue_incoming(union ctl_io *io);
551 static void ctl_enqueue_rtr(union ctl_io *io);
552 static void ctl_enqueue_done(union ctl_io *io);
553 static void ctl_enqueue_isc(union ctl_io *io);
554 static const struct ctl_cmd_entry *
555     ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
556 static const struct ctl_cmd_entry *
557     ctl_validate_command(struct ctl_scsiio *ctsio);
558 static int ctl_cmd_applicable(uint8_t lun_type,
559     const struct ctl_cmd_entry *entry);
560 static int ctl_ha_init(void);
561 static int ctl_ha_shutdown(void);
562
563 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
564 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
565 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
566 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
567
568 /*
569  * Load the serialization table.  This isn't very pretty, but is probably
570  * the easiest way to do it.
571  */
572 #include "ctl_ser_table.c"
573
574 /*
575  * We only need to define open, close and ioctl routines for this driver.
576  */
577 static struct cdevsw ctl_cdevsw = {
578         .d_version =    D_VERSION,
579         .d_flags =      0,
580         .d_open =       ctl_open,
581         .d_close =      ctl_close,
582         .d_ioctl =      ctl_ioctl,
583         .d_name =       "ctl",
584 };
585
586
587 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
588
589 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
590
591 static moduledata_t ctl_moduledata = {
592         "ctl",
593         ctl_module_event_handler,
594         NULL
595 };
596
597 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
598 MODULE_VERSION(ctl, 1);
599
600 static struct ctl_frontend ha_frontend =
601 {
602         .name = "ha",
603         .init = ctl_ha_init,
604         .shutdown = ctl_ha_shutdown,
605 };
606
607 static int
608 ctl_ha_init(void)
609 {
610         struct ctl_softc *softc = control_softc;
611
612         if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
613                             &softc->othersc_pool) != 0)
614                 return (ENOMEM);
615         if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
616                 ctl_pool_free(softc->othersc_pool);
617                 return (EIO);
618         }
619         if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
620             != CTL_HA_STATUS_SUCCESS) {
621                 ctl_ha_msg_destroy(softc);
622                 ctl_pool_free(softc->othersc_pool);
623                 return (EIO);
624         }
625         return (0);
626 };
627
628 static int
629 ctl_ha_shutdown(void)
630 {
631         struct ctl_softc *softc = control_softc;
632         struct ctl_port *port;
633
634         ctl_ha_msg_shutdown(softc);
635         if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
636                 return (EIO);
637         if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
638                 return (EIO);
639         ctl_pool_free(softc->othersc_pool);
640         while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
641                 ctl_port_deregister(port);
642                 free(port->port_name, M_CTL);
643                 free(port, M_CTL);
644         }
645         return (0);
646 };
647
648 static void
649 ctl_ha_datamove(union ctl_io *io)
650 {
651         struct ctl_lun *lun = CTL_LUN(io);
652         struct ctl_sg_entry *sgl;
653         union ctl_ha_msg msg;
654         uint32_t sg_entries_sent;
655         int do_sg_copy, i, j;
656
657         memset(&msg.dt, 0, sizeof(msg.dt));
658         msg.hdr.msg_type = CTL_MSG_DATAMOVE;
659         msg.hdr.original_sc = io->io_hdr.remote_io;
660         msg.hdr.serializing_sc = io;
661         msg.hdr.nexus = io->io_hdr.nexus;
662         msg.hdr.status = io->io_hdr.status;
663         msg.dt.flags = io->io_hdr.flags;
664
665         /*
666          * We convert everything into a S/G list here.  We can't
667          * pass by reference, only by value between controllers.
668          * So we can't pass a pointer to the S/G list, only as many
669          * S/G entries as we can fit in here.  If it's possible for
670          * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
671          * then we need to break this up into multiple transfers.
672          */
673         if (io->scsiio.kern_sg_entries == 0) {
674                 msg.dt.kern_sg_entries = 1;
675 #if 0
676                 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
677                         msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
678                 } else {
679                         /* XXX KDM use busdma here! */
680                         msg.dt.sg_list[0].addr =
681                             (void *)vtophys(io->scsiio.kern_data_ptr);
682                 }
683 #else
684                 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
685                     ("HA does not support BUS_ADDR"));
686                 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
687 #endif
688                 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
689                 do_sg_copy = 0;
690         } else {
691                 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
692                 do_sg_copy = 1;
693         }
694
695         msg.dt.kern_data_len = io->scsiio.kern_data_len;
696         msg.dt.kern_total_len = io->scsiio.kern_total_len;
697         msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
698         msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
699         msg.dt.sg_sequence = 0;
700
701         /*
702          * Loop until we've sent all of the S/G entries.  On the
703          * other end, we'll recompose these S/G entries into one
704          * contiguous list before processing.
705          */
706         for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
707             msg.dt.sg_sequence++) {
708                 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
709                     sizeof(msg.dt.sg_list[0])),
710                     msg.dt.kern_sg_entries - sg_entries_sent);
711                 if (do_sg_copy != 0) {
712                         sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
713                         for (i = sg_entries_sent, j = 0;
714                              i < msg.dt.cur_sg_entries; i++, j++) {
715 #if 0
716                                 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
717                                         msg.dt.sg_list[j].addr = sgl[i].addr;
718                                 } else {
719                                         /* XXX KDM use busdma here! */
720                                         msg.dt.sg_list[j].addr =
721                                             (void *)vtophys(sgl[i].addr);
722                                 }
723 #else
724                                 KASSERT((io->io_hdr.flags &
725                                     CTL_FLAG_BUS_ADDR) == 0,
726                                     ("HA does not support BUS_ADDR"));
727                                 msg.dt.sg_list[j].addr = sgl[i].addr;
728 #endif
729                                 msg.dt.sg_list[j].len = sgl[i].len;
730                         }
731                 }
732
733                 sg_entries_sent += msg.dt.cur_sg_entries;
734                 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
735                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
736                     sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
737                     sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
738                     M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
739                         io->io_hdr.port_status = 31341;
740                         io->scsiio.be_move_done(io);
741                         return;
742                 }
743                 msg.dt.sent_sg_entries = sg_entries_sent;
744         }
745
746         /*
747          * Officially handover the request from us to peer.
748          * If failover has just happened, then we must return error.
749          * If failover happen just after, then it is not our problem.
750          */
751         if (lun)
752                 mtx_lock(&lun->lun_lock);
753         if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
754                 if (lun)
755                         mtx_unlock(&lun->lun_lock);
756                 io->io_hdr.port_status = 31342;
757                 io->scsiio.be_move_done(io);
758                 return;
759         }
760         io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
761         io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
762         if (lun)
763                 mtx_unlock(&lun->lun_lock);
764 }
765
766 static void
767 ctl_ha_done(union ctl_io *io)
768 {
769         union ctl_ha_msg msg;
770
771         if (io->io_hdr.io_type == CTL_IO_SCSI) {
772                 memset(&msg, 0, sizeof(msg));
773                 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
774                 msg.hdr.original_sc = io->io_hdr.remote_io;
775                 msg.hdr.nexus = io->io_hdr.nexus;
776                 msg.hdr.status = io->io_hdr.status;
777                 msg.scsi.scsi_status = io->scsiio.scsi_status;
778                 msg.scsi.tag_num = io->scsiio.tag_num;
779                 msg.scsi.tag_type = io->scsiio.tag_type;
780                 msg.scsi.sense_len = io->scsiio.sense_len;
781                 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
782                     io->scsiio.sense_len);
783                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
784                     sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
785                     msg.scsi.sense_len, M_WAITOK);
786         }
787         ctl_free_io(io);
788 }
789
790 static void
791 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
792                             union ctl_ha_msg *msg_info)
793 {
794         struct ctl_scsiio *ctsio;
795
796         if (msg_info->hdr.original_sc == NULL) {
797                 printf("%s: original_sc == NULL!\n", __func__);
798                 /* XXX KDM now what? */
799                 return;
800         }
801
802         ctsio = &msg_info->hdr.original_sc->scsiio;
803         ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
804         ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
805         ctsio->io_hdr.status = msg_info->hdr.status;
806         ctsio->scsi_status = msg_info->scsi.scsi_status;
807         ctsio->sense_len = msg_info->scsi.sense_len;
808         memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
809                msg_info->scsi.sense_len);
810         ctl_enqueue_isc((union ctl_io *)ctsio);
811 }
812
813 static void
814 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
815                                 union ctl_ha_msg *msg_info)
816 {
817         struct ctl_scsiio *ctsio;
818
819         if (msg_info->hdr.serializing_sc == NULL) {
820                 printf("%s: serializing_sc == NULL!\n", __func__);
821                 /* XXX KDM now what? */
822                 return;
823         }
824
825         ctsio = &msg_info->hdr.serializing_sc->scsiio;
826         ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
827         ctl_enqueue_isc((union ctl_io *)ctsio);
828 }
829
830 void
831 ctl_isc_announce_lun(struct ctl_lun *lun)
832 {
833         struct ctl_softc *softc = lun->ctl_softc;
834         union ctl_ha_msg *msg;
835         struct ctl_ha_msg_lun_pr_key pr_key;
836         int i, k;
837
838         if (softc->ha_link != CTL_HA_LINK_ONLINE)
839                 return;
840         mtx_lock(&lun->lun_lock);
841         i = sizeof(msg->lun);
842         if (lun->lun_devid)
843                 i += lun->lun_devid->len;
844         i += sizeof(pr_key) * lun->pr_key_count;
845 alloc:
846         mtx_unlock(&lun->lun_lock);
847         msg = malloc(i, M_CTL, M_WAITOK);
848         mtx_lock(&lun->lun_lock);
849         k = sizeof(msg->lun);
850         if (lun->lun_devid)
851                 k += lun->lun_devid->len;
852         k += sizeof(pr_key) * lun->pr_key_count;
853         if (i < k) {
854                 free(msg, M_CTL);
855                 i = k;
856                 goto alloc;
857         }
858         bzero(&msg->lun, sizeof(msg->lun));
859         msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
860         msg->hdr.nexus.targ_lun = lun->lun;
861         msg->hdr.nexus.targ_mapped_lun = lun->lun;
862         msg->lun.flags = lun->flags;
863         msg->lun.pr_generation = lun->pr_generation;
864         msg->lun.pr_res_idx = lun->pr_res_idx;
865         msg->lun.pr_res_type = lun->pr_res_type;
866         msg->lun.pr_key_count = lun->pr_key_count;
867         i = 0;
868         if (lun->lun_devid) {
869                 msg->lun.lun_devid_len = lun->lun_devid->len;
870                 memcpy(&msg->lun.data[i], lun->lun_devid->data,
871                     msg->lun.lun_devid_len);
872                 i += msg->lun.lun_devid_len;
873         }
874         for (k = 0; k < CTL_MAX_INITIATORS; k++) {
875                 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
876                         continue;
877                 pr_key.pr_iid = k;
878                 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
879                 i += sizeof(pr_key);
880         }
881         mtx_unlock(&lun->lun_lock);
882         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
883             M_WAITOK);
884         free(msg, M_CTL);
885
886         if (lun->flags & CTL_LUN_PRIMARY_SC) {
887                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
888                         ctl_isc_announce_mode(lun, -1,
889                             lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
890                             lun->mode_pages.index[i].subpage);
891                 }
892         }
893 }
894
895 void
896 ctl_isc_announce_port(struct ctl_port *port)
897 {
898         struct ctl_softc *softc = port->ctl_softc;
899         union ctl_ha_msg *msg;
900         int i;
901
902         if (port->targ_port < softc->port_min ||
903             port->targ_port >= softc->port_max ||
904             softc->ha_link != CTL_HA_LINK_ONLINE)
905                 return;
906         i = sizeof(msg->port) + strlen(port->port_name) + 1;
907         if (port->lun_map)
908                 i += port->lun_map_size * sizeof(uint32_t);
909         if (port->port_devid)
910                 i += port->port_devid->len;
911         if (port->target_devid)
912                 i += port->target_devid->len;
913         if (port->init_devid)
914                 i += port->init_devid->len;
915         msg = malloc(i, M_CTL, M_WAITOK);
916         bzero(&msg->port, sizeof(msg->port));
917         msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
918         msg->hdr.nexus.targ_port = port->targ_port;
919         msg->port.port_type = port->port_type;
920         msg->port.physical_port = port->physical_port;
921         msg->port.virtual_port = port->virtual_port;
922         msg->port.status = port->status;
923         i = 0;
924         msg->port.name_len = sprintf(&msg->port.data[i],
925             "%d:%s", softc->ha_id, port->port_name) + 1;
926         i += msg->port.name_len;
927         if (port->lun_map) {
928                 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
929                 memcpy(&msg->port.data[i], port->lun_map,
930                     msg->port.lun_map_len);
931                 i += msg->port.lun_map_len;
932         }
933         if (port->port_devid) {
934                 msg->port.port_devid_len = port->port_devid->len;
935                 memcpy(&msg->port.data[i], port->port_devid->data,
936                     msg->port.port_devid_len);
937                 i += msg->port.port_devid_len;
938         }
939         if (port->target_devid) {
940                 msg->port.target_devid_len = port->target_devid->len;
941                 memcpy(&msg->port.data[i], port->target_devid->data,
942                     msg->port.target_devid_len);
943                 i += msg->port.target_devid_len;
944         }
945         if (port->init_devid) {
946                 msg->port.init_devid_len = port->init_devid->len;
947                 memcpy(&msg->port.data[i], port->init_devid->data,
948                     msg->port.init_devid_len);
949                 i += msg->port.init_devid_len;
950         }
951         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
952             M_WAITOK);
953         free(msg, M_CTL);
954 }
955
956 void
957 ctl_isc_announce_iid(struct ctl_port *port, int iid)
958 {
959         struct ctl_softc *softc = port->ctl_softc;
960         union ctl_ha_msg *msg;
961         int i, l;
962
963         if (port->targ_port < softc->port_min ||
964             port->targ_port >= softc->port_max ||
965             softc->ha_link != CTL_HA_LINK_ONLINE)
966                 return;
967         mtx_lock(&softc->ctl_lock);
968         i = sizeof(msg->iid);
969         l = 0;
970         if (port->wwpn_iid[iid].name)
971                 l = strlen(port->wwpn_iid[iid].name) + 1;
972         i += l;
973         msg = malloc(i, M_CTL, M_NOWAIT);
974         if (msg == NULL) {
975                 mtx_unlock(&softc->ctl_lock);
976                 return;
977         }
978         bzero(&msg->iid, sizeof(msg->iid));
979         msg->hdr.msg_type = CTL_MSG_IID_SYNC;
980         msg->hdr.nexus.targ_port = port->targ_port;
981         msg->hdr.nexus.initid = iid;
982         msg->iid.in_use = port->wwpn_iid[iid].in_use;
983         msg->iid.name_len = l;
984         msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
985         if (port->wwpn_iid[iid].name)
986                 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
987         mtx_unlock(&softc->ctl_lock);
988         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
989         free(msg, M_CTL);
990 }
991
992 void
993 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
994     uint8_t page, uint8_t subpage)
995 {
996         struct ctl_softc *softc = lun->ctl_softc;
997         union ctl_ha_msg msg;
998         u_int i;
999
1000         if (softc->ha_link != CTL_HA_LINK_ONLINE)
1001                 return;
1002         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1003                 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1004                     page && lun->mode_pages.index[i].subpage == subpage)
1005                         break;
1006         }
1007         if (i == CTL_NUM_MODE_PAGES)
1008                 return;
1009
1010         /* Don't try to replicate pages not present on this device. */
1011         if (lun->mode_pages.index[i].page_data == NULL)
1012                 return;
1013
1014         bzero(&msg.mode, sizeof(msg.mode));
1015         msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1016         msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1017         msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1018         msg.hdr.nexus.targ_lun = lun->lun;
1019         msg.hdr.nexus.targ_mapped_lun = lun->lun;
1020         msg.mode.page_code = page;
1021         msg.mode.subpage = subpage;
1022         msg.mode.page_len = lun->mode_pages.index[i].page_len;
1023         memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1024             msg.mode.page_len);
1025         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1026             M_WAITOK);
1027 }
1028
1029 static void
1030 ctl_isc_ha_link_up(struct ctl_softc *softc)
1031 {
1032         struct ctl_port *port;
1033         struct ctl_lun *lun;
1034         union ctl_ha_msg msg;
1035         int i;
1036
1037         /* Announce this node parameters to peer for validation. */
1038         msg.login.msg_type = CTL_MSG_LOGIN;
1039         msg.login.version = CTL_HA_VERSION;
1040         msg.login.ha_mode = softc->ha_mode;
1041         msg.login.ha_id = softc->ha_id;
1042         msg.login.max_luns = ctl_max_luns;
1043         msg.login.max_ports = ctl_max_ports;
1044         msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1045         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1046             M_WAITOK);
1047
1048         STAILQ_FOREACH(port, &softc->port_list, links) {
1049                 ctl_isc_announce_port(port);
1050                 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1051                         if (port->wwpn_iid[i].in_use)
1052                                 ctl_isc_announce_iid(port, i);
1053                 }
1054         }
1055         STAILQ_FOREACH(lun, &softc->lun_list, links)
1056                 ctl_isc_announce_lun(lun);
1057 }
1058
1059 static void
1060 ctl_isc_ha_link_down(struct ctl_softc *softc)
1061 {
1062         struct ctl_port *port;
1063         struct ctl_lun *lun;
1064         union ctl_io *io;
1065         int i;
1066
1067         mtx_lock(&softc->ctl_lock);
1068         STAILQ_FOREACH(lun, &softc->lun_list, links) {
1069                 mtx_lock(&lun->lun_lock);
1070                 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1071                         lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1072                         ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1073                 }
1074                 mtx_unlock(&lun->lun_lock);
1075
1076                 mtx_unlock(&softc->ctl_lock);
1077                 io = ctl_alloc_io(softc->othersc_pool);
1078                 mtx_lock(&softc->ctl_lock);
1079                 ctl_zero_io(io);
1080                 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1081                 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1082                 ctl_enqueue_isc(io);
1083         }
1084
1085         STAILQ_FOREACH(port, &softc->port_list, links) {
1086                 if (port->targ_port >= softc->port_min &&
1087                     port->targ_port < softc->port_max)
1088                         continue;
1089                 port->status &= ~CTL_PORT_STATUS_ONLINE;
1090                 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1091                         port->wwpn_iid[i].in_use = 0;
1092                         free(port->wwpn_iid[i].name, M_CTL);
1093                         port->wwpn_iid[i].name = NULL;
1094                 }
1095         }
1096         mtx_unlock(&softc->ctl_lock);
1097 }
1098
1099 static void
1100 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1101 {
1102         struct ctl_lun *lun;
1103         uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1104
1105         mtx_lock(&softc->ctl_lock);
1106         if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1107             (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1108                 mtx_unlock(&softc->ctl_lock);
1109                 return;
1110         }
1111         mtx_lock(&lun->lun_lock);
1112         mtx_unlock(&softc->ctl_lock);
1113         if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1114                 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1115         if (msg->ua.ua_all) {
1116                 if (msg->ua.ua_set)
1117                         ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1118                 else
1119                         ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1120         } else {
1121                 if (msg->ua.ua_set)
1122                         ctl_est_ua(lun, iid, msg->ua.ua_type);
1123                 else
1124                         ctl_clr_ua(lun, iid, msg->ua.ua_type);
1125         }
1126         mtx_unlock(&lun->lun_lock);
1127 }
1128
1129 static void
1130 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1131 {
1132         struct ctl_lun *lun;
1133         struct ctl_ha_msg_lun_pr_key pr_key;
1134         int i, k;
1135         ctl_lun_flags oflags;
1136         uint32_t targ_lun;
1137
1138         targ_lun = msg->hdr.nexus.targ_mapped_lun;
1139         mtx_lock(&softc->ctl_lock);
1140         if (targ_lun >= ctl_max_luns ||
1141             (lun = softc->ctl_luns[targ_lun]) == NULL) {
1142                 mtx_unlock(&softc->ctl_lock);
1143                 return;
1144         }
1145         mtx_lock(&lun->lun_lock);
1146         mtx_unlock(&softc->ctl_lock);
1147         if (lun->flags & CTL_LUN_DISABLED) {
1148                 mtx_unlock(&lun->lun_lock);
1149                 return;
1150         }
1151         i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1152         if (msg->lun.lun_devid_len != i || (i > 0 &&
1153             memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1154                 mtx_unlock(&lun->lun_lock);
1155                 printf("%s: Received conflicting HA LUN %d\n",
1156                     __func__, targ_lun);
1157                 return;
1158         } else {
1159                 /* Record whether peer is primary. */
1160                 oflags = lun->flags;
1161                 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1162                     (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1163                         lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1164                 else
1165                         lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1166                 if (oflags != lun->flags)
1167                         ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1168
1169                 /* If peer is primary and we are not -- use data */
1170                 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1171                     (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1172                         lun->pr_generation = msg->lun.pr_generation;
1173                         lun->pr_res_idx = msg->lun.pr_res_idx;
1174                         lun->pr_res_type = msg->lun.pr_res_type;
1175                         lun->pr_key_count = msg->lun.pr_key_count;
1176                         for (k = 0; k < CTL_MAX_INITIATORS; k++)
1177                                 ctl_clr_prkey(lun, k);
1178                         for (k = 0; k < msg->lun.pr_key_count; k++) {
1179                                 memcpy(&pr_key, &msg->lun.data[i],
1180                                     sizeof(pr_key));
1181                                 ctl_alloc_prkey(lun, pr_key.pr_iid);
1182                                 ctl_set_prkey(lun, pr_key.pr_iid,
1183                                     pr_key.pr_key);
1184                                 i += sizeof(pr_key);
1185                         }
1186                 }
1187
1188                 mtx_unlock(&lun->lun_lock);
1189                 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1190                     __func__, targ_lun,
1191                     (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1192                     "primary" : "secondary"));
1193
1194                 /* If we are primary but peer doesn't know -- notify */
1195                 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1196                     (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1197                         ctl_isc_announce_lun(lun);
1198         }
1199 }
1200
1201 static void
1202 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1203 {
1204         struct ctl_port *port;
1205         struct ctl_lun *lun;
1206         int i, new;
1207
1208         port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1209         if (port == NULL) {
1210                 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1211                     msg->hdr.nexus.targ_port));
1212                 new = 1;
1213                 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1214                 port->frontend = &ha_frontend;
1215                 port->targ_port = msg->hdr.nexus.targ_port;
1216                 port->fe_datamove = ctl_ha_datamove;
1217                 port->fe_done = ctl_ha_done;
1218         } else if (port->frontend == &ha_frontend) {
1219                 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1220                     msg->hdr.nexus.targ_port));
1221                 new = 0;
1222         } else {
1223                 printf("%s: Received conflicting HA port %d\n",
1224                     __func__, msg->hdr.nexus.targ_port);
1225                 return;
1226         }
1227         port->port_type = msg->port.port_type;
1228         port->physical_port = msg->port.physical_port;
1229         port->virtual_port = msg->port.virtual_port;
1230         port->status = msg->port.status;
1231         i = 0;
1232         free(port->port_name, M_CTL);
1233         port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1234             M_CTL);
1235         i += msg->port.name_len;
1236         if (msg->port.lun_map_len != 0) {
1237                 if (port->lun_map == NULL ||
1238                     port->lun_map_size * sizeof(uint32_t) <
1239                     msg->port.lun_map_len) {
1240                         port->lun_map_size = 0;
1241                         free(port->lun_map, M_CTL);
1242                         port->lun_map = malloc(msg->port.lun_map_len,
1243                             M_CTL, M_WAITOK);
1244                 }
1245                 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1246                 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1247                 i += msg->port.lun_map_len;
1248         } else {
1249                 port->lun_map_size = 0;
1250                 free(port->lun_map, M_CTL);
1251                 port->lun_map = NULL;
1252         }
1253         if (msg->port.port_devid_len != 0) {
1254                 if (port->port_devid == NULL ||
1255                     port->port_devid->len < msg->port.port_devid_len) {
1256                         free(port->port_devid, M_CTL);
1257                         port->port_devid = malloc(sizeof(struct ctl_devid) +
1258                             msg->port.port_devid_len, M_CTL, M_WAITOK);
1259                 }
1260                 memcpy(port->port_devid->data, &msg->port.data[i],
1261                     msg->port.port_devid_len);
1262                 port->port_devid->len = msg->port.port_devid_len;
1263                 i += msg->port.port_devid_len;
1264         } else {
1265                 free(port->port_devid, M_CTL);
1266                 port->port_devid = NULL;
1267         }
1268         if (msg->port.target_devid_len != 0) {
1269                 if (port->target_devid == NULL ||
1270                     port->target_devid->len < msg->port.target_devid_len) {
1271                         free(port->target_devid, M_CTL);
1272                         port->target_devid = malloc(sizeof(struct ctl_devid) +
1273                             msg->port.target_devid_len, M_CTL, M_WAITOK);
1274                 }
1275                 memcpy(port->target_devid->data, &msg->port.data[i],
1276                     msg->port.target_devid_len);
1277                 port->target_devid->len = msg->port.target_devid_len;
1278                 i += msg->port.target_devid_len;
1279         } else {
1280                 free(port->target_devid, M_CTL);
1281                 port->target_devid = NULL;
1282         }
1283         if (msg->port.init_devid_len != 0) {
1284                 if (port->init_devid == NULL ||
1285                     port->init_devid->len < msg->port.init_devid_len) {
1286                         free(port->init_devid, M_CTL);
1287                         port->init_devid = malloc(sizeof(struct ctl_devid) +
1288                             msg->port.init_devid_len, M_CTL, M_WAITOK);
1289                 }
1290                 memcpy(port->init_devid->data, &msg->port.data[i],
1291                     msg->port.init_devid_len);
1292                 port->init_devid->len = msg->port.init_devid_len;
1293                 i += msg->port.init_devid_len;
1294         } else {
1295                 free(port->init_devid, M_CTL);
1296                 port->init_devid = NULL;
1297         }
1298         if (new) {
1299                 if (ctl_port_register(port) != 0) {
1300                         printf("%s: ctl_port_register() failed with error\n",
1301                             __func__);
1302                 }
1303         }
1304         mtx_lock(&softc->ctl_lock);
1305         STAILQ_FOREACH(lun, &softc->lun_list, links) {
1306                 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1307                         continue;
1308                 mtx_lock(&lun->lun_lock);
1309                 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1310                 mtx_unlock(&lun->lun_lock);
1311         }
1312         mtx_unlock(&softc->ctl_lock);
1313 }
1314
1315 static void
1316 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1317 {
1318         struct ctl_port *port;
1319         int iid;
1320
1321         port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1322         if (port == NULL) {
1323                 printf("%s: Received IID for unknown port %d\n",
1324                     __func__, msg->hdr.nexus.targ_port);
1325                 return;
1326         }
1327         iid = msg->hdr.nexus.initid;
1328         if (port->wwpn_iid[iid].in_use != 0 &&
1329             msg->iid.in_use == 0)
1330                 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1331         port->wwpn_iid[iid].in_use = msg->iid.in_use;
1332         port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1333         free(port->wwpn_iid[iid].name, M_CTL);
1334         if (msg->iid.name_len) {
1335                 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1336                     msg->iid.name_len, M_CTL);
1337         } else
1338                 port->wwpn_iid[iid].name = NULL;
1339 }
1340
1341 static void
1342 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1343 {
1344
1345         if (msg->login.version != CTL_HA_VERSION) {
1346                 printf("CTL HA peers have different versions %d != %d\n",
1347                     msg->login.version, CTL_HA_VERSION);
1348                 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1349                 return;
1350         }
1351         if (msg->login.ha_mode != softc->ha_mode) {
1352                 printf("CTL HA peers have different ha_mode %d != %d\n",
1353                     msg->login.ha_mode, softc->ha_mode);
1354                 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1355                 return;
1356         }
1357         if (msg->login.ha_id == softc->ha_id) {
1358                 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1359                 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1360                 return;
1361         }
1362         if (msg->login.max_luns != ctl_max_luns ||
1363             msg->login.max_ports != ctl_max_ports ||
1364             msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1365                 printf("CTL HA peers have different limits\n");
1366                 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1367                 return;
1368         }
1369 }
1370
1371 static void
1372 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1373 {
1374         struct ctl_lun *lun;
1375         u_int i;
1376         uint32_t initidx, targ_lun;
1377
1378         targ_lun = msg->hdr.nexus.targ_mapped_lun;
1379         mtx_lock(&softc->ctl_lock);
1380         if (targ_lun >= ctl_max_luns ||
1381             (lun = softc->ctl_luns[targ_lun]) == NULL) {
1382                 mtx_unlock(&softc->ctl_lock);
1383                 return;
1384         }
1385         mtx_lock(&lun->lun_lock);
1386         mtx_unlock(&softc->ctl_lock);
1387         if (lun->flags & CTL_LUN_DISABLED) {
1388                 mtx_unlock(&lun->lun_lock);
1389                 return;
1390         }
1391         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1392                 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1393                     msg->mode.page_code &&
1394                     lun->mode_pages.index[i].subpage == msg->mode.subpage)
1395                         break;
1396         }
1397         if (i == CTL_NUM_MODE_PAGES) {
1398                 mtx_unlock(&lun->lun_lock);
1399                 return;
1400         }
1401         memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1402             lun->mode_pages.index[i].page_len);
1403         initidx = ctl_get_initindex(&msg->hdr.nexus);
1404         if (initidx != -1)
1405                 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1406         mtx_unlock(&lun->lun_lock);
1407 }
1408
1409 /*
1410  * ISC (Inter Shelf Communication) event handler.  Events from the HA
1411  * subsystem come in here.
1412  */
1413 static void
1414 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1415 {
1416         struct ctl_softc *softc = control_softc;
1417         union ctl_io *io;
1418         struct ctl_prio *presio;
1419         ctl_ha_status isc_status;
1420
1421         CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1422         if (event == CTL_HA_EVT_MSG_RECV) {
1423                 union ctl_ha_msg *msg, msgbuf;
1424
1425                 if (param > sizeof(msgbuf))
1426                         msg = malloc(param, M_CTL, M_WAITOK);
1427                 else
1428                         msg = &msgbuf;
1429                 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1430                     M_WAITOK);
1431                 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1432                         printf("%s: Error receiving message: %d\n",
1433                             __func__, isc_status);
1434                         if (msg != &msgbuf)
1435                                 free(msg, M_CTL);
1436                         return;
1437                 }
1438
1439                 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1440                 switch (msg->hdr.msg_type) {
1441                 case CTL_MSG_SERIALIZE:
1442                         io = ctl_alloc_io(softc->othersc_pool);
1443                         ctl_zero_io(io);
1444                         // populate ctsio from msg
1445                         io->io_hdr.io_type = CTL_IO_SCSI;
1446                         io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1447                         io->io_hdr.remote_io = msg->hdr.original_sc;
1448                         io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1449                                             CTL_FLAG_IO_ACTIVE;
1450                         /*
1451                          * If we're in serialization-only mode, we don't
1452                          * want to go through full done processing.  Thus
1453                          * the COPY flag.
1454                          *
1455                          * XXX KDM add another flag that is more specific.
1456                          */
1457                         if (softc->ha_mode != CTL_HA_MODE_XFER)
1458                                 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1459                         io->io_hdr.nexus = msg->hdr.nexus;
1460                         io->scsiio.tag_num = msg->scsi.tag_num;
1461                         io->scsiio.tag_type = msg->scsi.tag_type;
1462 #ifdef CTL_TIME_IO
1463                         io->io_hdr.start_time = time_uptime;
1464                         getbinuptime(&io->io_hdr.start_bt);
1465 #endif /* CTL_TIME_IO */
1466                         io->scsiio.cdb_len = msg->scsi.cdb_len;
1467                         memcpy(io->scsiio.cdb, msg->scsi.cdb,
1468                                CTL_MAX_CDBLEN);
1469                         if (softc->ha_mode == CTL_HA_MODE_XFER) {
1470                                 const struct ctl_cmd_entry *entry;
1471
1472                                 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1473                                 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1474                                 io->io_hdr.flags |=
1475                                         entry->flags & CTL_FLAG_DATA_MASK;
1476                         }
1477                         ctl_enqueue_isc(io);
1478                         break;
1479
1480                 /* Performed on the Originating SC, XFER mode only */
1481                 case CTL_MSG_DATAMOVE: {
1482                         struct ctl_sg_entry *sgl;
1483                         int i, j;
1484
1485                         io = msg->hdr.original_sc;
1486                         if (io == NULL) {
1487                                 printf("%s: original_sc == NULL!\n", __func__);
1488                                 /* XXX KDM do something here */
1489                                 break;
1490                         }
1491                         io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1492                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1493                         /*
1494                          * Keep track of this, we need to send it back over
1495                          * when the datamove is complete.
1496                          */
1497                         io->io_hdr.remote_io = msg->hdr.serializing_sc;
1498                         if (msg->hdr.status == CTL_SUCCESS)
1499                                 io->io_hdr.status = msg->hdr.status;
1500
1501                         if (msg->dt.sg_sequence == 0) {
1502 #ifdef CTL_TIME_IO
1503                                 getbinuptime(&io->io_hdr.dma_start_bt);
1504 #endif
1505                                 i = msg->dt.kern_sg_entries +
1506                                     msg->dt.kern_data_len /
1507                                     CTL_HA_DATAMOVE_SEGMENT + 1;
1508                                 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1509                                     M_WAITOK | M_ZERO);
1510                                 CTL_RSGL(io) = sgl;
1511                                 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1512
1513                                 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1514
1515                                 io->scsiio.kern_sg_entries =
1516                                         msg->dt.kern_sg_entries;
1517                                 io->scsiio.rem_sg_entries =
1518                                         msg->dt.kern_sg_entries;
1519                                 io->scsiio.kern_data_len =
1520                                         msg->dt.kern_data_len;
1521                                 io->scsiio.kern_total_len =
1522                                         msg->dt.kern_total_len;
1523                                 io->scsiio.kern_data_resid =
1524                                         msg->dt.kern_data_resid;
1525                                 io->scsiio.kern_rel_offset =
1526                                         msg->dt.kern_rel_offset;
1527                                 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1528                                 io->io_hdr.flags |= msg->dt.flags &
1529                                     CTL_FLAG_BUS_ADDR;
1530                         } else
1531                                 sgl = (struct ctl_sg_entry *)
1532                                         io->scsiio.kern_data_ptr;
1533
1534                         for (i = msg->dt.sent_sg_entries, j = 0;
1535                              i < (msg->dt.sent_sg_entries +
1536                              msg->dt.cur_sg_entries); i++, j++) {
1537                                 sgl[i].addr = msg->dt.sg_list[j].addr;
1538                                 sgl[i].len = msg->dt.sg_list[j].len;
1539                         }
1540
1541                         /*
1542                          * If this is the last piece of the I/O, we've got
1543                          * the full S/G list.  Queue processing in the thread.
1544                          * Otherwise wait for the next piece.
1545                          */
1546                         if (msg->dt.sg_last != 0)
1547                                 ctl_enqueue_isc(io);
1548                         break;
1549                 }
1550                 /* Performed on the Serializing (primary) SC, XFER mode only */
1551                 case CTL_MSG_DATAMOVE_DONE: {
1552                         if (msg->hdr.serializing_sc == NULL) {
1553                                 printf("%s: serializing_sc == NULL!\n",
1554                                        __func__);
1555                                 /* XXX KDM now what? */
1556                                 break;
1557                         }
1558                         /*
1559                          * We grab the sense information here in case
1560                          * there was a failure, so we can return status
1561                          * back to the initiator.
1562                          */
1563                         io = msg->hdr.serializing_sc;
1564                         io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1565                         io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1566                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1567                         io->io_hdr.port_status = msg->scsi.port_status;
1568                         io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1569                         if (msg->hdr.status != CTL_STATUS_NONE) {
1570                                 io->io_hdr.status = msg->hdr.status;
1571                                 io->scsiio.scsi_status = msg->scsi.scsi_status;
1572                                 io->scsiio.sense_len = msg->scsi.sense_len;
1573                                 memcpy(&io->scsiio.sense_data,
1574                                     &msg->scsi.sense_data,
1575                                     msg->scsi.sense_len);
1576                                 if (msg->hdr.status == CTL_SUCCESS)
1577                                         io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1578                         }
1579                         ctl_enqueue_isc(io);
1580                         break;
1581                 }
1582
1583                 /* Preformed on Originating SC, SER_ONLY mode */
1584                 case CTL_MSG_R2R:
1585                         io = msg->hdr.original_sc;
1586                         if (io == NULL) {
1587                                 printf("%s: original_sc == NULL!\n",
1588                                     __func__);
1589                                 break;
1590                         }
1591                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1592                         io->io_hdr.msg_type = CTL_MSG_R2R;
1593                         io->io_hdr.remote_io = msg->hdr.serializing_sc;
1594                         ctl_enqueue_isc(io);
1595                         break;
1596
1597                 /*
1598                  * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1599                  * mode.
1600                  * Performed on the Originating (i.e. secondary) SC in XFER
1601                  * mode
1602                  */
1603                 case CTL_MSG_FINISH_IO:
1604                         if (softc->ha_mode == CTL_HA_MODE_XFER)
1605                                 ctl_isc_handler_finish_xfer(softc, msg);
1606                         else
1607                                 ctl_isc_handler_finish_ser_only(softc, msg);
1608                         break;
1609
1610                 /* Preformed on Originating SC */
1611                 case CTL_MSG_BAD_JUJU:
1612                         io = msg->hdr.original_sc;
1613                         if (io == NULL) {
1614                                 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1615                                        __func__);
1616                                 break;
1617                         }
1618                         ctl_copy_sense_data(msg, io);
1619                         /*
1620                          * IO should have already been cleaned up on other
1621                          * SC so clear this flag so we won't send a message
1622                          * back to finish the IO there.
1623                          */
1624                         io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1625                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1626
1627                         /* io = msg->hdr.serializing_sc; */
1628                         io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1629                         ctl_enqueue_isc(io);
1630                         break;
1631
1632                 /* Handle resets sent from the other side */
1633                 case CTL_MSG_MANAGE_TASKS: {
1634                         struct ctl_taskio *taskio;
1635                         taskio = (struct ctl_taskio *)ctl_alloc_io(
1636                             softc->othersc_pool);
1637                         ctl_zero_io((union ctl_io *)taskio);
1638                         taskio->io_hdr.io_type = CTL_IO_TASK;
1639                         taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1640                         taskio->io_hdr.nexus = msg->hdr.nexus;
1641                         taskio->task_action = msg->task.task_action;
1642                         taskio->tag_num = msg->task.tag_num;
1643                         taskio->tag_type = msg->task.tag_type;
1644 #ifdef CTL_TIME_IO
1645                         taskio->io_hdr.start_time = time_uptime;
1646                         getbinuptime(&taskio->io_hdr.start_bt);
1647 #endif /* CTL_TIME_IO */
1648                         ctl_run_task((union ctl_io *)taskio);
1649                         break;
1650                 }
1651                 /* Persistent Reserve action which needs attention */
1652                 case CTL_MSG_PERS_ACTION:
1653                         presio = (struct ctl_prio *)ctl_alloc_io(
1654                             softc->othersc_pool);
1655                         ctl_zero_io((union ctl_io *)presio);
1656                         presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1657                         presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1658                         presio->io_hdr.nexus = msg->hdr.nexus;
1659                         presio->pr_msg = msg->pr;
1660                         ctl_enqueue_isc((union ctl_io *)presio);
1661                         break;
1662                 case CTL_MSG_UA:
1663                         ctl_isc_ua(softc, msg, param);
1664                         break;
1665                 case CTL_MSG_PORT_SYNC:
1666                         ctl_isc_port_sync(softc, msg, param);
1667                         break;
1668                 case CTL_MSG_LUN_SYNC:
1669                         ctl_isc_lun_sync(softc, msg, param);
1670                         break;
1671                 case CTL_MSG_IID_SYNC:
1672                         ctl_isc_iid_sync(softc, msg, param);
1673                         break;
1674                 case CTL_MSG_LOGIN:
1675                         ctl_isc_login(softc, msg, param);
1676                         break;
1677                 case CTL_MSG_MODE_SYNC:
1678                         ctl_isc_mode_sync(softc, msg, param);
1679                         break;
1680                 default:
1681                         printf("Received HA message of unknown type %d\n",
1682                             msg->hdr.msg_type);
1683                         ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1684                         break;
1685                 }
1686                 if (msg != &msgbuf)
1687                         free(msg, M_CTL);
1688         } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1689                 printf("CTL: HA link status changed from %d to %d\n",
1690                     softc->ha_link, param);
1691                 if (param == softc->ha_link)
1692                         return;
1693                 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1694                         softc->ha_link = param;
1695                         ctl_isc_ha_link_down(softc);
1696                 } else {
1697                         softc->ha_link = param;
1698                         if (softc->ha_link == CTL_HA_LINK_ONLINE)
1699                                 ctl_isc_ha_link_up(softc);
1700                 }
1701                 return;
1702         } else {
1703                 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1704                 return;
1705         }
1706 }
1707
1708 static void
1709 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1710 {
1711
1712         memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1713             src->scsi.sense_len);
1714         dest->scsiio.scsi_status = src->scsi.scsi_status;
1715         dest->scsiio.sense_len = src->scsi.sense_len;
1716         dest->io_hdr.status = src->hdr.status;
1717 }
1718
1719 static void
1720 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1721 {
1722
1723         memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1724             src->scsiio.sense_len);
1725         dest->scsi.scsi_status = src->scsiio.scsi_status;
1726         dest->scsi.sense_len = src->scsiio.sense_len;
1727         dest->hdr.status = src->io_hdr.status;
1728 }
1729
1730 void
1731 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1732 {
1733         struct ctl_softc *softc = lun->ctl_softc;
1734         ctl_ua_type *pu;
1735
1736         if (initidx < softc->init_min || initidx >= softc->init_max)
1737                 return;
1738         mtx_assert(&lun->lun_lock, MA_OWNED);
1739         pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1740         if (pu == NULL)
1741                 return;
1742         pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1743 }
1744
1745 void
1746 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1747 {
1748         int i;
1749
1750         mtx_assert(&lun->lun_lock, MA_OWNED);
1751         if (lun->pending_ua[port] == NULL)
1752                 return;
1753         for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1754                 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1755                         continue;
1756                 lun->pending_ua[port][i] |= ua;
1757         }
1758 }
1759
1760 void
1761 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1762 {
1763         struct ctl_softc *softc = lun->ctl_softc;
1764         int i;
1765
1766         mtx_assert(&lun->lun_lock, MA_OWNED);
1767         for (i = softc->port_min; i < softc->port_max; i++)
1768                 ctl_est_ua_port(lun, i, except, ua);
1769 }
1770
1771 void
1772 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1773 {
1774         struct ctl_softc *softc = lun->ctl_softc;
1775         ctl_ua_type *pu;
1776
1777         if (initidx < softc->init_min || initidx >= softc->init_max)
1778                 return;
1779         mtx_assert(&lun->lun_lock, MA_OWNED);
1780         pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1781         if (pu == NULL)
1782                 return;
1783         pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1784 }
1785
1786 void
1787 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1788 {
1789         struct ctl_softc *softc = lun->ctl_softc;
1790         int i, j;
1791
1792         mtx_assert(&lun->lun_lock, MA_OWNED);
1793         for (i = softc->port_min; i < softc->port_max; i++) {
1794                 if (lun->pending_ua[i] == NULL)
1795                         continue;
1796                 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1797                         if (i * CTL_MAX_INIT_PER_PORT + j == except)
1798                                 continue;
1799                         lun->pending_ua[i][j] &= ~ua;
1800                 }
1801         }
1802 }
1803
1804 void
1805 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1806     ctl_ua_type ua_type)
1807 {
1808         struct ctl_lun *lun;
1809
1810         mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1811         STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1812                 mtx_lock(&lun->lun_lock);
1813                 ctl_clr_ua(lun, initidx, ua_type);
1814                 mtx_unlock(&lun->lun_lock);
1815         }
1816 }
1817
1818 static int
1819 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1820 {
1821         struct ctl_softc *softc = (struct ctl_softc *)arg1;
1822         struct ctl_lun *lun;
1823         struct ctl_lun_req ireq;
1824         int error, value;
1825
1826         value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1827         error = sysctl_handle_int(oidp, &value, 0, req);
1828         if ((error != 0) || (req->newptr == NULL))
1829                 return (error);
1830
1831         mtx_lock(&softc->ctl_lock);
1832         if (value == 0)
1833                 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1834         else
1835                 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1836         STAILQ_FOREACH(lun, &softc->lun_list, links) {
1837                 mtx_unlock(&softc->ctl_lock);
1838                 bzero(&ireq, sizeof(ireq));
1839                 ireq.reqtype = CTL_LUNREQ_MODIFY;
1840                 ireq.reqdata.modify.lun_id = lun->lun;
1841                 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1842                     curthread);
1843                 if (ireq.status != CTL_LUN_OK) {
1844                         printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1845                             __func__, ireq.status, ireq.error_str);
1846                 }
1847                 mtx_lock(&softc->ctl_lock);
1848         }
1849         mtx_unlock(&softc->ctl_lock);
1850         return (0);
1851 }
1852
1853 static int
1854 ctl_init(void)
1855 {
1856         struct make_dev_args args;
1857         struct ctl_softc *softc;
1858         int i, error;
1859
1860         softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1861                                M_WAITOK | M_ZERO);
1862
1863         make_dev_args_init(&args);
1864         args.mda_devsw = &ctl_cdevsw;
1865         args.mda_uid = UID_ROOT;
1866         args.mda_gid = GID_OPERATOR;
1867         args.mda_mode = 0600;
1868         args.mda_si_drv1 = softc;
1869         args.mda_si_drv2 = NULL;
1870         error = make_dev_s(&args, &softc->dev, "cam/ctl");
1871         if (error != 0) {
1872                 free(softc, M_DEVBUF);
1873                 control_softc = NULL;
1874                 return (error);
1875         }
1876
1877         sysctl_ctx_init(&softc->sysctl_ctx);
1878         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1879                 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1880                 CTLFLAG_RD, 0, "CAM Target Layer");
1881
1882         if (softc->sysctl_tree == NULL) {
1883                 printf("%s: unable to allocate sysctl tree\n", __func__);
1884                 destroy_dev(softc->dev);
1885                 free(softc, M_DEVBUF);
1886                 control_softc = NULL;
1887                 return (ENOMEM);
1888         }
1889
1890         mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1891         softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1892             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1893         softc->flags = 0;
1894
1895         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1896             OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1897             "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1898
1899         if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1900                 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1901                     ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1902                 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1903         }
1904         softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1905             M_DEVBUF, M_WAITOK | M_ZERO);
1906         softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1907             ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1908         if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1909                 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1910                     ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1911                 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1912         }
1913         softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1914           ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1915         softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1916              M_DEVBUF, M_WAITOK | M_ZERO);
1917
1918
1919         /*
1920          * In Copan's HA scheme, the "master" and "slave" roles are
1921          * figured out through the slot the controller is in.  Although it
1922          * is an active/active system, someone has to be in charge.
1923          */
1924         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1925             OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1926             "HA head ID (0 - no HA)");
1927         if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1928                 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1929                 softc->is_single = 1;
1930                 softc->port_cnt = ctl_max_ports;
1931                 softc->port_min = 0;
1932         } else {
1933                 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1934                 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1935         }
1936         softc->port_max = softc->port_min + softc->port_cnt;
1937         softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1938         softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1939
1940         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1941             OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1942             "HA link state (0 - offline, 1 - unknown, 2 - online)");
1943
1944         STAILQ_INIT(&softc->lun_list);
1945         STAILQ_INIT(&softc->pending_lun_queue);
1946         STAILQ_INIT(&softc->fe_list);
1947         STAILQ_INIT(&softc->port_list);
1948         STAILQ_INIT(&softc->be_list);
1949         ctl_tpc_init(softc);
1950
1951         if (worker_threads <= 0)
1952                 worker_threads = max(1, mp_ncpus / 4);
1953         if (worker_threads > CTL_MAX_THREADS)
1954                 worker_threads = CTL_MAX_THREADS;
1955
1956         for (i = 0; i < worker_threads; i++) {
1957                 struct ctl_thread *thr = &softc->threads[i];
1958
1959                 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1960                 thr->ctl_softc = softc;
1961                 STAILQ_INIT(&thr->incoming_queue);
1962                 STAILQ_INIT(&thr->rtr_queue);
1963                 STAILQ_INIT(&thr->done_queue);
1964                 STAILQ_INIT(&thr->isc_queue);
1965
1966                 error = kproc_kthread_add(ctl_work_thread, thr,
1967                     &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1968                 if (error != 0) {
1969                         printf("error creating CTL work thread!\n");
1970                         return (error);
1971                 }
1972         }
1973         error = kproc_kthread_add(ctl_lun_thread, softc,
1974             &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1975         if (error != 0) {
1976                 printf("error creating CTL lun thread!\n");
1977                 return (error);
1978         }
1979         error = kproc_kthread_add(ctl_thresh_thread, softc,
1980             &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1981         if (error != 0) {
1982                 printf("error creating CTL threshold thread!\n");
1983                 return (error);
1984         }
1985
1986         SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1987             OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1988             softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1989
1990         if (softc->is_single == 0) {
1991                 if (ctl_frontend_register(&ha_frontend) != 0)
1992                         softc->is_single = 1;
1993         }
1994         return (0);
1995 }
1996
1997 static int
1998 ctl_shutdown(void)
1999 {
2000         struct ctl_softc *softc = control_softc;
2001         int i;
2002
2003         if (softc->is_single == 0)
2004                 ctl_frontend_deregister(&ha_frontend);
2005
2006         destroy_dev(softc->dev);
2007
2008         /* Shutdown CTL threads. */
2009         softc->shutdown = 1;
2010         for (i = 0; i < worker_threads; i++) {
2011                 struct ctl_thread *thr = &softc->threads[i];
2012                 while (thr->thread != NULL) {
2013                         wakeup(thr);
2014                         if (thr->thread != NULL)
2015                                 pause("CTL thr shutdown", 1);
2016                 }
2017                 mtx_destroy(&thr->queue_lock);
2018         }
2019         while (softc->lun_thread != NULL) {
2020                 wakeup(&softc->pending_lun_queue);
2021                 if (softc->lun_thread != NULL)
2022                         pause("CTL thr shutdown", 1);
2023         }
2024         while (softc->thresh_thread != NULL) {
2025                 wakeup(softc->thresh_thread);
2026                 if (softc->thresh_thread != NULL)
2027                         pause("CTL thr shutdown", 1);
2028         }
2029
2030         ctl_tpc_shutdown(softc);
2031         uma_zdestroy(softc->io_zone);
2032         mtx_destroy(&softc->ctl_lock);
2033
2034         free(softc->ctl_luns, M_DEVBUF);
2035         free(softc->ctl_lun_mask, M_DEVBUF);
2036         free(softc->ctl_port_mask, M_DEVBUF);
2037         free(softc->ctl_ports, M_DEVBUF);
2038
2039         sysctl_ctx_free(&softc->sysctl_ctx);
2040
2041         free(softc, M_DEVBUF);
2042         control_softc = NULL;
2043         return (0);
2044 }
2045
2046 static int
2047 ctl_module_event_handler(module_t mod, int what, void *arg)
2048 {
2049
2050         switch (what) {
2051         case MOD_LOAD:
2052                 return (ctl_init());
2053         case MOD_UNLOAD:
2054                 return (ctl_shutdown());
2055         default:
2056                 return (EOPNOTSUPP);
2057         }
2058 }
2059
2060 /*
2061  * XXX KDM should we do some access checks here?  Bump a reference count to
2062  * prevent a CTL module from being unloaded while someone has it open?
2063  */
2064 static int
2065 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2066 {
2067         return (0);
2068 }
2069
2070 static int
2071 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2072 {
2073         return (0);
2074 }
2075
2076 /*
2077  * Remove an initiator by port number and initiator ID.
2078  * Returns 0 for success, -1 for failure.
2079  */
2080 int
2081 ctl_remove_initiator(struct ctl_port *port, int iid)
2082 {
2083         struct ctl_softc *softc = port->ctl_softc;
2084         int last;
2085
2086         mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2087
2088         if (iid > CTL_MAX_INIT_PER_PORT) {
2089                 printf("%s: initiator ID %u > maximun %u!\n",
2090                        __func__, iid, CTL_MAX_INIT_PER_PORT);
2091                 return (-1);
2092         }
2093
2094         mtx_lock(&softc->ctl_lock);
2095         last = (--port->wwpn_iid[iid].in_use == 0);
2096         port->wwpn_iid[iid].last_use = time_uptime;
2097         mtx_unlock(&softc->ctl_lock);
2098         if (last)
2099                 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2100         ctl_isc_announce_iid(port, iid);
2101
2102         return (0);
2103 }
2104
2105 /*
2106  * Add an initiator to the initiator map.
2107  * Returns iid for success, < 0 for failure.
2108  */
2109 int
2110 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2111 {
2112         struct ctl_softc *softc = port->ctl_softc;
2113         time_t best_time;
2114         int i, best;
2115
2116         mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2117
2118         if (iid >= CTL_MAX_INIT_PER_PORT) {
2119                 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2120                        __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2121                 free(name, M_CTL);
2122                 return (-1);
2123         }
2124
2125         mtx_lock(&softc->ctl_lock);
2126
2127         if (iid < 0 && (wwpn != 0 || name != NULL)) {
2128                 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2129                         if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2130                                 iid = i;
2131                                 break;
2132                         }
2133                         if (name != NULL && port->wwpn_iid[i].name != NULL &&
2134                             strcmp(name, port->wwpn_iid[i].name) == 0) {
2135                                 iid = i;
2136                                 break;
2137                         }
2138                 }
2139         }
2140
2141         if (iid < 0) {
2142                 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2143                         if (port->wwpn_iid[i].in_use == 0 &&
2144                             port->wwpn_iid[i].wwpn == 0 &&
2145                             port->wwpn_iid[i].name == NULL) {
2146                                 iid = i;
2147                                 break;
2148                         }
2149                 }
2150         }
2151
2152         if (iid < 0) {
2153                 best = -1;
2154                 best_time = INT32_MAX;
2155                 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2156                         if (port->wwpn_iid[i].in_use == 0) {
2157                                 if (port->wwpn_iid[i].last_use < best_time) {
2158                                         best = i;
2159                                         best_time = port->wwpn_iid[i].last_use;
2160                                 }
2161                         }
2162                 }
2163                 iid = best;
2164         }
2165
2166         if (iid < 0) {
2167                 mtx_unlock(&softc->ctl_lock);
2168                 free(name, M_CTL);
2169                 return (-2);
2170         }
2171
2172         if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2173                 /*
2174                  * This is not an error yet.
2175                  */
2176                 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2177 #if 0
2178                         printf("%s: port %d iid %u WWPN %#jx arrived"
2179                             " again\n", __func__, port->targ_port,
2180                             iid, (uintmax_t)wwpn);
2181 #endif
2182                         goto take;
2183                 }
2184                 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2185                     strcmp(name, port->wwpn_iid[iid].name) == 0) {
2186 #if 0
2187                         printf("%s: port %d iid %u name '%s' arrived"
2188                             " again\n", __func__, port->targ_port,
2189                             iid, name);
2190 #endif
2191                         goto take;
2192                 }
2193
2194                 /*
2195                  * This is an error, but what do we do about it?  The
2196                  * driver is telling us we have a new WWPN for this
2197                  * initiator ID, so we pretty much need to use it.
2198                  */
2199                 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2200                     " but WWPN %#jx '%s' is still at that address\n",
2201                     __func__, port->targ_port, iid, wwpn, name,
2202                     (uintmax_t)port->wwpn_iid[iid].wwpn,
2203                     port->wwpn_iid[iid].name);
2204         }
2205 take:
2206         free(port->wwpn_iid[iid].name, M_CTL);
2207         port->wwpn_iid[iid].name = name;
2208         port->wwpn_iid[iid].wwpn = wwpn;
2209         port->wwpn_iid[iid].in_use++;
2210         mtx_unlock(&softc->ctl_lock);
2211         ctl_isc_announce_iid(port, iid);
2212
2213         return (iid);
2214 }
2215
2216 static int
2217 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2218 {
2219         int len;
2220
2221         switch (port->port_type) {
2222         case CTL_PORT_FC:
2223         {
2224                 struct scsi_transportid_fcp *id =
2225                     (struct scsi_transportid_fcp *)buf;
2226                 if (port->wwpn_iid[iid].wwpn == 0)
2227                         return (0);
2228                 memset(id, 0, sizeof(*id));
2229                 id->format_protocol = SCSI_PROTO_FC;
2230                 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2231                 return (sizeof(*id));
2232         }
2233         case CTL_PORT_ISCSI:
2234         {
2235                 struct scsi_transportid_iscsi_port *id =
2236                     (struct scsi_transportid_iscsi_port *)buf;
2237                 if (port->wwpn_iid[iid].name == NULL)
2238                         return (0);
2239                 memset(id, 0, 256);
2240                 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2241                     SCSI_PROTO_ISCSI;
2242                 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2243                 len = roundup2(min(len, 252), 4);
2244                 scsi_ulto2b(len, id->additional_length);
2245                 return (sizeof(*id) + len);
2246         }
2247         case CTL_PORT_SAS:
2248         {
2249                 struct scsi_transportid_sas *id =
2250                     (struct scsi_transportid_sas *)buf;
2251                 if (port->wwpn_iid[iid].wwpn == 0)
2252                         return (0);
2253                 memset(id, 0, sizeof(*id));
2254                 id->format_protocol = SCSI_PROTO_SAS;
2255                 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2256                 return (sizeof(*id));
2257         }
2258         default:
2259         {
2260                 struct scsi_transportid_spi *id =
2261                     (struct scsi_transportid_spi *)buf;
2262                 memset(id, 0, sizeof(*id));
2263                 id->format_protocol = SCSI_PROTO_SPI;
2264                 scsi_ulto2b(iid, id->scsi_addr);
2265                 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2266                 return (sizeof(*id));
2267         }
2268         }
2269 }
2270
2271 /*
2272  * Serialize a command that went down the "wrong" side, and so was sent to
2273  * this controller for execution.  The logic is a little different than the
2274  * standard case in ctl_scsiio_precheck().  Errors in this case need to get
2275  * sent back to the other side, but in the success case, we execute the
2276  * command on this side (XFER mode) or tell the other side to execute it
2277  * (SER_ONLY mode).
2278  */
2279 static void
2280 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2281 {
2282         struct ctl_softc *softc = CTL_SOFTC(ctsio);
2283         struct ctl_port *port = CTL_PORT(ctsio);
2284         union ctl_ha_msg msg_info;
2285         struct ctl_lun *lun;
2286         const struct ctl_cmd_entry *entry;
2287         union ctl_io *bio;
2288         uint32_t targ_lun;
2289
2290         targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2291
2292         /* Make sure that we know about this port. */
2293         if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2294                 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2295                                          /*retry_count*/ 1);
2296                 goto badjuju;
2297         }
2298
2299         /* Make sure that we know about this LUN. */
2300         mtx_lock(&softc->ctl_lock);
2301         if (targ_lun >= ctl_max_luns ||
2302             (lun = softc->ctl_luns[targ_lun]) == NULL) {
2303                 mtx_unlock(&softc->ctl_lock);
2304
2305                 /*
2306                  * The other node would not send this request to us unless
2307                  * received announce that we are primary node for this LUN.
2308                  * If this LUN does not exist now, it is probably result of
2309                  * a race, so respond to initiator in the most opaque way.
2310                  */
2311                 ctl_set_busy(ctsio);
2312                 goto badjuju;
2313         }
2314         mtx_lock(&lun->lun_lock);
2315         mtx_unlock(&softc->ctl_lock);
2316
2317         /*
2318          * If the LUN is invalid, pretend that it doesn't exist.
2319          * It will go away as soon as all pending I/Os completed.
2320          */
2321         if (lun->flags & CTL_LUN_DISABLED) {
2322                 mtx_unlock(&lun->lun_lock);
2323                 ctl_set_busy(ctsio);
2324                 goto badjuju;
2325         }
2326
2327         entry = ctl_get_cmd_entry(ctsio, NULL);
2328         if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2329                 mtx_unlock(&lun->lun_lock);
2330                 goto badjuju;
2331         }
2332
2333         CTL_LUN(ctsio) = lun;
2334         CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2335
2336         /*
2337          * Every I/O goes into the OOA queue for a
2338          * particular LUN, and stays there until completion.
2339          */
2340 #ifdef CTL_TIME_IO
2341         if (TAILQ_EMPTY(&lun->ooa_queue))
2342                 lun->idle_time += getsbinuptime() - lun->last_busy;
2343 #endif
2344         TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2345
2346         bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
2347         switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
2348         case CTL_ACTION_BLOCK:
2349                 ctsio->io_hdr.blocker = bio;
2350                 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
2351                                   blocked_links);
2352                 mtx_unlock(&lun->lun_lock);
2353                 break;
2354         case CTL_ACTION_PASS:
2355         case CTL_ACTION_SKIP:
2356                 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2357                         ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2358                         ctl_enqueue_rtr((union ctl_io *)ctsio);
2359                         mtx_unlock(&lun->lun_lock);
2360                 } else {
2361                         ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2362                         mtx_unlock(&lun->lun_lock);
2363
2364                         /* send msg back to other side */
2365                         msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2366                         msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2367                         msg_info.hdr.msg_type = CTL_MSG_R2R;
2368                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2369                             sizeof(msg_info.hdr), M_WAITOK);
2370                 }
2371                 break;
2372         case CTL_ACTION_OVERLAP:
2373                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2374                 mtx_unlock(&lun->lun_lock);
2375                 ctl_set_overlapped_cmd(ctsio);
2376                 goto badjuju;
2377         case CTL_ACTION_OVERLAP_TAG:
2378                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2379                 mtx_unlock(&lun->lun_lock);
2380                 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2381                 goto badjuju;
2382         case CTL_ACTION_ERROR:
2383         default:
2384                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2385                 mtx_unlock(&lun->lun_lock);
2386
2387                 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2388                                          /*retry_count*/ 0);
2389 badjuju:
2390                 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2391                 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2392                 msg_info.hdr.serializing_sc = NULL;
2393                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2394                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2395                     sizeof(msg_info.scsi), M_WAITOK);
2396                 ctl_free_io((union ctl_io *)ctsio);
2397                 break;
2398         }
2399 }
2400
2401 /*
2402  * Returns 0 for success, errno for failure.
2403  */
2404 static void
2405 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2406                    struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2407 {
2408         union ctl_io *io;
2409
2410         mtx_lock(&lun->lun_lock);
2411         for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2412              (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2413              ooa_links)) {
2414                 struct ctl_ooa_entry *entry;
2415
2416                 /*
2417                  * If we've got more than we can fit, just count the
2418                  * remaining entries.
2419                  */
2420                 if (*cur_fill_num >= ooa_hdr->alloc_num)
2421                         continue;
2422
2423                 entry = &kern_entries[*cur_fill_num];
2424
2425                 entry->tag_num = io->scsiio.tag_num;
2426                 entry->lun_num = lun->lun;
2427 #ifdef CTL_TIME_IO
2428                 entry->start_bt = io->io_hdr.start_bt;
2429 #endif
2430                 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2431                 entry->cdb_len = io->scsiio.cdb_len;
2432                 if (io->io_hdr.blocker != NULL)
2433                         entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2434
2435                 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2436                         entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2437
2438                 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2439                         entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2440
2441                 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2442                         entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2443
2444                 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2445                         entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2446         }
2447         mtx_unlock(&lun->lun_lock);
2448 }
2449
2450 /*
2451  * Escape characters that are illegal or not recommended in XML.
2452  */
2453 int
2454 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2455 {
2456         char *end = str + size;
2457         int retval;
2458
2459         retval = 0;
2460
2461         for (; *str && str < end; str++) {
2462                 switch (*str) {
2463                 case '&':
2464                         retval = sbuf_printf(sb, "&amp;");
2465                         break;
2466                 case '>':
2467                         retval = sbuf_printf(sb, "&gt;");
2468                         break;
2469                 case '<':
2470                         retval = sbuf_printf(sb, "&lt;");
2471                         break;
2472                 default:
2473                         retval = sbuf_putc(sb, *str);
2474                         break;
2475                 }
2476
2477                 if (retval != 0)
2478                         break;
2479
2480         }
2481
2482         return (retval);
2483 }
2484
2485 static void
2486 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2487 {
2488         struct scsi_vpd_id_descriptor *desc;
2489         int i;
2490
2491         if (id == NULL || id->len < 4)
2492                 return;
2493         desc = (struct scsi_vpd_id_descriptor *)id->data;
2494         switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2495         case SVPD_ID_TYPE_T10:
2496                 sbuf_printf(sb, "t10.");
2497                 break;
2498         case SVPD_ID_TYPE_EUI64:
2499                 sbuf_printf(sb, "eui.");
2500                 break;
2501         case SVPD_ID_TYPE_NAA:
2502                 sbuf_printf(sb, "naa.");
2503                 break;
2504         case SVPD_ID_TYPE_SCSI_NAME:
2505                 break;
2506         }
2507         switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2508         case SVPD_ID_CODESET_BINARY:
2509                 for (i = 0; i < desc->length; i++)
2510                         sbuf_printf(sb, "%02x", desc->identifier[i]);
2511                 break;
2512         case SVPD_ID_CODESET_ASCII:
2513                 sbuf_printf(sb, "%.*s", (int)desc->length,
2514                     (char *)desc->identifier);
2515                 break;
2516         case SVPD_ID_CODESET_UTF8:
2517                 sbuf_printf(sb, "%s", (char *)desc->identifier);
2518                 break;
2519         }
2520 }
2521
2522 static int
2523 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2524           struct thread *td)
2525 {
2526         struct ctl_softc *softc = dev->si_drv1;
2527         struct ctl_port *port;
2528         struct ctl_lun *lun;
2529         int retval;
2530
2531         retval = 0;
2532
2533         switch (cmd) {
2534         case CTL_IO:
2535                 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2536                 break;
2537         case CTL_ENABLE_PORT:
2538         case CTL_DISABLE_PORT:
2539         case CTL_SET_PORT_WWNS: {
2540                 struct ctl_port *port;
2541                 struct ctl_port_entry *entry;
2542
2543                 entry = (struct ctl_port_entry *)addr;
2544                 
2545                 mtx_lock(&softc->ctl_lock);
2546                 STAILQ_FOREACH(port, &softc->port_list, links) {
2547                         int action, done;
2548
2549                         if (port->targ_port < softc->port_min ||
2550                             port->targ_port >= softc->port_max)
2551                                 continue;
2552
2553                         action = 0;
2554                         done = 0;
2555                         if ((entry->port_type == CTL_PORT_NONE)
2556                          && (entry->targ_port == port->targ_port)) {
2557                                 /*
2558                                  * If the user only wants to enable or
2559                                  * disable or set WWNs on a specific port,
2560                                  * do the operation and we're done.
2561                                  */
2562                                 action = 1;
2563                                 done = 1;
2564                         } else if (entry->port_type & port->port_type) {
2565                                 /*
2566                                  * Compare the user's type mask with the
2567                                  * particular frontend type to see if we
2568                                  * have a match.
2569                                  */
2570                                 action = 1;
2571                                 done = 0;
2572
2573                                 /*
2574                                  * Make sure the user isn't trying to set
2575                                  * WWNs on multiple ports at the same time.
2576                                  */
2577                                 if (cmd == CTL_SET_PORT_WWNS) {
2578                                         printf("%s: Can't set WWNs on "
2579                                                "multiple ports\n", __func__);
2580                                         retval = EINVAL;
2581                                         break;
2582                                 }
2583                         }
2584                         if (action == 0)
2585                                 continue;
2586
2587                         /*
2588                          * XXX KDM we have to drop the lock here, because
2589                          * the online/offline operations can potentially
2590                          * block.  We need to reference count the frontends
2591                          * so they can't go away,
2592                          */
2593                         if (cmd == CTL_ENABLE_PORT) {
2594                                 mtx_unlock(&softc->ctl_lock);
2595                                 ctl_port_online(port);
2596                                 mtx_lock(&softc->ctl_lock);
2597                         } else if (cmd == CTL_DISABLE_PORT) {
2598                                 mtx_unlock(&softc->ctl_lock);
2599                                 ctl_port_offline(port);
2600                                 mtx_lock(&softc->ctl_lock);
2601                         } else if (cmd == CTL_SET_PORT_WWNS) {
2602                                 ctl_port_set_wwns(port,
2603                                     (entry->flags & CTL_PORT_WWNN_VALID) ?
2604                                     1 : 0, entry->wwnn,
2605                                     (entry->flags & CTL_PORT_WWPN_VALID) ?
2606                                     1 : 0, entry->wwpn);
2607                         }
2608                         if (done != 0)
2609                                 break;
2610                 }
2611                 mtx_unlock(&softc->ctl_lock);
2612                 break;
2613         }
2614         case CTL_GET_OOA: {
2615                 struct ctl_ooa *ooa_hdr;
2616                 struct ctl_ooa_entry *entries;
2617                 uint32_t cur_fill_num;
2618
2619                 ooa_hdr = (struct ctl_ooa *)addr;
2620
2621                 if ((ooa_hdr->alloc_len == 0)
2622                  || (ooa_hdr->alloc_num == 0)) {
2623                         printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2624                                "must be non-zero\n", __func__,
2625                                ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2626                         retval = EINVAL;
2627                         break;
2628                 }
2629
2630                 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2631                     sizeof(struct ctl_ooa_entry))) {
2632                         printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2633                                "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2634                                __func__, ooa_hdr->alloc_len,
2635                                ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2636                         retval = EINVAL;
2637                         break;
2638                 }
2639
2640                 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2641                 if (entries == NULL) {
2642                         printf("%s: could not allocate %d bytes for OOA "
2643                                "dump\n", __func__, ooa_hdr->alloc_len);
2644                         retval = ENOMEM;
2645                         break;
2646                 }
2647
2648                 mtx_lock(&softc->ctl_lock);
2649                 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2650                     (ooa_hdr->lun_num >= ctl_max_luns ||
2651                      softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2652                         mtx_unlock(&softc->ctl_lock);
2653                         free(entries, M_CTL);
2654                         printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2655                                __func__, (uintmax_t)ooa_hdr->lun_num);
2656                         retval = EINVAL;
2657                         break;
2658                 }
2659
2660                 cur_fill_num = 0;
2661
2662                 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2663                         STAILQ_FOREACH(lun, &softc->lun_list, links) {
2664                                 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2665                                     ooa_hdr, entries);
2666                         }
2667                 } else {
2668                         lun = softc->ctl_luns[ooa_hdr->lun_num];
2669                         ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2670                             entries);
2671                 }
2672                 mtx_unlock(&softc->ctl_lock);
2673
2674                 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2675                 ooa_hdr->fill_len = ooa_hdr->fill_num *
2676                         sizeof(struct ctl_ooa_entry);
2677                 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2678                 if (retval != 0) {
2679                         printf("%s: error copying out %d bytes for OOA dump\n", 
2680                                __func__, ooa_hdr->fill_len);
2681                 }
2682
2683                 getbinuptime(&ooa_hdr->cur_bt);
2684
2685                 if (cur_fill_num > ooa_hdr->alloc_num) {
2686                         ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2687                         ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2688                 } else {
2689                         ooa_hdr->dropped_num = 0;
2690                         ooa_hdr->status = CTL_OOA_OK;
2691                 }
2692
2693                 free(entries, M_CTL);
2694                 break;
2695         }
2696         case CTL_DELAY_IO: {
2697                 struct ctl_io_delay_info *delay_info;
2698
2699                 delay_info = (struct ctl_io_delay_info *)addr;
2700
2701 #ifdef CTL_IO_DELAY
2702                 mtx_lock(&softc->ctl_lock);
2703                 if (delay_info->lun_id >= ctl_max_luns ||
2704                     (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2705                         mtx_unlock(&softc->ctl_lock);
2706                         delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2707                         break;
2708                 }
2709                 mtx_lock(&lun->lun_lock);
2710                 mtx_unlock(&softc->ctl_lock);
2711                 delay_info->status = CTL_DELAY_STATUS_OK;
2712                 switch (delay_info->delay_type) {
2713                 case CTL_DELAY_TYPE_CONT:
2714                 case CTL_DELAY_TYPE_ONESHOT:
2715                         break;
2716                 default:
2717                         delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2718                         break;
2719                 }
2720                 switch (delay_info->delay_loc) {
2721                 case CTL_DELAY_LOC_DATAMOVE:
2722                         lun->delay_info.datamove_type = delay_info->delay_type;
2723                         lun->delay_info.datamove_delay = delay_info->delay_secs;
2724                         break;
2725                 case CTL_DELAY_LOC_DONE:
2726                         lun->delay_info.done_type = delay_info->delay_type;
2727                         lun->delay_info.done_delay = delay_info->delay_secs;
2728                         break;
2729                 default:
2730                         delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2731                         break;
2732                 }
2733                 mtx_unlock(&lun->lun_lock);
2734 #else
2735                 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2736 #endif /* CTL_IO_DELAY */
2737                 break;
2738         }
2739         case CTL_ERROR_INJECT: {
2740                 struct ctl_error_desc *err_desc, *new_err_desc;
2741
2742                 err_desc = (struct ctl_error_desc *)addr;
2743
2744                 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2745                                       M_WAITOK | M_ZERO);
2746                 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2747
2748                 mtx_lock(&softc->ctl_lock);
2749                 if (err_desc->lun_id >= ctl_max_luns ||
2750                     (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2751                         mtx_unlock(&softc->ctl_lock);
2752                         free(new_err_desc, M_CTL);
2753                         printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2754                                __func__, (uintmax_t)err_desc->lun_id);
2755                         retval = EINVAL;
2756                         break;
2757                 }
2758                 mtx_lock(&lun->lun_lock);
2759                 mtx_unlock(&softc->ctl_lock);
2760
2761                 /*
2762                  * We could do some checking here to verify the validity
2763                  * of the request, but given the complexity of error
2764                  * injection requests, the checking logic would be fairly
2765                  * complex.
2766                  *
2767                  * For now, if the request is invalid, it just won't get
2768                  * executed and might get deleted.
2769                  */
2770                 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2771
2772                 /*
2773                  * XXX KDM check to make sure the serial number is unique,
2774                  * in case we somehow manage to wrap.  That shouldn't
2775                  * happen for a very long time, but it's the right thing to
2776                  * do.
2777                  */
2778                 new_err_desc->serial = lun->error_serial;
2779                 err_desc->serial = lun->error_serial;
2780                 lun->error_serial++;
2781
2782                 mtx_unlock(&lun->lun_lock);
2783                 break;
2784         }
2785         case CTL_ERROR_INJECT_DELETE: {
2786                 struct ctl_error_desc *delete_desc, *desc, *desc2;
2787                 int delete_done;
2788
2789                 delete_desc = (struct ctl_error_desc *)addr;
2790                 delete_done = 0;
2791
2792                 mtx_lock(&softc->ctl_lock);
2793                 if (delete_desc->lun_id >= ctl_max_luns ||
2794                     (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2795                         mtx_unlock(&softc->ctl_lock);
2796                         printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2797                                __func__, (uintmax_t)delete_desc->lun_id);
2798                         retval = EINVAL;
2799                         break;
2800                 }
2801                 mtx_lock(&lun->lun_lock);
2802                 mtx_unlock(&softc->ctl_lock);
2803                 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2804                         if (desc->serial != delete_desc->serial)
2805                                 continue;
2806
2807                         STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2808                                       links);
2809                         free(desc, M_CTL);
2810                         delete_done = 1;
2811                 }
2812                 mtx_unlock(&lun->lun_lock);
2813                 if (delete_done == 0) {
2814                         printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2815                                "error serial %ju on LUN %u\n", __func__, 
2816                                delete_desc->serial, delete_desc->lun_id);
2817                         retval = EINVAL;
2818                         break;
2819                 }
2820                 break;
2821         }
2822         case CTL_DUMP_STRUCTS: {
2823                 int j, k;
2824                 struct ctl_port *port;
2825                 struct ctl_frontend *fe;
2826
2827                 mtx_lock(&softc->ctl_lock);
2828                 printf("CTL Persistent Reservation information start:\n");
2829                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2830                         mtx_lock(&lun->lun_lock);
2831                         if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2832                                 mtx_unlock(&lun->lun_lock);
2833                                 continue;
2834                         }
2835
2836                         for (j = 0; j < ctl_max_ports; j++) {
2837                                 if (lun->pr_keys[j] == NULL)
2838                                         continue;
2839                                 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2840                                         if (lun->pr_keys[j][k] == 0)
2841                                                 continue;
2842                                         printf("  LUN %ju port %d iid %d key "
2843                                                "%#jx\n", lun->lun, j, k,
2844                                                (uintmax_t)lun->pr_keys[j][k]);
2845                                 }
2846                         }
2847                         mtx_unlock(&lun->lun_lock);
2848                 }
2849                 printf("CTL Persistent Reservation information end\n");
2850                 printf("CTL Ports:\n");
2851                 STAILQ_FOREACH(port, &softc->port_list, links) {
2852                         printf("  Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2853                                "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2854                                port->frontend->name, port->port_type,
2855                                port->physical_port, port->virtual_port,
2856                                (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2857                         for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2858                                 if (port->wwpn_iid[j].in_use == 0 &&
2859                                     port->wwpn_iid[j].wwpn == 0 &&
2860                                     port->wwpn_iid[j].name == NULL)
2861                                         continue;
2862
2863                                 printf("    iid %u use %d WWPN %#jx '%s'\n",
2864                                     j, port->wwpn_iid[j].in_use,
2865                                     (uintmax_t)port->wwpn_iid[j].wwpn,
2866                                     port->wwpn_iid[j].name);
2867                         }
2868                 }
2869                 printf("CTL Port information end\n");
2870                 mtx_unlock(&softc->ctl_lock);
2871                 /*
2872                  * XXX KDM calling this without a lock.  We'd likely want
2873                  * to drop the lock before calling the frontend's dump
2874                  * routine anyway.
2875                  */
2876                 printf("CTL Frontends:\n");
2877                 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2878                         printf("  Frontend '%s'\n", fe->name);
2879                         if (fe->fe_dump != NULL)
2880                                 fe->fe_dump();
2881                 }
2882                 printf("CTL Frontend information end\n");
2883                 break;
2884         }
2885         case CTL_LUN_REQ: {
2886                 struct ctl_lun_req *lun_req;
2887                 struct ctl_backend_driver *backend;
2888                 void *packed;
2889                 nvlist_t *tmp_args_nvl;
2890                 size_t packed_len;
2891
2892                 lun_req = (struct ctl_lun_req *)addr;
2893                 tmp_args_nvl = lun_req->args_nvl;
2894
2895                 backend = ctl_backend_find(lun_req->backend);
2896                 if (backend == NULL) {
2897                         lun_req->status = CTL_LUN_ERROR;
2898                         snprintf(lun_req->error_str,
2899                                  sizeof(lun_req->error_str),
2900                                  "Backend \"%s\" not found.",
2901                                  lun_req->backend);
2902                         break;
2903                 }
2904
2905                 if (lun_req->args != NULL) {
2906                         packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2907                         if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2908                                 free(packed, M_CTL);
2909                                 lun_req->status = CTL_LUN_ERROR;
2910                                 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2911                                     "Cannot copyin args.");
2912                                 break;
2913                         }
2914                         lun_req->args_nvl = nvlist_unpack(packed,
2915                             lun_req->args_len, 0);
2916                         free(packed, M_CTL);
2917
2918                         if (lun_req->args_nvl == NULL) {
2919                                 lun_req->status = CTL_LUN_ERROR;
2920                                 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2921                                     "Cannot unpack args nvlist.");
2922                                 break;
2923                         }
2924                 } else
2925                         lun_req->args_nvl = nvlist_create(0);
2926
2927                 retval = backend->ioctl(dev, cmd, addr, flag, td);
2928                 nvlist_destroy(lun_req->args_nvl);
2929                 lun_req->args_nvl = tmp_args_nvl;
2930
2931                 if (lun_req->result_nvl != NULL) {
2932                         if (lun_req->result != NULL) {
2933                                 packed = nvlist_pack(lun_req->result_nvl,
2934                                     &packed_len);
2935                                 if (packed == NULL) {
2936                                         lun_req->status = CTL_LUN_ERROR;
2937                                         snprintf(lun_req->error_str,
2938                                             sizeof(lun_req->error_str),
2939                                             "Cannot pack result nvlist.");
2940                                         break;
2941                                 }
2942
2943                                 if (packed_len > lun_req->result_len) {
2944                                         lun_req->status = CTL_LUN_ERROR;
2945                                         snprintf(lun_req->error_str,
2946                                             sizeof(lun_req->error_str),
2947                                             "Result nvlist too large.");
2948                                         free(packed, M_NVLIST);
2949                                         break;
2950                                 }
2951
2952                                 if (copyout(packed, lun_req->result, packed_len)) {
2953                                         lun_req->status = CTL_LUN_ERROR;
2954                                         snprintf(lun_req->error_str,
2955                                             sizeof(lun_req->error_str),
2956                                             "Cannot copyout() the result.");
2957                                         free(packed, M_NVLIST);
2958                                         break;
2959                                 }
2960
2961                                 lun_req->result_len = packed_len;
2962                                 free(packed, M_NVLIST);
2963                         }
2964
2965                         nvlist_destroy(lun_req->result_nvl);
2966                 }
2967                 break;
2968         }
2969         case CTL_LUN_LIST: {
2970                 struct sbuf *sb;
2971                 struct ctl_lun_list *list;
2972                 const char *name, *value;
2973                 void *cookie;
2974                 int type;
2975
2976                 list = (struct ctl_lun_list *)addr;
2977
2978                 /*
2979                  * Allocate a fixed length sbuf here, based on the length
2980                  * of the user's buffer.  We could allocate an auto-extending
2981                  * buffer, and then tell the user how much larger our
2982                  * amount of data is than his buffer, but that presents
2983                  * some problems:
2984                  *
2985                  * 1.  The sbuf(9) routines use a blocking malloc, and so
2986                  *     we can't hold a lock while calling them with an
2987                  *     auto-extending buffer.
2988                  *
2989                  * 2.  There is not currently a LUN reference counting
2990                  *     mechanism, outside of outstanding transactions on
2991                  *     the LUN's OOA queue.  So a LUN could go away on us
2992                  *     while we're getting the LUN number, backend-specific
2993                  *     information, etc.  Thus, given the way things
2994                  *     currently work, we need to hold the CTL lock while
2995                  *     grabbing LUN information.
2996                  *
2997                  * So, from the user's standpoint, the best thing to do is
2998                  * allocate what he thinks is a reasonable buffer length,
2999                  * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3000                  * double the buffer length and try again.  (And repeat
3001                  * that until he succeeds.)
3002                  */
3003                 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3004                 if (sb == NULL) {
3005                         list->status = CTL_LUN_LIST_ERROR;
3006                         snprintf(list->error_str, sizeof(list->error_str),
3007                                  "Unable to allocate %d bytes for LUN list",
3008                                  list->alloc_len);
3009                         break;
3010                 }
3011
3012                 sbuf_printf(sb, "<ctllunlist>\n");
3013
3014                 mtx_lock(&softc->ctl_lock);
3015                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3016                         mtx_lock(&lun->lun_lock);
3017                         retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3018                                              (uintmax_t)lun->lun);
3019
3020                         /*
3021                          * Bail out as soon as we see that we've overfilled
3022                          * the buffer.
3023                          */
3024                         if (retval != 0)
3025                                 break;
3026
3027                         retval = sbuf_printf(sb, "\t<backend_type>%s"
3028                                              "</backend_type>\n",
3029                                              (lun->backend == NULL) ?  "none" :
3030                                              lun->backend->name);
3031
3032                         if (retval != 0)
3033                                 break;
3034
3035                         retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3036                                              lun->be_lun->lun_type);
3037
3038                         if (retval != 0)
3039                                 break;
3040
3041                         if (lun->backend == NULL) {
3042                                 retval = sbuf_printf(sb, "</lun>\n");
3043                                 if (retval != 0)
3044                                         break;
3045                                 continue;
3046                         }
3047
3048                         retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3049                                              (lun->be_lun->maxlba > 0) ?
3050                                              lun->be_lun->maxlba + 1 : 0);
3051
3052                         if (retval != 0)
3053                                 break;
3054
3055                         retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3056                                              lun->be_lun->blocksize);
3057
3058                         if (retval != 0)
3059                                 break;
3060
3061                         retval = sbuf_printf(sb, "\t<serial_number>");
3062
3063                         if (retval != 0)
3064                                 break;
3065
3066                         retval = ctl_sbuf_printf_esc(sb,
3067                             lun->be_lun->serial_num,
3068                             sizeof(lun->be_lun->serial_num));
3069
3070                         if (retval != 0)
3071                                 break;
3072
3073                         retval = sbuf_printf(sb, "</serial_number>\n");
3074                 
3075                         if (retval != 0)
3076                                 break;
3077
3078                         retval = sbuf_printf(sb, "\t<device_id>");
3079
3080                         if (retval != 0)
3081                                 break;
3082
3083                         retval = ctl_sbuf_printf_esc(sb,
3084                             lun->be_lun->device_id,
3085                             sizeof(lun->be_lun->device_id));
3086
3087                         if (retval != 0)
3088                                 break;
3089
3090                         retval = sbuf_printf(sb, "</device_id>\n");
3091
3092                         if (retval != 0)
3093                                 break;
3094
3095                         if (lun->backend->lun_info != NULL) {
3096                                 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3097                                 if (retval != 0)
3098                                         break;
3099                         }
3100
3101                         cookie = NULL;
3102                         while ((name = nvlist_next(lun->be_lun->options, &type,
3103                             &cookie)) != NULL) {
3104                                 sbuf_printf(sb, "\t<%s>", name);
3105
3106                                 if (type == NV_TYPE_STRING) {
3107                                         value = dnvlist_get_string(
3108                                             lun->be_lun->options, name, NULL);
3109                                         if (value != NULL)
3110                                                 sbuf_printf(sb, "%s", value);
3111                                 }
3112
3113                                 sbuf_printf(sb, "</%s>\n", name);
3114                         }
3115
3116                         retval = sbuf_printf(sb, "</lun>\n");
3117
3118                         if (retval != 0)
3119                                 break;
3120                         mtx_unlock(&lun->lun_lock);
3121                 }
3122                 if (lun != NULL)
3123                         mtx_unlock(&lun->lun_lock);
3124                 mtx_unlock(&softc->ctl_lock);
3125
3126                 if ((retval != 0)
3127                  || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3128                         retval = 0;
3129                         sbuf_delete(sb);
3130                         list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3131                         snprintf(list->error_str, sizeof(list->error_str),
3132                                  "Out of space, %d bytes is too small",
3133                                  list->alloc_len);
3134                         break;
3135                 }
3136
3137                 sbuf_finish(sb);
3138
3139                 retval = copyout(sbuf_data(sb), list->lun_xml,
3140                                  sbuf_len(sb) + 1);
3141
3142                 list->fill_len = sbuf_len(sb) + 1;
3143                 list->status = CTL_LUN_LIST_OK;
3144                 sbuf_delete(sb);
3145                 break;
3146         }
3147         case CTL_ISCSI: {
3148                 struct ctl_iscsi *ci;
3149                 struct ctl_frontend *fe;
3150
3151                 ci = (struct ctl_iscsi *)addr;
3152
3153                 fe = ctl_frontend_find("iscsi");
3154                 if (fe == NULL) {
3155                         ci->status = CTL_ISCSI_ERROR;
3156                         snprintf(ci->error_str, sizeof(ci->error_str),
3157                             "Frontend \"iscsi\" not found.");
3158                         break;
3159                 }
3160
3161                 retval = fe->ioctl(dev, cmd, addr, flag, td);
3162                 break;
3163         }
3164         case CTL_PORT_REQ: {
3165                 struct ctl_req *req;
3166                 struct ctl_frontend *fe;
3167                 void *packed;
3168                 nvlist_t *tmp_args_nvl;
3169                 size_t packed_len;
3170
3171                 req = (struct ctl_req *)addr;
3172                 tmp_args_nvl = req->args_nvl;
3173
3174                 fe = ctl_frontend_find(req->driver);
3175                 if (fe == NULL) {
3176                         req->status = CTL_LUN_ERROR;
3177                         snprintf(req->error_str, sizeof(req->error_str),
3178                             "Frontend \"%s\" not found.", req->driver);
3179                         break;
3180                 }
3181
3182                 if (req->args != NULL) {
3183                         packed = malloc(req->args_len, M_CTL, M_WAITOK);
3184                         if (copyin(req->args, packed, req->args_len) != 0) {
3185                                 free(packed, M_CTL);
3186                                 req->status = CTL_LUN_ERROR;
3187                                 snprintf(req->error_str, sizeof(req->error_str),
3188                                     "Cannot copyin args.");
3189                                 break;
3190                         }
3191                         req->args_nvl = nvlist_unpack(packed,
3192                             req->args_len, 0);
3193                         free(packed, M_CTL);
3194
3195                         if (req->args_nvl == NULL) {
3196                                 req->status = CTL_LUN_ERROR;
3197                                 snprintf(req->error_str, sizeof(req->error_str),
3198                                     "Cannot unpack args nvlist.");
3199                                 break;
3200                         }
3201                 } else
3202                         req->args_nvl = nvlist_create(0);
3203
3204                 if (fe->ioctl)
3205                         retval = fe->ioctl(dev, cmd, addr, flag, td);
3206                 else
3207                         retval = ENODEV;
3208
3209                 nvlist_destroy(req->args_nvl);
3210                 req->args_nvl = tmp_args_nvl;
3211
3212                 if (req->result_nvl != NULL) {
3213                         if (req->result != NULL) {
3214                                 packed = nvlist_pack(req->result_nvl,
3215                                     &packed_len);
3216                                 if (packed == NULL) {
3217                                         req->status = CTL_LUN_ERROR;
3218                                         snprintf(req->error_str,
3219                                             sizeof(req->error_str),
3220                                             "Cannot pack result nvlist.");
3221                                         break;
3222                                 }
3223
3224                                 if (packed_len > req->result_len) {
3225                                         req->status = CTL_LUN_ERROR;
3226                                         snprintf(req->error_str,
3227                                             sizeof(req->error_str),
3228                                             "Result nvlist too large.");
3229                                         free(packed, M_NVLIST);
3230                                         break;
3231                                 }
3232
3233                                 if (copyout(packed, req->result, packed_len)) {
3234                                         req->status = CTL_LUN_ERROR;
3235                                         snprintf(req->error_str,
3236                                             sizeof(req->error_str),
3237                                             "Cannot copyout() the result.");
3238                                         free(packed, M_NVLIST);
3239                                         break;
3240                                 }
3241
3242                                 req->result_len = packed_len;
3243                                 free(packed, M_NVLIST);
3244                         }
3245
3246                         nvlist_destroy(req->result_nvl);
3247                 }
3248                 break;
3249         }
3250         case CTL_PORT_LIST: {
3251                 struct sbuf *sb;
3252                 struct ctl_port *port;
3253                 struct ctl_lun_list *list;
3254                 const char *name, *value;
3255                 void *cookie;
3256                 int j, type;
3257                 uint32_t plun;
3258
3259                 list = (struct ctl_lun_list *)addr;
3260
3261                 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3262                 if (sb == NULL) {
3263                         list->status = CTL_LUN_LIST_ERROR;
3264                         snprintf(list->error_str, sizeof(list->error_str),
3265                                  "Unable to allocate %d bytes for LUN list",
3266                                  list->alloc_len);
3267                         break;
3268                 }
3269
3270                 sbuf_printf(sb, "<ctlportlist>\n");
3271
3272                 mtx_lock(&softc->ctl_lock);
3273                 STAILQ_FOREACH(port, &softc->port_list, links) {
3274                         retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3275                                              (uintmax_t)port->targ_port);
3276
3277                         /*
3278                          * Bail out as soon as we see that we've overfilled
3279                          * the buffer.
3280                          */
3281                         if (retval != 0)
3282                                 break;
3283
3284                         retval = sbuf_printf(sb, "\t<frontend_type>%s"
3285                             "</frontend_type>\n", port->frontend->name);
3286                         if (retval != 0)
3287                                 break;
3288
3289                         retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3290                                              port->port_type);
3291                         if (retval != 0)
3292                                 break;
3293
3294                         retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3295                             (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3296                         if (retval != 0)
3297                                 break;
3298
3299                         retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3300                             port->port_name);
3301                         if (retval != 0)
3302                                 break;
3303
3304                         retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3305                             port->physical_port);
3306                         if (retval != 0)
3307                                 break;
3308
3309                         retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3310                             port->virtual_port);
3311                         if (retval != 0)
3312                                 break;
3313
3314                         if (port->target_devid != NULL) {
3315                                 sbuf_printf(sb, "\t<target>");
3316                                 ctl_id_sbuf(port->target_devid, sb);
3317                                 sbuf_printf(sb, "</target>\n");
3318                         }
3319
3320                         if (port->port_devid != NULL) {
3321                                 sbuf_printf(sb, "\t<port>");
3322                                 ctl_id_sbuf(port->port_devid, sb);
3323                                 sbuf_printf(sb, "</port>\n");
3324                         }
3325
3326                         if (port->port_info != NULL) {
3327                                 retval = port->port_info(port->onoff_arg, sb);
3328                                 if (retval != 0)
3329                                         break;
3330                         }
3331
3332                         cookie = NULL;
3333                         while ((name = nvlist_next(port->options, &type,
3334                             &cookie)) != NULL) {
3335                                 sbuf_printf(sb, "\t<%s>", name);
3336
3337                                 if (type == NV_TYPE_STRING) {
3338                                         value = dnvlist_get_string(port->options,
3339                                             name, NULL);
3340                                         if (value != NULL)
3341                                                 sbuf_printf(sb, "%s", value);
3342                                 }
3343
3344                                 sbuf_printf(sb, "</%s>\n", name);
3345                         }
3346
3347                         if (port->lun_map != NULL) {
3348                                 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3349                                 for (j = 0; j < port->lun_map_size; j++) {
3350                                         plun = ctl_lun_map_from_port(port, j);
3351                                         if (plun == UINT32_MAX)
3352                                                 continue;
3353                                         sbuf_printf(sb,
3354                                             "\t<lun id=\"%u\">%u</lun>\n",
3355                                             j, plun);
3356                                 }
3357                         }
3358
3359                         for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3360                                 if (port->wwpn_iid[j].in_use == 0 ||
3361                                     (port->wwpn_iid[j].wwpn == 0 &&
3362                                      port->wwpn_iid[j].name == NULL))
3363                                         continue;
3364
3365                                 if (port->wwpn_iid[j].name != NULL)
3366                                         retval = sbuf_printf(sb,
3367                                             "\t<initiator id=\"%u\">%s</initiator>\n",
3368                                             j, port->wwpn_iid[j].name);
3369                                 else
3370                                         retval = sbuf_printf(sb,
3371                                             "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3372                                             j, port->wwpn_iid[j].wwpn);
3373                                 if (retval != 0)
3374                                         break;
3375                         }
3376                         if (retval != 0)
3377                                 break;
3378
3379                         retval = sbuf_printf(sb, "</targ_port>\n");
3380                         if (retval != 0)
3381                                 break;
3382                 }
3383                 mtx_unlock(&softc->ctl_lock);
3384
3385                 if ((retval != 0)
3386                  || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3387                         retval = 0;
3388                         sbuf_delete(sb);
3389                         list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3390                         snprintf(list->error_str, sizeof(list->error_str),
3391                                  "Out of space, %d bytes is too small",
3392                                  list->alloc_len);
3393                         break;
3394                 }
3395
3396                 sbuf_finish(sb);
3397
3398                 retval = copyout(sbuf_data(sb), list->lun_xml,
3399                                  sbuf_len(sb) + 1);
3400
3401                 list->fill_len = sbuf_len(sb) + 1;
3402                 list->status = CTL_LUN_LIST_OK;
3403                 sbuf_delete(sb);
3404                 break;
3405         }
3406         case CTL_LUN_MAP: {
3407                 struct ctl_lun_map *lm  = (struct ctl_lun_map *)addr;
3408                 struct ctl_port *port;
3409
3410                 mtx_lock(&softc->ctl_lock);
3411                 if (lm->port < softc->port_min ||
3412                     lm->port >= softc->port_max ||
3413                     (port = softc->ctl_ports[lm->port]) == NULL) {
3414                         mtx_unlock(&softc->ctl_lock);
3415                         return (ENXIO);
3416                 }
3417                 if (port->status & CTL_PORT_STATUS_ONLINE) {
3418                         STAILQ_FOREACH(lun, &softc->lun_list, links) {
3419                                 if (ctl_lun_map_to_port(port, lun->lun) ==
3420                                     UINT32_MAX)
3421                                         continue;
3422                                 mtx_lock(&lun->lun_lock);
3423                                 ctl_est_ua_port(lun, lm->port, -1,
3424                                     CTL_UA_LUN_CHANGE);
3425                                 mtx_unlock(&lun->lun_lock);
3426                         }
3427                 }
3428                 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3429                 if (lm->plun != UINT32_MAX) {
3430                         if (lm->lun == UINT32_MAX)
3431                                 retval = ctl_lun_map_unset(port, lm->plun);
3432                         else if (lm->lun < ctl_max_luns &&
3433                             softc->ctl_luns[lm->lun] != NULL)
3434                                 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3435                         else
3436                                 return (ENXIO);
3437                 } else {
3438                         if (lm->lun == UINT32_MAX)
3439                                 retval = ctl_lun_map_deinit(port);
3440                         else
3441                                 retval = ctl_lun_map_init(port);
3442                 }
3443                 if (port->status & CTL_PORT_STATUS_ONLINE)
3444                         ctl_isc_announce_port(port);
3445                 break;
3446         }
3447         case CTL_GET_LUN_STATS: {
3448                 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3449                 int i;
3450
3451                 /*
3452                  * XXX KDM no locking here.  If the LUN list changes,
3453                  * things can blow up.
3454                  */
3455                 i = 0;
3456                 stats->status = CTL_SS_OK;
3457                 stats->fill_len = 0;
3458                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3459                         if (lun->lun < stats->first_item)
3460                                 continue;
3461                         if (stats->fill_len + sizeof(lun->stats) >
3462                             stats->alloc_len) {
3463                                 stats->status = CTL_SS_NEED_MORE_SPACE;
3464                                 break;
3465                         }
3466                         retval = copyout(&lun->stats, &stats->stats[i++],
3467                                          sizeof(lun->stats));
3468                         if (retval != 0)
3469                                 break;
3470                         stats->fill_len += sizeof(lun->stats);
3471                 }
3472                 stats->num_items = softc->num_luns;
3473                 stats->flags = CTL_STATS_FLAG_NONE;
3474 #ifdef CTL_TIME_IO
3475                 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3476 #endif
3477                 getnanouptime(&stats->timestamp);
3478                 break;
3479         }
3480         case CTL_GET_PORT_STATS: {
3481                 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3482                 int i;
3483
3484                 /*
3485                  * XXX KDM no locking here.  If the LUN list changes,
3486                  * things can blow up.
3487                  */
3488                 i = 0;
3489                 stats->status = CTL_SS_OK;
3490                 stats->fill_len = 0;
3491                 STAILQ_FOREACH(port, &softc->port_list, links) {
3492                         if (port->targ_port < stats->first_item)
3493                                 continue;
3494                         if (stats->fill_len + sizeof(port->stats) >
3495                             stats->alloc_len) {
3496                                 stats->status = CTL_SS_NEED_MORE_SPACE;
3497                                 break;
3498                         }
3499                         retval = copyout(&port->stats, &stats->stats[i++],
3500                                          sizeof(port->stats));
3501                         if (retval != 0)
3502                                 break;
3503                         stats->fill_len += sizeof(port->stats);
3504                 }
3505                 stats->num_items = softc->num_ports;
3506                 stats->flags = CTL_STATS_FLAG_NONE;
3507 #ifdef CTL_TIME_IO
3508                 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3509 #endif
3510                 getnanouptime(&stats->timestamp);
3511                 break;
3512         }
3513         default: {
3514                 /* XXX KDM should we fix this? */
3515 #if 0
3516                 struct ctl_backend_driver *backend;
3517                 unsigned int type;
3518                 int found;
3519
3520                 found = 0;
3521
3522                 /*
3523                  * We encode the backend type as the ioctl type for backend
3524                  * ioctls.  So parse it out here, and then search for a
3525                  * backend of this type.
3526                  */
3527                 type = _IOC_TYPE(cmd);
3528
3529                 STAILQ_FOREACH(backend, &softc->be_list, links) {
3530                         if (backend->type == type) {
3531                                 found = 1;
3532                                 break;
3533                         }
3534                 }
3535                 if (found == 0) {
3536                         printf("ctl: unknown ioctl command %#lx or backend "
3537                                "%d\n", cmd, type);
3538                         retval = EINVAL;
3539                         break;
3540                 }
3541                 retval = backend->ioctl(dev, cmd, addr, flag, td);
3542 #endif
3543                 retval = ENOTTY;
3544                 break;
3545         }
3546         }
3547         return (retval);
3548 }
3549
3550 uint32_t
3551 ctl_get_initindex(struct ctl_nexus *nexus)
3552 {
3553         return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3554 }
3555
3556 int
3557 ctl_lun_map_init(struct ctl_port *port)
3558 {
3559         struct ctl_softc *softc = port->ctl_softc;
3560         struct ctl_lun *lun;
3561         int size = ctl_lun_map_size;
3562         uint32_t i;
3563
3564         if (port->lun_map == NULL || port->lun_map_size < size) {
3565                 port->lun_map_size = 0;
3566                 free(port->lun_map, M_CTL);
3567                 port->lun_map = malloc(size * sizeof(uint32_t),
3568                     M_CTL, M_NOWAIT);
3569         }
3570         if (port->lun_map == NULL)
3571                 return (ENOMEM);
3572         for (i = 0; i < size; i++)
3573                 port->lun_map[i] = UINT32_MAX;
3574         port->lun_map_size = size;
3575         if (port->status & CTL_PORT_STATUS_ONLINE) {
3576                 if (port->lun_disable != NULL) {
3577                         STAILQ_FOREACH(lun, &softc->lun_list, links)
3578                                 port->lun_disable(port->targ_lun_arg, lun->lun);
3579                 }
3580                 ctl_isc_announce_port(port);
3581         }
3582         return (0);
3583 }
3584
3585 int
3586 ctl_lun_map_deinit(struct ctl_port *port)
3587 {
3588         struct ctl_softc *softc = port->ctl_softc;
3589         struct ctl_lun *lun;
3590
3591         if (port->lun_map == NULL)
3592                 return (0);
3593         port->lun_map_size = 0;
3594         free(port->lun_map, M_CTL);
3595         port->lun_map = NULL;
3596         if (port->status & CTL_PORT_STATUS_ONLINE) {
3597                 if (port->lun_enable != NULL) {
3598                         STAILQ_FOREACH(lun, &softc->lun_list, links)
3599                                 port->lun_enable(port->targ_lun_arg, lun->lun);
3600                 }
3601                 ctl_isc_announce_port(port);
3602         }
3603         return (0);
3604 }
3605
3606 int
3607 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3608 {
3609         int status;
3610         uint32_t old;
3611
3612         if (port->lun_map == NULL) {
3613                 status = ctl_lun_map_init(port);
3614                 if (status != 0)
3615                         return (status);
3616         }
3617         if (plun >= port->lun_map_size)
3618                 return (EINVAL);
3619         old = port->lun_map[plun];
3620         port->lun_map[plun] = glun;
3621         if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3622                 if (port->lun_enable != NULL)
3623                         port->lun_enable(port->targ_lun_arg, plun);
3624                 ctl_isc_announce_port(port);
3625         }
3626         return (0);
3627 }
3628
3629 int
3630 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3631 {
3632         uint32_t old;
3633
3634         if (port->lun_map == NULL || plun >= port->lun_map_size)
3635                 return (0);
3636         old = port->lun_map[plun];
3637         port->lun_map[plun] = UINT32_MAX;
3638         if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3639                 if (port->lun_disable != NULL)
3640                         port->lun_disable(port->targ_lun_arg, plun);
3641                 ctl_isc_announce_port(port);
3642         }
3643         return (0);
3644 }
3645
3646 uint32_t
3647 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3648 {
3649
3650         if (port == NULL)
3651                 return (UINT32_MAX);
3652         if (port->lun_map == NULL)
3653                 return (lun_id);
3654         if (lun_id > port->lun_map_size)
3655                 return (UINT32_MAX);
3656         return (port->lun_map[lun_id]);
3657 }
3658
3659 uint32_t
3660 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3661 {
3662         uint32_t i;
3663
3664         if (port == NULL)
3665                 return (UINT32_MAX);
3666         if (port->lun_map == NULL)
3667                 return (lun_id);
3668         for (i = 0; i < port->lun_map_size; i++) {
3669                 if (port->lun_map[i] == lun_id)
3670                         return (i);
3671         }
3672         return (UINT32_MAX);
3673 }
3674
3675 uint32_t
3676 ctl_decode_lun(uint64_t encoded)
3677 {
3678         uint8_t lun[8];
3679         uint32_t result = 0xffffffff;
3680
3681         be64enc(lun, encoded);
3682         switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3683         case RPL_LUNDATA_ATYP_PERIPH:
3684                 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3685                     lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3686                         result = lun[1];
3687                 break;
3688         case RPL_LUNDATA_ATYP_FLAT:
3689                 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3690                     lun[6] == 0 && lun[7] == 0)
3691                         result = ((lun[0] & 0x3f) << 8) + lun[1];
3692                 break;
3693         case RPL_LUNDATA_ATYP_EXTLUN:
3694                 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3695                 case 0x02:
3696                         switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3697                         case 0x00:
3698                                 result = lun[1];
3699                                 break;
3700                         case 0x10:
3701                                 result = (lun[1] << 16) + (lun[2] << 8) +
3702                                     lun[3];
3703                                 break;
3704                         case 0x20:
3705                                 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3706                                         result = (lun[2] << 24) +
3707                                             (lun[3] << 16) + (lun[4] << 8) +
3708                                             lun[5];
3709                                 break;
3710                         }
3711                         break;
3712                 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3713                         result = 0xffffffff;
3714                         break;
3715                 }
3716                 break;
3717         }
3718         return (result);
3719 }
3720
3721 uint64_t
3722 ctl_encode_lun(uint32_t decoded)
3723 {
3724         uint64_t l = decoded;
3725
3726         if (l <= 0xff)
3727                 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3728         if (l <= 0x3fff)
3729                 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3730         if (l <= 0xffffff)
3731                 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3732                     (l << 32));
3733         return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3734 }
3735
3736 int
3737 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3738 {
3739         int i;
3740
3741         for (i = first; i < last; i++) {
3742                 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3743                         return (i);
3744         }
3745         return (-1);
3746 }
3747
3748 int
3749 ctl_set_mask(uint32_t *mask, uint32_t bit)
3750 {
3751         uint32_t chunk, piece;
3752
3753         chunk = bit >> 5;
3754         piece = bit % (sizeof(uint32_t) * 8);
3755
3756         if ((mask[chunk] & (1 << piece)) != 0)
3757                 return (-1);
3758         else
3759                 mask[chunk] |= (1 << piece);
3760
3761         return (0);
3762 }
3763
3764 int
3765 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3766 {
3767         uint32_t chunk, piece;
3768
3769         chunk = bit >> 5;
3770         piece = bit % (sizeof(uint32_t) * 8);
3771
3772         if ((mask[chunk] & (1 << piece)) == 0)
3773                 return (-1);
3774         else
3775                 mask[chunk] &= ~(1 << piece);
3776
3777         return (0);
3778 }
3779
3780 int
3781 ctl_is_set(uint32_t *mask, uint32_t bit)
3782 {
3783         uint32_t chunk, piece;
3784
3785         chunk = bit >> 5;
3786         piece = bit % (sizeof(uint32_t) * 8);
3787
3788         if ((mask[chunk] & (1 << piece)) == 0)
3789                 return (0);
3790         else
3791                 return (1);
3792 }
3793
3794 static uint64_t
3795 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3796 {
3797         uint64_t *t;
3798
3799         t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3800         if (t == NULL)
3801                 return (0);
3802         return (t[residx % CTL_MAX_INIT_PER_PORT]);
3803 }
3804
3805 static void
3806 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3807 {
3808         uint64_t *t;
3809
3810         t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3811         if (t == NULL)
3812                 return;
3813         t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3814 }
3815
3816 static void
3817 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3818 {
3819         uint64_t *p;
3820         u_int i;
3821
3822         i = residx/CTL_MAX_INIT_PER_PORT;
3823         if (lun->pr_keys[i] != NULL)
3824                 return;
3825         mtx_unlock(&lun->lun_lock);
3826         p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3827             M_WAITOK | M_ZERO);
3828         mtx_lock(&lun->lun_lock);
3829         if (lun->pr_keys[i] == NULL)
3830                 lun->pr_keys[i] = p;
3831         else
3832                 free(p, M_CTL);
3833 }
3834
3835 static void
3836 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3837 {
3838         uint64_t *t;
3839
3840         t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3841         KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3842         t[residx % CTL_MAX_INIT_PER_PORT] = key;
3843 }
3844
3845 /*
3846  * ctl_softc, pool_name, total_ctl_io are passed in.
3847  * npool is passed out.
3848  */
3849 int
3850 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3851                 uint32_t total_ctl_io, void **npool)
3852 {
3853         struct ctl_io_pool *pool;
3854
3855         pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3856                                             M_NOWAIT | M_ZERO);
3857         if (pool == NULL)
3858                 return (ENOMEM);
3859
3860         snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3861         pool->ctl_softc = ctl_softc;
3862 #ifdef IO_POOLS
3863         pool->zone = uma_zsecond_create(pool->name, NULL,
3864             NULL, NULL, NULL, ctl_softc->io_zone);
3865         /* uma_prealloc(pool->zone, total_ctl_io); */
3866 #else
3867         pool->zone = ctl_softc->io_zone;
3868 #endif
3869
3870         *npool = pool;
3871         return (0);
3872 }
3873
3874 void
3875 ctl_pool_free(struct ctl_io_pool *pool)
3876 {
3877
3878         if (pool == NULL)
3879                 return;
3880
3881 #ifdef IO_POOLS
3882         uma_zdestroy(pool->zone);
3883 #endif
3884         free(pool, M_CTL);
3885 }
3886
3887 union ctl_io *
3888 ctl_alloc_io(void *pool_ref)
3889 {
3890         struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3891         union ctl_io *io;
3892
3893         io = uma_zalloc(pool->zone, M_WAITOK);
3894         if (io != NULL) {
3895                 io->io_hdr.pool = pool_ref;
3896                 CTL_SOFTC(io) = pool->ctl_softc;
3897                 TAILQ_INIT(&io->io_hdr.blocked_queue);
3898         }
3899         return (io);
3900 }
3901
3902 union ctl_io *
3903 ctl_alloc_io_nowait(void *pool_ref)
3904 {
3905         struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3906         union ctl_io *io;
3907
3908         io = uma_zalloc(pool->zone, M_NOWAIT);
3909         if (io != NULL) {
3910                 io->io_hdr.pool = pool_ref;
3911                 CTL_SOFTC(io) = pool->ctl_softc;
3912                 TAILQ_INIT(&io->io_hdr.blocked_queue);
3913         }
3914         return (io);
3915 }
3916
3917 void
3918 ctl_free_io(union ctl_io *io)
3919 {
3920         struct ctl_io_pool *pool;
3921
3922         if (io == NULL)
3923                 return;
3924
3925         pool = (struct ctl_io_pool *)io->io_hdr.pool;
3926         uma_zfree(pool->zone, io);
3927 }
3928
3929 void
3930 ctl_zero_io(union ctl_io *io)
3931 {
3932         struct ctl_io_pool *pool;
3933
3934         if (io == NULL)
3935                 return;
3936
3937         /*
3938          * May need to preserve linked list pointers at some point too.
3939          */
3940         pool = io->io_hdr.pool;
3941         memset(io, 0, sizeof(*io));
3942         io->io_hdr.pool = pool;
3943         CTL_SOFTC(io) = pool->ctl_softc;
3944         TAILQ_INIT(&io->io_hdr.blocked_queue);
3945 }
3946
3947 int
3948 ctl_expand_number(const char *buf, uint64_t *num)
3949 {
3950         char *endptr;
3951         uint64_t number;
3952         unsigned shift;
3953
3954         number = strtoq(buf, &endptr, 0);
3955
3956         switch (tolower((unsigned char)*endptr)) {
3957         case 'e':
3958                 shift = 60;
3959                 break;
3960         case 'p':
3961                 shift = 50;
3962                 break;
3963         case 't':
3964                 shift = 40;
3965                 break;
3966         case 'g':
3967                 shift = 30;
3968                 break;
3969         case 'm':
3970                 shift = 20;
3971                 break;
3972         case 'k':
3973                 shift = 10;
3974                 break;
3975         case 'b':
3976         case '\0': /* No unit. */
3977                 *num = number;
3978                 return (0);
3979         default:
3980                 /* Unrecognized unit. */
3981                 return (-1);
3982         }
3983
3984         if ((number << shift) >> shift != number) {
3985                 /* Overflow */
3986                 return (-1);
3987         }
3988         *num = number << shift;
3989         return (0);
3990 }
3991
3992
3993 /*
3994  * This routine could be used in the future to load default and/or saved
3995  * mode page parameters for a particuar lun.
3996  */
3997 static int
3998 ctl_init_page_index(struct ctl_lun *lun)
3999 {
4000         int i, page_code;
4001         struct ctl_page_index *page_index;
4002         const char *value;
4003         uint64_t ival;
4004
4005         memcpy(&lun->mode_pages.index, page_index_template,
4006                sizeof(page_index_template));
4007
4008         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4009
4010                 page_index = &lun->mode_pages.index[i];
4011                 if (lun->be_lun->lun_type == T_DIRECT &&
4012                     (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4013                         continue;
4014                 if (lun->be_lun->lun_type == T_PROCESSOR &&
4015                     (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4016                         continue;
4017                 if (lun->be_lun->lun_type == T_CDROM &&
4018                     (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4019                         continue;
4020
4021                 page_code = page_index->page_code & SMPH_PC_MASK;
4022                 switch (page_code) {
4023                 case SMS_RW_ERROR_RECOVERY_PAGE: {
4024                         KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4025                             ("subpage %#x for page %#x is incorrect!",
4026                             page_index->subpage, page_code));
4027                         memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4028                                &rw_er_page_default,
4029                                sizeof(rw_er_page_default));
4030                         memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4031                                &rw_er_page_changeable,
4032                                sizeof(rw_er_page_changeable));
4033                         memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4034                                &rw_er_page_default,
4035                                sizeof(rw_er_page_default));
4036                         memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4037                                &rw_er_page_default,
4038                                sizeof(rw_er_page_default));
4039                         page_index->page_data =
4040                                 (uint8_t *)lun->mode_pages.rw_er_page;
4041                         break;
4042                 }
4043                 case SMS_FORMAT_DEVICE_PAGE: {
4044                         struct scsi_format_page *format_page;
4045
4046                         KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4047                             ("subpage %#x for page %#x is incorrect!",
4048                             page_index->subpage, page_code));
4049
4050                         /*
4051                          * Sectors per track are set above.  Bytes per
4052                          * sector need to be set here on a per-LUN basis.
4053                          */
4054                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4055                                &format_page_default,
4056                                sizeof(format_page_default));
4057                         memcpy(&lun->mode_pages.format_page[
4058                                CTL_PAGE_CHANGEABLE], &format_page_changeable,
4059                                sizeof(format_page_changeable));
4060                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4061                                &format_page_default,
4062                                sizeof(format_page_default));
4063                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4064                                &format_page_default,
4065                                sizeof(format_page_default));
4066
4067                         format_page = &lun->mode_pages.format_page[
4068                                 CTL_PAGE_CURRENT];
4069                         scsi_ulto2b(lun->be_lun->blocksize,
4070                                     format_page->bytes_per_sector);
4071
4072                         format_page = &lun->mode_pages.format_page[
4073                                 CTL_PAGE_DEFAULT];
4074                         scsi_ulto2b(lun->be_lun->blocksize,
4075                                     format_page->bytes_per_sector);
4076
4077                         format_page = &lun->mode_pages.format_page[
4078                                 CTL_PAGE_SAVED];
4079                         scsi_ulto2b(lun->be_lun->blocksize,
4080                                     format_page->bytes_per_sector);
4081
4082                         page_index->page_data =
4083                                 (uint8_t *)lun->mode_pages.format_page;
4084                         break;
4085                 }
4086                 case SMS_RIGID_DISK_PAGE: {
4087                         struct scsi_rigid_disk_page *rigid_disk_page;
4088                         uint32_t sectors_per_cylinder;
4089                         uint64_t cylinders;
4090 #ifndef __XSCALE__
4091                         int shift;
4092 #endif /* !__XSCALE__ */
4093
4094                         KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4095                             ("subpage %#x for page %#x is incorrect!",
4096                             page_index->subpage, page_code));
4097
4098                         /*
4099                          * Rotation rate and sectors per track are set
4100                          * above.  We calculate the cylinders here based on
4101                          * capacity.  Due to the number of heads and
4102                          * sectors per track we're using, smaller arrays
4103                          * may turn out to have 0 cylinders.  Linux and
4104                          * FreeBSD don't pay attention to these mode pages
4105                          * to figure out capacity, but Solaris does.  It
4106                          * seems to deal with 0 cylinders just fine, and
4107                          * works out a fake geometry based on the capacity.
4108                          */
4109                         memcpy(&lun->mode_pages.rigid_disk_page[
4110                                CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4111                                sizeof(rigid_disk_page_default));
4112                         memcpy(&lun->mode_pages.rigid_disk_page[
4113                                CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4114                                sizeof(rigid_disk_page_changeable));
4115
4116                         sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4117                                 CTL_DEFAULT_HEADS;
4118
4119                         /*
4120                          * The divide method here will be more accurate,
4121                          * probably, but results in floating point being
4122                          * used in the kernel on i386 (__udivdi3()).  On the
4123                          * XScale, though, __udivdi3() is implemented in
4124                          * software.
4125                          *
4126                          * The shift method for cylinder calculation is
4127                          * accurate if sectors_per_cylinder is a power of
4128                          * 2.  Otherwise it might be slightly off -- you
4129                          * might have a bit of a truncation problem.
4130                          */
4131 #ifdef  __XSCALE__
4132                         cylinders = (lun->be_lun->maxlba + 1) /
4133                                 sectors_per_cylinder;
4134 #else
4135                         for (shift = 31; shift > 0; shift--) {
4136                                 if (sectors_per_cylinder & (1 << shift))
4137                                         break;
4138                         }
4139                         cylinders = (lun->be_lun->maxlba + 1) >> shift;
4140 #endif
4141
4142                         /*
4143                          * We've basically got 3 bytes, or 24 bits for the
4144                          * cylinder size in the mode page.  If we're over,
4145                          * just round down to 2^24.
4146                          */
4147                         if (cylinders > 0xffffff)
4148                                 cylinders = 0xffffff;
4149
4150                         rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4151                                 CTL_PAGE_DEFAULT];
4152                         scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4153
4154                         if ((value = dnvlist_get_string(lun->be_lun->options,
4155                             "rpm", NULL)) != NULL) {
4156                                 scsi_ulto2b(strtol(value, NULL, 0),
4157                                      rigid_disk_page->rotation_rate);
4158                         }
4159
4160                         memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4161                                &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4162                                sizeof(rigid_disk_page_default));
4163                         memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4164                                &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4165                                sizeof(rigid_disk_page_default));
4166
4167                         page_index->page_data =
4168                                 (uint8_t *)lun->mode_pages.rigid_disk_page;
4169                         break;
4170                 }
4171                 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4172                         KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4173                             ("subpage %#x for page %#x is incorrect!",
4174                             page_index->subpage, page_code));
4175                         memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4176                                &verify_er_page_default,
4177                                sizeof(verify_er_page_default));
4178                         memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4179                                &verify_er_page_changeable,
4180                                sizeof(verify_er_page_changeable));
4181                         memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4182                                &verify_er_page_default,
4183                                sizeof(verify_er_page_default));
4184                         memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4185                                &verify_er_page_default,
4186                                sizeof(verify_er_page_default));
4187                         page_index->page_data =
4188                                 (uint8_t *)lun->mode_pages.verify_er_page;
4189                         break;
4190                 }
4191                 case SMS_CACHING_PAGE: {
4192                         struct scsi_caching_page *caching_page;
4193
4194                         KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4195                             ("subpage %#x for page %#x is incorrect!",
4196                             page_index->subpage, page_code));
4197                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4198                                &caching_page_default,
4199                                sizeof(caching_page_default));
4200                         memcpy(&lun->mode_pages.caching_page[
4201                                CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4202                                sizeof(caching_page_changeable));
4203                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4204                                &caching_page_default,
4205                                sizeof(caching_page_default));
4206                         caching_page = &lun->mode_pages.caching_page[
4207                             CTL_PAGE_SAVED];
4208                         value = dnvlist_get_string(lun->be_lun->options,
4209                             "writecache", NULL);
4210                         if (value != NULL && strcmp(value, "off") == 0)
4211                                 caching_page->flags1 &= ~SCP_WCE;
4212                         value = dnvlist_get_string(lun->be_lun->options,
4213                             "readcache", NULL);
4214                         if (value != NULL && strcmp(value, "off") == 0)
4215                                 caching_page->flags1 |= SCP_RCD;
4216                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4217                                &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4218                                sizeof(caching_page_default));
4219                         page_index->page_data =
4220                                 (uint8_t *)lun->mode_pages.caching_page;
4221                         break;
4222                 }
4223                 case SMS_CONTROL_MODE_PAGE: {
4224                         switch (page_index->subpage) {
4225                         case SMS_SUBPAGE_PAGE_0: {
4226                                 struct scsi_control_page *control_page;
4227
4228                                 memcpy(&lun->mode_pages.control_page[
4229                                     CTL_PAGE_DEFAULT],
4230                                        &control_page_default,
4231                                        sizeof(control_page_default));
4232                                 memcpy(&lun->mode_pages.control_page[
4233                                     CTL_PAGE_CHANGEABLE],
4234                                        &control_page_changeable,
4235                                        sizeof(control_page_changeable));
4236                                 memcpy(&lun->mode_pages.control_page[
4237                                     CTL_PAGE_SAVED],
4238                                        &control_page_default,
4239                                        sizeof(control_page_default));
4240                                 control_page = &lun->mode_pages.control_page[
4241                                     CTL_PAGE_SAVED];
4242                                 value = dnvlist_get_string(lun->be_lun->options,
4243                                     "reordering", NULL);
4244                                 if (value != NULL &&
4245                                     strcmp(value, "unrestricted") == 0) {
4246                                         control_page->queue_flags &=
4247                                             ~SCP_QUEUE_ALG_MASK;
4248                                         control_page->queue_flags |=
4249                                             SCP_QUEUE_ALG_UNRESTRICTED;
4250                                 }
4251                                 memcpy(&lun->mode_pages.control_page[
4252                                     CTL_PAGE_CURRENT],
4253                                        &lun->mode_pages.control_page[
4254                                     CTL_PAGE_SAVED],
4255                                        sizeof(control_page_default));
4256                                 page_index->page_data =
4257                                     (uint8_t *)lun->mode_pages.control_page;
4258                                 break;
4259                         }
4260                         case 0x01:
4261                                 memcpy(&lun->mode_pages.control_ext_page[
4262                                     CTL_PAGE_DEFAULT],
4263                                        &control_ext_page_default,
4264                                        sizeof(control_ext_page_default));
4265                                 memcpy(&lun->mode_pages.control_ext_page[
4266                                     CTL_PAGE_CHANGEABLE],
4267                                        &control_ext_page_changeable,
4268                                        sizeof(control_ext_page_changeable));
4269                                 memcpy(&lun->mode_pages.control_ext_page[
4270                                     CTL_PAGE_SAVED],
4271                                        &control_ext_page_default,
4272                                        sizeof(control_ext_page_default));
4273                                 memcpy(&lun->mode_pages.control_ext_page[
4274                                     CTL_PAGE_CURRENT],
4275                                        &lun->mode_pages.control_ext_page[
4276                                     CTL_PAGE_SAVED],
4277                                        sizeof(control_ext_page_default));
4278                                 page_index->page_data =
4279                                     (uint8_t *)lun->mode_pages.control_ext_page;
4280                                 break;
4281                         default:
4282                                 panic("subpage %#x for page %#x is incorrect!",
4283                                       page_index->subpage, page_code);
4284                         }
4285                         break;
4286                 }
4287                 case SMS_INFO_EXCEPTIONS_PAGE: {
4288                         switch (page_index->subpage) {
4289                         case SMS_SUBPAGE_PAGE_0:
4290                                 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4291                                        &ie_page_default,
4292                                        sizeof(ie_page_default));
4293                                 memcpy(&lun->mode_pages.ie_page[
4294                                        CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4295                                        sizeof(ie_page_changeable));
4296                                 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4297                                        &ie_page_default,
4298                                        sizeof(ie_page_default));
4299                                 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4300                                        &ie_page_default,
4301                                        sizeof(ie_page_default));
4302                                 page_index->page_data =
4303                                         (uint8_t *)lun->mode_pages.ie_page;
4304                                 break;
4305                         case 0x02: {
4306                                 struct ctl_logical_block_provisioning_page *page;
4307
4308                                 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4309                                        &lbp_page_default,
4310                                        sizeof(lbp_page_default));
4311                                 memcpy(&lun->mode_pages.lbp_page[
4312                                        CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4313                                        sizeof(lbp_page_changeable));
4314                                 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4315                                        &lbp_page_default,
4316                                        sizeof(lbp_page_default));
4317                                 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4318                                 value = dnvlist_get_string(lun->be_lun->options,
4319                                     "avail-threshold", NULL);
4320                                 if (value != NULL &&
4321                                     ctl_expand_number(value, &ival) == 0) {
4322                                         page->descr[0].flags |= SLBPPD_ENABLED |
4323                                             SLBPPD_ARMING_DEC;
4324                                         if (lun->be_lun->blocksize)
4325                                                 ival /= lun->be_lun->blocksize;
4326                                         else
4327                                                 ival /= 512;
4328                                         scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4329                                             page->descr[0].count);
4330                                 }
4331                                 value = dnvlist_get_string(lun->be_lun->options,
4332                                     "used-threshold", NULL);
4333                                 if (value != NULL &&
4334                                     ctl_expand_number(value, &ival) == 0) {
4335                                         page->descr[1].flags |= SLBPPD_ENABLED |
4336                                             SLBPPD_ARMING_INC;
4337                                         if (lun->be_lun->blocksize)
4338                                                 ival /= lun->be_lun->blocksize;
4339                                         else
4340                                                 ival /= 512;
4341                                         scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4342                                             page->descr[1].count);
4343                                 }
4344                                 value = dnvlist_get_string(lun->be_lun->options,
4345                                     "pool-avail-threshold", NULL);
4346                                 if (value != NULL &&
4347                                     ctl_expand_number(value, &ival) == 0) {
4348                                         page->descr[2].flags |= SLBPPD_ENABLED |
4349                                             SLBPPD_ARMING_DEC;
4350                                         if (lun->be_lun->blocksize)
4351                                                 ival /= lun->be_lun->blocksize;
4352                                         else
4353                                                 ival /= 512;
4354                                         scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4355                                             page->descr[2].count);
4356                                 }
4357                                 value = dnvlist_get_string(lun->be_lun->options,
4358                                     "pool-used-threshold", NULL);
4359                                 if (value != NULL &&
4360                                     ctl_expand_number(value, &ival) == 0) {
4361                                         page->descr[3].flags |= SLBPPD_ENABLED |
4362                                             SLBPPD_ARMING_INC;
4363                                         if (lun->be_lun->blocksize)
4364                                                 ival /= lun->be_lun->blocksize;
4365                                         else
4366                                                 ival /= 512;
4367                                         scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4368                                             page->descr[3].count);
4369                                 }
4370                                 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4371                                        &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4372                                        sizeof(lbp_page_default));
4373                                 page_index->page_data =
4374                                         (uint8_t *)lun->mode_pages.lbp_page;
4375                                 break;
4376                         }
4377                         default:
4378                                 panic("subpage %#x for page %#x is incorrect!",
4379                                       page_index->subpage, page_code);
4380                         }
4381                         break;
4382                 }
4383                 case SMS_CDDVD_CAPS_PAGE:{
4384                         KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4385                             ("subpage %#x for page %#x is incorrect!",
4386                             page_index->subpage, page_code));
4387                         memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4388                                &cddvd_page_default,
4389                                sizeof(cddvd_page_default));
4390                         memcpy(&lun->mode_pages.cddvd_page[
4391                                CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4392                                sizeof(cddvd_page_changeable));
4393                         memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4394                                &cddvd_page_default,
4395                                sizeof(cddvd_page_default));
4396                         memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4397                                &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4398                                sizeof(cddvd_page_default));
4399                         page_index->page_data =
4400                                 (uint8_t *)lun->mode_pages.cddvd_page;
4401                         break;
4402                 }
4403                 default:
4404                         panic("invalid page code value %#x", page_code);
4405                 }
4406         }
4407
4408         return (CTL_RETVAL_COMPLETE);
4409 }
4410
4411 static int
4412 ctl_init_log_page_index(struct ctl_lun *lun)
4413 {
4414         struct ctl_page_index *page_index;
4415         int i, j, k, prev;
4416
4417         memcpy(&lun->log_pages.index, log_page_index_template,
4418                sizeof(log_page_index_template));
4419
4420         prev = -1;
4421         for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4422
4423                 page_index = &lun->log_pages.index[i];
4424                 if (lun->be_lun->lun_type == T_DIRECT &&
4425                     (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4426                         continue;
4427                 if (lun->be_lun->lun_type == T_PROCESSOR &&
4428                     (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4429                         continue;
4430                 if (lun->be_lun->lun_type == T_CDROM &&
4431                     (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4432                         continue;
4433
4434                 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4435                     lun->backend->lun_attr == NULL)
4436                         continue;
4437
4438                 if (page_index->page_code != prev) {
4439                         lun->log_pages.pages_page[j] = page_index->page_code;
4440                         prev = page_index->page_code;
4441                         j++;
4442                 }
4443                 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4444                 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4445                 k++;
4446         }
4447         lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4448         lun->log_pages.index[0].page_len = j;
4449         lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4450         lun->log_pages.index[1].page_len = k * 2;
4451         lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4452         lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4453         lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4454         lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4455         lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4456         lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4457
4458         return (CTL_RETVAL_COMPLETE);
4459 }
4460
4461 static int
4462 hex2bin(const char *str, uint8_t *buf, int buf_size)
4463 {
4464         int i;
4465         u_char c;
4466
4467         memset(buf, 0, buf_size);
4468         while (isspace(str[0]))
4469                 str++;
4470         if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4471                 str += 2;
4472         buf_size *= 2;
4473         for (i = 0; str[i] != 0 && i < buf_size; i++) {
4474                 while (str[i] == '-')   /* Skip dashes in UUIDs. */
4475                         str++;
4476                 c = str[i];
4477                 if (isdigit(c))
4478                         c -= '0';
4479                 else if (isalpha(c))
4480                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4481                 else
4482                         break;
4483                 if (c >= 16)
4484                         break;
4485                 if ((i & 1) == 0)
4486                         buf[i / 2] |= (c << 4);
4487                 else
4488                         buf[i / 2] |= c;
4489         }
4490         return ((i + 1) / 2);
4491 }
4492
4493 /*
4494  * LUN allocation.
4495  *
4496  * Requirements:
4497  * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4498  *   wants us to allocate the LUN and he can block.
4499  * - ctl_softc is always set
4500  * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4501  *
4502  * Returns 0 for success, non-zero (errno) for failure.
4503  */
4504 static int
4505 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4506               struct ctl_be_lun *const be_lun)
4507 {
4508         struct ctl_lun *nlun, *lun;
4509         struct scsi_vpd_id_descriptor *desc;
4510         struct scsi_vpd_id_t10 *t10id;
4511         const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4512         int lun_number, lun_malloced;
4513         int devidlen, idlen1, idlen2 = 0, len;
4514
4515         if (be_lun == NULL)
4516                 return (EINVAL);
4517
4518         /*
4519          * We currently only support Direct Access or Processor LUN types.
4520          */
4521         switch (be_lun->lun_type) {
4522         case T_DIRECT:
4523         case T_PROCESSOR:
4524         case T_CDROM:
4525                 break;
4526         case T_SEQUENTIAL:
4527         case T_CHANGER:
4528         default:
4529                 be_lun->lun_config_status(be_lun->be_lun,
4530                                           CTL_LUN_CONFIG_FAILURE);
4531                 break;
4532         }
4533         if (ctl_lun == NULL) {
4534                 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4535                 lun_malloced = 1;
4536         } else {
4537                 lun_malloced = 0;
4538                 lun = ctl_lun;
4539         }
4540
4541         memset(lun, 0, sizeof(*lun));
4542         if (lun_malloced)
4543                 lun->flags = CTL_LUN_MALLOCED;
4544
4545         lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4546             ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4547         lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4548             M_DEVBUF, M_WAITOK | M_ZERO);
4549         lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4550             M_DEVBUF, M_WAITOK | M_ZERO);
4551
4552         /* Generate LUN ID. */
4553         devidlen = max(CTL_DEVID_MIN_LEN,
4554             strnlen(be_lun->device_id, CTL_DEVID_LEN));
4555         idlen1 = sizeof(*t10id) + devidlen;
4556         len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4557         scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4558         if (scsiname != NULL) {
4559                 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4560                 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4561         }
4562         eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4563         if (eui != NULL) {
4564                 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4565         }
4566         naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4567         if (naa != NULL) {
4568                 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4569         }
4570         uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4571         if (uuid != NULL) {
4572                 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4573         }
4574         lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4575             M_CTL, M_WAITOK | M_ZERO);
4576         desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4577         desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4578         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4579         desc->length = idlen1;
4580         t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4581         memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4582         if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4583                 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4584         } else {
4585                 strncpy(t10id->vendor, vendor,
4586                     min(sizeof(t10id->vendor), strlen(vendor)));
4587         }
4588         strncpy((char *)t10id->vendor_spec_id,
4589             (char *)be_lun->device_id, devidlen);
4590         if (scsiname != NULL) {
4591                 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4592                     desc->length);
4593                 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4594                 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4595                     SVPD_ID_TYPE_SCSI_NAME;
4596                 desc->length = idlen2;
4597                 strlcpy(desc->identifier, scsiname, idlen2);
4598         }
4599         if (eui != NULL) {
4600                 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4601                     desc->length);
4602                 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4603                 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4604                     SVPD_ID_TYPE_EUI64;
4605                 desc->length = hex2bin(eui, desc->identifier, 16);
4606                 desc->length = desc->length > 12 ? 16 :
4607                     (desc->length > 8 ? 12 : 8);
4608                 len -= 16 - desc->length;
4609         }
4610         if (naa != NULL) {
4611                 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4612                     desc->length);
4613                 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4614                 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4615                     SVPD_ID_TYPE_NAA;
4616                 desc->length = hex2bin(naa, desc->identifier, 16);
4617                 desc->length = desc->length > 8 ? 16 : 8;
4618                 len -= 16 - desc->length;
4619         }
4620         if (uuid != NULL) {
4621                 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4622                     desc->length);
4623                 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4624                 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4625                     SVPD_ID_TYPE_UUID;
4626                 desc->identifier[0] = 0x10;
4627                 hex2bin(uuid, &desc->identifier[2], 16);
4628                 desc->length = 18;
4629         }
4630         lun->lun_devid->len = len;
4631
4632         mtx_lock(&ctl_softc->ctl_lock);
4633         /*
4634          * See if the caller requested a particular LUN number.  If so, see
4635          * if it is available.  Otherwise, allocate the first available LUN.
4636          */
4637         if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4638                 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4639                  || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4640                         mtx_unlock(&ctl_softc->ctl_lock);
4641                         if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4642                                 printf("ctl: requested LUN ID %d is higher "
4643                                        "than ctl_max_luns - 1 (%d)\n",
4644                                        be_lun->req_lun_id, ctl_max_luns - 1);
4645                         } else {
4646                                 /*
4647                                  * XXX KDM return an error, or just assign
4648                                  * another LUN ID in this case??
4649                                  */
4650                                 printf("ctl: requested LUN ID %d is already "
4651                                        "in use\n", be_lun->req_lun_id);
4652                         }
4653 fail:
4654                         free(lun->lun_devid, M_CTL);
4655                         if (lun->flags & CTL_LUN_MALLOCED)
4656                                 free(lun, M_CTL);
4657                         be_lun->lun_config_status(be_lun->be_lun,
4658                                                   CTL_LUN_CONFIG_FAILURE);
4659                         return (ENOSPC);
4660                 }
4661                 lun_number = be_lun->req_lun_id;
4662         } else {
4663                 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4664                 if (lun_number == -1) {
4665                         mtx_unlock(&ctl_softc->ctl_lock);
4666                         printf("ctl: can't allocate LUN, out of LUNs\n");
4667                         goto fail;
4668                 }
4669         }
4670         ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4671         mtx_unlock(&ctl_softc->ctl_lock);
4672
4673         mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4674         lun->lun = lun_number;
4675         lun->be_lun = be_lun;
4676         /*
4677          * The processor LUN is always enabled.  Disk LUNs come on line
4678          * disabled, and must be enabled by the backend.
4679          */
4680         lun->flags |= CTL_LUN_DISABLED;
4681         lun->backend = be_lun->be;
4682         be_lun->ctl_lun = lun;
4683         be_lun->lun_id = lun_number;
4684         atomic_add_int(&be_lun->be->num_luns, 1);
4685         if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4686                 lun->flags |= CTL_LUN_EJECTED;
4687         if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4688                 lun->flags |= CTL_LUN_NO_MEDIA;
4689         if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4690                 lun->flags |= CTL_LUN_STOPPED;
4691
4692         if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4693                 lun->flags |= CTL_LUN_PRIMARY_SC;
4694
4695         value = dnvlist_get_string(be_lun->options, "removable", NULL);
4696         if (value != NULL) {
4697                 if (strcmp(value, "on") == 0)
4698                         lun->flags |= CTL_LUN_REMOVABLE;
4699         } else if (be_lun->lun_type == T_CDROM)
4700                 lun->flags |= CTL_LUN_REMOVABLE;
4701
4702         lun->ctl_softc = ctl_softc;
4703 #ifdef CTL_TIME_IO
4704         lun->last_busy = getsbinuptime();
4705 #endif
4706         TAILQ_INIT(&lun->ooa_queue);
4707         STAILQ_INIT(&lun->error_list);
4708         lun->ie_reported = 1;
4709         callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4710         ctl_tpc_lun_init(lun);
4711         if (lun->flags & CTL_LUN_REMOVABLE) {
4712                 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4713                     M_CTL, M_WAITOK);
4714         }
4715
4716         /*
4717          * Initialize the mode and log page index.
4718          */
4719         ctl_init_page_index(lun);
4720         ctl_init_log_page_index(lun);
4721
4722         /* Setup statistics gathering */
4723         lun->stats.item = lun_number;
4724
4725         /*
4726          * Now, before we insert this lun on the lun list, set the lun
4727          * inventory changed UA for all other luns.
4728          */
4729         mtx_lock(&ctl_softc->ctl_lock);
4730         STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4731                 mtx_lock(&nlun->lun_lock);
4732                 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4733                 mtx_unlock(&nlun->lun_lock);
4734         }
4735         STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4736         ctl_softc->ctl_luns[lun_number] = lun;
4737         ctl_softc->num_luns++;
4738         mtx_unlock(&ctl_softc->ctl_lock);
4739
4740         lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4741         return (0);
4742 }
4743
4744 /*
4745  * Delete a LUN.
4746  * Assumptions:
4747  * - LUN has already been marked invalid and any pending I/O has been taken
4748  *   care of.
4749  */
4750 static int
4751 ctl_free_lun(struct ctl_lun *lun)
4752 {
4753         struct ctl_softc *softc = lun->ctl_softc;
4754         struct ctl_lun *nlun;
4755         int i;
4756
4757         KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4758             ("Freeing a LUN %p with outstanding I/O!\n", lun));
4759
4760         mtx_lock(&softc->ctl_lock);
4761         STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4762         ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4763         softc->ctl_luns[lun->lun] = NULL;
4764         softc->num_luns--;
4765         STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4766                 mtx_lock(&nlun->lun_lock);
4767                 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4768                 mtx_unlock(&nlun->lun_lock);
4769         }
4770         mtx_unlock(&softc->ctl_lock);
4771
4772         /*
4773          * Tell the backend to free resources, if this LUN has a backend.
4774          */
4775         atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4776         lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4777
4778         lun->ie_reportcnt = UINT32_MAX;
4779         callout_drain(&lun->ie_callout);
4780         ctl_tpc_lun_shutdown(lun);
4781         mtx_destroy(&lun->lun_lock);
4782         free(lun->lun_devid, M_CTL);
4783         for (i = 0; i < ctl_max_ports; i++)
4784                 free(lun->pending_ua[i], M_CTL);
4785         free(lun->pending_ua, M_DEVBUF);
4786         for (i = 0; i < ctl_max_ports; i++)
4787                 free(lun->pr_keys[i], M_CTL);
4788         free(lun->pr_keys, M_DEVBUF);
4789         free(lun->write_buffer, M_CTL);
4790         free(lun->prevent, M_CTL);
4791         if (lun->flags & CTL_LUN_MALLOCED)
4792                 free(lun, M_CTL);
4793
4794         return (0);
4795 }
4796
4797 static void
4798 ctl_create_lun(struct ctl_be_lun *be_lun)
4799 {
4800
4801         /*
4802          * ctl_alloc_lun() should handle all potential failure cases.
4803          */
4804         ctl_alloc_lun(control_softc, NULL, be_lun);
4805 }
4806
4807 int
4808 ctl_add_lun(struct ctl_be_lun *be_lun)
4809 {
4810         struct ctl_softc *softc = control_softc;
4811
4812         mtx_lock(&softc->ctl_lock);
4813         STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4814         mtx_unlock(&softc->ctl_lock);
4815         wakeup(&softc->pending_lun_queue);
4816
4817         return (0);
4818 }
4819
4820 int
4821 ctl_enable_lun(struct ctl_be_lun *be_lun)
4822 {
4823         struct ctl_softc *softc;
4824         struct ctl_port *port, *nport;
4825         struct ctl_lun *lun;
4826         int retval;
4827
4828         lun = (struct ctl_lun *)be_lun->ctl_lun;
4829         softc = lun->ctl_softc;
4830
4831         mtx_lock(&softc->ctl_lock);
4832         mtx_lock(&lun->lun_lock);
4833         if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4834                 /*
4835                  * eh?  Why did we get called if the LUN is already
4836                  * enabled?
4837                  */
4838                 mtx_unlock(&lun->lun_lock);
4839                 mtx_unlock(&softc->ctl_lock);
4840                 return (0);
4841         }
4842         lun->flags &= ~CTL_LUN_DISABLED;
4843         mtx_unlock(&lun->lun_lock);
4844
4845         STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4846                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4847                     port->lun_map != NULL || port->lun_enable == NULL)
4848                         continue;
4849
4850                 /*
4851                  * Drop the lock while we call the FETD's enable routine.
4852                  * This can lead to a callback into CTL (at least in the
4853                  * case of the internal initiator frontend.
4854                  */
4855                 mtx_unlock(&softc->ctl_lock);
4856                 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4857                 mtx_lock(&softc->ctl_lock);
4858                 if (retval != 0) {
4859                         printf("%s: FETD %s port %d returned error "
4860                                "%d for lun_enable on lun %jd\n",
4861                                __func__, port->port_name, port->targ_port,
4862                                retval, (intmax_t)lun->lun);
4863                 }
4864         }
4865
4866         mtx_unlock(&softc->ctl_lock);
4867         ctl_isc_announce_lun(lun);
4868
4869         return (0);
4870 }
4871
4872 int
4873 ctl_disable_lun(struct ctl_be_lun *be_lun)
4874 {
4875         struct ctl_softc *softc;
4876         struct ctl_port *port;
4877         struct ctl_lun *lun;
4878         int retval;
4879
4880         lun = (struct ctl_lun *)be_lun->ctl_lun;
4881         softc = lun->ctl_softc;
4882
4883         mtx_lock(&softc->ctl_lock);
4884         mtx_lock(&lun->lun_lock);
4885         if (lun->flags & CTL_LUN_DISABLED) {
4886                 mtx_unlock(&lun->lun_lock);
4887                 mtx_unlock(&softc->ctl_lock);
4888                 return (0);
4889         }
4890         lun->flags |= CTL_LUN_DISABLED;
4891         mtx_unlock(&lun->lun_lock);
4892
4893         STAILQ_FOREACH(port, &softc->port_list, links) {
4894                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4895                     port->lun_map != NULL || port->lun_disable == NULL)
4896                         continue;
4897
4898                 /*
4899                  * Drop the lock before we call the frontend's disable
4900                  * routine, to avoid lock order reversals.
4901                  *
4902                  * XXX KDM what happens if the frontend list changes while
4903                  * we're traversing it?  It's unlikely, but should be handled.
4904                  */
4905                 mtx_unlock(&softc->ctl_lock);
4906                 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4907                 mtx_lock(&softc->ctl_lock);
4908                 if (retval != 0) {
4909                         printf("%s: FETD %s port %d returned error "
4910                                "%d for lun_disable on lun %jd\n",
4911                                __func__, port->port_name, port->targ_port,
4912                                retval, (intmax_t)lun->lun);
4913                 }
4914         }
4915
4916         mtx_unlock(&softc->ctl_lock);
4917         ctl_isc_announce_lun(lun);
4918
4919         return (0);
4920 }
4921
4922 int
4923 ctl_start_lun(struct ctl_be_lun *be_lun)
4924 {
4925         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4926
4927         mtx_lock(&lun->lun_lock);
4928         lun->flags &= ~CTL_LUN_STOPPED;
4929         mtx_unlock(&lun->lun_lock);
4930         return (0);
4931 }
4932
4933 int
4934 ctl_stop_lun(struct ctl_be_lun *be_lun)
4935 {
4936         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4937
4938         mtx_lock(&lun->lun_lock);
4939         lun->flags |= CTL_LUN_STOPPED;
4940         mtx_unlock(&lun->lun_lock);
4941         return (0);
4942 }
4943
4944 int
4945 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4946 {
4947         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4948
4949         mtx_lock(&lun->lun_lock);
4950         lun->flags |= CTL_LUN_NO_MEDIA;
4951         mtx_unlock(&lun->lun_lock);
4952         return (0);
4953 }
4954
4955 int
4956 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4957 {
4958         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4959         union ctl_ha_msg msg;
4960
4961         mtx_lock(&lun->lun_lock);
4962         lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4963         if (lun->flags & CTL_LUN_REMOVABLE)
4964                 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4965         mtx_unlock(&lun->lun_lock);
4966         if ((lun->flags & CTL_LUN_REMOVABLE) &&
4967             lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4968                 bzero(&msg.ua, sizeof(msg.ua));
4969                 msg.hdr.msg_type = CTL_MSG_UA;
4970                 msg.hdr.nexus.initid = -1;
4971                 msg.hdr.nexus.targ_port = -1;
4972                 msg.hdr.nexus.targ_lun = lun->lun;
4973                 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4974                 msg.ua.ua_all = 1;
4975                 msg.ua.ua_set = 1;
4976                 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4977                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4978                     M_WAITOK);
4979         }
4980         return (0);
4981 }
4982
4983 int
4984 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4985 {
4986         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4987
4988         mtx_lock(&lun->lun_lock);
4989         lun->flags |= CTL_LUN_EJECTED;
4990         mtx_unlock(&lun->lun_lock);
4991         return (0);
4992 }
4993
4994 int
4995 ctl_lun_primary(struct ctl_be_lun *be_lun)
4996 {
4997         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4998
4999         mtx_lock(&lun->lun_lock);
5000         lun->flags |= CTL_LUN_PRIMARY_SC;
5001         ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5002         mtx_unlock(&lun->lun_lock);
5003         ctl_isc_announce_lun(lun);
5004         return (0);
5005 }
5006
5007 int
5008 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5009 {
5010         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5011
5012         mtx_lock(&lun->lun_lock);
5013         lun->flags &= ~CTL_LUN_PRIMARY_SC;
5014         ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5015         mtx_unlock(&lun->lun_lock);
5016         ctl_isc_announce_lun(lun);
5017         return (0);
5018 }
5019
5020 int
5021 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5022 {
5023         struct ctl_lun *lun;
5024
5025         lun = (struct ctl_lun *)be_lun->ctl_lun;
5026
5027         mtx_lock(&lun->lun_lock);
5028
5029         /*
5030          * The LUN needs to be disabled before it can be marked invalid.
5031          */
5032         if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5033                 mtx_unlock(&lun->lun_lock);
5034                 return (-1);
5035         }
5036         /*
5037          * Mark the LUN invalid.
5038          */
5039         lun->flags |= CTL_LUN_INVALID;
5040
5041         /*
5042          * If there is nothing in the OOA queue, go ahead and free the LUN.
5043          * If we have something in the OOA queue, we'll free it when the
5044          * last I/O completes.
5045          */
5046         if (TAILQ_EMPTY(&lun->ooa_queue)) {
5047                 mtx_unlock(&lun->lun_lock);
5048                 ctl_free_lun(lun);
5049         } else
5050                 mtx_unlock(&lun->lun_lock);
5051
5052         return (0);
5053 }
5054
5055 void
5056 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5057 {
5058         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5059         union ctl_ha_msg msg;
5060
5061         mtx_lock(&lun->lun_lock);
5062         ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5063         mtx_unlock(&lun->lun_lock);
5064         if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5065                 /* Send msg to other side. */
5066                 bzero(&msg.ua, sizeof(msg.ua));
5067                 msg.hdr.msg_type = CTL_MSG_UA;
5068                 msg.hdr.nexus.initid = -1;
5069                 msg.hdr.nexus.targ_port = -1;
5070                 msg.hdr.nexus.targ_lun = lun->lun;
5071                 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5072                 msg.ua.ua_all = 1;
5073                 msg.ua.ua_set = 1;
5074                 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5075                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5076                     M_WAITOK);
5077         }
5078 }
5079
5080 /*
5081  * Backend "memory move is complete" callback for requests that never
5082  * make it down to say RAIDCore's configuration code.
5083  */
5084 int
5085 ctl_config_move_done(union ctl_io *io)
5086 {
5087         int retval;
5088
5089         CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5090         KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5091             ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5092
5093         if ((io->io_hdr.port_status != 0) &&
5094             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5095              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5096                 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5097                     /*retry_count*/ io->io_hdr.port_status);
5098         } else if (io->scsiio.kern_data_resid != 0 &&
5099             (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5100             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5101              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5102                 ctl_set_invalid_field_ciu(&io->scsiio);
5103         }
5104
5105         if (ctl_debug & CTL_DEBUG_CDB_DATA)
5106                 ctl_data_print(io);
5107         if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5108             ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5109              (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5110             ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5111                 /*
5112                  * XXX KDM just assuming a single pointer here, and not a
5113                  * S/G list.  If we start using S/G lists for config data,
5114                  * we'll need to know how to clean them up here as well.
5115                  */
5116                 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5117                         free(io->scsiio.kern_data_ptr, M_CTL);
5118                 ctl_done(io);
5119                 retval = CTL_RETVAL_COMPLETE;
5120         } else {
5121                 /*
5122                  * XXX KDM now we need to continue data movement.  Some
5123                  * options:
5124                  * - call ctl_scsiio() again?  We don't do this for data
5125                  *   writes, because for those at least we know ahead of
5126                  *   time where the write will go and how long it is.  For
5127                  *   config writes, though, that information is largely
5128                  *   contained within the write itself, thus we need to
5129                  *   parse out the data again.
5130                  *
5131                  * - Call some other function once the data is in?
5132                  */
5133
5134                 /*
5135                  * XXX KDM call ctl_scsiio() again for now, and check flag
5136                  * bits to see whether we're allocated or not.
5137                  */
5138                 retval = ctl_scsiio(&io->scsiio);
5139         }
5140         return (retval);
5141 }
5142
5143 /*
5144  * This gets called by a backend driver when it is done with a
5145  * data_submit method.
5146  */
5147 void
5148 ctl_data_submit_done(union ctl_io *io)
5149 {
5150         /*
5151          * If the IO_CONT flag is set, we need to call the supplied
5152          * function to continue processing the I/O, instead of completing
5153          * the I/O just yet.
5154          *
5155          * If there is an error, though, we don't want to keep processing.
5156          * Instead, just send status back to the initiator.
5157          */
5158         if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5159             (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5160             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5161              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5162                 io->scsiio.io_cont(io);
5163                 return;
5164         }
5165         ctl_done(io);
5166 }
5167
5168 /*
5169  * This gets called by a backend driver when it is done with a
5170  * configuration write.
5171  */
5172 void
5173 ctl_config_write_done(union ctl_io *io)
5174 {
5175         uint8_t *buf;
5176
5177         /*
5178          * If the IO_CONT flag is set, we need to call the supplied
5179          * function to continue processing the I/O, instead of completing
5180          * the I/O just yet.
5181          *
5182          * If there is an error, though, we don't want to keep processing.
5183          * Instead, just send status back to the initiator.
5184          */
5185         if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5186             (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5187             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5188              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5189                 io->scsiio.io_cont(io);
5190                 return;
5191         }
5192         /*
5193          * Since a configuration write can be done for commands that actually
5194          * have data allocated, like write buffer, and commands that have
5195          * no data, like start/stop unit, we need to check here.
5196          */
5197         if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5198                 buf = io->scsiio.kern_data_ptr;
5199         else
5200                 buf = NULL;
5201         ctl_done(io);
5202         if (buf)
5203                 free(buf, M_CTL);
5204 }
5205
5206 void
5207 ctl_config_read_done(union ctl_io *io)
5208 {
5209         uint8_t *buf;
5210
5211         /*
5212          * If there is some error -- we are done, skip data transfer.
5213          */
5214         if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5215             ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5216              (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5217                 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5218                         buf = io->scsiio.kern_data_ptr;
5219                 else
5220                         buf = NULL;
5221                 ctl_done(io);
5222                 if (buf)
5223                         free(buf, M_CTL);
5224                 return;
5225         }
5226
5227         /*
5228          * If the IO_CONT flag is set, we need to call the supplied
5229          * function to continue processing the I/O, instead of completing
5230          * the I/O just yet.
5231          */
5232         if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5233                 io->scsiio.io_cont(io);
5234                 return;
5235         }
5236
5237         ctl_datamove(io);
5238 }
5239
5240 /*
5241  * SCSI release command.
5242  */
5243 int
5244 ctl_scsi_release(struct ctl_scsiio *ctsio)
5245 {
5246         struct ctl_lun *lun = CTL_LUN(ctsio);
5247         uint32_t residx;
5248
5249         CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5250
5251         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5252
5253         /*
5254          * XXX KDM right now, we only support LUN reservation.  We don't
5255          * support 3rd party reservations, or extent reservations, which
5256          * might actually need the parameter list.  If we've gotten this
5257          * far, we've got a LUN reservation.  Anything else got kicked out
5258          * above.  So, according to SPC, ignore the length.
5259          */
5260
5261         mtx_lock(&lun->lun_lock);
5262
5263         /*
5264          * According to SPC, it is not an error for an intiator to attempt
5265          * to release a reservation on a LUN that isn't reserved, or that
5266          * is reserved by another initiator.  The reservation can only be
5267          * released, though, by the initiator who made it or by one of
5268          * several reset type events.
5269          */
5270         if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5271                         lun->flags &= ~CTL_LUN_RESERVED;
5272
5273         mtx_unlock(&lun->lun_lock);
5274
5275         ctl_set_success(ctsio);
5276         ctl_done((union ctl_io *)ctsio);
5277         return (CTL_RETVAL_COMPLETE);
5278 }
5279
5280 int
5281 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5282 {
5283         struct ctl_lun *lun = CTL_LUN(ctsio);
5284         uint32_t residx;
5285
5286         CTL_DEBUG_PRINT(("ctl_reserve\n"));
5287
5288         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5289
5290         /*
5291          * XXX KDM right now, we only support LUN reservation.  We don't
5292          * support 3rd party reservations, or extent reservations, which
5293          * might actually need the parameter list.  If we've gotten this
5294          * far, we've got a LUN reservation.  Anything else got kicked out
5295          * above.  So, according to SPC, ignore the length.
5296          */
5297
5298         mtx_lock(&lun->lun_lock);
5299         if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5300                 ctl_set_reservation_conflict(ctsio);
5301                 goto bailout;
5302         }
5303
5304         /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5305         if (lun->flags & CTL_LUN_PR_RESERVED) {
5306                 ctl_set_success(ctsio);
5307                 goto bailout;
5308         }
5309
5310         lun->flags |= CTL_LUN_RESERVED;
5311         lun->res_idx = residx;
5312         ctl_set_success(ctsio);
5313
5314 bailout:
5315         mtx_unlock(&lun->lun_lock);
5316         ctl_done((union ctl_io *)ctsio);
5317         return (CTL_RETVAL_COMPLETE);
5318 }
5319
5320 int
5321 ctl_start_stop(struct ctl_scsiio *ctsio)
5322 {
5323         struct ctl_lun *lun = CTL_LUN(ctsio);
5324         struct scsi_start_stop_unit *cdb;
5325         int retval;
5326
5327         CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5328
5329         cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5330
5331         if ((cdb->how & SSS_PC_MASK) == 0) {
5332                 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5333                     (cdb->how & SSS_START) == 0) {
5334                         uint32_t residx;
5335
5336                         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5337                         if (ctl_get_prkey(lun, residx) == 0 ||
5338                             (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5339
5340                                 ctl_set_reservation_conflict(ctsio);
5341                                 ctl_done((union ctl_io *)ctsio);
5342                                 return (CTL_RETVAL_COMPLETE);
5343                         }
5344                 }
5345
5346                 if ((cdb->how & SSS_LOEJ) &&
5347                     (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5348                         ctl_set_invalid_field(ctsio,
5349                                               /*sks_valid*/ 1,
5350                                               /*command*/ 1,
5351                                               /*field*/ 4,
5352                                               /*bit_valid*/ 1,
5353                                               /*bit*/ 1);
5354                         ctl_done((union ctl_io *)ctsio);
5355                         return (CTL_RETVAL_COMPLETE);
5356                 }
5357
5358                 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5359                     lun->prevent_count > 0) {
5360                         /* "Medium removal prevented" */
5361                         ctl_set_sense(ctsio, /*current_error*/ 1,
5362                             /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5363                              SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5364                             /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5365                         ctl_done((union ctl_io *)ctsio);
5366                         return (CTL_RETVAL_COMPLETE);
5367                 }
5368         }
5369
5370         retval = lun->backend->config_write((union ctl_io *)ctsio);
5371         return (retval);
5372 }
5373
5374 int
5375 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5376 {
5377         struct ctl_lun *lun = CTL_LUN(ctsio);
5378         struct scsi_prevent *cdb;
5379         int retval;
5380         uint32_t initidx;
5381
5382         CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5383
5384         cdb = (struct scsi_prevent *)ctsio->cdb;
5385
5386         if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5387                 ctl_set_invalid_opcode(ctsio);
5388                 ctl_done((union ctl_io *)ctsio);
5389                 return (CTL_RETVAL_COMPLETE);
5390         }
5391
5392         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5393         mtx_lock(&lun->lun_lock);
5394         if ((cdb->how & PR_PREVENT) &&
5395             ctl_is_set(lun->prevent, initidx) == 0) {
5396                 ctl_set_mask(lun->prevent, initidx);
5397                 lun->prevent_count++;
5398         } else if ((cdb->how & PR_PREVENT) == 0 &&
5399             ctl_is_set(lun->prevent, initidx)) {
5400                 ctl_clear_mask(lun->prevent, initidx);
5401                 lun->prevent_count--;
5402         }
5403         mtx_unlock(&lun->lun_lock);
5404         retval = lun->backend->config_write((union ctl_io *)ctsio);
5405         return (retval);
5406 }
5407
5408 /*
5409  * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5410  * we don't really do anything with the LBA and length fields if the user
5411  * passes them in.  Instead we'll just flush out the cache for the entire
5412  * LUN.
5413  */
5414 int
5415 ctl_sync_cache(struct ctl_scsiio *ctsio)
5416 {
5417         struct ctl_lun *lun = CTL_LUN(ctsio);
5418         struct ctl_lba_len_flags *lbalen;
5419         uint64_t starting_lba;
5420         uint32_t block_count;
5421         int retval;
5422         uint8_t byte2;
5423
5424         CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5425
5426         retval = 0;
5427
5428         switch (ctsio->cdb[0]) {
5429         case SYNCHRONIZE_CACHE: {
5430                 struct scsi_sync_cache *cdb;
5431                 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5432
5433                 starting_lba = scsi_4btoul(cdb->begin_lba);
5434                 block_count = scsi_2btoul(cdb->lb_count);
5435                 byte2 = cdb->byte2;
5436                 break;
5437         }
5438         case SYNCHRONIZE_CACHE_16: {
5439                 struct scsi_sync_cache_16 *cdb;
5440                 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5441
5442                 starting_lba = scsi_8btou64(cdb->begin_lba);
5443                 block_count = scsi_4btoul(cdb->lb_count);
5444                 byte2 = cdb->byte2;
5445                 break;
5446         }
5447         default:
5448                 ctl_set_invalid_opcode(ctsio);
5449                 ctl_done((union ctl_io *)ctsio);
5450                 goto bailout;
5451                 break; /* NOTREACHED */
5452         }
5453
5454         /*
5455          * We check the LBA and length, but don't do anything with them.
5456          * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5457          * get flushed.  This check will just help satisfy anyone who wants
5458          * to see an error for an out of range LBA.
5459          */
5460         if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5461                 ctl_set_lba_out_of_range(ctsio,
5462                     MAX(starting_lba, lun->be_lun->maxlba + 1));
5463                 ctl_done((union ctl_io *)ctsio);
5464                 goto bailout;
5465         }
5466
5467         lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5468         lbalen->lba = starting_lba;
5469         lbalen->len = block_count;
5470         lbalen->flags = byte2;
5471         retval = lun->backend->config_write((union ctl_io *)ctsio);
5472
5473 bailout:
5474         return (retval);
5475 }
5476
5477 int
5478 ctl_format(struct ctl_scsiio *ctsio)
5479 {
5480         struct scsi_format *cdb;
5481         int length, defect_list_len;
5482
5483         CTL_DEBUG_PRINT(("ctl_format\n"));
5484
5485         cdb = (struct scsi_format *)ctsio->cdb;
5486
5487         length = 0;
5488         if (cdb->byte2 & SF_FMTDATA) {
5489                 if (cdb->byte2 & SF_LONGLIST)
5490                         length = sizeof(struct scsi_format_header_long);
5491                 else
5492                         length = sizeof(struct scsi_format_header_short);
5493         }
5494
5495         if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5496          && (length > 0)) {
5497                 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5498                 ctsio->kern_data_len = length;
5499                 ctsio->kern_total_len = length;
5500                 ctsio->kern_rel_offset = 0;
5501                 ctsio->kern_sg_entries = 0;
5502                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5503                 ctsio->be_move_done = ctl_config_move_done;
5504                 ctl_datamove((union ctl_io *)ctsio);
5505
5506                 return (CTL_RETVAL_COMPLETE);
5507         }
5508
5509         defect_list_len = 0;
5510
5511         if (cdb->byte2 & SF_FMTDATA) {
5512                 if (cdb->byte2 & SF_LONGLIST) {
5513                         struct scsi_format_header_long *header;
5514
5515                         header = (struct scsi_format_header_long *)
5516                                 ctsio->kern_data_ptr;
5517
5518                         defect_list_len = scsi_4btoul(header->defect_list_len);
5519                         if (defect_list_len != 0) {
5520                                 ctl_set_invalid_field(ctsio,
5521                                                       /*sks_valid*/ 1,
5522                                                       /*command*/ 0,
5523                                                       /*field*/ 2,
5524                                                       /*bit_valid*/ 0,
5525                                                       /*bit*/ 0);
5526                                 goto bailout;
5527                         }
5528                 } else {
5529                         struct scsi_format_header_short *header;
5530
5531                         header = (struct scsi_format_header_short *)
5532                                 ctsio->kern_data_ptr;
5533
5534                         defect_list_len = scsi_2btoul(header->defect_list_len);
5535                         if (defect_list_len != 0) {
5536                                 ctl_set_invalid_field(ctsio,
5537                                                       /*sks_valid*/ 1,
5538                                                       /*command*/ 0,
5539                                                       /*field*/ 2,
5540                                                       /*bit_valid*/ 0,
5541                                                       /*bit*/ 0);
5542                                 goto bailout;
5543                         }
5544                 }
5545         }
5546
5547         ctl_set_success(ctsio);
5548 bailout:
5549
5550         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5551                 free(ctsio->kern_data_ptr, M_CTL);
5552                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5553         }
5554
5555         ctl_done((union ctl_io *)ctsio);
5556         return (CTL_RETVAL_COMPLETE);
5557 }
5558
5559 int
5560 ctl_read_buffer(struct ctl_scsiio *ctsio)
5561 {
5562         struct ctl_lun *lun = CTL_LUN(ctsio);
5563         uint64_t buffer_offset;
5564         uint32_t len;
5565         uint8_t byte2;
5566         static uint8_t descr[4];
5567         static uint8_t echo_descr[4] = { 0 };
5568
5569         CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5570
5571         switch (ctsio->cdb[0]) {
5572         case READ_BUFFER: {
5573                 struct scsi_read_buffer *cdb;
5574
5575                 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5576                 buffer_offset = scsi_3btoul(cdb->offset);
5577                 len = scsi_3btoul(cdb->length);
5578                 byte2 = cdb->byte2;
5579                 break;
5580         }
5581         case READ_BUFFER_16: {
5582                 struct scsi_read_buffer_16 *cdb;
5583
5584                 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5585                 buffer_offset = scsi_8btou64(cdb->offset);
5586                 len = scsi_4btoul(cdb->length);
5587                 byte2 = cdb->byte2;
5588                 break;
5589         }
5590         default: /* This shouldn't happen. */
5591                 ctl_set_invalid_opcode(ctsio);
5592                 ctl_done((union ctl_io *)ctsio);
5593                 return (CTL_RETVAL_COMPLETE);
5594         }
5595
5596         if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5597             buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5598                 ctl_set_invalid_field(ctsio,
5599                                       /*sks_valid*/ 1,
5600                                       /*command*/ 1,
5601                                       /*field*/ 6,
5602                                       /*bit_valid*/ 0,
5603                                       /*bit*/ 0);
5604                 ctl_done((union ctl_io *)ctsio);
5605                 return (CTL_RETVAL_COMPLETE);
5606         }
5607
5608         if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5609                 descr[0] = 0;
5610                 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5611                 ctsio->kern_data_ptr = descr;
5612                 len = min(len, sizeof(descr));
5613         } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5614                 ctsio->kern_data_ptr = echo_descr;
5615                 len = min(len, sizeof(echo_descr));
5616         } else {
5617                 if (lun->write_buffer == NULL) {
5618                         lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5619                             M_CTL, M_WAITOK);
5620                 }
5621                 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5622         }
5623         ctsio->kern_data_len = len;
5624         ctsio->kern_total_len = len;
5625         ctsio->kern_rel_offset = 0;
5626         ctsio->kern_sg_entries = 0;
5627         ctl_set_success(ctsio);
5628         ctsio->be_move_done = ctl_config_move_done;
5629         ctl_datamove((union ctl_io *)ctsio);
5630         return (CTL_RETVAL_COMPLETE);
5631 }
5632
5633 int
5634 ctl_write_buffer(struct ctl_scsiio *ctsio)
5635 {
5636         struct ctl_lun *lun = CTL_LUN(ctsio);
5637         struct scsi_write_buffer *cdb;
5638         int buffer_offset, len;
5639
5640         CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5641
5642         cdb = (struct scsi_write_buffer *)ctsio->cdb;
5643
5644         len = scsi_3btoul(cdb->length);
5645         buffer_offset = scsi_3btoul(cdb->offset);
5646
5647         if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5648                 ctl_set_invalid_field(ctsio,
5649                                       /*sks_valid*/ 1,
5650                                       /*command*/ 1,
5651                                       /*field*/ 6,
5652                                       /*bit_valid*/ 0,
5653                                       /*bit*/ 0);
5654                 ctl_done((union ctl_io *)ctsio);
5655                 return (CTL_RETVAL_COMPLETE);
5656         }
5657
5658         /*
5659          * If we've got a kernel request that hasn't been malloced yet,
5660          * malloc it and tell the caller the data buffer is here.
5661          */
5662         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5663                 if (lun->write_buffer == NULL) {
5664                         lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5665                             M_CTL, M_WAITOK);
5666                 }
5667                 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5668                 ctsio->kern_data_len = len;
5669                 ctsio->kern_total_len = len;
5670                 ctsio->kern_rel_offset = 0;
5671                 ctsio->kern_sg_entries = 0;
5672                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5673                 ctsio->be_move_done = ctl_config_move_done;
5674                 ctl_datamove((union ctl_io *)ctsio);
5675
5676                 return (CTL_RETVAL_COMPLETE);
5677         }
5678
5679         ctl_set_success(ctsio);
5680         ctl_done((union ctl_io *)ctsio);
5681         return (CTL_RETVAL_COMPLETE);
5682 }
5683
5684 int
5685 ctl_write_same(struct ctl_scsiio *ctsio)
5686 {
5687         struct ctl_lun *lun = CTL_LUN(ctsio);
5688         struct ctl_lba_len_flags *lbalen;
5689         uint64_t lba;
5690         uint32_t num_blocks;
5691         int len, retval;
5692         uint8_t byte2;
5693
5694         CTL_DEBUG_PRINT(("ctl_write_same\n"));
5695
5696         switch (ctsio->cdb[0]) {
5697         case WRITE_SAME_10: {
5698                 struct scsi_write_same_10 *cdb;
5699
5700                 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5701
5702                 lba = scsi_4btoul(cdb->addr);
5703                 num_blocks = scsi_2btoul(cdb->length);
5704                 byte2 = cdb->byte2;
5705                 break;
5706         }
5707         case WRITE_SAME_16: {
5708                 struct scsi_write_same_16 *cdb;
5709
5710                 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5711
5712                 lba = scsi_8btou64(cdb->addr);
5713                 num_blocks = scsi_4btoul(cdb->length);
5714                 byte2 = cdb->byte2;
5715                 break;
5716         }
5717         default:
5718                 /*
5719                  * We got a command we don't support.  This shouldn't
5720                  * happen, commands should be filtered out above us.
5721                  */
5722                 ctl_set_invalid_opcode(ctsio);
5723                 ctl_done((union ctl_io *)ctsio);
5724
5725                 return (CTL_RETVAL_COMPLETE);
5726                 break; /* NOTREACHED */
5727         }
5728
5729         /* ANCHOR flag can be used only together with UNMAP */
5730         if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5731                 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5732                     /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5733                 ctl_done((union ctl_io *)ctsio);
5734                 return (CTL_RETVAL_COMPLETE);
5735         }
5736
5737         /*
5738          * The first check is to make sure we're in bounds, the second
5739          * check is to catch wrap-around problems.  If the lba + num blocks
5740          * is less than the lba, then we've wrapped around and the block
5741          * range is invalid anyway.
5742          */
5743         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5744          || ((lba + num_blocks) < lba)) {
5745                 ctl_set_lba_out_of_range(ctsio,
5746                     MAX(lba, lun->be_lun->maxlba + 1));
5747                 ctl_done((union ctl_io *)ctsio);
5748                 return (CTL_RETVAL_COMPLETE);
5749         }
5750
5751         /* Zero number of blocks means "to the last logical block" */
5752         if (num_blocks == 0) {
5753                 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5754                         ctl_set_invalid_field(ctsio,
5755                                               /*sks_valid*/ 0,
5756                                               /*command*/ 1,
5757                                               /*field*/ 0,
5758                                               /*bit_valid*/ 0,
5759                                               /*bit*/ 0);
5760                         ctl_done((union ctl_io *)ctsio);
5761                         return (CTL_RETVAL_COMPLETE);
5762                 }
5763                 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5764         }
5765
5766         len = lun->be_lun->blocksize;
5767
5768         /*
5769          * If we've got a kernel request that hasn't been malloced yet,
5770          * malloc it and tell the caller the data buffer is here.
5771          */
5772         if ((byte2 & SWS_NDOB) == 0 &&
5773             (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5774                 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5775                 ctsio->kern_data_len = len;
5776                 ctsio->kern_total_len = len;
5777                 ctsio->kern_rel_offset = 0;
5778                 ctsio->kern_sg_entries = 0;
5779                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5780                 ctsio->be_move_done = ctl_config_move_done;
5781                 ctl_datamove((union ctl_io *)ctsio);
5782
5783                 return (CTL_RETVAL_COMPLETE);
5784         }
5785
5786         lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5787         lbalen->lba = lba;
5788         lbalen->len = num_blocks;
5789         lbalen->flags = byte2;
5790         retval = lun->backend->config_write((union ctl_io *)ctsio);
5791
5792         return (retval);
5793 }
5794
5795 int
5796 ctl_unmap(struct ctl_scsiio *ctsio)
5797 {
5798         struct ctl_lun *lun = CTL_LUN(ctsio);
5799         struct scsi_unmap *cdb;
5800         struct ctl_ptr_len_flags *ptrlen;
5801         struct scsi_unmap_header *hdr;
5802         struct scsi_unmap_desc *buf, *end, *endnz, *range;
5803         uint64_t lba;
5804         uint32_t num_blocks;
5805         int len, retval;
5806         uint8_t byte2;
5807
5808         CTL_DEBUG_PRINT(("ctl_unmap\n"));
5809
5810         cdb = (struct scsi_unmap *)ctsio->cdb;
5811         len = scsi_2btoul(cdb->length);
5812         byte2 = cdb->byte2;
5813
5814         /*
5815          * If we've got a kernel request that hasn't been malloced yet,
5816          * malloc it and tell the caller the data buffer is here.
5817          */
5818         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5819                 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5820                 ctsio->kern_data_len = len;
5821                 ctsio->kern_total_len = len;
5822                 ctsio->kern_rel_offset = 0;
5823                 ctsio->kern_sg_entries = 0;
5824                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5825                 ctsio->be_move_done = ctl_config_move_done;
5826                 ctl_datamove((union ctl_io *)ctsio);
5827
5828                 return (CTL_RETVAL_COMPLETE);
5829         }
5830
5831         len = ctsio->kern_total_len - ctsio->kern_data_resid;
5832         hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5833         if (len < sizeof (*hdr) ||
5834             len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5835             len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5836             scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5837                 ctl_set_invalid_field(ctsio,
5838                                       /*sks_valid*/ 0,
5839                                       /*command*/ 0,
5840                                       /*field*/ 0,
5841                                       /*bit_valid*/ 0,
5842                                       /*bit*/ 0);
5843                 goto done;
5844         }
5845         len = scsi_2btoul(hdr->desc_length);
5846         buf = (struct scsi_unmap_desc *)(hdr + 1);
5847         end = buf + len / sizeof(*buf);
5848
5849         endnz = buf;
5850         for (range = buf; range < end; range++) {
5851                 lba = scsi_8btou64(range->lba);
5852                 num_blocks = scsi_4btoul(range->length);
5853                 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5854                  || ((lba + num_blocks) < lba)) {
5855                         ctl_set_lba_out_of_range(ctsio,
5856                             MAX(lba, lun->be_lun->maxlba + 1));
5857                         ctl_done((union ctl_io *)ctsio);
5858                         return (CTL_RETVAL_COMPLETE);
5859                 }
5860                 if (num_blocks != 0)
5861                         endnz = range + 1;
5862         }
5863
5864         /*
5865          * Block backend can not handle zero last range.
5866          * Filter it out and return if there is nothing left.
5867          */
5868         len = (uint8_t *)endnz - (uint8_t *)buf;
5869         if (len == 0) {
5870                 ctl_set_success(ctsio);
5871                 goto done;
5872         }
5873
5874         mtx_lock(&lun->lun_lock);
5875         ptrlen = (struct ctl_ptr_len_flags *)
5876             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5877         ptrlen->ptr = (void *)buf;
5878         ptrlen->len = len;
5879         ptrlen->flags = byte2;
5880         ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5881         mtx_unlock(&lun->lun_lock);
5882
5883         retval = lun->backend->config_write((union ctl_io *)ctsio);
5884         return (retval);
5885
5886 done:
5887         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5888                 free(ctsio->kern_data_ptr, M_CTL);
5889                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5890         }
5891         ctl_done((union ctl_io *)ctsio);
5892         return (CTL_RETVAL_COMPLETE);
5893 }
5894
5895 int
5896 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5897                          struct ctl_page_index *page_index, uint8_t *page_ptr)
5898 {
5899         struct ctl_lun *lun = CTL_LUN(ctsio);
5900         uint8_t *current_cp;
5901         int set_ua;
5902         uint32_t initidx;
5903
5904         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5905         set_ua = 0;
5906
5907         current_cp = (page_index->page_data + (page_index->page_len *
5908             CTL_PAGE_CURRENT));
5909
5910         mtx_lock(&lun->lun_lock);
5911         if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5912                 memcpy(current_cp, page_ptr, page_index->page_len);
5913                 set_ua = 1;
5914         }
5915         if (set_ua != 0)
5916                 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5917         mtx_unlock(&lun->lun_lock);
5918         if (set_ua) {
5919                 ctl_isc_announce_mode(lun,
5920                     ctl_get_initindex(&ctsio->io_hdr.nexus),
5921                     page_index->page_code, page_index->subpage);
5922         }
5923         return (CTL_RETVAL_COMPLETE);
5924 }
5925
5926 static void
5927 ctl_ie_timer(void *arg)
5928 {
5929         struct ctl_lun *lun = arg;
5930         uint64_t t;
5931
5932         if (lun->ie_asc == 0)
5933                 return;
5934
5935         if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5936                 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5937         else
5938                 lun->ie_reported = 0;
5939
5940         if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5941                 lun->ie_reportcnt++;
5942                 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5943                 if (t == 0 || t == UINT32_MAX)
5944                         t = 3000;  /* 5 min */
5945                 callout_schedule(&lun->ie_callout, t * hz / 10);
5946         }
5947 }
5948
5949 int
5950 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5951                          struct ctl_page_index *page_index, uint8_t *page_ptr)
5952 {
5953         struct ctl_lun *lun = CTL_LUN(ctsio);
5954         struct scsi_info_exceptions_page *pg;
5955         uint64_t t;
5956
5957         (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5958
5959         pg = (struct scsi_info_exceptions_page *)page_ptr;
5960         mtx_lock(&lun->lun_lock);
5961         if (pg->info_flags & SIEP_FLAGS_TEST) {
5962                 lun->ie_asc = 0x5d;
5963                 lun->ie_ascq = 0xff;
5964                 if (pg->mrie == SIEP_MRIE_UA) {
5965                         ctl_est_ua_all(lun, -1, CTL_UA_IE);
5966                         lun->ie_reported = 1;
5967                 } else {
5968                         ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5969                         lun->ie_reported = -1;
5970                 }
5971                 lun->ie_reportcnt = 1;
5972                 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5973                         lun->ie_reportcnt++;
5974                         t = scsi_4btoul(pg->interval_timer);
5975                         if (t == 0 || t == UINT32_MAX)
5976                                 t = 3000;  /* 5 min */
5977                         callout_reset(&lun->ie_callout, t * hz / 10,
5978                             ctl_ie_timer, lun);
5979                 }
5980         } else {
5981                 lun->ie_asc = 0;
5982                 lun->ie_ascq = 0;
5983                 lun->ie_reported = 1;
5984                 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5985                 lun->ie_reportcnt = UINT32_MAX;
5986                 callout_stop(&lun->ie_callout);
5987         }
5988         mtx_unlock(&lun->lun_lock);
5989         return (CTL_RETVAL_COMPLETE);
5990 }
5991
5992 static int
5993 ctl_do_mode_select(union ctl_io *io)
5994 {
5995         struct ctl_lun *lun = CTL_LUN(io);
5996         struct scsi_mode_page_header *page_header;
5997         struct ctl_page_index *page_index;
5998         struct ctl_scsiio *ctsio;
5999         int page_len, page_len_offset, page_len_size;
6000         union ctl_modepage_info *modepage_info;
6001         uint16_t *len_left, *len_used;
6002         int retval, i;
6003
6004         ctsio = &io->scsiio;
6005         page_index = NULL;
6006         page_len = 0;
6007
6008         modepage_info = (union ctl_modepage_info *)
6009                 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6010         len_left = &modepage_info->header.len_left;
6011         len_used = &modepage_info->header.len_used;
6012
6013 do_next_page:
6014
6015         page_header = (struct scsi_mode_page_header *)
6016                 (ctsio->kern_data_ptr + *len_used);
6017
6018         if (*len_left == 0) {
6019                 free(ctsio->kern_data_ptr, M_CTL);
6020                 ctl_set_success(ctsio);
6021                 ctl_done((union ctl_io *)ctsio);
6022                 return (CTL_RETVAL_COMPLETE);
6023         } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6024
6025                 free(ctsio->kern_data_ptr, M_CTL);
6026                 ctl_set_param_len_error(ctsio);
6027                 ctl_done((union ctl_io *)ctsio);
6028                 return (CTL_RETVAL_COMPLETE);
6029
6030         } else if ((page_header->page_code & SMPH_SPF)
6031                 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6032
6033                 free(ctsio->kern_data_ptr, M_CTL);
6034                 ctl_set_param_len_error(ctsio);
6035                 ctl_done((union ctl_io *)ctsio);
6036                 return (CTL_RETVAL_COMPLETE);
6037         }
6038
6039
6040         /*
6041          * XXX KDM should we do something with the block descriptor?
6042          */
6043         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6044                 page_index = &lun->mode_pages.index[i];
6045                 if (lun->be_lun->lun_type == T_DIRECT &&
6046                     (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6047                         continue;
6048                 if (lun->be_lun->lun_type == T_PROCESSOR &&
6049                     (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6050                         continue;
6051                 if (lun->be_lun->lun_type == T_CDROM &&
6052                     (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6053                         continue;
6054
6055                 if ((page_index->page_code & SMPH_PC_MASK) !=
6056                     (page_header->page_code & SMPH_PC_MASK))
6057                         continue;
6058
6059                 /*
6060                  * If neither page has a subpage code, then we've got a
6061                  * match.
6062                  */
6063                 if (((page_index->page_code & SMPH_SPF) == 0)
6064                  && ((page_header->page_code & SMPH_SPF) == 0)) {
6065                         page_len = page_header->page_length;
6066                         break;
6067                 }
6068
6069                 /*
6070                  * If both pages have subpages, then the subpage numbers
6071                  * have to match.
6072                  */
6073                 if ((page_index->page_code & SMPH_SPF)
6074                   && (page_header->page_code & SMPH_SPF)) {
6075                         struct scsi_mode_page_header_sp *sph;
6076
6077                         sph = (struct scsi_mode_page_header_sp *)page_header;
6078                         if (page_index->subpage == sph->subpage) {
6079                                 page_len = scsi_2btoul(sph->page_length);
6080                                 break;
6081                         }
6082                 }
6083         }
6084
6085         /*
6086          * If we couldn't find the page, or if we don't have a mode select
6087          * handler for it, send back an error to the user.
6088          */
6089         if ((i >= CTL_NUM_MODE_PAGES)
6090          || (page_index->select_handler == NULL)) {
6091                 ctl_set_invalid_field(ctsio,
6092                                       /*sks_valid*/ 1,
6093                                       /*command*/ 0,
6094                                       /*field*/ *len_used,
6095                                       /*bit_valid*/ 0,
6096                                       /*bit*/ 0);
6097                 free(ctsio->kern_data_ptr, M_CTL);
6098                 ctl_done((union ctl_io *)ctsio);
6099                 return (CTL_RETVAL_COMPLETE);
6100         }
6101
6102         if (page_index->page_code & SMPH_SPF) {
6103                 page_len_offset = 2;
6104                 page_len_size = 2;
6105         } else {
6106                 page_len_size = 1;
6107                 page_len_offset = 1;
6108         }
6109
6110         /*
6111          * If the length the initiator gives us isn't the one we specify in
6112          * the mode page header, or if they didn't specify enough data in
6113          * the CDB to avoid truncating this page, kick out the request.
6114          */
6115         if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6116                 ctl_set_invalid_field(ctsio,
6117                                       /*sks_valid*/ 1,
6118                                       /*command*/ 0,
6119                                       /*field*/ *len_used + page_len_offset,
6120                                       /*bit_valid*/ 0,
6121                                       /*bit*/ 0);
6122                 free(ctsio->kern_data_ptr, M_CTL);
6123                 ctl_done((union ctl_io *)ctsio);
6124                 return (CTL_RETVAL_COMPLETE);
6125         }
6126         if (*len_left < page_index->page_len) {
6127                 free(ctsio->kern_data_ptr, M_CTL);
6128                 ctl_set_param_len_error(ctsio);
6129                 ctl_done((union ctl_io *)ctsio);
6130                 return (CTL_RETVAL_COMPLETE);
6131         }
6132
6133         /*
6134          * Run through the mode page, checking to make sure that the bits
6135          * the user changed are actually legal for him to change.
6136          */
6137         for (i = 0; i < page_index->page_len; i++) {
6138                 uint8_t *user_byte, *change_mask, *current_byte;
6139                 int bad_bit;
6140                 int j;
6141
6142                 user_byte = (uint8_t *)page_header + i;
6143                 change_mask = page_index->page_data +
6144                               (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6145                 current_byte = page_index->page_data +
6146                                (page_index->page_len * CTL_PAGE_CURRENT) + i;
6147
6148                 /*
6149                  * Check to see whether the user set any bits in this byte
6150                  * that he is not allowed to set.
6151                  */
6152                 if ((*user_byte & ~(*change_mask)) ==
6153                     (*current_byte & ~(*change_mask)))
6154                         continue;
6155
6156                 /*
6157                  * Go through bit by bit to determine which one is illegal.
6158                  */
6159                 bad_bit = 0;
6160                 for (j = 7; j >= 0; j--) {
6161                         if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6162                             (((1 << i) & ~(*change_mask)) & *current_byte)) {
6163                                 bad_bit = i;
6164                                 break;
6165                         }
6166                 }
6167                 ctl_set_invalid_field(ctsio,
6168                                       /*sks_valid*/ 1,
6169                                       /*command*/ 0,
6170                                       /*field*/ *len_used + i,
6171                                       /*bit_valid*/ 1,
6172                                       /*bit*/ bad_bit);
6173                 free(ctsio->kern_data_ptr, M_CTL);
6174                 ctl_done((union ctl_io *)ctsio);
6175                 return (CTL_RETVAL_COMPLETE);
6176         }
6177
6178         /*
6179          * Decrement these before we call the page handler, since we may
6180          * end up getting called back one way or another before the handler
6181          * returns to this context.
6182          */
6183         *len_left -= page_index->page_len;
6184         *len_used += page_index->page_len;
6185
6186         retval = page_index->select_handler(ctsio, page_index,
6187                                             (uint8_t *)page_header);
6188
6189         /*
6190          * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6191          * wait until this queued command completes to finish processing
6192          * the mode page.  If it returns anything other than
6193          * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6194          * already set the sense information, freed the data pointer, and
6195          * completed the io for us.
6196          */
6197         if (retval != CTL_RETVAL_COMPLETE)
6198                 goto bailout_no_done;
6199
6200         /*
6201          * If the initiator sent us more than one page, parse the next one.
6202          */
6203         if (*len_left > 0)
6204                 goto do_next_page;
6205
6206         ctl_set_success(ctsio);
6207         free(ctsio->kern_data_ptr, M_CTL);
6208         ctl_done((union ctl_io *)ctsio);
6209
6210 bailout_no_done:
6211
6212         return (CTL_RETVAL_COMPLETE);
6213
6214 }
6215
6216 int
6217 ctl_mode_select(struct ctl_scsiio *ctsio)
6218 {
6219         struct ctl_lun *lun = CTL_LUN(ctsio);
6220         union ctl_modepage_info *modepage_info;
6221         int bd_len, i, header_size, param_len, rtd;
6222         uint32_t initidx;
6223
6224         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6225         switch (ctsio->cdb[0]) {
6226         case MODE_SELECT_6: {
6227                 struct scsi_mode_select_6 *cdb;
6228
6229                 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6230
6231                 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6232                 param_len = cdb->length;
6233                 header_size = sizeof(struct scsi_mode_header_6);
6234                 break;
6235         }
6236         case MODE_SELECT_10: {
6237                 struct scsi_mode_select_10 *cdb;
6238
6239                 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6240
6241                 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6242                 param_len = scsi_2btoul(cdb->length);
6243                 header_size = sizeof(struct scsi_mode_header_10);
6244                 break;
6245         }
6246         default:
6247                 ctl_set_invalid_opcode(ctsio);
6248                 ctl_done((union ctl_io *)ctsio);
6249                 return (CTL_RETVAL_COMPLETE);
6250         }
6251
6252         if (rtd) {
6253                 if (param_len != 0) {
6254                         ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6255                             /*command*/ 1, /*field*/ 0,
6256                             /*bit_valid*/ 0, /*bit*/ 0);
6257                         ctl_done((union ctl_io *)ctsio);
6258                         return (CTL_RETVAL_COMPLETE);
6259                 }
6260
6261                 /* Revert to defaults. */
6262                 ctl_init_page_index(lun);
6263                 mtx_lock(&lun->lun_lock);
6264                 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6265                 mtx_unlock(&lun->lun_lock);
6266                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6267                         ctl_isc_announce_mode(lun, -1,
6268                             lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6269                             lun->mode_pages.index[i].subpage);
6270                 }
6271                 ctl_set_success(ctsio);
6272                 ctl_done((union ctl_io *)ctsio);
6273                 return (CTL_RETVAL_COMPLETE);
6274         }
6275
6276         /*
6277          * From SPC-3:
6278          * "A parameter list length of zero indicates that the Data-Out Buffer
6279          * shall be empty. This condition shall not be considered as an error."
6280          */
6281         if (param_len == 0) {
6282                 ctl_set_success(ctsio);
6283                 ctl_done((union ctl_io *)ctsio);
6284                 return (CTL_RETVAL_COMPLETE);
6285         }
6286
6287         /*
6288          * Since we'll hit this the first time through, prior to
6289          * allocation, we don't need to free a data buffer here.
6290          */
6291         if (param_len < header_size) {
6292                 ctl_set_param_len_error(ctsio);
6293                 ctl_done((union ctl_io *)ctsio);
6294                 return (CTL_RETVAL_COMPLETE);
6295         }
6296
6297         /*
6298          * Allocate the data buffer and grab the user's data.  In theory,
6299          * we shouldn't have to sanity check the parameter list length here
6300          * because the maximum size is 64K.  We should be able to malloc
6301          * that much without too many problems.
6302          */
6303         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6304                 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6305                 ctsio->kern_data_len = param_len;
6306                 ctsio->kern_total_len = param_len;
6307                 ctsio->kern_rel_offset = 0;
6308                 ctsio->kern_sg_entries = 0;
6309                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6310                 ctsio->be_move_done = ctl_config_move_done;
6311                 ctl_datamove((union ctl_io *)ctsio);
6312
6313                 return (CTL_RETVAL_COMPLETE);
6314         }
6315
6316         switch (ctsio->cdb[0]) {
6317         case MODE_SELECT_6: {
6318                 struct scsi_mode_header_6 *mh6;
6319
6320                 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6321                 bd_len = mh6->blk_desc_len;
6322                 break;
6323         }
6324         case MODE_SELECT_10: {
6325                 struct scsi_mode_header_10 *mh10;
6326
6327                 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6328                 bd_len = scsi_2btoul(mh10->blk_desc_len);
6329                 break;
6330         }
6331         default:
6332                 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6333         }
6334
6335         if (param_len < (header_size + bd_len)) {
6336                 free(ctsio->kern_data_ptr, M_CTL);
6337                 ctl_set_param_len_error(ctsio);
6338                 ctl_done((union ctl_io *)ctsio);
6339                 return (CTL_RETVAL_COMPLETE);
6340         }
6341
6342         /*
6343          * Set the IO_CONT flag, so that if this I/O gets passed to
6344          * ctl_config_write_done(), it'll get passed back to
6345          * ctl_do_mode_select() for further processing, or completion if
6346          * we're all done.
6347          */
6348         ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6349         ctsio->io_cont = ctl_do_mode_select;
6350
6351         modepage_info = (union ctl_modepage_info *)
6352                 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6353         memset(modepage_info, 0, sizeof(*modepage_info));
6354         modepage_info->header.len_left = param_len - header_size - bd_len;
6355         modepage_info->header.len_used = header_size + bd_len;
6356
6357         return (ctl_do_mode_select((union ctl_io *)ctsio));
6358 }
6359
6360 int
6361 ctl_mode_sense(struct ctl_scsiio *ctsio)
6362 {
6363         struct ctl_lun *lun = CTL_LUN(ctsio);
6364         int pc, page_code, dbd, subpage;
6365         int alloc_len, page_len, header_len, total_len;
6366         struct scsi_mode_block_descr *block_desc;
6367         struct ctl_page_index *page_index;
6368
6369         dbd = 0;
6370         block_desc = NULL;
6371
6372         CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6373
6374         switch (ctsio->cdb[0]) {
6375         case MODE_SENSE_6: {
6376                 struct scsi_mode_sense_6 *cdb;
6377
6378                 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6379
6380                 header_len = sizeof(struct scsi_mode_hdr_6);
6381                 if (cdb->byte2 & SMS_DBD)
6382                         dbd = 1;
6383                 else
6384                         header_len += sizeof(struct scsi_mode_block_descr);
6385
6386                 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6387                 page_code = cdb->page & SMS_PAGE_CODE;
6388                 subpage = cdb->subpage;
6389                 alloc_len = cdb->length;
6390                 break;
6391         }
6392         case MODE_SENSE_10: {
6393                 struct scsi_mode_sense_10 *cdb;
6394
6395                 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6396
6397                 header_len = sizeof(struct scsi_mode_hdr_10);
6398
6399                 if (cdb->byte2 & SMS_DBD)
6400                         dbd = 1;
6401                 else
6402                         header_len += sizeof(struct scsi_mode_block_descr);
6403                 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6404                 page_code = cdb->page & SMS_PAGE_CODE;
6405                 subpage = cdb->subpage;
6406                 alloc_len = scsi_2btoul(cdb->length);
6407                 break;
6408         }
6409         default:
6410                 ctl_set_invalid_opcode(ctsio);
6411                 ctl_done((union ctl_io *)ctsio);
6412                 return (CTL_RETVAL_COMPLETE);
6413                 break; /* NOTREACHED */
6414         }
6415
6416         /*
6417          * We have to make a first pass through to calculate the size of
6418          * the pages that match the user's query.  Then we allocate enough
6419          * memory to hold it, and actually copy the data into the buffer.
6420          */
6421         switch (page_code) {
6422         case SMS_ALL_PAGES_PAGE: {
6423                 u_int i;
6424
6425                 page_len = 0;
6426
6427                 /*
6428                  * At the moment, values other than 0 and 0xff here are
6429                  * reserved according to SPC-3.
6430                  */
6431                 if ((subpage != SMS_SUBPAGE_PAGE_0)
6432                  && (subpage != SMS_SUBPAGE_ALL)) {
6433                         ctl_set_invalid_field(ctsio,
6434                                               /*sks_valid*/ 1,
6435                                               /*command*/ 1,
6436                                               /*field*/ 3,
6437                                               /*bit_valid*/ 0,
6438                                               /*bit*/ 0);
6439                         ctl_done((union ctl_io *)ctsio);
6440                         return (CTL_RETVAL_COMPLETE);
6441                 }
6442
6443                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6444                         page_index = &lun->mode_pages.index[i];
6445
6446                         /* Make sure the page is supported for this dev type */
6447                         if (lun->be_lun->lun_type == T_DIRECT &&
6448                             (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6449                                 continue;
6450                         if (lun->be_lun->lun_type == T_PROCESSOR &&
6451                             (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6452                                 continue;
6453                         if (lun->be_lun->lun_type == T_CDROM &&
6454                             (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6455                                 continue;
6456
6457                         /*
6458                          * We don't use this subpage if the user didn't
6459                          * request all subpages.
6460                          */
6461                         if ((page_index->subpage != 0)
6462                          && (subpage == SMS_SUBPAGE_PAGE_0))
6463                                 continue;
6464
6465                         page_len += page_index->page_len;
6466                 }
6467                 break;
6468         }
6469         default: {
6470                 u_int i;
6471
6472                 page_len = 0;
6473
6474                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6475                         page_index = &lun->mode_pages.index[i];
6476
6477                         /* Make sure the page is supported for this dev type */
6478                         if (lun->be_lun->lun_type == T_DIRECT &&
6479                             (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6480                                 continue;
6481                         if (lun->be_lun->lun_type == T_PROCESSOR &&
6482                             (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6483                                 continue;
6484                         if (lun->be_lun->lun_type == T_CDROM &&
6485                             (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6486                                 continue;
6487
6488                         /* Look for the right page code */
6489                         if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6490                                 continue;
6491
6492                         /* Look for the right subpage or the subpage wildcard*/
6493                         if ((page_index->subpage != subpage)
6494                          && (subpage != SMS_SUBPAGE_ALL))
6495                                 continue;
6496
6497                         page_len += page_index->page_len;
6498                 }
6499
6500                 if (page_len == 0) {
6501                         ctl_set_invalid_field(ctsio,
6502                                               /*sks_valid*/ 1,
6503                                               /*command*/ 1,
6504                                               /*field*/ 2,
6505                                               /*bit_valid*/ 1,
6506                                               /*bit*/ 5);
6507                         ctl_done((union ctl_io *)ctsio);
6508                         return (CTL_RETVAL_COMPLETE);
6509                 }
6510                 break;
6511         }
6512         }
6513
6514         total_len = header_len + page_len;
6515
6516         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6517         ctsio->kern_sg_entries = 0;
6518         ctsio->kern_rel_offset = 0;
6519         ctsio->kern_data_len = min(total_len, alloc_len);
6520         ctsio->kern_total_len = ctsio->kern_data_len;
6521
6522         switch (ctsio->cdb[0]) {
6523         case MODE_SENSE_6: {
6524                 struct scsi_mode_hdr_6 *header;
6525
6526                 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6527
6528                 header->datalen = MIN(total_len - 1, 254);
6529                 if (lun->be_lun->lun_type == T_DIRECT) {
6530                         header->dev_specific = 0x10; /* DPOFUA */
6531                         if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6532                             (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6533                                 header->dev_specific |= 0x80; /* WP */
6534                 }
6535                 if (dbd)
6536                         header->block_descr_len = 0;
6537                 else
6538                         header->block_descr_len =
6539                                 sizeof(struct scsi_mode_block_descr);
6540                 block_desc = (struct scsi_mode_block_descr *)&header[1];
6541                 break;
6542         }
6543         case MODE_SENSE_10: {
6544                 struct scsi_mode_hdr_10 *header;
6545                 int datalen;
6546
6547                 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6548
6549                 datalen = MIN(total_len - 2, 65533);
6550                 scsi_ulto2b(datalen, header->datalen);
6551                 if (lun->be_lun->lun_type == T_DIRECT) {
6552                         header->dev_specific = 0x10; /* DPOFUA */
6553                         if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6554                             (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6555                                 header->dev_specific |= 0x80; /* WP */
6556                 }
6557                 if (dbd)
6558                         scsi_ulto2b(0, header->block_descr_len);
6559                 else
6560                         scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6561                                     header->block_descr_len);
6562                 block_desc = (struct scsi_mode_block_descr *)&header[1];
6563                 break;
6564         }
6565         default:
6566                 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6567         }
6568
6569         /*
6570          * If we've got a disk, use its blocksize in the block
6571          * descriptor.  Otherwise, just set it to 0.
6572          */
6573         if (dbd == 0) {
6574                 if (lun->be_lun->lun_type == T_DIRECT)
6575                         scsi_ulto3b(lun->be_lun->blocksize,
6576                                     block_desc->block_len);
6577                 else
6578                         scsi_ulto3b(0, block_desc->block_len);
6579         }
6580
6581         switch (page_code) {
6582         case SMS_ALL_PAGES_PAGE: {
6583                 int i, data_used;
6584
6585                 data_used = header_len;
6586                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6587                         struct ctl_page_index *page_index;
6588
6589                         page_index = &lun->mode_pages.index[i];
6590                         if (lun->be_lun->lun_type == T_DIRECT &&
6591                             (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6592                                 continue;
6593                         if (lun->be_lun->lun_type == T_PROCESSOR &&
6594                             (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6595                                 continue;
6596                         if (lun->be_lun->lun_type == T_CDROM &&
6597                             (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6598                                 continue;
6599
6600                         /*
6601                          * We don't use this subpage if the user didn't
6602                          * request all subpages.  We already checked (above)
6603                          * to make sure the user only specified a subpage
6604                          * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6605                          */
6606                         if ((page_index->subpage != 0)
6607                          && (subpage == SMS_SUBPAGE_PAGE_0))
6608                                 continue;
6609
6610                         /*
6611                          * Call the handler, if it exists, to update the
6612                          * page to the latest values.
6613                          */
6614                         if (page_index->sense_handler != NULL)
6615                                 page_index->sense_handler(ctsio, page_index,pc);
6616
6617                         memcpy(ctsio->kern_data_ptr + data_used,
6618                                page_index->page_data +
6619                                (page_index->page_len * pc),
6620                                page_index->page_len);
6621                         data_used += page_index->page_len;
6622                 }
6623                 break;
6624         }
6625         default: {
6626                 int i, data_used;
6627
6628                 data_used = header_len;
6629
6630                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6631                         struct ctl_page_index *page_index;
6632
6633                         page_index = &lun->mode_pages.index[i];
6634
6635                         /* Look for the right page code */
6636                         if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6637                                 continue;
6638
6639                         /* Look for the right subpage or the subpage wildcard*/
6640                         if ((page_index->subpage != subpage)
6641                          && (subpage != SMS_SUBPAGE_ALL))
6642                                 continue;
6643
6644                         /* Make sure the page is supported for this dev type */
6645                         if (lun->be_lun->lun_type == T_DIRECT &&
6646                             (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6647                                 continue;
6648                         if (lun->be_lun->lun_type == T_PROCESSOR &&
6649                             (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6650                                 continue;
6651                         if (lun->be_lun->lun_type == T_CDROM &&
6652                             (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6653                                 continue;
6654
6655                         /*
6656                          * Call the handler, if it exists, to update the
6657                          * page to the latest values.
6658                          */
6659                         if (page_index->sense_handler != NULL)
6660                                 page_index->sense_handler(ctsio, page_index,pc);
6661
6662                         memcpy(ctsio->kern_data_ptr + data_used,
6663                                page_index->page_data +
6664                                (page_index->page_len * pc),
6665                                page_index->page_len);
6666                         data_used += page_index->page_len;
6667                 }
6668                 break;
6669         }
6670         }
6671
6672         ctl_set_success(ctsio);
6673         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6674         ctsio->be_move_done = ctl_config_move_done;
6675         ctl_datamove((union ctl_io *)ctsio);
6676         return (CTL_RETVAL_COMPLETE);
6677 }
6678
6679 int
6680 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6681                                struct ctl_page_index *page_index,
6682                                int pc)
6683 {
6684         struct ctl_lun *lun = CTL_LUN(ctsio);
6685         struct scsi_log_param_header *phdr;
6686         uint8_t *data;
6687         uint64_t val;
6688
6689         data = page_index->page_data;
6690
6691         if (lun->backend->lun_attr != NULL &&
6692             (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6693              != UINT64_MAX) {
6694                 phdr = (struct scsi_log_param_header *)data;
6695                 scsi_ulto2b(0x0001, phdr->param_code);
6696                 phdr->param_control = SLP_LBIN | SLP_LP;
6697                 phdr->param_len = 8;
6698                 data = (uint8_t *)(phdr + 1);
6699                 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6700                 data[4] = 0x02; /* per-pool */
6701                 data += phdr->param_len;
6702         }
6703
6704         if (lun->backend->lun_attr != NULL &&
6705             (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6706              != UINT64_MAX) {
6707                 phdr = (struct scsi_log_param_header *)data;
6708                 scsi_ulto2b(0x0002, phdr->param_code);
6709                 phdr->param_control = SLP_LBIN | SLP_LP;
6710                 phdr->param_len = 8;
6711                 data = (uint8_t *)(phdr + 1);
6712                 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6713                 data[4] = 0x01; /* per-LUN */
6714                 data += phdr->param_len;
6715         }
6716
6717         if (lun->backend->lun_attr != NULL &&
6718             (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6719              != UINT64_MAX) {
6720                 phdr = (struct scsi_log_param_header *)data;
6721                 scsi_ulto2b(0x00f1, phdr->param_code);
6722                 phdr->param_control = SLP_LBIN | SLP_LP;
6723                 phdr->param_len = 8;
6724                 data = (uint8_t *)(phdr + 1);
6725                 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6726                 data[4] = 0x02; /* per-pool */
6727                 data += phdr->param_len;
6728         }
6729
6730         if (lun->backend->lun_attr != NULL &&
6731             (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6732              != UINT64_MAX) {
6733                 phdr = (struct scsi_log_param_header *)data;
6734                 scsi_ulto2b(0x00f2, phdr->param_code);
6735                 phdr->param_control = SLP_LBIN | SLP_LP;
6736                 phdr->param_len = 8;
6737                 data = (uint8_t *)(phdr + 1);
6738                 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6739                 data[4] = 0x02; /* per-pool */
6740                 data += phdr->param_len;
6741         }
6742
6743         page_index->page_len = data - page_index->page_data;
6744         return (0);
6745 }
6746
6747 int
6748 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6749                                struct ctl_page_index *page_index,
6750                                int pc)
6751 {
6752         struct ctl_lun *lun = CTL_LUN(ctsio);
6753         struct stat_page *data;
6754         struct bintime *t;
6755
6756         data = (struct stat_page *)page_index->page_data;
6757
6758         scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6759         data->sap.hdr.param_control = SLP_LBIN;
6760         data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6761             sizeof(struct scsi_log_param_header);
6762         scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6763             data->sap.read_num);
6764         scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6765             data->sap.write_num);
6766         if (lun->be_lun->blocksize > 0) {
6767                 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6768                     lun->be_lun->blocksize, data->sap.recvieved_lba);
6769                 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6770                     lun->be_lun->blocksize, data->sap.transmitted_lba);
6771         }
6772         t = &lun->stats.time[CTL_STATS_READ];
6773         scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6774             data->sap.read_int);
6775         t = &lun->stats.time[CTL_STATS_WRITE];
6776         scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6777             data->sap.write_int);
6778         scsi_u64to8b(0, data->sap.weighted_num);
6779         scsi_u64to8b(0, data->sap.weighted_int);
6780         scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6781         data->it.hdr.param_control = SLP_LBIN;
6782         data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6783             sizeof(struct scsi_log_param_header);
6784 #ifdef CTL_TIME_IO
6785         scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6786 #endif
6787         scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6788         data->it.hdr.param_control = SLP_LBIN;
6789         data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6790             sizeof(struct scsi_log_param_header);
6791         scsi_ulto4b(3, data->ti.exponent);
6792         scsi_ulto4b(1, data->ti.integer);
6793         return (0);
6794 }
6795
6796 int
6797 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6798                                struct ctl_page_index *page_index,
6799                                int pc)
6800 {
6801         struct ctl_lun *lun = CTL_LUN(ctsio);
6802         struct scsi_log_informational_exceptions *data;
6803
6804         data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6805
6806         scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6807         data->hdr.param_control = SLP_LBIN;
6808         data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6809             sizeof(struct scsi_log_param_header);
6810         data->ie_asc = lun->ie_asc;
6811         data->ie_ascq = lun->ie_ascq;
6812         data->temperature = 0xff;
6813         return (0);
6814 }
6815
6816 int
6817 ctl_log_sense(struct ctl_scsiio *ctsio)
6818 {
6819         struct ctl_lun *lun = CTL_LUN(ctsio);
6820         int i, pc, page_code, subpage;
6821         int alloc_len, total_len;
6822         struct ctl_page_index *page_index;
6823         struct scsi_log_sense *cdb;
6824         struct scsi_log_header *header;
6825
6826         CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6827
6828         cdb = (struct scsi_log_sense *)ctsio->cdb;
6829         pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6830         page_code = cdb->page & SLS_PAGE_CODE;
6831         subpage = cdb->subpage;
6832         alloc_len = scsi_2btoul(cdb->length);
6833
6834         page_index = NULL;
6835         for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6836                 page_index = &lun->log_pages.index[i];
6837
6838                 /* Look for the right page code */
6839                 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6840                         continue;
6841
6842                 /* Look for the right subpage or the subpage wildcard*/
6843                 if (page_index->subpage != subpage)
6844                         continue;
6845
6846                 break;
6847         }
6848         if (i >= CTL_NUM_LOG_PAGES) {
6849                 ctl_set_invalid_field(ctsio,
6850                                       /*sks_valid*/ 1,
6851                                       /*command*/ 1,
6852                                       /*field*/ 2,
6853                                       /*bit_valid*/ 0,
6854                                       /*bit*/ 0);
6855                 ctl_done((union ctl_io *)ctsio);
6856                 return (CTL_RETVAL_COMPLETE);
6857         }
6858
6859         total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6860
6861         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6862         ctsio->kern_sg_entries = 0;
6863         ctsio->kern_rel_offset = 0;
6864         ctsio->kern_data_len = min(total_len, alloc_len);
6865         ctsio->kern_total_len = ctsio->kern_data_len;
6866
6867         header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6868         header->page = page_index->page_code;
6869         if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6870                 header->page |= SL_DS;
6871         if (page_index->subpage) {
6872                 header->page |= SL_SPF;
6873                 header->subpage = page_index->subpage;
6874         }
6875         scsi_ulto2b(page_index->page_len, header->datalen);
6876
6877         /*
6878          * Call the handler, if it exists, to update the
6879          * page to the latest values.
6880          */
6881         if (page_index->sense_handler != NULL)
6882                 page_index->sense_handler(ctsio, page_index, pc);
6883
6884         memcpy(header + 1, page_index->page_data, page_index->page_len);
6885
6886         ctl_set_success(ctsio);
6887         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6888         ctsio->be_move_done = ctl_config_move_done;
6889         ctl_datamove((union ctl_io *)ctsio);
6890         return (CTL_RETVAL_COMPLETE);
6891 }
6892
6893 int
6894 ctl_read_capacity(struct ctl_scsiio *ctsio)
6895 {
6896         struct ctl_lun *lun = CTL_LUN(ctsio);
6897         struct scsi_read_capacity *cdb;
6898         struct scsi_read_capacity_data *data;
6899         uint32_t lba;
6900
6901         CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6902
6903         cdb = (struct scsi_read_capacity *)ctsio->cdb;
6904
6905         lba = scsi_4btoul(cdb->addr);
6906         if (((cdb->pmi & SRC_PMI) == 0)
6907          && (lba != 0)) {
6908                 ctl_set_invalid_field(/*ctsio*/ ctsio,
6909                                       /*sks_valid*/ 1,
6910                                       /*command*/ 1,
6911                                       /*field*/ 2,
6912                                       /*bit_valid*/ 0,
6913                                       /*bit*/ 0);
6914                 ctl_done((union ctl_io *)ctsio);
6915                 return (CTL_RETVAL_COMPLETE);
6916         }
6917
6918         ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6919         data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6920         ctsio->kern_data_len = sizeof(*data);
6921         ctsio->kern_total_len = sizeof(*data);
6922         ctsio->kern_rel_offset = 0;
6923         ctsio->kern_sg_entries = 0;
6924
6925         /*
6926          * If the maximum LBA is greater than 0xfffffffe, the user must
6927          * issue a SERVICE ACTION IN (16) command, with the read capacity
6928          * serivce action set.
6929          */
6930         if (lun->be_lun->maxlba > 0xfffffffe)
6931                 scsi_ulto4b(0xffffffff, data->addr);
6932         else
6933                 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6934
6935         /*
6936          * XXX KDM this may not be 512 bytes...
6937          */
6938         scsi_ulto4b(lun->be_lun->blocksize, data->length);
6939
6940         ctl_set_success(ctsio);
6941         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6942         ctsio->be_move_done = ctl_config_move_done;
6943         ctl_datamove((union ctl_io *)ctsio);
6944         return (CTL_RETVAL_COMPLETE);
6945 }
6946
6947 int
6948 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6949 {
6950         struct ctl_lun *lun = CTL_LUN(ctsio);
6951         struct scsi_read_capacity_16 *cdb;
6952         struct scsi_read_capacity_data_long *data;
6953         uint64_t lba;
6954         uint32_t alloc_len;
6955
6956         CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6957
6958         cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6959
6960         alloc_len = scsi_4btoul(cdb->alloc_len);
6961         lba = scsi_8btou64(cdb->addr);
6962
6963         if ((cdb->reladr & SRC16_PMI)
6964          && (lba != 0)) {
6965                 ctl_set_invalid_field(/*ctsio*/ ctsio,
6966                                       /*sks_valid*/ 1,
6967                                       /*command*/ 1,
6968                                       /*field*/ 2,
6969                                       /*bit_valid*/ 0,
6970                                       /*bit*/ 0);
6971                 ctl_done((union ctl_io *)ctsio);
6972                 return (CTL_RETVAL_COMPLETE);
6973         }
6974
6975         ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6976         data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6977         ctsio->kern_rel_offset = 0;
6978         ctsio->kern_sg_entries = 0;
6979         ctsio->kern_data_len = min(sizeof(*data), alloc_len);
6980         ctsio->kern_total_len = ctsio->kern_data_len;
6981
6982         scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6983         /* XXX KDM this may not be 512 bytes... */
6984         scsi_ulto4b(lun->be_lun->blocksize, data->length);
6985         data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6986         scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6987         if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6988                 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6989
6990         ctl_set_success(ctsio);
6991         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6992         ctsio->be_move_done = ctl_config_move_done;
6993         ctl_datamove((union ctl_io *)ctsio);
6994         return (CTL_RETVAL_COMPLETE);
6995 }
6996
6997 int
6998 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6999 {
7000         struct ctl_lun *lun = CTL_LUN(ctsio);
7001         struct scsi_get_lba_status *cdb;
7002         struct scsi_get_lba_status_data *data;
7003         struct ctl_lba_len_flags *lbalen;
7004         uint64_t lba;
7005         uint32_t alloc_len, total_len;
7006         int retval;
7007
7008         CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7009
7010         cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7011         lba = scsi_8btou64(cdb->addr);
7012         alloc_len = scsi_4btoul(cdb->alloc_len);
7013
7014         if (lba > lun->be_lun->maxlba) {
7015                 ctl_set_lba_out_of_range(ctsio, lba);
7016                 ctl_done((union ctl_io *)ctsio);
7017                 return (CTL_RETVAL_COMPLETE);
7018         }
7019
7020         total_len = sizeof(*data) + sizeof(data->descr[0]);
7021         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7022         data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7023         ctsio->kern_rel_offset = 0;
7024         ctsio->kern_sg_entries = 0;
7025         ctsio->kern_data_len = min(total_len, alloc_len);
7026         ctsio->kern_total_len = ctsio->kern_data_len;
7027
7028         /* Fill dummy data in case backend can't tell anything. */
7029         scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7030         scsi_u64to8b(lba, data->descr[0].addr);
7031         scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7032             data->descr[0].length);
7033         data->descr[0].status = 0; /* Mapped or unknown. */
7034
7035         ctl_set_success(ctsio);
7036         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7037         ctsio->be_move_done = ctl_config_move_done;
7038
7039         lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7040         lbalen->lba = lba;
7041         lbalen->len = total_len;
7042         lbalen->flags = 0;
7043         retval = lun->backend->config_read((union ctl_io *)ctsio);
7044         return (retval);
7045 }
7046
7047 int
7048 ctl_read_defect(struct ctl_scsiio *ctsio)
7049 {
7050         struct scsi_read_defect_data_10 *ccb10;
7051         struct scsi_read_defect_data_12 *ccb12;
7052         struct scsi_read_defect_data_hdr_10 *data10;
7053         struct scsi_read_defect_data_hdr_12 *data12;
7054         uint32_t alloc_len, data_len;
7055         uint8_t format;
7056
7057         CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7058
7059         if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7060                 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7061                 format = ccb10->format;
7062                 alloc_len = scsi_2btoul(ccb10->alloc_length);
7063                 data_len = sizeof(*data10);
7064         } else {
7065                 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7066                 format = ccb12->format;
7067                 alloc_len = scsi_4btoul(ccb12->alloc_length);
7068                 data_len = sizeof(*data12);
7069         }
7070         if (alloc_len == 0) {
7071                 ctl_set_success(ctsio);
7072                 ctl_done((union ctl_io *)ctsio);
7073                 return (CTL_RETVAL_COMPLETE);
7074         }
7075
7076         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7077         ctsio->kern_rel_offset = 0;
7078         ctsio->kern_sg_entries = 0;
7079         ctsio->kern_data_len = min(data_len, alloc_len);
7080         ctsio->kern_total_len = ctsio->kern_data_len;
7081
7082         if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7083                 data10 = (struct scsi_read_defect_data_hdr_10 *)
7084                     ctsio->kern_data_ptr;
7085                 data10->format = format;
7086                 scsi_ulto2b(0, data10->length);
7087         } else {
7088                 data12 = (struct scsi_read_defect_data_hdr_12 *)
7089                     ctsio->kern_data_ptr;
7090                 data12->format = format;
7091                 scsi_ulto2b(0, data12->generation);
7092                 scsi_ulto4b(0, data12->length);
7093         }
7094
7095         ctl_set_success(ctsio);
7096         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7097         ctsio->be_move_done = ctl_config_move_done;
7098         ctl_datamove((union ctl_io *)ctsio);
7099         return (CTL_RETVAL_COMPLETE);
7100 }
7101
7102 int
7103 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7104 {
7105         struct ctl_softc *softc = CTL_SOFTC(ctsio);
7106         struct ctl_lun *lun = CTL_LUN(ctsio);
7107         struct scsi_maintenance_in *cdb;
7108         int retval;
7109         int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7110         int num_ha_groups, num_target_ports, shared_group;
7111         struct ctl_port *port;
7112         struct scsi_target_group_data *rtg_ptr;
7113         struct scsi_target_group_data_extended *rtg_ext_ptr;
7114         struct scsi_target_port_group_descriptor *tpg_desc;
7115
7116         CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7117
7118         cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7119         retval = CTL_RETVAL_COMPLETE;
7120
7121         switch (cdb->byte2 & STG_PDF_MASK) {
7122         case STG_PDF_LENGTH:
7123                 ext = 0;
7124                 break;
7125         case STG_PDF_EXTENDED:
7126                 ext = 1;
7127                 break;
7128         default:
7129                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7130                                       /*sks_valid*/ 1,
7131                                       /*command*/ 1,
7132                                       /*field*/ 2,
7133                                       /*bit_valid*/ 1,
7134                                       /*bit*/ 5);
7135                 ctl_done((union ctl_io *)ctsio);
7136                 return(retval);
7137         }
7138
7139         num_target_ports = 0;
7140         shared_group = (softc->is_single != 0);
7141         mtx_lock(&softc->ctl_lock);
7142         STAILQ_FOREACH(port, &softc->port_list, links) {
7143                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7144                         continue;
7145                 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7146                         continue;
7147                 num_target_ports++;
7148                 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7149                         shared_group = 1;
7150         }
7151         mtx_unlock(&softc->ctl_lock);
7152         num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7153
7154         if (ext)
7155                 total_len = sizeof(struct scsi_target_group_data_extended);
7156         else
7157                 total_len = sizeof(struct scsi_target_group_data);
7158         total_len += sizeof(struct scsi_target_port_group_descriptor) *
7159                 (shared_group + num_ha_groups) +
7160             sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7161
7162         alloc_len = scsi_4btoul(cdb->length);
7163
7164         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7165         ctsio->kern_sg_entries = 0;
7166         ctsio->kern_rel_offset = 0;
7167         ctsio->kern_data_len = min(total_len, alloc_len);
7168         ctsio->kern_total_len = ctsio->kern_data_len;
7169
7170         if (ext) {
7171                 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7172                     ctsio->kern_data_ptr;
7173                 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7174                 rtg_ext_ptr->format_type = 0x10;
7175                 rtg_ext_ptr->implicit_transition_time = 0;
7176                 tpg_desc = &rtg_ext_ptr->groups[0];
7177         } else {
7178                 rtg_ptr = (struct scsi_target_group_data *)
7179                     ctsio->kern_data_ptr;
7180                 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7181                 tpg_desc = &rtg_ptr->groups[0];
7182         }
7183
7184         mtx_lock(&softc->ctl_lock);
7185         pg = softc->port_min / softc->port_cnt;
7186         if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7187                 /* Some shelf is known to be primary. */
7188                 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7189                         os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7190                 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7191                         os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7192                 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7193                         os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7194                 else
7195                         os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7196                 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7197                         ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7198                 } else {
7199                         ts = os;
7200                         os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7201                 }
7202         } else {
7203                 /* No known primary shelf. */
7204                 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7205                         ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7206                         os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7207                 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7208                         ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7209                         os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7210                 } else {
7211                         ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7212                 }
7213         }
7214         if (shared_group) {
7215                 tpg_desc->pref_state = ts;
7216                 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7217                     TPG_U_SUP | TPG_T_SUP;
7218                 scsi_ulto2b(1, tpg_desc->target_port_group);
7219                 tpg_desc->status = TPG_IMPLICIT;
7220                 pc = 0;
7221                 STAILQ_FOREACH(port, &softc->port_list, links) {
7222                         if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7223                                 continue;
7224                         if (!softc->is_single &&
7225                             (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7226                                 continue;
7227                         if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7228                                 continue;
7229                         scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7230                             relative_target_port_identifier);
7231                         pc++;
7232                 }
7233                 tpg_desc->target_port_count = pc;
7234                 tpg_desc = (struct scsi_target_port_group_descriptor *)
7235                     &tpg_desc->descriptors[pc];
7236         }
7237         for (g = 0; g < num_ha_groups; g++) {
7238                 tpg_desc->pref_state = (g == pg) ? ts : os;
7239                 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7240                     TPG_U_SUP | TPG_T_SUP;
7241                 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7242                 tpg_desc->status = TPG_IMPLICIT;
7243                 pc = 0;
7244                 STAILQ_FOREACH(port, &softc->port_list, links) {
7245                         if (port->targ_port < g * softc->port_cnt ||
7246                             port->targ_port >= (g + 1) * softc->port_cnt)
7247                                 continue;
7248                         if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7249                                 continue;
7250                         if (port->status & CTL_PORT_STATUS_HA_SHARED)
7251                                 continue;
7252                         if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7253                                 continue;
7254                         scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7255                             relative_target_port_identifier);
7256                         pc++;
7257                 }
7258                 tpg_desc->target_port_count = pc;
7259                 tpg_desc = (struct scsi_target_port_group_descriptor *)
7260                     &tpg_desc->descriptors[pc];
7261         }
7262         mtx_unlock(&softc->ctl_lock);
7263
7264         ctl_set_success(ctsio);
7265         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7266         ctsio->be_move_done = ctl_config_move_done;
7267         ctl_datamove((union ctl_io *)ctsio);
7268         return(retval);
7269 }
7270
7271 int
7272 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7273 {
7274         struct ctl_lun *lun = CTL_LUN(ctsio);
7275         struct scsi_report_supported_opcodes *cdb;
7276         const struct ctl_cmd_entry *entry, *sentry;
7277         struct scsi_report_supported_opcodes_all *all;
7278         struct scsi_report_supported_opcodes_descr *descr;
7279         struct scsi_report_supported_opcodes_one *one;
7280         int retval;
7281         int alloc_len, total_len;
7282         int opcode, service_action, i, j, num;
7283
7284         CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7285
7286         cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7287         retval = CTL_RETVAL_COMPLETE;
7288
7289         opcode = cdb->requested_opcode;
7290         service_action = scsi_2btoul(cdb->requested_service_action);
7291         switch (cdb->options & RSO_OPTIONS_MASK) {
7292         case RSO_OPTIONS_ALL:
7293                 num = 0;
7294                 for (i = 0; i < 256; i++) {
7295                         entry = &ctl_cmd_table[i];
7296                         if (entry->flags & CTL_CMD_FLAG_SA5) {
7297                                 for (j = 0; j < 32; j++) {
7298                                         sentry = &((const struct ctl_cmd_entry *)
7299                                             entry->execute)[j];
7300                                         if (ctl_cmd_applicable(
7301                                             lun->be_lun->lun_type, sentry))
7302                                                 num++;
7303                                 }
7304                         } else {
7305                                 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7306                                     entry))
7307                                         num++;
7308                         }
7309                 }
7310                 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7311                     num * sizeof(struct scsi_report_supported_opcodes_descr);
7312                 break;
7313         case RSO_OPTIONS_OC:
7314                 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7315                         ctl_set_invalid_field(/*ctsio*/ ctsio,
7316                                               /*sks_valid*/ 1,
7317                                               /*command*/ 1,
7318                                               /*field*/ 2,
7319                                               /*bit_valid*/ 1,
7320                                               /*bit*/ 2);
7321                         ctl_done((union ctl_io *)ctsio);
7322                         return (CTL_RETVAL_COMPLETE);
7323                 }
7324                 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7325                 break;
7326         case RSO_OPTIONS_OC_SA:
7327                 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7328                     service_action >= 32) {
7329                         ctl_set_invalid_field(/*ctsio*/ ctsio,
7330                                               /*sks_valid*/ 1,
7331                                               /*command*/ 1,
7332                                               /*field*/ 2,
7333                                               /*bit_valid*/ 1,
7334                                               /*bit*/ 2);
7335                         ctl_done((union ctl_io *)ctsio);
7336                         return (CTL_RETVAL_COMPLETE);
7337                 }
7338                 /* FALLTHROUGH */
7339         case RSO_OPTIONS_OC_ASA:
7340                 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7341                 break;
7342         default:
7343                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7344                                       /*sks_valid*/ 1,
7345                                       /*command*/ 1,
7346                                       /*field*/ 2,
7347                                       /*bit_valid*/ 1,
7348                                       /*bit*/ 2);
7349                 ctl_done((union ctl_io *)ctsio);
7350                 return (CTL_RETVAL_COMPLETE);
7351         }
7352
7353         alloc_len = scsi_4btoul(cdb->length);
7354
7355         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7356         ctsio->kern_sg_entries = 0;
7357         ctsio->kern_rel_offset = 0;
7358         ctsio->kern_data_len = min(total_len, alloc_len);
7359         ctsio->kern_total_len = ctsio->kern_data_len;
7360
7361         switch (cdb->options & RSO_OPTIONS_MASK) {
7362         case RSO_OPTIONS_ALL:
7363                 all = (struct scsi_report_supported_opcodes_all *)
7364                     ctsio->kern_data_ptr;
7365                 num = 0;
7366                 for (i = 0; i < 256; i++) {
7367                         entry = &ctl_cmd_table[i];
7368                         if (entry->flags & CTL_CMD_FLAG_SA5) {
7369                                 for (j = 0; j < 32; j++) {
7370                                         sentry = &((const struct ctl_cmd_entry *)
7371                                             entry->execute)[j];
7372                                         if (!ctl_cmd_applicable(
7373                                             lun->be_lun->lun_type, sentry))
7374                                                 continue;
7375                                         descr = &all->descr[num++];
7376                                         descr->opcode = i;
7377                                         scsi_ulto2b(j, descr->service_action);
7378                                         descr->flags = RSO_SERVACTV;
7379                                         scsi_ulto2b(sentry->length,
7380                                             descr->cdb_length);
7381                                 }
7382                         } else {
7383                                 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7384                                     entry))
7385                                         continue;
7386                                 descr = &all->descr[num++];
7387                                 descr->opcode = i;
7388                                 scsi_ulto2b(0, descr->service_action);
7389                                 descr->flags = 0;
7390                                 scsi_ulto2b(entry->length, descr->cdb_length);
7391                         }
7392                 }
7393                 scsi_ulto4b(
7394                     num * sizeof(struct scsi_report_supported_opcodes_descr),
7395                     all->length);
7396                 break;
7397         case RSO_OPTIONS_OC:
7398                 one = (struct scsi_report_supported_opcodes_one *)
7399                     ctsio->kern_data_ptr;
7400                 entry = &ctl_cmd_table[opcode];
7401                 goto fill_one;
7402         case RSO_OPTIONS_OC_SA:
7403                 one = (struct scsi_report_supported_opcodes_one *)
7404                     ctsio->kern_data_ptr;
7405                 entry = &ctl_cmd_table[opcode];
7406                 entry = &((const struct ctl_cmd_entry *)
7407                     entry->execute)[service_action];
7408 fill_one:
7409                 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7410                         one->support = 3;
7411                         scsi_ulto2b(entry->length, one->cdb_length);
7412                         one->cdb_usage[0] = opcode;
7413                         memcpy(&one->cdb_usage[1], entry->usage,
7414                             entry->length - 1);
7415                 } else
7416                         one->support = 1;
7417                 break;
7418         case RSO_OPTIONS_OC_ASA:
7419                 one = (struct scsi_report_supported_opcodes_one *)
7420                     ctsio->kern_data_ptr;
7421                 entry = &ctl_cmd_table[opcode];
7422                 if (entry->flags & CTL_CMD_FLAG_SA5) {
7423                         entry = &((const struct ctl_cmd_entry *)
7424                             entry->execute)[service_action];
7425                 } else if (service_action != 0) {
7426                         one->support = 1;
7427                         break;
7428                 }
7429                 goto fill_one;
7430         }
7431
7432         ctl_set_success(ctsio);
7433         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7434         ctsio->be_move_done = ctl_config_move_done;
7435         ctl_datamove((union ctl_io *)ctsio);
7436         return(retval);
7437 }
7438
7439 int
7440 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7441 {
7442         struct scsi_report_supported_tmf *cdb;
7443         struct scsi_report_supported_tmf_ext_data *data;
7444         int retval;
7445         int alloc_len, total_len;
7446
7447         CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7448
7449         cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7450
7451         retval = CTL_RETVAL_COMPLETE;
7452
7453         if (cdb->options & RST_REPD)
7454                 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7455         else
7456                 total_len = sizeof(struct scsi_report_supported_tmf_data);
7457         alloc_len = scsi_4btoul(cdb->length);
7458
7459         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7460         ctsio->kern_sg_entries = 0;
7461         ctsio->kern_rel_offset = 0;
7462         ctsio->kern_data_len = min(total_len, alloc_len);
7463         ctsio->kern_total_len = ctsio->kern_data_len;
7464
7465         data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7466         data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7467             RST_TRS;
7468         data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7469         data->length = total_len - 4;
7470
7471         ctl_set_success(ctsio);
7472         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7473         ctsio->be_move_done = ctl_config_move_done;
7474         ctl_datamove((union ctl_io *)ctsio);
7475         return (retval);
7476 }
7477
7478 int
7479 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7480 {
7481         struct scsi_report_timestamp *cdb;
7482         struct scsi_report_timestamp_data *data;
7483         struct timeval tv;
7484         int64_t timestamp;
7485         int retval;
7486         int alloc_len, total_len;
7487
7488         CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7489
7490         cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7491
7492         retval = CTL_RETVAL_COMPLETE;
7493
7494         total_len = sizeof(struct scsi_report_timestamp_data);
7495         alloc_len = scsi_4btoul(cdb->length);
7496
7497         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7498         ctsio->kern_sg_entries = 0;
7499         ctsio->kern_rel_offset = 0;
7500         ctsio->kern_data_len = min(total_len, alloc_len);
7501         ctsio->kern_total_len = ctsio->kern_data_len;
7502
7503         data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7504         scsi_ulto2b(sizeof(*data) - 2, data->length);
7505         data->origin = RTS_ORIG_OUTSIDE;
7506         getmicrotime(&tv);
7507         timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7508         scsi_ulto4b(timestamp >> 16, data->timestamp);
7509         scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7510
7511         ctl_set_success(ctsio);
7512         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7513         ctsio->be_move_done = ctl_config_move_done;
7514         ctl_datamove((union ctl_io *)ctsio);
7515         return (retval);
7516 }
7517
7518 int
7519 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7520 {
7521         struct ctl_softc *softc = CTL_SOFTC(ctsio);
7522         struct ctl_lun *lun = CTL_LUN(ctsio);
7523         struct scsi_per_res_in *cdb;
7524         int alloc_len, total_len = 0;
7525         /* struct scsi_per_res_in_rsrv in_data; */
7526         uint64_t key;
7527
7528         CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7529
7530         cdb = (struct scsi_per_res_in *)ctsio->cdb;
7531
7532         alloc_len = scsi_2btoul(cdb->length);
7533
7534 retry:
7535         mtx_lock(&lun->lun_lock);
7536         switch (cdb->action) {
7537         case SPRI_RK: /* read keys */
7538                 total_len = sizeof(struct scsi_per_res_in_keys) +
7539                         lun->pr_key_count *
7540                         sizeof(struct scsi_per_res_key);
7541                 break;
7542         case SPRI_RR: /* read reservation */
7543                 if (lun->flags & CTL_LUN_PR_RESERVED)
7544                         total_len = sizeof(struct scsi_per_res_in_rsrv);
7545                 else
7546                         total_len = sizeof(struct scsi_per_res_in_header);
7547                 break;
7548         case SPRI_RC: /* report capabilities */
7549                 total_len = sizeof(struct scsi_per_res_cap);
7550                 break;
7551         case SPRI_RS: /* read full status */
7552                 total_len = sizeof(struct scsi_per_res_in_header) +
7553                     (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7554                     lun->pr_key_count;
7555                 break;
7556         default:
7557                 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7558         }
7559         mtx_unlock(&lun->lun_lock);
7560
7561         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7562         ctsio->kern_rel_offset = 0;
7563         ctsio->kern_sg_entries = 0;
7564         ctsio->kern_data_len = min(total_len, alloc_len);
7565         ctsio->kern_total_len = ctsio->kern_data_len;
7566
7567         mtx_lock(&lun->lun_lock);
7568         switch (cdb->action) {
7569         case SPRI_RK: { // read keys
7570         struct scsi_per_res_in_keys *res_keys;
7571                 int i, key_count;
7572
7573                 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7574
7575                 /*
7576                  * We had to drop the lock to allocate our buffer, which
7577                  * leaves time for someone to come in with another
7578                  * persistent reservation.  (That is unlikely, though,
7579                  * since this should be the only persistent reservation
7580                  * command active right now.)
7581                  */
7582                 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7583                     (lun->pr_key_count *
7584                      sizeof(struct scsi_per_res_key)))){
7585                         mtx_unlock(&lun->lun_lock);
7586                         free(ctsio->kern_data_ptr, M_CTL);
7587                         printf("%s: reservation length changed, retrying\n",
7588                                __func__);
7589                         goto retry;
7590                 }
7591
7592                 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7593
7594                 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7595                              lun->pr_key_count, res_keys->header.length);
7596
7597                 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7598                         if ((key = ctl_get_prkey(lun, i)) == 0)
7599                                 continue;
7600
7601                         /*
7602                          * We used lun->pr_key_count to calculate the
7603                          * size to allocate.  If it turns out the number of
7604                          * initiators with the registered flag set is
7605                          * larger than that (i.e. they haven't been kept in
7606                          * sync), we've got a problem.
7607                          */
7608                         if (key_count >= lun->pr_key_count) {
7609                                 key_count++;
7610                                 continue;
7611                         }
7612                         scsi_u64to8b(key, res_keys->keys[key_count].key);
7613                         key_count++;
7614                 }
7615                 break;
7616         }
7617         case SPRI_RR: { // read reservation
7618                 struct scsi_per_res_in_rsrv *res;
7619                 int tmp_len, header_only;
7620
7621                 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7622
7623                 scsi_ulto4b(lun->pr_generation, res->header.generation);
7624
7625                 if (lun->flags & CTL_LUN_PR_RESERVED)
7626                 {
7627                         tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7628                         scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7629                                     res->header.length);
7630                         header_only = 0;
7631                 } else {
7632                         tmp_len = sizeof(struct scsi_per_res_in_header);
7633                         scsi_ulto4b(0, res->header.length);
7634                         header_only = 1;
7635                 }
7636
7637                 /*
7638                  * We had to drop the lock to allocate our buffer, which
7639                  * leaves time for someone to come in with another
7640                  * persistent reservation.  (That is unlikely, though,
7641                  * since this should be the only persistent reservation
7642                  * command active right now.)
7643                  */
7644                 if (tmp_len != total_len) {
7645                         mtx_unlock(&lun->lun_lock);
7646                         free(ctsio->kern_data_ptr, M_CTL);
7647                         printf("%s: reservation status changed, retrying\n",
7648                                __func__);
7649                         goto retry;
7650                 }
7651
7652                 /*
7653                  * No reservation held, so we're done.
7654                  */
7655                 if (header_only != 0)
7656                         break;
7657
7658                 /*
7659                  * If the registration is an All Registrants type, the key
7660                  * is 0, since it doesn't really matter.
7661                  */
7662                 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7663                         scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7664                             res->data.reservation);
7665                 }
7666                 res->data.scopetype = lun->pr_res_type;
7667                 break;
7668         }
7669         case SPRI_RC:     //report capabilities
7670         {
7671                 struct scsi_per_res_cap *res_cap;
7672                 uint16_t type_mask;
7673
7674                 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7675                 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7676                 res_cap->flags1 = SPRI_CRH;
7677                 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7678                 type_mask = SPRI_TM_WR_EX_AR |
7679                             SPRI_TM_EX_AC_RO |
7680                             SPRI_TM_WR_EX_RO |
7681                             SPRI_TM_EX_AC |
7682                             SPRI_TM_WR_EX |
7683                             SPRI_TM_EX_AC_AR;
7684                 scsi_ulto2b(type_mask, res_cap->type_mask);
7685                 break;
7686         }
7687         case SPRI_RS: { // read full status
7688                 struct scsi_per_res_in_full *res_status;
7689                 struct scsi_per_res_in_full_desc *res_desc;
7690                 struct ctl_port *port;
7691                 int i, len;
7692
7693                 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7694
7695                 /*
7696                  * We had to drop the lock to allocate our buffer, which
7697                  * leaves time for someone to come in with another
7698                  * persistent reservation.  (That is unlikely, though,
7699                  * since this should be the only persistent reservation
7700                  * command active right now.)
7701                  */
7702                 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7703                     (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7704                      lun->pr_key_count)){
7705                         mtx_unlock(&lun->lun_lock);
7706                         free(ctsio->kern_data_ptr, M_CTL);
7707                         printf("%s: reservation length changed, retrying\n",
7708                                __func__);
7709                         goto retry;
7710                 }
7711
7712                 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7713
7714                 res_desc = &res_status->desc[0];
7715                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7716                         if ((key = ctl_get_prkey(lun, i)) == 0)
7717                                 continue;
7718
7719                         scsi_u64to8b(key, res_desc->res_key.key);
7720                         if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7721                             (lun->pr_res_idx == i ||
7722                              lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7723                                 res_desc->flags = SPRI_FULL_R_HOLDER;
7724                                 res_desc->scopetype = lun->pr_res_type;
7725                         }
7726                         scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7727                             res_desc->rel_trgt_port_id);
7728                         len = 0;
7729                         port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7730                         if (port != NULL)
7731                                 len = ctl_create_iid(port,
7732                                     i % CTL_MAX_INIT_PER_PORT,
7733                                     res_desc->transport_id);
7734                         scsi_ulto4b(len, res_desc->additional_length);
7735                         res_desc = (struct scsi_per_res_in_full_desc *)
7736                             &res_desc->transport_id[len];
7737                 }
7738                 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7739                     res_status->header.length);
7740                 break;
7741         }
7742         default:
7743                 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7744         }
7745         mtx_unlock(&lun->lun_lock);
7746
7747         ctl_set_success(ctsio);
7748         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7749         ctsio->be_move_done = ctl_config_move_done;
7750         ctl_datamove((union ctl_io *)ctsio);
7751         return (CTL_RETVAL_COMPLETE);
7752 }
7753
7754 /*
7755  * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7756  * it should return.
7757  */
7758 static int
7759 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7760                 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7761                 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7762                 struct scsi_per_res_out_parms* param)
7763 {
7764         union ctl_ha_msg persis_io;
7765         int i;
7766
7767         mtx_lock(&lun->lun_lock);
7768         if (sa_res_key == 0) {
7769                 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7770                         /* validate scope and type */
7771                         if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7772                              SPR_LU_SCOPE) {
7773                                 mtx_unlock(&lun->lun_lock);
7774                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7775                                                       /*sks_valid*/ 1,
7776                                                       /*command*/ 1,
7777                                                       /*field*/ 2,
7778                                                       /*bit_valid*/ 1,
7779                                                       /*bit*/ 4);
7780                                 ctl_done((union ctl_io *)ctsio);
7781                                 return (1);
7782                         }
7783
7784                         if (type>8 || type==2 || type==4 || type==0) {
7785                                 mtx_unlock(&lun->lun_lock);
7786                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7787                                                       /*sks_valid*/ 1,
7788                                                       /*command*/ 1,
7789                                                       /*field*/ 2,
7790                                                       /*bit_valid*/ 1,
7791                                                       /*bit*/ 0);
7792                                 ctl_done((union ctl_io *)ctsio);
7793                                 return (1);
7794                         }
7795
7796                         /*
7797                          * Unregister everybody else and build UA for
7798                          * them
7799                          */
7800                         for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7801                                 if (i == residx || ctl_get_prkey(lun, i) == 0)
7802                                         continue;
7803
7804                                 ctl_clr_prkey(lun, i);
7805                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7806                         }
7807                         lun->pr_key_count = 1;
7808                         lun->pr_res_type = type;
7809                         if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7810                             lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7811                                 lun->pr_res_idx = residx;
7812                         lun->pr_generation++;
7813                         mtx_unlock(&lun->lun_lock);
7814
7815                         /* send msg to other side */
7816                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7817                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7818                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7819                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
7820                         persis_io.pr.pr_info.res_type = type;
7821                         memcpy(persis_io.pr.pr_info.sa_res_key,
7822                                param->serv_act_res_key,
7823                                sizeof(param->serv_act_res_key));
7824                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7825                             sizeof(persis_io.pr), M_WAITOK);
7826                 } else {
7827                         /* not all registrants */
7828                         mtx_unlock(&lun->lun_lock);
7829                         free(ctsio->kern_data_ptr, M_CTL);
7830                         ctl_set_invalid_field(ctsio,
7831                                               /*sks_valid*/ 1,
7832                                               /*command*/ 0,
7833                                               /*field*/ 8,
7834                                               /*bit_valid*/ 0,
7835                                               /*bit*/ 0);
7836                         ctl_done((union ctl_io *)ctsio);
7837                         return (1);
7838                 }
7839         } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7840                 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7841                 int found = 0;
7842
7843                 if (res_key == sa_res_key) {
7844                         /* special case */
7845                         /*
7846                          * The spec implies this is not good but doesn't
7847                          * say what to do. There are two choices either
7848                          * generate a res conflict or check condition
7849                          * with illegal field in parameter data. Since
7850                          * that is what is done when the sa_res_key is
7851                          * zero I'll take that approach since this has
7852                          * to do with the sa_res_key.
7853                          */
7854                         mtx_unlock(&lun->lun_lock);
7855                         free(ctsio->kern_data_ptr, M_CTL);
7856                         ctl_set_invalid_field(ctsio,
7857                                               /*sks_valid*/ 1,
7858                                               /*command*/ 0,
7859                                               /*field*/ 8,
7860                                               /*bit_valid*/ 0,
7861                                               /*bit*/ 0);
7862                         ctl_done((union ctl_io *)ctsio);
7863                         return (1);
7864                 }
7865
7866                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7867                         if (ctl_get_prkey(lun, i) != sa_res_key)
7868                                 continue;
7869
7870                         found = 1;
7871                         ctl_clr_prkey(lun, i);
7872                         lun->pr_key_count--;
7873                         ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7874                 }
7875                 if (!found) {
7876                         mtx_unlock(&lun->lun_lock);
7877                         free(ctsio->kern_data_ptr, M_CTL);
7878                         ctl_set_reservation_conflict(ctsio);
7879                         ctl_done((union ctl_io *)ctsio);
7880                         return (CTL_RETVAL_COMPLETE);
7881                 }
7882                 lun->pr_generation++;
7883                 mtx_unlock(&lun->lun_lock);
7884
7885                 /* send msg to other side */
7886                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7887                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7888                 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7889                 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7890                 persis_io.pr.pr_info.res_type = type;
7891                 memcpy(persis_io.pr.pr_info.sa_res_key,
7892                        param->serv_act_res_key,
7893                        sizeof(param->serv_act_res_key));
7894                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7895                     sizeof(persis_io.pr), M_WAITOK);
7896         } else {
7897                 /* Reserved but not all registrants */
7898                 /* sa_res_key is res holder */
7899                 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7900                         /* validate scope and type */
7901                         if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7902                              SPR_LU_SCOPE) {
7903                                 mtx_unlock(&lun->lun_lock);
7904                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7905                                                       /*sks_valid*/ 1,
7906                                                       /*command*/ 1,
7907                                                       /*field*/ 2,
7908                                                       /*bit_valid*/ 1,
7909                                                       /*bit*/ 4);
7910                                 ctl_done((union ctl_io *)ctsio);
7911                                 return (1);
7912                         }
7913
7914                         if (type>8 || type==2 || type==4 || type==0) {
7915                                 mtx_unlock(&lun->lun_lock);
7916                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7917                                                       /*sks_valid*/ 1,
7918                                                       /*command*/ 1,
7919                                                       /*field*/ 2,
7920                                                       /*bit_valid*/ 1,
7921                                                       /*bit*/ 0);
7922                                 ctl_done((union ctl_io *)ctsio);
7923                                 return (1);
7924                         }
7925
7926                         /*
7927                          * Do the following:
7928                          * if sa_res_key != res_key remove all
7929                          * registrants w/sa_res_key and generate UA
7930                          * for these registrants(Registrations
7931                          * Preempted) if it wasn't an exclusive
7932                          * reservation generate UA(Reservations
7933                          * Preempted) for all other registered nexuses
7934                          * if the type has changed. Establish the new
7935                          * reservation and holder. If res_key and
7936                          * sa_res_key are the same do the above
7937                          * except don't unregister the res holder.
7938                          */
7939
7940                         for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7941                                 if (i == residx || ctl_get_prkey(lun, i) == 0)
7942                                         continue;
7943
7944                                 if (sa_res_key == ctl_get_prkey(lun, i)) {
7945                                         ctl_clr_prkey(lun, i);
7946                                         lun->pr_key_count--;
7947                                         ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7948                                 } else if (type != lun->pr_res_type &&
7949                                     (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7950                                      lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7951                                         ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7952                                 }
7953                         }
7954                         lun->pr_res_type = type;
7955                         if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7956                             lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7957                                 lun->pr_res_idx = residx;
7958                         else
7959                                 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7960                         lun->pr_generation++;
7961                         mtx_unlock(&lun->lun_lock);
7962
7963                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7964                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7965                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7966                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
7967                         persis_io.pr.pr_info.res_type = type;
7968                         memcpy(persis_io.pr.pr_info.sa_res_key,
7969                                param->serv_act_res_key,
7970                                sizeof(param->serv_act_res_key));
7971                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7972                             sizeof(persis_io.pr), M_WAITOK);
7973                 } else {
7974                         /*
7975                          * sa_res_key is not the res holder just
7976                          * remove registrants
7977                          */
7978                         int found=0;
7979
7980                         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7981                                 if (sa_res_key != ctl_get_prkey(lun, i))
7982                                         continue;
7983
7984                                 found = 1;
7985                                 ctl_clr_prkey(lun, i);
7986                                 lun->pr_key_count--;
7987                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7988                         }
7989
7990                         if (!found) {
7991                                 mtx_unlock(&lun->lun_lock);
7992                                 free(ctsio->kern_data_ptr, M_CTL);
7993                                 ctl_set_reservation_conflict(ctsio);
7994                                 ctl_done((union ctl_io *)ctsio);
7995                                 return (1);
7996                         }
7997                         lun->pr_generation++;
7998                         mtx_unlock(&lun->lun_lock);
7999
8000                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8001                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8002                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8003                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
8004                         persis_io.pr.pr_info.res_type = type;
8005                         memcpy(persis_io.pr.pr_info.sa_res_key,
8006                                param->serv_act_res_key,
8007                                sizeof(param->serv_act_res_key));
8008                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8009                             sizeof(persis_io.pr), M_WAITOK);
8010                 }
8011         }
8012         return (0);
8013 }
8014
8015 static void
8016 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8017 {
8018         uint64_t sa_res_key;
8019         int i;
8020
8021         sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8022
8023         if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8024          || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8025          || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8026                 if (sa_res_key == 0) {
8027                         /*
8028                          * Unregister everybody else and build UA for
8029                          * them
8030                          */
8031                         for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8032                                 if (i == msg->pr.pr_info.residx ||
8033                                     ctl_get_prkey(lun, i) == 0)
8034                                         continue;
8035
8036                                 ctl_clr_prkey(lun, i);
8037                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8038                         }
8039
8040                         lun->pr_key_count = 1;
8041                         lun->pr_res_type = msg->pr.pr_info.res_type;
8042                         if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8043                             lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8044                                 lun->pr_res_idx = msg->pr.pr_info.residx;
8045                 } else {
8046                         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8047                                 if (sa_res_key == ctl_get_prkey(lun, i))
8048                                         continue;
8049
8050                                 ctl_clr_prkey(lun, i);
8051                                 lun->pr_key_count--;
8052                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8053                         }
8054                 }
8055         } else {
8056                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8057                         if (i == msg->pr.pr_info.residx ||
8058                             ctl_get_prkey(lun, i) == 0)
8059                                 continue;
8060
8061                         if (sa_res_key == ctl_get_prkey(lun, i)) {
8062                                 ctl_clr_prkey(lun, i);
8063                                 lun->pr_key_count--;
8064                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8065                         } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8066                             && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8067                              lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8068                                 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8069                         }
8070                 }
8071                 lun->pr_res_type = msg->pr.pr_info.res_type;
8072                 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8073                     lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8074                         lun->pr_res_idx = msg->pr.pr_info.residx;
8075                 else
8076                         lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8077         }
8078         lun->pr_generation++;
8079
8080 }
8081
8082
8083 int
8084 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8085 {
8086         struct ctl_softc *softc = CTL_SOFTC(ctsio);
8087         struct ctl_lun *lun = CTL_LUN(ctsio);
8088         int retval;
8089         u_int32_t param_len;
8090         struct scsi_per_res_out *cdb;
8091         struct scsi_per_res_out_parms* param;
8092         uint32_t residx;
8093         uint64_t res_key, sa_res_key, key;
8094         uint8_t type;
8095         union ctl_ha_msg persis_io;
8096         int    i;
8097
8098         CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8099
8100         cdb = (struct scsi_per_res_out *)ctsio->cdb;
8101         retval = CTL_RETVAL_COMPLETE;
8102
8103         /*
8104          * We only support whole-LUN scope.  The scope & type are ignored for
8105          * register, register and ignore existing key and clear.
8106          * We sometimes ignore scope and type on preempts too!!
8107          * Verify reservation type here as well.
8108          */
8109         type = cdb->scope_type & SPR_TYPE_MASK;
8110         if ((cdb->action == SPRO_RESERVE)
8111          || (cdb->action == SPRO_RELEASE)) {
8112                 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8113                         ctl_set_invalid_field(/*ctsio*/ ctsio,
8114                                               /*sks_valid*/ 1,
8115                                               /*command*/ 1,
8116                                               /*field*/ 2,
8117                                               /*bit_valid*/ 1,
8118                                               /*bit*/ 4);
8119                         ctl_done((union ctl_io *)ctsio);
8120                         return (CTL_RETVAL_COMPLETE);
8121                 }
8122
8123                 if (type>8 || type==2 || type==4 || type==0) {
8124                         ctl_set_invalid_field(/*ctsio*/ ctsio,
8125                                               /*sks_valid*/ 1,
8126                                               /*command*/ 1,
8127                                               /*field*/ 2,
8128                                               /*bit_valid*/ 1,
8129                                               /*bit*/ 0);
8130                         ctl_done((union ctl_io *)ctsio);
8131                         return (CTL_RETVAL_COMPLETE);
8132                 }
8133         }
8134
8135         param_len = scsi_4btoul(cdb->length);
8136
8137         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8138                 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8139                 ctsio->kern_data_len = param_len;
8140                 ctsio->kern_total_len = param_len;
8141                 ctsio->kern_rel_offset = 0;
8142                 ctsio->kern_sg_entries = 0;
8143                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8144                 ctsio->be_move_done = ctl_config_move_done;
8145                 ctl_datamove((union ctl_io *)ctsio);
8146
8147                 return (CTL_RETVAL_COMPLETE);
8148         }
8149
8150         param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8151
8152         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8153         res_key = scsi_8btou64(param->res_key.key);
8154         sa_res_key = scsi_8btou64(param->serv_act_res_key);
8155
8156         /*
8157          * Validate the reservation key here except for SPRO_REG_IGNO
8158          * This must be done for all other service actions
8159          */
8160         if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8161                 mtx_lock(&lun->lun_lock);
8162                 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8163                         if (res_key != key) {
8164                                 /*
8165                                  * The current key passed in doesn't match
8166                                  * the one the initiator previously
8167                                  * registered.
8168                                  */
8169                                 mtx_unlock(&lun->lun_lock);
8170                                 free(ctsio->kern_data_ptr, M_CTL);
8171                                 ctl_set_reservation_conflict(ctsio);
8172                                 ctl_done((union ctl_io *)ctsio);
8173                                 return (CTL_RETVAL_COMPLETE);
8174                         }
8175                 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8176                         /*
8177                          * We are not registered
8178                          */
8179                         mtx_unlock(&lun->lun_lock);
8180                         free(ctsio->kern_data_ptr, M_CTL);
8181                         ctl_set_reservation_conflict(ctsio);
8182                         ctl_done((union ctl_io *)ctsio);
8183                         return (CTL_RETVAL_COMPLETE);
8184                 } else if (res_key != 0) {
8185                         /*
8186                          * We are not registered and trying to register but
8187                          * the register key isn't zero.
8188                          */
8189                         mtx_unlock(&lun->lun_lock);
8190                         free(ctsio->kern_data_ptr, M_CTL);
8191                         ctl_set_reservation_conflict(ctsio);
8192                         ctl_done((union ctl_io *)ctsio);
8193                         return (CTL_RETVAL_COMPLETE);
8194                 }
8195                 mtx_unlock(&lun->lun_lock);
8196         }
8197
8198         switch (cdb->action & SPRO_ACTION_MASK) {
8199         case SPRO_REGISTER:
8200         case SPRO_REG_IGNO: {
8201
8202                 /*
8203                  * We don't support any of these options, as we report in
8204                  * the read capabilities request (see
8205                  * ctl_persistent_reserve_in(), above).
8206                  */
8207                 if ((param->flags & SPR_SPEC_I_PT)
8208                  || (param->flags & SPR_ALL_TG_PT)
8209                  || (param->flags & SPR_APTPL)) {
8210                         int bit_ptr;
8211
8212                         if (param->flags & SPR_APTPL)
8213                                 bit_ptr = 0;
8214                         else if (param->flags & SPR_ALL_TG_PT)
8215                                 bit_ptr = 2;
8216                         else /* SPR_SPEC_I_PT */
8217                                 bit_ptr = 3;
8218
8219                         free(ctsio->kern_data_ptr, M_CTL);
8220                         ctl_set_invalid_field(ctsio,
8221                                               /*sks_valid*/ 1,
8222                                               /*command*/ 0,
8223                                               /*field*/ 20,
8224                                               /*bit_valid*/ 1,
8225                                               /*bit*/ bit_ptr);
8226                         ctl_done((union ctl_io *)ctsio);
8227                         return (CTL_RETVAL_COMPLETE);
8228                 }
8229
8230                 mtx_lock(&lun->lun_lock);
8231
8232                 /*
8233                  * The initiator wants to clear the
8234                  * key/unregister.
8235                  */
8236                 if (sa_res_key == 0) {
8237                         if ((res_key == 0
8238                           && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8239                          || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8240                           && ctl_get_prkey(lun, residx) == 0)) {
8241                                 mtx_unlock(&lun->lun_lock);
8242                                 goto done;
8243                         }
8244
8245                         ctl_clr_prkey(lun, residx);
8246                         lun->pr_key_count--;
8247
8248                         if (residx == lun->pr_res_idx) {
8249                                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8250                                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8251
8252                                 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8253                                      lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8254                                     lun->pr_key_count) {
8255                                         /*
8256                                          * If the reservation is a registrants
8257                                          * only type we need to generate a UA
8258                                          * for other registered inits.  The
8259                                          * sense code should be RESERVATIONS
8260                                          * RELEASED
8261                                          */
8262
8263                                         for (i = softc->init_min; i < softc->init_max; i++){
8264                                                 if (ctl_get_prkey(lun, i) == 0)
8265                                                         continue;
8266                                                 ctl_est_ua(lun, i,
8267                                                     CTL_UA_RES_RELEASE);
8268                                         }
8269                                 }
8270                                 lun->pr_res_type = 0;
8271                         } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8272                                 if (lun->pr_key_count==0) {
8273                                         lun->flags &= ~CTL_LUN_PR_RESERVED;
8274                                         lun->pr_res_type = 0;
8275                                         lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8276                                 }
8277                         }
8278                         lun->pr_generation++;
8279                         mtx_unlock(&lun->lun_lock);
8280
8281                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8282                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8283                         persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8284                         persis_io.pr.pr_info.residx = residx;
8285                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8286                             sizeof(persis_io.pr), M_WAITOK);
8287                 } else /* sa_res_key != 0 */ {
8288
8289                         /*
8290                          * If we aren't registered currently then increment
8291                          * the key count and set the registered flag.
8292                          */
8293                         ctl_alloc_prkey(lun, residx);
8294                         if (ctl_get_prkey(lun, residx) == 0)
8295                                 lun->pr_key_count++;
8296                         ctl_set_prkey(lun, residx, sa_res_key);
8297                         lun->pr_generation++;
8298                         mtx_unlock(&lun->lun_lock);
8299
8300                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8301                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8302                         persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8303                         persis_io.pr.pr_info.residx = residx;
8304                         memcpy(persis_io.pr.pr_info.sa_res_key,
8305                                param->serv_act_res_key,
8306                                sizeof(param->serv_act_res_key));
8307                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8308                             sizeof(persis_io.pr), M_WAITOK);
8309                 }
8310
8311                 break;
8312         }
8313         case SPRO_RESERVE:
8314                 mtx_lock(&lun->lun_lock);
8315                 if (lun->flags & CTL_LUN_PR_RESERVED) {
8316                         /*
8317                          * if this isn't the reservation holder and it's
8318                          * not a "all registrants" type or if the type is
8319                          * different then we have a conflict
8320                          */
8321                         if ((lun->pr_res_idx != residx
8322                           && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8323                          || lun->pr_res_type != type) {
8324                                 mtx_unlock(&lun->lun_lock);
8325                                 free(ctsio->kern_data_ptr, M_CTL);
8326                                 ctl_set_reservation_conflict(ctsio);
8327                                 ctl_done((union ctl_io *)ctsio);
8328                                 return (CTL_RETVAL_COMPLETE);
8329                         }
8330                         mtx_unlock(&lun->lun_lock);
8331                 } else /* create a reservation */ {
8332                         /*
8333                          * If it's not an "all registrants" type record
8334                          * reservation holder
8335                          */
8336                         if (type != SPR_TYPE_WR_EX_AR
8337                          && type != SPR_TYPE_EX_AC_AR)
8338                                 lun->pr_res_idx = residx; /* Res holder */
8339                         else
8340                                 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8341
8342                         lun->flags |= CTL_LUN_PR_RESERVED;
8343                         lun->pr_res_type = type;
8344
8345                         mtx_unlock(&lun->lun_lock);
8346
8347                         /* send msg to other side */
8348                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8349                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8350                         persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8351                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
8352                         persis_io.pr.pr_info.res_type = type;
8353                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8354                             sizeof(persis_io.pr), M_WAITOK);
8355                 }
8356                 break;
8357
8358         case SPRO_RELEASE:
8359                 mtx_lock(&lun->lun_lock);
8360                 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8361                         /* No reservation exists return good status */
8362                         mtx_unlock(&lun->lun_lock);
8363                         goto done;
8364                 }
8365                 /*
8366                  * Is this nexus a reservation holder?
8367                  */
8368                 if (lun->pr_res_idx != residx
8369                  && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8370                         /*
8371                          * not a res holder return good status but
8372                          * do nothing
8373                          */
8374                         mtx_unlock(&lun->lun_lock);
8375                         goto done;
8376                 }
8377
8378                 if (lun->pr_res_type != type) {
8379                         mtx_unlock(&lun->lun_lock);
8380                         free(ctsio->kern_data_ptr, M_CTL);
8381                         ctl_set_illegal_pr_release(ctsio);
8382                         ctl_done((union ctl_io *)ctsio);
8383                         return (CTL_RETVAL_COMPLETE);
8384                 }
8385
8386                 /* okay to release */
8387                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8388                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8389                 lun->pr_res_type = 0;
8390
8391                 /*
8392                  * If this isn't an exclusive access reservation and NUAR
8393                  * is not set, generate UA for all other registrants.
8394                  */
8395                 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8396                     (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8397                         for (i = softc->init_min; i < softc->init_max; i++) {
8398                                 if (i == residx || ctl_get_prkey(lun, i) == 0)
8399                                         continue;
8400                                 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8401                         }
8402                 }
8403                 mtx_unlock(&lun->lun_lock);
8404
8405                 /* Send msg to other side */
8406                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8407                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8408                 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8409                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8410                      sizeof(persis_io.pr), M_WAITOK);
8411                 break;
8412
8413         case SPRO_CLEAR:
8414                 /* send msg to other side */
8415
8416                 mtx_lock(&lun->lun_lock);
8417                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8418                 lun->pr_res_type = 0;
8419                 lun->pr_key_count = 0;
8420                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8421
8422                 ctl_clr_prkey(lun, residx);
8423                 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8424                         if (ctl_get_prkey(lun, i) != 0) {
8425                                 ctl_clr_prkey(lun, i);
8426                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8427                         }
8428                 lun->pr_generation++;
8429                 mtx_unlock(&lun->lun_lock);
8430
8431                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8432                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8433                 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8434                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8435                      sizeof(persis_io.pr), M_WAITOK);
8436                 break;
8437
8438         case SPRO_PREEMPT:
8439         case SPRO_PRE_ABO: {
8440                 int nretval;
8441
8442                 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8443                                           residx, ctsio, cdb, param);
8444                 if (nretval != 0)
8445                         return (CTL_RETVAL_COMPLETE);
8446                 break;
8447         }
8448         default:
8449                 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8450         }
8451
8452 done:
8453         free(ctsio->kern_data_ptr, M_CTL);
8454         ctl_set_success(ctsio);
8455         ctl_done((union ctl_io *)ctsio);
8456
8457         return (retval);
8458 }
8459
8460 /*
8461  * This routine is for handling a message from the other SC pertaining to
8462  * persistent reserve out. All the error checking will have been done
8463  * so only perorming the action need be done here to keep the two
8464  * in sync.
8465  */
8466 static void
8467 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8468 {
8469         struct ctl_softc *softc = CTL_SOFTC(io);
8470         union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8471         struct ctl_lun *lun;
8472         int i;
8473         uint32_t residx, targ_lun;
8474
8475         targ_lun = msg->hdr.nexus.targ_mapped_lun;
8476         mtx_lock(&softc->ctl_lock);
8477         if (targ_lun >= ctl_max_luns ||
8478             (lun = softc->ctl_luns[targ_lun]) == NULL) {
8479                 mtx_unlock(&softc->ctl_lock);
8480                 return;
8481         }
8482         mtx_lock(&lun->lun_lock);
8483         mtx_unlock(&softc->ctl_lock);
8484         if (lun->flags & CTL_LUN_DISABLED) {
8485                 mtx_unlock(&lun->lun_lock);
8486                 return;
8487         }
8488         residx = ctl_get_initindex(&msg->hdr.nexus);
8489         switch(msg->pr.pr_info.action) {
8490         case CTL_PR_REG_KEY:
8491                 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8492                 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8493                         lun->pr_key_count++;
8494                 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8495                     scsi_8btou64(msg->pr.pr_info.sa_res_key));
8496                 lun->pr_generation++;
8497                 break;
8498
8499         case CTL_PR_UNREG_KEY:
8500                 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8501                 lun->pr_key_count--;
8502
8503                 /* XXX Need to see if the reservation has been released */
8504                 /* if so do we need to generate UA? */
8505                 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8506                         lun->flags &= ~CTL_LUN_PR_RESERVED;
8507                         lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8508
8509                         if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8510                              lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8511                             lun->pr_key_count) {
8512                                 /*
8513                                  * If the reservation is a registrants
8514                                  * only type we need to generate a UA
8515                                  * for other registered inits.  The
8516                                  * sense code should be RESERVATIONS
8517                                  * RELEASED
8518                                  */
8519
8520                                 for (i = softc->init_min; i < softc->init_max; i++) {
8521                                         if (ctl_get_prkey(lun, i) == 0)
8522                                                 continue;
8523
8524                                         ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8525                                 }
8526                         }
8527                         lun->pr_res_type = 0;
8528                 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8529                         if (lun->pr_key_count==0) {
8530                                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8531                                 lun->pr_res_type = 0;
8532                                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8533                         }
8534                 }
8535                 lun->pr_generation++;
8536                 break;
8537
8538         case CTL_PR_RESERVE:
8539                 lun->flags |= CTL_LUN_PR_RESERVED;
8540                 lun->pr_res_type = msg->pr.pr_info.res_type;
8541                 lun->pr_res_idx = msg->pr.pr_info.residx;
8542
8543                 break;
8544
8545         case CTL_PR_RELEASE:
8546                 /*
8547                  * If this isn't an exclusive access reservation and NUAR
8548                  * is not set, generate UA for all other registrants.
8549                  */
8550                 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8551                     lun->pr_res_type != SPR_TYPE_WR_EX &&
8552                     (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8553                         for (i = softc->init_min; i < softc->init_max; i++) {
8554                                 if (i == residx || ctl_get_prkey(lun, i) == 0)
8555                                         continue;
8556                                 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8557                         }
8558                 }
8559
8560                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8561                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8562                 lun->pr_res_type = 0;
8563                 break;
8564
8565         case CTL_PR_PREEMPT:
8566                 ctl_pro_preempt_other(lun, msg);
8567                 break;
8568         case CTL_PR_CLEAR:
8569                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8570                 lun->pr_res_type = 0;
8571                 lun->pr_key_count = 0;
8572                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8573
8574                 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8575                         if (ctl_get_prkey(lun, i) == 0)
8576                                 continue;
8577                         ctl_clr_prkey(lun, i);
8578                         ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8579                 }
8580                 lun->pr_generation++;
8581                 break;
8582         }
8583
8584         mtx_unlock(&lun->lun_lock);
8585 }
8586
8587 int
8588 ctl_read_write(struct ctl_scsiio *ctsio)
8589 {
8590         struct ctl_lun *lun = CTL_LUN(ctsio);
8591         struct ctl_lba_len_flags *lbalen;
8592         uint64_t lba;
8593         uint32_t num_blocks;
8594         int flags, retval;
8595         int isread;
8596
8597         CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8598
8599         flags = 0;
8600         isread = ctsio->cdb[0] == READ_6  || ctsio->cdb[0] == READ_10
8601               || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8602         switch (ctsio->cdb[0]) {
8603         case READ_6:
8604         case WRITE_6: {
8605                 struct scsi_rw_6 *cdb;
8606
8607                 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8608
8609                 lba = scsi_3btoul(cdb->addr);
8610                 /* only 5 bits are valid in the most significant address byte */
8611                 lba &= 0x1fffff;
8612                 num_blocks = cdb->length;
8613                 /*
8614                  * This is correct according to SBC-2.
8615                  */
8616                 if (num_blocks == 0)
8617                         num_blocks = 256;
8618                 break;
8619         }
8620         case READ_10:
8621         case WRITE_10: {
8622                 struct scsi_rw_10 *cdb;
8623
8624                 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8625                 if (cdb->byte2 & SRW10_FUA)
8626                         flags |= CTL_LLF_FUA;
8627                 if (cdb->byte2 & SRW10_DPO)
8628                         flags |= CTL_LLF_DPO;
8629                 lba = scsi_4btoul(cdb->addr);
8630                 num_blocks = scsi_2btoul(cdb->length);
8631                 break;
8632         }
8633         case WRITE_VERIFY_10: {
8634                 struct scsi_write_verify_10 *cdb;
8635
8636                 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8637                 flags |= CTL_LLF_FUA;
8638                 if (cdb->byte2 & SWV_DPO)
8639                         flags |= CTL_LLF_DPO;
8640                 lba = scsi_4btoul(cdb->addr);
8641                 num_blocks = scsi_2btoul(cdb->length);
8642                 break;
8643         }
8644         case READ_12:
8645         case WRITE_12: {
8646                 struct scsi_rw_12 *cdb;
8647
8648                 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8649                 if (cdb->byte2 & SRW12_FUA)
8650                         flags |= CTL_LLF_FUA;
8651                 if (cdb->byte2 & SRW12_DPO)
8652                         flags |= CTL_LLF_DPO;
8653                 lba = scsi_4btoul(cdb->addr);
8654                 num_blocks = scsi_4btoul(cdb->length);
8655                 break;
8656         }
8657         case WRITE_VERIFY_12: {
8658                 struct scsi_write_verify_12 *cdb;
8659
8660                 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8661                 flags |= CTL_LLF_FUA;
8662                 if (cdb->byte2 & SWV_DPO)
8663                         flags |= CTL_LLF_DPO;
8664                 lba = scsi_4btoul(cdb->addr);
8665                 num_blocks = scsi_4btoul(cdb->length);
8666                 break;
8667         }
8668         case READ_16:
8669         case WRITE_16: {
8670                 struct scsi_rw_16 *cdb;
8671
8672                 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8673                 if (cdb->byte2 & SRW12_FUA)
8674                         flags |= CTL_LLF_FUA;
8675                 if (cdb->byte2 & SRW12_DPO)
8676                         flags |= CTL_LLF_DPO;
8677                 lba = scsi_8btou64(cdb->addr);
8678                 num_blocks = scsi_4btoul(cdb->length);
8679                 break;
8680         }
8681         case WRITE_ATOMIC_16: {
8682                 struct scsi_write_atomic_16 *cdb;
8683
8684                 if (lun->be_lun->atomicblock == 0) {
8685                         ctl_set_invalid_opcode(ctsio);
8686                         ctl_done((union ctl_io *)ctsio);
8687                         return (CTL_RETVAL_COMPLETE);
8688                 }
8689
8690                 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8691                 if (cdb->byte2 & SRW12_FUA)
8692                         flags |= CTL_LLF_FUA;
8693                 if (cdb->byte2 & SRW12_DPO)
8694                         flags |= CTL_LLF_DPO;
8695                 lba = scsi_8btou64(cdb->addr);
8696                 num_blocks = scsi_2btoul(cdb->length);
8697                 if (num_blocks > lun->be_lun->atomicblock) {
8698                         ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8699                             /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8700                             /*bit*/ 0);
8701                         ctl_done((union ctl_io *)ctsio);
8702                         return (CTL_RETVAL_COMPLETE);
8703                 }
8704                 break;
8705         }
8706         case WRITE_VERIFY_16: {
8707                 struct scsi_write_verify_16 *cdb;
8708
8709                 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8710                 flags |= CTL_LLF_FUA;
8711                 if (cdb->byte2 & SWV_DPO)
8712                         flags |= CTL_LLF_DPO;
8713                 lba = scsi_8btou64(cdb->addr);
8714                 num_blocks = scsi_4btoul(cdb->length);
8715                 break;
8716         }
8717         default:
8718                 /*
8719                  * We got a command we don't support.  This shouldn't
8720                  * happen, commands should be filtered out above us.
8721                  */
8722                 ctl_set_invalid_opcode(ctsio);
8723                 ctl_done((union ctl_io *)ctsio);
8724
8725                 return (CTL_RETVAL_COMPLETE);
8726                 break; /* NOTREACHED */
8727         }
8728
8729         /*
8730          * The first check is to make sure we're in bounds, the second
8731          * check is to catch wrap-around problems.  If the lba + num blocks
8732          * is less than the lba, then we've wrapped around and the block
8733          * range is invalid anyway.
8734          */
8735         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8736          || ((lba + num_blocks) < lba)) {
8737                 ctl_set_lba_out_of_range(ctsio,
8738                     MAX(lba, lun->be_lun->maxlba + 1));
8739                 ctl_done((union ctl_io *)ctsio);
8740                 return (CTL_RETVAL_COMPLETE);
8741         }
8742
8743         /*
8744          * According to SBC-3, a transfer length of 0 is not an error.
8745          * Note that this cannot happen with WRITE(6) or READ(6), since 0
8746          * translates to 256 blocks for those commands.
8747          */
8748         if (num_blocks == 0) {
8749                 ctl_set_success(ctsio);
8750                 ctl_done((union ctl_io *)ctsio);
8751                 return (CTL_RETVAL_COMPLETE);
8752         }
8753
8754         /* Set FUA and/or DPO if caches are disabled. */
8755         if (isread) {
8756                 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8757                         flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8758         } else {
8759                 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8760                         flags |= CTL_LLF_FUA;
8761         }
8762
8763         lbalen = (struct ctl_lba_len_flags *)
8764             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8765         lbalen->lba = lba;
8766         lbalen->len = num_blocks;
8767         lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8768
8769         ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8770         ctsio->kern_rel_offset = 0;
8771
8772         CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8773
8774         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8775         return (retval);
8776 }
8777
8778 static int
8779 ctl_cnw_cont(union ctl_io *io)
8780 {
8781         struct ctl_lun *lun = CTL_LUN(io);
8782         struct ctl_scsiio *ctsio;
8783         struct ctl_lba_len_flags *lbalen;
8784         int retval;
8785
8786         ctsio = &io->scsiio;
8787         ctsio->io_hdr.status = CTL_STATUS_NONE;
8788         ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8789         lbalen = (struct ctl_lba_len_flags *)
8790             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8791         lbalen->flags &= ~CTL_LLF_COMPARE;
8792         lbalen->flags |= CTL_LLF_WRITE;
8793
8794         CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8795         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8796         return (retval);
8797 }
8798
8799 int
8800 ctl_cnw(struct ctl_scsiio *ctsio)
8801 {
8802         struct ctl_lun *lun = CTL_LUN(ctsio);
8803         struct ctl_lba_len_flags *lbalen;
8804         uint64_t lba;
8805         uint32_t num_blocks;
8806         int flags, retval;
8807
8808         CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8809
8810         flags = 0;
8811         switch (ctsio->cdb[0]) {
8812         case COMPARE_AND_WRITE: {
8813                 struct scsi_compare_and_write *cdb;
8814
8815                 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8816                 if (cdb->byte2 & SRW10_FUA)
8817                         flags |= CTL_LLF_FUA;
8818                 if (cdb->byte2 & SRW10_DPO)
8819                         flags |= CTL_LLF_DPO;
8820                 lba = scsi_8btou64(cdb->addr);
8821                 num_blocks = cdb->length;
8822                 break;
8823         }
8824         default:
8825                 /*
8826                  * We got a command we don't support.  This shouldn't
8827                  * happen, commands should be filtered out above us.
8828                  */
8829                 ctl_set_invalid_opcode(ctsio);
8830                 ctl_done((union ctl_io *)ctsio);
8831
8832                 return (CTL_RETVAL_COMPLETE);
8833                 break; /* NOTREACHED */
8834         }
8835
8836         /*
8837          * The first check is to make sure we're in bounds, the second
8838          * check is to catch wrap-around problems.  If the lba + num blocks
8839          * is less than the lba, then we've wrapped around and the block
8840          * range is invalid anyway.
8841          */
8842         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8843          || ((lba + num_blocks) < lba)) {
8844                 ctl_set_lba_out_of_range(ctsio,
8845                     MAX(lba, lun->be_lun->maxlba + 1));
8846                 ctl_done((union ctl_io *)ctsio);
8847                 return (CTL_RETVAL_COMPLETE);
8848         }
8849
8850         /*
8851          * According to SBC-3, a transfer length of 0 is not an error.
8852          */
8853         if (num_blocks == 0) {
8854                 ctl_set_success(ctsio);
8855                 ctl_done((union ctl_io *)ctsio);
8856                 return (CTL_RETVAL_COMPLETE);
8857         }
8858
8859         /* Set FUA if write cache is disabled. */
8860         if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8861                 flags |= CTL_LLF_FUA;
8862
8863         ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8864         ctsio->kern_rel_offset = 0;
8865
8866         /*
8867          * Set the IO_CONT flag, so that if this I/O gets passed to
8868          * ctl_data_submit_done(), it'll get passed back to
8869          * ctl_ctl_cnw_cont() for further processing.
8870          */
8871         ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8872         ctsio->io_cont = ctl_cnw_cont;
8873
8874         lbalen = (struct ctl_lba_len_flags *)
8875             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8876         lbalen->lba = lba;
8877         lbalen->len = num_blocks;
8878         lbalen->flags = CTL_LLF_COMPARE | flags;
8879
8880         CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8881         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8882         return (retval);
8883 }
8884
8885 int
8886 ctl_verify(struct ctl_scsiio *ctsio)
8887 {
8888         struct ctl_lun *lun = CTL_LUN(ctsio);
8889         struct ctl_lba_len_flags *lbalen;
8890         uint64_t lba;
8891         uint32_t num_blocks;
8892         int bytchk, flags;
8893         int retval;
8894
8895         CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8896
8897         bytchk = 0;
8898         flags = CTL_LLF_FUA;
8899         switch (ctsio->cdb[0]) {
8900         case VERIFY_10: {
8901                 struct scsi_verify_10 *cdb;
8902
8903                 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8904                 if (cdb->byte2 & SVFY_BYTCHK)
8905                         bytchk = 1;
8906                 if (cdb->byte2 & SVFY_DPO)
8907                         flags |= CTL_LLF_DPO;
8908                 lba = scsi_4btoul(cdb->addr);
8909                 num_blocks = scsi_2btoul(cdb->length);
8910                 break;
8911         }
8912         case VERIFY_12: {
8913                 struct scsi_verify_12 *cdb;
8914
8915                 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8916                 if (cdb->byte2 & SVFY_BYTCHK)
8917                         bytchk = 1;
8918                 if (cdb->byte2 & SVFY_DPO)
8919                         flags |= CTL_LLF_DPO;
8920                 lba = scsi_4btoul(cdb->addr);
8921                 num_blocks = scsi_4btoul(cdb->length);
8922                 break;
8923         }
8924         case VERIFY_16: {
8925                 struct scsi_rw_16 *cdb;
8926
8927                 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8928                 if (cdb->byte2 & SVFY_BYTCHK)
8929                         bytchk = 1;
8930                 if (cdb->byte2 & SVFY_DPO)
8931                         flags |= CTL_LLF_DPO;
8932                 lba = scsi_8btou64(cdb->addr);
8933                 num_blocks = scsi_4btoul(cdb->length);
8934                 break;
8935         }
8936         default:
8937                 /*
8938                  * We got a command we don't support.  This shouldn't
8939                  * happen, commands should be filtered out above us.
8940                  */
8941                 ctl_set_invalid_opcode(ctsio);
8942                 ctl_done((union ctl_io *)ctsio);
8943                 return (CTL_RETVAL_COMPLETE);
8944         }
8945
8946         /*
8947          * The first check is to make sure we're in bounds, the second
8948          * check is to catch wrap-around problems.  If the lba + num blocks
8949          * is less than the lba, then we've wrapped around and the block
8950          * range is invalid anyway.
8951          */
8952         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8953          || ((lba + num_blocks) < lba)) {
8954                 ctl_set_lba_out_of_range(ctsio,
8955                     MAX(lba, lun->be_lun->maxlba + 1));
8956                 ctl_done((union ctl_io *)ctsio);
8957                 return (CTL_RETVAL_COMPLETE);
8958         }
8959
8960         /*
8961          * According to SBC-3, a transfer length of 0 is not an error.
8962          */
8963         if (num_blocks == 0) {
8964                 ctl_set_success(ctsio);
8965                 ctl_done((union ctl_io *)ctsio);
8966                 return (CTL_RETVAL_COMPLETE);
8967         }
8968
8969         lbalen = (struct ctl_lba_len_flags *)
8970             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8971         lbalen->lba = lba;
8972         lbalen->len = num_blocks;
8973         if (bytchk) {
8974                 lbalen->flags = CTL_LLF_COMPARE | flags;
8975                 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8976         } else {
8977                 lbalen->flags = CTL_LLF_VERIFY | flags;
8978                 ctsio->kern_total_len = 0;
8979         }
8980         ctsio->kern_rel_offset = 0;
8981
8982         CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8983         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8984         return (retval);
8985 }
8986
8987 int
8988 ctl_report_luns(struct ctl_scsiio *ctsio)
8989 {
8990         struct ctl_softc *softc = CTL_SOFTC(ctsio);
8991         struct ctl_port *port = CTL_PORT(ctsio);
8992         struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
8993         struct scsi_report_luns *cdb;
8994         struct scsi_report_luns_data *lun_data;
8995         int num_filled, num_luns, num_port_luns, retval;
8996         uint32_t alloc_len, lun_datalen;
8997         uint32_t initidx, targ_lun_id, lun_id;
8998
8999         retval = CTL_RETVAL_COMPLETE;
9000         cdb = (struct scsi_report_luns *)ctsio->cdb;
9001
9002         CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9003
9004         num_luns = 0;
9005         num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9006         mtx_lock(&softc->ctl_lock);
9007         for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9008                 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9009                         num_luns++;
9010         }
9011         mtx_unlock(&softc->ctl_lock);
9012
9013         switch (cdb->select_report) {
9014         case RPL_REPORT_DEFAULT:
9015         case RPL_REPORT_ALL:
9016         case RPL_REPORT_NONSUBSID:
9017                 break;
9018         case RPL_REPORT_WELLKNOWN:
9019         case RPL_REPORT_ADMIN:
9020         case RPL_REPORT_CONGLOM:
9021                 num_luns = 0;
9022                 break;
9023         default:
9024                 ctl_set_invalid_field(ctsio,
9025                                       /*sks_valid*/ 1,
9026                                       /*command*/ 1,
9027                                       /*field*/ 2,
9028                                       /*bit_valid*/ 0,
9029                                       /*bit*/ 0);
9030                 ctl_done((union ctl_io *)ctsio);
9031                 return (retval);
9032                 break; /* NOTREACHED */
9033         }
9034
9035         alloc_len = scsi_4btoul(cdb->length);
9036         /*
9037          * The initiator has to allocate at least 16 bytes for this request,
9038          * so he can at least get the header and the first LUN.  Otherwise
9039          * we reject the request (per SPC-3 rev 14, section 6.21).
9040          */
9041         if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9042             sizeof(struct scsi_report_luns_lundata))) {
9043                 ctl_set_invalid_field(ctsio,
9044                                       /*sks_valid*/ 1,
9045                                       /*command*/ 1,
9046                                       /*field*/ 6,
9047                                       /*bit_valid*/ 0,
9048                                       /*bit*/ 0);
9049                 ctl_done((union ctl_io *)ctsio);
9050                 return (retval);
9051         }
9052
9053         lun_datalen = sizeof(*lun_data) +
9054                 (num_luns * sizeof(struct scsi_report_luns_lundata));
9055
9056         ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9057         lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9058         ctsio->kern_sg_entries = 0;
9059
9060         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9061
9062         mtx_lock(&softc->ctl_lock);
9063         for (targ_lun_id = 0, num_filled = 0;
9064             targ_lun_id < num_port_luns && num_filled < num_luns;
9065             targ_lun_id++) {
9066                 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9067                 if (lun_id == UINT32_MAX)
9068                         continue;
9069                 lun = softc->ctl_luns[lun_id];
9070                 if (lun == NULL)
9071                         continue;
9072
9073                 be64enc(lun_data->luns[num_filled++].lundata,
9074                     ctl_encode_lun(targ_lun_id));
9075
9076                 /*
9077                  * According to SPC-3, rev 14 section 6.21:
9078                  *
9079                  * "The execution of a REPORT LUNS command to any valid and
9080                  * installed logical unit shall clear the REPORTED LUNS DATA
9081                  * HAS CHANGED unit attention condition for all logical
9082                  * units of that target with respect to the requesting
9083                  * initiator. A valid and installed logical unit is one
9084                  * having a PERIPHERAL QUALIFIER of 000b in the standard
9085                  * INQUIRY data (see 6.4.2)."
9086                  *
9087                  * If request_lun is NULL, the LUN this report luns command
9088                  * was issued to is either disabled or doesn't exist. In that
9089                  * case, we shouldn't clear any pending lun change unit
9090                  * attention.
9091                  */
9092                 if (request_lun != NULL) {
9093                         mtx_lock(&lun->lun_lock);
9094                         ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9095                         mtx_unlock(&lun->lun_lock);
9096                 }
9097         }
9098         mtx_unlock(&softc->ctl_lock);
9099
9100         /*
9101          * It's quite possible that we've returned fewer LUNs than we allocated
9102          * space for.  Trim it.
9103          */
9104         lun_datalen = sizeof(*lun_data) +
9105                 (num_filled * sizeof(struct scsi_report_luns_lundata));
9106         ctsio->kern_rel_offset = 0;
9107         ctsio->kern_sg_entries = 0;
9108         ctsio->kern_data_len = min(lun_datalen, alloc_len);
9109         ctsio->kern_total_len = ctsio->kern_data_len;
9110
9111         /*
9112          * We set this to the actual data length, regardless of how much
9113          * space we actually have to return results.  If the user looks at
9114          * this value, he'll know whether or not he allocated enough space
9115          * and reissue the command if necessary.  We don't support well
9116          * known logical units, so if the user asks for that, return none.
9117          */
9118         scsi_ulto4b(lun_datalen - 8, lun_data->length);
9119
9120         /*
9121          * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9122          * this request.
9123          */
9124         ctl_set_success(ctsio);
9125         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9126         ctsio->be_move_done = ctl_config_move_done;
9127         ctl_datamove((union ctl_io *)ctsio);
9128         return (retval);
9129 }
9130
9131 int
9132 ctl_request_sense(struct ctl_scsiio *ctsio)
9133 {
9134         struct ctl_softc *softc = CTL_SOFTC(ctsio);
9135         struct ctl_lun *lun = CTL_LUN(ctsio);
9136         struct scsi_request_sense *cdb;
9137         struct scsi_sense_data *sense_ptr, *ps;
9138         uint32_t initidx;
9139         int have_error;
9140         u_int sense_len = SSD_FULL_SIZE;
9141         scsi_sense_data_type sense_format;
9142         ctl_ua_type ua_type;
9143         uint8_t asc = 0, ascq = 0;
9144
9145         cdb = (struct scsi_request_sense *)ctsio->cdb;
9146
9147         CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9148
9149         /*
9150          * Determine which sense format the user wants.
9151          */
9152         if (cdb->byte2 & SRS_DESC)
9153                 sense_format = SSD_TYPE_DESC;
9154         else
9155                 sense_format = SSD_TYPE_FIXED;
9156
9157         ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9158         sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9159         ctsio->kern_sg_entries = 0;
9160         ctsio->kern_rel_offset = 0;
9161
9162         /*
9163          * struct scsi_sense_data, which is currently set to 256 bytes, is
9164          * larger than the largest allowed value for the length field in the
9165          * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9166          */
9167         ctsio->kern_data_len = cdb->length;
9168         ctsio->kern_total_len = cdb->length;
9169
9170         /*
9171          * If we don't have a LUN, we don't have any pending sense.
9172          */
9173         if (lun == NULL ||
9174             ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9175              softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9176                 /* "Logical unit not supported" */
9177                 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9178                     /*current_error*/ 1,
9179                     /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9180                     /*asc*/ 0x25,
9181                     /*ascq*/ 0x00,
9182                     SSD_ELEM_NONE);
9183                 goto send;
9184         }
9185
9186         have_error = 0;
9187         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9188         /*
9189          * Check for pending sense, and then for pending unit attentions.
9190          * Pending sense gets returned first, then pending unit attentions.
9191          */
9192         mtx_lock(&lun->lun_lock);
9193         ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9194         if (ps != NULL)
9195                 ps += initidx % CTL_MAX_INIT_PER_PORT;
9196         if (ps != NULL && ps->error_code != 0) {
9197                 scsi_sense_data_type stored_format;
9198
9199                 /*
9200                  * Check to see which sense format was used for the stored
9201                  * sense data.
9202                  */
9203                 stored_format = scsi_sense_type(ps);
9204
9205                 /*
9206                  * If the user requested a different sense format than the
9207                  * one we stored, then we need to convert it to the other
9208                  * format.  If we're going from descriptor to fixed format
9209                  * sense data, we may lose things in translation, depending
9210                  * on what options were used.
9211                  *
9212                  * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9213                  * for some reason we'll just copy it out as-is.
9214                  */
9215                 if ((stored_format == SSD_TYPE_FIXED)
9216                  && (sense_format == SSD_TYPE_DESC))
9217                         ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9218                             ps, (struct scsi_sense_data_desc *)sense_ptr);
9219                 else if ((stored_format == SSD_TYPE_DESC)
9220                       && (sense_format == SSD_TYPE_FIXED))
9221                         ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9222                             ps, (struct scsi_sense_data_fixed *)sense_ptr);
9223                 else
9224                         memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9225
9226                 ps->error_code = 0;
9227                 have_error = 1;
9228         } else {
9229                 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9230                     sense_format);
9231                 if (ua_type != CTL_UA_NONE)
9232                         have_error = 1;
9233         }
9234         if (have_error == 0) {
9235                 /*
9236                  * Report informational exception if have one and allowed.
9237                  */
9238                 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9239                         asc = lun->ie_asc;
9240                         ascq = lun->ie_ascq;
9241                 }
9242                 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9243                     /*current_error*/ 1,
9244                     /*sense_key*/ SSD_KEY_NO_SENSE,
9245                     /*asc*/ asc,
9246                     /*ascq*/ ascq,
9247                     SSD_ELEM_NONE);
9248         }
9249         mtx_unlock(&lun->lun_lock);
9250
9251 send:
9252         /*
9253          * We report the SCSI status as OK, since the status of the command
9254          * itself is OK.  We're reporting sense as parameter data.
9255          */
9256         ctl_set_success(ctsio);
9257         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9258         ctsio->be_move_done = ctl_config_move_done;
9259         ctl_datamove((union ctl_io *)ctsio);
9260         return (CTL_RETVAL_COMPLETE);
9261 }
9262
9263 int
9264 ctl_tur(struct ctl_scsiio *ctsio)
9265 {
9266
9267         CTL_DEBUG_PRINT(("ctl_tur\n"));
9268
9269         ctl_set_success(ctsio);
9270         ctl_done((union ctl_io *)ctsio);
9271
9272         return (CTL_RETVAL_COMPLETE);
9273 }
9274
9275 /*
9276  * SCSI VPD page 0x00, the Supported VPD Pages page.
9277  */
9278 static int
9279 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9280 {
9281         struct ctl_lun *lun = CTL_LUN(ctsio);
9282         struct scsi_vpd_supported_pages *pages;
9283         int sup_page_size;
9284         int p;
9285
9286         sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9287             SCSI_EVPD_NUM_SUPPORTED_PAGES;
9288         ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9289         pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9290         ctsio->kern_rel_offset = 0;
9291         ctsio->kern_sg_entries = 0;
9292         ctsio->kern_data_len = min(sup_page_size, alloc_len);
9293         ctsio->kern_total_len = ctsio->kern_data_len;
9294
9295         /*
9296          * The control device is always connected.  The disk device, on the
9297          * other hand, may not be online all the time.  Need to change this
9298          * to figure out whether the disk device is actually online or not.
9299          */
9300         if (lun != NULL)
9301                 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9302                                 lun->be_lun->lun_type;
9303         else
9304                 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9305
9306         p = 0;
9307         /* Supported VPD pages */
9308         pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9309         /* Serial Number */
9310         pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9311         /* Device Identification */
9312         pages->page_list[p++] = SVPD_DEVICE_ID;
9313         /* Extended INQUIRY Data */
9314         pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9315         /* Mode Page Policy */
9316         pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9317         /* SCSI Ports */
9318         pages->page_list[p++] = SVPD_SCSI_PORTS;
9319         /* Third-party Copy */
9320         pages->page_list[p++] = SVPD_SCSI_TPC;
9321         if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9322                 /* Block limits */
9323                 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9324                 /* Block Device Characteristics */
9325                 pages->page_list[p++] = SVPD_BDC;
9326                 /* Logical Block Provisioning */
9327                 pages->page_list[p++] = SVPD_LBP;
9328         }
9329         pages->length = p;
9330
9331         ctl_set_success(ctsio);
9332         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9333         ctsio->be_move_done = ctl_config_move_done;
9334         ctl_datamove((union ctl_io *)ctsio);
9335         return (CTL_RETVAL_COMPLETE);
9336 }
9337
9338 /*
9339  * SCSI VPD page 0x80, the Unit Serial Number page.
9340  */
9341 static int
9342 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9343 {
9344         struct ctl_lun *lun = CTL_LUN(ctsio);
9345         struct scsi_vpd_unit_serial_number *sn_ptr;
9346         int data_len;
9347
9348         data_len = 4 + CTL_SN_LEN;
9349         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9350         sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9351         ctsio->kern_rel_offset = 0;
9352         ctsio->kern_sg_entries = 0;
9353         ctsio->kern_data_len = min(data_len, alloc_len);
9354         ctsio->kern_total_len = ctsio->kern_data_len;
9355
9356         /*
9357          * The control device is always connected.  The disk device, on the
9358          * other hand, may not be online all the time.  Need to change this
9359          * to figure out whether the disk device is actually online or not.
9360          */
9361         if (lun != NULL)
9362                 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9363                                   lun->be_lun->lun_type;
9364         else
9365                 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9366
9367         sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9368         sn_ptr->length = CTL_SN_LEN;
9369         /*
9370          * If we don't have a LUN, we just leave the serial number as
9371          * all spaces.
9372          */
9373         if (lun != NULL) {
9374                 strncpy((char *)sn_ptr->serial_num,
9375                         (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9376         } else
9377                 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9378
9379         ctl_set_success(ctsio);
9380         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9381         ctsio->be_move_done = ctl_config_move_done;
9382         ctl_datamove((union ctl_io *)ctsio);
9383         return (CTL_RETVAL_COMPLETE);
9384 }
9385
9386
9387 /*
9388  * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9389  */
9390 static int
9391 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9392 {
9393         struct ctl_lun *lun = CTL_LUN(ctsio);
9394         struct scsi_vpd_extended_inquiry_data *eid_ptr;
9395         int data_len;
9396
9397         data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9398         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9399         eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9400         ctsio->kern_sg_entries = 0;
9401         ctsio->kern_rel_offset = 0;
9402         ctsio->kern_data_len = min(data_len, alloc_len);
9403         ctsio->kern_total_len = ctsio->kern_data_len;
9404
9405         /*
9406          * The control device is always connected.  The disk device, on the
9407          * other hand, may not be online all the time.
9408          */
9409         if (lun != NULL)
9410                 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9411                                      lun->be_lun->lun_type;
9412         else
9413                 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9414         eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9415         scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9416         /*
9417          * We support head of queue, ordered and simple tags.
9418          */
9419         eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9420         /*
9421          * Volatile cache supported.
9422          */
9423         eid_ptr->flags3 = SVPD_EID_V_SUP;
9424
9425         /*
9426          * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9427          * attention for a particular IT nexus on all LUNs once we report
9428          * it to that nexus once.  This bit is required as of SPC-4.
9429          */
9430         eid_ptr->flags4 = SVPD_EID_LUICLR;
9431
9432         /*
9433          * We support revert to defaults (RTD) bit in MODE SELECT.
9434          */
9435         eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9436
9437         /*
9438          * XXX KDM in order to correctly answer this, we would need
9439          * information from the SIM to determine how much sense data it
9440          * can send.  So this would really be a path inquiry field, most
9441          * likely.  This can be set to a maximum of 252 according to SPC-4,
9442          * but the hardware may or may not be able to support that much.
9443          * 0 just means that the maximum sense data length is not reported.
9444          */
9445         eid_ptr->max_sense_length = 0;
9446
9447         ctl_set_success(ctsio);
9448         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9449         ctsio->be_move_done = ctl_config_move_done;
9450         ctl_datamove((union ctl_io *)ctsio);
9451         return (CTL_RETVAL_COMPLETE);
9452 }
9453
9454 static int
9455 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9456 {
9457         struct ctl_lun *lun = CTL_LUN(ctsio);
9458         struct scsi_vpd_mode_page_policy *mpp_ptr;
9459         int data_len;
9460
9461         data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9462             sizeof(struct scsi_vpd_mode_page_policy_descr);
9463
9464         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9465         mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9466         ctsio->kern_rel_offset = 0;
9467         ctsio->kern_sg_entries = 0;
9468         ctsio->kern_data_len = min(data_len, alloc_len);
9469         ctsio->kern_total_len = ctsio->kern_data_len;
9470
9471         /*
9472          * The control device is always connected.  The disk device, on the
9473          * other hand, may not be online all the time.
9474          */
9475         if (lun != NULL)
9476                 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9477                                      lun->be_lun->lun_type;
9478         else
9479                 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9480         mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9481         scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9482         mpp_ptr->descr[0].page_code = 0x3f;
9483         mpp_ptr->descr[0].subpage_code = 0xff;
9484         mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9485
9486         ctl_set_success(ctsio);
9487         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9488         ctsio->be_move_done = ctl_config_move_done;
9489         ctl_datamove((union ctl_io *)ctsio);
9490         return (CTL_RETVAL_COMPLETE);
9491 }
9492
9493 /*
9494  * SCSI VPD page 0x83, the Device Identification page.
9495  */
9496 static int
9497 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9498 {
9499         struct ctl_softc *softc = CTL_SOFTC(ctsio);
9500         struct ctl_port *port = CTL_PORT(ctsio);
9501         struct ctl_lun *lun = CTL_LUN(ctsio);
9502         struct scsi_vpd_device_id *devid_ptr;
9503         struct scsi_vpd_id_descriptor *desc;
9504         int data_len, g;
9505         uint8_t proto;
9506
9507         data_len = sizeof(struct scsi_vpd_device_id) +
9508             sizeof(struct scsi_vpd_id_descriptor) +
9509                 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9510             sizeof(struct scsi_vpd_id_descriptor) +
9511                 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9512         if (lun && lun->lun_devid)
9513                 data_len += lun->lun_devid->len;
9514         if (port && port->port_devid)
9515                 data_len += port->port_devid->len;
9516         if (port && port->target_devid)
9517                 data_len += port->target_devid->len;
9518
9519         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9520         devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9521         ctsio->kern_sg_entries = 0;
9522         ctsio->kern_rel_offset = 0;
9523         ctsio->kern_sg_entries = 0;
9524         ctsio->kern_data_len = min(data_len, alloc_len);
9525         ctsio->kern_total_len = ctsio->kern_data_len;
9526
9527         /*
9528          * The control device is always connected.  The disk device, on the
9529          * other hand, may not be online all the time.
9530          */
9531         if (lun != NULL)
9532                 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9533                                      lun->be_lun->lun_type;
9534         else
9535                 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9536         devid_ptr->page_code = SVPD_DEVICE_ID;
9537         scsi_ulto2b(data_len - 4, devid_ptr->length);
9538
9539         if (port && port->port_type == CTL_PORT_FC)
9540                 proto = SCSI_PROTO_FC << 4;
9541         else if (port && port->port_type == CTL_PORT_SAS)
9542                 proto = SCSI_PROTO_SAS << 4;
9543         else if (port && port->port_type == CTL_PORT_ISCSI)
9544                 proto = SCSI_PROTO_ISCSI << 4;
9545         else
9546                 proto = SCSI_PROTO_SPI << 4;
9547         desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9548
9549         /*
9550          * We're using a LUN association here.  i.e., this device ID is a
9551          * per-LUN identifier.
9552          */
9553         if (lun && lun->lun_devid) {
9554                 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9555                 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9556                     lun->lun_devid->len);
9557         }
9558
9559         /*
9560          * This is for the WWPN which is a port association.
9561          */
9562         if (port && port->port_devid) {
9563                 memcpy(desc, port->port_devid->data, port->port_devid->len);
9564                 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9565                     port->port_devid->len);
9566         }
9567
9568         /*
9569          * This is for the Relative Target Port(type 4h) identifier
9570          */
9571         desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9572         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9573             SVPD_ID_TYPE_RELTARG;
9574         desc->length = 4;
9575         scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9576         desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9577             sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9578
9579         /*
9580          * This is for the Target Port Group(type 5h) identifier
9581          */
9582         desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9583         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9584             SVPD_ID_TYPE_TPORTGRP;
9585         desc->length = 4;
9586         if (softc->is_single ||
9587             (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9588                 g = 1;
9589         else
9590                 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9591         scsi_ulto2b(g, &desc->identifier[2]);
9592         desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9593             sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9594
9595         /*
9596          * This is for the Target identifier
9597          */
9598         if (port && port->target_devid) {
9599                 memcpy(desc, port->target_devid->data, port->target_devid->len);
9600         }
9601
9602         ctl_set_success(ctsio);
9603         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9604         ctsio->be_move_done = ctl_config_move_done;
9605         ctl_datamove((union ctl_io *)ctsio);
9606         return (CTL_RETVAL_COMPLETE);
9607 }
9608
9609 static int
9610 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9611 {
9612         struct ctl_softc *softc = CTL_SOFTC(ctsio);
9613         struct ctl_lun *lun = CTL_LUN(ctsio);
9614         struct scsi_vpd_scsi_ports *sp;
9615         struct scsi_vpd_port_designation *pd;
9616         struct scsi_vpd_port_designation_cont *pdc;
9617         struct ctl_port *port;
9618         int data_len, num_target_ports, iid_len, id_len;
9619
9620         num_target_ports = 0;
9621         iid_len = 0;
9622         id_len = 0;
9623         mtx_lock(&softc->ctl_lock);
9624         STAILQ_FOREACH(port, &softc->port_list, links) {
9625                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9626                         continue;
9627                 if (lun != NULL &&
9628                     ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9629                         continue;
9630                 num_target_ports++;
9631                 if (port->init_devid)
9632                         iid_len += port->init_devid->len;
9633                 if (port->port_devid)
9634                         id_len += port->port_devid->len;
9635         }
9636         mtx_unlock(&softc->ctl_lock);
9637
9638         data_len = sizeof(struct scsi_vpd_scsi_ports) +
9639             num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9640              sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9641         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9642         sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9643         ctsio->kern_sg_entries = 0;
9644         ctsio->kern_rel_offset = 0;
9645         ctsio->kern_sg_entries = 0;
9646         ctsio->kern_data_len = min(data_len, alloc_len);
9647         ctsio->kern_total_len = ctsio->kern_data_len;
9648
9649         /*
9650          * The control device is always connected.  The disk device, on the
9651          * other hand, may not be online all the time.  Need to change this
9652          * to figure out whether the disk device is actually online or not.
9653          */
9654         if (lun != NULL)
9655                 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9656                                   lun->be_lun->lun_type;
9657         else
9658                 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9659
9660         sp->page_code = SVPD_SCSI_PORTS;
9661         scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9662             sp->page_length);
9663         pd = &sp->design[0];
9664
9665         mtx_lock(&softc->ctl_lock);
9666         STAILQ_FOREACH(port, &softc->port_list, links) {
9667                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9668                         continue;
9669                 if (lun != NULL &&
9670                     ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9671                         continue;
9672                 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9673                 if (port->init_devid) {
9674                         iid_len = port->init_devid->len;
9675                         memcpy(pd->initiator_transportid,
9676                             port->init_devid->data, port->init_devid->len);
9677                 } else
9678                         iid_len = 0;
9679                 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9680                 pdc = (struct scsi_vpd_port_designation_cont *)
9681                     (&pd->initiator_transportid[iid_len]);
9682                 if (port->port_devid) {
9683                         id_len = port->port_devid->len;
9684                         memcpy(pdc->target_port_descriptors,
9685                             port->port_devid->data, port->port_devid->len);
9686                 } else
9687                         id_len = 0;
9688                 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9689                 pd = (struct scsi_vpd_port_designation *)
9690                     ((uint8_t *)pdc->target_port_descriptors + id_len);
9691         }
9692         mtx_unlock(&softc->ctl_lock);
9693
9694         ctl_set_success(ctsio);
9695         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9696         ctsio->be_move_done = ctl_config_move_done;
9697         ctl_datamove((union ctl_io *)ctsio);
9698         return (CTL_RETVAL_COMPLETE);
9699 }
9700
9701 static int
9702 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9703 {
9704         struct ctl_lun *lun = CTL_LUN(ctsio);
9705         struct scsi_vpd_block_limits *bl_ptr;
9706         const char *val;
9707         uint64_t ival;
9708
9709         ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9710         bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9711         ctsio->kern_sg_entries = 0;
9712         ctsio->kern_rel_offset = 0;
9713         ctsio->kern_sg_entries = 0;
9714         ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9715         ctsio->kern_total_len = ctsio->kern_data_len;
9716
9717         /*
9718          * The control device is always connected.  The disk device, on the
9719          * other hand, may not be online all the time.  Need to change this
9720          * to figure out whether the disk device is actually online or not.
9721          */
9722         if (lun != NULL)
9723                 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9724                                   lun->be_lun->lun_type;
9725         else
9726                 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9727
9728         bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9729         scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9730         bl_ptr->max_cmp_write_len = 0xff;
9731         scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9732         if (lun != NULL) {
9733                 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9734                 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9735                         ival = 0xffffffff;
9736                         val = dnvlist_get_string(lun->be_lun->options,
9737                             "unmap_max_lba", NULL);
9738                         if (val != NULL)
9739                                 ctl_expand_number(val, &ival);
9740                         scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9741                         ival = 0xffffffff;
9742                         val = dnvlist_get_string(lun->be_lun->options,
9743                             "unmap_max_descr", NULL);
9744                         if (val != NULL)
9745                                 ctl_expand_number(val, &ival);
9746                         scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9747                         if (lun->be_lun->ublockexp != 0) {
9748                                 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9749                                     bl_ptr->opt_unmap_grain);
9750                                 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9751                                     bl_ptr->unmap_grain_align);
9752                         }
9753                 }
9754                 scsi_ulto4b(lun->be_lun->atomicblock,
9755                     bl_ptr->max_atomic_transfer_length);
9756                 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9757                 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9758                 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9759                 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9760                 ival = UINT64_MAX;
9761                 val = dnvlist_get_string(lun->be_lun->options,
9762                     "write_same_max_lba", NULL);
9763                 if (val != NULL)
9764                         ctl_expand_number(val, &ival);
9765                 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9766         }
9767
9768         ctl_set_success(ctsio);
9769         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9770         ctsio->be_move_done = ctl_config_move_done;
9771         ctl_datamove((union ctl_io *)ctsio);
9772         return (CTL_RETVAL_COMPLETE);
9773 }
9774
9775 static int
9776 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9777 {
9778         struct ctl_lun *lun = CTL_LUN(ctsio);
9779         struct scsi_vpd_block_device_characteristics *bdc_ptr;
9780         const char *value;
9781         u_int i;
9782
9783         ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9784         bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9785         ctsio->kern_sg_entries = 0;
9786         ctsio->kern_rel_offset = 0;
9787         ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9788         ctsio->kern_total_len = ctsio->kern_data_len;
9789
9790         /*
9791          * The control device is always connected.  The disk device, on the
9792          * other hand, may not be online all the time.  Need to change this
9793          * to figure out whether the disk device is actually online or not.
9794          */
9795         if (lun != NULL)
9796                 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9797                                   lun->be_lun->lun_type;
9798         else
9799                 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9800         bdc_ptr->page_code = SVPD_BDC;
9801         scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9802         if (lun != NULL &&
9803             (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
9804                 i = strtol(value, NULL, 0);
9805         else
9806                 i = CTL_DEFAULT_ROTATION_RATE;
9807         scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9808         if (lun != NULL &&
9809             (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
9810                 i = strtol(value, NULL, 0);
9811         else
9812                 i = 0;
9813         bdc_ptr->wab_wac_ff = (i & 0x0f);
9814         bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9815
9816         ctl_set_success(ctsio);
9817         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9818         ctsio->be_move_done = ctl_config_move_done;
9819         ctl_datamove((union ctl_io *)ctsio);
9820         return (CTL_RETVAL_COMPLETE);
9821 }
9822
9823 static int
9824 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9825 {
9826         struct ctl_lun *lun = CTL_LUN(ctsio);
9827         struct scsi_vpd_logical_block_prov *lbp_ptr;
9828         const char *value;
9829
9830         ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9831         lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9832         ctsio->kern_sg_entries = 0;
9833         ctsio->kern_rel_offset = 0;
9834         ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9835         ctsio->kern_total_len = ctsio->kern_data_len;
9836
9837         /*
9838          * The control device is always connected.  The disk device, on the
9839          * other hand, may not be online all the time.  Need to change this
9840          * to figure out whether the disk device is actually online or not.
9841          */
9842         if (lun != NULL)
9843                 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9844                                   lun->be_lun->lun_type;
9845         else
9846                 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9847
9848         lbp_ptr->page_code = SVPD_LBP;
9849         scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9850         lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9851         if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9852                 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9853                     SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9854                 value = dnvlist_get_string(lun->be_lun->options,
9855                     "provisioning_type", NULL);
9856                 if (value != NULL) {
9857                         if (strcmp(value, "resource") == 0)
9858                                 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9859                         else if (strcmp(value, "thin") == 0)
9860                                 lbp_ptr->prov_type = SVPD_LBP_THIN;
9861                 } else
9862                         lbp_ptr->prov_type = SVPD_LBP_THIN;
9863         }
9864
9865         ctl_set_success(ctsio);
9866         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9867         ctsio->be_move_done = ctl_config_move_done;
9868         ctl_datamove((union ctl_io *)ctsio);
9869         return (CTL_RETVAL_COMPLETE);
9870 }
9871
9872 /*
9873  * INQUIRY with the EVPD bit set.
9874  */
9875 static int
9876 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9877 {
9878         struct ctl_lun *lun = CTL_LUN(ctsio);
9879         struct scsi_inquiry *cdb;
9880         int alloc_len, retval;
9881
9882         cdb = (struct scsi_inquiry *)ctsio->cdb;
9883         alloc_len = scsi_2btoul(cdb->length);
9884
9885         switch (cdb->page_code) {
9886         case SVPD_SUPPORTED_PAGES:
9887                 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9888                 break;
9889         case SVPD_UNIT_SERIAL_NUMBER:
9890                 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9891                 break;
9892         case SVPD_DEVICE_ID:
9893                 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9894                 break;
9895         case SVPD_EXTENDED_INQUIRY_DATA:
9896                 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9897                 break;
9898         case SVPD_MODE_PAGE_POLICY:
9899                 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9900                 break;
9901         case SVPD_SCSI_PORTS:
9902                 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9903                 break;
9904         case SVPD_SCSI_TPC:
9905                 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9906                 break;
9907         case SVPD_BLOCK_LIMITS:
9908                 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9909                         goto err;
9910                 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9911                 break;
9912         case SVPD_BDC:
9913                 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9914                         goto err;
9915                 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9916                 break;
9917         case SVPD_LBP:
9918                 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9919                         goto err;
9920                 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9921                 break;
9922         default:
9923 err:
9924                 ctl_set_invalid_field(ctsio,
9925                                       /*sks_valid*/ 1,
9926                                       /*command*/ 1,
9927                                       /*field*/ 2,
9928                                       /*bit_valid*/ 0,
9929                                       /*bit*/ 0);
9930                 ctl_done((union ctl_io *)ctsio);
9931                 retval = CTL_RETVAL_COMPLETE;
9932                 break;
9933         }
9934
9935         return (retval);
9936 }
9937
9938 /*
9939  * Standard INQUIRY data.
9940  */
9941 static int
9942 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9943 {
9944         struct ctl_softc *softc = CTL_SOFTC(ctsio);
9945         struct ctl_port *port = CTL_PORT(ctsio);
9946         struct ctl_lun *lun = CTL_LUN(ctsio);
9947         struct scsi_inquiry_data *inq_ptr;
9948         struct scsi_inquiry *cdb;
9949         const char *val;
9950         uint32_t alloc_len, data_len;
9951         ctl_port_type port_type;
9952
9953         port_type = port->port_type;
9954         if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
9955                 port_type = CTL_PORT_SCSI;
9956
9957         cdb = (struct scsi_inquiry *)ctsio->cdb;
9958         alloc_len = scsi_2btoul(cdb->length);
9959
9960         /*
9961          * We malloc the full inquiry data size here and fill it
9962          * in.  If the user only asks for less, we'll give him
9963          * that much.
9964          */
9965         data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
9966         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9967         inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9968         ctsio->kern_sg_entries = 0;
9969         ctsio->kern_rel_offset = 0;
9970         ctsio->kern_data_len = min(data_len, alloc_len);
9971         ctsio->kern_total_len = ctsio->kern_data_len;
9972
9973         if (lun != NULL) {
9974                 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
9975                     softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
9976                         inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9977                             lun->be_lun->lun_type;
9978                 } else {
9979                         inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
9980                             lun->be_lun->lun_type;
9981                 }
9982                 if (lun->flags & CTL_LUN_REMOVABLE)
9983                         inq_ptr->dev_qual2 |= SID_RMB;
9984         } else
9985                 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9986
9987         /* RMB in byte 2 is 0 */
9988         inq_ptr->version = SCSI_REV_SPC5;
9989
9990         /*
9991          * According to SAM-3, even if a device only supports a single
9992          * level of LUN addressing, it should still set the HISUP bit:
9993          *
9994          * 4.9.1 Logical unit numbers overview
9995          *
9996          * All logical unit number formats described in this standard are
9997          * hierarchical in structure even when only a single level in that
9998          * hierarchy is used. The HISUP bit shall be set to one in the
9999          * standard INQUIRY data (see SPC-2) when any logical unit number
10000          * format described in this standard is used.  Non-hierarchical
10001          * formats are outside the scope of this standard.
10002          *
10003          * Therefore we set the HiSup bit here.
10004          *
10005          * The response format is 2, per SPC-3.
10006          */
10007         inq_ptr->response_format = SID_HiSup | 2;
10008
10009         inq_ptr->additional_length = data_len -
10010             (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10011         CTL_DEBUG_PRINT(("additional_length = %d\n",
10012                          inq_ptr->additional_length));
10013
10014         inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10015         if (port_type == CTL_PORT_SCSI)
10016                 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10017         inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10018         inq_ptr->flags = SID_CmdQue;
10019         if (port_type == CTL_PORT_SCSI)
10020                 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10021
10022         /*
10023          * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10024          * We have 8 bytes for the vendor name, and 16 bytes for the device
10025          * name and 4 bytes for the revision.
10026          */
10027         if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10028             "vendor", NULL)) == NULL) {
10029                 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10030         } else {
10031                 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10032                 strncpy(inq_ptr->vendor, val,
10033                     min(sizeof(inq_ptr->vendor), strlen(val)));
10034         }
10035         if (lun == NULL) {
10036                 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10037                     sizeof(inq_ptr->product));
10038         } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10039             NULL)) == NULL) {
10040                 switch (lun->be_lun->lun_type) {
10041                 case T_DIRECT:
10042                         strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10043                             sizeof(inq_ptr->product));
10044                         break;
10045                 case T_PROCESSOR:
10046                         strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10047                             sizeof(inq_ptr->product));
10048                         break;
10049                 case T_CDROM:
10050                         strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10051                             sizeof(inq_ptr->product));
10052                         break;
10053                 default:
10054                         strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10055                             sizeof(inq_ptr->product));
10056                         break;
10057                 }
10058         } else {
10059                 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10060                 strncpy(inq_ptr->product, val,
10061                     min(sizeof(inq_ptr->product), strlen(val)));
10062         }
10063
10064         /*
10065          * XXX make this a macro somewhere so it automatically gets
10066          * incremented when we make changes.
10067          */
10068         if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10069             "revision", NULL)) == NULL) {
10070                 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10071         } else {
10072                 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10073                 strncpy(inq_ptr->revision, val,
10074                     min(sizeof(inq_ptr->revision), strlen(val)));
10075         }
10076
10077         /*
10078          * For parallel SCSI, we support double transition and single
10079          * transition clocking.  We also support QAS (Quick Arbitration
10080          * and Selection) and Information Unit transfers on both the
10081          * control and array devices.
10082          */
10083         if (port_type == CTL_PORT_SCSI)
10084                 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10085                                     SID_SPI_IUS;
10086
10087         /* SAM-6 (no version claimed) */
10088         scsi_ulto2b(0x00C0, inq_ptr->version1);
10089         /* SPC-5 (no version claimed) */
10090         scsi_ulto2b(0x05C0, inq_ptr->version2);
10091         if (port_type == CTL_PORT_FC) {
10092                 /* FCP-2 ANSI INCITS.350:2003 */
10093                 scsi_ulto2b(0x0917, inq_ptr->version3);
10094         } else if (port_type == CTL_PORT_SCSI) {
10095                 /* SPI-4 ANSI INCITS.362:200x */
10096                 scsi_ulto2b(0x0B56, inq_ptr->version3);
10097         } else if (port_type == CTL_PORT_ISCSI) {
10098                 /* iSCSI (no version claimed) */
10099                 scsi_ulto2b(0x0960, inq_ptr->version3);
10100         } else if (port_type == CTL_PORT_SAS) {
10101                 /* SAS (no version claimed) */
10102                 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10103         } else if (port_type == CTL_PORT_UMASS) {
10104                 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10105                 scsi_ulto2b(0x1730, inq_ptr->version3);
10106         }
10107
10108         if (lun == NULL) {
10109                 /* SBC-4 (no version claimed) */
10110                 scsi_ulto2b(0x0600, inq_ptr->version4);
10111         } else {
10112                 switch (lun->be_lun->lun_type) {
10113                 case T_DIRECT:
10114                         /* SBC-4 (no version claimed) */
10115                         scsi_ulto2b(0x0600, inq_ptr->version4);
10116                         break;
10117                 case T_PROCESSOR:
10118                         break;
10119                 case T_CDROM:
10120                         /* MMC-6 (no version claimed) */
10121                         scsi_ulto2b(0x04E0, inq_ptr->version4);
10122                         break;
10123                 default:
10124                         break;
10125                 }
10126         }
10127
10128         ctl_set_success(ctsio);
10129         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10130         ctsio->be_move_done = ctl_config_move_done;
10131         ctl_datamove((union ctl_io *)ctsio);
10132         return (CTL_RETVAL_COMPLETE);
10133 }
10134
10135 int
10136 ctl_inquiry(struct ctl_scsiio *ctsio)
10137 {
10138         struct scsi_inquiry *cdb;
10139         int retval;
10140
10141         CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10142
10143         cdb = (struct scsi_inquiry *)ctsio->cdb;
10144         if (cdb->byte2 & SI_EVPD)
10145                 retval = ctl_inquiry_evpd(ctsio);
10146         else if (cdb->page_code == 0)
10147                 retval = ctl_inquiry_std(ctsio);
10148         else {
10149                 ctl_set_invalid_field(ctsio,
10150                                       /*sks_valid*/ 1,
10151                                       /*command*/ 1,
10152                                       /*field*/ 2,
10153                                       /*bit_valid*/ 0,
10154                                       /*bit*/ 0);
10155                 ctl_done((union ctl_io *)ctsio);
10156                 return (CTL_RETVAL_COMPLETE);
10157         }
10158
10159         return (retval);
10160 }
10161
10162 int
10163 ctl_get_config(struct ctl_scsiio *ctsio)
10164 {
10165         struct ctl_lun *lun = CTL_LUN(ctsio);
10166         struct scsi_get_config_header *hdr;
10167         struct scsi_get_config_feature *feature;
10168         struct scsi_get_config *cdb;
10169         uint32_t alloc_len, data_len;
10170         int rt, starting;
10171
10172         cdb = (struct scsi_get_config *)ctsio->cdb;
10173         rt = (cdb->rt & SGC_RT_MASK);
10174         starting = scsi_2btoul(cdb->starting_feature);
10175         alloc_len = scsi_2btoul(cdb->length);
10176
10177         data_len = sizeof(struct scsi_get_config_header) +
10178             sizeof(struct scsi_get_config_feature) + 8 +
10179             sizeof(struct scsi_get_config_feature) + 8 +
10180             sizeof(struct scsi_get_config_feature) + 4 +
10181             sizeof(struct scsi_get_config_feature) + 4 +
10182             sizeof(struct scsi_get_config_feature) + 8 +
10183             sizeof(struct scsi_get_config_feature) +
10184             sizeof(struct scsi_get_config_feature) + 4 +
10185             sizeof(struct scsi_get_config_feature) + 4 +
10186             sizeof(struct scsi_get_config_feature) + 4 +
10187             sizeof(struct scsi_get_config_feature) + 4 +
10188             sizeof(struct scsi_get_config_feature) + 4 +
10189             sizeof(struct scsi_get_config_feature) + 4;
10190         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10191         ctsio->kern_sg_entries = 0;
10192         ctsio->kern_rel_offset = 0;
10193
10194         hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10195         if (lun->flags & CTL_LUN_NO_MEDIA)
10196                 scsi_ulto2b(0x0000, hdr->current_profile);
10197         else
10198                 scsi_ulto2b(0x0010, hdr->current_profile);
10199         feature = (struct scsi_get_config_feature *)(hdr + 1);
10200
10201         if (starting > 0x003b)
10202                 goto done;
10203         if (starting > 0x003a)
10204                 goto f3b;
10205         if (starting > 0x002b)
10206                 goto f3a;
10207         if (starting > 0x002a)
10208                 goto f2b;
10209         if (starting > 0x001f)
10210                 goto f2a;
10211         if (starting > 0x001e)
10212                 goto f1f;
10213         if (starting > 0x001d)
10214                 goto f1e;
10215         if (starting > 0x0010)
10216                 goto f1d;
10217         if (starting > 0x0003)
10218                 goto f10;
10219         if (starting > 0x0002)
10220                 goto f3;
10221         if (starting > 0x0001)
10222                 goto f2;
10223         if (starting > 0x0000)
10224                 goto f1;
10225
10226         /* Profile List */
10227         scsi_ulto2b(0x0000, feature->feature_code);
10228         feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10229         feature->add_length = 8;
10230         scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10231         feature->feature_data[2] = 0x00;
10232         scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10233         feature->feature_data[6] = 0x01;
10234         feature = (struct scsi_get_config_feature *)
10235             &feature->feature_data[feature->add_length];
10236
10237 f1:     /* Core */
10238         scsi_ulto2b(0x0001, feature->feature_code);
10239         feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10240         feature->add_length = 8;
10241         scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10242         feature->feature_data[4] = 0x03;
10243         feature = (struct scsi_get_config_feature *)
10244             &feature->feature_data[feature->add_length];
10245
10246 f2:     /* Morphing */
10247         scsi_ulto2b(0x0002, feature->feature_code);
10248         feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10249         feature->add_length = 4;
10250         feature->feature_data[0] = 0x02;
10251         feature = (struct scsi_get_config_feature *)
10252             &feature->feature_data[feature->add_length];
10253
10254 f3:     /* Removable Medium */
10255         scsi_ulto2b(0x0003, feature->feature_code);
10256         feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10257         feature->add_length = 4;
10258         feature->feature_data[0] = 0x39;
10259         feature = (struct scsi_get_config_feature *)
10260             &feature->feature_data[feature->add_length];
10261
10262         if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10263                 goto done;
10264
10265 f10:    /* Random Read */
10266         scsi_ulto2b(0x0010, feature->feature_code);
10267         feature->flags = 0x00;
10268         if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10269                 feature->flags |= SGC_F_CURRENT;
10270         feature->add_length = 8;
10271         scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10272         scsi_ulto2b(1, &feature->feature_data[4]);
10273         feature->feature_data[6] = 0x00;
10274         feature = (struct scsi_get_config_feature *)
10275             &feature->feature_data[feature->add_length];
10276
10277 f1d:    /* Multi-Read */
10278         scsi_ulto2b(0x001D, feature->feature_code);
10279         feature->flags = 0x00;
10280         if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10281                 feature->flags |= SGC_F_CURRENT;
10282         feature->add_length = 0;
10283         feature = (struct scsi_get_config_feature *)
10284             &feature->feature_data[feature->add_length];
10285
10286 f1e:    /* CD Read */
10287         scsi_ulto2b(0x001E, feature->feature_code);
10288         feature->flags = 0x00;
10289         if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10290                 feature->flags |= SGC_F_CURRENT;
10291         feature->add_length = 4;
10292         feature->feature_data[0] = 0x00;
10293         feature = (struct scsi_get_config_feature *)
10294             &feature->feature_data[feature->add_length];
10295
10296 f1f:    /* DVD Read */
10297         scsi_ulto2b(0x001F, feature->feature_code);
10298         feature->flags = 0x08;
10299         if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10300                 feature->flags |= SGC_F_CURRENT;
10301         feature->add_length = 4;
10302         feature->feature_data[0] = 0x01;
10303         feature->feature_data[2] = 0x03;
10304         feature = (struct scsi_get_config_feature *)
10305             &feature->feature_data[feature->add_length];
10306
10307 f2a:    /* DVD+RW */
10308         scsi_ulto2b(0x002A, feature->feature_code);
10309         feature->flags = 0x04;
10310         if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10311                 feature->flags |= SGC_F_CURRENT;
10312         feature->add_length = 4;
10313         feature->feature_data[0] = 0x00;
10314         feature->feature_data[1] = 0x00;
10315         feature = (struct scsi_get_config_feature *)
10316             &feature->feature_data[feature->add_length];
10317
10318 f2b:    /* DVD+R */
10319         scsi_ulto2b(0x002B, feature->feature_code);
10320         feature->flags = 0x00;
10321         if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10322                 feature->flags |= SGC_F_CURRENT;
10323         feature->add_length = 4;
10324         feature->feature_data[0] = 0x00;
10325         feature = (struct scsi_get_config_feature *)
10326             &feature->feature_data[feature->add_length];
10327
10328 f3a:    /* DVD+RW Dual Layer */
10329         scsi_ulto2b(0x003A, feature->feature_code);
10330         feature->flags = 0x00;
10331         if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10332                 feature->flags |= SGC_F_CURRENT;
10333         feature->add_length = 4;
10334         feature->feature_data[0] = 0x00;
10335         feature->feature_data[1] = 0x00;
10336         feature = (struct scsi_get_config_feature *)
10337             &feature->feature_data[feature->add_length];
10338
10339 f3b:    /* DVD+R Dual Layer */
10340         scsi_ulto2b(0x003B, feature->feature_code);
10341         feature->flags = 0x00;
10342         if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10343                 feature->flags |= SGC_F_CURRENT;
10344         feature->add_length = 4;
10345         feature->feature_data[0] = 0x00;
10346         feature = (struct scsi_get_config_feature *)
10347             &feature->feature_data[feature->add_length];
10348
10349 done:
10350         data_len = (uint8_t *)feature - (uint8_t *)hdr;
10351         if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10352                 feature = (struct scsi_get_config_feature *)(hdr + 1);
10353                 if (scsi_2btoul(feature->feature_code) == starting)
10354                         feature = (struct scsi_get_config_feature *)
10355                             &feature->feature_data[feature->add_length];
10356                 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10357         }
10358         scsi_ulto4b(data_len - 4, hdr->data_length);
10359         ctsio->kern_data_len = min(data_len, alloc_len);
10360         ctsio->kern_total_len = ctsio->kern_data_len;
10361
10362         ctl_set_success(ctsio);
10363         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10364         ctsio->be_move_done = ctl_config_move_done;
10365         ctl_datamove((union ctl_io *)ctsio);
10366         return (CTL_RETVAL_COMPLETE);
10367 }
10368
10369 int
10370 ctl_get_event_status(struct ctl_scsiio *ctsio)
10371 {
10372         struct scsi_get_event_status_header *hdr;
10373         struct scsi_get_event_status *cdb;
10374         uint32_t alloc_len, data_len;
10375
10376         cdb = (struct scsi_get_event_status *)ctsio->cdb;
10377         if ((cdb->byte2 & SGESN_POLLED) == 0) {
10378                 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10379                     /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10380                 ctl_done((union ctl_io *)ctsio);
10381                 return (CTL_RETVAL_COMPLETE);
10382         }
10383         alloc_len = scsi_2btoul(cdb->length);
10384
10385         data_len = sizeof(struct scsi_get_event_status_header);
10386         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10387         ctsio->kern_sg_entries = 0;
10388         ctsio->kern_rel_offset = 0;
10389         ctsio->kern_data_len = min(data_len, alloc_len);
10390         ctsio->kern_total_len = ctsio->kern_data_len;
10391
10392         hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10393         scsi_ulto2b(0, hdr->descr_length);
10394         hdr->nea_class = SGESN_NEA;
10395         hdr->supported_class = 0;
10396
10397         ctl_set_success(ctsio);
10398         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10399         ctsio->be_move_done = ctl_config_move_done;
10400         ctl_datamove((union ctl_io *)ctsio);
10401         return (CTL_RETVAL_COMPLETE);
10402 }
10403
10404 int
10405 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10406 {
10407         struct scsi_mechanism_status_header *hdr;
10408         struct scsi_mechanism_status *cdb;
10409         uint32_t alloc_len, data_len;
10410
10411         cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10412         alloc_len = scsi_2btoul(cdb->length);
10413
10414         data_len = sizeof(struct scsi_mechanism_status_header);
10415         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10416         ctsio->kern_sg_entries = 0;
10417         ctsio->kern_rel_offset = 0;
10418         ctsio->kern_data_len = min(data_len, alloc_len);
10419         ctsio->kern_total_len = ctsio->kern_data_len;
10420
10421         hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10422         hdr->state1 = 0x00;
10423         hdr->state2 = 0xe0;
10424         scsi_ulto3b(0, hdr->lba);
10425         hdr->slots_num = 0;
10426         scsi_ulto2b(0, hdr->slots_length);
10427
10428         ctl_set_success(ctsio);
10429         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10430         ctsio->be_move_done = ctl_config_move_done;
10431         ctl_datamove((union ctl_io *)ctsio);
10432         return (CTL_RETVAL_COMPLETE);
10433 }
10434
10435 static void
10436 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10437 {
10438
10439         lba += 150;
10440         buf[0] = 0;
10441         buf[1] = bin2bcd((lba / 75) / 60);
10442         buf[2] = bin2bcd((lba / 75) % 60);
10443         buf[3] = bin2bcd(lba % 75);
10444 }
10445
10446 int
10447 ctl_read_toc(struct ctl_scsiio *ctsio)
10448 {
10449         struct ctl_lun *lun = CTL_LUN(ctsio);
10450         struct scsi_read_toc_hdr *hdr;
10451         struct scsi_read_toc_type01_descr *descr;
10452         struct scsi_read_toc *cdb;
10453         uint32_t alloc_len, data_len;
10454         int format, msf;
10455
10456         cdb = (struct scsi_read_toc *)ctsio->cdb;
10457         msf = (cdb->byte2 & CD_MSF) != 0;
10458         format = cdb->format;
10459         alloc_len = scsi_2btoul(cdb->data_len);
10460
10461         data_len = sizeof(struct scsi_read_toc_hdr);
10462         if (format == 0)
10463                 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10464         else
10465                 data_len += sizeof(struct scsi_read_toc_type01_descr);
10466         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10467         ctsio->kern_sg_entries = 0;
10468         ctsio->kern_rel_offset = 0;
10469         ctsio->kern_data_len = min(data_len, alloc_len);
10470         ctsio->kern_total_len = ctsio->kern_data_len;
10471
10472         hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10473         if (format == 0) {
10474                 scsi_ulto2b(0x12, hdr->data_length);
10475                 hdr->first = 1;
10476                 hdr->last = 1;
10477                 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10478                 descr->addr_ctl = 0x14;
10479                 descr->track_number = 1;
10480                 if (msf)
10481                         ctl_ultomsf(0, descr->track_start);
10482                 else
10483                         scsi_ulto4b(0, descr->track_start);
10484                 descr++;
10485                 descr->addr_ctl = 0x14;
10486                 descr->track_number = 0xaa;
10487                 if (msf)
10488                         ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10489                 else
10490                         scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10491         } else {
10492                 scsi_ulto2b(0x0a, hdr->data_length);
10493                 hdr->first = 1;
10494                 hdr->last = 1;
10495                 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10496                 descr->addr_ctl = 0x14;
10497                 descr->track_number = 1;
10498                 if (msf)
10499                         ctl_ultomsf(0, descr->track_start);
10500                 else
10501                         scsi_ulto4b(0, descr->track_start);
10502         }
10503
10504         ctl_set_success(ctsio);
10505         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10506         ctsio->be_move_done = ctl_config_move_done;
10507         ctl_datamove((union ctl_io *)ctsio);
10508         return (CTL_RETVAL_COMPLETE);
10509 }
10510
10511 /*
10512  * For known CDB types, parse the LBA and length.
10513  */
10514 static int
10515 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10516 {
10517         if (io->io_hdr.io_type != CTL_IO_SCSI)
10518                 return (1);
10519
10520         switch (io->scsiio.cdb[0]) {
10521         case COMPARE_AND_WRITE: {
10522                 struct scsi_compare_and_write *cdb;
10523
10524                 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10525
10526                 *lba = scsi_8btou64(cdb->addr);
10527                 *len = cdb->length;
10528                 break;
10529         }
10530         case READ_6:
10531         case WRITE_6: {
10532                 struct scsi_rw_6 *cdb;
10533
10534                 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10535
10536                 *lba = scsi_3btoul(cdb->addr);
10537                 /* only 5 bits are valid in the most significant address byte */
10538                 *lba &= 0x1fffff;
10539                 *len = cdb->length;
10540                 break;
10541         }
10542         case READ_10:
10543         case WRITE_10: {
10544                 struct scsi_rw_10 *cdb;
10545
10546                 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10547
10548                 *lba = scsi_4btoul(cdb->addr);
10549                 *len = scsi_2btoul(cdb->length);
10550                 break;
10551         }
10552         case WRITE_VERIFY_10: {
10553                 struct scsi_write_verify_10 *cdb;
10554
10555                 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10556
10557                 *lba = scsi_4btoul(cdb->addr);
10558                 *len = scsi_2btoul(cdb->length);
10559                 break;
10560         }
10561         case READ_12:
10562         case WRITE_12: {
10563                 struct scsi_rw_12 *cdb;
10564
10565                 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10566
10567                 *lba = scsi_4btoul(cdb->addr);
10568                 *len = scsi_4btoul(cdb->length);
10569                 break;
10570         }
10571         case WRITE_VERIFY_12: {
10572                 struct scsi_write_verify_12 *cdb;
10573
10574                 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10575
10576                 *lba = scsi_4btoul(cdb->addr);
10577                 *len = scsi_4btoul(cdb->length);
10578                 break;
10579         }
10580         case READ_16:
10581         case WRITE_16: {
10582                 struct scsi_rw_16 *cdb;
10583
10584                 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10585
10586                 *lba = scsi_8btou64(cdb->addr);
10587                 *len = scsi_4btoul(cdb->length);
10588                 break;
10589         }
10590         case WRITE_ATOMIC_16: {
10591                 struct scsi_write_atomic_16 *cdb;
10592
10593                 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10594
10595                 *lba = scsi_8btou64(cdb->addr);
10596                 *len = scsi_2btoul(cdb->length);
10597                 break;
10598         }
10599         case WRITE_VERIFY_16: {
10600                 struct scsi_write_verify_16 *cdb;
10601
10602                 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10603
10604                 *lba = scsi_8btou64(cdb->addr);
10605                 *len = scsi_4btoul(cdb->length);
10606                 break;
10607         }
10608         case WRITE_SAME_10: {
10609                 struct scsi_write_same_10 *cdb;
10610
10611                 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10612
10613                 *lba = scsi_4btoul(cdb->addr);
10614                 *len = scsi_2btoul(cdb->length);
10615                 break;
10616         }
10617         case WRITE_SAME_16: {
10618                 struct scsi_write_same_16 *cdb;
10619
10620                 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10621
10622                 *lba = scsi_8btou64(cdb->addr);
10623                 *len = scsi_4btoul(cdb->length);
10624                 break;
10625         }
10626         case VERIFY_10: {
10627                 struct scsi_verify_10 *cdb;
10628
10629                 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10630
10631                 *lba = scsi_4btoul(cdb->addr);
10632                 *len = scsi_2btoul(cdb->length);
10633                 break;
10634         }
10635         case VERIFY_12: {
10636                 struct scsi_verify_12 *cdb;
10637
10638                 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10639
10640                 *lba = scsi_4btoul(cdb->addr);
10641                 *len = scsi_4btoul(cdb->length);
10642                 break;
10643         }
10644         case VERIFY_16: {
10645                 struct scsi_verify_16 *cdb;
10646
10647                 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10648
10649                 *lba = scsi_8btou64(cdb->addr);
10650                 *len = scsi_4btoul(cdb->length);
10651                 break;
10652         }
10653         case UNMAP: {
10654                 *lba = 0;
10655                 *len = UINT64_MAX;
10656                 break;
10657         }
10658         case SERVICE_ACTION_IN: {       /* GET LBA STATUS */
10659                 struct scsi_get_lba_status *cdb;
10660
10661                 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10662                 *lba = scsi_8btou64(cdb->addr);
10663                 *len = UINT32_MAX;
10664                 break;
10665         }
10666         default:
10667                 return (1);
10668                 break; /* NOTREACHED */
10669         }
10670
10671         return (0);
10672 }
10673
10674 static ctl_action
10675 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10676     bool seq)
10677 {
10678         uint64_t endlba1, endlba2;
10679
10680         endlba1 = lba1 + len1 - (seq ? 0 : 1);
10681         endlba2 = lba2 + len2 - 1;
10682
10683         if ((endlba1 < lba2) || (endlba2 < lba1))
10684                 return (CTL_ACTION_PASS);
10685         else
10686                 return (CTL_ACTION_BLOCK);
10687 }
10688
10689 static int
10690 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10691 {
10692         struct ctl_ptr_len_flags *ptrlen;
10693         struct scsi_unmap_desc *buf, *end, *range;
10694         uint64_t lba;
10695         uint32_t len;
10696
10697         /* If not UNMAP -- go other way. */
10698         if (io->io_hdr.io_type != CTL_IO_SCSI ||
10699             io->scsiio.cdb[0] != UNMAP)
10700                 return (CTL_ACTION_ERROR);
10701
10702         /* If UNMAP without data -- block and wait for data. */
10703         ptrlen = (struct ctl_ptr_len_flags *)
10704             &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10705         if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10706             ptrlen->ptr == NULL)
10707                 return (CTL_ACTION_BLOCK);
10708
10709         /* UNMAP with data -- check for collision. */
10710         buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10711         end = buf + ptrlen->len / sizeof(*buf);
10712         for (range = buf; range < end; range++) {
10713                 lba = scsi_8btou64(range->lba);
10714                 len = scsi_4btoul(range->length);
10715                 if ((lba < lba2 + len2) && (lba + len > lba2))
10716                         return (CTL_ACTION_BLOCK);
10717         }
10718         return (CTL_ACTION_PASS);
10719 }
10720
10721 static ctl_action
10722 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10723 {
10724         uint64_t lba1, lba2;
10725         uint64_t len1, len2;
10726         int retval;
10727
10728         if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10729                 return (CTL_ACTION_ERROR);
10730
10731         retval = ctl_extent_check_unmap(io1, lba2, len2);
10732         if (retval != CTL_ACTION_ERROR)
10733                 return (retval);
10734
10735         if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10736                 return (CTL_ACTION_ERROR);
10737
10738         if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10739                 seq = FALSE;
10740         return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10741 }
10742
10743 static ctl_action
10744 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10745 {
10746         uint64_t lba1, lba2;
10747         uint64_t len1, len2;
10748
10749         if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10750                 return (CTL_ACTION_PASS);
10751         if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10752                 return (CTL_ACTION_ERROR);
10753         if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10754                 return (CTL_ACTION_ERROR);
10755
10756         if (lba1 + len1 == lba2)
10757                 return (CTL_ACTION_BLOCK);
10758         return (CTL_ACTION_PASS);
10759 }
10760
10761 static ctl_action
10762 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10763     union ctl_io *ooa_io)
10764 {
10765         const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10766         const ctl_serialize_action *serialize_row;
10767
10768         /*
10769          * Aborted commands are not going to be executed and may even
10770          * not report completion, so we don't care about their order.
10771          * Let them complete ASAP to clean the OOA queue.
10772          */
10773         if (pending_io->io_hdr.flags & CTL_FLAG_ABORT)
10774                 return (CTL_ACTION_SKIP);
10775
10776         /*
10777          * The initiator attempted multiple untagged commands at the same
10778          * time.  Can't do that.
10779          */
10780         if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10781          && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10782          && ((pending_io->io_hdr.nexus.targ_port ==
10783               ooa_io->io_hdr.nexus.targ_port)
10784           && (pending_io->io_hdr.nexus.initid ==
10785               ooa_io->io_hdr.nexus.initid))
10786          && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10787               CTL_FLAG_STATUS_SENT)) == 0))
10788                 return (CTL_ACTION_OVERLAP);
10789
10790         /*
10791          * The initiator attempted to send multiple tagged commands with
10792          * the same ID.  (It's fine if different initiators have the same
10793          * tag ID.)
10794          *
10795          * Even if all of those conditions are true, we don't kill the I/O
10796          * if the command ahead of us has been aborted.  We won't end up
10797          * sending it to the FETD, and it's perfectly legal to resend a
10798          * command with the same tag number as long as the previous
10799          * instance of this tag number has been aborted somehow.
10800          */
10801         if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10802          && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10803          && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10804          && ((pending_io->io_hdr.nexus.targ_port ==
10805               ooa_io->io_hdr.nexus.targ_port)
10806           && (pending_io->io_hdr.nexus.initid ==
10807               ooa_io->io_hdr.nexus.initid))
10808          && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10809               CTL_FLAG_STATUS_SENT)) == 0))
10810                 return (CTL_ACTION_OVERLAP_TAG);
10811
10812         /*
10813          * If we get a head of queue tag, SAM-3 says that we should
10814          * immediately execute it.
10815          *
10816          * What happens if this command would normally block for some other
10817          * reason?  e.g. a request sense with a head of queue tag
10818          * immediately after a write.  Normally that would block, but this
10819          * will result in its getting executed immediately...
10820          *
10821          * We currently return "pass" instead of "skip", so we'll end up
10822          * going through the rest of the queue to check for overlapped tags.
10823          *
10824          * XXX KDM check for other types of blockage first??
10825          */
10826         if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10827                 return (CTL_ACTION_PASS);
10828
10829         /*
10830          * Ordered tags have to block until all items ahead of them
10831          * have completed.  If we get called with an ordered tag, we always
10832          * block, if something else is ahead of us in the queue.
10833          */
10834         if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10835                 return (CTL_ACTION_BLOCK);
10836
10837         /*
10838          * Simple tags get blocked until all head of queue and ordered tags
10839          * ahead of them have completed.  I'm lumping untagged commands in
10840          * with simple tags here.  XXX KDM is that the right thing to do?
10841          */
10842         if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10843           || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10844          && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10845           || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10846                 return (CTL_ACTION_BLOCK);
10847
10848         pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10849         KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10850             ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10851              __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10852              pending_io->scsiio.cdb[1], pending_io));
10853         ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10854         if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10855                 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10856         KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10857             ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10858              __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10859              ooa_io->scsiio.cdb[1], ooa_io));
10860
10861         serialize_row = ctl_serialize_table[ooa_entry->seridx];
10862
10863         switch (serialize_row[pending_entry->seridx]) {
10864         case CTL_SER_BLOCK:
10865                 return (CTL_ACTION_BLOCK);
10866         case CTL_SER_EXTENT:
10867                 return (ctl_extent_check(ooa_io, pending_io,
10868                     (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10869         case CTL_SER_EXTENTOPT:
10870                 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10871                     SCP_QUEUE_ALG_UNRESTRICTED)
10872                         return (ctl_extent_check(ooa_io, pending_io,
10873                             (lun->be_lun &&
10874                              lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10875                 return (CTL_ACTION_PASS);
10876         case CTL_SER_EXTENTSEQ:
10877                 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10878                         return (ctl_extent_check_seq(ooa_io, pending_io));
10879                 return (CTL_ACTION_PASS);
10880         case CTL_SER_PASS:
10881                 return (CTL_ACTION_PASS);
10882         case CTL_SER_BLOCKOPT:
10883                 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10884                     SCP_QUEUE_ALG_UNRESTRICTED)
10885                         return (CTL_ACTION_BLOCK);
10886                 return (CTL_ACTION_PASS);
10887         case CTL_SER_SKIP:
10888                 return (CTL_ACTION_SKIP);
10889         default:
10890                 panic("%s: Invalid serialization value %d for %d => %d",
10891                     __func__, serialize_row[pending_entry->seridx],
10892                     pending_entry->seridx, ooa_entry->seridx);
10893         }
10894
10895         return (CTL_ACTION_ERROR);
10896 }
10897
10898 /*
10899  * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10900  * Assumptions:
10901  * - pending_io is generally either incoming, or on the blocked queue
10902  * - starting I/O is the I/O we want to start the check with.
10903  */
10904 static ctl_action
10905 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10906               union ctl_io **starting_io)
10907 {
10908         union ctl_io *ooa_io;
10909         ctl_action action;
10910
10911         mtx_assert(&lun->lun_lock, MA_OWNED);
10912
10913         /*
10914          * Run back along the OOA queue, starting with the current
10915          * blocked I/O and going through every I/O before it on the
10916          * queue.  If starting_io is NULL, we'll just end up returning
10917          * CTL_ACTION_PASS.
10918          */
10919         for (ooa_io = *starting_io; ooa_io != NULL;
10920              ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10921              ooa_links)){
10922                 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10923                 if (action != CTL_ACTION_PASS) {
10924                         *starting_io = ooa_io;
10925                         return (action);
10926                 }
10927         }
10928
10929         *starting_io = NULL;
10930         return (CTL_ACTION_PASS);
10931 }
10932
10933 /*
10934  * Try to unblock the specified I/O.
10935  *
10936  * skip parameter allows explicitly skip present blocker of the I/O,
10937  * starting from the previous one on OOA queue.  It can be used when
10938  * we know for sure that the blocker I/O does no longer count.
10939  */
10940 static void
10941 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
10942 {
10943         struct ctl_softc *softc = lun->ctl_softc;
10944         union ctl_io *bio, *obio;
10945         const struct ctl_cmd_entry *entry;
10946         union ctl_ha_msg msg_info;
10947         ctl_action action;
10948
10949         mtx_assert(&lun->lun_lock, MA_OWNED);
10950
10951         if (io->io_hdr.blocker == NULL)
10952                 return;
10953
10954         obio = bio = io->io_hdr.blocker;
10955         if (skip)
10956                 bio = (union ctl_io *)TAILQ_PREV(&bio->io_hdr, ctl_ooaq,
10957                     ooa_links);
10958         action = ctl_check_ooa(lun, io, &bio);
10959         if (action == CTL_ACTION_BLOCK) {
10960                 /* Still blocked, but may be by different I/O now. */
10961                 if (bio != obio) {
10962                         TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
10963                             &io->io_hdr, blocked_links);
10964                         TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
10965                             &io->io_hdr, blocked_links);
10966                         io->io_hdr.blocker = bio;
10967                 }
10968                 return;
10969         }
10970
10971         /* No longer blocked, one way or another. */
10972         TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
10973         io->io_hdr.blocker = NULL;
10974
10975         switch (action) {
10976         case CTL_ACTION_OVERLAP:
10977                 ctl_set_overlapped_cmd(&io->scsiio);
10978                 goto error;
10979         case CTL_ACTION_OVERLAP_TAG:
10980                 ctl_set_overlapped_tag(&io->scsiio,
10981                     io->scsiio.tag_num & 0xff);
10982                 goto error;
10983         case CTL_ACTION_PASS:
10984         case CTL_ACTION_SKIP:
10985
10986                 /* Serializing commands from the other SC retire there. */
10987                 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
10988                     (softc->ha_mode != CTL_HA_MODE_XFER)) {
10989                         io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10990                         msg_info.hdr.original_sc = io->io_hdr.remote_io;
10991                         msg_info.hdr.serializing_sc = io;
10992                         msg_info.hdr.msg_type = CTL_MSG_R2R;
10993                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10994                             sizeof(msg_info.hdr), M_NOWAIT);
10995                         break;
10996                 }
10997
10998                 /*
10999                  * Check this I/O for LUN state changes that may have happened
11000                  * while this command was blocked. The LUN state may have been
11001                  * changed by a command ahead of us in the queue.
11002                  */
11003                 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11004                 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11005                         ctl_done(io);
11006                         break;
11007                 }
11008
11009                 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11010                 ctl_enqueue_rtr(io);
11011                 break;
11012         case CTL_ACTION_ERROR:
11013         default:
11014                 ctl_set_internal_failure(&io->scsiio,
11015                                          /*sks_valid*/ 0,
11016                                          /*retry_count*/ 0);
11017
11018 error:
11019                 /* Serializing commands from the other SC are done here. */
11020                 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11021                     (softc->ha_mode != CTL_HA_MODE_XFER)) {
11022                         ctl_try_unblock_others(lun, io, TRUE);
11023                         TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
11024
11025                         ctl_copy_sense_data_back(io, &msg_info);
11026                         msg_info.hdr.original_sc = io->io_hdr.remote_io;
11027                         msg_info.hdr.serializing_sc = NULL;
11028                         msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11029                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11030                             sizeof(msg_info.scsi), M_WAITOK);
11031                         ctl_free_io(io);
11032                         break;
11033                 }
11034
11035                 ctl_done(io);
11036                 break;
11037         }
11038 }
11039
11040 /*
11041  * Try to unblock I/Os blocked by the specified I/O.
11042  *
11043  * skip parameter allows explicitly skip the specified I/O as blocker,
11044  * starting from the previous one on the OOA queue.  It can be used when
11045  * we know for sure that the specified I/O does no longer count (done).
11046  * It has to be still on OOA queue though so that we know where to start.
11047  */
11048 static void
11049 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11050 {
11051         union ctl_io *io, *next_io;
11052
11053         mtx_assert(&lun->lun_lock, MA_OWNED);
11054
11055         for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11056              io != NULL; io = next_io) {
11057                 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11058
11059                 KASSERT(io->io_hdr.blocker != NULL,
11060                     ("I/O %p on blocked list without blocker", io));
11061                 ctl_try_unblock_io(lun, io, skip);
11062         }
11063         KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11064             ("blocked_queue is not empty after skipping %p", bio));
11065 }
11066
11067 /*
11068  * This routine (with one exception) checks LUN flags that can be set by
11069  * commands ahead of us in the OOA queue.  These flags have to be checked
11070  * when a command initially comes in, and when we pull a command off the
11071  * blocked queue and are preparing to execute it.  The reason we have to
11072  * check these flags for commands on the blocked queue is that the LUN
11073  * state may have been changed by a command ahead of us while we're on the
11074  * blocked queue.
11075  *
11076  * Ordering is somewhat important with these checks, so please pay
11077  * careful attention to the placement of any new checks.
11078  */
11079 static int
11080 ctl_scsiio_lun_check(struct ctl_lun *lun,
11081     const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11082 {
11083         struct ctl_softc *softc = lun->ctl_softc;
11084         int retval;
11085         uint32_t residx;
11086
11087         retval = 0;
11088
11089         mtx_assert(&lun->lun_lock, MA_OWNED);
11090
11091         /*
11092          * If this shelf is a secondary shelf controller, we may have to
11093          * reject some commands disallowed by HA mode and link state.
11094          */
11095         if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11096                 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11097                     (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11098                         ctl_set_lun_unavail(ctsio);
11099                         retval = 1;
11100                         goto bailout;
11101                 }
11102                 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11103                     (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11104                         ctl_set_lun_transit(ctsio);
11105                         retval = 1;
11106                         goto bailout;
11107                 }
11108                 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11109                     (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11110                         ctl_set_lun_standby(ctsio);
11111                         retval = 1;
11112                         goto bailout;
11113                 }
11114
11115                 /* The rest of checks are only done on executing side */
11116                 if (softc->ha_mode == CTL_HA_MODE_XFER)
11117                         goto bailout;
11118         }
11119
11120         if (entry->pattern & CTL_LUN_PAT_WRITE) {
11121                 if (lun->be_lun &&
11122                     lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11123                         ctl_set_hw_write_protected(ctsio);
11124                         retval = 1;
11125                         goto bailout;
11126                 }
11127                 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11128                         ctl_set_sense(ctsio, /*current_error*/ 1,
11129                             /*sense_key*/ SSD_KEY_DATA_PROTECT,
11130                             /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11131                         retval = 1;
11132                         goto bailout;
11133                 }
11134         }
11135
11136         /*
11137          * Check for a reservation conflict.  If this command isn't allowed
11138          * even on reserved LUNs, and if this initiator isn't the one who
11139          * reserved us, reject the command with a reservation conflict.
11140          */
11141         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11142         if ((lun->flags & CTL_LUN_RESERVED)
11143          && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11144                 if (lun->res_idx != residx) {
11145                         ctl_set_reservation_conflict(ctsio);
11146                         retval = 1;
11147                         goto bailout;
11148                 }
11149         }
11150
11151         if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11152             (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11153                 /* No reservation or command is allowed. */;
11154         } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11155             (lun->pr_res_type == SPR_TYPE_WR_EX ||
11156              lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11157              lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11158                 /* The command is allowed for Write Exclusive resv. */;
11159         } else {
11160                 /*
11161                  * if we aren't registered or it's a res holder type
11162                  * reservation and this isn't the res holder then set a
11163                  * conflict.
11164                  */
11165                 if (ctl_get_prkey(lun, residx) == 0 ||
11166                     (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11167                         ctl_set_reservation_conflict(ctsio);
11168                         retval = 1;
11169                         goto bailout;
11170                 }
11171         }
11172
11173         if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11174                 if (lun->flags & CTL_LUN_EJECTED)
11175                         ctl_set_lun_ejected(ctsio);
11176                 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11177                         if (lun->flags & CTL_LUN_REMOVABLE)
11178                                 ctl_set_lun_no_media(ctsio);
11179                         else
11180                                 ctl_set_lun_int_reqd(ctsio);
11181                 } else if (lun->flags & CTL_LUN_STOPPED)
11182                         ctl_set_lun_stopped(ctsio);
11183                 else
11184                         goto bailout;
11185                 retval = 1;
11186                 goto bailout;
11187         }
11188
11189 bailout:
11190         return (retval);
11191 }
11192
11193 static void
11194 ctl_failover_io(union ctl_io *io, int have_lock)
11195 {
11196         ctl_set_busy(&io->scsiio);
11197         ctl_done(io);
11198 }
11199
11200 static void
11201 ctl_failover_lun(union ctl_io *rio)
11202 {
11203         struct ctl_softc *softc = CTL_SOFTC(rio);
11204         struct ctl_lun *lun;
11205         struct ctl_io_hdr *io, *next_io;
11206         uint32_t targ_lun;
11207
11208         targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11209         CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11210
11211         /* Find and lock the LUN. */
11212         mtx_lock(&softc->ctl_lock);
11213         if (targ_lun > ctl_max_luns ||
11214             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11215                 mtx_unlock(&softc->ctl_lock);
11216                 return;
11217         }
11218         mtx_lock(&lun->lun_lock);
11219         mtx_unlock(&softc->ctl_lock);
11220         if (lun->flags & CTL_LUN_DISABLED) {
11221                 mtx_unlock(&lun->lun_lock);
11222                 return;
11223         }
11224
11225         if (softc->ha_mode == CTL_HA_MODE_XFER) {
11226                 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11227                         /* We are master */
11228                         if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11229                                 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11230                                         io->flags |= CTL_FLAG_ABORT;
11231                                         io->flags |= CTL_FLAG_FAILOVER;
11232                                         ctl_try_unblock_io(lun,
11233                                             (union ctl_io *)io, FALSE);
11234                                 } else { /* This can be only due to DATAMOVE */
11235                                         io->msg_type = CTL_MSG_DATAMOVE_DONE;
11236                                         io->flags &= ~CTL_FLAG_DMA_INPROG;
11237                                         io->flags |= CTL_FLAG_IO_ACTIVE;
11238                                         io->port_status = 31340;
11239                                         ctl_enqueue_isc((union ctl_io *)io);
11240                                 }
11241                         } else
11242                         /* We are slave */
11243                         if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11244                                 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11245                                 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11246                                         io->flags |= CTL_FLAG_FAILOVER;
11247                                 } else {
11248                                         ctl_set_busy(&((union ctl_io *)io)->
11249                                             scsiio);
11250                                         ctl_done((union ctl_io *)io);
11251                                 }
11252                         }
11253                 }
11254         } else { /* SERIALIZE modes */
11255                 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11256                         /* We are master */
11257                         if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11258                                 if (io->blocker != NULL) {
11259                                         TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11260                                             io, blocked_links);
11261                                         io->blocker = NULL;
11262                                 }
11263                                 ctl_try_unblock_others(lun, (union ctl_io *)io,
11264                                     TRUE);
11265                                 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11266                                 ctl_free_io((union ctl_io *)io);
11267                         } else
11268                         /* We are slave */
11269                         if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11270                                 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11271                                 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11272                                         ctl_set_busy(&((union ctl_io *)io)->
11273                                             scsiio);
11274                                         ctl_done((union ctl_io *)io);
11275                                 }
11276                         }
11277                 }
11278         }
11279         mtx_unlock(&lun->lun_lock);
11280 }
11281
11282 static int
11283 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11284 {
11285         struct ctl_lun *lun;
11286         const struct ctl_cmd_entry *entry;
11287         union ctl_io *bio;
11288         uint32_t initidx, targ_lun;
11289         int retval = 0;
11290
11291         lun = NULL;
11292         targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11293         if (targ_lun < ctl_max_luns)
11294                 lun = softc->ctl_luns[targ_lun];
11295         if (lun) {
11296                 /*
11297                  * If the LUN is invalid, pretend that it doesn't exist.
11298                  * It will go away as soon as all pending I/O has been
11299                  * completed.
11300                  */
11301                 mtx_lock(&lun->lun_lock);
11302                 if (lun->flags & CTL_LUN_DISABLED) {
11303                         mtx_unlock(&lun->lun_lock);
11304                         lun = NULL;
11305                 }
11306         }
11307         CTL_LUN(ctsio) = lun;
11308         if (lun) {
11309                 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11310
11311                 /*
11312                  * Every I/O goes into the OOA queue for a particular LUN,
11313                  * and stays there until completion.
11314                  */
11315 #ifdef CTL_TIME_IO
11316                 if (TAILQ_EMPTY(&lun->ooa_queue))
11317                         lun->idle_time += getsbinuptime() - lun->last_busy;
11318 #endif
11319                 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11320         }
11321
11322         /* Get command entry and return error if it is unsuppotyed. */
11323         entry = ctl_validate_command(ctsio);
11324         if (entry == NULL) {
11325                 if (lun)
11326                         mtx_unlock(&lun->lun_lock);
11327                 return (retval);
11328         }
11329
11330         ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11331         ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11332
11333         /*
11334          * Check to see whether we can send this command to LUNs that don't
11335          * exist.  This should pretty much only be the case for inquiry
11336          * and request sense.  Further checks, below, really require having
11337          * a LUN, so we can't really check the command anymore.  Just put
11338          * it on the rtr queue.
11339          */
11340         if (lun == NULL) {
11341                 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11342                         ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11343                         ctl_enqueue_rtr((union ctl_io *)ctsio);
11344                         return (retval);
11345                 }
11346
11347                 ctl_set_unsupported_lun(ctsio);
11348                 ctl_done((union ctl_io *)ctsio);
11349                 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11350                 return (retval);
11351         } else {
11352                 /*
11353                  * Make sure we support this particular command on this LUN.
11354                  * e.g., we don't support writes to the control LUN.
11355                  */
11356                 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11357                         mtx_unlock(&lun->lun_lock);
11358                         ctl_set_invalid_opcode(ctsio);
11359                         ctl_done((union ctl_io *)ctsio);
11360                         return (retval);
11361                 }
11362         }
11363
11364         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11365
11366         /*
11367          * If we've got a request sense, it'll clear the contingent
11368          * allegiance condition.  Otherwise, if we have a CA condition for
11369          * this initiator, clear it, because it sent down a command other
11370          * than request sense.
11371          */
11372         if (ctsio->cdb[0] != REQUEST_SENSE) {
11373                 struct scsi_sense_data *ps;
11374
11375                 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11376                 if (ps != NULL)
11377                         ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11378         }
11379
11380         /*
11381          * If the command has this flag set, it handles its own unit
11382          * attention reporting, we shouldn't do anything.  Otherwise we
11383          * check for any pending unit attentions, and send them back to the
11384          * initiator.  We only do this when a command initially comes in,
11385          * not when we pull it off the blocked queue.
11386          *
11387          * According to SAM-3, section 5.3.2, the order that things get
11388          * presented back to the host is basically unit attentions caused
11389          * by some sort of reset event, busy status, reservation conflicts
11390          * or task set full, and finally any other status.
11391          *
11392          * One issue here is that some of the unit attentions we report
11393          * don't fall into the "reset" category (e.g. "reported luns data
11394          * has changed").  So reporting it here, before the reservation
11395          * check, may be technically wrong.  I guess the only thing to do
11396          * would be to check for and report the reset events here, and then
11397          * check for the other unit attention types after we check for a
11398          * reservation conflict.
11399          *
11400          * XXX KDM need to fix this
11401          */
11402         if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11403                 ctl_ua_type ua_type;
11404                 u_int sense_len = 0;
11405
11406                 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11407                     &sense_len, SSD_TYPE_NONE);
11408                 if (ua_type != CTL_UA_NONE) {
11409                         mtx_unlock(&lun->lun_lock);
11410                         ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11411                         ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11412                         ctsio->sense_len = sense_len;
11413                         ctl_done((union ctl_io *)ctsio);
11414                         return (retval);
11415                 }
11416         }
11417
11418
11419         if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11420                 mtx_unlock(&lun->lun_lock);
11421                 ctl_done((union ctl_io *)ctsio);
11422                 return (retval);
11423         }
11424
11425         /*
11426          * XXX CHD this is where we want to send IO to other side if
11427          * this LUN is secondary on this SC. We will need to make a copy
11428          * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11429          * the copy we send as FROM_OTHER.
11430          * We also need to stuff the address of the original IO so we can
11431          * find it easily. Something similar will need be done on the other
11432          * side so when we are done we can find the copy.
11433          */
11434         if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11435             (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11436             (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11437                 union ctl_ha_msg msg_info;
11438                 int isc_retval;
11439
11440                 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11441                 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11442                 mtx_unlock(&lun->lun_lock);
11443
11444                 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11445                 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11446                 msg_info.hdr.serializing_sc = NULL;
11447                 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11448                 msg_info.scsi.tag_num = ctsio->tag_num;
11449                 msg_info.scsi.tag_type = ctsio->tag_type;
11450                 msg_info.scsi.cdb_len = ctsio->cdb_len;
11451                 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11452
11453                 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11454                     sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11455                     M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11456                         ctl_set_busy(ctsio);
11457                         ctl_done((union ctl_io *)ctsio);
11458                         return (retval);
11459                 }
11460                 return (retval);
11461         }
11462
11463         bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
11464         switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11465         case CTL_ACTION_BLOCK:
11466                 ctsio->io_hdr.blocker = bio;
11467                 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11468                                   blocked_links);
11469                 mtx_unlock(&lun->lun_lock);
11470                 return (retval);
11471         case CTL_ACTION_PASS:
11472         case CTL_ACTION_SKIP:
11473                 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11474                 mtx_unlock(&lun->lun_lock);
11475                 ctl_enqueue_rtr((union ctl_io *)ctsio);
11476                 break;
11477         case CTL_ACTION_OVERLAP:
11478                 mtx_unlock(&lun->lun_lock);
11479                 ctl_set_overlapped_cmd(ctsio);
11480                 ctl_done((union ctl_io *)ctsio);
11481                 break;
11482         case CTL_ACTION_OVERLAP_TAG:
11483                 mtx_unlock(&lun->lun_lock);
11484                 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11485                 ctl_done((union ctl_io *)ctsio);
11486                 break;
11487         case CTL_ACTION_ERROR:
11488         default:
11489                 mtx_unlock(&lun->lun_lock);
11490                 ctl_set_internal_failure(ctsio,
11491                                          /*sks_valid*/ 0,
11492                                          /*retry_count*/ 0);
11493                 ctl_done((union ctl_io *)ctsio);
11494                 break;
11495         }
11496         return (retval);
11497 }
11498
11499 const struct ctl_cmd_entry *
11500 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11501 {
11502         const struct ctl_cmd_entry *entry;
11503         int service_action;
11504
11505         entry = &ctl_cmd_table[ctsio->cdb[0]];
11506         if (sa)
11507                 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11508         if (entry->flags & CTL_CMD_FLAG_SA5) {
11509                 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11510                 entry = &((const struct ctl_cmd_entry *)
11511                     entry->execute)[service_action];
11512         }
11513         return (entry);
11514 }
11515
11516 const struct ctl_cmd_entry *
11517 ctl_validate_command(struct ctl_scsiio *ctsio)
11518 {
11519         const struct ctl_cmd_entry *entry;
11520         int i, sa;
11521         uint8_t diff;
11522
11523         entry = ctl_get_cmd_entry(ctsio, &sa);
11524         if (entry->execute == NULL) {
11525                 if (sa)
11526                         ctl_set_invalid_field(ctsio,
11527                                               /*sks_valid*/ 1,
11528                                               /*command*/ 1,
11529                                               /*field*/ 1,
11530                                               /*bit_valid*/ 1,
11531                                               /*bit*/ 4);
11532                 else
11533                         ctl_set_invalid_opcode(ctsio);
11534                 ctl_done((union ctl_io *)ctsio);
11535                 return (NULL);
11536         }
11537         KASSERT(entry->length > 0,
11538             ("Not defined length for command 0x%02x/0x%02x",
11539              ctsio->cdb[0], ctsio->cdb[1]));
11540         for (i = 1; i < entry->length; i++) {
11541                 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11542                 if (diff == 0)
11543                         continue;
11544                 ctl_set_invalid_field(ctsio,
11545                                       /*sks_valid*/ 1,
11546                                       /*command*/ 1,
11547                                       /*field*/ i,
11548                                       /*bit_valid*/ 1,
11549                                       /*bit*/ fls(diff) - 1);
11550                 ctl_done((union ctl_io *)ctsio);
11551                 return (NULL);
11552         }
11553         return (entry);
11554 }
11555
11556 static int
11557 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11558 {
11559
11560         switch (lun_type) {
11561         case T_DIRECT:
11562                 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11563                         return (0);
11564                 break;
11565         case T_PROCESSOR:
11566                 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11567                         return (0);
11568                 break;
11569         case T_CDROM:
11570                 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11571                         return (0);
11572                 break;
11573         default:
11574                 return (0);
11575         }
11576         return (1);
11577 }
11578
11579 static int
11580 ctl_scsiio(struct ctl_scsiio *ctsio)
11581 {
11582         int retval;
11583         const struct ctl_cmd_entry *entry;
11584
11585         retval = CTL_RETVAL_COMPLETE;
11586
11587         CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11588
11589         entry = ctl_get_cmd_entry(ctsio, NULL);
11590
11591         /*
11592          * If this I/O has been aborted, just send it straight to
11593          * ctl_done() without executing it.
11594          */
11595         if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11596                 ctl_done((union ctl_io *)ctsio);
11597                 goto bailout;
11598         }
11599
11600         /*
11601          * All the checks should have been handled by ctl_scsiio_precheck().
11602          * We should be clear now to just execute the I/O.
11603          */
11604         retval = entry->execute(ctsio);
11605
11606 bailout:
11607         return (retval);
11608 }
11609
11610 static int
11611 ctl_target_reset(union ctl_io *io)
11612 {
11613         struct ctl_softc *softc = CTL_SOFTC(io);
11614         struct ctl_port *port = CTL_PORT(io);
11615         struct ctl_lun *lun;
11616         uint32_t initidx;
11617         ctl_ua_type ua_type;
11618
11619         if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11620                 union ctl_ha_msg msg_info;
11621
11622                 msg_info.hdr.nexus = io->io_hdr.nexus;
11623                 msg_info.task.task_action = io->taskio.task_action;
11624                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11625                 msg_info.hdr.original_sc = NULL;
11626                 msg_info.hdr.serializing_sc = NULL;
11627                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11628                     sizeof(msg_info.task), M_WAITOK);
11629         }
11630
11631         initidx = ctl_get_initindex(&io->io_hdr.nexus);
11632         if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11633                 ua_type = CTL_UA_TARG_RESET;
11634         else
11635                 ua_type = CTL_UA_BUS_RESET;
11636         mtx_lock(&softc->ctl_lock);
11637         STAILQ_FOREACH(lun, &softc->lun_list, links) {
11638                 if (port != NULL &&
11639                     ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11640                         continue;
11641                 ctl_do_lun_reset(lun, initidx, ua_type);
11642         }
11643         mtx_unlock(&softc->ctl_lock);
11644         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11645         return (0);
11646 }
11647
11648 /*
11649  * The LUN should always be set.  The I/O is optional, and is used to
11650  * distinguish between I/Os sent by this initiator, and by other
11651  * initiators.  We set unit attention for initiators other than this one.
11652  * SAM-3 is vague on this point.  It does say that a unit attention should
11653  * be established for other initiators when a LUN is reset (see section
11654  * 5.7.3), but it doesn't specifically say that the unit attention should
11655  * be established for this particular initiator when a LUN is reset.  Here
11656  * is the relevant text, from SAM-3 rev 8:
11657  *
11658  * 5.7.2 When a SCSI initiator port aborts its own tasks
11659  *
11660  * When a SCSI initiator port causes its own task(s) to be aborted, no
11661  * notification that the task(s) have been aborted shall be returned to
11662  * the SCSI initiator port other than the completion response for the
11663  * command or task management function action that caused the task(s) to
11664  * be aborted and notification(s) associated with related effects of the
11665  * action (e.g., a reset unit attention condition).
11666  *
11667  * XXX KDM for now, we're setting unit attention for all initiators.
11668  */
11669 static void
11670 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11671 {
11672         union ctl_io *xio;
11673         int i;
11674
11675         mtx_lock(&lun->lun_lock);
11676         /* Abort tasks. */
11677         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11678              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11679                 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11680                 ctl_try_unblock_io(lun, xio, FALSE);
11681         }
11682         /* Clear CA. */
11683         for (i = 0; i < ctl_max_ports; i++) {
11684                 free(lun->pending_sense[i], M_CTL);
11685                 lun->pending_sense[i] = NULL;
11686         }
11687         /* Clear reservation. */
11688         lun->flags &= ~CTL_LUN_RESERVED;
11689         /* Clear prevent media removal. */
11690         if (lun->prevent) {
11691                 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11692                         ctl_clear_mask(lun->prevent, i);
11693                 lun->prevent_count = 0;
11694         }
11695         /* Clear TPC status */
11696         ctl_tpc_lun_clear(lun, -1);
11697         /* Establish UA. */
11698 #if 0
11699         ctl_est_ua_all(lun, initidx, ua_type);
11700 #else
11701         ctl_est_ua_all(lun, -1, ua_type);
11702 #endif
11703         mtx_unlock(&lun->lun_lock);
11704 }
11705
11706 static int
11707 ctl_lun_reset(union ctl_io *io)
11708 {
11709         struct ctl_softc *softc = CTL_SOFTC(io);
11710         struct ctl_lun *lun;
11711         uint32_t targ_lun, initidx;
11712
11713         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11714         initidx = ctl_get_initindex(&io->io_hdr.nexus);
11715         mtx_lock(&softc->ctl_lock);
11716         if (targ_lun >= ctl_max_luns ||
11717             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11718                 mtx_unlock(&softc->ctl_lock);
11719                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11720                 return (1);
11721         }
11722         ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11723         mtx_unlock(&softc->ctl_lock);
11724         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11725
11726         if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11727                 union ctl_ha_msg msg_info;
11728
11729                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11730                 msg_info.hdr.nexus = io->io_hdr.nexus;
11731                 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11732                 msg_info.hdr.original_sc = NULL;
11733                 msg_info.hdr.serializing_sc = NULL;
11734                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11735                     sizeof(msg_info.task), M_WAITOK);
11736         }
11737         return (0);
11738 }
11739
11740 static void
11741 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11742     int other_sc)
11743 {
11744         union ctl_io *xio;
11745
11746         mtx_assert(&lun->lun_lock, MA_OWNED);
11747
11748         /*
11749          * Run through the OOA queue and attempt to find the given I/O.
11750          * The target port, initiator ID, tag type and tag number have to
11751          * match the values that we got from the initiator.  If we have an
11752          * untagged command to abort, simply abort the first untagged command
11753          * we come to.  We only allow one untagged command at a time of course.
11754          */
11755         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11756              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11757
11758                 if ((targ_port == UINT32_MAX ||
11759                      targ_port == xio->io_hdr.nexus.targ_port) &&
11760                     (init_id == UINT32_MAX ||
11761                      init_id == xio->io_hdr.nexus.initid)) {
11762                         if (targ_port != xio->io_hdr.nexus.targ_port ||
11763                             init_id != xio->io_hdr.nexus.initid)
11764                                 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11765                         xio->io_hdr.flags |= CTL_FLAG_ABORT;
11766                         if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11767                                 union ctl_ha_msg msg_info;
11768
11769                                 msg_info.hdr.nexus = xio->io_hdr.nexus;
11770                                 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11771                                 msg_info.task.tag_num = xio->scsiio.tag_num;
11772                                 msg_info.task.tag_type = xio->scsiio.tag_type;
11773                                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11774                                 msg_info.hdr.original_sc = NULL;
11775                                 msg_info.hdr.serializing_sc = NULL;
11776                                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11777                                     sizeof(msg_info.task), M_NOWAIT);
11778                         }
11779                         ctl_try_unblock_io(lun, xio, FALSE);
11780                 }
11781         }
11782 }
11783
11784 static int
11785 ctl_abort_task_set(union ctl_io *io)
11786 {
11787         struct ctl_softc *softc = CTL_SOFTC(io);
11788         struct ctl_lun *lun;
11789         uint32_t targ_lun;
11790
11791         /*
11792          * Look up the LUN.
11793          */
11794         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11795         mtx_lock(&softc->ctl_lock);
11796         if (targ_lun >= ctl_max_luns ||
11797             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11798                 mtx_unlock(&softc->ctl_lock);
11799                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11800                 return (1);
11801         }
11802
11803         mtx_lock(&lun->lun_lock);
11804         mtx_unlock(&softc->ctl_lock);
11805         if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11806                 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11807                     io->io_hdr.nexus.initid,
11808                     (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11809         } else { /* CTL_TASK_CLEAR_TASK_SET */
11810                 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11811                     (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11812         }
11813         mtx_unlock(&lun->lun_lock);
11814         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11815         return (0);
11816 }
11817
11818 static void
11819 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11820     ctl_ua_type ua_type)
11821 {
11822         struct ctl_lun *lun;
11823         struct scsi_sense_data *ps;
11824         uint32_t p, i;
11825
11826         p = initidx / CTL_MAX_INIT_PER_PORT;
11827         i = initidx % CTL_MAX_INIT_PER_PORT;
11828         mtx_lock(&softc->ctl_lock);
11829         STAILQ_FOREACH(lun, &softc->lun_list, links) {
11830                 mtx_lock(&lun->lun_lock);
11831                 /* Abort tasks. */
11832                 ctl_abort_tasks_lun(lun, p, i, 1);
11833                 /* Clear CA. */
11834                 ps = lun->pending_sense[p];
11835                 if (ps != NULL)
11836                         ps[i].error_code = 0;
11837                 /* Clear reservation. */
11838                 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11839                         lun->flags &= ~CTL_LUN_RESERVED;
11840                 /* Clear prevent media removal. */
11841                 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11842                         ctl_clear_mask(lun->prevent, initidx);
11843                         lun->prevent_count--;
11844                 }
11845                 /* Clear TPC status */
11846                 ctl_tpc_lun_clear(lun, initidx);
11847                 /* Establish UA. */
11848                 ctl_est_ua(lun, initidx, ua_type);
11849                 mtx_unlock(&lun->lun_lock);
11850         }
11851         mtx_unlock(&softc->ctl_lock);
11852 }
11853
11854 static int
11855 ctl_i_t_nexus_reset(union ctl_io *io)
11856 {
11857         struct ctl_softc *softc = CTL_SOFTC(io);
11858         uint32_t initidx;
11859
11860         if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11861                 union ctl_ha_msg msg_info;
11862
11863                 msg_info.hdr.nexus = io->io_hdr.nexus;
11864                 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11865                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11866                 msg_info.hdr.original_sc = NULL;
11867                 msg_info.hdr.serializing_sc = NULL;
11868                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11869                     sizeof(msg_info.task), M_WAITOK);
11870         }
11871
11872         initidx = ctl_get_initindex(&io->io_hdr.nexus);
11873         ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11874         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11875         return (0);
11876 }
11877
11878 static int
11879 ctl_abort_task(union ctl_io *io)
11880 {
11881         struct ctl_softc *softc = CTL_SOFTC(io);
11882         union ctl_io *xio;
11883         struct ctl_lun *lun;
11884         uint32_t targ_lun;
11885
11886         /*
11887          * Look up the LUN.
11888          */
11889         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11890         mtx_lock(&softc->ctl_lock);
11891         if (targ_lun >= ctl_max_luns ||
11892             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11893                 mtx_unlock(&softc->ctl_lock);
11894                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11895                 return (1);
11896         }
11897
11898         mtx_lock(&lun->lun_lock);
11899         mtx_unlock(&softc->ctl_lock);
11900         /*
11901          * Run through the OOA queue and attempt to find the given I/O.
11902          * The target port, initiator ID, tag type and tag number have to
11903          * match the values that we got from the initiator.  If we have an
11904          * untagged command to abort, simply abort the first untagged command
11905          * we come to.  We only allow one untagged command at a time of course.
11906          */
11907         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11908              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11909
11910                 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11911                  || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11912                  || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11913                         continue;
11914
11915                 /*
11916                  * If the abort says that the task is untagged, the
11917                  * task in the queue must be untagged.  Otherwise,
11918                  * we just check to see whether the tag numbers
11919                  * match.  This is because the QLogic firmware
11920                  * doesn't pass back the tag type in an abort
11921                  * request.
11922                  */
11923 #if 0
11924                 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11925                   && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11926                  || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11927 #else
11928                 /*
11929                  * XXX KDM we've got problems with FC, because it
11930                  * doesn't send down a tag type with aborts.  So we
11931                  * can only really go by the tag number...
11932                  * This may cause problems with parallel SCSI.
11933                  * Need to figure that out!!
11934                  */
11935                 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11936 #endif
11937                         xio->io_hdr.flags |= CTL_FLAG_ABORT;
11938                         if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11939                             !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11940                                 union ctl_ha_msg msg_info;
11941
11942                                 msg_info.hdr.nexus = io->io_hdr.nexus;
11943                                 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11944                                 msg_info.task.tag_num = io->taskio.tag_num;
11945                                 msg_info.task.tag_type = io->taskio.tag_type;
11946                                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11947                                 msg_info.hdr.original_sc = NULL;
11948                                 msg_info.hdr.serializing_sc = NULL;
11949                                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11950                                     sizeof(msg_info.task), M_NOWAIT);
11951                         }
11952                         ctl_try_unblock_io(lun, xio, FALSE);
11953                 }
11954         }
11955         mtx_unlock(&lun->lun_lock);
11956         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11957         return (0);
11958 }
11959
11960 static int
11961 ctl_query_task(union ctl_io *io, int task_set)
11962 {
11963         struct ctl_softc *softc = CTL_SOFTC(io);
11964         union ctl_io *xio;
11965         struct ctl_lun *lun;
11966         int found = 0;
11967         uint32_t targ_lun;
11968
11969         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11970         mtx_lock(&softc->ctl_lock);
11971         if (targ_lun >= ctl_max_luns ||
11972             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11973                 mtx_unlock(&softc->ctl_lock);
11974                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11975                 return (1);
11976         }
11977         mtx_lock(&lun->lun_lock);
11978         mtx_unlock(&softc->ctl_lock);
11979         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11980              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11981
11982                 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11983                  || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11984                  || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11985                         continue;
11986
11987                 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
11988                         found = 1;
11989                         break;
11990                 }
11991         }
11992         mtx_unlock(&lun->lun_lock);
11993         if (found)
11994                 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11995         else
11996                 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11997         return (0);
11998 }
11999
12000 static int
12001 ctl_query_async_event(union ctl_io *io)
12002 {
12003         struct ctl_softc *softc = CTL_SOFTC(io);
12004         struct ctl_lun *lun;
12005         ctl_ua_type ua;
12006         uint32_t targ_lun, initidx;
12007
12008         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12009         mtx_lock(&softc->ctl_lock);
12010         if (targ_lun >= ctl_max_luns ||
12011             (lun = softc->ctl_luns[targ_lun]) == NULL) {
12012                 mtx_unlock(&softc->ctl_lock);
12013                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12014                 return (1);
12015         }
12016         mtx_lock(&lun->lun_lock);
12017         mtx_unlock(&softc->ctl_lock);
12018         initidx = ctl_get_initindex(&io->io_hdr.nexus);
12019         ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12020         mtx_unlock(&lun->lun_lock);
12021         if (ua != CTL_UA_NONE)
12022                 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12023         else
12024                 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12025         return (0);
12026 }
12027
12028 static void
12029 ctl_run_task(union ctl_io *io)
12030 {
12031         int retval = 1;
12032
12033         CTL_DEBUG_PRINT(("ctl_run_task\n"));
12034         KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12035             ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12036         io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12037         bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12038         switch (io->taskio.task_action) {
12039         case CTL_TASK_ABORT_TASK:
12040                 retval = ctl_abort_task(io);
12041                 break;
12042         case CTL_TASK_ABORT_TASK_SET:
12043         case CTL_TASK_CLEAR_TASK_SET:
12044                 retval = ctl_abort_task_set(io);
12045                 break;
12046         case CTL_TASK_CLEAR_ACA:
12047                 break;
12048         case CTL_TASK_I_T_NEXUS_RESET:
12049                 retval = ctl_i_t_nexus_reset(io);
12050                 break;
12051         case CTL_TASK_LUN_RESET:
12052                 retval = ctl_lun_reset(io);
12053                 break;
12054         case CTL_TASK_TARGET_RESET:
12055         case CTL_TASK_BUS_RESET:
12056                 retval = ctl_target_reset(io);
12057                 break;
12058         case CTL_TASK_PORT_LOGIN:
12059                 break;
12060         case CTL_TASK_PORT_LOGOUT:
12061                 break;
12062         case CTL_TASK_QUERY_TASK:
12063                 retval = ctl_query_task(io, 0);
12064                 break;
12065         case CTL_TASK_QUERY_TASK_SET:
12066                 retval = ctl_query_task(io, 1);
12067                 break;
12068         case CTL_TASK_QUERY_ASYNC_EVENT:
12069                 retval = ctl_query_async_event(io);
12070                 break;
12071         default:
12072                 printf("%s: got unknown task management event %d\n",
12073                        __func__, io->taskio.task_action);
12074                 break;
12075         }
12076         if (retval == 0)
12077                 io->io_hdr.status = CTL_SUCCESS;
12078         else
12079                 io->io_hdr.status = CTL_ERROR;
12080         ctl_done(io);
12081 }
12082
12083 /*
12084  * For HA operation.  Handle commands that come in from the other
12085  * controller.
12086  */
12087 static void
12088 ctl_handle_isc(union ctl_io *io)
12089 {
12090         struct ctl_softc *softc = CTL_SOFTC(io);
12091         struct ctl_lun *lun;
12092         const struct ctl_cmd_entry *entry;
12093         uint32_t targ_lun;
12094
12095         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12096         switch (io->io_hdr.msg_type) {
12097         case CTL_MSG_SERIALIZE:
12098                 ctl_serialize_other_sc_cmd(&io->scsiio);
12099                 break;
12100         case CTL_MSG_R2R:               /* Only used in SER_ONLY mode. */
12101                 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12102                 if (targ_lun >= ctl_max_luns ||
12103                     (lun = softc->ctl_luns[targ_lun]) == NULL) {
12104                         ctl_done(io);
12105                         break;
12106                 }
12107                 mtx_lock(&lun->lun_lock);
12108                 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12109                         mtx_unlock(&lun->lun_lock);
12110                         ctl_done(io);
12111                         break;
12112                 }
12113                 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12114                 mtx_unlock(&lun->lun_lock);
12115                 ctl_enqueue_rtr(io);
12116                 break;
12117         case CTL_MSG_FINISH_IO:
12118                 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12119                         ctl_done(io);
12120                         break;
12121                 }
12122                 if (targ_lun >= ctl_max_luns ||
12123                     (lun = softc->ctl_luns[targ_lun]) == NULL) {
12124                         ctl_free_io(io);
12125                         break;
12126                 }
12127                 mtx_lock(&lun->lun_lock);
12128                 ctl_try_unblock_others(lun, io, TRUE);
12129                 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12130                 mtx_unlock(&lun->lun_lock);
12131                 ctl_free_io(io);
12132                 break;
12133         case CTL_MSG_PERS_ACTION:
12134                 ctl_hndl_per_res_out_on_other_sc(io);
12135                 ctl_free_io(io);
12136                 break;
12137         case CTL_MSG_BAD_JUJU:
12138                 ctl_done(io);
12139                 break;
12140         case CTL_MSG_DATAMOVE:          /* Only used in XFER mode */
12141                 ctl_datamove_remote(io);
12142                 break;
12143         case CTL_MSG_DATAMOVE_DONE:     /* Only used in XFER mode */
12144                 io->scsiio.be_move_done(io);
12145                 break;
12146         case CTL_MSG_FAILOVER:
12147                 ctl_failover_lun(io);
12148                 ctl_free_io(io);
12149                 break;
12150         default:
12151                 printf("%s: Invalid message type %d\n",
12152                        __func__, io->io_hdr.msg_type);
12153                 ctl_free_io(io);
12154                 break;
12155         }
12156
12157 }
12158
12159
12160 /*
12161  * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12162  * there is no match.
12163  */
12164 static ctl_lun_error_pattern
12165 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12166 {
12167         const struct ctl_cmd_entry *entry;
12168         ctl_lun_error_pattern filtered_pattern, pattern;
12169
12170         pattern = desc->error_pattern;
12171
12172         /*
12173          * XXX KDM we need more data passed into this function to match a
12174          * custom pattern, and we actually need to implement custom pattern
12175          * matching.
12176          */
12177         if (pattern & CTL_LUN_PAT_CMD)
12178                 return (CTL_LUN_PAT_CMD);
12179
12180         if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12181                 return (CTL_LUN_PAT_ANY);
12182
12183         entry = ctl_get_cmd_entry(ctsio, NULL);
12184
12185         filtered_pattern = entry->pattern & pattern;
12186
12187         /*
12188          * If the user requested specific flags in the pattern (e.g.
12189          * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12190          * flags.
12191          *
12192          * If the user did not specify any flags, it doesn't matter whether
12193          * or not the command supports the flags.
12194          */
12195         if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12196              (pattern & ~CTL_LUN_PAT_MASK))
12197                 return (CTL_LUN_PAT_NONE);
12198
12199         /*
12200          * If the user asked for a range check, see if the requested LBA
12201          * range overlaps with this command's LBA range.
12202          */
12203         if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12204                 uint64_t lba1;
12205                 uint64_t len1;
12206                 ctl_action action;
12207                 int retval;
12208
12209                 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12210                 if (retval != 0)
12211                         return (CTL_LUN_PAT_NONE);
12212
12213                 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12214                                               desc->lba_range.len, FALSE);
12215                 /*
12216                  * A "pass" means that the LBA ranges don't overlap, so
12217                  * this doesn't match the user's range criteria.
12218                  */
12219                 if (action == CTL_ACTION_PASS)
12220                         return (CTL_LUN_PAT_NONE);
12221         }
12222
12223         return (filtered_pattern);
12224 }
12225
12226 static void
12227 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12228 {
12229         struct ctl_error_desc *desc, *desc2;
12230
12231         mtx_assert(&lun->lun_lock, MA_OWNED);
12232
12233         STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12234                 ctl_lun_error_pattern pattern;
12235                 /*
12236                  * Check to see whether this particular command matches
12237                  * the pattern in the descriptor.
12238                  */
12239                 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12240                 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12241                         continue;
12242
12243                 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12244                 case CTL_LUN_INJ_ABORTED:
12245                         ctl_set_aborted(&io->scsiio);
12246                         break;
12247                 case CTL_LUN_INJ_MEDIUM_ERR:
12248                         ctl_set_medium_error(&io->scsiio,
12249                             (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12250                              CTL_FLAG_DATA_OUT);
12251                         break;
12252                 case CTL_LUN_INJ_UA:
12253                         /* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET
12254                          * OCCURRED */
12255                         ctl_set_ua(&io->scsiio, 0x29, 0x00);
12256                         break;
12257                 case CTL_LUN_INJ_CUSTOM:
12258                         /*
12259                          * We're assuming the user knows what he is doing.
12260                          * Just copy the sense information without doing
12261                          * checks.
12262                          */
12263                         bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12264                               MIN(sizeof(desc->custom_sense),
12265                                   sizeof(io->scsiio.sense_data)));
12266                         io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12267                         io->scsiio.sense_len = SSD_FULL_SIZE;
12268                         io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12269                         break;
12270                 case CTL_LUN_INJ_NONE:
12271                 default:
12272                         /*
12273                          * If this is an error injection type we don't know
12274                          * about, clear the continuous flag (if it is set)
12275                          * so it will get deleted below.
12276                          */
12277                         desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12278                         break;
12279                 }
12280                 /*
12281                  * By default, each error injection action is a one-shot
12282                  */
12283                 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12284                         continue;
12285
12286                 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12287
12288                 free(desc, M_CTL);
12289         }
12290 }
12291
12292 #ifdef CTL_IO_DELAY
12293 static void
12294 ctl_datamove_timer_wakeup(void *arg)
12295 {
12296         union ctl_io *io;
12297
12298         io = (union ctl_io *)arg;
12299
12300         ctl_datamove(io);
12301 }
12302 #endif /* CTL_IO_DELAY */
12303
12304 void
12305 ctl_datamove(union ctl_io *io)
12306 {
12307         void (*fe_datamove)(union ctl_io *io);
12308
12309         mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12310
12311         CTL_DEBUG_PRINT(("ctl_datamove\n"));
12312
12313         /* No data transferred yet.  Frontend must update this when done. */
12314         io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12315
12316 #ifdef CTL_TIME_IO
12317         if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12318                 char str[256];
12319                 char path_str[64];
12320                 struct sbuf sb;
12321
12322                 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12323                 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12324
12325                 sbuf_cat(&sb, path_str);
12326                 switch (io->io_hdr.io_type) {
12327                 case CTL_IO_SCSI:
12328                         ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12329                         sbuf_printf(&sb, "\n");
12330                         sbuf_cat(&sb, path_str);
12331                         sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12332                                     io->scsiio.tag_num, io->scsiio.tag_type);
12333                         break;
12334                 case CTL_IO_TASK:
12335                         sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12336                                     "Tag Type: %d\n", io->taskio.task_action,
12337                                     io->taskio.tag_num, io->taskio.tag_type);
12338                         break;
12339                 default:
12340                         panic("%s: Invalid CTL I/O type %d\n",
12341                             __func__, io->io_hdr.io_type);
12342                 }
12343                 sbuf_cat(&sb, path_str);
12344                 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12345                             (intmax_t)time_uptime - io->io_hdr.start_time);
12346                 sbuf_finish(&sb);
12347                 printf("%s", sbuf_data(&sb));
12348         }
12349 #endif /* CTL_TIME_IO */
12350
12351 #ifdef CTL_IO_DELAY
12352         if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12353                 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12354         } else {
12355                 struct ctl_lun *lun;
12356
12357                 lun = CTL_LUN(io);
12358                 if ((lun != NULL)
12359                  && (lun->delay_info.datamove_delay > 0)) {
12360
12361                         callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12362                         io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12363                         callout_reset(&io->io_hdr.delay_callout,
12364                                       lun->delay_info.datamove_delay * hz,
12365                                       ctl_datamove_timer_wakeup, io);
12366                         if (lun->delay_info.datamove_type ==
12367                             CTL_DELAY_TYPE_ONESHOT)
12368                                 lun->delay_info.datamove_delay = 0;
12369                         return;
12370                 }
12371         }
12372 #endif
12373
12374         /*
12375          * This command has been aborted.  Set the port status, so we fail
12376          * the data move.
12377          */
12378         if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12379                 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12380                        io->scsiio.tag_num, io->io_hdr.nexus.initid,
12381                        io->io_hdr.nexus.targ_port,
12382                        io->io_hdr.nexus.targ_lun);
12383                 io->io_hdr.port_status = 31337;
12384                 /*
12385                  * Note that the backend, in this case, will get the
12386                  * callback in its context.  In other cases it may get
12387                  * called in the frontend's interrupt thread context.
12388                  */
12389                 io->scsiio.be_move_done(io);
12390                 return;
12391         }
12392
12393         /* Don't confuse frontend with zero length data move. */
12394         if (io->scsiio.kern_data_len == 0) {
12395                 io->scsiio.be_move_done(io);
12396                 return;
12397         }
12398
12399         fe_datamove = CTL_PORT(io)->fe_datamove;
12400         fe_datamove(io);
12401 }
12402
12403 static void
12404 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12405 {
12406         union ctl_ha_msg msg;
12407 #ifdef CTL_TIME_IO
12408         struct bintime cur_bt;
12409 #endif
12410
12411         memset(&msg, 0, sizeof(msg));
12412         msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12413         msg.hdr.original_sc = io;
12414         msg.hdr.serializing_sc = io->io_hdr.remote_io;
12415         msg.hdr.nexus = io->io_hdr.nexus;
12416         msg.hdr.status = io->io_hdr.status;
12417         msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12418         msg.scsi.tag_num = io->scsiio.tag_num;
12419         msg.scsi.tag_type = io->scsiio.tag_type;
12420         msg.scsi.scsi_status = io->scsiio.scsi_status;
12421         memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12422                io->scsiio.sense_len);
12423         msg.scsi.sense_len = io->scsiio.sense_len;
12424         msg.scsi.port_status = io->io_hdr.port_status;
12425         io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12426         if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12427                 ctl_failover_io(io, /*have_lock*/ have_lock);
12428                 return;
12429         }
12430         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12431             sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12432             msg.scsi.sense_len, M_WAITOK);
12433
12434 #ifdef CTL_TIME_IO
12435         getbinuptime(&cur_bt);
12436         bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12437         bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12438 #endif
12439         io->io_hdr.num_dmas++;
12440 }
12441
12442 /*
12443  * The DMA to the remote side is done, now we need to tell the other side
12444  * we're done so it can continue with its data movement.
12445  */
12446 static void
12447 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12448 {
12449         union ctl_io *io;
12450         uint32_t i;
12451
12452         io = rq->context;
12453
12454         if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12455                 printf("%s: ISC DMA write failed with error %d", __func__,
12456                        rq->ret);
12457                 ctl_set_internal_failure(&io->scsiio,
12458                                          /*sks_valid*/ 1,
12459                                          /*retry_count*/ rq->ret);
12460         }
12461
12462         ctl_dt_req_free(rq);
12463
12464         for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12465                 free(CTL_LSGLT(io)[i].addr, M_CTL);
12466         free(CTL_RSGL(io), M_CTL);
12467         CTL_RSGL(io) = NULL;
12468         CTL_LSGL(io) = NULL;
12469
12470         /*
12471          * The data is in local and remote memory, so now we need to send
12472          * status (good or back) back to the other side.
12473          */
12474         ctl_send_datamove_done(io, /*have_lock*/ 0);
12475 }
12476
12477 /*
12478  * We've moved the data from the host/controller into local memory.  Now we
12479  * need to push it over to the remote controller's memory.
12480  */
12481 static int
12482 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12483 {
12484         int retval;
12485
12486         retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12487                                           ctl_datamove_remote_write_cb);
12488         return (retval);
12489 }
12490
12491 static void
12492 ctl_datamove_remote_write(union ctl_io *io)
12493 {
12494         int retval;
12495         void (*fe_datamove)(union ctl_io *io);
12496
12497         /*
12498          * - Get the data from the host/HBA into local memory.
12499          * - DMA memory from the local controller to the remote controller.
12500          * - Send status back to the remote controller.
12501          */
12502
12503         retval = ctl_datamove_remote_sgl_setup(io);
12504         if (retval != 0)
12505                 return;
12506
12507         /* Switch the pointer over so the FETD knows what to do */
12508         io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12509
12510         /*
12511          * Use a custom move done callback, since we need to send completion
12512          * back to the other controller, not to the backend on this side.
12513          */
12514         io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12515
12516         fe_datamove = CTL_PORT(io)->fe_datamove;
12517         fe_datamove(io);
12518 }
12519
12520 static int
12521 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12522 {
12523         uint32_t i;
12524
12525         for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12526                 free(CTL_LSGLT(io)[i].addr, M_CTL);
12527         free(CTL_RSGL(io), M_CTL);
12528         CTL_RSGL(io) = NULL;
12529         CTL_LSGL(io) = NULL;
12530
12531         /*
12532          * The read is done, now we need to send status (good or bad) back
12533          * to the other side.
12534          */
12535         ctl_send_datamove_done(io, /*have_lock*/ 0);
12536
12537         return (0);
12538 }
12539
12540 static void
12541 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12542 {
12543         union ctl_io *io;
12544         void (*fe_datamove)(union ctl_io *io);
12545
12546         io = rq->context;
12547
12548         if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12549                 printf("%s: ISC DMA read failed with error %d\n", __func__,
12550                        rq->ret);
12551                 ctl_set_internal_failure(&io->scsiio,
12552                                          /*sks_valid*/ 1,
12553                                          /*retry_count*/ rq->ret);
12554         }
12555
12556         ctl_dt_req_free(rq);
12557
12558         /* Switch the pointer over so the FETD knows what to do */
12559         io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12560
12561         /*
12562          * Use a custom move done callback, since we need to send completion
12563          * back to the other controller, not to the backend on this side.
12564          */
12565         io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12566
12567         /* XXX KDM add checks like the ones in ctl_datamove? */
12568
12569         fe_datamove = CTL_PORT(io)->fe_datamove;
12570         fe_datamove(io);
12571 }
12572
12573 static int
12574 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12575 {
12576         struct ctl_sg_entry *local_sglist;
12577         uint32_t len_to_go;
12578         int retval;
12579         int i;
12580
12581         retval = 0;
12582         local_sglist = CTL_LSGL(io);
12583         len_to_go = io->scsiio.kern_data_len;
12584
12585         /*
12586          * The difficult thing here is that the size of the various
12587          * S/G segments may be different than the size from the
12588          * remote controller.  That'll make it harder when DMAing
12589          * the data back to the other side.
12590          */
12591         for (i = 0; len_to_go > 0; i++) {
12592                 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12593                 local_sglist[i].addr =
12594                     malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12595
12596                 len_to_go -= local_sglist[i].len;
12597         }
12598         /*
12599          * Reset the number of S/G entries accordingly.  The original
12600          * number of S/G entries is available in rem_sg_entries.
12601          */
12602         io->scsiio.kern_sg_entries = i;
12603
12604         return (retval);
12605 }
12606
12607 static int
12608 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12609                          ctl_ha_dt_cb callback)
12610 {
12611         struct ctl_ha_dt_req *rq;
12612         struct ctl_sg_entry *remote_sglist, *local_sglist;
12613         uint32_t local_used, remote_used, total_used;
12614         int i, j, isc_ret;
12615
12616         rq = ctl_dt_req_alloc();
12617
12618         /*
12619          * If we failed to allocate the request, and if the DMA didn't fail
12620          * anyway, set busy status.  This is just a resource allocation
12621          * failure.
12622          */
12623         if ((rq == NULL)
12624          && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12625              (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12626                 ctl_set_busy(&io->scsiio);
12627
12628         if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12629             (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12630
12631                 if (rq != NULL)
12632                         ctl_dt_req_free(rq);
12633
12634                 /*
12635                  * The data move failed.  We need to return status back
12636                  * to the other controller.  No point in trying to DMA
12637                  * data to the remote controller.
12638                  */
12639
12640                 ctl_send_datamove_done(io, /*have_lock*/ 0);
12641
12642                 return (1);
12643         }
12644
12645         local_sglist = CTL_LSGL(io);
12646         remote_sglist = CTL_RSGL(io);
12647         local_used = 0;
12648         remote_used = 0;
12649         total_used = 0;
12650
12651         /*
12652          * Pull/push the data over the wire from/to the other controller.
12653          * This takes into account the possibility that the local and
12654          * remote sglists may not be identical in terms of the size of
12655          * the elements and the number of elements.
12656          *
12657          * One fundamental assumption here is that the length allocated for
12658          * both the local and remote sglists is identical.  Otherwise, we've
12659          * essentially got a coding error of some sort.
12660          */
12661         isc_ret = CTL_HA_STATUS_SUCCESS;
12662         for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12663                 uint32_t cur_len;
12664                 uint8_t *tmp_ptr;
12665
12666                 rq->command = command;
12667                 rq->context = io;
12668
12669                 /*
12670                  * Both pointers should be aligned.  But it is possible
12671                  * that the allocation length is not.  They should both
12672                  * also have enough slack left over at the end, though,
12673                  * to round up to the next 8 byte boundary.
12674                  */
12675                 cur_len = MIN(local_sglist[i].len - local_used,
12676                               remote_sglist[j].len - remote_used);
12677                 rq->size = cur_len;
12678
12679                 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12680                 tmp_ptr += local_used;
12681
12682 #if 0
12683                 /* Use physical addresses when talking to ISC hardware */
12684                 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12685                         /* XXX KDM use busdma */
12686                         rq->local = vtophys(tmp_ptr);
12687                 } else
12688                         rq->local = tmp_ptr;
12689 #else
12690                 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12691                     ("HA does not support BUS_ADDR"));
12692                 rq->local = tmp_ptr;
12693 #endif
12694
12695                 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12696                 tmp_ptr += remote_used;
12697                 rq->remote = tmp_ptr;
12698
12699                 rq->callback = NULL;
12700
12701                 local_used += cur_len;
12702                 if (local_used >= local_sglist[i].len) {
12703                         i++;
12704                         local_used = 0;
12705                 }
12706
12707                 remote_used += cur_len;
12708                 if (remote_used >= remote_sglist[j].len) {
12709                         j++;
12710                         remote_used = 0;
12711                 }
12712                 total_used += cur_len;
12713
12714                 if (total_used >= io->scsiio.kern_data_len)
12715                         rq->callback = callback;
12716
12717                 isc_ret = ctl_dt_single(rq);
12718                 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12719                         break;
12720         }
12721         if (isc_ret != CTL_HA_STATUS_WAIT) {
12722                 rq->ret = isc_ret;
12723                 callback(rq);
12724         }
12725
12726         return (0);
12727 }
12728
12729 static void
12730 ctl_datamove_remote_read(union ctl_io *io)
12731 {
12732         int retval;
12733         uint32_t i;
12734
12735         /*
12736          * This will send an error to the other controller in the case of a
12737          * failure.
12738          */
12739         retval = ctl_datamove_remote_sgl_setup(io);
12740         if (retval != 0)
12741                 return;
12742
12743         retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12744                                           ctl_datamove_remote_read_cb);
12745         if (retval != 0) {
12746                 /*
12747                  * Make sure we free memory if there was an error..  The
12748                  * ctl_datamove_remote_xfer() function will send the
12749                  * datamove done message, or call the callback with an
12750                  * error if there is a problem.
12751                  */
12752                 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12753                         free(CTL_LSGLT(io)[i].addr, M_CTL);
12754                 free(CTL_RSGL(io), M_CTL);
12755                 CTL_RSGL(io) = NULL;
12756                 CTL_LSGL(io) = NULL;
12757         }
12758 }
12759
12760 /*
12761  * Process a datamove request from the other controller.  This is used for
12762  * XFER mode only, not SER_ONLY mode.  For writes, we DMA into local memory
12763  * first.  Once that is complete, the data gets DMAed into the remote
12764  * controller's memory.  For reads, we DMA from the remote controller's
12765  * memory into our memory first, and then move it out to the FETD.
12766  */
12767 static void
12768 ctl_datamove_remote(union ctl_io *io)
12769 {
12770
12771         mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12772
12773         if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12774                 ctl_failover_io(io, /*have_lock*/ 0);
12775                 return;
12776         }
12777
12778         /*
12779          * Note that we look for an aborted I/O here, but don't do some of
12780          * the other checks that ctl_datamove() normally does.
12781          * We don't need to run the datamove delay code, since that should
12782          * have been done if need be on the other controller.
12783          */
12784         if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12785                 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12786                        io->scsiio.tag_num, io->io_hdr.nexus.initid,
12787                        io->io_hdr.nexus.targ_port,
12788                        io->io_hdr.nexus.targ_lun);
12789                 io->io_hdr.port_status = 31338;
12790                 ctl_send_datamove_done(io, /*have_lock*/ 0);
12791                 return;
12792         }
12793
12794         if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12795                 ctl_datamove_remote_write(io);
12796         else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12797                 ctl_datamove_remote_read(io);
12798         else {
12799                 io->io_hdr.port_status = 31339;
12800                 ctl_send_datamove_done(io, /*have_lock*/ 0);
12801         }
12802 }
12803
12804 static void
12805 ctl_process_done(union ctl_io *io)
12806 {
12807         struct ctl_softc *softc = CTL_SOFTC(io);
12808         struct ctl_port *port = CTL_PORT(io);
12809         struct ctl_lun *lun = CTL_LUN(io);
12810         void (*fe_done)(union ctl_io *io);
12811         union ctl_ha_msg msg;
12812
12813         CTL_DEBUG_PRINT(("ctl_process_done\n"));
12814         fe_done = port->fe_done;
12815
12816 #ifdef CTL_TIME_IO
12817         if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12818                 char str[256];
12819                 char path_str[64];
12820                 struct sbuf sb;
12821
12822                 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12823                 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12824
12825                 sbuf_cat(&sb, path_str);
12826                 switch (io->io_hdr.io_type) {
12827                 case CTL_IO_SCSI:
12828                         ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12829                         sbuf_printf(&sb, "\n");
12830                         sbuf_cat(&sb, path_str);
12831                         sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12832                                     io->scsiio.tag_num, io->scsiio.tag_type);
12833                         break;
12834                 case CTL_IO_TASK:
12835                         sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12836                                     "Tag Type: %d\n", io->taskio.task_action,
12837                                     io->taskio.tag_num, io->taskio.tag_type);
12838                         break;
12839                 default:
12840                         panic("%s: Invalid CTL I/O type %d\n",
12841                             __func__, io->io_hdr.io_type);
12842                 }
12843                 sbuf_cat(&sb, path_str);
12844                 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12845                             (intmax_t)time_uptime - io->io_hdr.start_time);
12846                 sbuf_finish(&sb);
12847                 printf("%s", sbuf_data(&sb));
12848         }
12849 #endif /* CTL_TIME_IO */
12850
12851         switch (io->io_hdr.io_type) {
12852         case CTL_IO_SCSI:
12853                 break;
12854         case CTL_IO_TASK:
12855                 if (ctl_debug & CTL_DEBUG_INFO)
12856                         ctl_io_error_print(io, NULL);
12857                 fe_done(io);
12858                 return;
12859         default:
12860                 panic("%s: Invalid CTL I/O type %d\n",
12861                     __func__, io->io_hdr.io_type);
12862         }
12863
12864         if (lun == NULL) {
12865                 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12866                                  io->io_hdr.nexus.targ_mapped_lun));
12867                 goto bailout;
12868         }
12869
12870         mtx_lock(&lun->lun_lock);
12871
12872         /*
12873          * Check to see if we have any informational exception and status
12874          * of this command can be modified to report it in form of either
12875          * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12876          */
12877         if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12878             io->io_hdr.status == CTL_SUCCESS &&
12879             (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12880                 uint8_t mrie = lun->MODE_IE.mrie;
12881                 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12882                     (lun->MODE_VER.byte3 & SMS_VER_PER));
12883                 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12884                      mrie == SIEP_MRIE_REC_UNCOND ||
12885                      mrie == SIEP_MRIE_NO_SENSE) &&
12886                     (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12887                      CTL_CMD_FLAG_NO_SENSE) == 0) {
12888                         ctl_set_sense(&io->scsiio,
12889                               /*current_error*/ 1,
12890                               /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12891                                 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12892                               /*asc*/ lun->ie_asc,
12893                               /*ascq*/ lun->ie_ascq,
12894                               SSD_ELEM_NONE);
12895                         lun->ie_reported = 1;
12896                 }
12897         } else if (lun->ie_reported < 0)
12898                 lun->ie_reported = 0;
12899
12900         /*
12901          * Check to see if we have any errors to inject here.  We only
12902          * inject errors for commands that don't already have errors set.
12903          */
12904         if (!STAILQ_EMPTY(&lun->error_list) &&
12905             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12906             ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12907                 ctl_inject_error(lun, io);
12908
12909         /*
12910          * XXX KDM how do we treat commands that aren't completed
12911          * successfully?
12912          *
12913          * XXX KDM should we also track I/O latency?
12914          */
12915         if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12916             io->io_hdr.io_type == CTL_IO_SCSI) {
12917                 int type;
12918 #ifdef CTL_TIME_IO
12919                 struct bintime bt;
12920
12921                 getbinuptime(&bt);
12922                 bintime_sub(&bt, &io->io_hdr.start_bt);
12923 #endif
12924                 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12925                     CTL_FLAG_DATA_IN)
12926                         type = CTL_STATS_READ;
12927                 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12928                     CTL_FLAG_DATA_OUT)
12929                         type = CTL_STATS_WRITE;
12930                 else
12931                         type = CTL_STATS_NO_IO;
12932
12933                 lun->stats.bytes[type] += io->scsiio.kern_total_len;
12934                 lun->stats.operations[type] ++;
12935                 lun->stats.dmas[type] += io->io_hdr.num_dmas;
12936 #ifdef CTL_TIME_IO
12937                 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
12938                 bintime_add(&lun->stats.time[type], &bt);
12939 #endif
12940
12941                 mtx_lock(&port->port_lock);
12942                 port->stats.bytes[type] += io->scsiio.kern_total_len;
12943                 port->stats.operations[type] ++;
12944                 port->stats.dmas[type] += io->io_hdr.num_dmas;
12945 #ifdef CTL_TIME_IO
12946                 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
12947                 bintime_add(&port->stats.time[type], &bt);
12948 #endif
12949                 mtx_unlock(&port->port_lock);
12950         }
12951
12952         /*
12953          * Run through the blocked queue of this I/O and see if anything
12954          * can be unblocked, now that this I/O is done and will be removed.
12955          * We need to do it before removal to have OOA position to start.
12956          */
12957         ctl_try_unblock_others(lun, io, TRUE);
12958
12959         /*
12960          * Remove this from the OOA queue.
12961          */
12962         TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12963 #ifdef CTL_TIME_IO
12964         if (TAILQ_EMPTY(&lun->ooa_queue))
12965                 lun->last_busy = getsbinuptime();
12966 #endif
12967
12968         /*
12969          * If the LUN has been invalidated, free it if there is nothing
12970          * left on its OOA queue.
12971          */
12972         if ((lun->flags & CTL_LUN_INVALID)
12973          && TAILQ_EMPTY(&lun->ooa_queue)) {
12974                 mtx_unlock(&lun->lun_lock);
12975                 ctl_free_lun(lun);
12976         } else
12977                 mtx_unlock(&lun->lun_lock);
12978
12979 bailout:
12980
12981         /*
12982          * If this command has been aborted, make sure we set the status
12983          * properly.  The FETD is responsible for freeing the I/O and doing
12984          * whatever it needs to do to clean up its state.
12985          */
12986         if (io->io_hdr.flags & CTL_FLAG_ABORT)
12987                 ctl_set_task_aborted(&io->scsiio);
12988
12989         /*
12990          * If enabled, print command error status.
12991          */
12992         if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
12993             (ctl_debug & CTL_DEBUG_INFO) != 0)
12994                 ctl_io_error_print(io, NULL);
12995
12996         /*
12997          * Tell the FETD or the other shelf controller we're done with this
12998          * command.  Note that only SCSI commands get to this point.  Task
12999          * management commands are completed above.
13000          */
13001         if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13002             (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13003                 memset(&msg, 0, sizeof(msg));
13004                 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13005                 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13006                 msg.hdr.nexus = io->io_hdr.nexus;
13007                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13008                     sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13009                     M_WAITOK);
13010         }
13011
13012         fe_done(io);
13013 }
13014
13015 /*
13016  * Front end should call this if it doesn't do autosense.  When the request
13017  * sense comes back in from the initiator, we'll dequeue this and send it.
13018  */
13019 int
13020 ctl_queue_sense(union ctl_io *io)
13021 {
13022         struct ctl_softc *softc = CTL_SOFTC(io);
13023         struct ctl_port *port = CTL_PORT(io);
13024         struct ctl_lun *lun;
13025         struct scsi_sense_data *ps;
13026         uint32_t initidx, p, targ_lun;
13027
13028         CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13029
13030         targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13031
13032         /*
13033          * LUN lookup will likely move to the ctl_work_thread() once we
13034          * have our new queueing infrastructure (that doesn't put things on
13035          * a per-LUN queue initially).  That is so that we can handle
13036          * things like an INQUIRY to a LUN that we don't have enabled.  We
13037          * can't deal with that right now.
13038          * If we don't have a LUN for this, just toss the sense information.
13039          */
13040         mtx_lock(&softc->ctl_lock);
13041         if (targ_lun >= ctl_max_luns ||
13042             (lun = softc->ctl_luns[targ_lun]) == NULL) {
13043                 mtx_unlock(&softc->ctl_lock);
13044                 goto bailout;
13045         }
13046         mtx_lock(&lun->lun_lock);
13047         mtx_unlock(&softc->ctl_lock);
13048
13049         initidx = ctl_get_initindex(&io->io_hdr.nexus);
13050         p = initidx / CTL_MAX_INIT_PER_PORT;
13051         if (lun->pending_sense[p] == NULL) {
13052                 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13053                     M_CTL, M_NOWAIT | M_ZERO);
13054         }
13055         if ((ps = lun->pending_sense[p]) != NULL) {
13056                 ps += initidx % CTL_MAX_INIT_PER_PORT;
13057                 memset(ps, 0, sizeof(*ps));
13058                 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13059         }
13060         mtx_unlock(&lun->lun_lock);
13061
13062 bailout:
13063         ctl_free_io(io);
13064         return (CTL_RETVAL_COMPLETE);
13065 }
13066
13067 /*
13068  * Primary command inlet from frontend ports.  All SCSI and task I/O
13069  * requests must go through this function.
13070  */
13071 int
13072 ctl_queue(union ctl_io *io)
13073 {
13074         struct ctl_port *port = CTL_PORT(io);
13075
13076         CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13077
13078 #ifdef CTL_TIME_IO
13079         io->io_hdr.start_time = time_uptime;
13080         getbinuptime(&io->io_hdr.start_bt);
13081 #endif /* CTL_TIME_IO */
13082
13083         /* Map FE-specific LUN ID into global one. */
13084         io->io_hdr.nexus.targ_mapped_lun =
13085             ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13086
13087         switch (io->io_hdr.io_type) {
13088         case CTL_IO_SCSI:
13089         case CTL_IO_TASK:
13090                 if (ctl_debug & CTL_DEBUG_CDB)
13091                         ctl_io_print(io);
13092                 ctl_enqueue_incoming(io);
13093                 break;
13094         default:
13095                 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13096                 return (EINVAL);
13097         }
13098
13099         return (CTL_RETVAL_COMPLETE);
13100 }
13101
13102 #ifdef CTL_IO_DELAY
13103 static void
13104 ctl_done_timer_wakeup(void *arg)
13105 {
13106         union ctl_io *io;
13107
13108         io = (union ctl_io *)arg;
13109         ctl_done(io);
13110 }
13111 #endif /* CTL_IO_DELAY */
13112
13113 void
13114 ctl_serseq_done(union ctl_io *io)
13115 {
13116         struct ctl_lun *lun = CTL_LUN(io);;
13117
13118         if (lun->be_lun == NULL ||
13119             lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13120                 return;
13121         mtx_lock(&lun->lun_lock);
13122         io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13123         ctl_try_unblock_others(lun, io, FALSE);
13124         mtx_unlock(&lun->lun_lock);
13125 }
13126
13127 void
13128 ctl_done(union ctl_io *io)
13129 {
13130
13131         /*
13132          * Enable this to catch duplicate completion issues.
13133          */
13134 #if 0
13135         if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13136                 printf("%s: type %d msg %d cdb %x iptl: "
13137                        "%u:%u:%u tag 0x%04x "
13138                        "flag %#x status %x\n",
13139                         __func__,
13140                         io->io_hdr.io_type,
13141                         io->io_hdr.msg_type,
13142                         io->scsiio.cdb[0],
13143                         io->io_hdr.nexus.initid,
13144                         io->io_hdr.nexus.targ_port,
13145                         io->io_hdr.nexus.targ_lun,
13146                         (io->io_hdr.io_type ==
13147                         CTL_IO_TASK) ?
13148                         io->taskio.tag_num :
13149                         io->scsiio.tag_num,
13150                         io->io_hdr.flags,
13151                         io->io_hdr.status);
13152         } else
13153                 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13154 #endif
13155
13156         /*
13157          * This is an internal copy of an I/O, and should not go through
13158          * the normal done processing logic.
13159          */
13160         if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13161                 return;
13162
13163 #ifdef CTL_IO_DELAY
13164         if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13165                 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13166         } else {
13167                 struct ctl_lun *lun = CTL_LUN(io);
13168
13169                 if ((lun != NULL)
13170                  && (lun->delay_info.done_delay > 0)) {
13171
13172                         callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13173                         io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13174                         callout_reset(&io->io_hdr.delay_callout,
13175                                       lun->delay_info.done_delay * hz,
13176                                       ctl_done_timer_wakeup, io);
13177                         if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13178                                 lun->delay_info.done_delay = 0;
13179                         return;
13180                 }
13181         }
13182 #endif /* CTL_IO_DELAY */
13183
13184         ctl_enqueue_done(io);
13185 }
13186
13187 static void
13188 ctl_work_thread(void *arg)
13189 {
13190         struct ctl_thread *thr = (struct ctl_thread *)arg;
13191         struct ctl_softc *softc = thr->ctl_softc;
13192         union ctl_io *io;
13193         int retval;
13194
13195         CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13196         thread_lock(curthread);
13197         sched_prio(curthread, PUSER - 1);
13198         thread_unlock(curthread);
13199
13200         while (!softc->shutdown) {
13201                 /*
13202                  * We handle the queues in this order:
13203                  * - ISC
13204                  * - done queue (to free up resources, unblock other commands)
13205                  * - incoming queue
13206                  * - RtR queue
13207                  *
13208                  * If those queues are empty, we break out of the loop and
13209                  * go to sleep.
13210                  */
13211                 mtx_lock(&thr->queue_lock);
13212                 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13213                 if (io != NULL) {
13214                         STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13215                         mtx_unlock(&thr->queue_lock);
13216                         ctl_handle_isc(io);
13217                         continue;
13218                 }
13219                 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13220                 if (io != NULL) {
13221                         STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13222                         /* clear any blocked commands, call fe_done */
13223                         mtx_unlock(&thr->queue_lock);
13224                         ctl_process_done(io);
13225                         continue;
13226                 }
13227                 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13228                 if (io != NULL) {
13229                         STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13230                         mtx_unlock(&thr->queue_lock);
13231                         if (io->io_hdr.io_type == CTL_IO_TASK)
13232                                 ctl_run_task(io);
13233                         else
13234                                 ctl_scsiio_precheck(softc, &io->scsiio);
13235                         continue;
13236                 }
13237                 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13238                 if (io != NULL) {
13239                         STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13240                         mtx_unlock(&thr->queue_lock);
13241                         retval = ctl_scsiio(&io->scsiio);
13242                         if (retval != CTL_RETVAL_COMPLETE)
13243                                 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13244                         continue;
13245                 }
13246
13247                 /* Sleep until we have something to do. */
13248                 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13249         }
13250         thr->thread = NULL;
13251         kthread_exit();
13252 }
13253
13254 static void
13255 ctl_lun_thread(void *arg)
13256 {
13257         struct ctl_softc *softc = (struct ctl_softc *)arg;
13258         struct ctl_be_lun *be_lun;
13259
13260         CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13261         thread_lock(curthread);
13262         sched_prio(curthread, PUSER - 1);
13263         thread_unlock(curthread);
13264
13265         while (!softc->shutdown) {
13266                 mtx_lock(&softc->ctl_lock);
13267                 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13268                 if (be_lun != NULL) {
13269                         STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13270                         mtx_unlock(&softc->ctl_lock);
13271                         ctl_create_lun(be_lun);
13272                         continue;
13273                 }
13274
13275                 /* Sleep until we have something to do. */
13276                 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13277                     PDROP, "-", 0);
13278         }
13279         softc->lun_thread = NULL;
13280         kthread_exit();
13281 }
13282
13283 static void
13284 ctl_thresh_thread(void *arg)
13285 {
13286         struct ctl_softc *softc = (struct ctl_softc *)arg;
13287         struct ctl_lun *lun;
13288         struct ctl_logical_block_provisioning_page *page;
13289         const char *attr;
13290         union ctl_ha_msg msg;
13291         uint64_t thres, val;
13292         int i, e, set;
13293
13294         CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13295         thread_lock(curthread);
13296         sched_prio(curthread, PUSER - 1);
13297         thread_unlock(curthread);
13298
13299         while (!softc->shutdown) {
13300                 mtx_lock(&softc->ctl_lock);
13301                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13302                         if ((lun->flags & CTL_LUN_DISABLED) ||
13303                             (lun->flags & CTL_LUN_NO_MEDIA) ||
13304                             lun->backend->lun_attr == NULL)
13305                                 continue;
13306                         if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13307                             softc->ha_mode == CTL_HA_MODE_XFER)
13308                                 continue;
13309                         if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13310                                 continue;
13311                         e = 0;
13312                         page = &lun->MODE_LBP;
13313                         for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13314                                 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13315                                         continue;
13316                                 thres = scsi_4btoul(page->descr[i].count);
13317                                 thres <<= CTL_LBP_EXPONENT;
13318                                 switch (page->descr[i].resource) {
13319                                 case 0x01:
13320                                         attr = "blocksavail";
13321                                         break;
13322                                 case 0x02:
13323                                         attr = "blocksused";
13324                                         break;
13325                                 case 0xf1:
13326                                         attr = "poolblocksavail";
13327                                         break;
13328                                 case 0xf2:
13329                                         attr = "poolblocksused";
13330                                         break;
13331                                 default:
13332                                         continue;
13333                                 }
13334                                 mtx_unlock(&softc->ctl_lock); // XXX
13335                                 val = lun->backend->lun_attr(
13336                                     lun->be_lun->be_lun, attr);
13337                                 mtx_lock(&softc->ctl_lock);
13338                                 if (val == UINT64_MAX)
13339                                         continue;
13340                                 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13341                                     == SLBPPD_ARMING_INC)
13342                                         e = (val >= thres);
13343                                 else
13344                                         e = (val <= thres);
13345                                 if (e)
13346                                         break;
13347                         }
13348                         mtx_lock(&lun->lun_lock);
13349                         if (e) {
13350                                 scsi_u64to8b((uint8_t *)&page->descr[i] -
13351                                     (uint8_t *)page, lun->ua_tpt_info);
13352                                 if (lun->lasttpt == 0 ||
13353                                     time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13354                                         lun->lasttpt = time_uptime;
13355                                         ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13356                                         set = 1;
13357                                 } else
13358                                         set = 0;
13359                         } else {
13360                                 lun->lasttpt = 0;
13361                                 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13362                                 set = -1;
13363                         }
13364                         mtx_unlock(&lun->lun_lock);
13365                         if (set != 0 &&
13366                             lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13367                                 /* Send msg to other side. */
13368                                 bzero(&msg.ua, sizeof(msg.ua));
13369                                 msg.hdr.msg_type = CTL_MSG_UA;
13370                                 msg.hdr.nexus.initid = -1;
13371                                 msg.hdr.nexus.targ_port = -1;
13372                                 msg.hdr.nexus.targ_lun = lun->lun;
13373                                 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13374                                 msg.ua.ua_all = 1;
13375                                 msg.ua.ua_set = (set > 0);
13376                                 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13377                                 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13378                                 mtx_unlock(&softc->ctl_lock); // XXX
13379                                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13380                                     sizeof(msg.ua), M_WAITOK);
13381                                 mtx_lock(&softc->ctl_lock);
13382                         }
13383                 }
13384                 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13385                     PDROP, "-", CTL_LBP_PERIOD * hz);
13386         }
13387         softc->thresh_thread = NULL;
13388         kthread_exit();
13389 }
13390
13391 static void
13392 ctl_enqueue_incoming(union ctl_io *io)
13393 {
13394         struct ctl_softc *softc = CTL_SOFTC(io);
13395         struct ctl_thread *thr;
13396         u_int idx;
13397
13398         idx = (io->io_hdr.nexus.targ_port * 127 +
13399                io->io_hdr.nexus.initid) % worker_threads;
13400         thr = &softc->threads[idx];
13401         mtx_lock(&thr->queue_lock);
13402         STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13403         mtx_unlock(&thr->queue_lock);
13404         wakeup(thr);
13405 }
13406
13407 static void
13408 ctl_enqueue_rtr(union ctl_io *io)
13409 {
13410         struct ctl_softc *softc = CTL_SOFTC(io);
13411         struct ctl_thread *thr;
13412
13413         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13414         mtx_lock(&thr->queue_lock);
13415         STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13416         mtx_unlock(&thr->queue_lock);
13417         wakeup(thr);
13418 }
13419
13420 static void
13421 ctl_enqueue_done(union ctl_io *io)
13422 {
13423         struct ctl_softc *softc = CTL_SOFTC(io);
13424         struct ctl_thread *thr;
13425
13426         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13427         mtx_lock(&thr->queue_lock);
13428         STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13429         mtx_unlock(&thr->queue_lock);
13430         wakeup(thr);
13431 }
13432
13433 static void
13434 ctl_enqueue_isc(union ctl_io *io)
13435 {
13436         struct ctl_softc *softc = CTL_SOFTC(io);
13437         struct ctl_thread *thr;
13438
13439         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13440         mtx_lock(&thr->queue_lock);
13441         STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13442         mtx_unlock(&thr->queue_lock);
13443         wakeup(thr);
13444 }
13445
13446 /*
13447  *  vim: ts=8
13448  */