]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/ctl/ctl.c
MFC r287784: Implement data/status aggregation for secondary HA node.
[FreeBSD/stable/10.git] / sys / cam / ctl / ctl.c
1 /*-
2  * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3  * Copyright (c) 2012 The FreeBSD Foundation
4  * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Edward Tomasz Napierala
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17  *    substantially similar to the "NO WARRANTY" disclaimer below
18  *    ("Disclaimer") and any redistribution must be conditioned upon
19  *    including a substantially similar Disclaimer requirement for further
20  *    binary redistribution.
21  *
22  * NO WARRANTY
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * $Id$
36  */
37 /*
38  * CAM Target Layer, a SCSI device emulation subsystem.
39  *
40  * Author: Ken Merry <ken@FreeBSD.org>
41  */
42
43 #define _CTL_C
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
54 #include <sys/bio.h>
55 #include <sys/fcntl.h>
56 #include <sys/lock.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
61 #include <sys/conf.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
64 #include <sys/sbuf.h>
65 #include <sys/smp.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
68 #include <vm/uma.h>
69
70 #include <cam/cam.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_da.h>
73 #include <cam/ctl/ctl_io.h>
74 #include <cam/ctl/ctl.h>
75 #include <cam/ctl/ctl_frontend.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
84
85 struct ctl_softc *control_softc = NULL;
86
87 /*
88  * Template mode pages.
89  */
90
91 /*
92  * Note that these are default values only.  The actual values will be
93  * filled in when the user does a mode sense.
94  */
95 const static struct copan_debugconf_subpage debugconf_page_default = {
96         DBGCNF_PAGE_CODE | SMPH_SPF,    /* page_code */
97         DBGCNF_SUBPAGE_CODE,            /* subpage */
98         {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
99          (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
100         DBGCNF_VERSION,                 /* page_version */
101         {CTL_TIME_IO_DEFAULT_SECS>>8,
102          CTL_TIME_IO_DEFAULT_SECS>>0},  /* ctl_time_io_secs */
103 };
104
105 const static struct copan_debugconf_subpage debugconf_page_changeable = {
106         DBGCNF_PAGE_CODE | SMPH_SPF,    /* page_code */
107         DBGCNF_SUBPAGE_CODE,            /* subpage */
108         {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
109          (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
110         0,                              /* page_version */
111         {0xff,0xff},                    /* ctl_time_io_secs */
112 };
113
114 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
115         /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
116         /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
117         /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
118         /*read_retry_count*/0,
119         /*correction_span*/0,
120         /*head_offset_count*/0,
121         /*data_strobe_offset_cnt*/0,
122         /*byte8*/SMS_RWER_LBPERE,
123         /*write_retry_count*/0,
124         /*reserved2*/0,
125         /*recovery_time_limit*/{0, 0},
126 };
127
128 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
129         /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
130         /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
131         /*byte3*/0,
132         /*read_retry_count*/0,
133         /*correction_span*/0,
134         /*head_offset_count*/0,
135         /*data_strobe_offset_cnt*/0,
136         /*byte8*/0,
137         /*write_retry_count*/0,
138         /*reserved2*/0,
139         /*recovery_time_limit*/{0, 0},
140 };
141
142 const static struct scsi_format_page format_page_default = {
143         /*page_code*/SMS_FORMAT_DEVICE_PAGE,
144         /*page_length*/sizeof(struct scsi_format_page) - 2,
145         /*tracks_per_zone*/ {0, 0},
146         /*alt_sectors_per_zone*/ {0, 0},
147         /*alt_tracks_per_zone*/ {0, 0},
148         /*alt_tracks_per_lun*/ {0, 0},
149         /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
150                                 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
151         /*bytes_per_sector*/ {0, 0},
152         /*interleave*/ {0, 0},
153         /*track_skew*/ {0, 0},
154         /*cylinder_skew*/ {0, 0},
155         /*flags*/ SFP_HSEC,
156         /*reserved*/ {0, 0, 0}
157 };
158
159 const static struct scsi_format_page format_page_changeable = {
160         /*page_code*/SMS_FORMAT_DEVICE_PAGE,
161         /*page_length*/sizeof(struct scsi_format_page) - 2,
162         /*tracks_per_zone*/ {0, 0},
163         /*alt_sectors_per_zone*/ {0, 0},
164         /*alt_tracks_per_zone*/ {0, 0},
165         /*alt_tracks_per_lun*/ {0, 0},
166         /*sectors_per_track*/ {0, 0},
167         /*bytes_per_sector*/ {0, 0},
168         /*interleave*/ {0, 0},
169         /*track_skew*/ {0, 0},
170         /*cylinder_skew*/ {0, 0},
171         /*flags*/ 0,
172         /*reserved*/ {0, 0, 0}
173 };
174
175 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
176         /*page_code*/SMS_RIGID_DISK_PAGE,
177         /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
178         /*cylinders*/ {0, 0, 0},
179         /*heads*/ CTL_DEFAULT_HEADS,
180         /*start_write_precomp*/ {0, 0, 0},
181         /*start_reduced_current*/ {0, 0, 0},
182         /*step_rate*/ {0, 0},
183         /*landing_zone_cylinder*/ {0, 0, 0},
184         /*rpl*/ SRDP_RPL_DISABLED,
185         /*rotational_offset*/ 0,
186         /*reserved1*/ 0,
187         /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
188                            CTL_DEFAULT_ROTATION_RATE & 0xff},
189         /*reserved2*/ {0, 0}
190 };
191
192 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
193         /*page_code*/SMS_RIGID_DISK_PAGE,
194         /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
195         /*cylinders*/ {0, 0, 0},
196         /*heads*/ 0,
197         /*start_write_precomp*/ {0, 0, 0},
198         /*start_reduced_current*/ {0, 0, 0},
199         /*step_rate*/ {0, 0},
200         /*landing_zone_cylinder*/ {0, 0, 0},
201         /*rpl*/ 0,
202         /*rotational_offset*/ 0,
203         /*reserved1*/ 0,
204         /*rotation_rate*/ {0, 0},
205         /*reserved2*/ {0, 0}
206 };
207
208 const static struct scsi_caching_page caching_page_default = {
209         /*page_code*/SMS_CACHING_PAGE,
210         /*page_length*/sizeof(struct scsi_caching_page) - 2,
211         /*flags1*/ SCP_DISC | SCP_WCE,
212         /*ret_priority*/ 0,
213         /*disable_pf_transfer_len*/ {0xff, 0xff},
214         /*min_prefetch*/ {0, 0},
215         /*max_prefetch*/ {0xff, 0xff},
216         /*max_pf_ceiling*/ {0xff, 0xff},
217         /*flags2*/ 0,
218         /*cache_segments*/ 0,
219         /*cache_seg_size*/ {0, 0},
220         /*reserved*/ 0,
221         /*non_cache_seg_size*/ {0, 0, 0}
222 };
223
224 const static struct scsi_caching_page caching_page_changeable = {
225         /*page_code*/SMS_CACHING_PAGE,
226         /*page_length*/sizeof(struct scsi_caching_page) - 2,
227         /*flags1*/ SCP_WCE | SCP_RCD,
228         /*ret_priority*/ 0,
229         /*disable_pf_transfer_len*/ {0, 0},
230         /*min_prefetch*/ {0, 0},
231         /*max_prefetch*/ {0, 0},
232         /*max_pf_ceiling*/ {0, 0},
233         /*flags2*/ 0,
234         /*cache_segments*/ 0,
235         /*cache_seg_size*/ {0, 0},
236         /*reserved*/ 0,
237         /*non_cache_seg_size*/ {0, 0, 0}
238 };
239
240 const static struct scsi_control_page control_page_default = {
241         /*page_code*/SMS_CONTROL_MODE_PAGE,
242         /*page_length*/sizeof(struct scsi_control_page) - 2,
243         /*rlec*/0,
244         /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
245         /*eca_and_aen*/0,
246         /*flags4*/SCP_TAS,
247         /*aen_holdoff_period*/{0, 0},
248         /*busy_timeout_period*/{0, 0},
249         /*extended_selftest_completion_time*/{0, 0}
250 };
251
252 const static struct scsi_control_page control_page_changeable = {
253         /*page_code*/SMS_CONTROL_MODE_PAGE,
254         /*page_length*/sizeof(struct scsi_control_page) - 2,
255         /*rlec*/SCP_DSENSE,
256         /*queue_flags*/SCP_QUEUE_ALG_MASK,
257         /*eca_and_aen*/SCP_SWP,
258         /*flags4*/0,
259         /*aen_holdoff_period*/{0, 0},
260         /*busy_timeout_period*/{0, 0},
261         /*extended_selftest_completion_time*/{0, 0}
262 };
263
264 const static struct scsi_info_exceptions_page ie_page_default = {
265         /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
266         /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
267         /*info_flags*/SIEP_FLAGS_DEXCPT,
268         /*mrie*/0,
269         /*interval_timer*/{0, 0, 0, 0},
270         /*report_count*/{0, 0, 0, 0}
271 };
272
273 const static struct scsi_info_exceptions_page ie_page_changeable = {
274         /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
275         /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
276         /*info_flags*/0,
277         /*mrie*/0,
278         /*interval_timer*/{0, 0, 0, 0},
279         /*report_count*/{0, 0, 0, 0}
280 };
281
282 #define CTL_LBPM_LEN    (sizeof(struct ctl_logical_block_provisioning_page) - 4)
283
284 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
285         /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
286         /*subpage_code*/0x02,
287         /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
288         /*flags*/0,
289         /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
290         /*descr*/{}},
291         {{/*flags*/0,
292           /*resource*/0x01,
293           /*reserved*/{0, 0},
294           /*count*/{0, 0, 0, 0}},
295          {/*flags*/0,
296           /*resource*/0x02,
297           /*reserved*/{0, 0},
298           /*count*/{0, 0, 0, 0}},
299          {/*flags*/0,
300           /*resource*/0xf1,
301           /*reserved*/{0, 0},
302           /*count*/{0, 0, 0, 0}},
303          {/*flags*/0,
304           /*resource*/0xf2,
305           /*reserved*/{0, 0},
306           /*count*/{0, 0, 0, 0}}
307         }
308 };
309
310 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
311         /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
312         /*subpage_code*/0x02,
313         /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
314         /*flags*/0,
315         /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
316         /*descr*/{}},
317         {{/*flags*/0,
318           /*resource*/0,
319           /*reserved*/{0, 0},
320           /*count*/{0, 0, 0, 0}},
321          {/*flags*/0,
322           /*resource*/0,
323           /*reserved*/{0, 0},
324           /*count*/{0, 0, 0, 0}},
325          {/*flags*/0,
326           /*resource*/0,
327           /*reserved*/{0, 0},
328           /*count*/{0, 0, 0, 0}},
329          {/*flags*/0,
330           /*resource*/0,
331           /*reserved*/{0, 0},
332           /*count*/{0, 0, 0, 0}}
333         }
334 };
335
336 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
337 static int worker_threads = -1;
338 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
339 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
340     &worker_threads, 1, "Number of worker threads");
341 static int ctl_debug = CTL_DEBUG_NONE;
342 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
343 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
344     &ctl_debug, 0, "Enabled debug flags");
345
346 /*
347  * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
348  * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
349  * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
350  * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
351  */
352 #define SCSI_EVPD_NUM_SUPPORTED_PAGES   10
353
354 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
355                                   int param);
356 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
357 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
358 static int ctl_init(void);
359 void ctl_shutdown(void);
360 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
361 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
362 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
363 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
364                               struct ctl_ooa *ooa_hdr,
365                               struct ctl_ooa_entry *kern_entries);
366 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
367                      struct thread *td);
368 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
369                          struct ctl_be_lun *be_lun);
370 static int ctl_free_lun(struct ctl_lun *lun);
371 static void ctl_create_lun(struct ctl_be_lun *be_lun);
372 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
373
374 static int ctl_do_mode_select(union ctl_io *io);
375 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
376                            uint64_t res_key, uint64_t sa_res_key,
377                            uint8_t type, uint32_t residx,
378                            struct ctl_scsiio *ctsio,
379                            struct scsi_per_res_out *cdb,
380                            struct scsi_per_res_out_parms* param);
381 static void ctl_pro_preempt_other(struct ctl_lun *lun,
382                                   union ctl_ha_msg *msg);
383 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
384 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
385 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
386 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
387 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
388 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
389 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
390                                          int alloc_len);
391 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
392                                          int alloc_len);
393 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
394 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
395 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
396 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
397 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
398 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
399     bool seq);
400 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
401 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
402     union ctl_io *pending_io, union ctl_io *ooa_io);
403 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
404                                 union ctl_io *starting_io);
405 static int ctl_check_blocked(struct ctl_lun *lun);
406 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
407                                 const struct ctl_cmd_entry *entry,
408                                 struct ctl_scsiio *ctsio);
409 static void ctl_failover_lun(struct ctl_lun *lun);
410 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
411                                struct ctl_scsiio *ctsio);
412 static int ctl_scsiio(struct ctl_scsiio *ctsio);
413
414 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
415 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
416                             ctl_ua_type ua_type);
417 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
418                          ctl_ua_type ua_type);
419 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
420 static int ctl_abort_task(union ctl_io *io);
421 static int ctl_abort_task_set(union ctl_io *io);
422 static int ctl_query_task(union ctl_io *io, int task_set);
423 static int ctl_i_t_nexus_reset(union ctl_io *io);
424 static int ctl_query_async_event(union ctl_io *io);
425 static void ctl_run_task(union ctl_io *io);
426 #ifdef CTL_IO_DELAY
427 static void ctl_datamove_timer_wakeup(void *arg);
428 static void ctl_done_timer_wakeup(void *arg);
429 #endif /* CTL_IO_DELAY */
430
431 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
432 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
433 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
434 static void ctl_datamove_remote_write(union ctl_io *io);
435 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
436 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
437 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
438 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
439                                     ctl_ha_dt_cb callback);
440 static void ctl_datamove_remote_read(union ctl_io *io);
441 static void ctl_datamove_remote(union ctl_io *io);
442 static int ctl_process_done(union ctl_io *io);
443 static void ctl_lun_thread(void *arg);
444 static void ctl_thresh_thread(void *arg);
445 static void ctl_work_thread(void *arg);
446 static void ctl_enqueue_incoming(union ctl_io *io);
447 static void ctl_enqueue_rtr(union ctl_io *io);
448 static void ctl_enqueue_done(union ctl_io *io);
449 static void ctl_enqueue_isc(union ctl_io *io);
450 static const struct ctl_cmd_entry *
451     ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
452 static const struct ctl_cmd_entry *
453     ctl_validate_command(struct ctl_scsiio *ctsio);
454 static int ctl_cmd_applicable(uint8_t lun_type,
455     const struct ctl_cmd_entry *entry);
456
457 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
458 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
459 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
460 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
461
462 /*
463  * Load the serialization table.  This isn't very pretty, but is probably
464  * the easiest way to do it.
465  */
466 #include "ctl_ser_table.c"
467
468 /*
469  * We only need to define open, close and ioctl routines for this driver.
470  */
471 static struct cdevsw ctl_cdevsw = {
472         .d_version =    D_VERSION,
473         .d_flags =      0,
474         .d_open =       ctl_open,
475         .d_close =      ctl_close,
476         .d_ioctl =      ctl_ioctl,
477         .d_name =       "ctl",
478 };
479
480
481 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
482
483 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
484
485 static moduledata_t ctl_moduledata = {
486         "ctl",
487         ctl_module_event_handler,
488         NULL
489 };
490
491 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
492 MODULE_VERSION(ctl, 1);
493
494 static struct ctl_frontend ha_frontend =
495 {
496         .name = "ha",
497 };
498
499 static void
500 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
501                             union ctl_ha_msg *msg_info)
502 {
503         struct ctl_scsiio *ctsio;
504
505         if (msg_info->hdr.original_sc == NULL) {
506                 printf("%s: original_sc == NULL!\n", __func__);
507                 /* XXX KDM now what? */
508                 return;
509         }
510
511         ctsio = &msg_info->hdr.original_sc->scsiio;
512         ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
513         ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
514         ctsio->io_hdr.status = msg_info->hdr.status;
515         ctsio->scsi_status = msg_info->scsi.scsi_status;
516         ctsio->sense_len = msg_info->scsi.sense_len;
517         ctsio->sense_residual = msg_info->scsi.sense_residual;
518         ctsio->residual = msg_info->scsi.residual;
519         memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
520                msg_info->scsi.sense_len);
521         ctl_enqueue_isc((union ctl_io *)ctsio);
522 }
523
524 static void
525 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
526                                 union ctl_ha_msg *msg_info)
527 {
528         struct ctl_scsiio *ctsio;
529
530         if (msg_info->hdr.serializing_sc == NULL) {
531                 printf("%s: serializing_sc == NULL!\n", __func__);
532                 /* XXX KDM now what? */
533                 return;
534         }
535
536         ctsio = &msg_info->hdr.serializing_sc->scsiio;
537         ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
538         ctl_enqueue_isc((union ctl_io *)ctsio);
539 }
540
541 void
542 ctl_isc_announce_lun(struct ctl_lun *lun)
543 {
544         struct ctl_softc *softc = lun->ctl_softc;
545         union ctl_ha_msg *msg;
546         struct ctl_ha_msg_lun_pr_key pr_key;
547         int i, k;
548
549         if (softc->ha_link != CTL_HA_LINK_ONLINE)
550                 return;
551         mtx_lock(&lun->lun_lock);
552         i = sizeof(msg->lun);
553         if (lun->lun_devid)
554                 i += lun->lun_devid->len;
555         i += sizeof(pr_key) * lun->pr_key_count;
556 alloc:
557         mtx_unlock(&lun->lun_lock);
558         msg = malloc(i, M_CTL, M_WAITOK);
559         mtx_lock(&lun->lun_lock);
560         k = sizeof(msg->lun);
561         if (lun->lun_devid)
562                 k += lun->lun_devid->len;
563         k += sizeof(pr_key) * lun->pr_key_count;
564         if (i < k) {
565                 free(msg, M_CTL);
566                 i = k;
567                 goto alloc;
568         }
569         bzero(&msg->lun, sizeof(msg->lun));
570         msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
571         msg->hdr.nexus.targ_lun = lun->lun;
572         msg->hdr.nexus.targ_mapped_lun = lun->lun;
573         msg->lun.flags = lun->flags;
574         msg->lun.pr_generation = lun->PRGeneration;
575         msg->lun.pr_res_idx = lun->pr_res_idx;
576         msg->lun.pr_res_type = lun->res_type;
577         msg->lun.pr_key_count = lun->pr_key_count;
578         i = 0;
579         if (lun->lun_devid) {
580                 msg->lun.lun_devid_len = lun->lun_devid->len;
581                 memcpy(&msg->lun.data[i], lun->lun_devid->data,
582                     msg->lun.lun_devid_len);
583                 i += msg->lun.lun_devid_len;
584         }
585         for (k = 0; k < CTL_MAX_INITIATORS; k++) {
586                 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
587                         continue;
588                 pr_key.pr_iid = k;
589                 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
590                 i += sizeof(pr_key);
591         }
592         mtx_unlock(&lun->lun_lock);
593         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
594             M_WAITOK);
595         free(msg, M_CTL);
596 }
597
598 void
599 ctl_isc_announce_port(struct ctl_port *port)
600 {
601         struct ctl_softc *softc = control_softc;
602         union ctl_ha_msg *msg;
603         int i;
604
605         if (port->targ_port < softc->port_min ||
606             port->targ_port >= softc->port_max ||
607             softc->ha_link != CTL_HA_LINK_ONLINE)
608                 return;
609         i = sizeof(msg->port) + strlen(port->port_name) + 1;
610         if (port->lun_map)
611                 i += sizeof(uint32_t) * CTL_MAX_LUNS;
612         if (port->port_devid)
613                 i += port->port_devid->len;
614         if (port->target_devid)
615                 i += port->target_devid->len;
616         msg = malloc(i, M_CTL, M_WAITOK);
617         bzero(&msg->port, sizeof(msg->port));
618         msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
619         msg->hdr.nexus.targ_port = port->targ_port;
620         msg->port.port_type = port->port_type;
621         msg->port.physical_port = port->physical_port;
622         msg->port.virtual_port = port->virtual_port;
623         msg->port.status = port->status;
624         i = 0;
625         msg->port.name_len = sprintf(&msg->port.data[i],
626             "%d:%s", softc->ha_id, port->port_name) + 1;
627         i += msg->port.name_len;
628         if (port->lun_map) {
629                 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
630                 memcpy(&msg->port.data[i], port->lun_map,
631                     msg->port.lun_map_len);
632                 i += msg->port.lun_map_len;
633         }
634         if (port->port_devid) {
635                 msg->port.port_devid_len = port->port_devid->len;
636                 memcpy(&msg->port.data[i], port->port_devid->data,
637                     msg->port.port_devid_len);
638                 i += msg->port.port_devid_len;
639         }
640         if (port->target_devid) {
641                 msg->port.target_devid_len = port->target_devid->len;
642                 memcpy(&msg->port.data[i], port->target_devid->data,
643                     msg->port.target_devid_len);
644                 i += msg->port.target_devid_len;
645         }
646         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
647             M_WAITOK);
648         free(msg, M_CTL);
649 }
650
651 static void
652 ctl_isc_ha_link_up(struct ctl_softc *softc)
653 {
654         struct ctl_port *port;
655         struct ctl_lun *lun;
656
657         STAILQ_FOREACH(port, &softc->port_list, links)
658                 ctl_isc_announce_port(port);
659         STAILQ_FOREACH(lun, &softc->lun_list, links)
660                 ctl_isc_announce_lun(lun);
661 }
662
663 static void
664 ctl_isc_ha_link_down(struct ctl_softc *softc)
665 {
666         struct ctl_port *port;
667         struct ctl_lun *lun;
668         union ctl_io *io;
669
670         mtx_lock(&softc->ctl_lock);
671         STAILQ_FOREACH(lun, &softc->lun_list, links) {
672                 mtx_lock(&lun->lun_lock);
673                 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
674                         lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
675                         ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
676                 }
677                 mtx_unlock(&lun->lun_lock);
678
679                 mtx_unlock(&softc->ctl_lock);
680                 io = ctl_alloc_io(softc->othersc_pool);
681                 mtx_lock(&softc->ctl_lock);
682                 ctl_zero_io(io);
683                 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
684                 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
685                 ctl_enqueue_isc(io);
686         }
687
688         STAILQ_FOREACH(port, &softc->port_list, links) {
689                 if (port->targ_port >= softc->port_min &&
690                     port->targ_port < softc->port_max)
691                         continue;
692                 port->status &= ~CTL_PORT_STATUS_ONLINE;
693         }
694         mtx_unlock(&softc->ctl_lock);
695 }
696
697 static void
698 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
699 {
700         struct ctl_lun *lun;
701         uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
702
703         mtx_lock(&softc->ctl_lock);
704         if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
705             (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
706                 mtx_lock(&lun->lun_lock);
707                 mtx_unlock(&softc->ctl_lock);
708                 if (msg->ua.ua_all) {
709                         if (msg->ua.ua_set)
710                                 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
711                         else
712                                 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
713                 } else {
714                         if (msg->ua.ua_set)
715                                 ctl_est_ua(lun, iid, msg->ua.ua_type);
716                         else
717                                 ctl_clr_ua(lun, iid, msg->ua.ua_type);
718                 }
719                 mtx_unlock(&lun->lun_lock);
720         } else
721                 mtx_unlock(&softc->ctl_lock);
722 }
723
724 static void
725 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
726 {
727         struct ctl_lun *lun;
728         struct ctl_ha_msg_lun_pr_key pr_key;
729         int i, k;
730         ctl_lun_flags oflags;
731         uint32_t targ_lun;
732
733         targ_lun = msg->hdr.nexus.targ_mapped_lun;
734         mtx_lock(&softc->ctl_lock);
735         if ((targ_lun >= CTL_MAX_LUNS) ||
736             ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
737                 mtx_unlock(&softc->ctl_lock);
738                 return;
739         }
740         mtx_lock(&lun->lun_lock);
741         mtx_unlock(&softc->ctl_lock);
742         if (lun->flags & CTL_LUN_DISABLED) {
743                 mtx_unlock(&lun->lun_lock);
744                 return;
745         }
746         i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
747         if (msg->lun.lun_devid_len != i || (i > 0 &&
748             memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
749                 mtx_unlock(&lun->lun_lock);
750                 printf("%s: Received conflicting HA LUN %d\n",
751                     __func__, msg->hdr.nexus.targ_lun);
752                 return;
753         } else {
754                 /* Record whether peer is primary. */
755                 oflags = lun->flags;
756                 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
757                     (msg->lun.flags & CTL_LUN_DISABLED) == 0)
758                         lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
759                 else
760                         lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
761                 if (oflags != lun->flags)
762                         ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
763
764                 /* If peer is primary and we are not -- use data */
765                 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
766                     (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
767                         lun->PRGeneration = msg->lun.pr_generation;
768                         lun->pr_res_idx = msg->lun.pr_res_idx;
769                         lun->res_type = msg->lun.pr_res_type;
770                         lun->pr_key_count = msg->lun.pr_key_count;
771                         for (k = 0; k < CTL_MAX_INITIATORS; k++)
772                                 ctl_clr_prkey(lun, k);
773                         for (k = 0; k < msg->lun.pr_key_count; k++) {
774                                 memcpy(&pr_key, &msg->lun.data[i],
775                                     sizeof(pr_key));
776                                 ctl_alloc_prkey(lun, pr_key.pr_iid);
777                                 ctl_set_prkey(lun, pr_key.pr_iid,
778                                     pr_key.pr_key);
779                                 i += sizeof(pr_key);
780                         }
781                 }
782
783                 mtx_unlock(&lun->lun_lock);
784                 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
785                     __func__, msg->hdr.nexus.targ_lun,
786                     (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
787                     "primary" : "secondary"));
788
789                 /* If we are primary but peer doesn't know -- notify */
790                 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
791                     (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
792                         ctl_isc_announce_lun(lun);
793         }
794 }
795
796 static void
797 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
798 {
799         struct ctl_port *port;
800         struct ctl_lun *lun;
801         int i, new;
802
803         port = softc->ctl_ports[msg->hdr.nexus.targ_port];
804         if (port == NULL) {
805                 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
806                     msg->hdr.nexus.targ_port));
807                 new = 1;
808                 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
809                 port->frontend = &ha_frontend;
810                 port->targ_port = msg->hdr.nexus.targ_port;
811         } else if (port->frontend == &ha_frontend) {
812                 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
813                     msg->hdr.nexus.targ_port));
814                 new = 0;
815         } else {
816                 printf("%s: Received conflicting HA port %d\n",
817                     __func__, msg->hdr.nexus.targ_port);
818                 return;
819         }
820         port->port_type = msg->port.port_type;
821         port->physical_port = msg->port.physical_port;
822         port->virtual_port = msg->port.virtual_port;
823         port->status = msg->port.status;
824         i = 0;
825         free(port->port_name, M_CTL);
826         port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
827             M_CTL);
828         i += msg->port.name_len;
829         if (msg->port.lun_map_len != 0) {
830                 if (port->lun_map == NULL)
831                         port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
832                             M_CTL, M_WAITOK);
833                 memcpy(port->lun_map, &msg->port.data[i],
834                     sizeof(uint32_t) * CTL_MAX_LUNS);
835                 i += msg->port.lun_map_len;
836         } else {
837                 free(port->lun_map, M_CTL);
838                 port->lun_map = NULL;
839         }
840         if (msg->port.port_devid_len != 0) {
841                 if (port->port_devid == NULL ||
842                     port->port_devid->len != msg->port.port_devid_len) {
843                         free(port->port_devid, M_CTL);
844                         port->port_devid = malloc(sizeof(struct ctl_devid) +
845                             msg->port.port_devid_len, M_CTL, M_WAITOK);
846                 }
847                 memcpy(port->port_devid->data, &msg->port.data[i],
848                     msg->port.port_devid_len);
849                 port->port_devid->len = msg->port.port_devid_len;
850                 i += msg->port.port_devid_len;
851         } else {
852                 free(port->port_devid, M_CTL);
853                 port->port_devid = NULL;
854         }
855         if (msg->port.target_devid_len != 0) {
856                 if (port->target_devid == NULL ||
857                     port->target_devid->len != msg->port.target_devid_len) {
858                         free(port->target_devid, M_CTL);
859                         port->target_devid = malloc(sizeof(struct ctl_devid) +
860                             msg->port.target_devid_len, M_CTL, M_WAITOK);
861                 }
862                 memcpy(port->target_devid->data, &msg->port.data[i],
863                     msg->port.target_devid_len);
864                 port->target_devid->len = msg->port.target_devid_len;
865                 i += msg->port.target_devid_len;
866         } else {
867                 free(port->port_devid, M_CTL);
868                 port->port_devid = NULL;
869         }
870         if (new) {
871                 if (ctl_port_register(port) != 0) {
872                         printf("%s: ctl_port_register() failed with error\n",
873                             __func__);
874                 }
875         }
876         mtx_lock(&softc->ctl_lock);
877         STAILQ_FOREACH(lun, &softc->lun_list, links) {
878                 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
879                         continue;
880                 mtx_lock(&lun->lun_lock);
881                 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
882                 mtx_unlock(&lun->lun_lock);
883         }
884         mtx_unlock(&softc->ctl_lock);
885 }
886
887 /*
888  * ISC (Inter Shelf Communication) event handler.  Events from the HA
889  * subsystem come in here.
890  */
891 static void
892 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
893 {
894         struct ctl_softc *softc;
895         union ctl_io *io;
896         struct ctl_prio *presio;
897         ctl_ha_status isc_status;
898
899         softc = control_softc;
900         CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
901         if (event == CTL_HA_EVT_MSG_RECV) {
902                 union ctl_ha_msg *msg, msgbuf;
903
904                 if (param > sizeof(msgbuf))
905                         msg = malloc(param, M_CTL, M_WAITOK);
906                 else
907                         msg = &msgbuf;
908                 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
909                     M_WAITOK);
910                 if (isc_status != CTL_HA_STATUS_SUCCESS) {
911                         printf("%s: Error receiving message: %d\n",
912                             __func__, isc_status);
913                         if (msg != &msgbuf)
914                                 free(msg, M_CTL);
915                         return;
916                 }
917
918                 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
919                 switch (msg->hdr.msg_type) {
920                 case CTL_MSG_SERIALIZE:
921                         io = ctl_alloc_io(softc->othersc_pool);
922                         ctl_zero_io(io);
923                         // populate ctsio from msg
924                         io->io_hdr.io_type = CTL_IO_SCSI;
925                         io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
926                         io->io_hdr.original_sc = msg->hdr.original_sc;
927                         io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
928                                             CTL_FLAG_IO_ACTIVE;
929                         /*
930                          * If we're in serialization-only mode, we don't
931                          * want to go through full done processing.  Thus
932                          * the COPY flag.
933                          *
934                          * XXX KDM add another flag that is more specific.
935                          */
936                         if (softc->ha_mode != CTL_HA_MODE_XFER)
937                                 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
938                         io->io_hdr.nexus = msg->hdr.nexus;
939 #if 0
940                         printf("port %u, iid %u, lun %u\n",
941                                io->io_hdr.nexus.targ_port,
942                                io->io_hdr.nexus.initid,
943                                io->io_hdr.nexus.targ_lun);
944 #endif
945                         io->scsiio.tag_num = msg->scsi.tag_num;
946                         io->scsiio.tag_type = msg->scsi.tag_type;
947 #ifdef CTL_TIME_IO
948                         io->io_hdr.start_time = time_uptime;
949                         getbintime(&io->io_hdr.start_bt);
950 #endif /* CTL_TIME_IO */
951                         io->scsiio.cdb_len = msg->scsi.cdb_len;
952                         memcpy(io->scsiio.cdb, msg->scsi.cdb,
953                                CTL_MAX_CDBLEN);
954                         if (softc->ha_mode == CTL_HA_MODE_XFER) {
955                                 const struct ctl_cmd_entry *entry;
956
957                                 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
958                                 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
959                                 io->io_hdr.flags |=
960                                         entry->flags & CTL_FLAG_DATA_MASK;
961                         }
962                         ctl_enqueue_isc(io);
963                         break;
964
965                 /* Performed on the Originating SC, XFER mode only */
966                 case CTL_MSG_DATAMOVE: {
967                         struct ctl_sg_entry *sgl;
968                         int i, j;
969
970                         io = msg->hdr.original_sc;
971                         if (io == NULL) {
972                                 printf("%s: original_sc == NULL!\n", __func__);
973                                 /* XXX KDM do something here */
974                                 break;
975                         }
976                         io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
977                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
978                         /*
979                          * Keep track of this, we need to send it back over
980                          * when the datamove is complete.
981                          */
982                         io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
983                         if (msg->hdr.status == CTL_SUCCESS)
984                                 io->io_hdr.status = msg->hdr.status;
985
986                         if (msg->dt.sg_sequence == 0) {
987                                 i = msg->dt.kern_sg_entries +
988                                     io->scsiio.kern_data_len /
989                                     CTL_HA_DATAMOVE_SEGMENT + 1;
990                                 sgl = malloc(sizeof(*sgl) * i, M_CTL,
991                                     M_WAITOK | M_ZERO);
992                                 io->io_hdr.remote_sglist = sgl;
993                                 io->io_hdr.local_sglist =
994                                     &sgl[msg->dt.kern_sg_entries];
995
996                                 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
997
998                                 io->scsiio.kern_sg_entries =
999                                         msg->dt.kern_sg_entries;
1000                                 io->scsiio.rem_sg_entries =
1001                                         msg->dt.kern_sg_entries;
1002                                 io->scsiio.kern_data_len =
1003                                         msg->dt.kern_data_len;
1004                                 io->scsiio.kern_total_len =
1005                                         msg->dt.kern_total_len;
1006                                 io->scsiio.kern_data_resid =
1007                                         msg->dt.kern_data_resid;
1008                                 io->scsiio.kern_rel_offset =
1009                                         msg->dt.kern_rel_offset;
1010                                 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1011                                 io->io_hdr.flags |= msg->dt.flags &
1012                                     CTL_FLAG_BUS_ADDR;
1013                         } else
1014                                 sgl = (struct ctl_sg_entry *)
1015                                         io->scsiio.kern_data_ptr;
1016
1017                         for (i = msg->dt.sent_sg_entries, j = 0;
1018                              i < (msg->dt.sent_sg_entries +
1019                              msg->dt.cur_sg_entries); i++, j++) {
1020                                 sgl[i].addr = msg->dt.sg_list[j].addr;
1021                                 sgl[i].len = msg->dt.sg_list[j].len;
1022
1023 #if 0
1024                                 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
1025                                        __func__,
1026                                        msg->dt.sg_list[j].addr,
1027                                        msg->dt.sg_list[j].len,
1028                                        sgl[i].addr, sgl[i].len, j, i);
1029 #endif
1030                         }
1031
1032                         /*
1033                          * If this is the last piece of the I/O, we've got
1034                          * the full S/G list.  Queue processing in the thread.
1035                          * Otherwise wait for the next piece.
1036                          */
1037                         if (msg->dt.sg_last != 0)
1038                                 ctl_enqueue_isc(io);
1039                         break;
1040                 }
1041                 /* Performed on the Serializing (primary) SC, XFER mode only */
1042                 case CTL_MSG_DATAMOVE_DONE: {
1043                         if (msg->hdr.serializing_sc == NULL) {
1044                                 printf("%s: serializing_sc == NULL!\n",
1045                                        __func__);
1046                                 /* XXX KDM now what? */
1047                                 break;
1048                         }
1049                         /*
1050                          * We grab the sense information here in case
1051                          * there was a failure, so we can return status
1052                          * back to the initiator.
1053                          */
1054                         io = msg->hdr.serializing_sc;
1055                         io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1056                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1057                         io->io_hdr.port_status = msg->scsi.fetd_status;
1058                         io->scsiio.residual = msg->scsi.residual;
1059                         if (msg->hdr.status != CTL_STATUS_NONE) {
1060                                 io->io_hdr.status = msg->hdr.status;
1061                                 io->scsiio.scsi_status = msg->scsi.scsi_status;
1062                                 io->scsiio.sense_len = msg->scsi.sense_len;
1063                                 io->scsiio.sense_residual =msg->scsi.sense_residual;
1064                                 memcpy(&io->scsiio.sense_data,
1065                                     &msg->scsi.sense_data,
1066                                     msg->scsi.sense_len);
1067                                 if (msg->hdr.status == CTL_SUCCESS)
1068                                         io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1069                         }
1070                         ctl_enqueue_isc(io);
1071                         break;
1072                 }
1073
1074                 /* Preformed on Originating SC, SER_ONLY mode */
1075                 case CTL_MSG_R2R:
1076                         io = msg->hdr.original_sc;
1077                         if (io == NULL) {
1078                                 printf("%s: original_sc == NULL!\n",
1079                                     __func__);
1080                                 break;
1081                         }
1082                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1083                         io->io_hdr.msg_type = CTL_MSG_R2R;
1084                         io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1085                         ctl_enqueue_isc(io);
1086                         break;
1087
1088                 /*
1089                  * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1090                  * mode.
1091                  * Performed on the Originating (i.e. secondary) SC in XFER
1092                  * mode
1093                  */
1094                 case CTL_MSG_FINISH_IO:
1095                         if (softc->ha_mode == CTL_HA_MODE_XFER)
1096                                 ctl_isc_handler_finish_xfer(softc, msg);
1097                         else
1098                                 ctl_isc_handler_finish_ser_only(softc, msg);
1099                         break;
1100
1101                 /* Preformed on Originating SC */
1102                 case CTL_MSG_BAD_JUJU:
1103                         io = msg->hdr.original_sc;
1104                         if (io == NULL) {
1105                                 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1106                                        __func__);
1107                                 break;
1108                         }
1109                         ctl_copy_sense_data(msg, io);
1110                         /*
1111                          * IO should have already been cleaned up on other
1112                          * SC so clear this flag so we won't send a message
1113                          * back to finish the IO there.
1114                          */
1115                         io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1116                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1117
1118                         /* io = msg->hdr.serializing_sc; */
1119                         io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1120                         ctl_enqueue_isc(io);
1121                         break;
1122
1123                 /* Handle resets sent from the other side */
1124                 case CTL_MSG_MANAGE_TASKS: {
1125                         struct ctl_taskio *taskio;
1126                         taskio = (struct ctl_taskio *)ctl_alloc_io(
1127                             softc->othersc_pool);
1128                         ctl_zero_io((union ctl_io *)taskio);
1129                         taskio->io_hdr.io_type = CTL_IO_TASK;
1130                         taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1131                         taskio->io_hdr.nexus = msg->hdr.nexus;
1132                         taskio->task_action = msg->task.task_action;
1133                         taskio->tag_num = msg->task.tag_num;
1134                         taskio->tag_type = msg->task.tag_type;
1135 #ifdef CTL_TIME_IO
1136                         taskio->io_hdr.start_time = time_uptime;
1137                         getbintime(&taskio->io_hdr.start_bt);
1138 #endif /* CTL_TIME_IO */
1139                         ctl_run_task((union ctl_io *)taskio);
1140                         break;
1141                 }
1142                 /* Persistent Reserve action which needs attention */
1143                 case CTL_MSG_PERS_ACTION:
1144                         presio = (struct ctl_prio *)ctl_alloc_io(
1145                             softc->othersc_pool);
1146                         ctl_zero_io((union ctl_io *)presio);
1147                         presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1148                         presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1149                         presio->io_hdr.nexus = msg->hdr.nexus;
1150                         presio->pr_msg = msg->pr;
1151                         ctl_enqueue_isc((union ctl_io *)presio);
1152                         break;
1153                 case CTL_MSG_UA:
1154                         ctl_isc_ua(softc, msg, param);
1155                         break;
1156                 case CTL_MSG_PORT_SYNC:
1157                         ctl_isc_port_sync(softc, msg, param);
1158                         break;
1159                 case CTL_MSG_LUN_SYNC:
1160                         ctl_isc_lun_sync(softc, msg, param);
1161                         break;
1162                 default:
1163                         printf("Received HA message of unknown type %d\n",
1164                             msg->hdr.msg_type);
1165                         break;
1166                 }
1167                 if (msg != &msgbuf)
1168                         free(msg, M_CTL);
1169         } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1170                 printf("CTL: HA link status changed from %d to %d\n",
1171                     softc->ha_link, param);
1172                 if (param == softc->ha_link)
1173                         return;
1174                 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1175                         softc->ha_link = param;
1176                         ctl_isc_ha_link_down(softc);
1177                 } else {
1178                         softc->ha_link = param;
1179                         if (softc->ha_link == CTL_HA_LINK_ONLINE)
1180                                 ctl_isc_ha_link_up(softc);
1181                 }
1182                 return;
1183         } else {
1184                 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1185                 return;
1186         }
1187 }
1188
1189 static void
1190 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1191 {
1192
1193         memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1194             src->scsi.sense_len);
1195         dest->scsiio.scsi_status = src->scsi.scsi_status;
1196         dest->scsiio.sense_len = src->scsi.sense_len;
1197         dest->io_hdr.status = src->hdr.status;
1198 }
1199
1200 static void
1201 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1202 {
1203
1204         memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1205             src->scsiio.sense_len);
1206         dest->scsi.scsi_status = src->scsiio.scsi_status;
1207         dest->scsi.sense_len = src->scsiio.sense_len;
1208         dest->hdr.status = src->io_hdr.status;
1209 }
1210
1211 void
1212 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1213 {
1214         struct ctl_softc *softc = lun->ctl_softc;
1215         ctl_ua_type *pu;
1216
1217         if (initidx < softc->init_min || initidx >= softc->init_max)
1218                 return;
1219         mtx_assert(&lun->lun_lock, MA_OWNED);
1220         pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1221         if (pu == NULL)
1222                 return;
1223         pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1224 }
1225
1226 void
1227 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1228 {
1229         int i;
1230
1231         mtx_assert(&lun->lun_lock, MA_OWNED);
1232         if (lun->pending_ua[port] == NULL)
1233                 return;
1234         for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1235                 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1236                         continue;
1237                 lun->pending_ua[port][i] |= ua;
1238         }
1239 }
1240
1241 void
1242 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1243 {
1244         struct ctl_softc *softc = lun->ctl_softc;
1245         int i;
1246
1247         mtx_assert(&lun->lun_lock, MA_OWNED);
1248         for (i = softc->port_min; i < softc->port_max; i++)
1249                 ctl_est_ua_port(lun, i, except, ua);
1250 }
1251
1252 void
1253 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1254 {
1255         struct ctl_softc *softc = lun->ctl_softc;
1256         ctl_ua_type *pu;
1257
1258         if (initidx < softc->init_min || initidx >= softc->init_max)
1259                 return;
1260         mtx_assert(&lun->lun_lock, MA_OWNED);
1261         pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1262         if (pu == NULL)
1263                 return;
1264         pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1265 }
1266
1267 void
1268 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1269 {
1270         struct ctl_softc *softc = lun->ctl_softc;
1271         int i, j;
1272
1273         mtx_assert(&lun->lun_lock, MA_OWNED);
1274         for (i = softc->port_min; i < softc->port_max; i++) {
1275                 if (lun->pending_ua[i] == NULL)
1276                         continue;
1277                 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1278                         if (i * CTL_MAX_INIT_PER_PORT + j == except)
1279                                 continue;
1280                         lun->pending_ua[i][j] &= ~ua;
1281                 }
1282         }
1283 }
1284
1285 void
1286 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1287     ctl_ua_type ua_type)
1288 {
1289         struct ctl_lun *lun;
1290
1291         mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1292         STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1293                 mtx_lock(&lun->lun_lock);
1294                 ctl_clr_ua(lun, initidx, ua_type);
1295                 mtx_unlock(&lun->lun_lock);
1296         }
1297 }
1298
1299 static int
1300 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1301 {
1302         struct ctl_softc *softc = (struct ctl_softc *)arg1;
1303         struct ctl_lun *lun;
1304         struct ctl_lun_req ireq;
1305         int error, value;
1306
1307         value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1308         error = sysctl_handle_int(oidp, &value, 0, req);
1309         if ((error != 0) || (req->newptr == NULL))
1310                 return (error);
1311
1312         mtx_lock(&softc->ctl_lock);
1313         if (value == 0)
1314                 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1315         else
1316                 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1317         STAILQ_FOREACH(lun, &softc->lun_list, links) {
1318                 mtx_unlock(&softc->ctl_lock);
1319                 bzero(&ireq, sizeof(ireq));
1320                 ireq.reqtype = CTL_LUNREQ_MODIFY;
1321                 ireq.reqdata.modify.lun_id = lun->lun;
1322                 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1323                     curthread);
1324                 if (ireq.status != CTL_LUN_OK) {
1325                         printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1326                             __func__, ireq.status, ireq.error_str);
1327                 }
1328                 mtx_lock(&softc->ctl_lock);
1329         }
1330         mtx_unlock(&softc->ctl_lock);
1331         return (0);
1332 }
1333
1334 static int
1335 ctl_init(void)
1336 {
1337         struct ctl_softc *softc;
1338         void *other_pool;
1339         int i, error, retval;
1340
1341         retval = 0;
1342         control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1343                                M_WAITOK | M_ZERO);
1344         softc = control_softc;
1345
1346         softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1347                               "cam/ctl");
1348
1349         softc->dev->si_drv1 = softc;
1350
1351         sysctl_ctx_init(&softc->sysctl_ctx);
1352         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1353                 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1354                 CTLFLAG_RD, 0, "CAM Target Layer");
1355
1356         if (softc->sysctl_tree == NULL) {
1357                 printf("%s: unable to allocate sysctl tree\n", __func__);
1358                 destroy_dev(softc->dev);
1359                 free(control_softc, M_DEVBUF);
1360                 control_softc = NULL;
1361                 return (ENOMEM);
1362         }
1363
1364         mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1365         softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1366             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1367         softc->open_count = 0;
1368
1369         /*
1370          * Default to actually sending a SYNCHRONIZE CACHE command down to
1371          * the drive.
1372          */
1373         softc->flags = CTL_FLAG_REAL_SYNC;
1374
1375         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1376             OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1377             "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1378
1379         /*
1380          * In Copan's HA scheme, the "master" and "slave" roles are
1381          * figured out through the slot the controller is in.  Although it
1382          * is an active/active system, someone has to be in charge.
1383          */
1384         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1385             OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1386             "HA head ID (0 - no HA)");
1387         if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1388                 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1389                 softc->is_single = 1;
1390                 softc->port_cnt = CTL_MAX_PORTS;
1391                 softc->port_min = 0;
1392         } else {
1393                 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1394                 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1395         }
1396         softc->port_max = softc->port_min + softc->port_cnt;
1397         softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1398         softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1399
1400         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1401             OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1402             "HA link state (0 - offline, 1 - unknown, 2 - online)");
1403
1404         STAILQ_INIT(&softc->lun_list);
1405         STAILQ_INIT(&softc->pending_lun_queue);
1406         STAILQ_INIT(&softc->fe_list);
1407         STAILQ_INIT(&softc->port_list);
1408         STAILQ_INIT(&softc->be_list);
1409         ctl_tpc_init(softc);
1410
1411         if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1412                             &other_pool) != 0)
1413         {
1414                 printf("ctl: can't allocate %d entry other SC pool, "
1415                        "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1416                 return (ENOMEM);
1417         }
1418         softc->othersc_pool = other_pool;
1419
1420         if (worker_threads <= 0)
1421                 worker_threads = max(1, mp_ncpus / 4);
1422         if (worker_threads > CTL_MAX_THREADS)
1423                 worker_threads = CTL_MAX_THREADS;
1424
1425         for (i = 0; i < worker_threads; i++) {
1426                 struct ctl_thread *thr = &softc->threads[i];
1427
1428                 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1429                 thr->ctl_softc = softc;
1430                 STAILQ_INIT(&thr->incoming_queue);
1431                 STAILQ_INIT(&thr->rtr_queue);
1432                 STAILQ_INIT(&thr->done_queue);
1433                 STAILQ_INIT(&thr->isc_queue);
1434
1435                 error = kproc_kthread_add(ctl_work_thread, thr,
1436                     &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1437                 if (error != 0) {
1438                         printf("error creating CTL work thread!\n");
1439                         ctl_pool_free(other_pool);
1440                         return (error);
1441                 }
1442         }
1443         error = kproc_kthread_add(ctl_lun_thread, softc,
1444             &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1445         if (error != 0) {
1446                 printf("error creating CTL lun thread!\n");
1447                 ctl_pool_free(other_pool);
1448                 return (error);
1449         }
1450         error = kproc_kthread_add(ctl_thresh_thread, softc,
1451             &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1452         if (error != 0) {
1453                 printf("error creating CTL threshold thread!\n");
1454                 ctl_pool_free(other_pool);
1455                 return (error);
1456         }
1457
1458         SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1459             OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1460             softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1461
1462         if (softc->is_single == 0) {
1463                 ctl_frontend_register(&ha_frontend);
1464                 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1465                         printf("ctl_init: ctl_ha_msg_init failed.\n");
1466                         softc->is_single = 1;
1467                 } else
1468                 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1469                     != CTL_HA_STATUS_SUCCESS) {
1470                         printf("ctl_init: ctl_ha_msg_register failed.\n");
1471                         softc->is_single = 1;
1472                 }
1473         }
1474         return (0);
1475 }
1476
1477 void
1478 ctl_shutdown(void)
1479 {
1480         struct ctl_softc *softc;
1481         struct ctl_lun *lun, *next_lun;
1482
1483         softc = (struct ctl_softc *)control_softc;
1484
1485         if (softc->is_single == 0) {
1486                 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1487                     != CTL_HA_STATUS_SUCCESS) {
1488                         printf("ctl_shutdown: ctl_ha_msg_deregister failed.\n");
1489                 }
1490                 if (ctl_ha_msg_shutdown(softc) != CTL_HA_STATUS_SUCCESS) {
1491                         printf("ctl_shutdown: ctl_ha_msg_shutdown failed.\n");
1492                 }
1493                 ctl_frontend_deregister(&ha_frontend);
1494         }
1495
1496         mtx_lock(&softc->ctl_lock);
1497
1498         /*
1499          * Free up each LUN.
1500          */
1501         for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1502                 next_lun = STAILQ_NEXT(lun, links);
1503                 ctl_free_lun(lun);
1504         }
1505
1506         mtx_unlock(&softc->ctl_lock);
1507
1508 #if 0
1509         ctl_shutdown_thread(softc->work_thread);
1510         mtx_destroy(&softc->queue_lock);
1511 #endif
1512
1513         ctl_tpc_shutdown(softc);
1514         uma_zdestroy(softc->io_zone);
1515         mtx_destroy(&softc->ctl_lock);
1516
1517         destroy_dev(softc->dev);
1518
1519         sysctl_ctx_free(&softc->sysctl_ctx);
1520
1521         free(control_softc, M_DEVBUF);
1522         control_softc = NULL;
1523 }
1524
1525 static int
1526 ctl_module_event_handler(module_t mod, int what, void *arg)
1527 {
1528
1529         switch (what) {
1530         case MOD_LOAD:
1531                 return (ctl_init());
1532         case MOD_UNLOAD:
1533                 return (EBUSY);
1534         default:
1535                 return (EOPNOTSUPP);
1536         }
1537 }
1538
1539 /*
1540  * XXX KDM should we do some access checks here?  Bump a reference count to
1541  * prevent a CTL module from being unloaded while someone has it open?
1542  */
1543 static int
1544 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1545 {
1546         return (0);
1547 }
1548
1549 static int
1550 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1551 {
1552         return (0);
1553 }
1554
1555 /*
1556  * Remove an initiator by port number and initiator ID.
1557  * Returns 0 for success, -1 for failure.
1558  */
1559 int
1560 ctl_remove_initiator(struct ctl_port *port, int iid)
1561 {
1562         struct ctl_softc *softc = control_softc;
1563
1564         mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1565
1566         if (iid > CTL_MAX_INIT_PER_PORT) {
1567                 printf("%s: initiator ID %u > maximun %u!\n",
1568                        __func__, iid, CTL_MAX_INIT_PER_PORT);
1569                 return (-1);
1570         }
1571
1572         mtx_lock(&softc->ctl_lock);
1573         port->wwpn_iid[iid].in_use--;
1574         port->wwpn_iid[iid].last_use = time_uptime;
1575         mtx_unlock(&softc->ctl_lock);
1576
1577         return (0);
1578 }
1579
1580 /*
1581  * Add an initiator to the initiator map.
1582  * Returns iid for success, < 0 for failure.
1583  */
1584 int
1585 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1586 {
1587         struct ctl_softc *softc = control_softc;
1588         time_t best_time;
1589         int i, best;
1590
1591         mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1592
1593         if (iid >= CTL_MAX_INIT_PER_PORT) {
1594                 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1595                        __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1596                 free(name, M_CTL);
1597                 return (-1);
1598         }
1599
1600         mtx_lock(&softc->ctl_lock);
1601
1602         if (iid < 0 && (wwpn != 0 || name != NULL)) {
1603                 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1604                         if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1605                                 iid = i;
1606                                 break;
1607                         }
1608                         if (name != NULL && port->wwpn_iid[i].name != NULL &&
1609                             strcmp(name, port->wwpn_iid[i].name) == 0) {
1610                                 iid = i;
1611                                 break;
1612                         }
1613                 }
1614         }
1615
1616         if (iid < 0) {
1617                 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1618                         if (port->wwpn_iid[i].in_use == 0 &&
1619                             port->wwpn_iid[i].wwpn == 0 &&
1620                             port->wwpn_iid[i].name == NULL) {
1621                                 iid = i;
1622                                 break;
1623                         }
1624                 }
1625         }
1626
1627         if (iid < 0) {
1628                 best = -1;
1629                 best_time = INT32_MAX;
1630                 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1631                         if (port->wwpn_iid[i].in_use == 0) {
1632                                 if (port->wwpn_iid[i].last_use < best_time) {
1633                                         best = i;
1634                                         best_time = port->wwpn_iid[i].last_use;
1635                                 }
1636                         }
1637                 }
1638                 iid = best;
1639         }
1640
1641         if (iid < 0) {
1642                 mtx_unlock(&softc->ctl_lock);
1643                 free(name, M_CTL);
1644                 return (-2);
1645         }
1646
1647         if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1648                 /*
1649                  * This is not an error yet.
1650                  */
1651                 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1652 #if 0
1653                         printf("%s: port %d iid %u WWPN %#jx arrived"
1654                             " again\n", __func__, port->targ_port,
1655                             iid, (uintmax_t)wwpn);
1656 #endif
1657                         goto take;
1658                 }
1659                 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1660                     strcmp(name, port->wwpn_iid[iid].name) == 0) {
1661 #if 0
1662                         printf("%s: port %d iid %u name '%s' arrived"
1663                             " again\n", __func__, port->targ_port,
1664                             iid, name);
1665 #endif
1666                         goto take;
1667                 }
1668
1669                 /*
1670                  * This is an error, but what do we do about it?  The
1671                  * driver is telling us we have a new WWPN for this
1672                  * initiator ID, so we pretty much need to use it.
1673                  */
1674                 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1675                     " but WWPN %#jx '%s' is still at that address\n",
1676                     __func__, port->targ_port, iid, wwpn, name,
1677                     (uintmax_t)port->wwpn_iid[iid].wwpn,
1678                     port->wwpn_iid[iid].name);
1679
1680                 /*
1681                  * XXX KDM clear have_ca and ua_pending on each LUN for
1682                  * this initiator.
1683                  */
1684         }
1685 take:
1686         free(port->wwpn_iid[iid].name, M_CTL);
1687         port->wwpn_iid[iid].name = name;
1688         port->wwpn_iid[iid].wwpn = wwpn;
1689         port->wwpn_iid[iid].in_use++;
1690         mtx_unlock(&softc->ctl_lock);
1691
1692         return (iid);
1693 }
1694
1695 static int
1696 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1697 {
1698         int len;
1699
1700         switch (port->port_type) {
1701         case CTL_PORT_FC:
1702         {
1703                 struct scsi_transportid_fcp *id =
1704                     (struct scsi_transportid_fcp *)buf;
1705                 if (port->wwpn_iid[iid].wwpn == 0)
1706                         return (0);
1707                 memset(id, 0, sizeof(*id));
1708                 id->format_protocol = SCSI_PROTO_FC;
1709                 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1710                 return (sizeof(*id));
1711         }
1712         case CTL_PORT_ISCSI:
1713         {
1714                 struct scsi_transportid_iscsi_port *id =
1715                     (struct scsi_transportid_iscsi_port *)buf;
1716                 if (port->wwpn_iid[iid].name == NULL)
1717                         return (0);
1718                 memset(id, 0, 256);
1719                 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1720                     SCSI_PROTO_ISCSI;
1721                 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1722                 len = roundup2(min(len, 252), 4);
1723                 scsi_ulto2b(len, id->additional_length);
1724                 return (sizeof(*id) + len);
1725         }
1726         case CTL_PORT_SAS:
1727         {
1728                 struct scsi_transportid_sas *id =
1729                     (struct scsi_transportid_sas *)buf;
1730                 if (port->wwpn_iid[iid].wwpn == 0)
1731                         return (0);
1732                 memset(id, 0, sizeof(*id));
1733                 id->format_protocol = SCSI_PROTO_SAS;
1734                 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1735                 return (sizeof(*id));
1736         }
1737         default:
1738         {
1739                 struct scsi_transportid_spi *id =
1740                     (struct scsi_transportid_spi *)buf;
1741                 memset(id, 0, sizeof(*id));
1742                 id->format_protocol = SCSI_PROTO_SPI;
1743                 scsi_ulto2b(iid, id->scsi_addr);
1744                 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1745                 return (sizeof(*id));
1746         }
1747         }
1748 }
1749
1750 /*
1751  * Serialize a command that went down the "wrong" side, and so was sent to
1752  * this controller for execution.  The logic is a little different than the
1753  * standard case in ctl_scsiio_precheck().  Errors in this case need to get
1754  * sent back to the other side, but in the success case, we execute the
1755  * command on this side (XFER mode) or tell the other side to execute it
1756  * (SER_ONLY mode).
1757  */
1758 static int
1759 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1760 {
1761         struct ctl_softc *softc;
1762         union ctl_ha_msg msg_info;
1763         struct ctl_lun *lun;
1764         const struct ctl_cmd_entry *entry;
1765         int retval = 0;
1766         uint32_t targ_lun;
1767
1768         softc = control_softc;
1769
1770         targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1771         mtx_lock(&softc->ctl_lock);
1772         if ((targ_lun < CTL_MAX_LUNS) &&
1773             ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
1774                 mtx_lock(&lun->lun_lock);
1775                 mtx_unlock(&softc->ctl_lock);
1776                 /*
1777                  * If the LUN is invalid, pretend that it doesn't exist.
1778                  * It will go away as soon as all pending I/O has been
1779                  * completed.
1780                  */
1781                 if (lun->flags & CTL_LUN_DISABLED) {
1782                         mtx_unlock(&lun->lun_lock);
1783                         lun = NULL;
1784                 }
1785         } else {
1786                 mtx_unlock(&softc->ctl_lock);
1787                 lun = NULL;
1788         }
1789         if (lun == NULL) {
1790                 /*
1791                  * The other node would not send this request to us unless
1792                  * received announce that we are primary node for this LUN.
1793                  * If this LUN does not exist now, it is probably result of
1794                  * a race, so respond to initiator in the most opaque way.
1795                  */
1796                 ctl_set_busy(ctsio);
1797                 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1798                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1799                 msg_info.hdr.serializing_sc = NULL;
1800                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1801                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1802                     sizeof(msg_info.scsi), M_WAITOK);
1803                 return(1);
1804         }
1805
1806         entry = ctl_get_cmd_entry(ctsio, NULL);
1807         if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
1808                 mtx_unlock(&lun->lun_lock);
1809                 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1810                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1811                 msg_info.hdr.serializing_sc = NULL;
1812                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1813                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1814                     sizeof(msg_info.scsi), M_WAITOK);
1815                 return(1);
1816         }
1817
1818         ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
1819         ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
1820
1821         /*
1822          * Every I/O goes into the OOA queue for a
1823          * particular LUN, and stays there until completion.
1824          */
1825 #ifdef CTL_TIME_IO
1826         if (TAILQ_EMPTY(&lun->ooa_queue))
1827                 lun->idle_time += getsbinuptime() - lun->last_busy;
1828 #endif
1829         TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1830
1831         switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1832                 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1833                  ooa_links))) {
1834         case CTL_ACTION_BLOCK:
1835                 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1836                 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1837                                   blocked_links);
1838                 mtx_unlock(&lun->lun_lock);
1839                 break;
1840         case CTL_ACTION_PASS:
1841         case CTL_ACTION_SKIP:
1842                 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1843                         ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1844                         ctl_enqueue_rtr((union ctl_io *)ctsio);
1845                         mtx_unlock(&lun->lun_lock);
1846                 } else {
1847                         ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
1848                         mtx_unlock(&lun->lun_lock);
1849
1850                         /* send msg back to other side */
1851                         msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1852                         msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1853                         msg_info.hdr.msg_type = CTL_MSG_R2R;
1854                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1855                             sizeof(msg_info.hdr), M_WAITOK);
1856                 }
1857                 break;
1858         case CTL_ACTION_OVERLAP:
1859                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1860                 mtx_unlock(&lun->lun_lock);
1861                 retval = 1;
1862
1863                 ctl_set_overlapped_cmd(ctsio);
1864                 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1865                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1866                 msg_info.hdr.serializing_sc = NULL;
1867                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1868                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1869                     sizeof(msg_info.scsi), M_WAITOK);
1870                 break;
1871         case CTL_ACTION_OVERLAP_TAG:
1872                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1873                 mtx_unlock(&lun->lun_lock);
1874                 retval = 1;
1875                 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
1876                 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1877                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1878                 msg_info.hdr.serializing_sc = NULL;
1879                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1880                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1881                     sizeof(msg_info.scsi), M_WAITOK);
1882                 break;
1883         case CTL_ACTION_ERROR:
1884         default:
1885                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1886                 mtx_unlock(&lun->lun_lock);
1887                 retval = 1;
1888
1889                 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
1890                                          /*retry_count*/ 0);
1891                 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1892                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1893                 msg_info.hdr.serializing_sc = NULL;
1894                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1895                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1896                     sizeof(msg_info.scsi), M_WAITOK);
1897                 break;
1898         }
1899         return (retval);
1900 }
1901
1902 /*
1903  * Returns 0 for success, errno for failure.
1904  */
1905 static int
1906 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1907                    struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1908 {
1909         union ctl_io *io;
1910         int retval;
1911
1912         retval = 0;
1913
1914         mtx_lock(&lun->lun_lock);
1915         for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1916              (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1917              ooa_links)) {
1918                 struct ctl_ooa_entry *entry;
1919
1920                 /*
1921                  * If we've got more than we can fit, just count the
1922                  * remaining entries.
1923                  */
1924                 if (*cur_fill_num >= ooa_hdr->alloc_num)
1925                         continue;
1926
1927                 entry = &kern_entries[*cur_fill_num];
1928
1929                 entry->tag_num = io->scsiio.tag_num;
1930                 entry->lun_num = lun->lun;
1931 #ifdef CTL_TIME_IO
1932                 entry->start_bt = io->io_hdr.start_bt;
1933 #endif
1934                 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
1935                 entry->cdb_len = io->scsiio.cdb_len;
1936                 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1937                         entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1938
1939                 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1940                         entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
1941
1942                 if (io->io_hdr.flags & CTL_FLAG_ABORT)
1943                         entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1944
1945                 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1946                         entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
1947
1948                 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1949                         entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
1950         }
1951         mtx_unlock(&lun->lun_lock);
1952
1953         return (retval);
1954 }
1955
1956 static void *
1957 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
1958                  size_t error_str_len)
1959 {
1960         void *kptr;
1961
1962         kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
1963
1964         if (copyin(user_addr, kptr, len) != 0) {
1965                 snprintf(error_str, error_str_len, "Error copying %d bytes "
1966                          "from user address %p to kernel address %p", len,
1967                          user_addr, kptr);
1968                 free(kptr, M_CTL);
1969                 return (NULL);
1970         }
1971
1972         return (kptr);
1973 }
1974
1975 static void
1976 ctl_free_args(int num_args, struct ctl_be_arg *args)
1977 {
1978         int i;
1979
1980         if (args == NULL)
1981                 return;
1982
1983         for (i = 0; i < num_args; i++) {
1984                 free(args[i].kname, M_CTL);
1985                 free(args[i].kvalue, M_CTL);
1986         }
1987
1988         free(args, M_CTL);
1989 }
1990
1991 static struct ctl_be_arg *
1992 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
1993                 char *error_str, size_t error_str_len)
1994 {
1995         struct ctl_be_arg *args;
1996         int i;
1997
1998         args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
1999                                 error_str, error_str_len);
2000
2001         if (args == NULL)
2002                 goto bailout;
2003
2004         for (i = 0; i < num_args; i++) {
2005                 args[i].kname = NULL;
2006                 args[i].kvalue = NULL;
2007         }
2008
2009         for (i = 0; i < num_args; i++) {
2010                 uint8_t *tmpptr;
2011
2012                 args[i].kname = ctl_copyin_alloc(args[i].name,
2013                         args[i].namelen, error_str, error_str_len);
2014                 if (args[i].kname == NULL)
2015                         goto bailout;
2016
2017                 if (args[i].kname[args[i].namelen - 1] != '\0') {
2018                         snprintf(error_str, error_str_len, "Argument %d "
2019                                  "name is not NUL-terminated", i);
2020                         goto bailout;
2021                 }
2022
2023                 if (args[i].flags & CTL_BEARG_RD) {
2024                         tmpptr = ctl_copyin_alloc(args[i].value,
2025                                 args[i].vallen, error_str, error_str_len);
2026                         if (tmpptr == NULL)
2027                                 goto bailout;
2028                         if ((args[i].flags & CTL_BEARG_ASCII)
2029                          && (tmpptr[args[i].vallen - 1] != '\0')) {
2030                                 snprintf(error_str, error_str_len, "Argument "
2031                                     "%d value is not NUL-terminated", i);
2032                                 goto bailout;
2033                         }
2034                         args[i].kvalue = tmpptr;
2035                 } else {
2036                         args[i].kvalue = malloc(args[i].vallen,
2037                             M_CTL, M_WAITOK | M_ZERO);
2038                 }
2039         }
2040
2041         return (args);
2042 bailout:
2043
2044         ctl_free_args(num_args, args);
2045
2046         return (NULL);
2047 }
2048
2049 static void
2050 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2051 {
2052         int i;
2053
2054         for (i = 0; i < num_args; i++) {
2055                 if (args[i].flags & CTL_BEARG_WR)
2056                         copyout(args[i].kvalue, args[i].value, args[i].vallen);
2057         }
2058 }
2059
2060 /*
2061  * Escape characters that are illegal or not recommended in XML.
2062  */
2063 int
2064 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2065 {
2066         char *end = str + size;
2067         int retval;
2068
2069         retval = 0;
2070
2071         for (; *str && str < end; str++) {
2072                 switch (*str) {
2073                 case '&':
2074                         retval = sbuf_printf(sb, "&amp;");
2075                         break;
2076                 case '>':
2077                         retval = sbuf_printf(sb, "&gt;");
2078                         break;
2079                 case '<':
2080                         retval = sbuf_printf(sb, "&lt;");
2081                         break;
2082                 default:
2083                         retval = sbuf_putc(sb, *str);
2084                         break;
2085                 }
2086
2087                 if (retval != 0)
2088                         break;
2089
2090         }
2091
2092         return (retval);
2093 }
2094
2095 static void
2096 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2097 {
2098         struct scsi_vpd_id_descriptor *desc;
2099         int i;
2100
2101         if (id == NULL || id->len < 4)
2102                 return;
2103         desc = (struct scsi_vpd_id_descriptor *)id->data;
2104         switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2105         case SVPD_ID_TYPE_T10:
2106                 sbuf_printf(sb, "t10.");
2107                 break;
2108         case SVPD_ID_TYPE_EUI64:
2109                 sbuf_printf(sb, "eui.");
2110                 break;
2111         case SVPD_ID_TYPE_NAA:
2112                 sbuf_printf(sb, "naa.");
2113                 break;
2114         case SVPD_ID_TYPE_SCSI_NAME:
2115                 break;
2116         }
2117         switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2118         case SVPD_ID_CODESET_BINARY:
2119                 for (i = 0; i < desc->length; i++)
2120                         sbuf_printf(sb, "%02x", desc->identifier[i]);
2121                 break;
2122         case SVPD_ID_CODESET_ASCII:
2123                 sbuf_printf(sb, "%.*s", (int)desc->length,
2124                     (char *)desc->identifier);
2125                 break;
2126         case SVPD_ID_CODESET_UTF8:
2127                 sbuf_printf(sb, "%s", (char *)desc->identifier);
2128                 break;
2129         }
2130 }
2131
2132 static int
2133 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2134           struct thread *td)
2135 {
2136         struct ctl_softc *softc;
2137         struct ctl_lun *lun;
2138         int retval;
2139
2140         softc = control_softc;
2141
2142         retval = 0;
2143
2144         switch (cmd) {
2145         case CTL_IO:
2146                 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2147                 break;
2148         case CTL_ENABLE_PORT:
2149         case CTL_DISABLE_PORT:
2150         case CTL_SET_PORT_WWNS: {
2151                 struct ctl_port *port;
2152                 struct ctl_port_entry *entry;
2153
2154                 entry = (struct ctl_port_entry *)addr;
2155                 
2156                 mtx_lock(&softc->ctl_lock);
2157                 STAILQ_FOREACH(port, &softc->port_list, links) {
2158                         int action, done;
2159
2160                         if (port->targ_port < softc->port_min ||
2161                             port->targ_port >= softc->port_max)
2162                                 continue;
2163
2164                         action = 0;
2165                         done = 0;
2166                         if ((entry->port_type == CTL_PORT_NONE)
2167                          && (entry->targ_port == port->targ_port)) {
2168                                 /*
2169                                  * If the user only wants to enable or
2170                                  * disable or set WWNs on a specific port,
2171                                  * do the operation and we're done.
2172                                  */
2173                                 action = 1;
2174                                 done = 1;
2175                         } else if (entry->port_type & port->port_type) {
2176                                 /*
2177                                  * Compare the user's type mask with the
2178                                  * particular frontend type to see if we
2179                                  * have a match.
2180                                  */
2181                                 action = 1;
2182                                 done = 0;
2183
2184                                 /*
2185                                  * Make sure the user isn't trying to set
2186                                  * WWNs on multiple ports at the same time.
2187                                  */
2188                                 if (cmd == CTL_SET_PORT_WWNS) {
2189                                         printf("%s: Can't set WWNs on "
2190                                                "multiple ports\n", __func__);
2191                                         retval = EINVAL;
2192                                         break;
2193                                 }
2194                         }
2195                         if (action == 0)
2196                                 continue;
2197
2198                         /*
2199                          * XXX KDM we have to drop the lock here, because
2200                          * the online/offline operations can potentially
2201                          * block.  We need to reference count the frontends
2202                          * so they can't go away,
2203                          */
2204                         if (cmd == CTL_ENABLE_PORT) {
2205                                 mtx_unlock(&softc->ctl_lock);
2206                                 ctl_port_online(port);
2207                                 mtx_lock(&softc->ctl_lock);
2208                         } else if (cmd == CTL_DISABLE_PORT) {
2209                                 mtx_unlock(&softc->ctl_lock);
2210                                 ctl_port_offline(port);
2211                                 mtx_lock(&softc->ctl_lock);
2212                         } else if (cmd == CTL_SET_PORT_WWNS) {
2213                                 ctl_port_set_wwns(port,
2214                                     (entry->flags & CTL_PORT_WWNN_VALID) ?
2215                                     1 : 0, entry->wwnn,
2216                                     (entry->flags & CTL_PORT_WWPN_VALID) ?
2217                                     1 : 0, entry->wwpn);
2218                         }
2219                         if (done != 0)
2220                                 break;
2221                 }
2222                 mtx_unlock(&softc->ctl_lock);
2223                 break;
2224         }
2225         case CTL_GET_PORT_LIST: {
2226                 struct ctl_port *port;
2227                 struct ctl_port_list *list;
2228                 int i;
2229
2230                 list = (struct ctl_port_list *)addr;
2231
2232                 if (list->alloc_len != (list->alloc_num *
2233                     sizeof(struct ctl_port_entry))) {
2234                         printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2235                                "alloc_num %u * sizeof(struct ctl_port_entry) "
2236                                "%zu\n", __func__, list->alloc_len,
2237                                list->alloc_num, sizeof(struct ctl_port_entry));
2238                         retval = EINVAL;
2239                         break;
2240                 }
2241                 list->fill_len = 0;
2242                 list->fill_num = 0;
2243                 list->dropped_num = 0;
2244                 i = 0;
2245                 mtx_lock(&softc->ctl_lock);
2246                 STAILQ_FOREACH(port, &softc->port_list, links) {
2247                         struct ctl_port_entry entry, *list_entry;
2248
2249                         if (list->fill_num >= list->alloc_num) {
2250                                 list->dropped_num++;
2251                                 continue;
2252                         }
2253
2254                         entry.port_type = port->port_type;
2255                         strlcpy(entry.port_name, port->port_name,
2256                                 sizeof(entry.port_name));
2257                         entry.targ_port = port->targ_port;
2258                         entry.physical_port = port->physical_port;
2259                         entry.virtual_port = port->virtual_port;
2260                         entry.wwnn = port->wwnn;
2261                         entry.wwpn = port->wwpn;
2262                         if (port->status & CTL_PORT_STATUS_ONLINE)
2263                                 entry.online = 1;
2264                         else
2265                                 entry.online = 0;
2266
2267                         list_entry = &list->entries[i];
2268
2269                         retval = copyout(&entry, list_entry, sizeof(entry));
2270                         if (retval != 0) {
2271                                 printf("%s: CTL_GET_PORT_LIST: copyout "
2272                                        "returned %d\n", __func__, retval);
2273                                 break;
2274                         }
2275                         i++;
2276                         list->fill_num++;
2277                         list->fill_len += sizeof(entry);
2278                 }
2279                 mtx_unlock(&softc->ctl_lock);
2280
2281                 /*
2282                  * If this is non-zero, we had a copyout fault, so there's
2283                  * probably no point in attempting to set the status inside
2284                  * the structure.
2285                  */
2286                 if (retval != 0)
2287                         break;
2288
2289                 if (list->dropped_num > 0)
2290                         list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2291                 else
2292                         list->status = CTL_PORT_LIST_OK;
2293                 break;
2294         }
2295         case CTL_DUMP_OOA: {
2296                 union ctl_io *io;
2297                 char printbuf[128];
2298                 struct sbuf sb;
2299
2300                 mtx_lock(&softc->ctl_lock);
2301                 printf("Dumping OOA queues:\n");
2302                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2303                         mtx_lock(&lun->lun_lock);
2304                         for (io = (union ctl_io *)TAILQ_FIRST(
2305                              &lun->ooa_queue); io != NULL;
2306                              io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2307                              ooa_links)) {
2308                                 sbuf_new(&sb, printbuf, sizeof(printbuf),
2309                                          SBUF_FIXEDLEN);
2310                                 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2311                                             (intmax_t)lun->lun,
2312                                             io->scsiio.tag_num,
2313                                             (io->io_hdr.flags &
2314                                             CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2315                                             (io->io_hdr.flags &
2316                                             CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2317                                             (io->io_hdr.flags &
2318                                             CTL_FLAG_ABORT) ? " ABORT" : "",
2319                                             (io->io_hdr.flags &
2320                                         CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2321                                 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2322                                 sbuf_finish(&sb);
2323                                 printf("%s\n", sbuf_data(&sb));
2324                         }
2325                         mtx_unlock(&lun->lun_lock);
2326                 }
2327                 printf("OOA queues dump done\n");
2328                 mtx_unlock(&softc->ctl_lock);
2329                 break;
2330         }
2331         case CTL_GET_OOA: {
2332                 struct ctl_ooa *ooa_hdr;
2333                 struct ctl_ooa_entry *entries;
2334                 uint32_t cur_fill_num;
2335
2336                 ooa_hdr = (struct ctl_ooa *)addr;
2337
2338                 if ((ooa_hdr->alloc_len == 0)
2339                  || (ooa_hdr->alloc_num == 0)) {
2340                         printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2341                                "must be non-zero\n", __func__,
2342                                ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2343                         retval = EINVAL;
2344                         break;
2345                 }
2346
2347                 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2348                     sizeof(struct ctl_ooa_entry))) {
2349                         printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2350                                "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2351                                __func__, ooa_hdr->alloc_len,
2352                                ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2353                         retval = EINVAL;
2354                         break;
2355                 }
2356
2357                 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2358                 if (entries == NULL) {
2359                         printf("%s: could not allocate %d bytes for OOA "
2360                                "dump\n", __func__, ooa_hdr->alloc_len);
2361                         retval = ENOMEM;
2362                         break;
2363                 }
2364
2365                 mtx_lock(&softc->ctl_lock);
2366                 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2367                  && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2368                   || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2369                         mtx_unlock(&softc->ctl_lock);
2370                         free(entries, M_CTL);
2371                         printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2372                                __func__, (uintmax_t)ooa_hdr->lun_num);
2373                         retval = EINVAL;
2374                         break;
2375                 }
2376
2377                 cur_fill_num = 0;
2378
2379                 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2380                         STAILQ_FOREACH(lun, &softc->lun_list, links) {
2381                                 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2382                                         ooa_hdr, entries);
2383                                 if (retval != 0)
2384                                         break;
2385                         }
2386                         if (retval != 0) {
2387                                 mtx_unlock(&softc->ctl_lock);
2388                                 free(entries, M_CTL);
2389                                 break;
2390                         }
2391                 } else {
2392                         lun = softc->ctl_luns[ooa_hdr->lun_num];
2393
2394                         retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2395                                                     entries);
2396                 }
2397                 mtx_unlock(&softc->ctl_lock);
2398
2399                 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2400                 ooa_hdr->fill_len = ooa_hdr->fill_num *
2401                         sizeof(struct ctl_ooa_entry);
2402                 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2403                 if (retval != 0) {
2404                         printf("%s: error copying out %d bytes for OOA dump\n", 
2405                                __func__, ooa_hdr->fill_len);
2406                 }
2407
2408                 getbintime(&ooa_hdr->cur_bt);
2409
2410                 if (cur_fill_num > ooa_hdr->alloc_num) {
2411                         ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2412                         ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2413                 } else {
2414                         ooa_hdr->dropped_num = 0;
2415                         ooa_hdr->status = CTL_OOA_OK;
2416                 }
2417
2418                 free(entries, M_CTL);
2419                 break;
2420         }
2421         case CTL_CHECK_OOA: {
2422                 union ctl_io *io;
2423                 struct ctl_ooa_info *ooa_info;
2424
2425
2426                 ooa_info = (struct ctl_ooa_info *)addr;
2427
2428                 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2429                         ooa_info->status = CTL_OOA_INVALID_LUN;
2430                         break;
2431                 }
2432                 mtx_lock(&softc->ctl_lock);
2433                 lun = softc->ctl_luns[ooa_info->lun_id];
2434                 if (lun == NULL) {
2435                         mtx_unlock(&softc->ctl_lock);
2436                         ooa_info->status = CTL_OOA_INVALID_LUN;
2437                         break;
2438                 }
2439                 mtx_lock(&lun->lun_lock);
2440                 mtx_unlock(&softc->ctl_lock);
2441                 ooa_info->num_entries = 0;
2442                 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2443                      io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2444                      &io->io_hdr, ooa_links)) {
2445                         ooa_info->num_entries++;
2446                 }
2447                 mtx_unlock(&lun->lun_lock);
2448
2449                 ooa_info->status = CTL_OOA_SUCCESS;
2450
2451                 break;
2452         }
2453         case CTL_DELAY_IO: {
2454                 struct ctl_io_delay_info *delay_info;
2455
2456                 delay_info = (struct ctl_io_delay_info *)addr;
2457
2458 #ifdef CTL_IO_DELAY
2459                 mtx_lock(&softc->ctl_lock);
2460
2461                 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2462                  || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2463                         delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2464                 } else {
2465                         lun = softc->ctl_luns[delay_info->lun_id];
2466                         mtx_lock(&lun->lun_lock);
2467
2468                         delay_info->status = CTL_DELAY_STATUS_OK;
2469
2470                         switch (delay_info->delay_type) {
2471                         case CTL_DELAY_TYPE_CONT:
2472                                 break;
2473                         case CTL_DELAY_TYPE_ONESHOT:
2474                                 break;
2475                         default:
2476                                 delay_info->status =
2477                                         CTL_DELAY_STATUS_INVALID_TYPE;
2478                                 break;
2479                         }
2480
2481                         switch (delay_info->delay_loc) {
2482                         case CTL_DELAY_LOC_DATAMOVE:
2483                                 lun->delay_info.datamove_type =
2484                                         delay_info->delay_type;
2485                                 lun->delay_info.datamove_delay =
2486                                         delay_info->delay_secs;
2487                                 break;
2488                         case CTL_DELAY_LOC_DONE:
2489                                 lun->delay_info.done_type =
2490                                         delay_info->delay_type;
2491                                 lun->delay_info.done_delay =
2492                                         delay_info->delay_secs;
2493                                 break;
2494                         default:
2495                                 delay_info->status =
2496                                         CTL_DELAY_STATUS_INVALID_LOC;
2497                                 break;
2498                         }
2499                         mtx_unlock(&lun->lun_lock);
2500                 }
2501
2502                 mtx_unlock(&softc->ctl_lock);
2503 #else
2504                 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2505 #endif /* CTL_IO_DELAY */
2506                 break;
2507         }
2508         case CTL_REALSYNC_SET: {
2509                 int *syncstate;
2510
2511                 syncstate = (int *)addr;
2512
2513                 mtx_lock(&softc->ctl_lock);
2514                 switch (*syncstate) {
2515                 case 0:
2516                         softc->flags &= ~CTL_FLAG_REAL_SYNC;
2517                         break;
2518                 case 1:
2519                         softc->flags |= CTL_FLAG_REAL_SYNC;
2520                         break;
2521                 default:
2522                         retval = EINVAL;
2523                         break;
2524                 }
2525                 mtx_unlock(&softc->ctl_lock);
2526                 break;
2527         }
2528         case CTL_REALSYNC_GET: {
2529                 int *syncstate;
2530
2531                 syncstate = (int*)addr;
2532
2533                 mtx_lock(&softc->ctl_lock);
2534                 if (softc->flags & CTL_FLAG_REAL_SYNC)
2535                         *syncstate = 1;
2536                 else
2537                         *syncstate = 0;
2538                 mtx_unlock(&softc->ctl_lock);
2539
2540                 break;
2541         }
2542         case CTL_SETSYNC:
2543         case CTL_GETSYNC: {
2544                 struct ctl_sync_info *sync_info;
2545
2546                 sync_info = (struct ctl_sync_info *)addr;
2547
2548                 mtx_lock(&softc->ctl_lock);
2549                 lun = softc->ctl_luns[sync_info->lun_id];
2550                 if (lun == NULL) {
2551                         mtx_unlock(&softc->ctl_lock);
2552                         sync_info->status = CTL_GS_SYNC_NO_LUN;
2553                         break;
2554                 }
2555                 /*
2556                  * Get or set the sync interval.  We're not bounds checking
2557                  * in the set case, hopefully the user won't do something
2558                  * silly.
2559                  */
2560                 mtx_lock(&lun->lun_lock);
2561                 mtx_unlock(&softc->ctl_lock);
2562                 if (cmd == CTL_GETSYNC)
2563                         sync_info->sync_interval = lun->sync_interval;
2564                 else
2565                         lun->sync_interval = sync_info->sync_interval;
2566                 mtx_unlock(&lun->lun_lock);
2567
2568                 sync_info->status = CTL_GS_SYNC_OK;
2569
2570                 break;
2571         }
2572         case CTL_GETSTATS: {
2573                 struct ctl_stats *stats;
2574                 int i;
2575
2576                 stats = (struct ctl_stats *)addr;
2577
2578                 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2579                      stats->alloc_len) {
2580                         stats->status = CTL_SS_NEED_MORE_SPACE;
2581                         stats->num_luns = softc->num_luns;
2582                         break;
2583                 }
2584                 /*
2585                  * XXX KDM no locking here.  If the LUN list changes,
2586                  * things can blow up.
2587                  */
2588                 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2589                      i++, lun = STAILQ_NEXT(lun, links)) {
2590                         retval = copyout(&lun->stats, &stats->lun_stats[i],
2591                                          sizeof(lun->stats));
2592                         if (retval != 0)
2593                                 break;
2594                 }
2595                 stats->num_luns = softc->num_luns;
2596                 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2597                                  softc->num_luns;
2598                 stats->status = CTL_SS_OK;
2599 #ifdef CTL_TIME_IO
2600                 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2601 #else
2602                 stats->flags = CTL_STATS_FLAG_NONE;
2603 #endif
2604                 getnanouptime(&stats->timestamp);
2605                 break;
2606         }
2607         case CTL_ERROR_INJECT: {
2608                 struct ctl_error_desc *err_desc, *new_err_desc;
2609
2610                 err_desc = (struct ctl_error_desc *)addr;
2611
2612                 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2613                                       M_WAITOK | M_ZERO);
2614                 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2615
2616                 mtx_lock(&softc->ctl_lock);
2617                 lun = softc->ctl_luns[err_desc->lun_id];
2618                 if (lun == NULL) {
2619                         mtx_unlock(&softc->ctl_lock);
2620                         free(new_err_desc, M_CTL);
2621                         printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2622                                __func__, (uintmax_t)err_desc->lun_id);
2623                         retval = EINVAL;
2624                         break;
2625                 }
2626                 mtx_lock(&lun->lun_lock);
2627                 mtx_unlock(&softc->ctl_lock);
2628
2629                 /*
2630                  * We could do some checking here to verify the validity
2631                  * of the request, but given the complexity of error
2632                  * injection requests, the checking logic would be fairly
2633                  * complex.
2634                  *
2635                  * For now, if the request is invalid, it just won't get
2636                  * executed and might get deleted.
2637                  */
2638                 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2639
2640                 /*
2641                  * XXX KDM check to make sure the serial number is unique,
2642                  * in case we somehow manage to wrap.  That shouldn't
2643                  * happen for a very long time, but it's the right thing to
2644                  * do.
2645                  */
2646                 new_err_desc->serial = lun->error_serial;
2647                 err_desc->serial = lun->error_serial;
2648                 lun->error_serial++;
2649
2650                 mtx_unlock(&lun->lun_lock);
2651                 break;
2652         }
2653         case CTL_ERROR_INJECT_DELETE: {
2654                 struct ctl_error_desc *delete_desc, *desc, *desc2;
2655                 int delete_done;
2656
2657                 delete_desc = (struct ctl_error_desc *)addr;
2658                 delete_done = 0;
2659
2660                 mtx_lock(&softc->ctl_lock);
2661                 lun = softc->ctl_luns[delete_desc->lun_id];
2662                 if (lun == NULL) {
2663                         mtx_unlock(&softc->ctl_lock);
2664                         printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2665                                __func__, (uintmax_t)delete_desc->lun_id);
2666                         retval = EINVAL;
2667                         break;
2668                 }
2669                 mtx_lock(&lun->lun_lock);
2670                 mtx_unlock(&softc->ctl_lock);
2671                 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2672                         if (desc->serial != delete_desc->serial)
2673                                 continue;
2674
2675                         STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2676                                       links);
2677                         free(desc, M_CTL);
2678                         delete_done = 1;
2679                 }
2680                 mtx_unlock(&lun->lun_lock);
2681                 if (delete_done == 0) {
2682                         printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2683                                "error serial %ju on LUN %u\n", __func__, 
2684                                delete_desc->serial, delete_desc->lun_id);
2685                         retval = EINVAL;
2686                         break;
2687                 }
2688                 break;
2689         }
2690         case CTL_DUMP_STRUCTS: {
2691                 int i, j, k;
2692                 struct ctl_port *port;
2693                 struct ctl_frontend *fe;
2694
2695                 mtx_lock(&softc->ctl_lock);
2696                 printf("CTL Persistent Reservation information start:\n");
2697                 for (i = 0; i < CTL_MAX_LUNS; i++) {
2698                         lun = softc->ctl_luns[i];
2699
2700                         if ((lun == NULL)
2701                          || ((lun->flags & CTL_LUN_DISABLED) != 0))
2702                                 continue;
2703
2704                         for (j = 0; j < CTL_MAX_PORTS; j++) {
2705                                 if (lun->pr_keys[j] == NULL)
2706                                         continue;
2707                                 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2708                                         if (lun->pr_keys[j][k] == 0)
2709                                                 continue;
2710                                         printf("  LUN %d port %d iid %d key "
2711                                                "%#jx\n", i, j, k,
2712                                                (uintmax_t)lun->pr_keys[j][k]);
2713                                 }
2714                         }
2715                 }
2716                 printf("CTL Persistent Reservation information end\n");
2717                 printf("CTL Ports:\n");
2718                 STAILQ_FOREACH(port, &softc->port_list, links) {
2719                         printf("  Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2720                                "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2721                                port->frontend->name, port->port_type,
2722                                port->physical_port, port->virtual_port,
2723                                (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2724                         for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2725                                 if (port->wwpn_iid[j].in_use == 0 &&
2726                                     port->wwpn_iid[j].wwpn == 0 &&
2727                                     port->wwpn_iid[j].name == NULL)
2728                                         continue;
2729
2730                                 printf("    iid %u use %d WWPN %#jx '%s'\n",
2731                                     j, port->wwpn_iid[j].in_use,
2732                                     (uintmax_t)port->wwpn_iid[j].wwpn,
2733                                     port->wwpn_iid[j].name);
2734                         }
2735                 }
2736                 printf("CTL Port information end\n");
2737                 mtx_unlock(&softc->ctl_lock);
2738                 /*
2739                  * XXX KDM calling this without a lock.  We'd likely want
2740                  * to drop the lock before calling the frontend's dump
2741                  * routine anyway.
2742                  */
2743                 printf("CTL Frontends:\n");
2744                 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2745                         printf("  Frontend '%s'\n", fe->name);
2746                         if (fe->fe_dump != NULL)
2747                                 fe->fe_dump();
2748                 }
2749                 printf("CTL Frontend information end\n");
2750                 break;
2751         }
2752         case CTL_LUN_REQ: {
2753                 struct ctl_lun_req *lun_req;
2754                 struct ctl_backend_driver *backend;
2755
2756                 lun_req = (struct ctl_lun_req *)addr;
2757
2758                 backend = ctl_backend_find(lun_req->backend);
2759                 if (backend == NULL) {
2760                         lun_req->status = CTL_LUN_ERROR;
2761                         snprintf(lun_req->error_str,
2762                                  sizeof(lun_req->error_str),
2763                                  "Backend \"%s\" not found.",
2764                                  lun_req->backend);
2765                         break;
2766                 }
2767                 if (lun_req->num_be_args > 0) {
2768                         lun_req->kern_be_args = ctl_copyin_args(
2769                                 lun_req->num_be_args,
2770                                 lun_req->be_args,
2771                                 lun_req->error_str,
2772                                 sizeof(lun_req->error_str));
2773                         if (lun_req->kern_be_args == NULL) {
2774                                 lun_req->status = CTL_LUN_ERROR;
2775                                 break;
2776                         }
2777                 }
2778
2779                 retval = backend->ioctl(dev, cmd, addr, flag, td);
2780
2781                 if (lun_req->num_be_args > 0) {
2782                         ctl_copyout_args(lun_req->num_be_args,
2783                                       lun_req->kern_be_args);
2784                         ctl_free_args(lun_req->num_be_args,
2785                                       lun_req->kern_be_args);
2786                 }
2787                 break;
2788         }
2789         case CTL_LUN_LIST: {
2790                 struct sbuf *sb;
2791                 struct ctl_lun_list *list;
2792                 struct ctl_option *opt;
2793
2794                 list = (struct ctl_lun_list *)addr;
2795
2796                 /*
2797                  * Allocate a fixed length sbuf here, based on the length
2798                  * of the user's buffer.  We could allocate an auto-extending
2799                  * buffer, and then tell the user how much larger our
2800                  * amount of data is than his buffer, but that presents
2801                  * some problems:
2802                  *
2803                  * 1.  The sbuf(9) routines use a blocking malloc, and so
2804                  *     we can't hold a lock while calling them with an
2805                  *     auto-extending buffer.
2806                  *
2807                  * 2.  There is not currently a LUN reference counting
2808                  *     mechanism, outside of outstanding transactions on
2809                  *     the LUN's OOA queue.  So a LUN could go away on us
2810                  *     while we're getting the LUN number, backend-specific
2811                  *     information, etc.  Thus, given the way things
2812                  *     currently work, we need to hold the CTL lock while
2813                  *     grabbing LUN information.
2814                  *
2815                  * So, from the user's standpoint, the best thing to do is
2816                  * allocate what he thinks is a reasonable buffer length,
2817                  * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2818                  * double the buffer length and try again.  (And repeat
2819                  * that until he succeeds.)
2820                  */
2821                 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2822                 if (sb == NULL) {
2823                         list->status = CTL_LUN_LIST_ERROR;
2824                         snprintf(list->error_str, sizeof(list->error_str),
2825                                  "Unable to allocate %d bytes for LUN list",
2826                                  list->alloc_len);
2827                         break;
2828                 }
2829
2830                 sbuf_printf(sb, "<ctllunlist>\n");
2831
2832                 mtx_lock(&softc->ctl_lock);
2833                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2834                         mtx_lock(&lun->lun_lock);
2835                         retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
2836                                              (uintmax_t)lun->lun);
2837
2838                         /*
2839                          * Bail out as soon as we see that we've overfilled
2840                          * the buffer.
2841                          */
2842                         if (retval != 0)
2843                                 break;
2844
2845                         retval = sbuf_printf(sb, "\t<backend_type>%s"
2846                                              "</backend_type>\n",
2847                                              (lun->backend == NULL) ?  "none" :
2848                                              lun->backend->name);
2849
2850                         if (retval != 0)
2851                                 break;
2852
2853                         retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
2854                                              lun->be_lun->lun_type);
2855
2856                         if (retval != 0)
2857                                 break;
2858
2859                         if (lun->backend == NULL) {
2860                                 retval = sbuf_printf(sb, "</lun>\n");
2861                                 if (retval != 0)
2862                                         break;
2863                                 continue;
2864                         }
2865
2866                         retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
2867                                              (lun->be_lun->maxlba > 0) ?
2868                                              lun->be_lun->maxlba + 1 : 0);
2869
2870                         if (retval != 0)
2871                                 break;
2872
2873                         retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
2874                                              lun->be_lun->blocksize);
2875
2876                         if (retval != 0)
2877                                 break;
2878
2879                         retval = sbuf_printf(sb, "\t<serial_number>");
2880
2881                         if (retval != 0)
2882                                 break;
2883
2884                         retval = ctl_sbuf_printf_esc(sb,
2885                             lun->be_lun->serial_num,
2886                             sizeof(lun->be_lun->serial_num));
2887
2888                         if (retval != 0)
2889                                 break;
2890
2891                         retval = sbuf_printf(sb, "</serial_number>\n");
2892                 
2893                         if (retval != 0)
2894                                 break;
2895
2896                         retval = sbuf_printf(sb, "\t<device_id>");
2897
2898                         if (retval != 0)
2899                                 break;
2900
2901                         retval = ctl_sbuf_printf_esc(sb,
2902                             lun->be_lun->device_id,
2903                             sizeof(lun->be_lun->device_id));
2904
2905                         if (retval != 0)
2906                                 break;
2907
2908                         retval = sbuf_printf(sb, "</device_id>\n");
2909
2910                         if (retval != 0)
2911                                 break;
2912
2913                         if (lun->backend->lun_info != NULL) {
2914                                 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
2915                                 if (retval != 0)
2916                                         break;
2917                         }
2918                         STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
2919                                 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
2920                                     opt->name, opt->value, opt->name);
2921                                 if (retval != 0)
2922                                         break;
2923                         }
2924
2925                         retval = sbuf_printf(sb, "</lun>\n");
2926
2927                         if (retval != 0)
2928                                 break;
2929                         mtx_unlock(&lun->lun_lock);
2930                 }
2931                 if (lun != NULL)
2932                         mtx_unlock(&lun->lun_lock);
2933                 mtx_unlock(&softc->ctl_lock);
2934
2935                 if ((retval != 0)
2936                  || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
2937                         retval = 0;
2938                         sbuf_delete(sb);
2939                         list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
2940                         snprintf(list->error_str, sizeof(list->error_str),
2941                                  "Out of space, %d bytes is too small",
2942                                  list->alloc_len);
2943                         break;
2944                 }
2945
2946                 sbuf_finish(sb);
2947
2948                 retval = copyout(sbuf_data(sb), list->lun_xml,
2949                                  sbuf_len(sb) + 1);
2950
2951                 list->fill_len = sbuf_len(sb) + 1;
2952                 list->status = CTL_LUN_LIST_OK;
2953                 sbuf_delete(sb);
2954                 break;
2955         }
2956         case CTL_ISCSI: {
2957                 struct ctl_iscsi *ci;
2958                 struct ctl_frontend *fe;
2959
2960                 ci = (struct ctl_iscsi *)addr;
2961
2962                 fe = ctl_frontend_find("iscsi");
2963                 if (fe == NULL) {
2964                         ci->status = CTL_ISCSI_ERROR;
2965                         snprintf(ci->error_str, sizeof(ci->error_str),
2966                             "Frontend \"iscsi\" not found.");
2967                         break;
2968                 }
2969
2970                 retval = fe->ioctl(dev, cmd, addr, flag, td);
2971                 break;
2972         }
2973         case CTL_PORT_REQ: {
2974                 struct ctl_req *req;
2975                 struct ctl_frontend *fe;
2976
2977                 req = (struct ctl_req *)addr;
2978
2979                 fe = ctl_frontend_find(req->driver);
2980                 if (fe == NULL) {
2981                         req->status = CTL_LUN_ERROR;
2982                         snprintf(req->error_str, sizeof(req->error_str),
2983                             "Frontend \"%s\" not found.", req->driver);
2984                         break;
2985                 }
2986                 if (req->num_args > 0) {
2987                         req->kern_args = ctl_copyin_args(req->num_args,
2988                             req->args, req->error_str, sizeof(req->error_str));
2989                         if (req->kern_args == NULL) {
2990                                 req->status = CTL_LUN_ERROR;
2991                                 break;
2992                         }
2993                 }
2994
2995                 if (fe->ioctl)
2996                         retval = fe->ioctl(dev, cmd, addr, flag, td);
2997                 else
2998                         retval = ENODEV;
2999
3000                 if (req->num_args > 0) {
3001                         ctl_copyout_args(req->num_args, req->kern_args);
3002                         ctl_free_args(req->num_args, req->kern_args);
3003                 }
3004                 break;
3005         }
3006         case CTL_PORT_LIST: {
3007                 struct sbuf *sb;
3008                 struct ctl_port *port;
3009                 struct ctl_lun_list *list;
3010                 struct ctl_option *opt;
3011                 int j;
3012                 uint32_t plun;
3013
3014                 list = (struct ctl_lun_list *)addr;
3015
3016                 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3017                 if (sb == NULL) {
3018                         list->status = CTL_LUN_LIST_ERROR;
3019                         snprintf(list->error_str, sizeof(list->error_str),
3020                                  "Unable to allocate %d bytes for LUN list",
3021                                  list->alloc_len);
3022                         break;
3023                 }
3024
3025                 sbuf_printf(sb, "<ctlportlist>\n");
3026
3027                 mtx_lock(&softc->ctl_lock);
3028                 STAILQ_FOREACH(port, &softc->port_list, links) {
3029                         retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3030                                              (uintmax_t)port->targ_port);
3031
3032                         /*
3033                          * Bail out as soon as we see that we've overfilled
3034                          * the buffer.
3035                          */
3036                         if (retval != 0)
3037                                 break;
3038
3039                         retval = sbuf_printf(sb, "\t<frontend_type>%s"
3040                             "</frontend_type>\n", port->frontend->name);
3041                         if (retval != 0)
3042                                 break;
3043
3044                         retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3045                                              port->port_type);
3046                         if (retval != 0)
3047                                 break;
3048
3049                         retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3050                             (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3051                         if (retval != 0)
3052                                 break;
3053
3054                         retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3055                             port->port_name);
3056                         if (retval != 0)
3057                                 break;
3058
3059                         retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3060                             port->physical_port);
3061                         if (retval != 0)
3062                                 break;
3063
3064                         retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3065                             port->virtual_port);
3066                         if (retval != 0)
3067                                 break;
3068
3069                         if (port->target_devid != NULL) {
3070                                 sbuf_printf(sb, "\t<target>");
3071                                 ctl_id_sbuf(port->target_devid, sb);
3072                                 sbuf_printf(sb, "</target>\n");
3073                         }
3074
3075                         if (port->port_devid != NULL) {
3076                                 sbuf_printf(sb, "\t<port>");
3077                                 ctl_id_sbuf(port->port_devid, sb);
3078                                 sbuf_printf(sb, "</port>\n");
3079                         }
3080
3081                         if (port->port_info != NULL) {
3082                                 retval = port->port_info(port->onoff_arg, sb);
3083                                 if (retval != 0)
3084                                         break;
3085                         }
3086                         STAILQ_FOREACH(opt, &port->options, links) {
3087                                 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3088                                     opt->name, opt->value, opt->name);
3089                                 if (retval != 0)
3090                                         break;
3091                         }
3092
3093                         if (port->lun_map != NULL) {
3094                                 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3095                                 for (j = 0; j < CTL_MAX_LUNS; j++) {
3096                                         plun = ctl_lun_map_from_port(port, j);
3097                                         if (plun >= CTL_MAX_LUNS)
3098                                                 continue;
3099                                         sbuf_printf(sb,
3100                                             "\t<lun id=\"%u\">%u</lun>\n",
3101                                             j, plun);
3102                                 }
3103                         }
3104
3105                         for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3106                                 if (port->wwpn_iid[j].in_use == 0 ||
3107                                     (port->wwpn_iid[j].wwpn == 0 &&
3108                                      port->wwpn_iid[j].name == NULL))
3109                                         continue;
3110
3111                                 if (port->wwpn_iid[j].name != NULL)
3112                                         retval = sbuf_printf(sb,
3113                                             "\t<initiator id=\"%u\">%s</initiator>\n",
3114                                             j, port->wwpn_iid[j].name);
3115                                 else
3116                                         retval = sbuf_printf(sb,
3117                                             "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3118                                             j, port->wwpn_iid[j].wwpn);
3119                                 if (retval != 0)
3120                                         break;
3121                         }
3122                         if (retval != 0)
3123                                 break;
3124
3125                         retval = sbuf_printf(sb, "</targ_port>\n");
3126                         if (retval != 0)
3127                                 break;
3128                 }
3129                 mtx_unlock(&softc->ctl_lock);
3130
3131                 if ((retval != 0)
3132                  || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3133                         retval = 0;
3134                         sbuf_delete(sb);
3135                         list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3136                         snprintf(list->error_str, sizeof(list->error_str),
3137                                  "Out of space, %d bytes is too small",
3138                                  list->alloc_len);
3139                         break;
3140                 }
3141
3142                 sbuf_finish(sb);
3143
3144                 retval = copyout(sbuf_data(sb), list->lun_xml,
3145                                  sbuf_len(sb) + 1);
3146
3147                 list->fill_len = sbuf_len(sb) + 1;
3148                 list->status = CTL_LUN_LIST_OK;
3149                 sbuf_delete(sb);
3150                 break;
3151         }
3152         case CTL_LUN_MAP: {
3153                 struct ctl_lun_map *lm  = (struct ctl_lun_map *)addr;
3154                 struct ctl_port *port;
3155
3156                 mtx_lock(&softc->ctl_lock);
3157                 if (lm->port < softc->port_min ||
3158                     lm->port >= softc->port_max ||
3159                     (port = softc->ctl_ports[lm->port]) == NULL) {
3160                         mtx_unlock(&softc->ctl_lock);
3161                         return (ENXIO);
3162                 }
3163                 if (port->status & CTL_PORT_STATUS_ONLINE) {
3164                         STAILQ_FOREACH(lun, &softc->lun_list, links) {
3165                                 if (ctl_lun_map_to_port(port, lun->lun) >=
3166                                     CTL_MAX_LUNS)
3167                                         continue;
3168                                 mtx_lock(&lun->lun_lock);
3169                                 ctl_est_ua_port(lun, lm->port, -1,
3170                                     CTL_UA_LUN_CHANGE);
3171                                 mtx_unlock(&lun->lun_lock);
3172                         }
3173                 }
3174                 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3175                 if (lm->plun < CTL_MAX_LUNS) {
3176                         if (lm->lun == UINT32_MAX)
3177                                 retval = ctl_lun_map_unset(port, lm->plun);
3178                         else if (lm->lun < CTL_MAX_LUNS &&
3179                             softc->ctl_luns[lm->lun] != NULL)
3180                                 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3181                         else
3182                                 return (ENXIO);
3183                 } else if (lm->plun == UINT32_MAX) {
3184                         if (lm->lun == UINT32_MAX)
3185                                 retval = ctl_lun_map_deinit(port);
3186                         else
3187                                 retval = ctl_lun_map_init(port);
3188                 } else
3189                         return (ENXIO);
3190                 if (port->status & CTL_PORT_STATUS_ONLINE)
3191                         ctl_isc_announce_port(port);
3192                 break;
3193         }
3194         default: {
3195                 /* XXX KDM should we fix this? */
3196 #if 0
3197                 struct ctl_backend_driver *backend;
3198                 unsigned int type;
3199                 int found;
3200
3201                 found = 0;
3202
3203                 /*
3204                  * We encode the backend type as the ioctl type for backend
3205                  * ioctls.  So parse it out here, and then search for a
3206                  * backend of this type.
3207                  */
3208                 type = _IOC_TYPE(cmd);
3209
3210                 STAILQ_FOREACH(backend, &softc->be_list, links) {
3211                         if (backend->type == type) {
3212                                 found = 1;
3213                                 break;
3214                         }
3215                 }
3216                 if (found == 0) {
3217                         printf("ctl: unknown ioctl command %#lx or backend "
3218                                "%d\n", cmd, type);
3219                         retval = EINVAL;
3220                         break;
3221                 }
3222                 retval = backend->ioctl(dev, cmd, addr, flag, td);
3223 #endif
3224                 retval = ENOTTY;
3225                 break;
3226         }
3227         }
3228         return (retval);
3229 }
3230
3231 uint32_t
3232 ctl_get_initindex(struct ctl_nexus *nexus)
3233 {
3234         return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3235 }
3236
3237 int
3238 ctl_lun_map_init(struct ctl_port *port)
3239 {
3240         struct ctl_softc *softc = control_softc;
3241         struct ctl_lun *lun;
3242         uint32_t i;
3243
3244         if (port->lun_map == NULL)
3245                 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3246                     M_CTL, M_NOWAIT);
3247         if (port->lun_map == NULL)
3248                 return (ENOMEM);
3249         for (i = 0; i < CTL_MAX_LUNS; i++)
3250                 port->lun_map[i] = UINT32_MAX;
3251         if (port->status & CTL_PORT_STATUS_ONLINE) {
3252                 if (port->lun_disable != NULL) {
3253                         STAILQ_FOREACH(lun, &softc->lun_list, links)
3254                                 port->lun_disable(port->targ_lun_arg, lun->lun);
3255                 }
3256                 ctl_isc_announce_port(port);
3257         }
3258         return (0);
3259 }
3260
3261 int
3262 ctl_lun_map_deinit(struct ctl_port *port)
3263 {
3264         struct ctl_softc *softc = control_softc;
3265         struct ctl_lun *lun;
3266
3267         if (port->lun_map == NULL)
3268                 return (0);
3269         free(port->lun_map, M_CTL);
3270         port->lun_map = NULL;
3271         if (port->status & CTL_PORT_STATUS_ONLINE) {
3272                 if (port->lun_enable != NULL) {
3273                         STAILQ_FOREACH(lun, &softc->lun_list, links)
3274                                 port->lun_enable(port->targ_lun_arg, lun->lun);
3275                 }
3276                 ctl_isc_announce_port(port);
3277         }
3278         return (0);
3279 }
3280
3281 int
3282 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3283 {
3284         int status;
3285         uint32_t old;
3286
3287         if (port->lun_map == NULL) {
3288                 status = ctl_lun_map_init(port);
3289                 if (status != 0)
3290                         return (status);
3291         }
3292         old = port->lun_map[plun];
3293         port->lun_map[plun] = glun;
3294         if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3295                 if (port->lun_enable != NULL)
3296                         port->lun_enable(port->targ_lun_arg, plun);
3297                 ctl_isc_announce_port(port);
3298         }
3299         return (0);
3300 }
3301
3302 int
3303 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3304 {
3305         uint32_t old;
3306
3307         if (port->lun_map == NULL)
3308                 return (0);
3309         old = port->lun_map[plun];
3310         port->lun_map[plun] = UINT32_MAX;
3311         if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3312                 if (port->lun_disable != NULL)
3313                         port->lun_disable(port->targ_lun_arg, plun);
3314                 ctl_isc_announce_port(port);
3315         }
3316         return (0);
3317 }
3318
3319 uint32_t
3320 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3321 {
3322
3323         if (port == NULL)
3324                 return (UINT32_MAX);
3325         if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3326                 return (lun_id);
3327         return (port->lun_map[lun_id]);
3328 }
3329
3330 uint32_t
3331 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3332 {
3333         uint32_t i;
3334
3335         if (port == NULL)
3336                 return (UINT32_MAX);
3337         if (port->lun_map == NULL)
3338                 return (lun_id);
3339         for (i = 0; i < CTL_MAX_LUNS; i++) {
3340                 if (port->lun_map[i] == lun_id)
3341                         return (i);
3342         }
3343         return (UINT32_MAX);
3344 }
3345
3346 static struct ctl_port *
3347 ctl_io_port(struct ctl_io_hdr *io_hdr)
3348 {
3349
3350         return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3351 }
3352
3353 int
3354 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3355 {
3356         int i;
3357
3358         for (i = first; i < last; i++) {
3359                 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3360                         return (i);
3361         }
3362         return (-1);
3363 }
3364
3365 int
3366 ctl_set_mask(uint32_t *mask, uint32_t bit)
3367 {
3368         uint32_t chunk, piece;
3369
3370         chunk = bit >> 5;
3371         piece = bit % (sizeof(uint32_t) * 8);
3372
3373         if ((mask[chunk] & (1 << piece)) != 0)
3374                 return (-1);
3375         else
3376                 mask[chunk] |= (1 << piece);
3377
3378         return (0);
3379 }
3380
3381 int
3382 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3383 {
3384         uint32_t chunk, piece;
3385
3386         chunk = bit >> 5;
3387         piece = bit % (sizeof(uint32_t) * 8);
3388
3389         if ((mask[chunk] & (1 << piece)) == 0)
3390                 return (-1);
3391         else
3392                 mask[chunk] &= ~(1 << piece);
3393
3394         return (0);
3395 }
3396
3397 int
3398 ctl_is_set(uint32_t *mask, uint32_t bit)
3399 {
3400         uint32_t chunk, piece;
3401
3402         chunk = bit >> 5;
3403         piece = bit % (sizeof(uint32_t) * 8);
3404
3405         if ((mask[chunk] & (1 << piece)) == 0)
3406                 return (0);
3407         else
3408                 return (1);
3409 }
3410
3411 static uint64_t
3412 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3413 {
3414         uint64_t *t;
3415
3416         t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3417         if (t == NULL)
3418                 return (0);
3419         return (t[residx % CTL_MAX_INIT_PER_PORT]);
3420 }
3421
3422 static void
3423 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3424 {
3425         uint64_t *t;
3426
3427         t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3428         if (t == NULL)
3429                 return;
3430         t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3431 }
3432
3433 static void
3434 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3435 {
3436         uint64_t *p;
3437         u_int i;
3438
3439         i = residx/CTL_MAX_INIT_PER_PORT;
3440         if (lun->pr_keys[i] != NULL)
3441                 return;
3442         mtx_unlock(&lun->lun_lock);
3443         p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3444             M_WAITOK | M_ZERO);
3445         mtx_lock(&lun->lun_lock);
3446         if (lun->pr_keys[i] == NULL)
3447                 lun->pr_keys[i] = p;
3448         else
3449                 free(p, M_CTL);
3450 }
3451
3452 static void
3453 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3454 {
3455         uint64_t *t;
3456
3457         t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3458         KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3459         t[residx % CTL_MAX_INIT_PER_PORT] = key;
3460 }
3461
3462 /*
3463  * ctl_softc, pool_name, total_ctl_io are passed in.
3464  * npool is passed out.
3465  */
3466 int
3467 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3468                 uint32_t total_ctl_io, void **npool)
3469 {
3470 #ifdef IO_POOLS
3471         struct ctl_io_pool *pool;
3472
3473         pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3474                                             M_NOWAIT | M_ZERO);
3475         if (pool == NULL)
3476                 return (ENOMEM);
3477
3478         snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3479         pool->ctl_softc = ctl_softc;
3480         pool->zone = uma_zsecond_create(pool->name, NULL,
3481             NULL, NULL, NULL, ctl_softc->io_zone);
3482         /* uma_prealloc(pool->zone, total_ctl_io); */
3483
3484         *npool = pool;
3485 #else
3486         *npool = ctl_softc->io_zone;
3487 #endif
3488         return (0);
3489 }
3490
3491 void
3492 ctl_pool_free(struct ctl_io_pool *pool)
3493 {
3494
3495         if (pool == NULL)
3496                 return;
3497
3498 #ifdef IO_POOLS
3499         uma_zdestroy(pool->zone);
3500         free(pool, M_CTL);
3501 #endif
3502 }
3503
3504 union ctl_io *
3505 ctl_alloc_io(void *pool_ref)
3506 {
3507         union ctl_io *io;
3508 #ifdef IO_POOLS
3509         struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3510
3511         io = uma_zalloc(pool->zone, M_WAITOK);
3512 #else
3513         io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3514 #endif
3515         if (io != NULL)
3516                 io->io_hdr.pool = pool_ref;
3517         return (io);
3518 }
3519
3520 union ctl_io *
3521 ctl_alloc_io_nowait(void *pool_ref)
3522 {
3523         union ctl_io *io;
3524 #ifdef IO_POOLS
3525         struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3526
3527         io = uma_zalloc(pool->zone, M_NOWAIT);
3528 #else
3529         io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3530 #endif
3531         if (io != NULL)
3532                 io->io_hdr.pool = pool_ref;
3533         return (io);
3534 }
3535
3536 void
3537 ctl_free_io(union ctl_io *io)
3538 {
3539 #ifdef IO_POOLS
3540         struct ctl_io_pool *pool;
3541 #endif
3542
3543         if (io == NULL)
3544                 return;
3545
3546 #ifdef IO_POOLS
3547         pool = (struct ctl_io_pool *)io->io_hdr.pool;
3548         uma_zfree(pool->zone, io);
3549 #else
3550         uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3551 #endif
3552 }
3553
3554 void
3555 ctl_zero_io(union ctl_io *io)
3556 {
3557         void *pool_ref;
3558
3559         if (io == NULL)
3560                 return;
3561
3562         /*
3563          * May need to preserve linked list pointers at some point too.
3564          */
3565         pool_ref = io->io_hdr.pool;
3566         memset(io, 0, sizeof(*io));
3567         io->io_hdr.pool = pool_ref;
3568 }
3569
3570 /*
3571  * This routine is currently used for internal copies of ctl_ios that need
3572  * to persist for some reason after we've already returned status to the
3573  * FETD.  (Thus the flag set.)
3574  *
3575  * XXX XXX
3576  * Note that this makes a blind copy of all fields in the ctl_io, except
3577  * for the pool reference.  This includes any memory that has been
3578  * allocated!  That memory will no longer be valid after done has been
3579  * called, so this would be VERY DANGEROUS for command that actually does
3580  * any reads or writes.  Right now (11/7/2005), this is only used for immediate
3581  * start and stop commands, which don't transfer any data, so this is not a
3582  * problem.  If it is used for anything else, the caller would also need to
3583  * allocate data buffer space and this routine would need to be modified to
3584  * copy the data buffer(s) as well.
3585  */
3586 void
3587 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3588 {
3589         void *pool_ref;
3590
3591         if ((src == NULL)
3592          || (dest == NULL))
3593                 return;
3594
3595         /*
3596          * May need to preserve linked list pointers at some point too.
3597          */
3598         pool_ref = dest->io_hdr.pool;
3599
3600         memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3601
3602         dest->io_hdr.pool = pool_ref;
3603         /*
3604          * We need to know that this is an internal copy, and doesn't need
3605          * to get passed back to the FETD that allocated it.
3606          */
3607         dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3608 }
3609
3610 int
3611 ctl_expand_number(const char *buf, uint64_t *num)
3612 {
3613         char *endptr;
3614         uint64_t number;
3615         unsigned shift;
3616
3617         number = strtoq(buf, &endptr, 0);
3618
3619         switch (tolower((unsigned char)*endptr)) {
3620         case 'e':
3621                 shift = 60;
3622                 break;
3623         case 'p':
3624                 shift = 50;
3625                 break;
3626         case 't':
3627                 shift = 40;
3628                 break;
3629         case 'g':
3630                 shift = 30;
3631                 break;
3632         case 'm':
3633                 shift = 20;
3634                 break;
3635         case 'k':
3636                 shift = 10;
3637                 break;
3638         case 'b':
3639         case '\0': /* No unit. */
3640                 *num = number;
3641                 return (0);
3642         default:
3643                 /* Unrecognized unit. */
3644                 return (-1);
3645         }
3646
3647         if ((number << shift) >> shift != number) {
3648                 /* Overflow */
3649                 return (-1);
3650         }
3651         *num = number << shift;
3652         return (0);
3653 }
3654
3655
3656 /*
3657  * This routine could be used in the future to load default and/or saved
3658  * mode page parameters for a particuar lun.
3659  */
3660 static int
3661 ctl_init_page_index(struct ctl_lun *lun)
3662 {
3663         int i;
3664         struct ctl_page_index *page_index;
3665         const char *value;
3666         uint64_t ival;
3667
3668         memcpy(&lun->mode_pages.index, page_index_template,
3669                sizeof(page_index_template));
3670
3671         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3672
3673                 page_index = &lun->mode_pages.index[i];
3674                 /*
3675                  * If this is a disk-only mode page, there's no point in
3676                  * setting it up.  For some pages, we have to have some
3677                  * basic information about the disk in order to calculate the
3678                  * mode page data.
3679                  */
3680                 if ((lun->be_lun->lun_type != T_DIRECT)
3681                  && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3682                         continue;
3683
3684                 switch (page_index->page_code & SMPH_PC_MASK) {
3685                 case SMS_RW_ERROR_RECOVERY_PAGE: {
3686                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3687                                 panic("subpage is incorrect!");
3688                         memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3689                                &rw_er_page_default,
3690                                sizeof(rw_er_page_default));
3691                         memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3692                                &rw_er_page_changeable,
3693                                sizeof(rw_er_page_changeable));
3694                         memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3695                                &rw_er_page_default,
3696                                sizeof(rw_er_page_default));
3697                         memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3698                                &rw_er_page_default,
3699                                sizeof(rw_er_page_default));
3700                         page_index->page_data =
3701                                 (uint8_t *)lun->mode_pages.rw_er_page;
3702                         break;
3703                 }
3704                 case SMS_FORMAT_DEVICE_PAGE: {
3705                         struct scsi_format_page *format_page;
3706
3707                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3708                                 panic("subpage is incorrect!");
3709
3710                         /*
3711                          * Sectors per track are set above.  Bytes per
3712                          * sector need to be set here on a per-LUN basis.
3713                          */
3714                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3715                                &format_page_default,
3716                                sizeof(format_page_default));
3717                         memcpy(&lun->mode_pages.format_page[
3718                                CTL_PAGE_CHANGEABLE], &format_page_changeable,
3719                                sizeof(format_page_changeable));
3720                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3721                                &format_page_default,
3722                                sizeof(format_page_default));
3723                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3724                                &format_page_default,
3725                                sizeof(format_page_default));
3726
3727                         format_page = &lun->mode_pages.format_page[
3728                                 CTL_PAGE_CURRENT];
3729                         scsi_ulto2b(lun->be_lun->blocksize,
3730                                     format_page->bytes_per_sector);
3731
3732                         format_page = &lun->mode_pages.format_page[
3733                                 CTL_PAGE_DEFAULT];
3734                         scsi_ulto2b(lun->be_lun->blocksize,
3735                                     format_page->bytes_per_sector);
3736
3737                         format_page = &lun->mode_pages.format_page[
3738                                 CTL_PAGE_SAVED];
3739                         scsi_ulto2b(lun->be_lun->blocksize,
3740                                     format_page->bytes_per_sector);
3741
3742                         page_index->page_data =
3743                                 (uint8_t *)lun->mode_pages.format_page;
3744                         break;
3745                 }
3746                 case SMS_RIGID_DISK_PAGE: {
3747                         struct scsi_rigid_disk_page *rigid_disk_page;
3748                         uint32_t sectors_per_cylinder;
3749                         uint64_t cylinders;
3750 #ifndef __XSCALE__
3751                         int shift;
3752 #endif /* !__XSCALE__ */
3753
3754                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3755                                 panic("invalid subpage value %d",
3756                                       page_index->subpage);
3757
3758                         /*
3759                          * Rotation rate and sectors per track are set
3760                          * above.  We calculate the cylinders here based on
3761                          * capacity.  Due to the number of heads and
3762                          * sectors per track we're using, smaller arrays
3763                          * may turn out to have 0 cylinders.  Linux and
3764                          * FreeBSD don't pay attention to these mode pages
3765                          * to figure out capacity, but Solaris does.  It
3766                          * seems to deal with 0 cylinders just fine, and
3767                          * works out a fake geometry based on the capacity.
3768                          */
3769                         memcpy(&lun->mode_pages.rigid_disk_page[
3770                                CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3771                                sizeof(rigid_disk_page_default));
3772                         memcpy(&lun->mode_pages.rigid_disk_page[
3773                                CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3774                                sizeof(rigid_disk_page_changeable));
3775
3776                         sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3777                                 CTL_DEFAULT_HEADS;
3778
3779                         /*
3780                          * The divide method here will be more accurate,
3781                          * probably, but results in floating point being
3782                          * used in the kernel on i386 (__udivdi3()).  On the
3783                          * XScale, though, __udivdi3() is implemented in
3784                          * software.
3785                          *
3786                          * The shift method for cylinder calculation is
3787                          * accurate if sectors_per_cylinder is a power of
3788                          * 2.  Otherwise it might be slightly off -- you
3789                          * might have a bit of a truncation problem.
3790                          */
3791 #ifdef  __XSCALE__
3792                         cylinders = (lun->be_lun->maxlba + 1) /
3793                                 sectors_per_cylinder;
3794 #else
3795                         for (shift = 31; shift > 0; shift--) {
3796                                 if (sectors_per_cylinder & (1 << shift))
3797                                         break;
3798                         }
3799                         cylinders = (lun->be_lun->maxlba + 1) >> shift;
3800 #endif
3801
3802                         /*
3803                          * We've basically got 3 bytes, or 24 bits for the
3804                          * cylinder size in the mode page.  If we're over,
3805                          * just round down to 2^24.
3806                          */
3807                         if (cylinders > 0xffffff)
3808                                 cylinders = 0xffffff;
3809
3810                         rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3811                                 CTL_PAGE_DEFAULT];
3812                         scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3813
3814                         if ((value = ctl_get_opt(&lun->be_lun->options,
3815                             "rpm")) != NULL) {
3816                                 scsi_ulto2b(strtol(value, NULL, 0),
3817                                      rigid_disk_page->rotation_rate);
3818                         }
3819
3820                         memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
3821                                &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3822                                sizeof(rigid_disk_page_default));
3823                         memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
3824                                &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3825                                sizeof(rigid_disk_page_default));
3826
3827                         page_index->page_data =
3828                                 (uint8_t *)lun->mode_pages.rigid_disk_page;
3829                         break;
3830                 }
3831                 case SMS_CACHING_PAGE: {
3832                         struct scsi_caching_page *caching_page;
3833
3834                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3835                                 panic("invalid subpage value %d",
3836                                       page_index->subpage);
3837                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3838                                &caching_page_default,
3839                                sizeof(caching_page_default));
3840                         memcpy(&lun->mode_pages.caching_page[
3841                                CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3842                                sizeof(caching_page_changeable));
3843                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3844                                &caching_page_default,
3845                                sizeof(caching_page_default));
3846                         caching_page = &lun->mode_pages.caching_page[
3847                             CTL_PAGE_SAVED];
3848                         value = ctl_get_opt(&lun->be_lun->options, "writecache");
3849                         if (value != NULL && strcmp(value, "off") == 0)
3850                                 caching_page->flags1 &= ~SCP_WCE;
3851                         value = ctl_get_opt(&lun->be_lun->options, "readcache");
3852                         if (value != NULL && strcmp(value, "off") == 0)
3853                                 caching_page->flags1 |= SCP_RCD;
3854                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3855                                &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3856                                sizeof(caching_page_default));
3857                         page_index->page_data =
3858                                 (uint8_t *)lun->mode_pages.caching_page;
3859                         break;
3860                 }
3861                 case SMS_CONTROL_MODE_PAGE: {
3862                         struct scsi_control_page *control_page;
3863
3864                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3865                                 panic("invalid subpage value %d",
3866                                       page_index->subpage);
3867
3868                         memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
3869                                &control_page_default,
3870                                sizeof(control_page_default));
3871                         memcpy(&lun->mode_pages.control_page[
3872                                CTL_PAGE_CHANGEABLE], &control_page_changeable,
3873                                sizeof(control_page_changeable));
3874                         memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
3875                                &control_page_default,
3876                                sizeof(control_page_default));
3877                         control_page = &lun->mode_pages.control_page[
3878                             CTL_PAGE_SAVED];
3879                         value = ctl_get_opt(&lun->be_lun->options, "reordering");
3880                         if (value != NULL && strcmp(value, "unrestricted") == 0) {
3881                                 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
3882                                 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
3883                         }
3884                         memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
3885                                &lun->mode_pages.control_page[CTL_PAGE_SAVED],
3886                                sizeof(control_page_default));
3887                         page_index->page_data =
3888                                 (uint8_t *)lun->mode_pages.control_page;
3889                         break;
3890
3891                 }
3892                 case SMS_INFO_EXCEPTIONS_PAGE: {
3893                         switch (page_index->subpage) {
3894                         case SMS_SUBPAGE_PAGE_0:
3895                                 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
3896                                        &ie_page_default,
3897                                        sizeof(ie_page_default));
3898                                 memcpy(&lun->mode_pages.ie_page[
3899                                        CTL_PAGE_CHANGEABLE], &ie_page_changeable,
3900                                        sizeof(ie_page_changeable));
3901                                 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
3902                                        &ie_page_default,
3903                                        sizeof(ie_page_default));
3904                                 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
3905                                        &ie_page_default,
3906                                        sizeof(ie_page_default));
3907                                 page_index->page_data =
3908                                         (uint8_t *)lun->mode_pages.ie_page;
3909                                 break;
3910                         case 0x02: {
3911                                 struct ctl_logical_block_provisioning_page *page;
3912
3913                                 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
3914                                        &lbp_page_default,
3915                                        sizeof(lbp_page_default));
3916                                 memcpy(&lun->mode_pages.lbp_page[
3917                                        CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
3918                                        sizeof(lbp_page_changeable));
3919                                 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
3920                                        &lbp_page_default,
3921                                        sizeof(lbp_page_default));
3922                                 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
3923                                 value = ctl_get_opt(&lun->be_lun->options,
3924                                     "avail-threshold");
3925                                 if (value != NULL &&
3926                                     ctl_expand_number(value, &ival) == 0) {
3927                                         page->descr[0].flags |= SLBPPD_ENABLED |
3928                                             SLBPPD_ARMING_DEC;
3929                                         if (lun->be_lun->blocksize)
3930                                                 ival /= lun->be_lun->blocksize;
3931                                         else
3932                                                 ival /= 512;
3933                                         scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3934                                             page->descr[0].count);
3935                                 }
3936                                 value = ctl_get_opt(&lun->be_lun->options,
3937                                     "used-threshold");
3938                                 if (value != NULL &&
3939                                     ctl_expand_number(value, &ival) == 0) {
3940                                         page->descr[1].flags |= SLBPPD_ENABLED |
3941                                             SLBPPD_ARMING_INC;
3942                                         if (lun->be_lun->blocksize)
3943                                                 ival /= lun->be_lun->blocksize;
3944                                         else
3945                                                 ival /= 512;
3946                                         scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3947                                             page->descr[1].count);
3948                                 }
3949                                 value = ctl_get_opt(&lun->be_lun->options,
3950                                     "pool-avail-threshold");
3951                                 if (value != NULL &&
3952                                     ctl_expand_number(value, &ival) == 0) {
3953                                         page->descr[2].flags |= SLBPPD_ENABLED |
3954                                             SLBPPD_ARMING_DEC;
3955                                         if (lun->be_lun->blocksize)
3956                                                 ival /= lun->be_lun->blocksize;
3957                                         else
3958                                                 ival /= 512;
3959                                         scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3960                                             page->descr[2].count);
3961                                 }
3962                                 value = ctl_get_opt(&lun->be_lun->options,
3963                                     "pool-used-threshold");
3964                                 if (value != NULL &&
3965                                     ctl_expand_number(value, &ival) == 0) {
3966                                         page->descr[3].flags |= SLBPPD_ENABLED |
3967                                             SLBPPD_ARMING_INC;
3968                                         if (lun->be_lun->blocksize)
3969                                                 ival /= lun->be_lun->blocksize;
3970                                         else
3971                                                 ival /= 512;
3972                                         scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3973                                             page->descr[3].count);
3974                                 }
3975                                 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
3976                                        &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
3977                                        sizeof(lbp_page_default));
3978                                 page_index->page_data =
3979                                         (uint8_t *)lun->mode_pages.lbp_page;
3980                         }}
3981                         break;
3982                 }
3983                 case SMS_VENDOR_SPECIFIC_PAGE:{
3984                         switch (page_index->subpage) {
3985                         case DBGCNF_SUBPAGE_CODE: {
3986                                 struct copan_debugconf_subpage *current_page,
3987                                                                *saved_page;
3988
3989                                 memcpy(&lun->mode_pages.debugconf_subpage[
3990                                        CTL_PAGE_CURRENT],
3991                                        &debugconf_page_default,
3992                                        sizeof(debugconf_page_default));
3993                                 memcpy(&lun->mode_pages.debugconf_subpage[
3994                                        CTL_PAGE_CHANGEABLE],
3995                                        &debugconf_page_changeable,
3996                                        sizeof(debugconf_page_changeable));
3997                                 memcpy(&lun->mode_pages.debugconf_subpage[
3998                                        CTL_PAGE_DEFAULT],
3999                                        &debugconf_page_default,
4000                                        sizeof(debugconf_page_default));
4001                                 memcpy(&lun->mode_pages.debugconf_subpage[
4002                                        CTL_PAGE_SAVED],
4003                                        &debugconf_page_default,
4004                                        sizeof(debugconf_page_default));
4005                                 page_index->page_data =
4006                                         (uint8_t *)lun->mode_pages.debugconf_subpage;
4007
4008                                 current_page = (struct copan_debugconf_subpage *)
4009                                         (page_index->page_data +
4010                                          (page_index->page_len *
4011                                           CTL_PAGE_CURRENT));
4012                                 saved_page = (struct copan_debugconf_subpage *)
4013                                         (page_index->page_data +
4014                                          (page_index->page_len *
4015                                           CTL_PAGE_SAVED));
4016                                 break;
4017                         }
4018                         default:
4019                                 panic("invalid subpage value %d",
4020                                       page_index->subpage);
4021                                 break;
4022                         }
4023                         break;
4024                 }
4025                 default:
4026                         panic("invalid page value %d",
4027                               page_index->page_code & SMPH_PC_MASK);
4028                         break;
4029         }
4030         }
4031
4032         return (CTL_RETVAL_COMPLETE);
4033 }
4034
4035 static int
4036 ctl_init_log_page_index(struct ctl_lun *lun)
4037 {
4038         struct ctl_page_index *page_index;
4039         int i, j, k, prev;
4040
4041         memcpy(&lun->log_pages.index, log_page_index_template,
4042                sizeof(log_page_index_template));
4043
4044         prev = -1;
4045         for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4046
4047                 page_index = &lun->log_pages.index[i];
4048                 /*
4049                  * If this is a disk-only mode page, there's no point in
4050                  * setting it up.  For some pages, we have to have some
4051                  * basic information about the disk in order to calculate the
4052                  * mode page data.
4053                  */
4054                 if ((lun->be_lun->lun_type != T_DIRECT)
4055                  && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4056                         continue;
4057
4058                 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4059                      lun->backend->lun_attr == NULL)
4060                         continue;
4061
4062                 if (page_index->page_code != prev) {
4063                         lun->log_pages.pages_page[j] = page_index->page_code;
4064                         prev = page_index->page_code;
4065                         j++;
4066                 }
4067                 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4068                 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4069                 k++;
4070         }
4071         lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4072         lun->log_pages.index[0].page_len = j;
4073         lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4074         lun->log_pages.index[1].page_len = k * 2;
4075         lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4076         lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4077         lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4078         lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4079
4080         return (CTL_RETVAL_COMPLETE);
4081 }
4082
4083 static int
4084 hex2bin(const char *str, uint8_t *buf, int buf_size)
4085 {
4086         int i;
4087         u_char c;
4088
4089         memset(buf, 0, buf_size);
4090         while (isspace(str[0]))
4091                 str++;
4092         if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4093                 str += 2;
4094         buf_size *= 2;
4095         for (i = 0; str[i] != 0 && i < buf_size; i++) {
4096                 c = str[i];
4097                 if (isdigit(c))
4098                         c -= '0';
4099                 else if (isalpha(c))
4100                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4101                 else
4102                         break;
4103                 if (c >= 16)
4104                         break;
4105                 if ((i & 1) == 0)
4106                         buf[i / 2] |= (c << 4);
4107                 else
4108                         buf[i / 2] |= c;
4109         }
4110         return ((i + 1) / 2);
4111 }
4112
4113 /*
4114  * LUN allocation.
4115  *
4116  * Requirements:
4117  * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4118  *   wants us to allocate the LUN and he can block.
4119  * - ctl_softc is always set
4120  * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4121  *
4122  * Returns 0 for success, non-zero (errno) for failure.
4123  */
4124 static int
4125 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4126               struct ctl_be_lun *const be_lun)
4127 {
4128         struct ctl_lun *nlun, *lun;
4129         struct scsi_vpd_id_descriptor *desc;
4130         struct scsi_vpd_id_t10 *t10id;
4131         const char *eui, *naa, *scsiname, *vendor;
4132         int lun_number, i, lun_malloced;
4133         int devidlen, idlen1, idlen2 = 0, len;
4134
4135         if (be_lun == NULL)
4136                 return (EINVAL);
4137
4138         /*
4139          * We currently only support Direct Access or Processor LUN types.
4140          */
4141         switch (be_lun->lun_type) {
4142         case T_DIRECT:
4143                 break;
4144         case T_PROCESSOR:
4145                 break;
4146         case T_SEQUENTIAL:
4147         case T_CHANGER:
4148         default:
4149                 be_lun->lun_config_status(be_lun->be_lun,
4150                                           CTL_LUN_CONFIG_FAILURE);
4151                 break;
4152         }
4153         if (ctl_lun == NULL) {
4154                 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4155                 lun_malloced = 1;
4156         } else {
4157                 lun_malloced = 0;
4158                 lun = ctl_lun;
4159         }
4160
4161         memset(lun, 0, sizeof(*lun));
4162         if (lun_malloced)
4163                 lun->flags = CTL_LUN_MALLOCED;
4164
4165         /* Generate LUN ID. */
4166         devidlen = max(CTL_DEVID_MIN_LEN,
4167             strnlen(be_lun->device_id, CTL_DEVID_LEN));
4168         idlen1 = sizeof(*t10id) + devidlen;
4169         len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4170         scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4171         if (scsiname != NULL) {
4172                 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4173                 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4174         }
4175         eui = ctl_get_opt(&be_lun->options, "eui");
4176         if (eui != NULL) {
4177                 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4178         }
4179         naa = ctl_get_opt(&be_lun->options, "naa");
4180         if (naa != NULL) {
4181                 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4182         }
4183         lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4184             M_CTL, M_WAITOK | M_ZERO);
4185         desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4186         desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4187         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4188         desc->length = idlen1;
4189         t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4190         memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4191         if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4192                 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4193         } else {
4194                 strncpy(t10id->vendor, vendor,
4195                     min(sizeof(t10id->vendor), strlen(vendor)));
4196         }
4197         strncpy((char *)t10id->vendor_spec_id,
4198             (char *)be_lun->device_id, devidlen);
4199         if (scsiname != NULL) {
4200                 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4201                     desc->length);
4202                 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4203                 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4204                     SVPD_ID_TYPE_SCSI_NAME;
4205                 desc->length = idlen2;
4206                 strlcpy(desc->identifier, scsiname, idlen2);
4207         }
4208         if (eui != NULL) {
4209                 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4210                     desc->length);
4211                 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4212                 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4213                     SVPD_ID_TYPE_EUI64;
4214                 desc->length = hex2bin(eui, desc->identifier, 16);
4215                 desc->length = desc->length > 12 ? 16 :
4216                     (desc->length > 8 ? 12 : 8);
4217                 len -= 16 - desc->length;
4218         }
4219         if (naa != NULL) {
4220                 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4221                     desc->length);
4222                 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4223                 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4224                     SVPD_ID_TYPE_NAA;
4225                 desc->length = hex2bin(naa, desc->identifier, 16);
4226                 desc->length = desc->length > 8 ? 16 : 8;
4227                 len -= 16 - desc->length;
4228         }
4229         lun->lun_devid->len = len;
4230
4231         mtx_lock(&ctl_softc->ctl_lock);
4232         /*
4233          * See if the caller requested a particular LUN number.  If so, see
4234          * if it is available.  Otherwise, allocate the first available LUN.
4235          */
4236         if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4237                 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4238                  || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4239                         mtx_unlock(&ctl_softc->ctl_lock);
4240                         if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4241                                 printf("ctl: requested LUN ID %d is higher "
4242                                        "than CTL_MAX_LUNS - 1 (%d)\n",
4243                                        be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4244                         } else {
4245                                 /*
4246                                  * XXX KDM return an error, or just assign
4247                                  * another LUN ID in this case??
4248                                  */
4249                                 printf("ctl: requested LUN ID %d is already "
4250                                        "in use\n", be_lun->req_lun_id);
4251                         }
4252                         if (lun->flags & CTL_LUN_MALLOCED)
4253                                 free(lun, M_CTL);
4254                         be_lun->lun_config_status(be_lun->be_lun,
4255                                                   CTL_LUN_CONFIG_FAILURE);
4256                         return (ENOSPC);
4257                 }
4258                 lun_number = be_lun->req_lun_id;
4259         } else {
4260                 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4261                 if (lun_number == -1) {
4262                         mtx_unlock(&ctl_softc->ctl_lock);
4263                         printf("ctl: can't allocate LUN, out of LUNs\n");
4264                         if (lun->flags & CTL_LUN_MALLOCED)
4265                                 free(lun, M_CTL);
4266                         be_lun->lun_config_status(be_lun->be_lun,
4267                                                   CTL_LUN_CONFIG_FAILURE);
4268                         return (ENOSPC);
4269                 }
4270         }
4271         ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4272
4273         mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4274         lun->lun = lun_number;
4275         lun->be_lun = be_lun;
4276         /*
4277          * The processor LUN is always enabled.  Disk LUNs come on line
4278          * disabled, and must be enabled by the backend.
4279          */
4280         lun->flags |= CTL_LUN_DISABLED;
4281         lun->backend = be_lun->be;
4282         be_lun->ctl_lun = lun;
4283         be_lun->lun_id = lun_number;
4284         atomic_add_int(&be_lun->be->num_luns, 1);
4285         if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4286                 lun->flags |= CTL_LUN_OFFLINE;
4287
4288         if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4289                 lun->flags |= CTL_LUN_STOPPED;
4290
4291         if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4292                 lun->flags |= CTL_LUN_INOPERABLE;
4293
4294         if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4295                 lun->flags |= CTL_LUN_PRIMARY_SC;
4296
4297         lun->ctl_softc = ctl_softc;
4298 #ifdef CTL_TIME_IO
4299         lun->last_busy = getsbinuptime();
4300 #endif
4301         TAILQ_INIT(&lun->ooa_queue);
4302         TAILQ_INIT(&lun->blocked_queue);
4303         STAILQ_INIT(&lun->error_list);
4304         ctl_tpc_lun_init(lun);
4305
4306         /*
4307          * Initialize the mode and log page index.
4308          */
4309         ctl_init_page_index(lun);
4310         ctl_init_log_page_index(lun);
4311
4312         /*
4313          * Now, before we insert this lun on the lun list, set the lun
4314          * inventory changed UA for all other luns.
4315          */
4316         STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4317                 mtx_lock(&nlun->lun_lock);
4318                 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4319                 mtx_unlock(&nlun->lun_lock);
4320         }
4321
4322         STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4323
4324         ctl_softc->ctl_luns[lun_number] = lun;
4325
4326         ctl_softc->num_luns++;
4327
4328         /* Setup statistics gathering */
4329         lun->stats.device_type = be_lun->lun_type;
4330         lun->stats.lun_number = lun_number;
4331         if (lun->stats.device_type == T_DIRECT)
4332                 lun->stats.blocksize = be_lun->blocksize;
4333         else
4334                 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4335         for (i = 0;i < CTL_MAX_PORTS;i++)
4336                 lun->stats.ports[i].targ_port = i;
4337
4338         mtx_unlock(&ctl_softc->ctl_lock);
4339
4340         lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4341         return (0);
4342 }
4343
4344 /*
4345  * Delete a LUN.
4346  * Assumptions:
4347  * - LUN has already been marked invalid and any pending I/O has been taken
4348  *   care of.
4349  */
4350 static int
4351 ctl_free_lun(struct ctl_lun *lun)
4352 {
4353         struct ctl_softc *softc;
4354         struct ctl_lun *nlun;
4355         int i;
4356
4357         softc = lun->ctl_softc;
4358
4359         mtx_assert(&softc->ctl_lock, MA_OWNED);
4360
4361         STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4362
4363         ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4364
4365         softc->ctl_luns[lun->lun] = NULL;
4366
4367         if (!TAILQ_EMPTY(&lun->ooa_queue))
4368                 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4369
4370         softc->num_luns--;
4371
4372         /*
4373          * Tell the backend to free resources, if this LUN has a backend.
4374          */
4375         atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4376         lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4377
4378         ctl_tpc_lun_shutdown(lun);
4379         mtx_destroy(&lun->lun_lock);
4380         free(lun->lun_devid, M_CTL);
4381         for (i = 0; i < CTL_MAX_PORTS; i++)
4382                 free(lun->pending_ua[i], M_CTL);
4383         for (i = 0; i < CTL_MAX_PORTS; i++)
4384                 free(lun->pr_keys[i], M_CTL);
4385         free(lun->write_buffer, M_CTL);
4386         if (lun->flags & CTL_LUN_MALLOCED)
4387                 free(lun, M_CTL);
4388
4389         STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4390                 mtx_lock(&nlun->lun_lock);
4391                 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4392                 mtx_unlock(&nlun->lun_lock);
4393         }
4394
4395         return (0);
4396 }
4397
4398 static void
4399 ctl_create_lun(struct ctl_be_lun *be_lun)
4400 {
4401         struct ctl_softc *softc;
4402
4403         softc = control_softc;
4404
4405         /*
4406          * ctl_alloc_lun() should handle all potential failure cases.
4407          */
4408         ctl_alloc_lun(softc, NULL, be_lun);
4409 }
4410
4411 int
4412 ctl_add_lun(struct ctl_be_lun *be_lun)
4413 {
4414         struct ctl_softc *softc = control_softc;
4415
4416         mtx_lock(&softc->ctl_lock);
4417         STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4418         mtx_unlock(&softc->ctl_lock);
4419         wakeup(&softc->pending_lun_queue);
4420
4421         return (0);
4422 }
4423
4424 int
4425 ctl_enable_lun(struct ctl_be_lun *be_lun)
4426 {
4427         struct ctl_softc *softc;
4428         struct ctl_port *port, *nport;
4429         struct ctl_lun *lun;
4430         int retval;
4431
4432         lun = (struct ctl_lun *)be_lun->ctl_lun;
4433         softc = lun->ctl_softc;
4434
4435         mtx_lock(&softc->ctl_lock);
4436         mtx_lock(&lun->lun_lock);
4437         if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4438                 /*
4439                  * eh?  Why did we get called if the LUN is already
4440                  * enabled?
4441                  */
4442                 mtx_unlock(&lun->lun_lock);
4443                 mtx_unlock(&softc->ctl_lock);
4444                 return (0);
4445         }
4446         lun->flags &= ~CTL_LUN_DISABLED;
4447         mtx_unlock(&lun->lun_lock);
4448
4449         for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4450                 nport = STAILQ_NEXT(port, links);
4451                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4452                     port->lun_map != NULL || port->lun_enable == NULL)
4453                         continue;
4454
4455                 /*
4456                  * Drop the lock while we call the FETD's enable routine.
4457                  * This can lead to a callback into CTL (at least in the
4458                  * case of the internal initiator frontend.
4459                  */
4460                 mtx_unlock(&softc->ctl_lock);
4461                 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4462                 mtx_lock(&softc->ctl_lock);
4463                 if (retval != 0) {
4464                         printf("%s: FETD %s port %d returned error "
4465                                "%d for lun_enable on lun %jd\n",
4466                                __func__, port->port_name, port->targ_port,
4467                                retval, (intmax_t)lun->lun);
4468                 }
4469         }
4470
4471         mtx_unlock(&softc->ctl_lock);
4472         ctl_isc_announce_lun(lun);
4473
4474         return (0);
4475 }
4476
4477 int
4478 ctl_disable_lun(struct ctl_be_lun *be_lun)
4479 {
4480         struct ctl_softc *softc;
4481         struct ctl_port *port;
4482         struct ctl_lun *lun;
4483         int retval;
4484
4485         lun = (struct ctl_lun *)be_lun->ctl_lun;
4486         softc = lun->ctl_softc;
4487
4488         mtx_lock(&softc->ctl_lock);
4489         mtx_lock(&lun->lun_lock);
4490         if (lun->flags & CTL_LUN_DISABLED) {
4491                 mtx_unlock(&lun->lun_lock);
4492                 mtx_unlock(&softc->ctl_lock);
4493                 return (0);
4494         }
4495         lun->flags |= CTL_LUN_DISABLED;
4496         mtx_unlock(&lun->lun_lock);
4497
4498         STAILQ_FOREACH(port, &softc->port_list, links) {
4499                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4500                     port->lun_map != NULL || port->lun_disable == NULL)
4501                         continue;
4502
4503                 /*
4504                  * Drop the lock before we call the frontend's disable
4505                  * routine, to avoid lock order reversals.
4506                  *
4507                  * XXX KDM what happens if the frontend list changes while
4508                  * we're traversing it?  It's unlikely, but should be handled.
4509                  */
4510                 mtx_unlock(&softc->ctl_lock);
4511                 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4512                 mtx_lock(&softc->ctl_lock);
4513                 if (retval != 0) {
4514                         printf("%s: FETD %s port %d returned error "
4515                                "%d for lun_disable on lun %jd\n",
4516                                __func__, port->port_name, port->targ_port,
4517                                retval, (intmax_t)lun->lun);
4518                 }
4519         }
4520
4521         mtx_unlock(&softc->ctl_lock);
4522         ctl_isc_announce_lun(lun);
4523
4524         return (0);
4525 }
4526
4527 int
4528 ctl_start_lun(struct ctl_be_lun *be_lun)
4529 {
4530         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4531
4532         mtx_lock(&lun->lun_lock);
4533         lun->flags &= ~CTL_LUN_STOPPED;
4534         mtx_unlock(&lun->lun_lock);
4535         return (0);
4536 }
4537
4538 int
4539 ctl_stop_lun(struct ctl_be_lun *be_lun)
4540 {
4541         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4542
4543         mtx_lock(&lun->lun_lock);
4544         lun->flags |= CTL_LUN_STOPPED;
4545         mtx_unlock(&lun->lun_lock);
4546         return (0);
4547 }
4548
4549 int
4550 ctl_lun_offline(struct ctl_be_lun *be_lun)
4551 {
4552         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4553
4554         mtx_lock(&lun->lun_lock);
4555         lun->flags |= CTL_LUN_OFFLINE;
4556         mtx_unlock(&lun->lun_lock);
4557         return (0);
4558 }
4559
4560 int
4561 ctl_lun_online(struct ctl_be_lun *be_lun)
4562 {
4563         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4564
4565         mtx_lock(&lun->lun_lock);
4566         lun->flags &= ~CTL_LUN_OFFLINE;
4567         mtx_unlock(&lun->lun_lock);
4568         return (0);
4569 }
4570
4571 int
4572 ctl_lun_primary(struct ctl_be_lun *be_lun)
4573 {
4574         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4575
4576         mtx_lock(&lun->lun_lock);
4577         lun->flags |= CTL_LUN_PRIMARY_SC;
4578         ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4579         mtx_unlock(&lun->lun_lock);
4580         ctl_isc_announce_lun(lun);
4581         return (0);
4582 }
4583
4584 int
4585 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4586 {
4587         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4588
4589         mtx_lock(&lun->lun_lock);
4590         lun->flags &= ~CTL_LUN_PRIMARY_SC;
4591         ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4592         mtx_unlock(&lun->lun_lock);
4593         ctl_isc_announce_lun(lun);
4594         return (0);
4595 }
4596
4597 int
4598 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4599 {
4600         struct ctl_softc *softc;
4601         struct ctl_lun *lun;
4602
4603         lun = (struct ctl_lun *)be_lun->ctl_lun;
4604         softc = lun->ctl_softc;
4605
4606         mtx_lock(&lun->lun_lock);
4607
4608         /*
4609          * The LUN needs to be disabled before it can be marked invalid.
4610          */
4611         if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4612                 mtx_unlock(&lun->lun_lock);
4613                 return (-1);
4614         }
4615         /*
4616          * Mark the LUN invalid.
4617          */
4618         lun->flags |= CTL_LUN_INVALID;
4619
4620         /*
4621          * If there is nothing in the OOA queue, go ahead and free the LUN.
4622          * If we have something in the OOA queue, we'll free it when the
4623          * last I/O completes.
4624          */
4625         if (TAILQ_EMPTY(&lun->ooa_queue)) {
4626                 mtx_unlock(&lun->lun_lock);
4627                 mtx_lock(&softc->ctl_lock);
4628                 ctl_free_lun(lun);
4629                 mtx_unlock(&softc->ctl_lock);
4630         } else
4631                 mtx_unlock(&lun->lun_lock);
4632
4633         return (0);
4634 }
4635
4636 int
4637 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4638 {
4639         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4640
4641         mtx_lock(&lun->lun_lock);
4642         lun->flags |= CTL_LUN_INOPERABLE;
4643         mtx_unlock(&lun->lun_lock);
4644         return (0);
4645 }
4646
4647 int
4648 ctl_lun_operable(struct ctl_be_lun *be_lun)
4649 {
4650         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4651
4652         mtx_lock(&lun->lun_lock);
4653         lun->flags &= ~CTL_LUN_INOPERABLE;
4654         mtx_unlock(&lun->lun_lock);
4655         return (0);
4656 }
4657
4658 void
4659 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4660 {
4661         struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4662         union ctl_ha_msg msg;
4663
4664         mtx_lock(&lun->lun_lock);
4665         ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
4666         mtx_unlock(&lun->lun_lock);
4667         if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4668                 /* Send msg to other side. */
4669                 bzero(&msg.ua, sizeof(msg.ua));
4670                 msg.hdr.msg_type = CTL_MSG_UA;
4671                 msg.hdr.nexus.initid = -1;
4672                 msg.hdr.nexus.targ_port = -1;
4673                 msg.hdr.nexus.targ_lun = lun->lun;
4674                 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4675                 msg.ua.ua_all = 1;
4676                 msg.ua.ua_set = 1;
4677                 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED;
4678                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4679                     M_WAITOK);
4680         }
4681 }
4682
4683 /*
4684  * Backend "memory move is complete" callback for requests that never
4685  * make it down to say RAIDCore's configuration code.
4686  */
4687 int
4688 ctl_config_move_done(union ctl_io *io)
4689 {
4690         int retval;
4691
4692         CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4693         KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4694             ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4695
4696         if ((io->io_hdr.port_status != 0) &&
4697             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4698              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4699                 /*
4700                  * For hardware error sense keys, the sense key
4701                  * specific value is defined to be a retry count,
4702                  * but we use it to pass back an internal FETD
4703                  * error code.  XXX KDM  Hopefully the FETD is only
4704                  * using 16 bits for an error code, since that's
4705                  * all the space we have in the sks field.
4706                  */
4707                 ctl_set_internal_failure(&io->scsiio,
4708                                          /*sks_valid*/ 1,
4709                                          /*retry_count*/
4710                                          io->io_hdr.port_status);
4711         }
4712
4713         if (ctl_debug & CTL_DEBUG_CDB_DATA)
4714                 ctl_data_print(io);
4715         if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4716             ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4717              (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4718             ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4719                 /*
4720                  * XXX KDM just assuming a single pointer here, and not a
4721                  * S/G list.  If we start using S/G lists for config data,
4722                  * we'll need to know how to clean them up here as well.
4723                  */
4724                 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4725                         free(io->scsiio.kern_data_ptr, M_CTL);
4726                 ctl_done(io);
4727                 retval = CTL_RETVAL_COMPLETE;
4728         } else {
4729                 /*
4730                  * XXX KDM now we need to continue data movement.  Some
4731                  * options:
4732                  * - call ctl_scsiio() again?  We don't do this for data
4733                  *   writes, because for those at least we know ahead of
4734                  *   time where the write will go and how long it is.  For
4735                  *   config writes, though, that information is largely
4736                  *   contained within the write itself, thus we need to
4737                  *   parse out the data again.
4738                  *
4739                  * - Call some other function once the data is in?
4740                  */
4741
4742                 /*
4743                  * XXX KDM call ctl_scsiio() again for now, and check flag
4744                  * bits to see whether we're allocated or not.
4745                  */
4746                 retval = ctl_scsiio(&io->scsiio);
4747         }
4748         return (retval);
4749 }
4750
4751 /*
4752  * This gets called by a backend driver when it is done with a
4753  * data_submit method.
4754  */
4755 void
4756 ctl_data_submit_done(union ctl_io *io)
4757 {
4758         /*
4759          * If the IO_CONT flag is set, we need to call the supplied
4760          * function to continue processing the I/O, instead of completing
4761          * the I/O just yet.
4762          *
4763          * If there is an error, though, we don't want to keep processing.
4764          * Instead, just send status back to the initiator.
4765          */
4766         if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4767             (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4768             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4769              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4770                 io->scsiio.io_cont(io);
4771                 return;
4772         }
4773         ctl_done(io);
4774 }
4775
4776 /*
4777  * This gets called by a backend driver when it is done with a
4778  * configuration write.
4779  */
4780 void
4781 ctl_config_write_done(union ctl_io *io)
4782 {
4783         uint8_t *buf;
4784
4785         /*
4786          * If the IO_CONT flag is set, we need to call the supplied
4787          * function to continue processing the I/O, instead of completing
4788          * the I/O just yet.
4789          *
4790          * If there is an error, though, we don't want to keep processing.
4791          * Instead, just send status back to the initiator.
4792          */
4793         if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4794             (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4795             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4796              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4797                 io->scsiio.io_cont(io);
4798                 return;
4799         }
4800         /*
4801          * Since a configuration write can be done for commands that actually
4802          * have data allocated, like write buffer, and commands that have
4803          * no data, like start/stop unit, we need to check here.
4804          */
4805         if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4806                 buf = io->scsiio.kern_data_ptr;
4807         else
4808                 buf = NULL;
4809         ctl_done(io);
4810         if (buf)
4811                 free(buf, M_CTL);
4812 }
4813
4814 void
4815 ctl_config_read_done(union ctl_io *io)
4816 {
4817         uint8_t *buf;
4818
4819         /*
4820          * If there is some error -- we are done, skip data transfer.
4821          */
4822         if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
4823             ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4824              (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
4825                 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4826                         buf = io->scsiio.kern_data_ptr;
4827                 else
4828                         buf = NULL;
4829                 ctl_done(io);
4830                 if (buf)
4831                         free(buf, M_CTL);
4832                 return;
4833         }
4834
4835         /*
4836          * If the IO_CONT flag is set, we need to call the supplied
4837          * function to continue processing the I/O, instead of completing
4838          * the I/O just yet.
4839          */
4840         if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
4841                 io->scsiio.io_cont(io);
4842                 return;
4843         }
4844
4845         ctl_datamove(io);
4846 }
4847
4848 /*
4849  * SCSI release command.
4850  */
4851 int
4852 ctl_scsi_release(struct ctl_scsiio *ctsio)
4853 {
4854         int length, longid, thirdparty_id, resv_id;
4855         struct ctl_lun *lun;
4856         uint32_t residx;
4857
4858         length = 0;
4859         resv_id = 0;
4860
4861         CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4862
4863         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4864         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4865
4866         switch (ctsio->cdb[0]) {
4867         case RELEASE_10: {
4868                 struct scsi_release_10 *cdb;
4869
4870                 cdb = (struct scsi_release_10 *)ctsio->cdb;
4871
4872                 if (cdb->byte2 & SR10_LONGID)
4873                         longid = 1;
4874                 else
4875                         thirdparty_id = cdb->thirdparty_id;
4876
4877                 resv_id = cdb->resv_id;
4878                 length = scsi_2btoul(cdb->length);
4879                 break;
4880         }
4881         }
4882
4883
4884         /*
4885          * XXX KDM right now, we only support LUN reservation.  We don't
4886          * support 3rd party reservations, or extent reservations, which
4887          * might actually need the parameter list.  If we've gotten this
4888          * far, we've got a LUN reservation.  Anything else got kicked out
4889          * above.  So, according to SPC, ignore the length.
4890          */
4891         length = 0;
4892
4893         if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4894          && (length > 0)) {
4895                 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4896                 ctsio->kern_data_len = length;
4897                 ctsio->kern_total_len = length;
4898                 ctsio->kern_data_resid = 0;
4899                 ctsio->kern_rel_offset = 0;
4900                 ctsio->kern_sg_entries = 0;
4901                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4902                 ctsio->be_move_done = ctl_config_move_done;
4903                 ctl_datamove((union ctl_io *)ctsio);
4904
4905                 return (CTL_RETVAL_COMPLETE);
4906         }
4907
4908         if (length > 0)
4909                 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
4910
4911         mtx_lock(&lun->lun_lock);
4912
4913         /*
4914          * According to SPC, it is not an error for an intiator to attempt
4915          * to release a reservation on a LUN that isn't reserved, or that
4916          * is reserved by another initiator.  The reservation can only be
4917          * released, though, by the initiator who made it or by one of
4918          * several reset type events.
4919          */
4920         if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
4921                         lun->flags &= ~CTL_LUN_RESERVED;
4922
4923         mtx_unlock(&lun->lun_lock);
4924
4925         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
4926                 free(ctsio->kern_data_ptr, M_CTL);
4927                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
4928         }
4929
4930         ctl_set_success(ctsio);
4931         ctl_done((union ctl_io *)ctsio);
4932         return (CTL_RETVAL_COMPLETE);
4933 }
4934
4935 int
4936 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
4937 {
4938         int extent, thirdparty, longid;
4939         int resv_id, length;
4940         uint64_t thirdparty_id;
4941         struct ctl_lun *lun;
4942         uint32_t residx;
4943
4944         extent = 0;
4945         thirdparty = 0;
4946         longid = 0;
4947         resv_id = 0;
4948         length = 0;
4949         thirdparty_id = 0;
4950
4951         CTL_DEBUG_PRINT(("ctl_reserve\n"));
4952
4953         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4954         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4955
4956         switch (ctsio->cdb[0]) {
4957         case RESERVE_10: {
4958                 struct scsi_reserve_10 *cdb;
4959
4960                 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
4961
4962                 if (cdb->byte2 & SR10_LONGID)
4963                         longid = 1;
4964                 else
4965                         thirdparty_id = cdb->thirdparty_id;
4966
4967                 resv_id = cdb->resv_id;
4968                 length = scsi_2btoul(cdb->length);
4969                 break;
4970         }
4971         }
4972
4973         /*
4974          * XXX KDM right now, we only support LUN reservation.  We don't
4975          * support 3rd party reservations, or extent reservations, which
4976          * might actually need the parameter list.  If we've gotten this
4977          * far, we've got a LUN reservation.  Anything else got kicked out
4978          * above.  So, according to SPC, ignore the length.
4979          */
4980         length = 0;
4981
4982         if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4983          && (length > 0)) {
4984                 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4985                 ctsio->kern_data_len = length;
4986                 ctsio->kern_total_len = length;
4987                 ctsio->kern_data_resid = 0;
4988                 ctsio->kern_rel_offset = 0;
4989                 ctsio->kern_sg_entries = 0;
4990                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4991                 ctsio->be_move_done = ctl_config_move_done;
4992                 ctl_datamove((union ctl_io *)ctsio);
4993
4994                 return (CTL_RETVAL_COMPLETE);
4995         }
4996
4997         if (length > 0)
4998                 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
4999
5000         mtx_lock(&lun->lun_lock);
5001         if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5002                 ctl_set_reservation_conflict(ctsio);
5003                 goto bailout;
5004         }
5005
5006         lun->flags |= CTL_LUN_RESERVED;
5007         lun->res_idx = residx;
5008
5009         ctl_set_success(ctsio);
5010
5011 bailout:
5012         mtx_unlock(&lun->lun_lock);
5013
5014         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5015                 free(ctsio->kern_data_ptr, M_CTL);
5016                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5017         }
5018
5019         ctl_done((union ctl_io *)ctsio);
5020         return (CTL_RETVAL_COMPLETE);
5021 }
5022
5023 int
5024 ctl_start_stop(struct ctl_scsiio *ctsio)
5025 {
5026         struct scsi_start_stop_unit *cdb;
5027         struct ctl_lun *lun;
5028         int retval;
5029
5030         CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5031
5032         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5033         retval = 0;
5034
5035         cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5036
5037         /*
5038          * XXX KDM
5039          * We don't support the immediate bit on a stop unit.  In order to
5040          * do that, we would need to code up a way to know that a stop is
5041          * pending, and hold off any new commands until it completes, one
5042          * way or another.  Then we could accept or reject those commands
5043          * depending on its status.  We would almost need to do the reverse
5044          * of what we do below for an immediate start -- return the copy of
5045          * the ctl_io to the FETD with status to send to the host (and to
5046          * free the copy!) and then free the original I/O once the stop
5047          * actually completes.  That way, the OOA queue mechanism can work
5048          * to block commands that shouldn't proceed.  Another alternative
5049          * would be to put the copy in the queue in place of the original,
5050          * and return the original back to the caller.  That could be
5051          * slightly safer..
5052          */
5053         if ((cdb->byte2 & SSS_IMMED)
5054          && ((cdb->how & SSS_START) == 0)) {
5055                 ctl_set_invalid_field(ctsio,
5056                                       /*sks_valid*/ 1,
5057                                       /*command*/ 1,
5058                                       /*field*/ 1,
5059                                       /*bit_valid*/ 1,
5060                                       /*bit*/ 0);
5061                 ctl_done((union ctl_io *)ctsio);
5062                 return (CTL_RETVAL_COMPLETE);
5063         }
5064
5065         if ((lun->flags & CTL_LUN_PR_RESERVED)
5066          && ((cdb->how & SSS_START)==0)) {
5067                 uint32_t residx;
5068
5069                 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5070                 if (ctl_get_prkey(lun, residx) == 0
5071                  || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5072
5073                         ctl_set_reservation_conflict(ctsio);
5074                         ctl_done((union ctl_io *)ctsio);
5075                         return (CTL_RETVAL_COMPLETE);
5076                 }
5077         }
5078
5079         /*
5080          * If there is no backend on this device, we can't start or stop
5081          * it.  In theory we shouldn't get any start/stop commands in the
5082          * first place at this level if the LUN doesn't have a backend.
5083          * That should get stopped by the command decode code.
5084          */
5085         if (lun->backend == NULL) {
5086                 ctl_set_invalid_opcode(ctsio);
5087                 ctl_done((union ctl_io *)ctsio);
5088                 return (CTL_RETVAL_COMPLETE);
5089         }
5090
5091         /*
5092          * XXX KDM Copan-specific offline behavior.
5093          * Figure out a reasonable way to port this?
5094          */
5095 #ifdef NEEDTOPORT
5096         mtx_lock(&lun->lun_lock);
5097
5098         if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5099          && (lun->flags & CTL_LUN_OFFLINE)) {
5100                 /*
5101                  * If the LUN is offline, and the on/offline bit isn't set,
5102                  * reject the start or stop.  Otherwise, let it through.
5103                  */
5104                 mtx_unlock(&lun->lun_lock);
5105                 ctl_set_lun_not_ready(ctsio);
5106                 ctl_done((union ctl_io *)ctsio);
5107         } else {
5108                 mtx_unlock(&lun->lun_lock);
5109 #endif /* NEEDTOPORT */
5110                 /*
5111                  * This could be a start or a stop when we're online,
5112                  * or a stop/offline or start/online.  A start or stop when
5113                  * we're offline is covered in the case above.
5114                  */
5115                 /*
5116                  * In the non-immediate case, we send the request to
5117                  * the backend and return status to the user when
5118                  * it is done.
5119                  *
5120                  * In the immediate case, we allocate a new ctl_io
5121                  * to hold a copy of the request, and send that to
5122                  * the backend.  We then set good status on the
5123                  * user's request and return it immediately.
5124                  */
5125                 if (cdb->byte2 & SSS_IMMED) {
5126                         union ctl_io *new_io;
5127
5128                         new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5129                         ctl_copy_io((union ctl_io *)ctsio, new_io);
5130                         retval = lun->backend->config_write(new_io);
5131                         ctl_set_success(ctsio);
5132                         ctl_done((union ctl_io *)ctsio);
5133                 } else {
5134                         retval = lun->backend->config_write(
5135                                 (union ctl_io *)ctsio);
5136                 }
5137 #ifdef NEEDTOPORT
5138         }
5139 #endif
5140         return (retval);
5141 }
5142
5143 /*
5144  * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5145  * we don't really do anything with the LBA and length fields if the user
5146  * passes them in.  Instead we'll just flush out the cache for the entire
5147  * LUN.
5148  */
5149 int
5150 ctl_sync_cache(struct ctl_scsiio *ctsio)
5151 {
5152         struct ctl_lun *lun;
5153         struct ctl_softc *softc;
5154         struct ctl_lba_len_flags *lbalen;
5155         uint64_t starting_lba;
5156         uint32_t block_count;
5157         int retval;
5158         uint8_t byte2;
5159
5160         CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5161
5162         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5163         softc = lun->ctl_softc;
5164         retval = 0;
5165
5166         switch (ctsio->cdb[0]) {
5167         case SYNCHRONIZE_CACHE: {
5168                 struct scsi_sync_cache *cdb;
5169                 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5170
5171                 starting_lba = scsi_4btoul(cdb->begin_lba);
5172                 block_count = scsi_2btoul(cdb->lb_count);
5173                 byte2 = cdb->byte2;
5174                 break;
5175         }
5176         case SYNCHRONIZE_CACHE_16: {
5177                 struct scsi_sync_cache_16 *cdb;
5178                 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5179
5180                 starting_lba = scsi_8btou64(cdb->begin_lba);
5181                 block_count = scsi_4btoul(cdb->lb_count);
5182                 byte2 = cdb->byte2;
5183                 break;
5184         }
5185         default:
5186                 ctl_set_invalid_opcode(ctsio);
5187                 ctl_done((union ctl_io *)ctsio);
5188                 goto bailout;
5189                 break; /* NOTREACHED */
5190         }
5191
5192         /*
5193          * We check the LBA and length, but don't do anything with them.
5194          * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5195          * get flushed.  This check will just help satisfy anyone who wants
5196          * to see an error for an out of range LBA.
5197          */
5198         if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5199                 ctl_set_lba_out_of_range(ctsio);
5200                 ctl_done((union ctl_io *)ctsio);
5201                 goto bailout;
5202         }
5203
5204         /*
5205          * If this LUN has no backend, we can't flush the cache anyway.
5206          */
5207         if (lun->backend == NULL) {
5208                 ctl_set_invalid_opcode(ctsio);
5209                 ctl_done((union ctl_io *)ctsio);
5210                 goto bailout;
5211         }
5212
5213         lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5214         lbalen->lba = starting_lba;
5215         lbalen->len = block_count;
5216         lbalen->flags = byte2;
5217
5218         /*
5219          * Check to see whether we're configured to send the SYNCHRONIZE
5220          * CACHE command directly to the back end.
5221          */
5222         mtx_lock(&lun->lun_lock);
5223         if ((softc->flags & CTL_FLAG_REAL_SYNC)
5224          && (++(lun->sync_count) >= lun->sync_interval)) {
5225                 lun->sync_count = 0;
5226                 mtx_unlock(&lun->lun_lock);
5227                 retval = lun->backend->config_write((union ctl_io *)ctsio);
5228         } else {
5229                 mtx_unlock(&lun->lun_lock);
5230                 ctl_set_success(ctsio);
5231                 ctl_done((union ctl_io *)ctsio);
5232         }
5233
5234 bailout:
5235
5236         return (retval);
5237 }
5238
5239 int
5240 ctl_format(struct ctl_scsiio *ctsio)
5241 {
5242         struct scsi_format *cdb;
5243         struct ctl_lun *lun;
5244         int length, defect_list_len;
5245
5246         CTL_DEBUG_PRINT(("ctl_format\n"));
5247
5248         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5249
5250         cdb = (struct scsi_format *)ctsio->cdb;
5251
5252         length = 0;
5253         if (cdb->byte2 & SF_FMTDATA) {
5254                 if (cdb->byte2 & SF_LONGLIST)
5255                         length = sizeof(struct scsi_format_header_long);
5256                 else
5257                         length = sizeof(struct scsi_format_header_short);
5258         }
5259
5260         if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5261          && (length > 0)) {
5262                 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5263                 ctsio->kern_data_len = length;
5264                 ctsio->kern_total_len = length;
5265                 ctsio->kern_data_resid = 0;
5266                 ctsio->kern_rel_offset = 0;
5267                 ctsio->kern_sg_entries = 0;
5268                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5269                 ctsio->be_move_done = ctl_config_move_done;
5270                 ctl_datamove((union ctl_io *)ctsio);
5271
5272                 return (CTL_RETVAL_COMPLETE);
5273         }
5274
5275         defect_list_len = 0;
5276
5277         if (cdb->byte2 & SF_FMTDATA) {
5278                 if (cdb->byte2 & SF_LONGLIST) {
5279                         struct scsi_format_header_long *header;
5280
5281                         header = (struct scsi_format_header_long *)
5282                                 ctsio->kern_data_ptr;
5283
5284                         defect_list_len = scsi_4btoul(header->defect_list_len);
5285                         if (defect_list_len != 0) {
5286                                 ctl_set_invalid_field(ctsio,
5287                                                       /*sks_valid*/ 1,
5288                                                       /*command*/ 0,
5289                                                       /*field*/ 2,
5290                                                       /*bit_valid*/ 0,
5291                                                       /*bit*/ 0);
5292                                 goto bailout;
5293                         }
5294                 } else {
5295                         struct scsi_format_header_short *header;
5296
5297                         header = (struct scsi_format_header_short *)
5298                                 ctsio->kern_data_ptr;
5299
5300                         defect_list_len = scsi_2btoul(header->defect_list_len);
5301                         if (defect_list_len != 0) {
5302                                 ctl_set_invalid_field(ctsio,
5303                                                       /*sks_valid*/ 1,
5304                                                       /*command*/ 0,
5305                                                       /*field*/ 2,
5306                                                       /*bit_valid*/ 0,
5307                                                       /*bit*/ 0);
5308                                 goto bailout;
5309                         }
5310                 }
5311         }
5312
5313         /*
5314          * The format command will clear out the "Medium format corrupted"
5315          * status if set by the configuration code.  That status is really
5316          * just a way to notify the host that we have lost the media, and
5317          * get them to issue a command that will basically make them think
5318          * they're blowing away the media.
5319          */
5320         mtx_lock(&lun->lun_lock);
5321         lun->flags &= ~CTL_LUN_INOPERABLE;
5322         mtx_unlock(&lun->lun_lock);
5323
5324         ctl_set_success(ctsio);
5325 bailout:
5326
5327         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5328                 free(ctsio->kern_data_ptr, M_CTL);
5329                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5330         }
5331
5332         ctl_done((union ctl_io *)ctsio);
5333         return (CTL_RETVAL_COMPLETE);
5334 }
5335
5336 int
5337 ctl_read_buffer(struct ctl_scsiio *ctsio)
5338 {
5339         struct scsi_read_buffer *cdb;
5340         struct ctl_lun *lun;
5341         int buffer_offset, len;
5342         static uint8_t descr[4];
5343         static uint8_t echo_descr[4] = { 0 };
5344
5345         CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5346
5347         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5348         cdb = (struct scsi_read_buffer *)ctsio->cdb;
5349
5350         if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5351             (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5352             (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5353                 ctl_set_invalid_field(ctsio,
5354                                       /*sks_valid*/ 1,
5355                                       /*command*/ 1,
5356                                       /*field*/ 1,
5357                                       /*bit_valid*/ 1,
5358                                       /*bit*/ 4);
5359                 ctl_done((union ctl_io *)ctsio);
5360                 return (CTL_RETVAL_COMPLETE);
5361         }
5362
5363         len = scsi_3btoul(cdb->length);
5364         buffer_offset = scsi_3btoul(cdb->offset);
5365
5366         if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5367                 ctl_set_invalid_field(ctsio,
5368                                       /*sks_valid*/ 1,
5369                                       /*command*/ 1,
5370                                       /*field*/ 6,
5371                                       /*bit_valid*/ 0,
5372                                       /*bit*/ 0);
5373                 ctl_done((union ctl_io *)ctsio);
5374                 return (CTL_RETVAL_COMPLETE);
5375         }
5376
5377         if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5378                 descr[0] = 0;
5379                 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5380                 ctsio->kern_data_ptr = descr;
5381                 len = min(len, sizeof(descr));
5382         } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5383                 ctsio->kern_data_ptr = echo_descr;
5384                 len = min(len, sizeof(echo_descr));
5385         } else {
5386                 if (lun->write_buffer == NULL) {
5387                         lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5388                             M_CTL, M_WAITOK);
5389                 }
5390                 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5391         }
5392         ctsio->kern_data_len = len;
5393         ctsio->kern_total_len = len;
5394         ctsio->kern_data_resid = 0;
5395         ctsio->kern_rel_offset = 0;
5396         ctsio->kern_sg_entries = 0;
5397         ctl_set_success(ctsio);
5398         ctsio->be_move_done = ctl_config_move_done;
5399         ctl_datamove((union ctl_io *)ctsio);
5400         return (CTL_RETVAL_COMPLETE);
5401 }
5402
5403 int
5404 ctl_write_buffer(struct ctl_scsiio *ctsio)
5405 {
5406         struct scsi_write_buffer *cdb;
5407         struct ctl_lun *lun;
5408         int buffer_offset, len;
5409
5410         CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5411
5412         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5413         cdb = (struct scsi_write_buffer *)ctsio->cdb;
5414
5415         if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5416                 ctl_set_invalid_field(ctsio,
5417                                       /*sks_valid*/ 1,
5418                                       /*command*/ 1,
5419                                       /*field*/ 1,
5420                                       /*bit_valid*/ 1,
5421                                       /*bit*/ 4);
5422                 ctl_done((union ctl_io *)ctsio);
5423                 return (CTL_RETVAL_COMPLETE);
5424         }
5425
5426         len = scsi_3btoul(cdb->length);
5427         buffer_offset = scsi_3btoul(cdb->offset);
5428
5429         if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5430                 ctl_set_invalid_field(ctsio,
5431                                       /*sks_valid*/ 1,
5432                                       /*command*/ 1,
5433                                       /*field*/ 6,
5434                                       /*bit_valid*/ 0,
5435                                       /*bit*/ 0);
5436                 ctl_done((union ctl_io *)ctsio);
5437                 return (CTL_RETVAL_COMPLETE);
5438         }
5439
5440         /*
5441          * If we've got a kernel request that hasn't been malloced yet,
5442          * malloc it and tell the caller the data buffer is here.
5443          */
5444         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5445                 if (lun->write_buffer == NULL) {
5446                         lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5447                             M_CTL, M_WAITOK);
5448                 }
5449                 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5450                 ctsio->kern_data_len = len;
5451                 ctsio->kern_total_len = len;
5452                 ctsio->kern_data_resid = 0;
5453                 ctsio->kern_rel_offset = 0;
5454                 ctsio->kern_sg_entries = 0;
5455                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5456                 ctsio->be_move_done = ctl_config_move_done;
5457                 ctl_datamove((union ctl_io *)ctsio);
5458
5459                 return (CTL_RETVAL_COMPLETE);
5460         }
5461
5462         ctl_set_success(ctsio);
5463         ctl_done((union ctl_io *)ctsio);
5464         return (CTL_RETVAL_COMPLETE);
5465 }
5466
5467 int
5468 ctl_write_same(struct ctl_scsiio *ctsio)
5469 {
5470         struct ctl_lun *lun;
5471         struct ctl_lba_len_flags *lbalen;
5472         uint64_t lba;
5473         uint32_t num_blocks;
5474         int len, retval;
5475         uint8_t byte2;
5476
5477         retval = CTL_RETVAL_COMPLETE;
5478
5479         CTL_DEBUG_PRINT(("ctl_write_same\n"));
5480
5481         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5482
5483         switch (ctsio->cdb[0]) {
5484         case WRITE_SAME_10: {
5485                 struct scsi_write_same_10 *cdb;
5486
5487                 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5488
5489                 lba = scsi_4btoul(cdb->addr);
5490                 num_blocks = scsi_2btoul(cdb->length);
5491                 byte2 = cdb->byte2;
5492                 break;
5493         }
5494         case WRITE_SAME_16: {
5495                 struct scsi_write_same_16 *cdb;
5496
5497                 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5498
5499                 lba = scsi_8btou64(cdb->addr);
5500                 num_blocks = scsi_4btoul(cdb->length);
5501                 byte2 = cdb->byte2;
5502                 break;
5503         }
5504         default:
5505                 /*
5506                  * We got a command we don't support.  This shouldn't
5507                  * happen, commands should be filtered out above us.
5508                  */
5509                 ctl_set_invalid_opcode(ctsio);
5510                 ctl_done((union ctl_io *)ctsio);
5511
5512                 return (CTL_RETVAL_COMPLETE);
5513                 break; /* NOTREACHED */
5514         }
5515
5516         /* NDOB and ANCHOR flags can be used only together with UNMAP */
5517         if ((byte2 & SWS_UNMAP) == 0 &&
5518             (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5519                 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5520                     /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5521                 ctl_done((union ctl_io *)ctsio);
5522                 return (CTL_RETVAL_COMPLETE);
5523         }
5524
5525         /*
5526          * The first check is to make sure we're in bounds, the second
5527          * check is to catch wrap-around problems.  If the lba + num blocks
5528          * is less than the lba, then we've wrapped around and the block
5529          * range is invalid anyway.
5530          */
5531         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5532          || ((lba + num_blocks) < lba)) {
5533                 ctl_set_lba_out_of_range(ctsio);
5534                 ctl_done((union ctl_io *)ctsio);
5535                 return (CTL_RETVAL_COMPLETE);
5536         }
5537
5538         /* Zero number of blocks means "to the last logical block" */
5539         if (num_blocks == 0) {
5540                 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5541                         ctl_set_invalid_field(ctsio,
5542                                               /*sks_valid*/ 0,
5543                                               /*command*/ 1,
5544                                               /*field*/ 0,
5545                                               /*bit_valid*/ 0,
5546                                               /*bit*/ 0);
5547                         ctl_done((union ctl_io *)ctsio);
5548                         return (CTL_RETVAL_COMPLETE);
5549                 }
5550                 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5551         }
5552
5553         len = lun->be_lun->blocksize;
5554
5555         /*
5556          * If we've got a kernel request that hasn't been malloced yet,
5557          * malloc it and tell the caller the data buffer is here.
5558          */
5559         if ((byte2 & SWS_NDOB) == 0 &&
5560             (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5561                 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5562                 ctsio->kern_data_len = len;
5563                 ctsio->kern_total_len = len;
5564                 ctsio->kern_data_resid = 0;
5565                 ctsio->kern_rel_offset = 0;
5566                 ctsio->kern_sg_entries = 0;
5567                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5568                 ctsio->be_move_done = ctl_config_move_done;
5569                 ctl_datamove((union ctl_io *)ctsio);
5570
5571                 return (CTL_RETVAL_COMPLETE);
5572         }
5573
5574         lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5575         lbalen->lba = lba;
5576         lbalen->len = num_blocks;
5577         lbalen->flags = byte2;
5578         retval = lun->backend->config_write((union ctl_io *)ctsio);
5579
5580         return (retval);
5581 }
5582
5583 int
5584 ctl_unmap(struct ctl_scsiio *ctsio)
5585 {
5586         struct ctl_lun *lun;
5587         struct scsi_unmap *cdb;
5588         struct ctl_ptr_len_flags *ptrlen;
5589         struct scsi_unmap_header *hdr;
5590         struct scsi_unmap_desc *buf, *end, *endnz, *range;
5591         uint64_t lba;
5592         uint32_t num_blocks;
5593         int len, retval;
5594         uint8_t byte2;
5595
5596         retval = CTL_RETVAL_COMPLETE;
5597
5598         CTL_DEBUG_PRINT(("ctl_unmap\n"));
5599
5600         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5601         cdb = (struct scsi_unmap *)ctsio->cdb;
5602
5603         len = scsi_2btoul(cdb->length);
5604         byte2 = cdb->byte2;
5605
5606         /*
5607          * If we've got a kernel request that hasn't been malloced yet,
5608          * malloc it and tell the caller the data buffer is here.
5609          */
5610         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5611                 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5612                 ctsio->kern_data_len = len;
5613                 ctsio->kern_total_len = len;
5614                 ctsio->kern_data_resid = 0;
5615                 ctsio->kern_rel_offset = 0;
5616                 ctsio->kern_sg_entries = 0;
5617                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5618                 ctsio->be_move_done = ctl_config_move_done;
5619                 ctl_datamove((union ctl_io *)ctsio);
5620
5621                 return (CTL_RETVAL_COMPLETE);
5622         }
5623
5624         len = ctsio->kern_total_len - ctsio->kern_data_resid;
5625         hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5626         if (len < sizeof (*hdr) ||
5627             len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5628             len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5629             scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5630                 ctl_set_invalid_field(ctsio,
5631                                       /*sks_valid*/ 0,
5632                                       /*command*/ 0,
5633                                       /*field*/ 0,
5634                                       /*bit_valid*/ 0,
5635                                       /*bit*/ 0);
5636                 goto done;
5637         }
5638         len = scsi_2btoul(hdr->desc_length);
5639         buf = (struct scsi_unmap_desc *)(hdr + 1);
5640         end = buf + len / sizeof(*buf);
5641
5642         endnz = buf;
5643         for (range = buf; range < end; range++) {
5644                 lba = scsi_8btou64(range->lba);
5645                 num_blocks = scsi_4btoul(range->length);
5646                 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5647                  || ((lba + num_blocks) < lba)) {
5648                         ctl_set_lba_out_of_range(ctsio);
5649                         ctl_done((union ctl_io *)ctsio);
5650                         return (CTL_RETVAL_COMPLETE);
5651                 }
5652                 if (num_blocks != 0)
5653                         endnz = range + 1;
5654         }
5655
5656         /*
5657          * Block backend can not handle zero last range.
5658          * Filter it out and return if there is nothing left.
5659          */
5660         len = (uint8_t *)endnz - (uint8_t *)buf;
5661         if (len == 0) {
5662                 ctl_set_success(ctsio);
5663                 goto done;
5664         }
5665
5666         mtx_lock(&lun->lun_lock);
5667         ptrlen = (struct ctl_ptr_len_flags *)
5668             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5669         ptrlen->ptr = (void *)buf;
5670         ptrlen->len = len;
5671         ptrlen->flags = byte2;
5672         ctl_check_blocked(lun);
5673         mtx_unlock(&lun->lun_lock);
5674
5675         retval = lun->backend->config_write((union ctl_io *)ctsio);
5676         return (retval);
5677
5678 done:
5679         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5680                 free(ctsio->kern_data_ptr, M_CTL);
5681                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5682         }
5683         ctl_done((union ctl_io *)ctsio);
5684         return (CTL_RETVAL_COMPLETE);
5685 }
5686
5687 /*
5688  * Note that this function currently doesn't actually do anything inside
5689  * CTL to enforce things if the DQue bit is turned on.
5690  *
5691  * Also note that this function can't be used in the default case, because
5692  * the DQue bit isn't set in the changeable mask for the control mode page
5693  * anyway.  This is just here as an example for how to implement a page
5694  * handler, and a placeholder in case we want to allow the user to turn
5695  * tagged queueing on and off.
5696  *
5697  * The D_SENSE bit handling is functional, however, and will turn
5698  * descriptor sense on and off for a given LUN.
5699  */
5700 int
5701 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5702                          struct ctl_page_index *page_index, uint8_t *page_ptr)
5703 {
5704         struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5705         struct ctl_lun *lun;
5706         int set_ua;
5707         uint32_t initidx;
5708
5709         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5710         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5711         set_ua = 0;
5712
5713         user_cp = (struct scsi_control_page *)page_ptr;
5714         current_cp = (struct scsi_control_page *)
5715                 (page_index->page_data + (page_index->page_len *
5716                 CTL_PAGE_CURRENT));
5717         saved_cp = (struct scsi_control_page *)
5718                 (page_index->page_data + (page_index->page_len *
5719                 CTL_PAGE_SAVED));
5720
5721         mtx_lock(&lun->lun_lock);
5722         if (((current_cp->rlec & SCP_DSENSE) == 0)
5723          && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5724                 /*
5725                  * Descriptor sense is currently turned off and the user
5726                  * wants to turn it on.
5727                  */
5728                 current_cp->rlec |= SCP_DSENSE;
5729                 saved_cp->rlec |= SCP_DSENSE;
5730                 lun->flags |= CTL_LUN_SENSE_DESC;
5731                 set_ua = 1;
5732         } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5733                 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5734                 /*
5735                  * Descriptor sense is currently turned on, and the user
5736                  * wants to turn it off.
5737                  */
5738                 current_cp->rlec &= ~SCP_DSENSE;
5739                 saved_cp->rlec &= ~SCP_DSENSE;
5740                 lun->flags &= ~CTL_LUN_SENSE_DESC;
5741                 set_ua = 1;
5742         }
5743         if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5744             (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5745                 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5746                 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5747                 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5748                 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5749                 set_ua = 1;
5750         }
5751         if ((current_cp->eca_and_aen & SCP_SWP) !=
5752             (user_cp->eca_and_aen & SCP_SWP)) {
5753                 current_cp->eca_and_aen &= ~SCP_SWP;
5754                 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5755                 saved_cp->eca_and_aen &= ~SCP_SWP;
5756                 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5757                 set_ua = 1;
5758         }
5759         if (set_ua != 0)
5760                 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5761         mtx_unlock(&lun->lun_lock);
5762
5763         return (0);
5764 }
5765
5766 int
5767 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5768                      struct ctl_page_index *page_index, uint8_t *page_ptr)
5769 {
5770         struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5771         struct ctl_lun *lun;
5772         int set_ua;
5773         uint32_t initidx;
5774
5775         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5776         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5777         set_ua = 0;
5778
5779         user_cp = (struct scsi_caching_page *)page_ptr;
5780         current_cp = (struct scsi_caching_page *)
5781                 (page_index->page_data + (page_index->page_len *
5782                 CTL_PAGE_CURRENT));
5783         saved_cp = (struct scsi_caching_page *)
5784                 (page_index->page_data + (page_index->page_len *
5785                 CTL_PAGE_SAVED));
5786
5787         mtx_lock(&lun->lun_lock);
5788         if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5789             (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5790                 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5791                 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5792                 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5793                 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5794                 set_ua = 1;
5795         }
5796         if (set_ua != 0)
5797                 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5798         mtx_unlock(&lun->lun_lock);
5799
5800         return (0);
5801 }
5802
5803 int
5804 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5805                                 struct ctl_page_index *page_index,
5806                                 uint8_t *page_ptr)
5807 {
5808         uint8_t *c;
5809         int i;
5810
5811         c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5812         ctl_time_io_secs =
5813                 (c[0] << 8) |
5814                 (c[1] << 0) |
5815                 0;
5816         CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5817         printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5818         printf("page data:");
5819         for (i=0; i<8; i++)
5820                 printf(" %.2x",page_ptr[i]);
5821         printf("\n");
5822         return (0);
5823 }
5824
5825 int
5826 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5827                                struct ctl_page_index *page_index,
5828                                int pc)
5829 {
5830         struct copan_debugconf_subpage *page;
5831
5832         page = (struct copan_debugconf_subpage *)page_index->page_data +
5833                 (page_index->page_len * pc);
5834
5835         switch (pc) {
5836         case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5837         case SMS_PAGE_CTRL_DEFAULT >> 6:
5838         case SMS_PAGE_CTRL_SAVED >> 6:
5839                 /*
5840                  * We don't update the changable or default bits for this page.
5841                  */
5842                 break;
5843         case SMS_PAGE_CTRL_CURRENT >> 6:
5844                 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5845                 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5846                 break;
5847         default:
5848 #ifdef NEEDTOPORT
5849                 EPRINT(0, "Invalid PC %d!!", pc);
5850 #endif /* NEEDTOPORT */
5851                 break;
5852         }
5853         return (0);
5854 }
5855
5856
5857 static int
5858 ctl_do_mode_select(union ctl_io *io)
5859 {
5860         struct scsi_mode_page_header *page_header;
5861         struct ctl_page_index *page_index;
5862         struct ctl_scsiio *ctsio;
5863         int control_dev, page_len;
5864         int page_len_offset, page_len_size;
5865         union ctl_modepage_info *modepage_info;
5866         struct ctl_lun *lun;
5867         int *len_left, *len_used;
5868         int retval, i;
5869
5870         ctsio = &io->scsiio;
5871         page_index = NULL;
5872         page_len = 0;
5873         retval = CTL_RETVAL_COMPLETE;
5874
5875         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5876
5877         if (lun->be_lun->lun_type != T_DIRECT)
5878                 control_dev = 1;
5879         else
5880                 control_dev = 0;
5881
5882         modepage_info = (union ctl_modepage_info *)
5883                 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5884         len_left = &modepage_info->header.len_left;
5885         len_used = &modepage_info->header.len_used;
5886
5887 do_next_page:
5888
5889         page_header = (struct scsi_mode_page_header *)
5890                 (ctsio->kern_data_ptr + *len_used);
5891
5892         if (*len_left == 0) {
5893                 free(ctsio->kern_data_ptr, M_CTL);
5894                 ctl_set_success(ctsio);
5895                 ctl_done((union ctl_io *)ctsio);
5896                 return (CTL_RETVAL_COMPLETE);
5897         } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5898
5899                 free(ctsio->kern_data_ptr, M_CTL);
5900                 ctl_set_param_len_error(ctsio);
5901                 ctl_done((union ctl_io *)ctsio);
5902                 return (CTL_RETVAL_COMPLETE);
5903
5904         } else if ((page_header->page_code & SMPH_SPF)
5905                 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5906
5907                 free(ctsio->kern_data_ptr, M_CTL);
5908                 ctl_set_param_len_error(ctsio);
5909                 ctl_done((union ctl_io *)ctsio);
5910                 return (CTL_RETVAL_COMPLETE);
5911         }
5912
5913
5914         /*
5915          * XXX KDM should we do something with the block descriptor?
5916          */
5917         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5918
5919                 if ((control_dev != 0)
5920                  && (lun->mode_pages.index[i].page_flags &
5921                      CTL_PAGE_FLAG_DISK_ONLY))
5922                         continue;
5923
5924                 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5925                     (page_header->page_code & SMPH_PC_MASK))
5926                         continue;
5927
5928                 /*
5929                  * If neither page has a subpage code, then we've got a
5930                  * match.
5931                  */
5932                 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
5933                  && ((page_header->page_code & SMPH_SPF) == 0)) {
5934                         page_index = &lun->mode_pages.index[i];
5935                         page_len = page_header->page_length;
5936                         break;
5937                 }
5938
5939                 /*
5940                  * If both pages have subpages, then the subpage numbers
5941                  * have to match.
5942                  */
5943                 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
5944                   && (page_header->page_code & SMPH_SPF)) {
5945                         struct scsi_mode_page_header_sp *sph;
5946
5947                         sph = (struct scsi_mode_page_header_sp *)page_header;
5948
5949                         if (lun->mode_pages.index[i].subpage ==
5950                             sph->subpage) {
5951                                 page_index = &lun->mode_pages.index[i];
5952                                 page_len = scsi_2btoul(sph->page_length);
5953                                 break;
5954                         }
5955                 }
5956         }
5957
5958         /*
5959          * If we couldn't find the page, or if we don't have a mode select
5960          * handler for it, send back an error to the user.
5961          */
5962         if ((page_index == NULL)
5963          || (page_index->select_handler == NULL)) {
5964                 ctl_set_invalid_field(ctsio,
5965                                       /*sks_valid*/ 1,
5966                                       /*command*/ 0,
5967                                       /*field*/ *len_used,
5968                                       /*bit_valid*/ 0,
5969                                       /*bit*/ 0);
5970                 free(ctsio->kern_data_ptr, M_CTL);
5971                 ctl_done((union ctl_io *)ctsio);
5972                 return (CTL_RETVAL_COMPLETE);
5973         }
5974
5975         if (page_index->page_code & SMPH_SPF) {
5976                 page_len_offset = 2;
5977                 page_len_size = 2;
5978         } else {
5979                 page_len_size = 1;
5980                 page_len_offset = 1;
5981         }
5982
5983         /*
5984          * If the length the initiator gives us isn't the one we specify in
5985          * the mode page header, or if they didn't specify enough data in
5986          * the CDB to avoid truncating this page, kick out the request.
5987          */
5988         if ((page_len != (page_index->page_len - page_len_offset -
5989                           page_len_size))
5990          || (*len_left < page_index->page_len)) {
5991
5992
5993                 ctl_set_invalid_field(ctsio,
5994                                       /*sks_valid*/ 1,
5995                                       /*command*/ 0,
5996                                       /*field*/ *len_used + page_len_offset,
5997                                       /*bit_valid*/ 0,
5998                                       /*bit*/ 0);
5999                 free(ctsio->kern_data_ptr, M_CTL);
6000                 ctl_done((union ctl_io *)ctsio);
6001                 return (CTL_RETVAL_COMPLETE);
6002         }
6003
6004         /*
6005          * Run through the mode page, checking to make sure that the bits
6006          * the user changed are actually legal for him to change.
6007          */
6008         for (i = 0; i < page_index->page_len; i++) {
6009                 uint8_t *user_byte, *change_mask, *current_byte;
6010                 int bad_bit;
6011                 int j;
6012
6013                 user_byte = (uint8_t *)page_header + i;
6014                 change_mask = page_index->page_data +
6015                               (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6016                 current_byte = page_index->page_data +
6017                                (page_index->page_len * CTL_PAGE_CURRENT) + i;
6018
6019                 /*
6020                  * Check to see whether the user set any bits in this byte
6021                  * that he is not allowed to set.
6022                  */
6023                 if ((*user_byte & ~(*change_mask)) ==
6024                     (*current_byte & ~(*change_mask)))
6025                         continue;
6026
6027                 /*
6028                  * Go through bit by bit to determine which one is illegal.
6029                  */
6030                 bad_bit = 0;
6031                 for (j = 7; j >= 0; j--) {
6032                         if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6033                             (((1 << i) & ~(*change_mask)) & *current_byte)) {
6034                                 bad_bit = i;
6035                                 break;
6036                         }
6037                 }
6038                 ctl_set_invalid_field(ctsio,
6039                                       /*sks_valid*/ 1,
6040                                       /*command*/ 0,
6041                                       /*field*/ *len_used + i,
6042                                       /*bit_valid*/ 1,
6043                                       /*bit*/ bad_bit);
6044                 free(ctsio->kern_data_ptr, M_CTL);
6045                 ctl_done((union ctl_io *)ctsio);
6046                 return (CTL_RETVAL_COMPLETE);
6047         }
6048
6049         /*
6050          * Decrement these before we call the page handler, since we may
6051          * end up getting called back one way or another before the handler
6052          * returns to this context.
6053          */
6054         *len_left -= page_index->page_len;
6055         *len_used += page_index->page_len;
6056
6057         retval = page_index->select_handler(ctsio, page_index,
6058                                             (uint8_t *)page_header);
6059
6060         /*
6061          * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6062          * wait until this queued command completes to finish processing
6063          * the mode page.  If it returns anything other than
6064          * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6065          * already set the sense information, freed the data pointer, and
6066          * completed the io for us.
6067          */
6068         if (retval != CTL_RETVAL_COMPLETE)
6069                 goto bailout_no_done;
6070
6071         /*
6072          * If the initiator sent us more than one page, parse the next one.
6073          */
6074         if (*len_left > 0)
6075                 goto do_next_page;
6076
6077         ctl_set_success(ctsio);
6078         free(ctsio->kern_data_ptr, M_CTL);
6079         ctl_done((union ctl_io *)ctsio);
6080
6081 bailout_no_done:
6082
6083         return (CTL_RETVAL_COMPLETE);
6084
6085 }
6086
6087 int
6088 ctl_mode_select(struct ctl_scsiio *ctsio)
6089 {
6090         int param_len, pf, sp;
6091         int header_size, bd_len;
6092         int len_left, len_used;
6093         struct ctl_page_index *page_index;
6094         struct ctl_lun *lun;
6095         int control_dev, page_len;
6096         union ctl_modepage_info *modepage_info;
6097         int retval;
6098
6099         pf = 0;
6100         sp = 0;
6101         page_len = 0;
6102         len_used = 0;
6103         len_left = 0;
6104         retval = 0;
6105         bd_len = 0;
6106         page_index = NULL;
6107
6108         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6109
6110         if (lun->be_lun->lun_type != T_DIRECT)
6111                 control_dev = 1;
6112         else
6113                 control_dev = 0;
6114
6115         switch (ctsio->cdb[0]) {
6116         case MODE_SELECT_6: {
6117                 struct scsi_mode_select_6 *cdb;
6118
6119                 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6120
6121                 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6122                 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6123
6124                 param_len = cdb->length;
6125                 header_size = sizeof(struct scsi_mode_header_6);
6126                 break;
6127         }
6128         case MODE_SELECT_10: {
6129                 struct scsi_mode_select_10 *cdb;
6130
6131                 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6132
6133                 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6134                 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6135
6136                 param_len = scsi_2btoul(cdb->length);
6137                 header_size = sizeof(struct scsi_mode_header_10);
6138                 break;
6139         }
6140         default:
6141                 ctl_set_invalid_opcode(ctsio);
6142                 ctl_done((union ctl_io *)ctsio);
6143                 return (CTL_RETVAL_COMPLETE);
6144                 break; /* NOTREACHED */
6145         }
6146
6147         /*
6148          * From SPC-3:
6149          * "A parameter list length of zero indicates that the Data-Out Buffer
6150          * shall be empty. This condition shall not be considered as an error."
6151          */
6152         if (param_len == 0) {
6153                 ctl_set_success(ctsio);
6154                 ctl_done((union ctl_io *)ctsio);
6155                 return (CTL_RETVAL_COMPLETE);
6156         }
6157
6158         /*
6159          * Since we'll hit this the first time through, prior to
6160          * allocation, we don't need to free a data buffer here.
6161          */
6162         if (param_len < header_size) {
6163                 ctl_set_param_len_error(ctsio);
6164                 ctl_done((union ctl_io *)ctsio);
6165                 return (CTL_RETVAL_COMPLETE);
6166         }
6167
6168         /*
6169          * Allocate the data buffer and grab the user's data.  In theory,
6170          * we shouldn't have to sanity check the parameter list length here
6171          * because the maximum size is 64K.  We should be able to malloc
6172          * that much without too many problems.
6173          */
6174         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6175                 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6176                 ctsio->kern_data_len = param_len;
6177                 ctsio->kern_total_len = param_len;
6178                 ctsio->kern_data_resid = 0;
6179                 ctsio->kern_rel_offset = 0;
6180                 ctsio->kern_sg_entries = 0;
6181                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6182                 ctsio->be_move_done = ctl_config_move_done;
6183                 ctl_datamove((union ctl_io *)ctsio);
6184
6185                 return (CTL_RETVAL_COMPLETE);
6186         }
6187
6188         switch (ctsio->cdb[0]) {
6189         case MODE_SELECT_6: {
6190                 struct scsi_mode_header_6 *mh6;
6191
6192                 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6193                 bd_len = mh6->blk_desc_len;
6194                 break;
6195         }
6196         case MODE_SELECT_10: {
6197                 struct scsi_mode_header_10 *mh10;
6198
6199                 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6200                 bd_len = scsi_2btoul(mh10->blk_desc_len);
6201                 break;
6202         }
6203         default:
6204                 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6205                 break;
6206         }
6207
6208         if (param_len < (header_size + bd_len)) {
6209                 free(ctsio->kern_data_ptr, M_CTL);
6210                 ctl_set_param_len_error(ctsio);
6211                 ctl_done((union ctl_io *)ctsio);
6212                 return (CTL_RETVAL_COMPLETE);
6213         }
6214
6215         /*
6216          * Set the IO_CONT flag, so that if this I/O gets passed to
6217          * ctl_config_write_done(), it'll get passed back to
6218          * ctl_do_mode_select() for further processing, or completion if
6219          * we're all done.
6220          */
6221         ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6222         ctsio->io_cont = ctl_do_mode_select;
6223
6224         modepage_info = (union ctl_modepage_info *)
6225                 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6226
6227         memset(modepage_info, 0, sizeof(*modepage_info));
6228
6229         len_left = param_len - header_size - bd_len;
6230         len_used = header_size + bd_len;
6231
6232         modepage_info->header.len_left = len_left;
6233         modepage_info->header.len_used = len_used;
6234
6235         return (ctl_do_mode_select((union ctl_io *)ctsio));
6236 }
6237
6238 int
6239 ctl_mode_sense(struct ctl_scsiio *ctsio)
6240 {
6241         struct ctl_lun *lun;
6242         int pc, page_code, dbd, llba, subpage;
6243         int alloc_len, page_len, header_len, total_len;
6244         struct scsi_mode_block_descr *block_desc;
6245         struct ctl_page_index *page_index;
6246         int control_dev;
6247
6248         dbd = 0;
6249         llba = 0;
6250         block_desc = NULL;
6251         page_index = NULL;
6252
6253         CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6254
6255         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6256
6257         if (lun->be_lun->lun_type != T_DIRECT)
6258                 control_dev = 1;
6259         else
6260                 control_dev = 0;
6261
6262         switch (ctsio->cdb[0]) {
6263         case MODE_SENSE_6: {
6264                 struct scsi_mode_sense_6 *cdb;
6265
6266                 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6267
6268                 header_len = sizeof(struct scsi_mode_hdr_6);
6269                 if (cdb->byte2 & SMS_DBD)
6270                         dbd = 1;
6271                 else
6272                         header_len += sizeof(struct scsi_mode_block_descr);
6273
6274                 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6275                 page_code = cdb->page & SMS_PAGE_CODE;
6276                 subpage = cdb->subpage;
6277                 alloc_len = cdb->length;
6278                 break;
6279         }
6280         case MODE_SENSE_10: {
6281                 struct scsi_mode_sense_10 *cdb;
6282
6283                 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6284
6285                 header_len = sizeof(struct scsi_mode_hdr_10);
6286
6287                 if (cdb->byte2 & SMS_DBD)
6288                         dbd = 1;
6289                 else
6290                         header_len += sizeof(struct scsi_mode_block_descr);
6291                 if (cdb->byte2 & SMS10_LLBAA)
6292                         llba = 1;
6293                 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6294                 page_code = cdb->page & SMS_PAGE_CODE;
6295                 subpage = cdb->subpage;
6296                 alloc_len = scsi_2btoul(cdb->length);
6297                 break;
6298         }
6299         default:
6300                 ctl_set_invalid_opcode(ctsio);
6301                 ctl_done((union ctl_io *)ctsio);
6302                 return (CTL_RETVAL_COMPLETE);
6303                 break; /* NOTREACHED */
6304         }
6305
6306         /*
6307          * We have to make a first pass through to calculate the size of
6308          * the pages that match the user's query.  Then we allocate enough
6309          * memory to hold it, and actually copy the data into the buffer.
6310          */
6311         switch (page_code) {
6312         case SMS_ALL_PAGES_PAGE: {
6313                 int i;
6314
6315                 page_len = 0;
6316
6317                 /*
6318                  * At the moment, values other than 0 and 0xff here are
6319                  * reserved according to SPC-3.
6320                  */
6321                 if ((subpage != SMS_SUBPAGE_PAGE_0)
6322                  && (subpage != SMS_SUBPAGE_ALL)) {
6323                         ctl_set_invalid_field(ctsio,
6324                                               /*sks_valid*/ 1,
6325                                               /*command*/ 1,
6326                                               /*field*/ 3,
6327                                               /*bit_valid*/ 0,
6328                                               /*bit*/ 0);
6329                         ctl_done((union ctl_io *)ctsio);
6330                         return (CTL_RETVAL_COMPLETE);
6331                 }
6332
6333                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6334                         if ((control_dev != 0)
6335                          && (lun->mode_pages.index[i].page_flags &
6336                              CTL_PAGE_FLAG_DISK_ONLY))
6337                                 continue;
6338
6339                         /*
6340                          * We don't use this subpage if the user didn't
6341                          * request all subpages.
6342                          */
6343                         if ((lun->mode_pages.index[i].subpage != 0)
6344                          && (subpage == SMS_SUBPAGE_PAGE_0))
6345                                 continue;
6346
6347 #if 0
6348                         printf("found page %#x len %d\n",
6349                                lun->mode_pages.index[i].page_code &
6350                                SMPH_PC_MASK,
6351                                lun->mode_pages.index[i].page_len);
6352 #endif
6353                         page_len += lun->mode_pages.index[i].page_len;
6354                 }
6355                 break;
6356         }
6357         default: {
6358                 int i;
6359
6360                 page_len = 0;
6361
6362                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6363                         /* Look for the right page code */
6364                         if ((lun->mode_pages.index[i].page_code &
6365                              SMPH_PC_MASK) != page_code)
6366                                 continue;
6367
6368                         /* Look for the right subpage or the subpage wildcard*/
6369                         if ((lun->mode_pages.index[i].subpage != subpage)
6370                          && (subpage != SMS_SUBPAGE_ALL))
6371                                 continue;
6372
6373                         /* Make sure the page is supported for this dev type */
6374                         if ((control_dev != 0)
6375                          && (lun->mode_pages.index[i].page_flags &
6376                              CTL_PAGE_FLAG_DISK_ONLY))
6377                                 continue;
6378
6379 #if 0
6380                         printf("found page %#x len %d\n",
6381                                lun->mode_pages.index[i].page_code &
6382                                SMPH_PC_MASK,
6383                                lun->mode_pages.index[i].page_len);
6384 #endif
6385
6386                         page_len += lun->mode_pages.index[i].page_len;
6387                 }
6388
6389                 if (page_len == 0) {
6390                         ctl_set_invalid_field(ctsio,
6391                                               /*sks_valid*/ 1,
6392                                               /*command*/ 1,
6393                                               /*field*/ 2,
6394                                               /*bit_valid*/ 1,
6395                                               /*bit*/ 5);
6396                         ctl_done((union ctl_io *)ctsio);
6397                         return (CTL_RETVAL_COMPLETE);
6398                 }
6399                 break;
6400         }
6401         }
6402
6403         total_len = header_len + page_len;
6404 #if 0
6405         printf("header_len = %d, page_len = %d, total_len = %d\n",
6406                header_len, page_len, total_len);
6407 #endif
6408
6409         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6410         ctsio->kern_sg_entries = 0;
6411         ctsio->kern_data_resid = 0;
6412         ctsio->kern_rel_offset = 0;
6413         if (total_len < alloc_len) {
6414                 ctsio->residual = alloc_len - total_len;
6415                 ctsio->kern_data_len = total_len;
6416                 ctsio->kern_total_len = total_len;
6417         } else {
6418                 ctsio->residual = 0;
6419                 ctsio->kern_data_len = alloc_len;
6420                 ctsio->kern_total_len = alloc_len;
6421         }
6422
6423         switch (ctsio->cdb[0]) {
6424         case MODE_SENSE_6: {
6425                 struct scsi_mode_hdr_6 *header;
6426
6427                 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6428
6429                 header->datalen = MIN(total_len - 1, 254);
6430                 if (control_dev == 0) {
6431                         header->dev_specific = 0x10; /* DPOFUA */
6432                         if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6433                             (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6434                             .eca_and_aen & SCP_SWP) != 0)
6435                                     header->dev_specific |= 0x80; /* WP */
6436                 }
6437                 if (dbd)
6438                         header->block_descr_len = 0;
6439                 else
6440                         header->block_descr_len =
6441                                 sizeof(struct scsi_mode_block_descr);
6442                 block_desc = (struct scsi_mode_block_descr *)&header[1];
6443                 break;
6444         }
6445         case MODE_SENSE_10: {
6446                 struct scsi_mode_hdr_10 *header;
6447                 int datalen;
6448
6449                 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6450
6451                 datalen = MIN(total_len - 2, 65533);
6452                 scsi_ulto2b(datalen, header->datalen);
6453                 if (control_dev == 0) {
6454                         header->dev_specific = 0x10; /* DPOFUA */
6455                         if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6456                             (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6457                             .eca_and_aen & SCP_SWP) != 0)
6458                                     header->dev_specific |= 0x80; /* WP */
6459                 }
6460                 if (dbd)
6461                         scsi_ulto2b(0, header->block_descr_len);
6462                 else
6463                         scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6464                                     header->block_descr_len);
6465                 block_desc = (struct scsi_mode_block_descr *)&header[1];
6466                 break;
6467         }
6468         default:
6469                 panic("invalid CDB type %#x", ctsio->cdb[0]);
6470                 break; /* NOTREACHED */
6471         }
6472
6473         /*
6474          * If we've got a disk, use its blocksize in the block
6475          * descriptor.  Otherwise, just set it to 0.
6476          */
6477         if (dbd == 0) {
6478                 if (control_dev == 0)
6479                         scsi_ulto3b(lun->be_lun->blocksize,
6480                                     block_desc->block_len);
6481                 else
6482                         scsi_ulto3b(0, block_desc->block_len);
6483         }
6484
6485         switch (page_code) {
6486         case SMS_ALL_PAGES_PAGE: {
6487                 int i, data_used;
6488
6489                 data_used = header_len;
6490                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6491                         struct ctl_page_index *page_index;
6492
6493                         page_index = &lun->mode_pages.index[i];
6494
6495                         if ((control_dev != 0)
6496                          && (page_index->page_flags &
6497                             CTL_PAGE_FLAG_DISK_ONLY))
6498                                 continue;
6499
6500                         /*
6501                          * We don't use this subpage if the user didn't
6502                          * request all subpages.  We already checked (above)
6503                          * to make sure the user only specified a subpage
6504                          * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6505                          */
6506                         if ((page_index->subpage != 0)
6507                          && (subpage == SMS_SUBPAGE_PAGE_0))
6508                                 continue;
6509
6510                         /*
6511                          * Call the handler, if it exists, to update the
6512                          * page to the latest values.
6513                          */
6514                         if (page_index->sense_handler != NULL)
6515                                 page_index->sense_handler(ctsio, page_index,pc);
6516
6517                         memcpy(ctsio->kern_data_ptr + data_used,
6518                                page_index->page_data +
6519                                (page_index->page_len * pc),
6520                                page_index->page_len);
6521                         data_used += page_index->page_len;
6522                 }
6523                 break;
6524         }
6525         default: {
6526                 int i, data_used;
6527
6528                 data_used = header_len;
6529
6530                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6531                         struct ctl_page_index *page_index;
6532
6533                         page_index = &lun->mode_pages.index[i];
6534
6535                         /* Look for the right page code */
6536                         if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6537                                 continue;
6538
6539                         /* Look for the right subpage or the subpage wildcard*/
6540                         if ((page_index->subpage != subpage)
6541                          && (subpage != SMS_SUBPAGE_ALL))
6542                                 continue;
6543
6544                         /* Make sure the page is supported for this dev type */
6545                         if ((control_dev != 0)
6546                          && (page_index->page_flags &
6547                              CTL_PAGE_FLAG_DISK_ONLY))
6548                                 continue;
6549
6550                         /*
6551                          * Call the handler, if it exists, to update the
6552                          * page to the latest values.
6553                          */
6554                         if (page_index->sense_handler != NULL)
6555                                 page_index->sense_handler(ctsio, page_index,pc);
6556
6557                         memcpy(ctsio->kern_data_ptr + data_used,
6558                                page_index->page_data +
6559                                (page_index->page_len * pc),
6560                                page_index->page_len);
6561                         data_used += page_index->page_len;
6562                 }
6563                 break;
6564         }
6565         }
6566
6567         ctl_set_success(ctsio);
6568         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6569         ctsio->be_move_done = ctl_config_move_done;
6570         ctl_datamove((union ctl_io *)ctsio);
6571         return (CTL_RETVAL_COMPLETE);
6572 }
6573
6574 int
6575 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6576                                struct ctl_page_index *page_index,
6577                                int pc)
6578 {
6579         struct ctl_lun *lun;
6580         struct scsi_log_param_header *phdr;
6581         uint8_t *data;
6582         uint64_t val;
6583
6584         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6585         data = page_index->page_data;
6586
6587         if (lun->backend->lun_attr != NULL &&
6588             (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6589              != UINT64_MAX) {
6590                 phdr = (struct scsi_log_param_header *)data;
6591                 scsi_ulto2b(0x0001, phdr->param_code);
6592                 phdr->param_control = SLP_LBIN | SLP_LP;
6593                 phdr->param_len = 8;
6594                 data = (uint8_t *)(phdr + 1);
6595                 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6596                 data[4] = 0x02; /* per-pool */
6597                 data += phdr->param_len;
6598         }
6599
6600         if (lun->backend->lun_attr != NULL &&
6601             (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6602              != UINT64_MAX) {
6603                 phdr = (struct scsi_log_param_header *)data;
6604                 scsi_ulto2b(0x0002, phdr->param_code);
6605                 phdr->param_control = SLP_LBIN | SLP_LP;
6606                 phdr->param_len = 8;
6607                 data = (uint8_t *)(phdr + 1);
6608                 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6609                 data[4] = 0x01; /* per-LUN */
6610                 data += phdr->param_len;
6611         }
6612
6613         if (lun->backend->lun_attr != NULL &&
6614             (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6615              != UINT64_MAX) {
6616                 phdr = (struct scsi_log_param_header *)data;
6617                 scsi_ulto2b(0x00f1, phdr->param_code);
6618                 phdr->param_control = SLP_LBIN | SLP_LP;
6619                 phdr->param_len = 8;
6620                 data = (uint8_t *)(phdr + 1);
6621                 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6622                 data[4] = 0x02; /* per-pool */
6623                 data += phdr->param_len;
6624         }
6625
6626         if (lun->backend->lun_attr != NULL &&
6627             (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6628              != UINT64_MAX) {
6629                 phdr = (struct scsi_log_param_header *)data;
6630                 scsi_ulto2b(0x00f2, phdr->param_code);
6631                 phdr->param_control = SLP_LBIN | SLP_LP;
6632                 phdr->param_len = 8;
6633                 data = (uint8_t *)(phdr + 1);
6634                 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6635                 data[4] = 0x02; /* per-pool */
6636                 data += phdr->param_len;
6637         }
6638
6639         page_index->page_len = data - page_index->page_data;
6640         return (0);
6641 }
6642
6643 int
6644 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6645                                struct ctl_page_index *page_index,
6646                                int pc)
6647 {
6648         struct ctl_lun *lun;
6649         struct stat_page *data;
6650         uint64_t rn, wn, rb, wb;
6651         struct bintime rt, wt;
6652         int i;
6653
6654         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6655         data = (struct stat_page *)page_index->page_data;
6656
6657         scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6658         data->sap.hdr.param_control = SLP_LBIN;
6659         data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6660             sizeof(struct scsi_log_param_header);
6661         rn = wn = rb = wb = 0;
6662         bintime_clear(&rt);
6663         bintime_clear(&wt);
6664         for (i = 0; i < CTL_MAX_PORTS; i++) {
6665                 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6666                 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6667                 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6668                 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6669                 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6670                 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6671         }
6672         scsi_u64to8b(rn, data->sap.read_num);
6673         scsi_u64to8b(wn, data->sap.write_num);
6674         if (lun->stats.blocksize > 0) {
6675                 scsi_u64to8b(wb / lun->stats.blocksize,
6676                     data->sap.recvieved_lba);
6677                 scsi_u64to8b(rb / lun->stats.blocksize,
6678                     data->sap.transmitted_lba);
6679         }
6680         scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6681             data->sap.read_int);
6682         scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6683             data->sap.write_int);
6684         scsi_u64to8b(0, data->sap.weighted_num);
6685         scsi_u64to8b(0, data->sap.weighted_int);
6686         scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6687         data->it.hdr.param_control = SLP_LBIN;
6688         data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6689             sizeof(struct scsi_log_param_header);
6690 #ifdef CTL_TIME_IO
6691         scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6692 #endif
6693         scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6694         data->it.hdr.param_control = SLP_LBIN;
6695         data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6696             sizeof(struct scsi_log_param_header);
6697         scsi_ulto4b(3, data->ti.exponent);
6698         scsi_ulto4b(1, data->ti.integer);
6699
6700         page_index->page_len = sizeof(*data);
6701         return (0);
6702 }
6703
6704 int
6705 ctl_log_sense(struct ctl_scsiio *ctsio)
6706 {
6707         struct ctl_lun *lun;
6708         int i, pc, page_code, subpage;
6709         int alloc_len, total_len;
6710         struct ctl_page_index *page_index;
6711         struct scsi_log_sense *cdb;
6712         struct scsi_log_header *header;
6713
6714         CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6715
6716         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6717         cdb = (struct scsi_log_sense *)ctsio->cdb;
6718         pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6719         page_code = cdb->page & SLS_PAGE_CODE;
6720         subpage = cdb->subpage;
6721         alloc_len = scsi_2btoul(cdb->length);
6722
6723         page_index = NULL;
6724         for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6725                 page_index = &lun->log_pages.index[i];
6726
6727                 /* Look for the right page code */
6728                 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6729                         continue;
6730
6731                 /* Look for the right subpage or the subpage wildcard*/
6732                 if (page_index->subpage != subpage)
6733                         continue;
6734
6735                 break;
6736         }
6737         if (i >= CTL_NUM_LOG_PAGES) {
6738                 ctl_set_invalid_field(ctsio,
6739                                       /*sks_valid*/ 1,
6740                                       /*command*/ 1,
6741                                       /*field*/ 2,
6742                                       /*bit_valid*/ 0,
6743                                       /*bit*/ 0);
6744                 ctl_done((union ctl_io *)ctsio);
6745                 return (CTL_RETVAL_COMPLETE);
6746         }
6747
6748         total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6749
6750         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6751         ctsio->kern_sg_entries = 0;
6752         ctsio->kern_data_resid = 0;
6753         ctsio->kern_rel_offset = 0;
6754         if (total_len < alloc_len) {
6755                 ctsio->residual = alloc_len - total_len;
6756                 ctsio->kern_data_len = total_len;
6757                 ctsio->kern_total_len = total_len;
6758         } else {
6759                 ctsio->residual = 0;
6760                 ctsio->kern_data_len = alloc_len;
6761                 ctsio->kern_total_len = alloc_len;
6762         }
6763
6764         header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6765         header->page = page_index->page_code;
6766         if (page_index->subpage) {
6767                 header->page |= SL_SPF;
6768                 header->subpage = page_index->subpage;
6769         }
6770         scsi_ulto2b(page_index->page_len, header->datalen);
6771
6772         /*
6773          * Call the handler, if it exists, to update the
6774          * page to the latest values.
6775          */
6776         if (page_index->sense_handler != NULL)
6777                 page_index->sense_handler(ctsio, page_index, pc);
6778
6779         memcpy(header + 1, page_index->page_data, page_index->page_len);
6780
6781         ctl_set_success(ctsio);
6782         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6783         ctsio->be_move_done = ctl_config_move_done;
6784         ctl_datamove((union ctl_io *)ctsio);
6785         return (CTL_RETVAL_COMPLETE);
6786 }
6787
6788 int
6789 ctl_read_capacity(struct ctl_scsiio *ctsio)
6790 {
6791         struct scsi_read_capacity *cdb;
6792         struct scsi_read_capacity_data *data;
6793         struct ctl_lun *lun;
6794         uint32_t lba;
6795
6796         CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6797
6798         cdb = (struct scsi_read_capacity *)ctsio->cdb;
6799
6800         lba = scsi_4btoul(cdb->addr);
6801         if (((cdb->pmi & SRC_PMI) == 0)
6802          && (lba != 0)) {
6803                 ctl_set_invalid_field(/*ctsio*/ ctsio,
6804                                       /*sks_valid*/ 1,
6805                                       /*command*/ 1,
6806                                       /*field*/ 2,
6807                                       /*bit_valid*/ 0,
6808                                       /*bit*/ 0);
6809                 ctl_done((union ctl_io *)ctsio);
6810                 return (CTL_RETVAL_COMPLETE);
6811         }
6812
6813         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6814
6815         ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6816         data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6817         ctsio->residual = 0;
6818         ctsio->kern_data_len = sizeof(*data);
6819         ctsio->kern_total_len = sizeof(*data);
6820         ctsio->kern_data_resid = 0;
6821         ctsio->kern_rel_offset = 0;
6822         ctsio->kern_sg_entries = 0;
6823
6824         /*
6825          * If the maximum LBA is greater than 0xfffffffe, the user must
6826          * issue a SERVICE ACTION IN (16) command, with the read capacity
6827          * serivce action set.
6828          */
6829         if (lun->be_lun->maxlba > 0xfffffffe)
6830                 scsi_ulto4b(0xffffffff, data->addr);
6831         else
6832                 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6833
6834         /*
6835          * XXX KDM this may not be 512 bytes...
6836          */
6837         scsi_ulto4b(lun->be_lun->blocksize, data->length);
6838
6839         ctl_set_success(ctsio);
6840         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6841         ctsio->be_move_done = ctl_config_move_done;
6842         ctl_datamove((union ctl_io *)ctsio);
6843         return (CTL_RETVAL_COMPLETE);
6844 }
6845
6846 int
6847 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6848 {
6849         struct scsi_read_capacity_16 *cdb;
6850         struct scsi_read_capacity_data_long *data;
6851         struct ctl_lun *lun;
6852         uint64_t lba;
6853         uint32_t alloc_len;
6854
6855         CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6856
6857         cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6858
6859         alloc_len = scsi_4btoul(cdb->alloc_len);
6860         lba = scsi_8btou64(cdb->addr);
6861
6862         if ((cdb->reladr & SRC16_PMI)
6863          && (lba != 0)) {
6864                 ctl_set_invalid_field(/*ctsio*/ ctsio,
6865                                       /*sks_valid*/ 1,
6866                                       /*command*/ 1,
6867                                       /*field*/ 2,
6868                                       /*bit_valid*/ 0,
6869                                       /*bit*/ 0);
6870                 ctl_done((union ctl_io *)ctsio);
6871                 return (CTL_RETVAL_COMPLETE);
6872         }
6873
6874         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6875
6876         ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6877         data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6878
6879         if (sizeof(*data) < alloc_len) {
6880                 ctsio->residual = alloc_len - sizeof(*data);
6881                 ctsio->kern_data_len = sizeof(*data);
6882                 ctsio->kern_total_len = sizeof(*data);
6883         } else {
6884                 ctsio->residual = 0;
6885                 ctsio->kern_data_len = alloc_len;
6886                 ctsio->kern_total_len = alloc_len;
6887         }
6888         ctsio->kern_data_resid = 0;
6889         ctsio->kern_rel_offset = 0;
6890         ctsio->kern_sg_entries = 0;
6891
6892         scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6893         /* XXX KDM this may not be 512 bytes... */
6894         scsi_ulto4b(lun->be_lun->blocksize, data->length);
6895         data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6896         scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6897         if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6898                 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6899
6900         ctl_set_success(ctsio);
6901         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6902         ctsio->be_move_done = ctl_config_move_done;
6903         ctl_datamove((union ctl_io *)ctsio);
6904         return (CTL_RETVAL_COMPLETE);
6905 }
6906
6907 int
6908 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6909 {
6910         struct scsi_get_lba_status *cdb;
6911         struct scsi_get_lba_status_data *data;
6912         struct ctl_lun *lun;
6913         struct ctl_lba_len_flags *lbalen;
6914         uint64_t lba;
6915         uint32_t alloc_len, total_len;
6916         int retval;
6917
6918         CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6919
6920         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6921         cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6922         lba = scsi_8btou64(cdb->addr);
6923         alloc_len = scsi_4btoul(cdb->alloc_len);
6924
6925         if (lba > lun->be_lun->maxlba) {
6926                 ctl_set_lba_out_of_range(ctsio);
6927                 ctl_done((union ctl_io *)ctsio);
6928                 return (CTL_RETVAL_COMPLETE);
6929         }
6930
6931         total_len = sizeof(*data) + sizeof(data->descr[0]);
6932         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6933         data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6934
6935         if (total_len < alloc_len) {
6936                 ctsio->residual = alloc_len - total_len;
6937                 ctsio->kern_data_len = total_len;
6938                 ctsio->kern_total_len = total_len;
6939         } else {
6940                 ctsio->residual = 0;
6941                 ctsio->kern_data_len = alloc_len;
6942                 ctsio->kern_total_len = alloc_len;
6943         }
6944         ctsio->kern_data_resid = 0;
6945         ctsio->kern_rel_offset = 0;
6946         ctsio->kern_sg_entries = 0;
6947
6948         /* Fill dummy data in case backend can't tell anything. */
6949         scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6950         scsi_u64to8b(lba, data->descr[0].addr);
6951         scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
6952             data->descr[0].length);
6953         data->descr[0].status = 0; /* Mapped or unknown. */
6954
6955         ctl_set_success(ctsio);
6956         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6957         ctsio->be_move_done = ctl_config_move_done;
6958
6959         lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6960         lbalen->lba = lba;
6961         lbalen->len = total_len;
6962         lbalen->flags = 0;
6963         retval = lun->backend->config_read((union ctl_io *)ctsio);
6964         return (CTL_RETVAL_COMPLETE);
6965 }
6966
6967 int
6968 ctl_read_defect(struct ctl_scsiio *ctsio)
6969 {
6970         struct scsi_read_defect_data_10 *ccb10;
6971         struct scsi_read_defect_data_12 *ccb12;
6972         struct scsi_read_defect_data_hdr_10 *data10;
6973         struct scsi_read_defect_data_hdr_12 *data12;
6974         uint32_t alloc_len, data_len;
6975         uint8_t format;
6976
6977         CTL_DEBUG_PRINT(("ctl_read_defect\n"));
6978
6979         if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
6980                 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
6981                 format = ccb10->format;
6982                 alloc_len = scsi_2btoul(ccb10->alloc_length);
6983                 data_len = sizeof(*data10);
6984         } else {
6985                 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
6986                 format = ccb12->format;
6987                 alloc_len = scsi_4btoul(ccb12->alloc_length);
6988                 data_len = sizeof(*data12);
6989         }
6990         if (alloc_len == 0) {
6991                 ctl_set_success(ctsio);
6992                 ctl_done((union ctl_io *)ctsio);
6993                 return (CTL_RETVAL_COMPLETE);
6994         }
6995
6996         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
6997         if (data_len < alloc_len) {
6998                 ctsio->residual = alloc_len - data_len;
6999                 ctsio->kern_data_len = data_len;
7000                 ctsio->kern_total_len = data_len;
7001         } else {
7002                 ctsio->residual = 0;
7003                 ctsio->kern_data_len = alloc_len;
7004                 ctsio->kern_total_len = alloc_len;
7005         }
7006         ctsio->kern_data_resid = 0;
7007         ctsio->kern_rel_offset = 0;
7008         ctsio->kern_sg_entries = 0;
7009
7010         if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7011                 data10 = (struct scsi_read_defect_data_hdr_10 *)
7012                     ctsio->kern_data_ptr;
7013                 data10->format = format;
7014                 scsi_ulto2b(0, data10->length);
7015         } else {
7016                 data12 = (struct scsi_read_defect_data_hdr_12 *)
7017                     ctsio->kern_data_ptr;
7018                 data12->format = format;
7019                 scsi_ulto2b(0, data12->generation);
7020                 scsi_ulto4b(0, data12->length);
7021         }
7022
7023         ctl_set_success(ctsio);
7024         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7025         ctsio->be_move_done = ctl_config_move_done;
7026         ctl_datamove((union ctl_io *)ctsio);
7027         return (CTL_RETVAL_COMPLETE);
7028 }
7029
7030 int
7031 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7032 {
7033         struct scsi_maintenance_in *cdb;
7034         int retval;
7035         int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7036         int num_target_port_groups, num_target_ports;
7037         struct ctl_lun *lun;
7038         struct ctl_softc *softc;
7039         struct ctl_port *port;
7040         struct scsi_target_group_data *rtg_ptr;
7041         struct scsi_target_group_data_extended *rtg_ext_ptr;
7042         struct scsi_target_port_group_descriptor *tpg_desc;
7043
7044         CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7045
7046         cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7047         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7048         softc = lun->ctl_softc;
7049
7050         retval = CTL_RETVAL_COMPLETE;
7051
7052         switch (cdb->byte2 & STG_PDF_MASK) {
7053         case STG_PDF_LENGTH:
7054                 ext = 0;
7055                 break;
7056         case STG_PDF_EXTENDED:
7057                 ext = 1;
7058                 break;
7059         default:
7060                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7061                                       /*sks_valid*/ 1,
7062                                       /*command*/ 1,
7063                                       /*field*/ 2,
7064                                       /*bit_valid*/ 1,
7065                                       /*bit*/ 5);
7066                 ctl_done((union ctl_io *)ctsio);
7067                 return(retval);
7068         }
7069
7070         if (softc->is_single)
7071                 num_target_port_groups = 1;
7072         else
7073                 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7074         num_target_ports = 0;
7075         mtx_lock(&softc->ctl_lock);
7076         STAILQ_FOREACH(port, &softc->port_list, links) {
7077                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7078                         continue;
7079                 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7080                         continue;
7081                 num_target_ports++;
7082         }
7083         mtx_unlock(&softc->ctl_lock);
7084
7085         if (ext)
7086                 total_len = sizeof(struct scsi_target_group_data_extended);
7087         else
7088                 total_len = sizeof(struct scsi_target_group_data);
7089         total_len += sizeof(struct scsi_target_port_group_descriptor) *
7090                 num_target_port_groups +
7091             sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7092
7093         alloc_len = scsi_4btoul(cdb->length);
7094
7095         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7096
7097         ctsio->kern_sg_entries = 0;
7098
7099         if (total_len < alloc_len) {
7100                 ctsio->residual = alloc_len - total_len;
7101                 ctsio->kern_data_len = total_len;
7102                 ctsio->kern_total_len = total_len;
7103         } else {
7104                 ctsio->residual = 0;
7105                 ctsio->kern_data_len = alloc_len;
7106                 ctsio->kern_total_len = alloc_len;
7107         }
7108         ctsio->kern_data_resid = 0;
7109         ctsio->kern_rel_offset = 0;
7110
7111         if (ext) {
7112                 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7113                     ctsio->kern_data_ptr;
7114                 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7115                 rtg_ext_ptr->format_type = 0x10;
7116                 rtg_ext_ptr->implicit_transition_time = 0;
7117                 tpg_desc = &rtg_ext_ptr->groups[0];
7118         } else {
7119                 rtg_ptr = (struct scsi_target_group_data *)
7120                     ctsio->kern_data_ptr;
7121                 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7122                 tpg_desc = &rtg_ptr->groups[0];
7123         }
7124
7125         mtx_lock(&softc->ctl_lock);
7126         pg = softc->port_min / softc->port_cnt;
7127         if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7128                 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7129         else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7130                 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7131         else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7132                 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7133         else
7134                 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7135         if (lun->flags & CTL_LUN_PRIMARY_SC) {
7136                 os = gs;
7137                 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7138         } else
7139                 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7140         for (g = 0; g < num_target_port_groups; g++) {
7141                 tpg_desc->pref_state = (g == pg) ? gs : os;
7142                 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7143                     TPG_U_SUP | TPG_T_SUP;
7144                 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7145                 tpg_desc->status = TPG_IMPLICIT;
7146                 pc = 0;
7147                 STAILQ_FOREACH(port, &softc->port_list, links) {
7148                         if (port->targ_port < g * softc->port_cnt ||
7149                             port->targ_port >= (g + 1) * softc->port_cnt)
7150                                 continue;
7151                         if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7152                                 continue;
7153                         if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7154                                 continue;
7155                         scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7156                             relative_target_port_identifier);
7157                         pc++;
7158                 }
7159                 tpg_desc->target_port_count = pc;
7160                 tpg_desc = (struct scsi_target_port_group_descriptor *)
7161                     &tpg_desc->descriptors[pc];
7162         }
7163         mtx_unlock(&softc->ctl_lock);
7164
7165         ctl_set_success(ctsio);
7166         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7167         ctsio->be_move_done = ctl_config_move_done;
7168         ctl_datamove((union ctl_io *)ctsio);
7169         return(retval);
7170 }
7171
7172 int
7173 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7174 {
7175         struct ctl_lun *lun;
7176         struct scsi_report_supported_opcodes *cdb;
7177         const struct ctl_cmd_entry *entry, *sentry;
7178         struct scsi_report_supported_opcodes_all *all;
7179         struct scsi_report_supported_opcodes_descr *descr;
7180         struct scsi_report_supported_opcodes_one *one;
7181         int retval;
7182         int alloc_len, total_len;
7183         int opcode, service_action, i, j, num;
7184
7185         CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7186
7187         cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7188         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7189
7190         retval = CTL_RETVAL_COMPLETE;
7191
7192         opcode = cdb->requested_opcode;
7193         service_action = scsi_2btoul(cdb->requested_service_action);
7194         switch (cdb->options & RSO_OPTIONS_MASK) {
7195         case RSO_OPTIONS_ALL:
7196                 num = 0;
7197                 for (i = 0; i < 256; i++) {
7198                         entry = &ctl_cmd_table[i];
7199                         if (entry->flags & CTL_CMD_FLAG_SA5) {
7200                                 for (j = 0; j < 32; j++) {
7201                                         sentry = &((const struct ctl_cmd_entry *)
7202                                             entry->execute)[j];
7203                                         if (ctl_cmd_applicable(
7204                                             lun->be_lun->lun_type, sentry))
7205                                                 num++;
7206                                 }
7207                         } else {
7208                                 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7209                                     entry))
7210                                         num++;
7211                         }
7212                 }
7213                 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7214                     num * sizeof(struct scsi_report_supported_opcodes_descr);
7215                 break;
7216         case RSO_OPTIONS_OC:
7217                 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7218                         ctl_set_invalid_field(/*ctsio*/ ctsio,
7219                                               /*sks_valid*/ 1,
7220                                               /*command*/ 1,
7221                                               /*field*/ 2,
7222                                               /*bit_valid*/ 1,
7223                                               /*bit*/ 2);
7224                         ctl_done((union ctl_io *)ctsio);
7225                         return (CTL_RETVAL_COMPLETE);
7226                 }
7227                 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7228                 break;
7229         case RSO_OPTIONS_OC_SA:
7230                 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7231                     service_action >= 32) {
7232                         ctl_set_invalid_field(/*ctsio*/ ctsio,
7233                                               /*sks_valid*/ 1,
7234                                               /*command*/ 1,
7235                                               /*field*/ 2,
7236                                               /*bit_valid*/ 1,
7237                                               /*bit*/ 2);
7238                         ctl_done((union ctl_io *)ctsio);
7239                         return (CTL_RETVAL_COMPLETE);
7240                 }
7241                 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7242                 break;
7243         default:
7244                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7245                                       /*sks_valid*/ 1,
7246                                       /*command*/ 1,
7247                                       /*field*/ 2,
7248                                       /*bit_valid*/ 1,
7249                                       /*bit*/ 2);
7250                 ctl_done((union ctl_io *)ctsio);
7251                 return (CTL_RETVAL_COMPLETE);
7252         }
7253
7254         alloc_len = scsi_4btoul(cdb->length);
7255
7256         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7257
7258         ctsio->kern_sg_entries = 0;
7259
7260         if (total_len < alloc_len) {
7261                 ctsio->residual = alloc_len - total_len;
7262                 ctsio->kern_data_len = total_len;
7263                 ctsio->kern_total_len = total_len;
7264         } else {
7265                 ctsio->residual = 0;
7266                 ctsio->kern_data_len = alloc_len;
7267                 ctsio->kern_total_len = alloc_len;
7268         }
7269         ctsio->kern_data_resid = 0;
7270         ctsio->kern_rel_offset = 0;
7271
7272         switch (cdb->options & RSO_OPTIONS_MASK) {
7273         case RSO_OPTIONS_ALL:
7274                 all = (struct scsi_report_supported_opcodes_all *)
7275                     ctsio->kern_data_ptr;
7276                 num = 0;
7277                 for (i = 0; i < 256; i++) {
7278                         entry = &ctl_cmd_table[i];
7279                         if (entry->flags & CTL_CMD_FLAG_SA5) {
7280                                 for (j = 0; j < 32; j++) {
7281                                         sentry = &((const struct ctl_cmd_entry *)
7282                                             entry->execute)[j];
7283                                         if (!ctl_cmd_applicable(
7284                                             lun->be_lun->lun_type, sentry))
7285                                                 continue;
7286                                         descr = &all->descr[num++];
7287                                         descr->opcode = i;
7288                                         scsi_ulto2b(j, descr->service_action);
7289                                         descr->flags = RSO_SERVACTV;
7290                                         scsi_ulto2b(sentry->length,
7291                                             descr->cdb_length);
7292                                 }
7293                         } else {
7294                                 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7295                                     entry))
7296                                         continue;
7297                                 descr = &all->descr[num++];
7298                                 descr->opcode = i;
7299                                 scsi_ulto2b(0, descr->service_action);
7300                                 descr->flags = 0;
7301                                 scsi_ulto2b(entry->length, descr->cdb_length);
7302                         }
7303                 }
7304                 scsi_ulto4b(
7305                     num * sizeof(struct scsi_report_supported_opcodes_descr),
7306                     all->length);
7307                 break;
7308         case RSO_OPTIONS_OC:
7309                 one = (struct scsi_report_supported_opcodes_one *)
7310                     ctsio->kern_data_ptr;
7311                 entry = &ctl_cmd_table[opcode];
7312                 goto fill_one;
7313         case RSO_OPTIONS_OC_SA:
7314                 one = (struct scsi_report_supported_opcodes_one *)
7315                     ctsio->kern_data_ptr;
7316                 entry = &ctl_cmd_table[opcode];
7317                 entry = &((const struct ctl_cmd_entry *)
7318                     entry->execute)[service_action];
7319 fill_one:
7320                 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7321                         one->support = 3;
7322                         scsi_ulto2b(entry->length, one->cdb_length);
7323                         one->cdb_usage[0] = opcode;
7324                         memcpy(&one->cdb_usage[1], entry->usage,
7325                             entry->length - 1);
7326                 } else
7327                         one->support = 1;
7328                 break;
7329         }
7330
7331         ctl_set_success(ctsio);
7332         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7333         ctsio->be_move_done = ctl_config_move_done;
7334         ctl_datamove((union ctl_io *)ctsio);
7335         return(retval);
7336 }
7337
7338 int
7339 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7340 {
7341         struct scsi_report_supported_tmf *cdb;
7342         struct scsi_report_supported_tmf_data *data;
7343         int retval;
7344         int alloc_len, total_len;
7345
7346         CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7347
7348         cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7349
7350         retval = CTL_RETVAL_COMPLETE;
7351
7352         total_len = sizeof(struct scsi_report_supported_tmf_data);
7353         alloc_len = scsi_4btoul(cdb->length);
7354
7355         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7356
7357         ctsio->kern_sg_entries = 0;
7358
7359         if (total_len < alloc_len) {
7360                 ctsio->residual = alloc_len - total_len;
7361                 ctsio->kern_data_len = total_len;
7362                 ctsio->kern_total_len = total_len;
7363         } else {
7364                 ctsio->residual = 0;
7365                 ctsio->kern_data_len = alloc_len;
7366                 ctsio->kern_total_len = alloc_len;
7367         }
7368         ctsio->kern_data_resid = 0;
7369         ctsio->kern_rel_offset = 0;
7370
7371         data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7372         data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7373             RST_TRS;
7374         data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7375
7376         ctl_set_success(ctsio);
7377         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7378         ctsio->be_move_done = ctl_config_move_done;
7379         ctl_datamove((union ctl_io *)ctsio);
7380         return (retval);
7381 }
7382
7383 int
7384 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7385 {
7386         struct scsi_report_timestamp *cdb;
7387         struct scsi_report_timestamp_data *data;
7388         struct timeval tv;
7389         int64_t timestamp;
7390         int retval;
7391         int alloc_len, total_len;
7392
7393         CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7394
7395         cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7396
7397         retval = CTL_RETVAL_COMPLETE;
7398
7399         total_len = sizeof(struct scsi_report_timestamp_data);
7400         alloc_len = scsi_4btoul(cdb->length);
7401
7402         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7403
7404         ctsio->kern_sg_entries = 0;
7405
7406         if (total_len < alloc_len) {
7407                 ctsio->residual = alloc_len - total_len;
7408                 ctsio->kern_data_len = total_len;
7409                 ctsio->kern_total_len = total_len;
7410         } else {
7411                 ctsio->residual = 0;
7412                 ctsio->kern_data_len = alloc_len;
7413                 ctsio->kern_total_len = alloc_len;
7414         }
7415         ctsio->kern_data_resid = 0;
7416         ctsio->kern_rel_offset = 0;
7417
7418         data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7419         scsi_ulto2b(sizeof(*data) - 2, data->length);
7420         data->origin = RTS_ORIG_OUTSIDE;
7421         getmicrotime(&tv);
7422         timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7423         scsi_ulto4b(timestamp >> 16, data->timestamp);
7424         scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7425
7426         ctl_set_success(ctsio);
7427         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7428         ctsio->be_move_done = ctl_config_move_done;
7429         ctl_datamove((union ctl_io *)ctsio);
7430         return (retval);
7431 }
7432
7433 int
7434 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7435 {
7436         struct scsi_per_res_in *cdb;
7437         int alloc_len, total_len = 0;
7438         /* struct scsi_per_res_in_rsrv in_data; */
7439         struct ctl_lun *lun;
7440         struct ctl_softc *softc;
7441         uint64_t key;
7442
7443         CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7444
7445         cdb = (struct scsi_per_res_in *)ctsio->cdb;
7446
7447         alloc_len = scsi_2btoul(cdb->length);
7448
7449         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7450         softc = lun->ctl_softc;
7451
7452 retry:
7453         mtx_lock(&lun->lun_lock);
7454         switch (cdb->action) {
7455         case SPRI_RK: /* read keys */
7456                 total_len = sizeof(struct scsi_per_res_in_keys) +
7457                         lun->pr_key_count *
7458                         sizeof(struct scsi_per_res_key);
7459                 break;
7460         case SPRI_RR: /* read reservation */
7461                 if (lun->flags & CTL_LUN_PR_RESERVED)
7462                         total_len = sizeof(struct scsi_per_res_in_rsrv);
7463                 else
7464                         total_len = sizeof(struct scsi_per_res_in_header);
7465                 break;
7466         case SPRI_RC: /* report capabilities */
7467                 total_len = sizeof(struct scsi_per_res_cap);
7468                 break;
7469         case SPRI_RS: /* read full status */
7470                 total_len = sizeof(struct scsi_per_res_in_header) +
7471                     (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7472                     lun->pr_key_count;
7473                 break;
7474         default:
7475                 panic("Invalid PR type %x", cdb->action);
7476         }
7477         mtx_unlock(&lun->lun_lock);
7478
7479         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7480
7481         if (total_len < alloc_len) {
7482                 ctsio->residual = alloc_len - total_len;
7483                 ctsio->kern_data_len = total_len;
7484                 ctsio->kern_total_len = total_len;
7485         } else {
7486                 ctsio->residual = 0;
7487                 ctsio->kern_data_len = alloc_len;
7488                 ctsio->kern_total_len = alloc_len;
7489         }
7490
7491         ctsio->kern_data_resid = 0;
7492         ctsio->kern_rel_offset = 0;
7493         ctsio->kern_sg_entries = 0;
7494
7495         mtx_lock(&lun->lun_lock);
7496         switch (cdb->action) {
7497         case SPRI_RK: { // read keys
7498         struct scsi_per_res_in_keys *res_keys;
7499                 int i, key_count;
7500
7501                 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7502
7503                 /*
7504                  * We had to drop the lock to allocate our buffer, which
7505                  * leaves time for someone to come in with another
7506                  * persistent reservation.  (That is unlikely, though,
7507                  * since this should be the only persistent reservation
7508                  * command active right now.)
7509                  */
7510                 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7511                     (lun->pr_key_count *
7512                      sizeof(struct scsi_per_res_key)))){
7513                         mtx_unlock(&lun->lun_lock);
7514                         free(ctsio->kern_data_ptr, M_CTL);
7515                         printf("%s: reservation length changed, retrying\n",
7516                                __func__);
7517                         goto retry;
7518                 }
7519
7520                 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7521
7522                 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7523                              lun->pr_key_count, res_keys->header.length);
7524
7525                 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7526                         if ((key = ctl_get_prkey(lun, i)) == 0)
7527                                 continue;
7528
7529                         /*
7530                          * We used lun->pr_key_count to calculate the
7531                          * size to allocate.  If it turns out the number of
7532                          * initiators with the registered flag set is
7533                          * larger than that (i.e. they haven't been kept in
7534                          * sync), we've got a problem.
7535                          */
7536                         if (key_count >= lun->pr_key_count) {
7537 #ifdef NEEDTOPORT
7538                                 csevent_log(CSC_CTL | CSC_SHELF_SW |
7539                                             CTL_PR_ERROR,
7540                                             csevent_LogType_Fault,
7541                                             csevent_AlertLevel_Yellow,
7542                                             csevent_FRU_ShelfController,
7543                                             csevent_FRU_Firmware,
7544                                         csevent_FRU_Unknown,
7545                                             "registered keys %d >= key "
7546                                             "count %d", key_count,
7547                                             lun->pr_key_count);
7548 #endif
7549                                 key_count++;
7550                                 continue;
7551                         }
7552                         scsi_u64to8b(key, res_keys->keys[key_count].key);
7553                         key_count++;
7554                 }
7555                 break;
7556         }
7557         case SPRI_RR: { // read reservation
7558                 struct scsi_per_res_in_rsrv *res;
7559                 int tmp_len, header_only;
7560
7561                 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7562
7563                 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7564
7565                 if (lun->flags & CTL_LUN_PR_RESERVED)
7566                 {
7567                         tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7568                         scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7569                                     res->header.length);
7570                         header_only = 0;
7571                 } else {
7572                         tmp_len = sizeof(struct scsi_per_res_in_header);
7573                         scsi_ulto4b(0, res->header.length);
7574                         header_only = 1;
7575                 }
7576
7577                 /*
7578                  * We had to drop the lock to allocate our buffer, which
7579                  * leaves time for someone to come in with another
7580                  * persistent reservation.  (That is unlikely, though,
7581                  * since this should be the only persistent reservation
7582                  * command active right now.)
7583                  */
7584                 if (tmp_len != total_len) {
7585                         mtx_unlock(&lun->lun_lock);
7586                         free(ctsio->kern_data_ptr, M_CTL);
7587                         printf("%s: reservation status changed, retrying\n",
7588                                __func__);
7589                         goto retry;
7590                 }
7591
7592                 /*
7593                  * No reservation held, so we're done.
7594                  */
7595                 if (header_only != 0)
7596                         break;
7597
7598                 /*
7599                  * If the registration is an All Registrants type, the key
7600                  * is 0, since it doesn't really matter.
7601                  */
7602                 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7603                         scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7604                             res->data.reservation);
7605                 }
7606                 res->data.scopetype = lun->res_type;
7607                 break;
7608         }
7609         case SPRI_RC:     //report capabilities
7610         {
7611                 struct scsi_per_res_cap *res_cap;
7612                 uint16_t type_mask;
7613
7614                 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7615                 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7616                 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7617                 type_mask = SPRI_TM_WR_EX_AR |
7618                             SPRI_TM_EX_AC_RO |
7619                             SPRI_TM_WR_EX_RO |
7620                             SPRI_TM_EX_AC |
7621                             SPRI_TM_WR_EX |
7622                             SPRI_TM_EX_AC_AR;
7623                 scsi_ulto2b(type_mask, res_cap->type_mask);
7624                 break;
7625         }
7626         case SPRI_RS: { // read full status
7627                 struct scsi_per_res_in_full *res_status;
7628                 struct scsi_per_res_in_full_desc *res_desc;
7629                 struct ctl_port *port;
7630                 int i, len;
7631
7632                 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7633
7634                 /*
7635                  * We had to drop the lock to allocate our buffer, which
7636                  * leaves time for someone to come in with another
7637                  * persistent reservation.  (That is unlikely, though,
7638                  * since this should be the only persistent reservation
7639                  * command active right now.)
7640                  */
7641                 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7642                     (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7643                      lun->pr_key_count)){
7644                         mtx_unlock(&lun->lun_lock);
7645                         free(ctsio->kern_data_ptr, M_CTL);
7646                         printf("%s: reservation length changed, retrying\n",
7647                                __func__);
7648                         goto retry;
7649                 }
7650
7651                 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7652
7653                 res_desc = &res_status->desc[0];
7654                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7655                         if ((key = ctl_get_prkey(lun, i)) == 0)
7656                                 continue;
7657
7658                         scsi_u64to8b(key, res_desc->res_key.key);
7659                         if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7660                             (lun->pr_res_idx == i ||
7661                              lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7662                                 res_desc->flags = SPRI_FULL_R_HOLDER;
7663                                 res_desc->scopetype = lun->res_type;
7664                         }
7665                         scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7666                             res_desc->rel_trgt_port_id);
7667                         len = 0;
7668                         port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7669                         if (port != NULL)
7670                                 len = ctl_create_iid(port,
7671                                     i % CTL_MAX_INIT_PER_PORT,
7672                                     res_desc->transport_id);
7673                         scsi_ulto4b(len, res_desc->additional_length);
7674                         res_desc = (struct scsi_per_res_in_full_desc *)
7675                             &res_desc->transport_id[len];
7676                 }
7677                 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7678                     res_status->header.length);
7679                 break;
7680         }
7681         default:
7682                 /*
7683                  * This is a bug, because we just checked for this above,
7684                  * and should have returned an error.
7685                  */
7686                 panic("Invalid PR type %x", cdb->action);
7687                 break; /* NOTREACHED */
7688         }
7689         mtx_unlock(&lun->lun_lock);
7690
7691         ctl_set_success(ctsio);
7692         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7693         ctsio->be_move_done = ctl_config_move_done;
7694         ctl_datamove((union ctl_io *)ctsio);
7695         return (CTL_RETVAL_COMPLETE);
7696 }
7697
7698 /*
7699  * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7700  * it should return.
7701  */
7702 static int
7703 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7704                 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7705                 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7706                 struct scsi_per_res_out_parms* param)
7707 {
7708         union ctl_ha_msg persis_io;
7709         int i;
7710
7711         mtx_lock(&lun->lun_lock);
7712         if (sa_res_key == 0) {
7713                 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7714                         /* validate scope and type */
7715                         if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7716                              SPR_LU_SCOPE) {
7717                                 mtx_unlock(&lun->lun_lock);
7718                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7719                                                       /*sks_valid*/ 1,
7720                                                       /*command*/ 1,
7721                                                       /*field*/ 2,
7722                                                       /*bit_valid*/ 1,
7723                                                       /*bit*/ 4);
7724                                 ctl_done((union ctl_io *)ctsio);
7725                                 return (1);
7726                         }
7727
7728                         if (type>8 || type==2 || type==4 || type==0) {
7729                                 mtx_unlock(&lun->lun_lock);
7730                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7731                                                       /*sks_valid*/ 1,
7732                                                       /*command*/ 1,
7733                                                       /*field*/ 2,
7734                                                       /*bit_valid*/ 1,
7735                                                       /*bit*/ 0);
7736                                 ctl_done((union ctl_io *)ctsio);
7737                                 return (1);
7738                         }
7739
7740                         /*
7741                          * Unregister everybody else and build UA for
7742                          * them
7743                          */
7744                         for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7745                                 if (i == residx || ctl_get_prkey(lun, i) == 0)
7746                                         continue;
7747
7748                                 ctl_clr_prkey(lun, i);
7749                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7750                         }
7751                         lun->pr_key_count = 1;
7752                         lun->res_type = type;
7753                         if (lun->res_type != SPR_TYPE_WR_EX_AR
7754                          && lun->res_type != SPR_TYPE_EX_AC_AR)
7755                                 lun->pr_res_idx = residx;
7756                         lun->PRGeneration++;
7757                         mtx_unlock(&lun->lun_lock);
7758
7759                         /* send msg to other side */
7760                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7761                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7762                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7763                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
7764                         persis_io.pr.pr_info.res_type = type;
7765                         memcpy(persis_io.pr.pr_info.sa_res_key,
7766                                param->serv_act_res_key,
7767                                sizeof(param->serv_act_res_key));
7768                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7769                             sizeof(persis_io.pr), M_WAITOK);
7770                 } else {
7771                         /* not all registrants */
7772                         mtx_unlock(&lun->lun_lock);
7773                         free(ctsio->kern_data_ptr, M_CTL);
7774                         ctl_set_invalid_field(ctsio,
7775                                               /*sks_valid*/ 1,
7776                                               /*command*/ 0,
7777                                               /*field*/ 8,
7778                                               /*bit_valid*/ 0,
7779                                               /*bit*/ 0);
7780                         ctl_done((union ctl_io *)ctsio);
7781                         return (1);
7782                 }
7783         } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7784                 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7785                 int found = 0;
7786
7787                 if (res_key == sa_res_key) {
7788                         /* special case */
7789                         /*
7790                          * The spec implies this is not good but doesn't
7791                          * say what to do. There are two choices either
7792                          * generate a res conflict or check condition
7793                          * with illegal field in parameter data. Since
7794                          * that is what is done when the sa_res_key is
7795                          * zero I'll take that approach since this has
7796                          * to do with the sa_res_key.
7797                          */
7798                         mtx_unlock(&lun->lun_lock);
7799                         free(ctsio->kern_data_ptr, M_CTL);
7800                         ctl_set_invalid_field(ctsio,
7801                                               /*sks_valid*/ 1,
7802                                               /*command*/ 0,
7803                                               /*field*/ 8,
7804                                               /*bit_valid*/ 0,
7805                                               /*bit*/ 0);
7806                         ctl_done((union ctl_io *)ctsio);
7807                         return (1);
7808                 }
7809
7810                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7811                         if (ctl_get_prkey(lun, i) != sa_res_key)
7812                                 continue;
7813
7814                         found = 1;
7815                         ctl_clr_prkey(lun, i);
7816                         lun->pr_key_count--;
7817                         ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7818                 }
7819                 if (!found) {
7820                         mtx_unlock(&lun->lun_lock);
7821                         free(ctsio->kern_data_ptr, M_CTL);
7822                         ctl_set_reservation_conflict(ctsio);
7823                         ctl_done((union ctl_io *)ctsio);
7824                         return (CTL_RETVAL_COMPLETE);
7825                 }
7826                 lun->PRGeneration++;
7827                 mtx_unlock(&lun->lun_lock);
7828
7829                 /* send msg to other side */
7830                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7831                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7832                 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7833                 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7834                 persis_io.pr.pr_info.res_type = type;
7835                 memcpy(persis_io.pr.pr_info.sa_res_key,
7836                        param->serv_act_res_key,
7837                        sizeof(param->serv_act_res_key));
7838                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7839                     sizeof(persis_io.pr), M_WAITOK);
7840         } else {
7841                 /* Reserved but not all registrants */
7842                 /* sa_res_key is res holder */
7843                 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7844                         /* validate scope and type */
7845                         if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7846                              SPR_LU_SCOPE) {
7847                                 mtx_unlock(&lun->lun_lock);
7848                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7849                                                       /*sks_valid*/ 1,
7850                                                       /*command*/ 1,
7851                                                       /*field*/ 2,
7852                                                       /*bit_valid*/ 1,
7853                                                       /*bit*/ 4);
7854                                 ctl_done((union ctl_io *)ctsio);
7855                                 return (1);
7856                         }
7857
7858                         if (type>8 || type==2 || type==4 || type==0) {
7859                                 mtx_unlock(&lun->lun_lock);
7860                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7861                                                       /*sks_valid*/ 1,
7862                                                       /*command*/ 1,
7863                                                       /*field*/ 2,
7864                                                       /*bit_valid*/ 1,
7865                                                       /*bit*/ 0);
7866                                 ctl_done((union ctl_io *)ctsio);
7867                                 return (1);
7868                         }
7869
7870                         /*
7871                          * Do the following:
7872                          * if sa_res_key != res_key remove all
7873                          * registrants w/sa_res_key and generate UA
7874                          * for these registrants(Registrations
7875                          * Preempted) if it wasn't an exclusive
7876                          * reservation generate UA(Reservations
7877                          * Preempted) for all other registered nexuses
7878                          * if the type has changed. Establish the new
7879                          * reservation and holder. If res_key and
7880                          * sa_res_key are the same do the above
7881                          * except don't unregister the res holder.
7882                          */
7883
7884                         for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7885                                 if (i == residx || ctl_get_prkey(lun, i) == 0)
7886                                         continue;
7887
7888                                 if (sa_res_key == ctl_get_prkey(lun, i)) {
7889                                         ctl_clr_prkey(lun, i);
7890                                         lun->pr_key_count--;
7891                                         ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7892                                 } else if (type != lun->res_type
7893                                         && (lun->res_type == SPR_TYPE_WR_EX_RO
7894                                          || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7895                                         ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7896                                 }
7897                         }
7898                         lun->res_type = type;
7899                         if (lun->res_type != SPR_TYPE_WR_EX_AR
7900                          && lun->res_type != SPR_TYPE_EX_AC_AR)
7901                                 lun->pr_res_idx = residx;
7902                         else
7903                                 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7904                         lun->PRGeneration++;
7905                         mtx_unlock(&lun->lun_lock);
7906
7907                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7908                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7909                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7910                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
7911                         persis_io.pr.pr_info.res_type = type;
7912                         memcpy(persis_io.pr.pr_info.sa_res_key,
7913                                param->serv_act_res_key,
7914                                sizeof(param->serv_act_res_key));
7915                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7916                             sizeof(persis_io.pr), M_WAITOK);
7917                 } else {
7918                         /*
7919                          * sa_res_key is not the res holder just
7920                          * remove registrants
7921                          */
7922                         int found=0;
7923
7924                         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7925                                 if (sa_res_key != ctl_get_prkey(lun, i))
7926                                         continue;
7927
7928                                 found = 1;
7929                                 ctl_clr_prkey(lun, i);
7930                                 lun->pr_key_count--;
7931                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7932                         }
7933
7934                         if (!found) {
7935                                 mtx_unlock(&lun->lun_lock);
7936                                 free(ctsio->kern_data_ptr, M_CTL);
7937                                 ctl_set_reservation_conflict(ctsio);
7938                                 ctl_done((union ctl_io *)ctsio);
7939                                 return (1);
7940                         }
7941                         lun->PRGeneration++;
7942                         mtx_unlock(&lun->lun_lock);
7943
7944                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7945                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7946                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7947                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
7948                         persis_io.pr.pr_info.res_type = type;
7949                         memcpy(persis_io.pr.pr_info.sa_res_key,
7950                                param->serv_act_res_key,
7951                                sizeof(param->serv_act_res_key));
7952                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7953                             sizeof(persis_io.pr), M_WAITOK);
7954                 }
7955         }
7956         return (0);
7957 }
7958
7959 static void
7960 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7961 {
7962         uint64_t sa_res_key;
7963         int i;
7964
7965         sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7966
7967         if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7968          || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7969          || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
7970                 if (sa_res_key == 0) {
7971                         /*
7972                          * Unregister everybody else and build UA for
7973                          * them
7974                          */
7975                         for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7976                                 if (i == msg->pr.pr_info.residx ||
7977                                     ctl_get_prkey(lun, i) == 0)
7978                                         continue;
7979
7980                                 ctl_clr_prkey(lun, i);
7981                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7982                         }
7983
7984                         lun->pr_key_count = 1;
7985                         lun->res_type = msg->pr.pr_info.res_type;
7986                         if (lun->res_type != SPR_TYPE_WR_EX_AR
7987                          && lun->res_type != SPR_TYPE_EX_AC_AR)
7988                                 lun->pr_res_idx = msg->pr.pr_info.residx;
7989                 } else {
7990                         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7991                                 if (sa_res_key == ctl_get_prkey(lun, i))
7992                                         continue;
7993
7994                                 ctl_clr_prkey(lun, i);
7995                                 lun->pr_key_count--;
7996                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7997                         }
7998                 }
7999         } else {
8000                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8001                         if (i == msg->pr.pr_info.residx ||
8002                             ctl_get_prkey(lun, i) == 0)
8003                                 continue;
8004
8005                         if (sa_res_key == ctl_get_prkey(lun, i)) {
8006                                 ctl_clr_prkey(lun, i);
8007                                 lun->pr_key_count--;
8008                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8009                         } else if (msg->pr.pr_info.res_type != lun->res_type
8010                                 && (lun->res_type == SPR_TYPE_WR_EX_RO
8011                                  || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8012                                 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8013                         }
8014                 }
8015                 lun->res_type = msg->pr.pr_info.res_type;
8016                 if (lun->res_type != SPR_TYPE_WR_EX_AR
8017                  && lun->res_type != SPR_TYPE_EX_AC_AR)
8018                         lun->pr_res_idx = msg->pr.pr_info.residx;
8019                 else
8020                         lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8021         }
8022         lun->PRGeneration++;
8023
8024 }
8025
8026
8027 int
8028 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8029 {
8030         int retval;
8031         u_int32_t param_len;
8032         struct scsi_per_res_out *cdb;
8033         struct ctl_lun *lun;
8034         struct scsi_per_res_out_parms* param;
8035         struct ctl_softc *softc;
8036         uint32_t residx;
8037         uint64_t res_key, sa_res_key, key;
8038         uint8_t type;
8039         union ctl_ha_msg persis_io;
8040         int    i;
8041
8042         CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8043
8044         retval = CTL_RETVAL_COMPLETE;
8045
8046         cdb = (struct scsi_per_res_out *)ctsio->cdb;
8047         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8048         softc = lun->ctl_softc;
8049
8050         /*
8051          * We only support whole-LUN scope.  The scope & type are ignored for
8052          * register, register and ignore existing key and clear.
8053          * We sometimes ignore scope and type on preempts too!!
8054          * Verify reservation type here as well.
8055          */
8056         type = cdb->scope_type & SPR_TYPE_MASK;
8057         if ((cdb->action == SPRO_RESERVE)
8058          || (cdb->action == SPRO_RELEASE)) {
8059                 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8060                         ctl_set_invalid_field(/*ctsio*/ ctsio,
8061                                               /*sks_valid*/ 1,
8062                                               /*command*/ 1,
8063                                               /*field*/ 2,
8064                                               /*bit_valid*/ 1,
8065                                               /*bit*/ 4);
8066                         ctl_done((union ctl_io *)ctsio);
8067                         return (CTL_RETVAL_COMPLETE);
8068                 }
8069
8070                 if (type>8 || type==2 || type==4 || type==0) {
8071                         ctl_set_invalid_field(/*ctsio*/ ctsio,
8072                                               /*sks_valid*/ 1,
8073                                               /*command*/ 1,
8074                                               /*field*/ 2,
8075                                               /*bit_valid*/ 1,
8076                                               /*bit*/ 0);
8077                         ctl_done((union ctl_io *)ctsio);
8078                         return (CTL_RETVAL_COMPLETE);
8079                 }
8080         }
8081
8082         param_len = scsi_4btoul(cdb->length);
8083
8084         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8085                 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8086                 ctsio->kern_data_len = param_len;
8087                 ctsio->kern_total_len = param_len;
8088                 ctsio->kern_data_resid = 0;
8089                 ctsio->kern_rel_offset = 0;
8090                 ctsio->kern_sg_entries = 0;
8091                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8092                 ctsio->be_move_done = ctl_config_move_done;
8093                 ctl_datamove((union ctl_io *)ctsio);
8094
8095                 return (CTL_RETVAL_COMPLETE);
8096         }
8097
8098         param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8099
8100         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8101         res_key = scsi_8btou64(param->res_key.key);
8102         sa_res_key = scsi_8btou64(param->serv_act_res_key);
8103
8104         /*
8105          * Validate the reservation key here except for SPRO_REG_IGNO
8106          * This must be done for all other service actions
8107          */
8108         if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8109                 mtx_lock(&lun->lun_lock);
8110                 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8111                         if (res_key != key) {
8112                                 /*
8113                                  * The current key passed in doesn't match
8114                                  * the one the initiator previously
8115                                  * registered.
8116                                  */
8117                                 mtx_unlock(&lun->lun_lock);
8118                                 free(ctsio->kern_data_ptr, M_CTL);
8119                                 ctl_set_reservation_conflict(ctsio);
8120                                 ctl_done((union ctl_io *)ctsio);
8121                                 return (CTL_RETVAL_COMPLETE);
8122                         }
8123                 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8124                         /*
8125                          * We are not registered
8126                          */
8127                         mtx_unlock(&lun->lun_lock);
8128                         free(ctsio->kern_data_ptr, M_CTL);
8129                         ctl_set_reservation_conflict(ctsio);
8130                         ctl_done((union ctl_io *)ctsio);
8131                         return (CTL_RETVAL_COMPLETE);
8132                 } else if (res_key != 0) {
8133                         /*
8134                          * We are not registered and trying to register but
8135                          * the register key isn't zero.
8136                          */
8137                         mtx_unlock(&lun->lun_lock);
8138                         free(ctsio->kern_data_ptr, M_CTL);
8139                         ctl_set_reservation_conflict(ctsio);
8140                         ctl_done((union ctl_io *)ctsio);
8141                         return (CTL_RETVAL_COMPLETE);
8142                 }
8143                 mtx_unlock(&lun->lun_lock);
8144         }
8145
8146         switch (cdb->action & SPRO_ACTION_MASK) {
8147         case SPRO_REGISTER:
8148         case SPRO_REG_IGNO: {
8149
8150 #if 0
8151                 printf("Registration received\n");
8152 #endif
8153
8154                 /*
8155                  * We don't support any of these options, as we report in
8156                  * the read capabilities request (see
8157                  * ctl_persistent_reserve_in(), above).
8158                  */
8159                 if ((param->flags & SPR_SPEC_I_PT)
8160                  || (param->flags & SPR_ALL_TG_PT)
8161                  || (param->flags & SPR_APTPL)) {
8162                         int bit_ptr;
8163
8164                         if (param->flags & SPR_APTPL)
8165                                 bit_ptr = 0;
8166                         else if (param->flags & SPR_ALL_TG_PT)
8167                                 bit_ptr = 2;
8168                         else /* SPR_SPEC_I_PT */
8169                                 bit_ptr = 3;
8170
8171                         free(ctsio->kern_data_ptr, M_CTL);
8172                         ctl_set_invalid_field(ctsio,
8173                                               /*sks_valid*/ 1,
8174                                               /*command*/ 0,
8175                                               /*field*/ 20,
8176                                               /*bit_valid*/ 1,
8177                                               /*bit*/ bit_ptr);
8178                         ctl_done((union ctl_io *)ctsio);
8179                         return (CTL_RETVAL_COMPLETE);
8180                 }
8181
8182                 mtx_lock(&lun->lun_lock);
8183
8184                 /*
8185                  * The initiator wants to clear the
8186                  * key/unregister.
8187                  */
8188                 if (sa_res_key == 0) {
8189                         if ((res_key == 0
8190                           && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8191                          || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8192                           && ctl_get_prkey(lun, residx) == 0)) {
8193                                 mtx_unlock(&lun->lun_lock);
8194                                 goto done;
8195                         }
8196
8197                         ctl_clr_prkey(lun, residx);
8198                         lun->pr_key_count--;
8199
8200                         if (residx == lun->pr_res_idx) {
8201                                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8202                                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8203
8204                                 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8205                                   || lun->res_type == SPR_TYPE_EX_AC_RO)
8206                                  && lun->pr_key_count) {
8207                                         /*
8208                                          * If the reservation is a registrants
8209                                          * only type we need to generate a UA
8210                                          * for other registered inits.  The
8211                                          * sense code should be RESERVATIONS
8212                                          * RELEASED
8213                                          */
8214
8215                                         for (i = softc->init_min; i < softc->init_max; i++){
8216                                                 if (ctl_get_prkey(lun, i) == 0)
8217                                                         continue;
8218                                                 ctl_est_ua(lun, i,
8219                                                     CTL_UA_RES_RELEASE);
8220                                         }
8221                                 }
8222                                 lun->res_type = 0;
8223                         } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8224                                 if (lun->pr_key_count==0) {
8225                                         lun->flags &= ~CTL_LUN_PR_RESERVED;
8226                                         lun->res_type = 0;
8227                                         lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8228                                 }
8229                         }
8230                         lun->PRGeneration++;
8231                         mtx_unlock(&lun->lun_lock);
8232
8233                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8234                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8235                         persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8236                         persis_io.pr.pr_info.residx = residx;
8237                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8238                             sizeof(persis_io.pr), M_WAITOK);
8239                 } else /* sa_res_key != 0 */ {
8240
8241                         /*
8242                          * If we aren't registered currently then increment
8243                          * the key count and set the registered flag.
8244                          */
8245                         ctl_alloc_prkey(lun, residx);
8246                         if (ctl_get_prkey(lun, residx) == 0)
8247                                 lun->pr_key_count++;
8248                         ctl_set_prkey(lun, residx, sa_res_key);
8249                         lun->PRGeneration++;
8250                         mtx_unlock(&lun->lun_lock);
8251
8252                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8253                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8254                         persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8255                         persis_io.pr.pr_info.residx = residx;
8256                         memcpy(persis_io.pr.pr_info.sa_res_key,
8257                                param->serv_act_res_key,
8258                                sizeof(param->serv_act_res_key));
8259                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8260                             sizeof(persis_io.pr), M_WAITOK);
8261                 }
8262
8263                 break;
8264         }
8265         case SPRO_RESERVE:
8266 #if 0
8267                 printf("Reserve executed type %d\n", type);
8268 #endif
8269                 mtx_lock(&lun->lun_lock);
8270                 if (lun->flags & CTL_LUN_PR_RESERVED) {
8271                         /*
8272                          * if this isn't the reservation holder and it's
8273                          * not a "all registrants" type or if the type is
8274                          * different then we have a conflict
8275                          */
8276                         if ((lun->pr_res_idx != residx
8277                           && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8278                          || lun->res_type != type) {
8279                                 mtx_unlock(&lun->lun_lock);
8280                                 free(ctsio->kern_data_ptr, M_CTL);
8281                                 ctl_set_reservation_conflict(ctsio);
8282                                 ctl_done((union ctl_io *)ctsio);
8283                                 return (CTL_RETVAL_COMPLETE);
8284                         }
8285                         mtx_unlock(&lun->lun_lock);
8286                 } else /* create a reservation */ {
8287                         /*
8288                          * If it's not an "all registrants" type record
8289                          * reservation holder
8290                          */
8291                         if (type != SPR_TYPE_WR_EX_AR
8292                          && type != SPR_TYPE_EX_AC_AR)
8293                                 lun->pr_res_idx = residx; /* Res holder */
8294                         else
8295                                 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8296
8297                         lun->flags |= CTL_LUN_PR_RESERVED;
8298                         lun->res_type = type;
8299
8300                         mtx_unlock(&lun->lun_lock);
8301
8302                         /* send msg to other side */
8303                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8304                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8305                         persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8306                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
8307                         persis_io.pr.pr_info.res_type = type;
8308                         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8309                             sizeof(persis_io.pr), M_WAITOK);
8310                 }
8311                 break;
8312
8313         case SPRO_RELEASE:
8314                 mtx_lock(&lun->lun_lock);
8315                 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8316                         /* No reservation exists return good status */
8317                         mtx_unlock(&lun->lun_lock);
8318                         goto done;
8319                 }
8320                 /*
8321                  * Is this nexus a reservation holder?
8322                  */
8323                 if (lun->pr_res_idx != residx
8324                  && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8325                         /*
8326                          * not a res holder return good status but
8327                          * do nothing
8328                          */
8329                         mtx_unlock(&lun->lun_lock);
8330                         goto done;
8331                 }
8332
8333                 if (lun->res_type != type) {
8334                         mtx_unlock(&lun->lun_lock);
8335                         free(ctsio->kern_data_ptr, M_CTL);
8336                         ctl_set_illegal_pr_release(ctsio);
8337                         ctl_done((union ctl_io *)ctsio);
8338                         return (CTL_RETVAL_COMPLETE);
8339                 }
8340
8341                 /* okay to release */
8342                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8343                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8344                 lun->res_type = 0;
8345
8346                 /*
8347                  * if this isn't an exclusive access
8348                  * res generate UA for all other
8349                  * registrants.
8350                  */
8351                 if (type != SPR_TYPE_EX_AC
8352                  && type != SPR_TYPE_WR_EX) {
8353                         for (i = softc->init_min; i < softc->init_max; i++) {
8354                                 if (i == residx || ctl_get_prkey(lun, i) == 0)
8355                                         continue;
8356                                 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8357                         }
8358                 }
8359                 mtx_unlock(&lun->lun_lock);
8360
8361                 /* Send msg to other side */
8362                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8363                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8364                 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8365                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8366                      sizeof(persis_io.pr), M_WAITOK);
8367                 break;
8368
8369         case SPRO_CLEAR:
8370                 /* send msg to other side */
8371
8372                 mtx_lock(&lun->lun_lock);
8373                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8374                 lun->res_type = 0;
8375                 lun->pr_key_count = 0;
8376                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8377
8378                 ctl_clr_prkey(lun, residx);
8379                 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8380                         if (ctl_get_prkey(lun, i) != 0) {
8381                                 ctl_clr_prkey(lun, i);
8382                                 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8383                         }
8384                 lun->PRGeneration++;
8385                 mtx_unlock(&lun->lun_lock);
8386
8387                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8388                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8389                 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8390                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8391                      sizeof(persis_io.pr), M_WAITOK);
8392                 break;
8393
8394         case SPRO_PREEMPT:
8395         case SPRO_PRE_ABO: {
8396                 int nretval;
8397
8398                 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8399                                           residx, ctsio, cdb, param);
8400                 if (nretval != 0)
8401                         return (CTL_RETVAL_COMPLETE);
8402                 break;
8403         }
8404         default:
8405                 panic("Invalid PR type %x", cdb->action);
8406         }
8407
8408 done:
8409         free(ctsio->kern_data_ptr, M_CTL);
8410         ctl_set_success(ctsio);
8411         ctl_done((union ctl_io *)ctsio);
8412
8413         return (retval);
8414 }
8415
8416 /*
8417  * This routine is for handling a message from the other SC pertaining to
8418  * persistent reserve out. All the error checking will have been done
8419  * so only perorming the action need be done here to keep the two
8420  * in sync.
8421  */
8422 static void
8423 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8424 {
8425         struct ctl_lun *lun;
8426         struct ctl_softc *softc;
8427         int i;
8428         uint32_t residx, targ_lun;
8429
8430         softc = control_softc;
8431         targ_lun = msg->hdr.nexus.targ_mapped_lun;
8432         mtx_lock(&softc->ctl_lock);
8433         if ((targ_lun >= CTL_MAX_LUNS) ||
8434             ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8435                 mtx_unlock(&softc->ctl_lock);
8436                 return;
8437         }
8438         mtx_lock(&lun->lun_lock);
8439         mtx_unlock(&softc->ctl_lock);
8440         if (lun->flags & CTL_LUN_DISABLED) {
8441                 mtx_unlock(&lun->lun_lock);
8442                 return;
8443         }
8444         residx = ctl_get_initindex(&msg->hdr.nexus);
8445         switch(msg->pr.pr_info.action) {
8446         case CTL_PR_REG_KEY:
8447                 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8448                 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8449                         lun->pr_key_count++;
8450                 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8451                     scsi_8btou64(msg->pr.pr_info.sa_res_key));
8452                 lun->PRGeneration++;
8453                 break;
8454
8455         case CTL_PR_UNREG_KEY:
8456                 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8457                 lun->pr_key_count--;
8458
8459                 /* XXX Need to see if the reservation has been released */
8460                 /* if so do we need to generate UA? */
8461                 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8462                         lun->flags &= ~CTL_LUN_PR_RESERVED;
8463                         lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8464
8465                         if ((lun->res_type == SPR_TYPE_WR_EX_RO
8466                           || lun->res_type == SPR_TYPE_EX_AC_RO)
8467                          && lun->pr_key_count) {
8468                                 /*
8469                                  * If the reservation is a registrants
8470                                  * only type we need to generate a UA
8471                                  * for other registered inits.  The
8472                                  * sense code should be RESERVATIONS
8473                                  * RELEASED
8474                                  */
8475
8476                                 for (i = softc->init_min; i < softc->init_max; i++) {
8477                                         if (ctl_get_prkey(lun, i) == 0)
8478                                                 continue;
8479
8480                                         ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8481                                 }
8482                         }
8483                         lun->res_type = 0;
8484                 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8485                         if (lun->pr_key_count==0) {
8486                                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8487                                 lun->res_type = 0;
8488                                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8489                         }
8490                 }
8491                 lun->PRGeneration++;
8492                 break;
8493
8494         case CTL_PR_RESERVE:
8495                 lun->flags |= CTL_LUN_PR_RESERVED;
8496                 lun->res_type = msg->pr.pr_info.res_type;
8497                 lun->pr_res_idx = msg->pr.pr_info.residx;
8498
8499                 break;
8500
8501         case CTL_PR_RELEASE:
8502                 /*
8503                  * if this isn't an exclusive access res generate UA for all
8504                  * other registrants.
8505                  */
8506                 if (lun->res_type != SPR_TYPE_EX_AC
8507                  && lun->res_type != SPR_TYPE_WR_EX) {
8508                         for (i = softc->init_min; i < softc->init_max; i++)
8509                                 if (i == residx || ctl_get_prkey(lun, i) == 0)
8510                                         continue;
8511                                 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8512                 }
8513
8514                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8515                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8516                 lun->res_type = 0;
8517                 break;
8518
8519         case CTL_PR_PREEMPT:
8520                 ctl_pro_preempt_other(lun, msg);
8521                 break;
8522         case CTL_PR_CLEAR:
8523                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8524                 lun->res_type = 0;
8525                 lun->pr_key_count = 0;
8526                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8527
8528                 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8529                         if (ctl_get_prkey(lun, i) == 0)
8530                                 continue;
8531                         ctl_clr_prkey(lun, i);
8532                         ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8533                 }
8534                 lun->PRGeneration++;
8535                 break;
8536         }
8537
8538         mtx_unlock(&lun->lun_lock);
8539 }
8540
8541 int
8542 ctl_read_write(struct ctl_scsiio *ctsio)
8543 {
8544         struct ctl_lun *lun;
8545         struct ctl_lba_len_flags *lbalen;
8546         uint64_t lba;
8547         uint32_t num_blocks;
8548         int flags, retval;
8549         int isread;
8550
8551         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8552
8553         CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8554
8555         flags = 0;
8556         retval = CTL_RETVAL_COMPLETE;
8557
8558         isread = ctsio->cdb[0] == READ_6  || ctsio->cdb[0] == READ_10
8559               || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8560         switch (ctsio->cdb[0]) {
8561         case READ_6:
8562         case WRITE_6: {
8563                 struct scsi_rw_6 *cdb;
8564
8565                 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8566
8567                 lba = scsi_3btoul(cdb->addr);
8568                 /* only 5 bits are valid in the most significant address byte */
8569                 lba &= 0x1fffff;
8570                 num_blocks = cdb->length;
8571                 /*
8572                  * This is correct according to SBC-2.
8573                  */
8574                 if (num_blocks == 0)
8575                         num_blocks = 256;
8576                 break;
8577         }
8578         case READ_10:
8579         case WRITE_10: {
8580                 struct scsi_rw_10 *cdb;
8581
8582                 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8583                 if (cdb->byte2 & SRW10_FUA)
8584                         flags |= CTL_LLF_FUA;
8585                 if (cdb->byte2 & SRW10_DPO)
8586                         flags |= CTL_LLF_DPO;
8587                 lba = scsi_4btoul(cdb->addr);
8588                 num_blocks = scsi_2btoul(cdb->length);
8589                 break;
8590         }
8591         case WRITE_VERIFY_10: {
8592                 struct scsi_write_verify_10 *cdb;
8593
8594                 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8595                 flags |= CTL_LLF_FUA;
8596                 if (cdb->byte2 & SWV_DPO)
8597                         flags |= CTL_LLF_DPO;
8598                 lba = scsi_4btoul(cdb->addr);
8599                 num_blocks = scsi_2btoul(cdb->length);
8600                 break;
8601         }
8602         case READ_12:
8603         case WRITE_12: {
8604                 struct scsi_rw_12 *cdb;
8605
8606                 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8607                 if (cdb->byte2 & SRW12_FUA)
8608                         flags |= CTL_LLF_FUA;
8609                 if (cdb->byte2 & SRW12_DPO)
8610                         flags |= CTL_LLF_DPO;
8611                 lba = scsi_4btoul(cdb->addr);
8612                 num_blocks = scsi_4btoul(cdb->length);
8613                 break;
8614         }
8615         case WRITE_VERIFY_12: {
8616                 struct scsi_write_verify_12 *cdb;
8617
8618                 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8619                 flags |= CTL_LLF_FUA;
8620                 if (cdb->byte2 & SWV_DPO)
8621                         flags |= CTL_LLF_DPO;
8622                 lba = scsi_4btoul(cdb->addr);
8623                 num_blocks = scsi_4btoul(cdb->length);
8624                 break;
8625         }
8626         case READ_16:
8627         case WRITE_16: {
8628                 struct scsi_rw_16 *cdb;
8629
8630                 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8631                 if (cdb->byte2 & SRW12_FUA)
8632                         flags |= CTL_LLF_FUA;
8633                 if (cdb->byte2 & SRW12_DPO)
8634                         flags |= CTL_LLF_DPO;
8635                 lba = scsi_8btou64(cdb->addr);
8636                 num_blocks = scsi_4btoul(cdb->length);
8637                 break;
8638         }
8639         case WRITE_ATOMIC_16: {
8640                 struct scsi_rw_16 *cdb;
8641
8642                 if (lun->be_lun->atomicblock == 0) {
8643                         ctl_set_invalid_opcode(ctsio);
8644                         ctl_done((union ctl_io *)ctsio);
8645                         return (CTL_RETVAL_COMPLETE);
8646                 }
8647
8648                 cdb = (struct scsi_rw_16 *)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_8btou64(cdb->addr);
8654                 num_blocks = scsi_4btoul(cdb->length);
8655                 if (num_blocks > lun->be_lun->atomicblock) {
8656                         ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8657                             /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8658                             /*bit*/ 0);
8659                         ctl_done((union ctl_io *)ctsio);
8660                         return (CTL_RETVAL_COMPLETE);
8661                 }
8662                 break;
8663         }
8664         case WRITE_VERIFY_16: {
8665                 struct scsi_write_verify_16 *cdb;
8666
8667                 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8668                 flags |= CTL_LLF_FUA;
8669                 if (cdb->byte2 & SWV_DPO)
8670                         flags |= CTL_LLF_DPO;
8671                 lba = scsi_8btou64(cdb->addr);
8672                 num_blocks = scsi_4btoul(cdb->length);
8673                 break;
8674         }
8675         default:
8676                 /*
8677                  * We got a command we don't support.  This shouldn't
8678                  * happen, commands should be filtered out above us.
8679                  */
8680                 ctl_set_invalid_opcode(ctsio);
8681                 ctl_done((union ctl_io *)ctsio);
8682
8683                 return (CTL_RETVAL_COMPLETE);
8684                 break; /* NOTREACHED */
8685         }
8686
8687         /*
8688          * The first check is to make sure we're in bounds, the second
8689          * check is to catch wrap-around problems.  If the lba + num blocks
8690          * is less than the lba, then we've wrapped around and the block
8691          * range is invalid anyway.
8692          */
8693         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8694          || ((lba + num_blocks) < lba)) {
8695                 ctl_set_lba_out_of_range(ctsio);
8696                 ctl_done((union ctl_io *)ctsio);
8697                 return (CTL_RETVAL_COMPLETE);
8698         }
8699
8700         /*
8701          * According to SBC-3, a transfer length of 0 is not an error.
8702          * Note that this cannot happen with WRITE(6) or READ(6), since 0
8703          * translates to 256 blocks for those commands.
8704          */
8705         if (num_blocks == 0) {
8706                 ctl_set_success(ctsio);
8707                 ctl_done((union ctl_io *)ctsio);
8708                 return (CTL_RETVAL_COMPLETE);
8709         }
8710
8711         /* Set FUA and/or DPO if caches are disabled. */
8712         if (isread) {
8713                 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8714                     SCP_RCD) != 0)
8715                         flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8716         } else {
8717                 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8718                     SCP_WCE) == 0)
8719                         flags |= CTL_LLF_FUA;
8720         }
8721
8722         lbalen = (struct ctl_lba_len_flags *)
8723             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8724         lbalen->lba = lba;
8725         lbalen->len = num_blocks;
8726         lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8727
8728         ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8729         ctsio->kern_rel_offset = 0;
8730
8731         CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8732
8733         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8734
8735         return (retval);
8736 }
8737
8738 static int
8739 ctl_cnw_cont(union ctl_io *io)
8740 {
8741         struct ctl_scsiio *ctsio;
8742         struct ctl_lun *lun;
8743         struct ctl_lba_len_flags *lbalen;
8744         int retval;
8745
8746         ctsio = &io->scsiio;
8747         ctsio->io_hdr.status = CTL_STATUS_NONE;
8748         ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8749         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8750         lbalen = (struct ctl_lba_len_flags *)
8751             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8752         lbalen->flags &= ~CTL_LLF_COMPARE;
8753         lbalen->flags |= CTL_LLF_WRITE;
8754
8755         CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8756         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8757         return (retval);
8758 }
8759
8760 int
8761 ctl_cnw(struct ctl_scsiio *ctsio)
8762 {
8763         struct ctl_lun *lun;
8764         struct ctl_lba_len_flags *lbalen;
8765         uint64_t lba;
8766         uint32_t num_blocks;
8767         int flags, retval;
8768
8769         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8770
8771         CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8772
8773         flags = 0;
8774         retval = CTL_RETVAL_COMPLETE;
8775
8776         switch (ctsio->cdb[0]) {
8777         case COMPARE_AND_WRITE: {
8778                 struct scsi_compare_and_write *cdb;
8779
8780                 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8781                 if (cdb->byte2 & SRW10_FUA)
8782                         flags |= CTL_LLF_FUA;
8783                 if (cdb->byte2 & SRW10_DPO)
8784                         flags |= CTL_LLF_DPO;
8785                 lba = scsi_8btou64(cdb->addr);
8786                 num_blocks = cdb->length;
8787                 break;
8788         }
8789         default:
8790                 /*
8791                  * We got a command we don't support.  This shouldn't
8792                  * happen, commands should be filtered out above us.
8793                  */
8794                 ctl_set_invalid_opcode(ctsio);
8795                 ctl_done((union ctl_io *)ctsio);
8796
8797                 return (CTL_RETVAL_COMPLETE);
8798                 break; /* NOTREACHED */
8799         }
8800
8801         /*
8802          * The first check is to make sure we're in bounds, the second
8803          * check is to catch wrap-around problems.  If the lba + num blocks
8804          * is less than the lba, then we've wrapped around and the block
8805          * range is invalid anyway.
8806          */
8807         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8808          || ((lba + num_blocks) < lba)) {
8809                 ctl_set_lba_out_of_range(ctsio);
8810                 ctl_done((union ctl_io *)ctsio);
8811                 return (CTL_RETVAL_COMPLETE);
8812         }
8813
8814         /*
8815          * According to SBC-3, a transfer length of 0 is not an error.
8816          */
8817         if (num_blocks == 0) {
8818                 ctl_set_success(ctsio);
8819                 ctl_done((union ctl_io *)ctsio);
8820                 return (CTL_RETVAL_COMPLETE);
8821         }
8822
8823         /* Set FUA if write cache is disabled. */
8824         if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8825             SCP_WCE) == 0)
8826                 flags |= CTL_LLF_FUA;
8827
8828         ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8829         ctsio->kern_rel_offset = 0;
8830
8831         /*
8832          * Set the IO_CONT flag, so that if this I/O gets passed to
8833          * ctl_data_submit_done(), it'll get passed back to
8834          * ctl_ctl_cnw_cont() for further processing.
8835          */
8836         ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8837         ctsio->io_cont = ctl_cnw_cont;
8838
8839         lbalen = (struct ctl_lba_len_flags *)
8840             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8841         lbalen->lba = lba;
8842         lbalen->len = num_blocks;
8843         lbalen->flags = CTL_LLF_COMPARE | flags;
8844
8845         CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8846         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8847         return (retval);
8848 }
8849
8850 int
8851 ctl_verify(struct ctl_scsiio *ctsio)
8852 {
8853         struct ctl_lun *lun;
8854         struct ctl_lba_len_flags *lbalen;
8855         uint64_t lba;
8856         uint32_t num_blocks;
8857         int bytchk, flags;
8858         int retval;
8859
8860         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8861
8862         CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8863
8864         bytchk = 0;
8865         flags = CTL_LLF_FUA;
8866         retval = CTL_RETVAL_COMPLETE;
8867
8868         switch (ctsio->cdb[0]) {
8869         case VERIFY_10: {
8870                 struct scsi_verify_10 *cdb;
8871
8872                 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8873                 if (cdb->byte2 & SVFY_BYTCHK)
8874                         bytchk = 1;
8875                 if (cdb->byte2 & SVFY_DPO)
8876                         flags |= CTL_LLF_DPO;
8877                 lba = scsi_4btoul(cdb->addr);
8878                 num_blocks = scsi_2btoul(cdb->length);
8879                 break;
8880         }
8881         case VERIFY_12: {
8882                 struct scsi_verify_12 *cdb;
8883
8884                 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8885                 if (cdb->byte2 & SVFY_BYTCHK)
8886                         bytchk = 1;
8887                 if (cdb->byte2 & SVFY_DPO)
8888                         flags |= CTL_LLF_DPO;
8889                 lba = scsi_4btoul(cdb->addr);
8890                 num_blocks = scsi_4btoul(cdb->length);
8891                 break;
8892         }
8893         case VERIFY_16: {
8894                 struct scsi_rw_16 *cdb;
8895
8896                 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8897                 if (cdb->byte2 & SVFY_BYTCHK)
8898                         bytchk = 1;
8899                 if (cdb->byte2 & SVFY_DPO)
8900                         flags |= CTL_LLF_DPO;
8901                 lba = scsi_8btou64(cdb->addr);
8902                 num_blocks = scsi_4btoul(cdb->length);
8903                 break;
8904         }
8905         default:
8906                 /*
8907                  * We got a command we don't support.  This shouldn't
8908                  * happen, commands should be filtered out above us.
8909                  */
8910                 ctl_set_invalid_opcode(ctsio);
8911                 ctl_done((union ctl_io *)ctsio);
8912                 return (CTL_RETVAL_COMPLETE);
8913         }
8914
8915         /*
8916          * The first check is to make sure we're in bounds, the second
8917          * check is to catch wrap-around problems.  If the lba + num blocks
8918          * is less than the lba, then we've wrapped around and the block
8919          * range is invalid anyway.
8920          */
8921         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8922          || ((lba + num_blocks) < lba)) {
8923                 ctl_set_lba_out_of_range(ctsio);
8924                 ctl_done((union ctl_io *)ctsio);
8925                 return (CTL_RETVAL_COMPLETE);
8926         }
8927
8928         /*
8929          * According to SBC-3, a transfer length of 0 is not an error.
8930          */
8931         if (num_blocks == 0) {
8932                 ctl_set_success(ctsio);
8933                 ctl_done((union ctl_io *)ctsio);
8934                 return (CTL_RETVAL_COMPLETE);
8935         }
8936
8937         lbalen = (struct ctl_lba_len_flags *)
8938             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8939         lbalen->lba = lba;
8940         lbalen->len = num_blocks;
8941         if (bytchk) {
8942                 lbalen->flags = CTL_LLF_COMPARE | flags;
8943                 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8944         } else {
8945                 lbalen->flags = CTL_LLF_VERIFY | flags;
8946                 ctsio->kern_total_len = 0;
8947         }
8948         ctsio->kern_rel_offset = 0;
8949
8950         CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8951         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8952         return (retval);
8953 }
8954
8955 int
8956 ctl_report_luns(struct ctl_scsiio *ctsio)
8957 {
8958         struct ctl_softc *softc = control_softc;
8959         struct scsi_report_luns *cdb;
8960         struct scsi_report_luns_data *lun_data;
8961         struct ctl_lun *lun, *request_lun;
8962         struct ctl_port *port;
8963         int num_luns, retval;
8964         uint32_t alloc_len, lun_datalen;
8965         int num_filled, well_known;
8966         uint32_t initidx, targ_lun_id, lun_id;
8967
8968         retval = CTL_RETVAL_COMPLETE;
8969         well_known = 0;
8970
8971         cdb = (struct scsi_report_luns *)ctsio->cdb;
8972         port = ctl_io_port(&ctsio->io_hdr);
8973
8974         CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8975
8976         mtx_lock(&softc->ctl_lock);
8977         num_luns = 0;
8978         for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
8979                 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
8980                         num_luns++;
8981         }
8982         mtx_unlock(&softc->ctl_lock);
8983
8984         switch (cdb->select_report) {
8985         case RPL_REPORT_DEFAULT:
8986         case RPL_REPORT_ALL:
8987                 break;
8988         case RPL_REPORT_WELLKNOWN:
8989                 well_known = 1;
8990                 num_luns = 0;
8991                 break;
8992         default:
8993                 ctl_set_invalid_field(ctsio,
8994                                       /*sks_valid*/ 1,
8995                                       /*command*/ 1,
8996                                       /*field*/ 2,
8997                                       /*bit_valid*/ 0,
8998                                       /*bit*/ 0);
8999                 ctl_done((union ctl_io *)ctsio);
9000                 return (retval);
9001                 break; /* NOTREACHED */
9002         }
9003
9004         alloc_len = scsi_4btoul(cdb->length);
9005         /*
9006          * The initiator has to allocate at least 16 bytes for this request,
9007          * so he can at least get the header and the first LUN.  Otherwise
9008          * we reject the request (per SPC-3 rev 14, section 6.21).
9009          */
9010         if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9011             sizeof(struct scsi_report_luns_lundata))) {
9012                 ctl_set_invalid_field(ctsio,
9013                                       /*sks_valid*/ 1,
9014                                       /*command*/ 1,
9015                                       /*field*/ 6,
9016                                       /*bit_valid*/ 0,
9017                                       /*bit*/ 0);
9018                 ctl_done((union ctl_io *)ctsio);
9019                 return (retval);
9020         }
9021
9022         request_lun = (struct ctl_lun *)
9023                 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9024
9025         lun_datalen = sizeof(*lun_data) +
9026                 (num_luns * sizeof(struct scsi_report_luns_lundata));
9027
9028         ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9029         lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9030         ctsio->kern_sg_entries = 0;
9031
9032         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9033
9034         mtx_lock(&softc->ctl_lock);
9035         for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9036                 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9037                 if (lun_id >= CTL_MAX_LUNS)
9038                         continue;
9039                 lun = softc->ctl_luns[lun_id];
9040                 if (lun == NULL)
9041                         continue;
9042
9043                 if (targ_lun_id <= 0xff) {
9044                         /*
9045                          * Peripheral addressing method, bus number 0.
9046                          */
9047                         lun_data->luns[num_filled].lundata[0] =
9048                                 RPL_LUNDATA_ATYP_PERIPH;
9049                         lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9050                         num_filled++;
9051                 } else if (targ_lun_id <= 0x3fff) {
9052                         /*
9053                          * Flat addressing method.
9054                          */
9055                         lun_data->luns[num_filled].lundata[0] =
9056                                 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9057                         lun_data->luns[num_filled].lundata[1] =
9058                                 (targ_lun_id & 0xff);
9059                         num_filled++;
9060                 } else if (targ_lun_id <= 0xffffff) {
9061                         /*
9062                          * Extended flat addressing method.
9063                          */
9064                         lun_data->luns[num_filled].lundata[0] =
9065                             RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9066                         scsi_ulto3b(targ_lun_id,
9067                             &lun_data->luns[num_filled].lundata[1]);
9068                         num_filled++;
9069                 } else {
9070                         printf("ctl_report_luns: bogus LUN number %jd, "
9071                                "skipping\n", (intmax_t)targ_lun_id);
9072                 }
9073                 /*
9074                  * According to SPC-3, rev 14 section 6.21:
9075                  *
9076                  * "The execution of a REPORT LUNS command to any valid and
9077                  * installed logical unit shall clear the REPORTED LUNS DATA
9078                  * HAS CHANGED unit attention condition for all logical
9079                  * units of that target with respect to the requesting
9080                  * initiator. A valid and installed logical unit is one
9081                  * having a PERIPHERAL QUALIFIER of 000b in the standard
9082                  * INQUIRY data (see 6.4.2)."
9083                  *
9084                  * If request_lun is NULL, the LUN this report luns command
9085                  * was issued to is either disabled or doesn't exist. In that
9086                  * case, we shouldn't clear any pending lun change unit
9087                  * attention.
9088                  */
9089                 if (request_lun != NULL) {
9090                         mtx_lock(&lun->lun_lock);
9091                         ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9092                         mtx_unlock(&lun->lun_lock);
9093                 }
9094         }
9095         mtx_unlock(&softc->ctl_lock);
9096
9097         /*
9098          * It's quite possible that we've returned fewer LUNs than we allocated
9099          * space for.  Trim it.
9100          */
9101         lun_datalen = sizeof(*lun_data) +
9102                 (num_filled * sizeof(struct scsi_report_luns_lundata));
9103
9104         if (lun_datalen < alloc_len) {
9105                 ctsio->residual = alloc_len - lun_datalen;
9106                 ctsio->kern_data_len = lun_datalen;
9107                 ctsio->kern_total_len = lun_datalen;
9108         } else {
9109                 ctsio->residual = 0;
9110                 ctsio->kern_data_len = alloc_len;
9111                 ctsio->kern_total_len = alloc_len;
9112         }
9113         ctsio->kern_data_resid = 0;
9114         ctsio->kern_rel_offset = 0;
9115         ctsio->kern_sg_entries = 0;
9116
9117         /*
9118          * We set this to the actual data length, regardless of how much
9119          * space we actually have to return results.  If the user looks at
9120          * this value, he'll know whether or not he allocated enough space
9121          * and reissue the command if necessary.  We don't support well
9122          * known logical units, so if the user asks for that, return none.
9123          */
9124         scsi_ulto4b(lun_datalen - 8, lun_data->length);
9125
9126         /*
9127          * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9128          * this request.
9129          */
9130         ctl_set_success(ctsio);
9131         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9132         ctsio->be_move_done = ctl_config_move_done;
9133         ctl_datamove((union ctl_io *)ctsio);
9134         return (retval);
9135 }
9136
9137 int
9138 ctl_request_sense(struct ctl_scsiio *ctsio)
9139 {
9140         struct scsi_request_sense *cdb;
9141         struct scsi_sense_data *sense_ptr;
9142         struct ctl_softc *ctl_softc;
9143         struct ctl_lun *lun;
9144         uint32_t initidx;
9145         int have_error;
9146         scsi_sense_data_type sense_format;
9147         ctl_ua_type ua_type;
9148
9149         cdb = (struct scsi_request_sense *)ctsio->cdb;
9150
9151         ctl_softc = control_softc;
9152         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9153
9154         CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9155
9156         /*
9157          * Determine which sense format the user wants.
9158          */
9159         if (cdb->byte2 & SRS_DESC)
9160                 sense_format = SSD_TYPE_DESC;
9161         else
9162                 sense_format = SSD_TYPE_FIXED;
9163
9164         ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9165         sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9166         ctsio->kern_sg_entries = 0;
9167
9168         /*
9169          * struct scsi_sense_data, which is currently set to 256 bytes, is
9170          * larger than the largest allowed value for the length field in the
9171          * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9172          */
9173         ctsio->residual = 0;
9174         ctsio->kern_data_len = cdb->length;
9175         ctsio->kern_total_len = cdb->length;
9176
9177         ctsio->kern_data_resid = 0;
9178         ctsio->kern_rel_offset = 0;
9179         ctsio->kern_sg_entries = 0;
9180
9181         /*
9182          * If we don't have a LUN, we don't have any pending sense.
9183          */
9184         if (lun == NULL)
9185                 goto no_sense;
9186
9187         have_error = 0;
9188         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9189         /*
9190          * Check for pending sense, and then for pending unit attentions.
9191          * Pending sense gets returned first, then pending unit attentions.
9192          */
9193         mtx_lock(&lun->lun_lock);
9194 #ifdef CTL_WITH_CA
9195         if (ctl_is_set(lun->have_ca, initidx)) {
9196                 scsi_sense_data_type stored_format;
9197
9198                 /*
9199                  * Check to see which sense format was used for the stored
9200                  * sense data.
9201                  */
9202                 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9203
9204                 /*
9205                  * If the user requested a different sense format than the
9206                  * one we stored, then we need to convert it to the other
9207                  * format.  If we're going from descriptor to fixed format
9208                  * sense data, we may lose things in translation, depending
9209                  * on what options were used.
9210                  *
9211                  * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9212                  * for some reason we'll just copy it out as-is.
9213                  */
9214                 if ((stored_format == SSD_TYPE_FIXED)
9215                  && (sense_format == SSD_TYPE_DESC))
9216                         ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9217                             &lun->pending_sense[initidx],
9218                             (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                             &lun->pending_sense[initidx],
9223                             (struct scsi_sense_data_fixed *)sense_ptr);
9224                 else
9225                         memcpy(sense_ptr, &lun->pending_sense[initidx],
9226                                MIN(sizeof(*sense_ptr),
9227                                sizeof(lun->pending_sense[initidx])));
9228
9229                 ctl_clear_mask(lun->have_ca, initidx);
9230                 have_error = 1;
9231         } else
9232 #endif
9233         {
9234                 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9235                 if (ua_type != CTL_UA_NONE)
9236                         have_error = 1;
9237                 if (ua_type == CTL_UA_LUN_CHANGE) {
9238                         mtx_unlock(&lun->lun_lock);
9239                         mtx_lock(&ctl_softc->ctl_lock);
9240                         ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9241                         mtx_unlock(&ctl_softc->ctl_lock);
9242                         mtx_lock(&lun->lun_lock);
9243                 }
9244
9245         }
9246         mtx_unlock(&lun->lun_lock);
9247
9248         /*
9249          * We already have a pending error, return it.
9250          */
9251         if (have_error != 0) {
9252                 /*
9253                  * We report the SCSI status as OK, since the status of the
9254                  * request sense command itself is OK.
9255                  * We report 0 for the sense length, because we aren't doing
9256                  * autosense in this case.  We're reporting sense as
9257                  * parameter data.
9258                  */
9259                 ctl_set_success(ctsio);
9260                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9261                 ctsio->be_move_done = ctl_config_move_done;
9262                 ctl_datamove((union ctl_io *)ctsio);
9263                 return (CTL_RETVAL_COMPLETE);
9264         }
9265
9266 no_sense:
9267
9268         /*
9269          * No sense information to report, so we report that everything is
9270          * okay.
9271          */
9272         ctl_set_sense_data(sense_ptr,
9273                            lun,
9274                            sense_format,
9275                            /*current_error*/ 1,
9276                            /*sense_key*/ SSD_KEY_NO_SENSE,
9277                            /*asc*/ 0x00,
9278                            /*ascq*/ 0x00,
9279                            SSD_ELEM_NONE);
9280
9281         /*
9282          * We report 0 for the sense length, because we aren't doing
9283          * autosense in this case.  We're reporting sense as parameter data.
9284          */
9285         ctl_set_success(ctsio);
9286         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9287         ctsio->be_move_done = ctl_config_move_done;
9288         ctl_datamove((union ctl_io *)ctsio);
9289         return (CTL_RETVAL_COMPLETE);
9290 }
9291
9292 int
9293 ctl_tur(struct ctl_scsiio *ctsio)
9294 {
9295
9296         CTL_DEBUG_PRINT(("ctl_tur\n"));
9297
9298         ctl_set_success(ctsio);
9299         ctl_done((union ctl_io *)ctsio);
9300
9301         return (CTL_RETVAL_COMPLETE);
9302 }
9303
9304 /*
9305  * SCSI VPD page 0x00, the Supported VPD Pages page.
9306  */
9307 static int
9308 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9309 {
9310         struct scsi_vpd_supported_pages *pages;
9311         int sup_page_size;
9312         struct ctl_lun *lun;
9313         int p;
9314
9315         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9316
9317         sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9318             SCSI_EVPD_NUM_SUPPORTED_PAGES;
9319         ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9320         pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9321         ctsio->kern_sg_entries = 0;
9322
9323         if (sup_page_size < alloc_len) {
9324                 ctsio->residual = alloc_len - sup_page_size;
9325                 ctsio->kern_data_len = sup_page_size;
9326                 ctsio->kern_total_len = sup_page_size;
9327         } else {
9328                 ctsio->residual = 0;
9329                 ctsio->kern_data_len = alloc_len;
9330                 ctsio->kern_total_len = alloc_len;
9331         }
9332         ctsio->kern_data_resid = 0;
9333         ctsio->kern_rel_offset = 0;
9334         ctsio->kern_sg_entries = 0;
9335
9336         /*
9337          * The control device is always connected.  The disk device, on the
9338          * other hand, may not be online all the time.  Need to change this
9339          * to figure out whether the disk device is actually online or not.
9340          */
9341         if (lun != NULL)
9342                 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9343                                 lun->be_lun->lun_type;
9344         else
9345                 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9346
9347         p = 0;
9348         /* Supported VPD pages */
9349         pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9350         /* Serial Number */
9351         pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9352         /* Device Identification */
9353         pages->page_list[p++] = SVPD_DEVICE_ID;
9354         /* Extended INQUIRY Data */
9355         pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9356         /* Mode Page Policy */
9357         pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9358         /* SCSI Ports */
9359         pages->page_list[p++] = SVPD_SCSI_PORTS;
9360         /* Third-party Copy */
9361         pages->page_list[p++] = SVPD_SCSI_TPC;
9362         if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9363                 /* Block limits */
9364                 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9365                 /* Block Device Characteristics */
9366                 pages->page_list[p++] = SVPD_BDC;
9367                 /* Logical Block Provisioning */
9368                 pages->page_list[p++] = SVPD_LBP;
9369         }
9370         pages->length = p;
9371
9372         ctl_set_success(ctsio);
9373         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9374         ctsio->be_move_done = ctl_config_move_done;
9375         ctl_datamove((union ctl_io *)ctsio);
9376         return (CTL_RETVAL_COMPLETE);
9377 }
9378
9379 /*
9380  * SCSI VPD page 0x80, the Unit Serial Number page.
9381  */
9382 static int
9383 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9384 {
9385         struct scsi_vpd_unit_serial_number *sn_ptr;
9386         struct ctl_lun *lun;
9387         int data_len;
9388
9389         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9390
9391         data_len = 4 + CTL_SN_LEN;
9392         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9393         sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9394         if (data_len < alloc_len) {
9395                 ctsio->residual = alloc_len - data_len;
9396                 ctsio->kern_data_len = data_len;
9397                 ctsio->kern_total_len = data_len;
9398         } else {
9399                 ctsio->residual = 0;
9400                 ctsio->kern_data_len = alloc_len;
9401                 ctsio->kern_total_len = alloc_len;
9402         }
9403         ctsio->kern_data_resid = 0;
9404         ctsio->kern_rel_offset = 0;
9405         ctsio->kern_sg_entries = 0;
9406
9407         /*
9408          * The control device is always connected.  The disk device, on the
9409          * other hand, may not be online all the time.  Need to change this
9410          * to figure out whether the disk device is actually online or not.
9411          */
9412         if (lun != NULL)
9413                 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9414                                   lun->be_lun->lun_type;
9415         else
9416                 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9417
9418         sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9419         sn_ptr->length = CTL_SN_LEN;
9420         /*
9421          * If we don't have a LUN, we just leave the serial number as
9422          * all spaces.
9423          */
9424         if (lun != NULL) {
9425                 strncpy((char *)sn_ptr->serial_num,
9426                         (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9427         } else
9428                 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9429
9430         ctl_set_success(ctsio);
9431         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9432         ctsio->be_move_done = ctl_config_move_done;
9433         ctl_datamove((union ctl_io *)ctsio);
9434         return (CTL_RETVAL_COMPLETE);
9435 }
9436
9437
9438 /*
9439  * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9440  */
9441 static int
9442 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9443 {
9444         struct scsi_vpd_extended_inquiry_data *eid_ptr;
9445         struct ctl_lun *lun;
9446         int data_len;
9447
9448         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9449
9450         data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9451         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9452         eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9453         ctsio->kern_sg_entries = 0;
9454
9455         if (data_len < alloc_len) {
9456                 ctsio->residual = alloc_len - data_len;
9457                 ctsio->kern_data_len = data_len;
9458                 ctsio->kern_total_len = data_len;
9459         } else {
9460                 ctsio->residual = 0;
9461                 ctsio->kern_data_len = alloc_len;
9462                 ctsio->kern_total_len = alloc_len;
9463         }
9464         ctsio->kern_data_resid = 0;
9465         ctsio->kern_rel_offset = 0;
9466         ctsio->kern_sg_entries = 0;
9467
9468         /*
9469          * The control device is always connected.  The disk device, on the
9470          * other hand, may not be online all the time.
9471          */
9472         if (lun != NULL)
9473                 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9474                                      lun->be_lun->lun_type;
9475         else
9476                 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9477         eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9478         scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9479         /*
9480          * We support head of queue, ordered and simple tags.
9481          */
9482         eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9483         /*
9484          * Volatile cache supported.
9485          */
9486         eid_ptr->flags3 = SVPD_EID_V_SUP;
9487
9488         /*
9489          * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9490          * attention for a particular IT nexus on all LUNs once we report
9491          * it to that nexus once.  This bit is required as of SPC-4.
9492          */
9493         eid_ptr->flags4 = SVPD_EID_LUICLT;
9494
9495         /*
9496          * XXX KDM in order to correctly answer this, we would need
9497          * information from the SIM to determine how much sense data it
9498          * can send.  So this would really be a path inquiry field, most
9499          * likely.  This can be set to a maximum of 252 according to SPC-4,
9500          * but the hardware may or may not be able to support that much.
9501          * 0 just means that the maximum sense data length is not reported.
9502          */
9503         eid_ptr->max_sense_length = 0;
9504
9505         ctl_set_success(ctsio);
9506         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9507         ctsio->be_move_done = ctl_config_move_done;
9508         ctl_datamove((union ctl_io *)ctsio);
9509         return (CTL_RETVAL_COMPLETE);
9510 }
9511
9512 static int
9513 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9514 {
9515         struct scsi_vpd_mode_page_policy *mpp_ptr;
9516         struct ctl_lun *lun;
9517         int data_len;
9518
9519         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9520
9521         data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9522             sizeof(struct scsi_vpd_mode_page_policy_descr);
9523
9524         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9525         mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9526         ctsio->kern_sg_entries = 0;
9527
9528         if (data_len < alloc_len) {
9529                 ctsio->residual = alloc_len - data_len;
9530                 ctsio->kern_data_len = data_len;
9531                 ctsio->kern_total_len = data_len;
9532         } else {
9533                 ctsio->residual = 0;
9534                 ctsio->kern_data_len = alloc_len;
9535                 ctsio->kern_total_len = alloc_len;
9536         }
9537         ctsio->kern_data_resid = 0;
9538         ctsio->kern_rel_offset = 0;
9539         ctsio->kern_sg_entries = 0;
9540
9541         /*
9542          * The control device is always connected.  The disk device, on the
9543          * other hand, may not be online all the time.
9544          */
9545         if (lun != NULL)
9546                 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9547                                      lun->be_lun->lun_type;
9548         else
9549                 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9550         mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9551         scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9552         mpp_ptr->descr[0].page_code = 0x3f;
9553         mpp_ptr->descr[0].subpage_code = 0xff;
9554         mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9555
9556         ctl_set_success(ctsio);
9557         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9558         ctsio->be_move_done = ctl_config_move_done;
9559         ctl_datamove((union ctl_io *)ctsio);
9560         return (CTL_RETVAL_COMPLETE);
9561 }
9562
9563 /*
9564  * SCSI VPD page 0x83, the Device Identification page.
9565  */
9566 static int
9567 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9568 {
9569         struct scsi_vpd_device_id *devid_ptr;
9570         struct scsi_vpd_id_descriptor *desc;
9571         struct ctl_softc *softc;
9572         struct ctl_lun *lun;
9573         struct ctl_port *port;
9574         int data_len;
9575         uint8_t proto;
9576
9577         softc = control_softc;
9578
9579         port = ctl_io_port(&ctsio->io_hdr);
9580         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9581
9582         data_len = sizeof(struct scsi_vpd_device_id) +
9583             sizeof(struct scsi_vpd_id_descriptor) +
9584                 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9585             sizeof(struct scsi_vpd_id_descriptor) +
9586                 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9587         if (lun && lun->lun_devid)
9588                 data_len += lun->lun_devid->len;
9589         if (port && port->port_devid)
9590                 data_len += port->port_devid->len;
9591         if (port && port->target_devid)
9592                 data_len += port->target_devid->len;
9593
9594         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9595         devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9596         ctsio->kern_sg_entries = 0;
9597
9598         if (data_len < alloc_len) {
9599                 ctsio->residual = alloc_len - data_len;
9600                 ctsio->kern_data_len = data_len;
9601                 ctsio->kern_total_len = data_len;
9602         } else {
9603                 ctsio->residual = 0;
9604                 ctsio->kern_data_len = alloc_len;
9605                 ctsio->kern_total_len = alloc_len;
9606         }
9607         ctsio->kern_data_resid = 0;
9608         ctsio->kern_rel_offset = 0;
9609         ctsio->kern_sg_entries = 0;
9610
9611         /*
9612          * The control device is always connected.  The disk device, on the
9613          * other hand, may not be online all the time.
9614          */
9615         if (lun != NULL)
9616                 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9617                                      lun->be_lun->lun_type;
9618         else
9619                 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9620         devid_ptr->page_code = SVPD_DEVICE_ID;
9621         scsi_ulto2b(data_len - 4, devid_ptr->length);
9622
9623         if (port && port->port_type == CTL_PORT_FC)
9624                 proto = SCSI_PROTO_FC << 4;
9625         else if (port && port->port_type == CTL_PORT_ISCSI)
9626                 proto = SCSI_PROTO_ISCSI << 4;
9627         else
9628                 proto = SCSI_PROTO_SPI << 4;
9629         desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9630
9631         /*
9632          * We're using a LUN association here.  i.e., this device ID is a
9633          * per-LUN identifier.
9634          */
9635         if (lun && lun->lun_devid) {
9636                 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9637                 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9638                     lun->lun_devid->len);
9639         }
9640
9641         /*
9642          * This is for the WWPN which is a port association.
9643          */
9644         if (port && port->port_devid) {
9645                 memcpy(desc, port->port_devid->data, port->port_devid->len);
9646                 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9647                     port->port_devid->len);
9648         }
9649
9650         /*
9651          * This is for the Relative Target Port(type 4h) identifier
9652          */
9653         desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9654         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9655             SVPD_ID_TYPE_RELTARG;
9656         desc->length = 4;
9657         scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9658         desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9659             sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9660
9661         /*
9662          * This is for the Target Port Group(type 5h) identifier
9663          */
9664         desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9665         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9666             SVPD_ID_TYPE_TPORTGRP;
9667         desc->length = 4;
9668         scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9669             &desc->identifier[2]);
9670         desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9671             sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9672
9673         /*
9674          * This is for the Target identifier
9675          */
9676         if (port && port->target_devid) {
9677                 memcpy(desc, port->target_devid->data, port->target_devid->len);
9678         }
9679
9680         ctl_set_success(ctsio);
9681         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9682         ctsio->be_move_done = ctl_config_move_done;
9683         ctl_datamove((union ctl_io *)ctsio);
9684         return (CTL_RETVAL_COMPLETE);
9685 }
9686
9687 static int
9688 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9689 {
9690         struct ctl_softc *softc = control_softc;
9691         struct scsi_vpd_scsi_ports *sp;
9692         struct scsi_vpd_port_designation *pd;
9693         struct scsi_vpd_port_designation_cont *pdc;
9694         struct ctl_lun *lun;
9695         struct ctl_port *port;
9696         int data_len, num_target_ports, iid_len, id_len;
9697
9698         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9699
9700         num_target_ports = 0;
9701         iid_len = 0;
9702         id_len = 0;
9703         mtx_lock(&softc->ctl_lock);
9704         STAILQ_FOREACH(port, &softc->port_list, links) {
9705                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9706                         continue;
9707                 if (lun != NULL &&
9708                     ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9709                         continue;
9710                 num_target_ports++;
9711                 if (port->init_devid)
9712                         iid_len += port->init_devid->len;
9713                 if (port->port_devid)
9714                         id_len += port->port_devid->len;
9715         }
9716         mtx_unlock(&softc->ctl_lock);
9717
9718         data_len = sizeof(struct scsi_vpd_scsi_ports) +
9719             num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9720              sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9721         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9722         sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9723         ctsio->kern_sg_entries = 0;
9724
9725         if (data_len < alloc_len) {
9726                 ctsio->residual = alloc_len - data_len;
9727                 ctsio->kern_data_len = data_len;
9728                 ctsio->kern_total_len = data_len;
9729         } else {
9730                 ctsio->residual = 0;
9731                 ctsio->kern_data_len = alloc_len;
9732                 ctsio->kern_total_len = alloc_len;
9733         }
9734         ctsio->kern_data_resid = 0;
9735         ctsio->kern_rel_offset = 0;
9736         ctsio->kern_sg_entries = 0;
9737
9738         /*
9739          * The control device is always connected.  The disk device, on the
9740          * other hand, may not be online all the time.  Need to change this
9741          * to figure out whether the disk device is actually online or not.
9742          */
9743         if (lun != NULL)
9744                 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9745                                   lun->be_lun->lun_type;
9746         else
9747                 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9748
9749         sp->page_code = SVPD_SCSI_PORTS;
9750         scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9751             sp->page_length);
9752         pd = &sp->design[0];
9753
9754         mtx_lock(&softc->ctl_lock);
9755         STAILQ_FOREACH(port, &softc->port_list, links) {
9756                 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9757                         continue;
9758                 if (lun != NULL &&
9759                     ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9760                         continue;
9761                 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9762                 if (port->init_devid) {
9763                         iid_len = port->init_devid->len;
9764                         memcpy(pd->initiator_transportid,
9765                             port->init_devid->data, port->init_devid->len);
9766                 } else
9767                         iid_len = 0;
9768                 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9769                 pdc = (struct scsi_vpd_port_designation_cont *)
9770                     (&pd->initiator_transportid[iid_len]);
9771                 if (port->port_devid) {
9772                         id_len = port->port_devid->len;
9773                         memcpy(pdc->target_port_descriptors,
9774                             port->port_devid->data, port->port_devid->len);
9775                 } else
9776                         id_len = 0;
9777                 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9778                 pd = (struct scsi_vpd_port_designation *)
9779                     ((uint8_t *)pdc->target_port_descriptors + id_len);
9780         }
9781         mtx_unlock(&softc->ctl_lock);
9782
9783         ctl_set_success(ctsio);
9784         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9785         ctsio->be_move_done = ctl_config_move_done;
9786         ctl_datamove((union ctl_io *)ctsio);
9787         return (CTL_RETVAL_COMPLETE);
9788 }
9789
9790 static int
9791 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9792 {
9793         struct scsi_vpd_block_limits *bl_ptr;
9794         struct ctl_lun *lun;
9795         int bs;
9796
9797         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9798
9799         ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9800         bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9801         ctsio->kern_sg_entries = 0;
9802
9803         if (sizeof(*bl_ptr) < alloc_len) {
9804                 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9805                 ctsio->kern_data_len = sizeof(*bl_ptr);
9806                 ctsio->kern_total_len = sizeof(*bl_ptr);
9807         } else {
9808                 ctsio->residual = 0;
9809                 ctsio->kern_data_len = alloc_len;
9810                 ctsio->kern_total_len = alloc_len;
9811         }
9812         ctsio->kern_data_resid = 0;
9813         ctsio->kern_rel_offset = 0;
9814         ctsio->kern_sg_entries = 0;
9815
9816         /*
9817          * The control device is always connected.  The disk device, on the
9818          * other hand, may not be online all the time.  Need to change this
9819          * to figure out whether the disk device is actually online or not.
9820          */
9821         if (lun != NULL)
9822                 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9823                                   lun->be_lun->lun_type;
9824         else
9825                 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9826
9827         bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9828         scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9829         bl_ptr->max_cmp_write_len = 0xff;
9830         scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9831         if (lun != NULL) {
9832                 bs = lun->be_lun->blocksize;
9833                 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9834                 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9835                         scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9836                         scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9837                         if (lun->be_lun->ublockexp != 0) {
9838                                 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9839                                     bl_ptr->opt_unmap_grain);
9840                                 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9841                                     bl_ptr->unmap_grain_align);
9842                         }
9843                 }
9844                 scsi_ulto4b(lun->be_lun->atomicblock,
9845                     bl_ptr->max_atomic_transfer_length);
9846                 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9847                 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9848         }
9849         scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9850
9851         ctl_set_success(ctsio);
9852         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9853         ctsio->be_move_done = ctl_config_move_done;
9854         ctl_datamove((union ctl_io *)ctsio);
9855         return (CTL_RETVAL_COMPLETE);
9856 }
9857
9858 static int
9859 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9860 {
9861         struct scsi_vpd_block_device_characteristics *bdc_ptr;
9862         struct ctl_lun *lun;
9863         const char *value;
9864         u_int i;
9865
9866         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9867
9868         ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9869         bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9870         ctsio->kern_sg_entries = 0;
9871
9872         if (sizeof(*bdc_ptr) < alloc_len) {
9873                 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9874                 ctsio->kern_data_len = sizeof(*bdc_ptr);
9875                 ctsio->kern_total_len = sizeof(*bdc_ptr);
9876         } else {
9877                 ctsio->residual = 0;
9878                 ctsio->kern_data_len = alloc_len;
9879                 ctsio->kern_total_len = alloc_len;
9880         }
9881         ctsio->kern_data_resid = 0;
9882         ctsio->kern_rel_offset = 0;
9883         ctsio->kern_sg_entries = 0;
9884
9885         /*
9886          * The control device is always connected.  The disk device, on the
9887          * other hand, may not be online all the time.  Need to change this
9888          * to figure out whether the disk device is actually online or not.
9889          */
9890         if (lun != NULL)
9891                 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9892                                   lun->be_lun->lun_type;
9893         else
9894                 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9895         bdc_ptr->page_code = SVPD_BDC;
9896         scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9897         if (lun != NULL &&
9898             (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9899                 i = strtol(value, NULL, 0);
9900         else
9901                 i = CTL_DEFAULT_ROTATION_RATE;
9902         scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9903         if (lun != NULL &&
9904             (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9905                 i = strtol(value, NULL, 0);
9906         else
9907                 i = 0;
9908         bdc_ptr->wab_wac_ff = (i & 0x0f);
9909         bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9910
9911         ctl_set_success(ctsio);
9912         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9913         ctsio->be_move_done = ctl_config_move_done;
9914         ctl_datamove((union ctl_io *)ctsio);
9915         return (CTL_RETVAL_COMPLETE);
9916 }
9917
9918 static int
9919 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9920 {
9921         struct scsi_vpd_logical_block_prov *lbp_ptr;
9922         struct ctl_lun *lun;
9923
9924         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9925
9926         ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9927         lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9928         ctsio->kern_sg_entries = 0;
9929
9930         if (sizeof(*lbp_ptr) < alloc_len) {
9931                 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9932                 ctsio->kern_data_len = sizeof(*lbp_ptr);
9933                 ctsio->kern_total_len = sizeof(*lbp_ptr);
9934         } else {
9935                 ctsio->residual = 0;
9936                 ctsio->kern_data_len = alloc_len;
9937                 ctsio->kern_total_len = alloc_len;
9938         }
9939         ctsio->kern_data_resid = 0;
9940         ctsio->kern_rel_offset = 0;
9941         ctsio->kern_sg_entries = 0;
9942
9943         /*
9944          * The control device is always connected.  The disk device, on the
9945          * other hand, may not be online all the time.  Need to change this
9946          * to figure out whether the disk device is actually online or not.
9947          */
9948         if (lun != NULL)
9949                 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9950                                   lun->be_lun->lun_type;
9951         else
9952                 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9953
9954         lbp_ptr->page_code = SVPD_LBP;
9955         scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9956         lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9957         if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9958                 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9959                     SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9960                 lbp_ptr->prov_type = SVPD_LBP_THIN;
9961         }
9962
9963         ctl_set_success(ctsio);
9964         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9965         ctsio->be_move_done = ctl_config_move_done;
9966         ctl_datamove((union ctl_io *)ctsio);
9967         return (CTL_RETVAL_COMPLETE);
9968 }
9969
9970 /*
9971  * INQUIRY with the EVPD bit set.
9972  */
9973 static int
9974 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9975 {
9976         struct ctl_lun *lun;
9977         struct scsi_inquiry *cdb;
9978         int alloc_len, retval;
9979
9980         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9981         cdb = (struct scsi_inquiry *)ctsio->cdb;
9982         alloc_len = scsi_2btoul(cdb->length);
9983
9984         switch (cdb->page_code) {
9985         case SVPD_SUPPORTED_PAGES:
9986                 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9987                 break;
9988         case SVPD_UNIT_SERIAL_NUMBER:
9989                 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9990                 break;
9991         case SVPD_DEVICE_ID:
9992                 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9993                 break;
9994         case SVPD_EXTENDED_INQUIRY_DATA:
9995                 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9996                 break;
9997         case SVPD_MODE_PAGE_POLICY:
9998                 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9999                 break;
10000         case SVPD_SCSI_PORTS:
10001                 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10002                 break;
10003         case SVPD_SCSI_TPC:
10004                 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10005                 break;
10006         case SVPD_BLOCK_LIMITS:
10007                 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10008                         goto err;
10009                 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10010                 break;
10011         case SVPD_BDC:
10012                 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10013                         goto err;
10014                 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10015                 break;
10016         case SVPD_LBP:
10017                 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10018                         goto err;
10019                 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10020                 break;
10021         default:
10022 err:
10023                 ctl_set_invalid_field(ctsio,
10024                                       /*sks_valid*/ 1,
10025                                       /*command*/ 1,
10026                                       /*field*/ 2,
10027                                       /*bit_valid*/ 0,
10028                                       /*bit*/ 0);
10029                 ctl_done((union ctl_io *)ctsio);
10030                 retval = CTL_RETVAL_COMPLETE;
10031                 break;
10032         }
10033
10034         return (retval);
10035 }
10036
10037 /*
10038  * Standard INQUIRY data.
10039  */
10040 static int
10041 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10042 {
10043         struct scsi_inquiry_data *inq_ptr;
10044         struct scsi_inquiry *cdb;
10045         struct ctl_softc *softc;
10046         struct ctl_port *port;
10047         struct ctl_lun *lun;
10048         char *val;
10049         uint32_t alloc_len, data_len;
10050         ctl_port_type port_type;
10051
10052         softc = control_softc;
10053
10054         /*
10055          * Figure out whether we're talking to a Fibre Channel port or not.
10056          * We treat the ioctl front end, and any SCSI adapters, as packetized
10057          * SCSI front ends.
10058          */
10059         port = ctl_io_port(&ctsio->io_hdr);
10060         if (port != NULL)
10061                 port_type = port->port_type;
10062         else
10063                 port_type = CTL_PORT_SCSI;
10064         if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10065                 port_type = CTL_PORT_SCSI;
10066
10067         lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10068         cdb = (struct scsi_inquiry *)ctsio->cdb;
10069         alloc_len = scsi_2btoul(cdb->length);
10070
10071         /*
10072          * We malloc the full inquiry data size here and fill it
10073          * in.  If the user only asks for less, we'll give him
10074          * that much.
10075          */
10076         data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10077         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10078         inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10079         ctsio->kern_sg_entries = 0;
10080         ctsio->kern_data_resid = 0;
10081         ctsio->kern_rel_offset = 0;
10082
10083         if (data_len < alloc_len) {
10084                 ctsio->residual = alloc_len - data_len;
10085                 ctsio->kern_data_len = data_len;
10086                 ctsio->kern_total_len = data_len;
10087         } else {
10088                 ctsio->residual = 0;
10089                 ctsio->kern_data_len = alloc_len;
10090                 ctsio->kern_total_len = alloc_len;
10091         }
10092
10093         if (lun != NULL) {
10094                 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10095                     softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10096                         inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10097                             lun->be_lun->lun_type;
10098                 } else {
10099                         inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10100                             lun->be_lun->lun_type;
10101                 }
10102         } else
10103                 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10104
10105         /* RMB in byte 2 is 0 */
10106         inq_ptr->version = SCSI_REV_SPC4;
10107
10108         /*
10109          * According to SAM-3, even if a device only supports a single
10110          * level of LUN addressing, it should still set the HISUP bit:
10111          *
10112          * 4.9.1 Logical unit numbers overview
10113          *
10114          * All logical unit number formats described in this standard are
10115          * hierarchical in structure even when only a single level in that
10116          * hierarchy is used. The HISUP bit shall be set to one in the
10117          * standard INQUIRY data (see SPC-2) when any logical unit number
10118          * format described in this standard is used.  Non-hierarchical
10119          * formats are outside the scope of this standard.
10120          *
10121          * Therefore we set the HiSup bit here.
10122          *
10123          * The reponse format is 2, per SPC-3.
10124          */
10125         inq_ptr->response_format = SID_HiSup | 2;
10126
10127         inq_ptr->additional_length = data_len -
10128             (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10129         CTL_DEBUG_PRINT(("additional_length = %d\n",
10130                          inq_ptr->additional_length));
10131
10132         inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10133         /* 16 bit addressing */
10134         if (port_type == CTL_PORT_SCSI)
10135                 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10136         /* XXX set the SID_MultiP bit here if we're actually going to
10137            respond on multiple ports */
10138         inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10139
10140         /* 16 bit data bus, synchronous transfers */
10141         if (port_type == CTL_PORT_SCSI)
10142                 inq_ptr->flags = SID_WBus16 | SID_Sync;
10143         /*
10144          * XXX KDM do we want to support tagged queueing on the control
10145          * device at all?
10146          */
10147         if ((lun == NULL)
10148          || (lun->be_lun->lun_type != T_PROCESSOR))
10149                 inq_ptr->flags |= SID_CmdQue;
10150         /*
10151          * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10152          * We have 8 bytes for the vendor name, and 16 bytes for the device
10153          * name and 4 bytes for the revision.
10154          */
10155         if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10156             "vendor")) == NULL) {
10157                 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10158         } else {
10159                 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10160                 strncpy(inq_ptr->vendor, val,
10161                     min(sizeof(inq_ptr->vendor), strlen(val)));
10162         }
10163         if (lun == NULL) {
10164                 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10165                     sizeof(inq_ptr->product));
10166         } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10167                 switch (lun->be_lun->lun_type) {
10168                 case T_DIRECT:
10169                         strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10170                             sizeof(inq_ptr->product));
10171                         break;
10172                 case T_PROCESSOR:
10173                         strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10174                             sizeof(inq_ptr->product));
10175                         break;
10176                 default:
10177                         strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10178                             sizeof(inq_ptr->product));
10179                         break;
10180                 }
10181         } else {
10182                 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10183                 strncpy(inq_ptr->product, val,
10184                     min(sizeof(inq_ptr->product), strlen(val)));
10185         }
10186
10187         /*
10188          * XXX make this a macro somewhere so it automatically gets
10189          * incremented when we make changes.
10190          */
10191         if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10192             "revision")) == NULL) {
10193                 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10194         } else {
10195                 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10196                 strncpy(inq_ptr->revision, val,
10197                     min(sizeof(inq_ptr->revision), strlen(val)));
10198         }
10199
10200         /*
10201          * For parallel SCSI, we support double transition and single
10202          * transition clocking.  We also support QAS (Quick Arbitration
10203          * and Selection) and Information Unit transfers on both the
10204          * control and array devices.
10205          */
10206         if (port_type == CTL_PORT_SCSI)
10207                 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10208                                     SID_SPI_IUS;
10209
10210         /* SAM-5 (no version claimed) */
10211         scsi_ulto2b(0x00A0, inq_ptr->version1);
10212         /* SPC-4 (no version claimed) */
10213         scsi_ulto2b(0x0460, inq_ptr->version2);
10214         if (port_type == CTL_PORT_FC) {
10215                 /* FCP-2 ANSI INCITS.350:2003 */
10216                 scsi_ulto2b(0x0917, inq_ptr->version3);
10217         } else if (port_type == CTL_PORT_SCSI) {
10218                 /* SPI-4 ANSI INCITS.362:200x */
10219                 scsi_ulto2b(0x0B56, inq_ptr->version3);
10220         } else if (port_type == CTL_PORT_ISCSI) {
10221                 /* iSCSI (no version claimed) */
10222                 scsi_ulto2b(0x0960, inq_ptr->version3);
10223         } else if (port_type == CTL_PORT_SAS) {
10224                 /* SAS (no version claimed) */
10225                 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10226         }
10227
10228         if (lun == NULL) {
10229                 /* SBC-4 (no version claimed) */
10230                 scsi_ulto2b(0x0600, inq_ptr->version4);
10231         } else {
10232                 switch (lun->be_lun->lun_type) {
10233                 case T_DIRECT:
10234                         /* SBC-4 (no version claimed) */
10235                         scsi_ulto2b(0x0600, inq_ptr->version4);
10236                         break;
10237                 case T_PROCESSOR:
10238                 default:
10239                         break;
10240                 }
10241         }
10242
10243         ctl_set_success(ctsio);
10244         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10245         ctsio->be_move_done = ctl_config_move_done;
10246         ctl_datamove((union ctl_io *)ctsio);
10247         return (CTL_RETVAL_COMPLETE);
10248 }
10249
10250 int
10251 ctl_inquiry(struct ctl_scsiio *ctsio)
10252 {
10253         struct scsi_inquiry *cdb;
10254         int retval;
10255
10256         CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10257
10258         cdb = (struct scsi_inquiry *)ctsio->cdb;
10259         if (cdb->byte2 & SI_EVPD)
10260                 retval = ctl_inquiry_evpd(ctsio);
10261         else if (cdb->page_code == 0)
10262                 retval = ctl_inquiry_std(ctsio);
10263         else {
10264                 ctl_set_invalid_field(ctsio,
10265                                       /*sks_valid*/ 1,
10266                                       /*command*/ 1,
10267                                       /*field*/ 2,
10268                                       /*bit_valid*/ 0,
10269                                       /*bit*/ 0);
10270                 ctl_done((union ctl_io *)ctsio);
10271                 return (CTL_RETVAL_COMPLETE);
10272         }
10273
10274         return (retval);
10275 }
10276
10277 /*
10278  * For known CDB types, parse the LBA and length.
10279  */
10280 static int
10281 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10282 {
10283         if (io->io_hdr.io_type != CTL_IO_SCSI)
10284                 return (1);
10285
10286         switch (io->scsiio.cdb[0]) {
10287         case COMPARE_AND_WRITE: {
10288                 struct scsi_compare_and_write *cdb;
10289
10290                 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10291
10292                 *lba = scsi_8btou64(cdb->addr);
10293                 *len = cdb->length;
10294                 break;
10295         }
10296         case READ_6:
10297         case WRITE_6: {
10298                 struct scsi_rw_6 *cdb;
10299
10300                 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10301
10302                 *lba = scsi_3btoul(cdb->addr);
10303                 /* only 5 bits are valid in the most significant address byte */
10304                 *lba &= 0x1fffff;
10305                 *len = cdb->length;
10306                 break;
10307         }
10308         case READ_10:
10309         case WRITE_10: {
10310                 struct scsi_rw_10 *cdb;
10311
10312                 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10313
10314                 *lba = scsi_4btoul(cdb->addr);
10315                 *len = scsi_2btoul(cdb->length);
10316                 break;
10317         }
10318         case WRITE_VERIFY_10: {
10319                 struct scsi_write_verify_10 *cdb;
10320
10321                 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10322
10323                 *lba = scsi_4btoul(cdb->addr);
10324                 *len = scsi_2btoul(cdb->length);
10325                 break;
10326         }
10327         case READ_12:
10328         case WRITE_12: {
10329                 struct scsi_rw_12 *cdb;
10330
10331                 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10332
10333                 *lba = scsi_4btoul(cdb->addr);
10334                 *len = scsi_4btoul(cdb->length);
10335                 break;
10336         }
10337         case WRITE_VERIFY_12: {
10338                 struct scsi_write_verify_12 *cdb;
10339
10340                 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10341
10342                 *lba = scsi_4btoul(cdb->addr);
10343                 *len = scsi_4btoul(cdb->length);
10344                 break;
10345         }
10346         case READ_16:
10347         case WRITE_16:
10348         case WRITE_ATOMIC_16: {
10349                 struct scsi_rw_16 *cdb;
10350
10351                 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10352
10353                 *lba = scsi_8btou64(cdb->addr);
10354                 *len = scsi_4btoul(cdb->length);
10355                 break;
10356         }
10357         case WRITE_VERIFY_16: {
10358                 struct scsi_write_verify_16 *cdb;
10359
10360                 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10361
10362                 *lba = scsi_8btou64(cdb->addr);
10363                 *len = scsi_4btoul(cdb->length);
10364                 break;
10365         }
10366         case WRITE_SAME_10: {
10367                 struct scsi_write_same_10 *cdb;
10368
10369                 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10370
10371                 *lba = scsi_4btoul(cdb->addr);
10372                 *len = scsi_2btoul(cdb->length);
10373                 break;
10374         }
10375         case WRITE_SAME_16: {
10376                 struct scsi_write_same_16 *cdb;
10377
10378                 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10379
10380                 *lba = scsi_8btou64(cdb->addr);
10381                 *len = scsi_4btoul(cdb->length);
10382                 break;
10383         }
10384         case VERIFY_10: {
10385                 struct scsi_verify_10 *cdb;
10386
10387                 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10388
10389                 *lba = scsi_4btoul(cdb->addr);
10390                 *len = scsi_2btoul(cdb->length);
10391                 break;
10392         }
10393         case VERIFY_12: {
10394                 struct scsi_verify_12 *cdb;
10395
10396                 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10397
10398                 *lba = scsi_4btoul(cdb->addr);
10399                 *len = scsi_4btoul(cdb->length);
10400                 break;
10401         }
10402         case VERIFY_16: {
10403                 struct scsi_verify_16 *cdb;
10404
10405                 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10406
10407                 *lba = scsi_8btou64(cdb->addr);
10408                 *len = scsi_4btoul(cdb->length);
10409                 break;
10410         }
10411         case UNMAP: {
10412                 *lba = 0;
10413                 *len = UINT64_MAX;
10414                 break;
10415         }
10416         case SERVICE_ACTION_IN: {       /* GET LBA STATUS */
10417                 struct scsi_get_lba_status *cdb;
10418
10419                 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10420                 *lba = scsi_8btou64(cdb->addr);
10421                 *len = UINT32_MAX;
10422                 break;
10423         }
10424         default:
10425                 return (1);
10426                 break; /* NOTREACHED */
10427         }
10428
10429         return (0);
10430 }
10431
10432 static ctl_action
10433 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10434     bool seq)
10435 {
10436         uint64_t endlba1, endlba2;
10437
10438         endlba1 = lba1 + len1 - (seq ? 0 : 1);
10439         endlba2 = lba2 + len2 - 1;
10440
10441         if ((endlba1 < lba2) || (endlba2 < lba1))
10442                 return (CTL_ACTION_PASS);
10443         else
10444                 return (CTL_ACTION_BLOCK);
10445 }
10446
10447 static int
10448 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10449 {
10450         struct ctl_ptr_len_flags *ptrlen;
10451         struct scsi_unmap_desc *buf, *end, *range;
10452         uint64_t lba;
10453         uint32_t len;
10454
10455         /* If not UNMAP -- go other way. */
10456         if (io->io_hdr.io_type != CTL_IO_SCSI ||
10457             io->scsiio.cdb[0] != UNMAP)
10458                 return (CTL_ACTION_ERROR);
10459
10460         /* If UNMAP without data -- block and wait for data. */
10461         ptrlen = (struct ctl_ptr_len_flags *)
10462             &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10463         if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10464             ptrlen->ptr == NULL)
10465                 return (CTL_ACTION_BLOCK);
10466
10467         /* UNMAP with data -- check for collision. */
10468         buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10469         end = buf + ptrlen->len / sizeof(*buf);
10470         for (range = buf; range < end; range++) {
10471                 lba = scsi_8btou64(range->lba);
10472                 len = scsi_4btoul(range->length);
10473                 if ((lba < lba2 + len2) && (lba + len > lba2))
10474                         return (CTL_ACTION_BLOCK);
10475         }
10476         return (CTL_ACTION_PASS);
10477 }
10478
10479 static ctl_action
10480 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10481 {
10482         uint64_t lba1, lba2;
10483         uint64_t len1, len2;
10484         int retval;
10485
10486         if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10487                 return (CTL_ACTION_ERROR);
10488
10489         retval = ctl_extent_check_unmap(io1, lba2, len2);
10490         if (retval != CTL_ACTION_ERROR)
10491                 return (retval);
10492
10493         if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10494                 return (CTL_ACTION_ERROR);
10495
10496         return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10497 }
10498
10499 static ctl_action
10500 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10501 {
10502         uint64_t lba1, lba2;
10503         uint64_t len1, len2;
10504
10505         if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10506                 return (CTL_ACTION_ERROR);
10507         if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10508                 return (CTL_ACTION_ERROR);
10509
10510         if (lba1 + len1 == lba2)
10511                 return (CTL_ACTION_BLOCK);
10512         return (CTL_ACTION_PASS);
10513 }
10514
10515 static ctl_action
10516 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10517     union ctl_io *ooa_io)
10518 {
10519         const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10520         ctl_serialize_action *serialize_row;
10521
10522         /*
10523          * The initiator attempted multiple untagged commands at the same
10524          * time.  Can't do that.
10525          */
10526         if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10527          && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10528          && ((pending_io->io_hdr.nexus.targ_port ==
10529               ooa_io->io_hdr.nexus.targ_port)
10530           && (pending_io->io_hdr.nexus.initid ==
10531               ooa_io->io_hdr.nexus.initid))
10532          && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10533               CTL_FLAG_STATUS_SENT)) == 0))
10534                 return (CTL_ACTION_OVERLAP);
10535
10536         /*
10537          * The initiator attempted to send multiple tagged commands with
10538          * the same ID.  (It's fine if different initiators have the same
10539          * tag ID.)
10540          *
10541          * Even if all of those conditions are true, we don't kill the I/O
10542          * if the command ahead of us has been aborted.  We won't end up
10543          * sending it to the FETD, and it's perfectly legal to resend a
10544          * command with the same tag number as long as the previous
10545          * instance of this tag number has been aborted somehow.
10546          */
10547         if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10548          && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10549          && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10550          && ((pending_io->io_hdr.nexus.targ_port ==
10551               ooa_io->io_hdr.nexus.targ_port)
10552           && (pending_io->io_hdr.nexus.initid ==
10553               ooa_io->io_hdr.nexus.initid))
10554          && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10555               CTL_FLAG_STATUS_SENT)) == 0))
10556                 return (CTL_ACTION_OVERLAP_TAG);
10557
10558         /*
10559          * If we get a head of queue tag, SAM-3 says that we should
10560          * immediately execute it.
10561          *
10562          * What happens if this command would normally block for some other
10563          * reason?  e.g. a request sense with a head of queue tag
10564          * immediately after a write.  Normally that would block, but this
10565          * will result in its getting executed immediately...
10566          *
10567          * We currently return "pass" instead of "skip", so we'll end up
10568          * going through the rest of the queue to check for overlapped tags.
10569          *
10570          * XXX KDM check for other types of blockage first??
10571          */
10572         if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10573                 return (CTL_ACTION_PASS);
10574
10575         /*
10576          * Ordered tags have to block until all items ahead of them
10577          * have completed.  If we get called with an ordered tag, we always
10578          * block, if something else is ahead of us in the queue.
10579          */
10580         if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10581                 return (CTL_ACTION_BLOCK);
10582
10583         /*
10584          * Simple tags get blocked until all head of queue and ordered tags
10585          * ahead of them have completed.  I'm lumping untagged commands in
10586          * with simple tags here.  XXX KDM is that the right thing to do?
10587          */
10588         if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10589           || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10590          && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10591           || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10592                 return (CTL_ACTION_BLOCK);
10593
10594         pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10595         ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10596
10597         serialize_row = ctl_serialize_table[ooa_entry->seridx];
10598
10599         switch (serialize_row[pending_entry->seridx]) {
10600         case CTL_SER_BLOCK:
10601                 return (CTL_ACTION_BLOCK);
10602         case CTL_SER_EXTENT:
10603                 return (ctl_extent_check(ooa_io, pending_io,
10604                     (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10605         case CTL_SER_EXTENTOPT:
10606                 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10607                     & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10608                         return (ctl_extent_check(ooa_io, pending_io,
10609                             (lun->be_lun &&
10610                              lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10611                 return (CTL_ACTION_PASS);
10612         case CTL_SER_EXTENTSEQ:
10613                 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10614                         return (ctl_extent_check_seq(ooa_io, pending_io));
10615                 return (CTL_ACTION_PASS);
10616         case CTL_SER_PASS:
10617                 return (CTL_ACTION_PASS);
10618         case CTL_SER_BLOCKOPT:
10619                 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10620                     & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10621                         return (CTL_ACTION_BLOCK);
10622                 return (CTL_ACTION_PASS);
10623         case CTL_SER_SKIP:
10624                 return (CTL_ACTION_SKIP);
10625         default:
10626                 panic("invalid serialization value %d",
10627                       serialize_row[pending_entry->seridx]);
10628         }
10629
10630         return (CTL_ACTION_ERROR);
10631 }
10632
10633 /*
10634  * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10635  * Assumptions:
10636  * - pending_io is generally either incoming, or on the blocked queue
10637  * - starting I/O is the I/O we want to start the check with.
10638  */
10639 static ctl_action
10640 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10641               union ctl_io *starting_io)
10642 {
10643         union ctl_io *ooa_io;
10644         ctl_action action;
10645
10646         mtx_assert(&lun->lun_lock, MA_OWNED);
10647
10648         /*
10649          * Run back along the OOA queue, starting with the current
10650          * blocked I/O and going through every I/O before it on the
10651          * queue.  If starting_io is NULL, we'll just end up returning
10652          * CTL_ACTION_PASS.
10653          */
10654         for (ooa_io = starting_io; ooa_io != NULL;
10655              ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10656              ooa_links)){
10657
10658                 /*
10659                  * This routine just checks to see whether
10660                  * cur_blocked is blocked by ooa_io, which is ahead
10661                  * of it in the queue.  It doesn't queue/dequeue
10662                  * cur_blocked.
10663                  */
10664                 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10665                 switch (action) {
10666                 case CTL_ACTION_BLOCK:
10667                 case CTL_ACTION_OVERLAP:
10668                 case CTL_ACTION_OVERLAP_TAG:
10669                 case CTL_ACTION_SKIP:
10670                 case CTL_ACTION_ERROR:
10671                         return (action);
10672                         break; /* NOTREACHED */
10673                 case CTL_ACTION_PASS:
10674                         break;
10675                 default:
10676                         panic("invalid action %d", action);
10677                         break;  /* NOTREACHED */
10678                 }
10679         }
10680
10681         return (CTL_ACTION_PASS);
10682 }
10683
10684 /*
10685  * Assumptions:
10686  * - An I/O has just completed, and has been removed from the per-LUN OOA
10687  *   queue, so some items on the blocked queue may now be unblocked.
10688  */
10689 static int
10690 ctl_check_blocked(struct ctl_lun *lun)
10691 {
10692         struct ctl_softc *softc = lun->ctl_softc;
10693         union ctl_io *cur_blocked, *next_blocked;
10694
10695         mtx_assert(&lun->lun_lock, MA_OWNED);
10696
10697         /*
10698          * Run forward from the head of the blocked queue, checking each
10699          * entry against the I/Os prior to it on the OOA queue to see if
10700          * there is still any blockage.
10701          *
10702          * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10703          * with our removing a variable on it while it is traversing the
10704          * list.
10705          */
10706         for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10707              cur_blocked != NULL; cur_blocked = next_blocked) {
10708                 union ctl_io *prev_ooa;
10709                 ctl_action action;
10710
10711                 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10712                                                           blocked_links);
10713
10714                 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10715                                                       ctl_ooaq, ooa_links);
10716
10717                 /*
10718                  * If cur_blocked happens to be the first item in the OOA
10719                  * queue now, prev_ooa will be NULL, and the action
10720                  * returned will just be CTL_ACTION_PASS.
10721                  */
10722                 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10723
10724                 switch (action) {
10725                 case CTL_ACTION_BLOCK:
10726                         /* Nothing to do here, still blocked */
10727                         break;
10728                 case CTL_ACTION_OVERLAP:
10729                 case CTL_ACTION_OVERLAP_TAG:
10730                         /*
10731                          * This shouldn't happen!  In theory we've already
10732                          * checked this command for overlap...
10733                          */
10734                         break;
10735                 case CTL_ACTION_PASS:
10736                 case CTL_ACTION_SKIP: {
10737                         const struct ctl_cmd_entry *entry;
10738
10739                         /*
10740                          * The skip case shouldn't happen, this transaction
10741                          * should have never made it onto the blocked queue.
10742                          */
10743                         /*
10744                          * This I/O is no longer blocked, we can remove it
10745                          * from the blocked queue.  Since this is a TAILQ
10746                          * (doubly linked list), we can do O(1) removals
10747                          * from any place on the list.
10748                          */
10749                         TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10750                                      blocked_links);
10751                         cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10752
10753                         if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
10754                             (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
10755                                 /*
10756                                  * Need to send IO back to original side to
10757                                  * run
10758                                  */
10759                                 union ctl_ha_msg msg_info;
10760
10761                                 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10762                                 msg_info.hdr.original_sc =
10763                                         cur_blocked->io_hdr.original_sc;
10764                                 msg_info.hdr.serializing_sc = cur_blocked;
10765                                 msg_info.hdr.msg_type = CTL_MSG_R2R;
10766                                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10767                                     sizeof(msg_info.hdr), M_NOWAIT);
10768                                 break;
10769                         }
10770                         entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10771
10772                         /*
10773                          * Check this I/O for LUN state changes that may
10774                          * have happened while this command was blocked.
10775                          * The LUN state may have been changed by a command
10776                          * ahead of us in the queue, so we need to re-check
10777                          * for any states that can be caused by SCSI
10778                          * commands.
10779                          */
10780                         if (ctl_scsiio_lun_check(lun, entry,
10781                                                  &cur_blocked->scsiio) == 0) {
10782                                 cur_blocked->io_hdr.flags |=
10783                                                       CTL_FLAG_IS_WAS_ON_RTR;
10784                                 ctl_enqueue_rtr(cur_blocked);
10785                         } else
10786                                 ctl_done(cur_blocked);
10787                         break;
10788                 }
10789                 default:
10790                         /*
10791                          * This probably shouldn't happen -- we shouldn't
10792                          * get CTL_ACTION_ERROR, or anything else.
10793                          */
10794                         break;
10795                 }
10796         }
10797
10798         return (CTL_RETVAL_COMPLETE);
10799 }
10800
10801 /*
10802  * This routine (with one exception) checks LUN flags that can be set by
10803  * commands ahead of us in the OOA queue.  These flags have to be checked
10804  * when a command initially comes in, and when we pull a command off the
10805  * blocked queue and are preparing to execute it.  The reason we have to
10806  * check these flags for commands on the blocked queue is that the LUN
10807  * state may have been changed by a command ahead of us while we're on the
10808  * blocked queue.
10809  *
10810  * Ordering is somewhat important with these checks, so please pay
10811  * careful attention to the placement of any new checks.
10812  */
10813 static int
10814 ctl_scsiio_lun_check(struct ctl_lun *lun,
10815     const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10816 {
10817         struct ctl_softc *softc = lun->ctl_softc;
10818         int retval;
10819         uint32_t residx;
10820
10821         retval = 0;
10822
10823         mtx_assert(&lun->lun_lock, MA_OWNED);
10824
10825         /*
10826          * If this shelf is a secondary shelf controller, we may have to
10827          * reject some commands disallowed by HA mode and link state.
10828          */
10829         if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10830                 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
10831                     (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10832                         ctl_set_lun_unavail(ctsio);
10833                         retval = 1;
10834                         goto bailout;
10835                 }
10836                 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
10837                     (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10838                         ctl_set_lun_transit(ctsio);
10839                         retval = 1;
10840                         goto bailout;
10841                 }
10842                 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
10843                     (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
10844                         ctl_set_lun_standby(ctsio);
10845                         retval = 1;
10846                         goto bailout;
10847                 }
10848
10849                 /* The rest of checks are only done on executing side */
10850                 if (softc->ha_mode == CTL_HA_MODE_XFER)
10851                         goto bailout;
10852         }
10853
10854         if (entry->pattern & CTL_LUN_PAT_WRITE) {
10855                 if (lun->be_lun &&
10856                     lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
10857                         ctl_set_hw_write_protected(ctsio);
10858                         retval = 1;
10859                         goto bailout;
10860                 }
10861                 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
10862                     .eca_and_aen & SCP_SWP) != 0) {
10863                         ctl_set_sense(ctsio, /*current_error*/ 1,
10864                             /*sense_key*/ SSD_KEY_DATA_PROTECT,
10865                             /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
10866                         retval = 1;
10867                         goto bailout;
10868                 }
10869         }
10870
10871         /*
10872          * Check for a reservation conflict.  If this command isn't allowed
10873          * even on reserved LUNs, and if this initiator isn't the one who
10874          * reserved us, reject the command with a reservation conflict.
10875          */
10876         residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10877         if ((lun->flags & CTL_LUN_RESERVED)
10878          && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10879                 if (lun->res_idx != residx) {
10880                         ctl_set_reservation_conflict(ctsio);
10881                         retval = 1;
10882                         goto bailout;
10883                 }
10884         }
10885
10886         if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
10887             (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
10888                 /* No reservation or command is allowed. */;
10889         } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
10890             (lun->res_type == SPR_TYPE_WR_EX ||
10891              lun->res_type == SPR_TYPE_WR_EX_RO ||
10892              lun->res_type == SPR_TYPE_WR_EX_AR)) {
10893                 /* The command is allowed for Write Exclusive resv. */;
10894         } else {
10895                 /*
10896                  * if we aren't registered or it's a res holder type
10897                  * reservation and this isn't the res holder then set a
10898                  * conflict.
10899                  */
10900                 if (ctl_get_prkey(lun, residx) == 0
10901                  || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10902                         ctl_set_reservation_conflict(ctsio);
10903                         retval = 1;
10904                         goto bailout;
10905                 }
10906         }
10907
10908         if ((lun->flags & CTL_LUN_OFFLINE)
10909          && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) {
10910                 ctl_set_lun_not_ready(ctsio);
10911                 retval = 1;
10912                 goto bailout;
10913         }
10914
10915         if ((lun->flags & CTL_LUN_STOPPED)
10916          && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10917                 /* "Logical unit not ready, initializing cmd. required" */
10918                 ctl_set_lun_stopped(ctsio);
10919                 retval = 1;
10920                 goto bailout;
10921         }
10922
10923         if ((lun->flags & CTL_LUN_INOPERABLE)
10924          && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10925                 /* "Medium format corrupted" */
10926                 ctl_set_medium_format_corrupted(ctsio);
10927                 retval = 1;
10928                 goto bailout;
10929         }
10930
10931 bailout:
10932         return (retval);
10933 }
10934
10935 static void
10936 ctl_failover_io(union ctl_io *io, int have_lock)
10937 {
10938         ctl_set_busy(&io->scsiio);
10939         ctl_done(io);
10940 }
10941
10942 static void
10943 ctl_failover_lun(struct ctl_lun *lun)
10944 {
10945         struct ctl_softc *softc = lun->ctl_softc;
10946         struct ctl_io_hdr *io, *next_io;
10947
10948         CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun));
10949         if (softc->ha_mode == CTL_HA_MODE_XFER) {
10950                 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10951                         /* We are master */
10952                         if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10953                                 if (io->flags & CTL_FLAG_IO_ACTIVE) {
10954                                         io->flags |= CTL_FLAG_ABORT;
10955                                 } else { /* This can be only due to DATAMOVE */
10956                                         io->msg_type = CTL_MSG_DATAMOVE_DONE;
10957                                         io->flags |= CTL_FLAG_IO_ACTIVE;
10958                                         io->port_status = 31340;
10959                                         ctl_enqueue_isc((union ctl_io *)io);
10960                                 }
10961                         }
10962                         /* We are slave */
10963                         if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10964                                 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10965                                 if (io->flags & CTL_FLAG_IO_ACTIVE) {
10966                                         io->flags |= CTL_FLAG_FAILOVER;
10967                                 } else {
10968                                         ctl_set_busy(&((union ctl_io *)io)->
10969                                             scsiio);
10970                                         ctl_done((union ctl_io *)io);
10971                                 }
10972                         }
10973                 }
10974         } else { /* SERIALIZE modes */
10975                 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
10976                     next_io) {
10977                         /* We are master */
10978                         if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10979                                 TAILQ_REMOVE(&lun->blocked_queue, io,
10980                                     blocked_links);
10981                                 io->flags &= ~CTL_FLAG_BLOCKED;
10982                                 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10983                                 ctl_free_io((union ctl_io *)io);
10984                         }
10985                 }
10986                 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10987                         /* We are master */
10988                         if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10989                                 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10990                                 ctl_free_io((union ctl_io *)io);
10991                         }
10992                         /* We are slave */
10993                         if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10994                                 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10995                                 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
10996                                         ctl_set_busy(&((union ctl_io *)io)->
10997                                             scsiio);
10998                                         ctl_done((union ctl_io *)io);
10999                                 }
11000                         }
11001                 }
11002                 ctl_check_blocked(lun);
11003         }
11004 }
11005
11006 static int
11007 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11008 {
11009         struct ctl_lun *lun;
11010         const struct ctl_cmd_entry *entry;
11011         uint32_t initidx, targ_lun;
11012         int retval;
11013
11014         retval = 0;
11015
11016         lun = NULL;
11017
11018         targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11019         if ((targ_lun < CTL_MAX_LUNS)
11020          && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11021                 /*
11022                  * If the LUN is invalid, pretend that it doesn't exist.
11023                  * It will go away as soon as all pending I/O has been
11024                  * completed.
11025                  */
11026                 mtx_lock(&lun->lun_lock);
11027                 if (lun->flags & CTL_LUN_DISABLED) {
11028                         mtx_unlock(&lun->lun_lock);
11029                         lun = NULL;
11030                         ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11031                         ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11032                 } else {
11033                         ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11034                         ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11035                                 lun->be_lun;
11036
11037                         /*
11038                          * Every I/O goes into the OOA queue for a
11039                          * particular LUN, and stays there until completion.
11040                          */
11041 #ifdef CTL_TIME_IO
11042                         if (TAILQ_EMPTY(&lun->ooa_queue)) {
11043                                 lun->idle_time += getsbinuptime() -
11044                                     lun->last_busy;
11045                         }
11046 #endif
11047                         TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11048                             ooa_links);
11049                 }
11050         } else {
11051                 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11052                 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11053         }
11054
11055         /* Get command entry and return error if it is unsuppotyed. */
11056         entry = ctl_validate_command(ctsio);
11057         if (entry == NULL) {
11058                 if (lun)
11059                         mtx_unlock(&lun->lun_lock);
11060                 return (retval);
11061         }
11062
11063         ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11064         ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11065
11066         /*
11067          * Check to see whether we can send this command to LUNs that don't
11068          * exist.  This should pretty much only be the case for inquiry
11069          * and request sense.  Further checks, below, really require having
11070          * a LUN, so we can't really check the command anymore.  Just put
11071          * it on the rtr queue.
11072          */
11073         if (lun == NULL) {
11074                 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11075                         ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11076                         ctl_enqueue_rtr((union ctl_io *)ctsio);
11077                         return (retval);
11078                 }
11079
11080                 ctl_set_unsupported_lun(ctsio);
11081                 ctl_done((union ctl_io *)ctsio);
11082                 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11083                 return (retval);
11084         } else {
11085                 /*
11086                  * Make sure we support this particular command on this LUN.
11087                  * e.g., we don't support writes to the control LUN.
11088                  */
11089                 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11090                         mtx_unlock(&lun->lun_lock);
11091                         ctl_set_invalid_opcode(ctsio);
11092                         ctl_done((union ctl_io *)ctsio);
11093                         return (retval);
11094                 }
11095         }
11096
11097         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11098
11099 #ifdef CTL_WITH_CA
11100         /*
11101          * If we've got a request sense, it'll clear the contingent
11102          * allegiance condition.  Otherwise, if we have a CA condition for
11103          * this initiator, clear it, because it sent down a command other
11104          * than request sense.
11105          */
11106         if ((ctsio->cdb[0] != REQUEST_SENSE)
11107          && (ctl_is_set(lun->have_ca, initidx)))
11108                 ctl_clear_mask(lun->have_ca, initidx);
11109 #endif
11110
11111         /*
11112          * If the command has this flag set, it handles its own unit
11113          * attention reporting, we shouldn't do anything.  Otherwise we
11114          * check for any pending unit attentions, and send them back to the
11115          * initiator.  We only do this when a command initially comes in,
11116          * not when we pull it off the blocked queue.
11117          *
11118          * According to SAM-3, section 5.3.2, the order that things get
11119          * presented back to the host is basically unit attentions caused
11120          * by some sort of reset event, busy status, reservation conflicts
11121          * or task set full, and finally any other status.
11122          *
11123          * One issue here is that some of the unit attentions we report
11124          * don't fall into the "reset" category (e.g. "reported luns data
11125          * has changed").  So reporting it here, before the reservation
11126          * check, may be technically wrong.  I guess the only thing to do
11127          * would be to check for and report the reset events here, and then
11128          * check for the other unit attention types after we check for a
11129          * reservation conflict.
11130          *
11131          * XXX KDM need to fix this
11132          */
11133         if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11134                 ctl_ua_type ua_type;
11135                 scsi_sense_data_type sense_format;
11136
11137                 if (lun->flags & CTL_LUN_SENSE_DESC)
11138                         sense_format = SSD_TYPE_DESC;
11139                 else
11140                         sense_format = SSD_TYPE_FIXED;
11141
11142                 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11143                     sense_format);
11144                 if (ua_type != CTL_UA_NONE) {
11145                         mtx_unlock(&lun->lun_lock);
11146                         ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11147                         ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11148                         ctsio->sense_len = SSD_FULL_SIZE;
11149                         ctl_done((union ctl_io *)ctsio);
11150                         return (retval);
11151                 }
11152         }
11153
11154
11155         if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11156                 mtx_unlock(&lun->lun_lock);
11157                 ctl_done((union ctl_io *)ctsio);
11158                 return (retval);
11159         }
11160
11161         /*
11162          * XXX CHD this is where we want to send IO to other side if
11163          * this LUN is secondary on this SC. We will need to make a copy
11164          * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11165          * the copy we send as FROM_OTHER.
11166          * We also need to stuff the address of the original IO so we can
11167          * find it easily. Something similar will need be done on the other
11168          * side so when we are done we can find the copy.
11169          */
11170         if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11171             (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0) {
11172                 union ctl_ha_msg msg_info;
11173                 int isc_retval;
11174
11175                 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11176                 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11177                 mtx_unlock(&lun->lun_lock);
11178
11179                 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11180                 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11181                 msg_info.hdr.serializing_sc = NULL;
11182                 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11183                 msg_info.scsi.tag_num = ctsio->tag_num;
11184                 msg_info.scsi.tag_type = ctsio->tag_type;
11185                 msg_info.scsi.cdb_len = ctsio->cdb_len;
11186                 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11187
11188                 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11189                     sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11190                     M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11191                         ctl_set_busy(ctsio);
11192                         ctl_done((union ctl_io *)ctsio);
11193                         return (retval);
11194                 }
11195                 return (retval);
11196         }
11197
11198         switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11199                               (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11200                               ctl_ooaq, ooa_links))) {
11201         case CTL_ACTION_BLOCK:
11202                 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11203                 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11204                                   blocked_links);
11205                 mtx_unlock(&lun->lun_lock);
11206                 return (retval);
11207         case CTL_ACTION_PASS:
11208         case CTL_ACTION_SKIP:
11209                 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11210                 mtx_unlock(&lun->lun_lock);
11211                 ctl_enqueue_rtr((union ctl_io *)ctsio);
11212                 break;
11213         case CTL_ACTION_OVERLAP:
11214                 mtx_unlock(&lun->lun_lock);
11215                 ctl_set_overlapped_cmd(ctsio);
11216                 ctl_done((union ctl_io *)ctsio);
11217                 break;
11218         case CTL_ACTION_OVERLAP_TAG:
11219                 mtx_unlock(&lun->lun_lock);
11220                 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11221                 ctl_done((union ctl_io *)ctsio);
11222                 break;
11223         case CTL_ACTION_ERROR:
11224         default:
11225                 mtx_unlock(&lun->lun_lock);
11226                 ctl_set_internal_failure(ctsio,
11227                                          /*sks_valid*/ 0,
11228                                          /*retry_count*/ 0);
11229                 ctl_done((union ctl_io *)ctsio);
11230                 break;
11231         }
11232         return (retval);
11233 }
11234
11235 const struct ctl_cmd_entry *
11236 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11237 {
11238         const struct ctl_cmd_entry *entry;
11239         int service_action;
11240
11241         entry = &ctl_cmd_table[ctsio->cdb[0]];
11242         if (sa)
11243                 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11244         if (entry->flags & CTL_CMD_FLAG_SA5) {
11245                 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11246                 entry = &((const struct ctl_cmd_entry *)
11247                     entry->execute)[service_action];
11248         }
11249         return (entry);
11250 }
11251
11252 const struct ctl_cmd_entry *
11253 ctl_validate_command(struct ctl_scsiio *ctsio)
11254 {
11255         const struct ctl_cmd_entry *entry;
11256         int i, sa;
11257         uint8_t diff;
11258
11259         entry = ctl_get_cmd_entry(ctsio, &sa);
11260         if (entry->execute == NULL) {
11261                 if (sa)
11262                         ctl_set_invalid_field(ctsio,
11263                                               /*sks_valid*/ 1,
11264                                               /*command*/ 1,
11265                                               /*field*/ 1,
11266                                               /*bit_valid*/ 1,
11267                                               /*bit*/ 4);
11268                 else
11269                         ctl_set_invalid_opcode(ctsio);
11270                 ctl_done((union ctl_io *)ctsio);
11271                 return (NULL);
11272         }
11273         KASSERT(entry->length > 0,
11274             ("Not defined length for command 0x%02x/0x%02x",
11275              ctsio->cdb[0], ctsio->cdb[1]));
11276         for (i = 1; i < entry->length; i++) {
11277                 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11278                 if (diff == 0)
11279                         continue;
11280                 ctl_set_invalid_field(ctsio,
11281                                       /*sks_valid*/ 1,
11282                                       /*command*/ 1,
11283                                       /*field*/ i,
11284                                       /*bit_valid*/ 1,
11285                                       /*bit*/ fls(diff) - 1);
11286                 ctl_done((union ctl_io *)ctsio);
11287                 return (NULL);
11288         }
11289         return (entry);
11290 }
11291
11292 static int
11293 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11294 {
11295
11296         switch (lun_type) {
11297         case T_PROCESSOR:
11298                 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11299                     ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11300                         return (0);
11301                 break;
11302         case T_DIRECT:
11303                 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11304                     ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11305                         return (0);
11306                 break;
11307         default:
11308                 return (0);
11309         }
11310         return (1);
11311 }
11312
11313 static int
11314 ctl_scsiio(struct ctl_scsiio *ctsio)
11315 {
11316         int retval;
11317         const struct ctl_cmd_entry *entry;
11318
11319         retval = CTL_RETVAL_COMPLETE;
11320
11321         CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11322
11323         entry = ctl_get_cmd_entry(ctsio, NULL);
11324
11325         /*
11326          * If this I/O has been aborted, just send it straight to
11327          * ctl_done() without executing it.
11328          */
11329         if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11330                 ctl_done((union ctl_io *)ctsio);
11331                 goto bailout;
11332         }
11333
11334         /*
11335          * All the checks should have been handled by ctl_scsiio_precheck().
11336          * We should be clear now to just execute the I/O.
11337          */
11338         retval = entry->execute(ctsio);
11339
11340 bailout:
11341         return (retval);
11342 }
11343
11344 /*
11345  * Since we only implement one target right now, a bus reset simply resets
11346  * our single target.
11347  */
11348 static int
11349 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11350 {
11351         return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11352 }
11353
11354 static int
11355 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11356                  ctl_ua_type ua_type)
11357 {
11358         struct ctl_port *port;
11359         struct ctl_lun *lun;
11360         int retval;
11361
11362         if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11363                 union ctl_ha_msg msg_info;
11364
11365                 msg_info.hdr.nexus = io->io_hdr.nexus;
11366                 if (ua_type==CTL_UA_TARG_RESET)
11367                         msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11368                 else
11369                         msg_info.task.task_action = CTL_TASK_BUS_RESET;
11370                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11371                 msg_info.hdr.original_sc = NULL;
11372                 msg_info.hdr.serializing_sc = NULL;
11373                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11374                     sizeof(msg_info.task), M_WAITOK);
11375         }
11376         retval = 0;
11377
11378         mtx_lock(&softc->ctl_lock);
11379         port = softc->ctl_ports[io->io_hdr.nexus.targ_port];
11380         STAILQ_FOREACH(lun, &softc->lun_list, links) {
11381                 if (port != NULL &&
11382                     ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11383                         continue;
11384                 retval += ctl_do_lun_reset(lun, io, ua_type);
11385         }
11386         mtx_unlock(&softc->ctl_lock);
11387         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11388         return (retval);
11389 }
11390
11391 /*
11392  * The LUN should always be set.  The I/O is optional, and is used to
11393  * distinguish between I/Os sent by this initiator, and by other
11394  * initiators.  We set unit attention for initiators other than this one.
11395  * SAM-3 is vague on this point.  It does say that a unit attention should
11396  * be established for other initiators when a LUN is reset (see section
11397  * 5.7.3), but it doesn't specifically say that the unit attention should
11398  * be established for this particular initiator when a LUN is reset.  Here
11399  * is the relevant text, from SAM-3 rev 8:
11400  *
11401  * 5.7.2 When a SCSI initiator port aborts its own tasks
11402  *
11403  * When a SCSI initiator port causes its own task(s) to be aborted, no
11404  * notification that the task(s) have been aborted shall be returned to
11405  * the SCSI initiator port other than the completion response for the
11406  * command or task management function action that caused the task(s) to
11407  * be aborted and notification(s) associated with related effects of the
11408  * action (e.g., a reset unit attention condition).
11409  *
11410  * XXX KDM for now, we're setting unit attention for all initiators.
11411  */
11412 static int
11413 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11414 {
11415         union ctl_io *xio;
11416 #if 0
11417         uint32_t initidx;
11418 #endif
11419 #ifdef CTL_WITH_CA
11420         int i;
11421 #endif
11422
11423         mtx_lock(&lun->lun_lock);
11424         /*
11425          * Run through the OOA queue and abort each I/O.
11426          */
11427         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11428              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11429                 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11430         }
11431
11432         /*
11433          * This version sets unit attention for every
11434          */
11435 #if 0
11436         initidx = ctl_get_initindex(&io->io_hdr.nexus);
11437         ctl_est_ua_all(lun, initidx, ua_type);
11438 #else
11439         ctl_est_ua_all(lun, -1, ua_type);
11440 #endif
11441
11442         /*
11443          * A reset (any kind, really) clears reservations established with
11444          * RESERVE/RELEASE.  It does not clear reservations established
11445          * with PERSISTENT RESERVE OUT, but we don't support that at the
11446          * moment anyway.  See SPC-2, section 5.6.  SPC-3 doesn't address
11447          * reservations made with the RESERVE/RELEASE commands, because
11448          * those commands are obsolete in SPC-3.
11449          */
11450         lun->flags &= ~CTL_LUN_RESERVED;
11451
11452 #ifdef CTL_WITH_CA
11453         for (i = 0; i < CTL_MAX_INITIATORS; i++)
11454                 ctl_clear_mask(lun->have_ca, i);
11455 #endif
11456         mtx_unlock(&lun->lun_lock);
11457
11458         return (0);
11459 }
11460
11461 static int
11462 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11463 {
11464         struct ctl_lun *lun;
11465         uint32_t targ_lun;
11466         int retval;
11467
11468         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11469         mtx_lock(&softc->ctl_lock);
11470         if ((targ_lun >= CTL_MAX_LUNS) ||
11471             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11472                 mtx_unlock(&softc->ctl_lock);
11473                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11474                 return (1);
11475         }
11476         retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11477         mtx_unlock(&softc->ctl_lock);
11478         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11479
11480         if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11481                 union ctl_ha_msg msg_info;
11482
11483                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11484                 msg_info.hdr.nexus = io->io_hdr.nexus;
11485                 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11486                 msg_info.hdr.original_sc = NULL;
11487                 msg_info.hdr.serializing_sc = NULL;
11488                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11489                     sizeof(msg_info.task), M_WAITOK);
11490         }
11491         return (retval);
11492 }
11493
11494 static void
11495 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11496     int other_sc)
11497 {
11498         union ctl_io *xio;
11499
11500         mtx_assert(&lun->lun_lock, MA_OWNED);
11501
11502         /*
11503          * Run through the OOA queue and attempt to find the given I/O.
11504          * The target port, initiator ID, tag type and tag number have to
11505          * match the values that we got from the initiator.  If we have an
11506          * untagged command to abort, simply abort the first untagged command
11507          * we come to.  We only allow one untagged command at a time of course.
11508          */
11509         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11510              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11511
11512                 if ((targ_port == UINT32_MAX ||
11513                      targ_port == xio->io_hdr.nexus.targ_port) &&
11514                     (init_id == UINT32_MAX ||
11515                      init_id == xio->io_hdr.nexus.initid)) {
11516                         if (targ_port != xio->io_hdr.nexus.targ_port ||
11517                             init_id != xio->io_hdr.nexus.initid)
11518                                 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11519                         xio->io_hdr.flags |= CTL_FLAG_ABORT;
11520                         if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11521                                 union ctl_ha_msg msg_info;
11522
11523                                 msg_info.hdr.nexus = xio->io_hdr.nexus;
11524                                 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11525                                 msg_info.task.tag_num = xio->scsiio.tag_num;
11526                                 msg_info.task.tag_type = xio->scsiio.tag_type;
11527                                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11528                                 msg_info.hdr.original_sc = NULL;
11529                                 msg_info.hdr.serializing_sc = NULL;
11530                                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11531                                     sizeof(msg_info.task), M_NOWAIT);
11532                         }
11533                 }
11534         }
11535 }
11536
11537 static int
11538 ctl_abort_task_set(union ctl_io *io)
11539 {
11540         struct ctl_softc *softc = control_softc;
11541         struct ctl_lun *lun;
11542         uint32_t targ_lun;
11543
11544         /*
11545          * Look up the LUN.
11546          */
11547         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11548         mtx_lock(&softc->ctl_lock);
11549         if ((targ_lun >= CTL_MAX_LUNS) ||
11550             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11551                 mtx_unlock(&softc->ctl_lock);
11552                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11553                 return (1);
11554         }
11555
11556         mtx_lock(&lun->lun_lock);
11557         mtx_unlock(&softc->ctl_lock);
11558         if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11559                 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11560                     io->io_hdr.nexus.initid,
11561                     (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11562         } else { /* CTL_TASK_CLEAR_TASK_SET */
11563                 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11564                     (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11565         }
11566         mtx_unlock(&lun->lun_lock);
11567         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11568         return (0);
11569 }
11570
11571 static int
11572 ctl_i_t_nexus_reset(union ctl_io *io)
11573 {
11574         struct ctl_softc *softc = control_softc;
11575         struct ctl_lun *lun;
11576         uint32_t initidx;
11577
11578         if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11579                 union ctl_ha_msg msg_info;
11580
11581                 msg_info.hdr.nexus = io->io_hdr.nexus;
11582                 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11583                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11584                 msg_info.hdr.original_sc = NULL;
11585                 msg_info.hdr.serializing_sc = NULL;
11586                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11587                     sizeof(msg_info.task), M_WAITOK);
11588         }
11589
11590         initidx = ctl_get_initindex(&io->io_hdr.nexus);
11591         mtx_lock(&softc->ctl_lock);
11592         STAILQ_FOREACH(lun, &softc->lun_list, links) {
11593                 mtx_lock(&lun->lun_lock);
11594                 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11595                     io->io_hdr.nexus.initid, 1);
11596 #ifdef CTL_WITH_CA
11597                 ctl_clear_mask(lun->have_ca, initidx);
11598 #endif
11599                 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11600                         lun->flags &= ~CTL_LUN_RESERVED;
11601                 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11602                 mtx_unlock(&lun->lun_lock);
11603         }
11604         mtx_unlock(&softc->ctl_lock);
11605         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11606         return (0);
11607 }
11608
11609 static int
11610 ctl_abort_task(union ctl_io *io)
11611 {
11612         union ctl_io *xio;
11613         struct ctl_lun *lun;
11614         struct ctl_softc *softc;
11615 #if 0
11616         struct sbuf sb;
11617         char printbuf[128];
11618 #endif
11619         int found;
11620         uint32_t targ_lun;
11621
11622         softc = control_softc;
11623         found = 0;
11624
11625         /*
11626          * Look up the LUN.
11627          */
11628         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11629         mtx_lock(&softc->ctl_lock);
11630         if ((targ_lun >= CTL_MAX_LUNS) ||
11631             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11632                 mtx_unlock(&softc->ctl_lock);
11633                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11634                 return (1);
11635         }
11636
11637 #if 0
11638         printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11639                lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11640 #endif
11641
11642         mtx_lock(&lun->lun_lock);
11643         mtx_unlock(&softc->ctl_lock);
11644         /*
11645          * Run through the OOA queue and attempt to find the given I/O.
11646          * The target port, initiator ID, tag type and tag number have to
11647          * match the values that we got from the initiator.  If we have an
11648          * untagged command to abort, simply abort the first untagged command
11649          * we come to.  We only allow one untagged command at a time of course.
11650          */
11651         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11652              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11653 #if 0
11654                 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11655
11656                 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11657                             lun->lun, xio->scsiio.tag_num,
11658                             xio->scsiio.tag_type,
11659                             (xio->io_hdr.blocked_links.tqe_prev
11660                             == NULL) ? "" : " BLOCKED",
11661                             (xio->io_hdr.flags &
11662                             CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11663                             (xio->io_hdr.flags &
11664                             CTL_FLAG_ABORT) ? " ABORT" : "",
11665                             (xio->io_hdr.flags &
11666                             CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11667                 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11668                 sbuf_finish(&sb);
11669                 printf("%s\n", sbuf_data(&sb));
11670 #endif
11671
11672                 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11673                  || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11674                  || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11675                         continue;
11676
11677                 /*
11678                  * If the abort says that the task is untagged, the
11679                  * task in the queue must be untagged.  Otherwise,
11680                  * we just check to see whether the tag numbers
11681                  * match.  This is because the QLogic firmware
11682                  * doesn't pass back the tag type in an abort
11683                  * request.
11684                  */
11685 #if 0
11686                 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11687                   && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11688                  || (xio->scsiio.tag_num == io->taskio.tag_num))
11689 #endif
11690                 /*
11691                  * XXX KDM we've got problems with FC, because it
11692                  * doesn't send down a tag type with aborts.  So we
11693                  * can only really go by the tag number...
11694                  * This may cause problems with parallel SCSI.
11695                  * Need to figure that out!!
11696                  */
11697                 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11698                         xio->io_hdr.flags |= CTL_FLAG_ABORT;
11699                         found = 1;
11700                         if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11701                             !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11702                                 union ctl_ha_msg msg_info;
11703
11704                                 msg_info.hdr.nexus = io->io_hdr.nexus;
11705                                 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11706                                 msg_info.task.tag_num = io->taskio.tag_num;
11707                                 msg_info.task.tag_type = io->taskio.tag_type;
11708                                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11709                                 msg_info.hdr.original_sc = NULL;
11710                                 msg_info.hdr.serializing_sc = NULL;
11711 #if 0
11712                                 printf("Sent Abort to other side\n");
11713 #endif
11714                                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11715                                     sizeof(msg_info.task), M_NOWAIT);
11716                         }
11717 #if 0
11718                         printf("ctl_abort_task: found I/O to abort\n");
11719 #endif
11720                 }
11721         }
11722         mtx_unlock(&lun->lun_lock);
11723
11724         if (found == 0) {
11725                 /*
11726                  * This isn't really an error.  It's entirely possible for
11727                  * the abort and command completion to cross on the wire.
11728                  * This is more of an informative/diagnostic error.
11729                  */
11730 #if 0
11731                 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11732                        "%u:%u:%u tag %d type %d\n",
11733                        io->io_hdr.nexus.initid,
11734                        io->io_hdr.nexus.targ_port,
11735                        io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11736                        io->taskio.tag_type);
11737 #endif
11738         }
11739         io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11740         return (0);
11741 }
11742
11743 static int
11744 ctl_query_task(union ctl_io *io, int task_set)
11745 {
11746         union ctl_io *xio;
11747         struct ctl_lun *lun;
11748         struct ctl_softc *softc;
11749         int found = 0;
11750         uint32_t targ_lun;
11751
11752         softc = control_softc;
11753         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11754         mtx_lock(&softc->ctl_lock);
11755         if ((targ_lun >= CTL_MAX_LUNS) ||
11756             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11757                 mtx_unlock(&softc->ctl_lock);
11758                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11759                 return (1);
11760         }
11761         mtx_lock(&lun->lun_lock);
11762         mtx_unlock(&softc->ctl_lock);
11763         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11764              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11765
11766                 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11767                  || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11768                  || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11769                         continue;
11770
11771                 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
11772                         found = 1;
11773                         break;
11774                 }
11775         }
11776         mtx_unlock(&lun->lun_lock);
11777         if (found)
11778                 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11779         else
11780                 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11781         return (0);
11782 }
11783
11784 static int
11785 ctl_query_async_event(union ctl_io *io)
11786 {
11787         struct ctl_lun *lun;
11788         struct ctl_softc *softc;
11789         ctl_ua_type ua;
11790         uint32_t targ_lun, initidx;
11791
11792         softc = control_softc;
11793         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11794         mtx_lock(&softc->ctl_lock);
11795         if ((targ_lun >= CTL_MAX_LUNS) ||
11796             (lun = softc->ctl_luns[targ_lun]) == NULL) {
11797                 mtx_unlock(&softc->ctl_lock);
11798                 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11799                 return (1);
11800         }
11801         mtx_lock(&lun->lun_lock);
11802         mtx_unlock(&softc->ctl_lock);
11803         initidx = ctl_get_initindex(&io->io_hdr.nexus);
11804         ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
11805         mtx_unlock(&lun->lun_lock);
11806         if (ua != CTL_UA_NONE)
11807                 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11808         else
11809                 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11810         return (0);
11811 }
11812
11813 static void
11814 ctl_run_task(union ctl_io *io)
11815 {
11816         struct ctl_softc *softc = control_softc;
11817         int retval = 1;
11818
11819         CTL_DEBUG_PRINT(("ctl_run_task\n"));
11820         KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11821             ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
11822         io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
11823         bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
11824         switch (io->taskio.task_action) {
11825         case CTL_TASK_ABORT_TASK:
11826                 retval = ctl_abort_task(io);
11827                 break;
11828         case CTL_TASK_ABORT_TASK_SET:
11829         case CTL_TASK_CLEAR_TASK_SET:
11830                 retval = ctl_abort_task_set(io);
11831                 break;
11832         case CTL_TASK_CLEAR_ACA:
11833                 break;
11834         case CTL_TASK_I_T_NEXUS_RESET:
11835                 retval = ctl_i_t_nexus_reset(io);
11836                 break;
11837         case CTL_TASK_LUN_RESET:
11838                 retval = ctl_lun_reset(softc, io);
11839                 break;
11840         case CTL_TASK_TARGET_RESET:
11841                 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
11842                 break;
11843         case CTL_TASK_BUS_RESET:
11844                 retval = ctl_bus_reset(softc, io);
11845                 break;
11846         case CTL_TASK_PORT_LOGIN:
11847                 break;
11848         case CTL_TASK_PORT_LOGOUT:
11849                 break;
11850         case CTL_TASK_QUERY_TASK:
11851                 retval = ctl_query_task(io, 0);
11852                 break;
11853         case CTL_TASK_QUERY_TASK_SET:
11854                 retval = ctl_query_task(io, 1);
11855                 break;
11856         case CTL_TASK_QUERY_ASYNC_EVENT:
11857                 retval = ctl_query_async_event(io);
11858                 break;
11859         default:
11860                 printf("%s: got unknown task management event %d\n",
11861                        __func__, io->taskio.task_action);
11862                 break;
11863         }
11864         if (retval == 0)
11865                 io->io_hdr.status = CTL_SUCCESS;
11866         else
11867                 io->io_hdr.status = CTL_ERROR;
11868         ctl_done(io);
11869 }
11870
11871 /*
11872  * For HA operation.  Handle commands that come in from the other
11873  * controller.
11874  */
11875 static void
11876 ctl_handle_isc(union ctl_io *io)
11877 {
11878         int free_io;
11879         struct ctl_lun *lun;
11880         struct ctl_softc *softc;
11881         uint32_t targ_lun;
11882
11883         softc = control_softc;
11884
11885         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11886         lun = softc->ctl_luns[targ_lun];
11887
11888         switch (io->io_hdr.msg_type) {
11889         case CTL_MSG_SERIALIZE:
11890                 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11891                 break;
11892         case CTL_MSG_R2R: {
11893                 const struct ctl_cmd_entry *entry;
11894
11895                 /*
11896                  * This is only used in SER_ONLY mode.
11897                  */
11898                 free_io = 0;
11899                 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11900                 mtx_lock(&lun->lun_lock);
11901                 if (ctl_scsiio_lun_check(lun,
11902                     entry, (struct ctl_scsiio *)io) != 0) {
11903                         mtx_unlock(&lun->lun_lock);
11904                         ctl_done(io);
11905                         break;
11906                 }
11907                 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11908                 mtx_unlock(&lun->lun_lock);
11909                 ctl_enqueue_rtr(io);
11910                 break;
11911         }
11912         case CTL_MSG_FINISH_IO:
11913                 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11914                         free_io = 0;
11915                         ctl_done(io);
11916                 } else {
11917                         free_io = 1;
11918                         mtx_lock(&lun->lun_lock);
11919                         TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11920                                      ooa_links);
11921                         ctl_check_blocked(lun);
11922                         mtx_unlock(&lun->lun_lock);
11923                 }
11924                 break;
11925         case CTL_MSG_PERS_ACTION:
11926                 ctl_hndl_per_res_out_on_other_sc(
11927                         (union ctl_ha_msg *)&io->presio.pr_msg);
11928                 free_io = 1;
11929                 break;
11930         case CTL_MSG_BAD_JUJU:
11931                 free_io = 0;
11932                 ctl_done(io);
11933                 break;
11934         case CTL_MSG_DATAMOVE:
11935                 /* Only used in XFER mode */
11936                 free_io = 0;
11937                 ctl_datamove_remote(io);
11938                 break;
11939         case CTL_MSG_DATAMOVE_DONE:
11940                 /* Only used in XFER mode */
11941                 free_io = 0;
11942                 io->scsiio.be_move_done(io);
11943                 break;
11944         case CTL_MSG_FAILOVER:
11945                 mtx_lock(&lun->lun_lock);
11946                 ctl_failover_lun(lun);
11947                 mtx_unlock(&lun->lun_lock);
11948                 free_io = 1;
11949                 break;
11950         default:
11951                 free_io = 1;
11952                 printf("%s: Invalid message type %d\n",
11953                        __func__, io->io_hdr.msg_type);
11954                 break;
11955         }
11956         if (free_io)
11957                 ctl_free_io(io);
11958
11959 }
11960
11961
11962 /*
11963  * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11964  * there is no match.
11965  */
11966 static ctl_lun_error_pattern
11967 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11968 {
11969         const struct ctl_cmd_entry *entry;
11970         ctl_lun_error_pattern filtered_pattern, pattern;
11971
11972         pattern = desc->error_pattern;
11973
11974         /*
11975          * XXX KDM we need more data passed into this function to match a
11976          * custom pattern, and we actually need to implement custom pattern
11977          * matching.
11978          */
11979         if (pattern & CTL_LUN_PAT_CMD)
11980                 return (CTL_LUN_PAT_CMD);
11981
11982         if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11983                 return (CTL_LUN_PAT_ANY);
11984
11985         entry = ctl_get_cmd_entry(ctsio, NULL);
11986
11987         filtered_pattern = entry->pattern & pattern;
11988
11989         /*
11990          * If the user requested specific flags in the pattern (e.g.
11991          * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11992          * flags.
11993          *
11994          * If the user did not specify any flags, it doesn't matter whether
11995          * or not the command supports the flags.
11996          */
11997         if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11998              (pattern & ~CTL_LUN_PAT_MASK))
11999                 return (CTL_LUN_PAT_NONE);
12000
12001         /*
12002          * If the user asked for a range check, see if the requested LBA
12003          * range overlaps with this command's LBA range.
12004          */
12005         if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12006                 uint64_t lba1;
12007                 uint64_t len1;
12008                 ctl_action action;
12009                 int retval;
12010
12011                 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12012                 if (retval != 0)
12013                         return (CTL_LUN_PAT_NONE);
12014
12015                 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12016                                               desc->lba_range.len, FALSE);
12017                 /*
12018                  * A "pass" means that the LBA ranges don't overlap, so
12019                  * this doesn't match the user's range criteria.
12020                  */
12021                 if (action == CTL_ACTION_PASS)
12022                         return (CTL_LUN_PAT_NONE);
12023         }
12024
12025         return (filtered_pattern);
12026 }
12027
12028 static void
12029 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12030 {
12031         struct ctl_error_desc *desc, *desc2;
12032
12033         mtx_assert(&lun->lun_lock, MA_OWNED);
12034
12035         STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12036                 ctl_lun_error_pattern pattern;
12037                 /*
12038                  * Check to see whether this particular command matches
12039                  * the pattern in the descriptor.
12040                  */
12041                 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12042                 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12043                         continue;
12044
12045                 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12046                 case CTL_LUN_INJ_ABORTED:
12047                         ctl_set_aborted(&io->scsiio);
12048                         break;
12049                 case CTL_LUN_INJ_MEDIUM_ERR:
12050                         ctl_set_medium_error(&io->scsiio);
12051                         break;
12052                 case CTL_LUN_INJ_UA:
12053                         /* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET
12054                          * OCCURRED */
12055                         ctl_set_ua(&io->scsiio, 0x29, 0x00);
12056                         break;
12057                 case CTL_LUN_INJ_CUSTOM:
12058                         /*
12059                          * We're assuming the user knows what he is doing.
12060                          * Just copy the sense information without doing
12061                          * checks.
12062                          */
12063                         bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12064                               MIN(sizeof(desc->custom_sense),
12065                                   sizeof(io->scsiio.sense_data)));
12066                         io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12067                         io->scsiio.sense_len = SSD_FULL_SIZE;
12068                         io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12069                         break;
12070                 case CTL_LUN_INJ_NONE:
12071                 default:
12072                         /*
12073                          * If this is an error injection type we don't know
12074                          * about, clear the continuous flag (if it is set)
12075                          * so it will get deleted below.
12076                          */
12077                         desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12078                         break;
12079                 }
12080                 /*
12081                  * By default, each error injection action is a one-shot
12082                  */
12083                 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12084                         continue;
12085
12086                 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12087
12088                 free(desc, M_CTL);
12089         }
12090 }
12091
12092 #ifdef CTL_IO_DELAY
12093 static void
12094 ctl_datamove_timer_wakeup(void *arg)
12095 {
12096         union ctl_io *io;
12097
12098         io = (union ctl_io *)arg;
12099
12100         ctl_datamove(io);
12101 }
12102 #endif /* CTL_IO_DELAY */
12103
12104 void
12105 ctl_datamove(union ctl_io *io)
12106 {
12107         void (*fe_datamove)(union ctl_io *io);
12108
12109         mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12110
12111         CTL_DEBUG_PRINT(("ctl_datamove\n"));
12112
12113 #ifdef CTL_TIME_IO
12114         if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12115                 char str[256];
12116                 char path_str[64];
12117                 struct sbuf sb;
12118
12119                 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12120                 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12121
12122                 sbuf_cat(&sb, path_str);
12123                 switch (io->io_hdr.io_type) {
12124                 case CTL_IO_SCSI:
12125                         ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12126                         sbuf_printf(&sb, "\n");
12127                         sbuf_cat(&sb, path_str);
12128                         sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12129                                     io->scsiio.tag_num, io->scsiio.tag_type);
12130                         break;
12131                 case CTL_IO_TASK:
12132                         sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12133                                     "Tag Type: %d\n", io->taskio.task_action,
12134                                     io->taskio.tag_num, io->taskio.tag_type);
12135                         break;
12136                 default:
12137                         printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12138                         panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12139                         break;
12140                 }
12141                 sbuf_cat(&sb, path_str);
12142                 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12143                             (intmax_t)time_uptime - io->io_hdr.start_time);
12144                 sbuf_finish(&sb);
12145                 printf("%s", sbuf_data(&sb));
12146         }
12147 #endif /* CTL_TIME_IO */
12148
12149 #ifdef CTL_IO_DELAY
12150         if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12151                 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12152         } else {
12153                 struct ctl_lun *lun;
12154
12155                 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12156                 if ((lun != NULL)
12157                  && (lun->delay_info.datamove_delay > 0)) {
12158
12159                         callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12160                         io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12161                         callout_reset(&io->io_hdr.delay_callout,
12162                                       lun->delay_info.datamove_delay * hz,
12163                                       ctl_datamove_timer_wakeup, io);
12164                         if (lun->delay_info.datamove_type ==
12165                             CTL_DELAY_TYPE_ONESHOT)
12166                                 lun->delay_info.datamove_delay = 0;
12167                         return;
12168                 }
12169         }
12170 #endif
12171
12172         /*
12173          * This command has been aborted.  Set the port status, so we fail
12174          * the data move.
12175          */
12176         if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12177                 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12178                        io->scsiio.tag_num, io->io_hdr.nexus.initid,
12179                        io->io_hdr.nexus.targ_port,
12180                        io->io_hdr.nexus.targ_lun);
12181                 io->io_hdr.port_status = 31337;
12182                 /*
12183                  * Note that the backend, in this case, will get the
12184                  * callback in its context.  In other cases it may get
12185                  * called in the frontend's interrupt thread context.
12186                  */
12187                 io->scsiio.be_move_done(io);
12188                 return;
12189         }
12190
12191         /* Don't confuse frontend with zero length data move. */
12192         if (io->scsiio.kern_data_len == 0) {
12193                 io->scsiio.be_move_done(io);
12194                 return;
12195         }
12196
12197         /*
12198          * If we're in XFER mode and this I/O is from the other shelf
12199          * controller, we need to send the DMA to the other side to
12200          * actually transfer the data to/from the host.  In serialize only
12201          * mode the transfer happens below CTL and ctl_datamove() is only
12202          * called on the machine that originally received the I/O.
12203          */
12204         if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12205          && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12206                 union ctl_ha_msg msg;
12207                 uint32_t sg_entries_sent;
12208                 int do_sg_copy;
12209                 int i;
12210
12211                 memset(&msg, 0, sizeof(msg));
12212                 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12213                 msg.hdr.original_sc = io->io_hdr.original_sc;
12214                 msg.hdr.serializing_sc = io;
12215                 msg.hdr.nexus = io->io_hdr.nexus;
12216                 msg.hdr.status = io->io_hdr.status;
12217                 msg.dt.flags = io->io_hdr.flags;
12218                 /*
12219                  * We convert everything into a S/G list here.  We can't
12220                  * pass by reference, only by value between controllers.
12221                  * So we can't pass a pointer to the S/G list, only as many
12222                  * S/G entries as we can fit in here.  If it's possible for
12223                  * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12224                  * then we need to break this up into multiple transfers.
12225                  */
12226                 if (io->scsiio.kern_sg_entries == 0) {
12227                         msg.dt.kern_sg_entries = 1;
12228 #if 0
12229                         /*
12230                          * Convert to a physical address if this is a
12231                          * virtual address.
12232                          */
12233                         if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12234                                 msg.dt.sg_list[0].addr =
12235                                         io->scsiio.kern_data_ptr;
12236                         } else {
12237                                 /*
12238                                  * XXX KDM use busdma here!
12239                                  */
12240                                 msg.dt.sg_list[0].addr = (void *)
12241                                         vtophys(io->scsiio.kern_data_ptr);
12242                         }
12243 #else
12244                         KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12245                             ("HA does not support BUS_ADDR"));
12246                         msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
12247 #endif
12248
12249                         msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12250                         do_sg_copy = 0;
12251                 } else {
12252                         msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12253                         do_sg_copy = 1;
12254                 }
12255
12256                 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12257                 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12258                 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12259                 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12260                 msg.dt.sg_sequence = 0;
12261
12262                 /*
12263                  * Loop until we've sent all of the S/G entries.  On the
12264                  * other end, we'll recompose these S/G entries into one
12265                  * contiguous list before passing it to the
12266                  */
12267                 for (sg_entries_sent = 0; sg_entries_sent <
12268                      msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12269                         msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12270                                 sizeof(msg.dt.sg_list[0])),
12271                                 msg.dt.kern_sg_entries - sg_entries_sent);
12272
12273                         if (do_sg_copy != 0) {
12274                                 struct ctl_sg_entry *sgl;
12275                                 int j;
12276
12277                                 sgl = (struct ctl_sg_entry *)
12278                                         io->scsiio.kern_data_ptr;
12279                                 /*
12280                                  * If this is in cached memory, flush the cache
12281                                  * before we send the DMA request to the other
12282                                  * controller.  We want to do this in either
12283                                  * the * read or the write case.  The read
12284                                  * case is straightforward.  In the write
12285                                  * case, we want to make sure nothing is
12286                                  * in the local cache that could overwrite
12287                                  * the DMAed data.
12288                                  */
12289
12290                                 for (i = sg_entries_sent, j = 0;
12291                                      i < msg.dt.cur_sg_entries; i++, j++) {
12292 #if 0
12293                                         if ((io->io_hdr.flags &
12294                                              CTL_FLAG_BUS_ADDR) == 0) {
12295                                                 /*
12296                                                  * XXX KDM use busdma.
12297                                                  */
12298                                                 msg.dt.sg_list[j].addr =(void *)
12299                                                        vtophys(sgl[i].addr);
12300                                         } else {
12301                                                 msg.dt.sg_list[j].addr =
12302                                                         sgl[i].addr;
12303                                         }
12304 #else
12305                                         KASSERT((io->io_hdr.flags &
12306                                             CTL_FLAG_BUS_ADDR) == 0,
12307                                             ("HA does not support BUS_ADDR"));
12308                                         msg.dt.sg_list[j].addr = sgl[i].addr;
12309 #endif
12310                                         msg.dt.sg_list[j].len = sgl[i].len;
12311                                 }
12312                         }
12313
12314                         sg_entries_sent += msg.dt.cur_sg_entries;
12315                         if (sg_entries_sent >= msg.dt.kern_sg_entries)
12316                                 msg.dt.sg_last = 1;
12317                         else
12318                                 msg.dt.sg_last = 0;
12319
12320                         if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12321                             sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
12322                             sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries,
12323                             M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
12324                                 io->io_hdr.port_status = 31341;
12325                                 io->scsiio.be_move_done(io);
12326                                 return;
12327                         }
12328
12329                         msg.dt.sent_sg_entries = sg_entries_sent;
12330                 }
12331                 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12332         } else {
12333
12334                 /*
12335                  * Lookup the fe_datamove() function for this particular
12336                  * front end.
12337                  */
12338                 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12339
12340                 fe_datamove(io);
12341         }
12342 }
12343
12344 static void
12345 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12346 {
12347         union ctl_ha_msg msg;
12348
12349         memset(&msg, 0, sizeof(msg));
12350
12351         msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12352         msg.hdr.original_sc = io;
12353         msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12354         msg.hdr.nexus = io->io_hdr.nexus;
12355         msg.hdr.status = io->io_hdr.status;
12356         msg.scsi.tag_num = io->scsiio.tag_num;
12357         msg.scsi.tag_type = io->scsiio.tag_type;
12358         msg.scsi.scsi_status = io->scsiio.scsi_status;
12359         memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12360                io->scsiio.sense_len);
12361         msg.scsi.sense_len = io->scsiio.sense_len;
12362         msg.scsi.sense_residual = io->scsiio.sense_residual;
12363         msg.scsi.fetd_status = io->io_hdr.port_status;
12364         msg.scsi.residual = io->scsiio.residual;
12365         io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12366
12367         if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12368                 ctl_failover_io(io, /*have_lock*/ have_lock);
12369                 return;
12370         }
12371
12372         ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12373             sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12374             msg.scsi.sense_len, M_WAITOK);
12375 }
12376
12377 /*
12378  * The DMA to the remote side is done, now we need to tell the other side
12379  * we're done so it can continue with its data movement.
12380  */
12381 static void
12382 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12383 {
12384         union ctl_io *io;
12385         int i;
12386
12387         io = rq->context;
12388
12389         if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12390                 printf("%s: ISC DMA write failed with error %d", __func__,
12391                        rq->ret);
12392                 ctl_set_internal_failure(&io->scsiio,
12393                                          /*sks_valid*/ 1,
12394                                          /*retry_count*/ rq->ret);
12395         }
12396
12397         ctl_dt_req_free(rq);
12398
12399         for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12400                 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12401         free(io->io_hdr.remote_sglist, M_CTL);
12402         io->io_hdr.remote_sglist = NULL;
12403         io->io_hdr.local_sglist = NULL;
12404
12405         /*
12406          * The data is in local and remote memory, so now we need to send
12407          * status (good or back) back to the other side.
12408          */
12409         ctl_send_datamove_done(io, /*have_lock*/ 0);
12410 }
12411
12412 /*
12413  * We've moved the data from the host/controller into local memory.  Now we
12414  * need to push it over to the remote controller's memory.
12415  */
12416 static int
12417 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12418 {
12419         int retval;
12420
12421         retval = 0;
12422
12423         retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12424                                           ctl_datamove_remote_write_cb);
12425
12426         return (retval);
12427 }
12428
12429 static void
12430 ctl_datamove_remote_write(union ctl_io *io)
12431 {
12432         int retval;
12433         void (*fe_datamove)(union ctl_io *io);
12434
12435         /*
12436          * - Get the data from the host/HBA into local memory.
12437          * - DMA memory from the local controller to the remote controller.
12438          * - Send status back to the remote controller.
12439          */
12440
12441         retval = ctl_datamove_remote_sgl_setup(io);
12442         if (retval != 0)
12443                 return;
12444
12445         /* Switch the pointer over so the FETD knows what to do */
12446         io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12447
12448         /*
12449          * Use a custom move done callback, since we need to send completion
12450          * back to the other controller, not to the backend on this side.
12451          */
12452         io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12453
12454         fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12455
12456         fe_datamove(io);
12457
12458         return;
12459
12460 }
12461
12462 static int
12463 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12464 {
12465 #if 0
12466         char str[256];
12467         char path_str[64];
12468         struct sbuf sb;
12469 #endif
12470         int i;
12471
12472         for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12473                 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12474         free(io->io_hdr.remote_sglist, M_CTL);
12475         io->io_hdr.remote_sglist = NULL;
12476         io->io_hdr.local_sglist = NULL;
12477
12478 #if 0
12479         scsi_path_string(io, path_str, sizeof(path_str));
12480         sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12481         sbuf_cat(&sb, path_str);
12482         scsi_command_string(&io->scsiio, NULL, &sb);
12483         sbuf_printf(&sb, "\n");
12484         sbuf_cat(&sb, path_str);
12485         sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12486                     io->scsiio.tag_num, io->scsiio.tag_type);
12487         sbuf_cat(&sb, path_str);
12488         sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12489                     io->io_hdr.flags, io->io_hdr.status);
12490         sbuf_finish(&sb);
12491         printk("%s", sbuf_data(&sb));
12492 #endif
12493
12494
12495         /*
12496          * The read is done, now we need to send status (good or bad) back
12497          * to the other side.
12498          */
12499         ctl_send_datamove_done(io, /*have_lock*/ 0);
12500
12501         return (0);
12502 }
12503
12504 static void
12505 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12506 {
12507         union ctl_io *io;
12508         void (*fe_datamove)(union ctl_io *io);
12509
12510         io = rq->context;
12511
12512         if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12513                 printf("%s: ISC DMA read failed with error %d\n", __func__,
12514                        rq->ret);
12515                 ctl_set_internal_failure(&io->scsiio,
12516                                          /*sks_valid*/ 1,
12517                                          /*retry_count*/ rq->ret);
12518         }
12519
12520         ctl_dt_req_free(rq);
12521
12522         /* Switch the pointer over so the FETD knows what to do */
12523         io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12524
12525         /*
12526          * Use a custom move done callback, since we need to send completion
12527          * back to the other controller, not to the backend on this side.
12528          */
12529         io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12530
12531         /* XXX KDM add checks like the ones in ctl_datamove? */
12532
12533         fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12534
12535         fe_datamove(io);
12536 }
12537
12538 static int
12539 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12540 {
12541         struct ctl_sg_entry *local_sglist, *remote_sglist;
12542         struct ctl_softc *softc;
12543         uint32_t len_to_go;
12544         int retval;
12545         int i;
12546
12547         retval = 0;
12548         softc = control_softc;
12549         local_sglist = io->io_hdr.local_sglist;
12550         remote_sglist = io->io_hdr.remote_sglist;
12551         len_to_go = io->scsiio.kern_data_len;
12552
12553         /*
12554          * The difficult thing here is that the size of the various
12555          * S/G segments may be different than the size from the
12556          * remote controller.  That'll make it harder when DMAing
12557          * the data back to the other side.
12558          */
12559         for (i = 0; len_to_go > 0; i++) {
12560                 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12561                 local_sglist[i].addr =
12562                     malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12563
12564                 len_to_go -= local_sglist[i].len;
12565         }
12566         /*
12567          * Reset the number of S/G entries accordingly.  The original
12568          * number of S/G entries is available in rem_sg_entries.
12569          */
12570         io->scsiio.kern_sg_entries = i;
12571
12572 #if 0
12573         printf("%s: kern_sg_entries = %d\n", __func__,
12574                io->scsiio.kern_sg_entries);
12575         for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12576                 printf("%s: sg[%d] = %p, %d\n", __func__, i,
12577                        local_sglist[i].addr, local_sglist[i].len);
12578 #endif
12579
12580         return (retval);
12581 }
12582
12583 static int
12584 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12585                          ctl_ha_dt_cb callback)
12586 {
12587         struct ctl_ha_dt_req *rq;
12588         struct ctl_sg_entry *remote_sglist, *local_sglist;
12589         uint32_t local_used, remote_used, total_used;
12590         int i, j, isc_ret;
12591
12592         rq = ctl_dt_req_alloc();
12593
12594         /*
12595          * If we failed to allocate the request, and if the DMA didn't fail
12596          * anyway, set busy status.  This is just a resource allocation
12597          * failure.
12598          */
12599         if ((rq == NULL)
12600          && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12601              (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12602                 ctl_set_busy(&io->scsiio);
12603
12604         if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12605             (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12606
12607                 if (rq != NULL)
12608                         ctl_dt_req_free(rq);
12609
12610                 /*
12611                  * The data move failed.  We need to return status back
12612                  * to the other controller.  No point in trying to DMA
12613                  * data to the remote controller.
12614                  */
12615
12616                 ctl_send_datamove_done(io, /*have_lock*/ 0);
12617
12618                 return (1);
12619         }
12620
12621         local_sglist = io->io_hdr.local_sglist;
12622         remote_sglist = io->io_hdr.remote_sglist;
12623         local_used = 0;
12624         remote_used = 0;
12625         total_used = 0;
12626
12627         /*
12628          * Pull/push the data over the wire from/to the other controller.
12629          * This takes into account the possibility that the local and
12630          * remote sglists may not be identical in terms of the size of
12631          * the elements and the number of elements.
12632          *
12633          * One fundamental assumption here is that the length allocated for
12634          * both the local and remote sglists is identical.  Otherwise, we've
12635          * essentially got a coding error of some sort.
12636          */
12637         isc_ret = CTL_HA_STATUS_SUCCESS;
12638         for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12639                 uint32_t cur_len;
12640                 uint8_t *tmp_ptr;
12641
12642                 rq->command = command;
12643                 rq->context = io;
12644
12645                 /*
12646                  * Both pointers should be aligned.  But it is possible
12647                  * that the allocation length is not.  They should both
12648                  * also have enough slack left over at the end, though,
12649                  * to round up to the next 8 byte boundary.
12650                  */
12651                 cur_len = MIN(local_sglist[i].len - local_used,
12652                               remote_sglist[j].len - remote_used);
12653                 rq->size = cur_len;
12654
12655                 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12656                 tmp_ptr += local_used;
12657
12658 #if 0
12659                 /* Use physical addresses when talking to ISC hardware */
12660                 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12661                         /* XXX KDM use busdma */
12662                         rq->local = vtophys(tmp_ptr);
12663                 } else
12664                         rq->local = tmp_ptr;
12665 #else
12666                 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12667                     ("HA does not support BUS_ADDR"));
12668                 rq->local = tmp_ptr;
12669 #endif
12670
12671                 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12672                 tmp_ptr += remote_used;
12673                 rq->remote = tmp_ptr;
12674
12675                 rq->callback = NULL;
12676
12677                 local_used += cur_len;
12678                 if (local_used >= local_sglist[i].len) {
12679                         i++;
12680                         local_used = 0;
12681                 }
12682
12683                 remote_used += cur_len;
12684                 if (remote_used >= remote_sglist[j].len) {
12685                         j++;
12686                         remote_used = 0;
12687                 }
12688                 total_used += cur_len;
12689
12690                 if (total_used >= io->scsiio.kern_data_len)
12691                         rq->callback = callback;
12692
12693 #if 0
12694                 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12695                        (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12696                        rq->local, rq->remote, rq->size);
12697 #endif
12698
12699                 isc_ret = ctl_dt_single(rq);
12700                 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12701                         break;
12702         }
12703         if (isc_ret != CTL_HA_STATUS_WAIT) {
12704                 rq->ret = isc_ret;
12705                 callback(rq);
12706         }
12707
12708         return (0);
12709 }
12710
12711 static void
12712 ctl_datamove_remote_read(union ctl_io *io)
12713 {
12714         int retval;
12715         int i;
12716
12717         /*
12718          * This will send an error to the other controller in the case of a
12719          * failure.
12720          */
12721         retval = ctl_datamove_remote_sgl_setup(io);
12722         if (retval != 0)
12723                 return;
12724
12725         retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12726                                           ctl_datamove_remote_read_cb);
12727         if (retval != 0) {
12728                 /*
12729                  * Make sure we free memory if there was an error..  The
12730                  * ctl_datamove_remote_xfer() function will send the
12731                  * datamove done message, or call the callback with an
12732                  * error if there is a problem.
12733                  */
12734                 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12735                         free(io->io_hdr.local_sglist[i].addr, M_CTL);
12736                 free(io->io_hdr.remote_sglist, M_CTL);
12737                 io->io_hdr.remote_sglist = NULL;
12738                 io->io_hdr.local_sglist = NULL;
12739         }
12740
12741         return;
12742 }
12743
12744 /*
12745  * Process a datamove request from the other controller.  This is used for
12746  * XFER mode only, not SER_ONLY mode.  For writes, we DMA into local memory
12747  * first.  Once that is complete, the data gets DMAed into the remote
12748  * controller's memory.  For reads, we DMA from the remote controller's
12749  * memory into our memory first, and then move it out to the FETD.
12750  */
12751 static void
12752 ctl_datamove_remote(union ctl_io *io)
12753 {
12754
12755         mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12756
12757         if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12758                 ctl_failover_io(io, /*have_lock*/ 0);
12759                 return;
12760         }
12761
12762         /*
12763          * Note that we look for an aborted I/O here, but don't do some of
12764          * the other checks that ctl_datamove() normally does.
12765          * We don't need to run the datamove delay code, since that should
12766          * have been done if need be on the other controller.
12767          */
12768         if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12769                 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12770                        io->scsiio.tag_num, io->io_hdr.nexus.initid,
12771                        io->io_hdr.nexus.targ_port,
12772                        io->io_hdr.nexus.targ_lun);
12773                 io->io_hdr.port_status = 31338;
12774                 ctl_send_datamove_done(io, /*have_lock*/ 0);
12775                 return;
12776         }
12777
12778         if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12779                 ctl_datamove_remote_write(io);
12780         else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12781                 ctl_datamove_remote_read(io);
12782         else {
12783                 io->io_hdr.port_status = 31339;
12784                 ctl_send_datamove_done(io, /*have_lock*/ 0);
12785         }
12786 }
12787
12788 static int
12789 ctl_process_done(union ctl_io *io)
12790 {
12791         struct ctl_lun *lun;
12792         struct ctl_softc *softc = control_softc;
12793         void (*fe_done)(union ctl_io *io);
12794         union ctl_ha_msg msg;
12795         uint32_t targ_port = io->io_hdr.nexus.targ_port;
12796
12797         CTL_DEBUG_PRINT(("ctl_process_done\n"));
12798
12799         if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0)
12800                 fe_done = softc->ctl_ports[targ_port]->fe_done;
12801         else
12802                 fe_done = NULL;
12803
12804 #ifdef CTL_TIME_IO
12805         if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12806                 char str[256];
12807                 char path_str[64];
12808                 struct sbuf sb;
12809
12810                 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12811                 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12812
12813                 sbuf_cat(&sb, path_str);
12814                 switch (io->io_hdr.io_type) {
12815                 case CTL_IO_SCSI:
12816                         ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12817                         sbuf_printf(&sb, "\n");
12818                         sbuf_cat(&sb, path_str);
12819                         sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12820                                     io->scsiio.tag_num, io->scsiio.tag_type);
12821                         break;
12822                 case CTL_IO_TASK:
12823                         sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12824                                     "Tag Type: %d\n", io->taskio.task_action,
12825                                     io->taskio.tag_num, io->taskio.tag_type);
12826                         break;
12827                 default:
12828                         printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12829                         panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12830                         break;
12831                 }
12832                 sbuf_cat(&sb, path_str);
12833                 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12834                             (intmax_t)time_uptime - io->io_hdr.start_time);
12835                 sbuf_finish(&sb);
12836                 printf("%s", sbuf_data(&sb));
12837         }
12838 #endif /* CTL_TIME_IO */
12839
12840         switch (io->io_hdr.io_type) {
12841         case CTL_IO_SCSI:
12842                 break;
12843         case CTL_IO_TASK:
12844                 if (ctl_debug & CTL_DEBUG_INFO)
12845                         ctl_io_error_print(io, NULL);
12846                 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12847                         ctl_free_io(io);
12848                 else
12849                         fe_done(io);
12850                 return (CTL_RETVAL_COMPLETE);
12851         default:
12852                 panic("ctl_process_done: invalid io type %d\n",
12853                       io->io_hdr.io_type);
12854                 break; /* NOTREACHED */
12855         }
12856
12857         lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12858         if (lun == NULL) {
12859                 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12860                                  io->io_hdr.nexus.targ_mapped_lun));
12861                 goto bailout;
12862         }
12863
12864         mtx_lock(&lun->lun_lock);
12865
12866         /*
12867          * Check to see if we have any errors to inject here.  We only
12868          * inject errors for commands that don't already have errors set.
12869          */
12870         if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
12871             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12872             ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12873                 ctl_inject_error(lun, io);
12874
12875         /*
12876          * XXX KDM how do we treat commands that aren't completed
12877          * successfully?
12878          *
12879          * XXX KDM should we also track I/O latency?
12880          */
12881         if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12882             io->io_hdr.io_type == CTL_IO_SCSI) {
12883 #ifdef CTL_TIME_IO
12884                 struct bintime cur_bt;
12885 #endif
12886                 int type;
12887
12888                 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12889                     CTL_FLAG_DATA_IN)
12890                         type = CTL_STATS_READ;
12891                 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12892                     CTL_FLAG_DATA_OUT)
12893                         type = CTL_STATS_WRITE;
12894                 else
12895                         type = CTL_STATS_NO_IO;
12896
12897                 lun->stats.ports[targ_port].bytes[type] +=
12898                     io->scsiio.kern_total_len;
12899                 lun->stats.ports[targ_port].operations[type]++;
12900 #ifdef CTL_TIME_IO
12901                 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
12902                    &io->io_hdr.dma_bt);
12903                 lun->stats.ports[targ_port].num_dmas[type] +=
12904                     io->io_hdr.num_dmas;
12905                 getbintime(&cur_bt);
12906                 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12907                 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
12908 #endif
12909         }
12910
12911         /*
12912          * Remove this from the OOA queue.
12913          */
12914         TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12915 #ifdef CTL_TIME_IO
12916         if (TAILQ_EMPTY(&lun->ooa_queue))
12917                 lun->last_busy = getsbinuptime();
12918 #endif
12919
12920         /*
12921          * Run through the blocked queue on this LUN and see if anything
12922          * has become unblocked, now that this transaction is done.
12923          */
12924         ctl_check_blocked(lun);
12925
12926         /*
12927          * If the LUN has been invalidated, free it if there is nothing
12928          * left on its OOA queue.
12929          */
12930         if ((lun->flags & CTL_LUN_INVALID)
12931          && TAILQ_EMPTY(&lun->ooa_queue)) {
12932                 mtx_unlock(&lun->lun_lock);
12933                 mtx_lock(&softc->ctl_lock);
12934                 ctl_free_lun(lun);
12935                 mtx_unlock(&softc->ctl_lock);
12936         } else
12937                 mtx_unlock(&lun->lun_lock);
12938
12939 bailout:
12940
12941         /*
12942          * If this command has been aborted, make sure we set the status
12943          * properly.  The FETD is responsible for freeing the I/O and doing
12944          * whatever it needs to do to clean up its state.
12945          */
12946         if (io->io_hdr.flags & CTL_FLAG_ABORT)
12947                 ctl_set_task_aborted(&io->scsiio);
12948
12949         /*
12950          * If enabled, print command error status.
12951          */
12952         if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
12953             (ctl_debug & CTL_DEBUG_INFO) != 0)
12954                 ctl_io_error_print(io, NULL);
12955
12956         /*
12957          * Tell the FETD or the other shelf controller we're done with this
12958          * command.  Note that only SCSI commands get to this point.  Task
12959          * management commands are completed above.
12960          */
12961         if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
12962             (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
12963                 memset(&msg, 0, sizeof(msg));
12964                 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12965                 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12966                 msg.hdr.nexus = io->io_hdr.nexus;
12967                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12968                     sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
12969                     M_WAITOK);
12970         }
12971         if ((softc->ha_mode == CTL_HA_MODE_XFER)
12972          && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12973                 memset(&msg, 0, sizeof(msg));
12974                 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12975                 msg.hdr.original_sc = io->io_hdr.original_sc;
12976                 msg.hdr.nexus = io->io_hdr.nexus;
12977                 msg.hdr.status = io->io_hdr.status;
12978                 msg.scsi.scsi_status = io->scsiio.scsi_status;
12979                 msg.scsi.tag_num = io->scsiio.tag_num;
12980                 msg.scsi.tag_type = io->scsiio.tag_type;
12981                 msg.scsi.sense_len = io->scsiio.sense_len;
12982                 msg.scsi.sense_residual = io->scsiio.sense_residual;
12983                 msg.scsi.residual = io->scsiio.residual;
12984                 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12985                        io->scsiio.sense_len);
12986
12987                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12988                     sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12989                     msg.scsi.sense_len, M_WAITOK);
12990                 ctl_free_io(io);
12991         } else 
12992                 fe_done(io);
12993
12994         return (CTL_RETVAL_COMPLETE);
12995 }
12996
12997 #ifdef CTL_WITH_CA
12998 /*
12999  * Front end should call this if it doesn't do autosense.  When the request
13000  * sense comes back in from the initiator, we'll dequeue this and send it.
13001  */
13002 int
13003 ctl_queue_sense(union ctl_io *io)
13004 {
13005         struct ctl_lun *lun;
13006         struct ctl_port *port;
13007         struct ctl_softc *softc;
13008         uint32_t initidx, targ_lun;
13009
13010         softc = control_softc;
13011
13012         CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13013
13014         /*
13015          * LUN lookup will likely move to the ctl_work_thread() once we
13016          * have our new queueing infrastructure (that doesn't put things on
13017          * a per-LUN queue initially).  That is so that we can handle
13018          * things like an INQUIRY to a LUN that we don't have enabled.  We
13019          * can't deal with that right now.
13020          */
13021         mtx_lock(&softc->ctl_lock);
13022
13023         /*
13024          * If we don't have a LUN for this, just toss the sense
13025          * information.
13026          */
13027         port = ctl_io_port(&ctsio->io_hdr);
13028         targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13029         if ((targ_lun < CTL_MAX_LUNS)
13030          && (softc->ctl_luns[targ_lun] != NULL))
13031                 lun = softc->ctl_luns[targ_lun];
13032         else
13033                 goto bailout;
13034
13035         initidx = ctl_get_initindex(&io->io_hdr.nexus);
13036
13037         mtx_lock(&lun->lun_lock);
13038         /*
13039          * Already have CA set for this LUN...toss the sense information.
13040          */
13041         if (ctl_is_set(lun->have_ca, initidx)) {
13042                 mtx_unlock(&lun->lun_lock);
13043                 goto bailout;
13044         }
13045
13046         memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13047                MIN(sizeof(lun->pending_sense[initidx]),
13048                sizeof(io->scsiio.sense_data)));
13049         ctl_set_mask(lun->have_ca, initidx);
13050         mtx_unlock(&lun->lun_lock);
13051
13052 bailout:
13053         mtx_unlock(&softc->ctl_lock);
13054
13055         ctl_free_io(io);
13056
13057         return (CTL_RETVAL_COMPLETE);
13058 }
13059 #endif
13060
13061 /*
13062  * Primary command inlet from frontend ports.  All SCSI and task I/O
13063  * requests must go through this function.
13064  */
13065 int
13066 ctl_queue(union ctl_io *io)
13067 {
13068         struct ctl_port *port;
13069
13070         CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13071
13072 #ifdef CTL_TIME_IO
13073         io->io_hdr.start_time = time_uptime;
13074         getbintime(&io->io_hdr.start_bt);
13075 #endif /* CTL_TIME_IO */
13076
13077         /* Map FE-specific LUN ID into global one. */
13078         port = ctl_io_port(&io->io_hdr);
13079         io->io_hdr.nexus.targ_mapped_lun =
13080             ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13081
13082         switch (io->io_hdr.io_type) {
13083         case CTL_IO_SCSI:
13084         case CTL_IO_TASK:
13085                 if (ctl_debug & CTL_DEBUG_CDB)
13086                         ctl_io_print(io);
13087                 ctl_enqueue_incoming(io);
13088                 break;
13089         default:
13090                 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13091                 return (EINVAL);
13092         }
13093
13094         return (CTL_RETVAL_COMPLETE);
13095 }
13096
13097 #ifdef CTL_IO_DELAY
13098 static void
13099 ctl_done_timer_wakeup(void *arg)
13100 {
13101         union ctl_io *io;
13102
13103         io = (union ctl_io *)arg;
13104         ctl_done(io);
13105 }
13106 #endif /* CTL_IO_DELAY */
13107
13108 void
13109 ctl_done(union ctl_io *io)
13110 {
13111
13112         /*
13113          * Enable this to catch duplicate completion issues.
13114          */
13115 #if 0
13116         if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13117                 printf("%s: type %d msg %d cdb %x iptl: "
13118                        "%u:%u:%u tag 0x%04x "
13119                        "flag %#x status %x\n",
13120                         __func__,
13121                         io->io_hdr.io_type,
13122                         io->io_hdr.msg_type,
13123                         io->scsiio.cdb[0],
13124                         io->io_hdr.nexus.initid,
13125                         io->io_hdr.nexus.targ_port,
13126                         io->io_hdr.nexus.targ_lun,
13127                         (io->io_hdr.io_type ==
13128                         CTL_IO_TASK) ?
13129                         io->taskio.tag_num :
13130                         io->scsiio.tag_num,
13131                         io->io_hdr.flags,
13132                         io->io_hdr.status);
13133         } else
13134                 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13135 #endif
13136
13137         /*
13138          * This is an internal copy of an I/O, and should not go through
13139          * the normal done processing logic.
13140          */
13141         if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13142                 return;
13143
13144 #ifdef CTL_IO_DELAY
13145         if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13146                 struct ctl_lun *lun;
13147
13148                 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13149
13150                 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13151         } else {
13152                 struct ctl_lun *lun;
13153
13154                 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13155
13156                 if ((lun != NULL)
13157                  && (lun->delay_info.done_delay > 0)) {
13158
13159                         callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13160                         io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13161                         callout_reset(&io->io_hdr.delay_callout,
13162                                       lun->delay_info.done_delay * hz,
13163                                       ctl_done_timer_wakeup, io);
13164                         if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13165                                 lun->delay_info.done_delay = 0;
13166                         return;
13167                 }
13168         }
13169 #endif /* CTL_IO_DELAY */
13170
13171         ctl_enqueue_done(io);
13172 }
13173
13174 static void
13175 ctl_work_thread(void *arg)
13176 {
13177         struct ctl_thread *thr = (struct ctl_thread *)arg;
13178         struct ctl_softc *softc = thr->ctl_softc;
13179         union ctl_io *io;
13180         int retval;
13181
13182         CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13183
13184         for (;;) {
13185                 retval = 0;
13186
13187                 /*
13188                  * We handle the queues in this order:
13189                  * - ISC
13190                  * - done queue (to free up resources, unblock other commands)
13191                  * - RtR queue
13192                  * - incoming queue
13193                  *
13194                  * If those queues are empty, we break out of the loop and
13195                  * go to sleep.
13196                  */
13197                 mtx_lock(&thr->queue_lock);
13198                 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13199                 if (io != NULL) {
13200                         STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13201                         mtx_unlock(&thr->queue_lock);
13202                         ctl_handle_isc(io);
13203                         continue;
13204                 }
13205                 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13206                 if (io != NULL) {
13207                         STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13208                         /* clear any blocked commands, call fe_done */
13209                         mtx_unlock(&thr->queue_lock);
13210                         retval = ctl_process_done(io);
13211                         continue;
13212                 }
13213                 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13214                 if (io != NULL) {
13215                         STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13216                         mtx_unlock(&thr->queue_lock);
13217                         if (io->io_hdr.io_type == CTL_IO_TASK)
13218                                 ctl_run_task(io);
13219                         else
13220                                 ctl_scsiio_precheck(softc, &io->scsiio);
13221                         continue;
13222                 }
13223                 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13224                 if (io != NULL) {
13225                         STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13226                         mtx_unlock(&thr->queue_lock);
13227                         retval = ctl_scsiio(&io->scsiio);
13228                         if (retval != CTL_RETVAL_COMPLETE)
13229                                 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13230                         continue;
13231                 }
13232
13233                 /* Sleep until we have something to do. */
13234                 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13235         }
13236 }
13237
13238 static void
13239 ctl_lun_thread(void *arg)
13240 {
13241         struct ctl_softc *softc = (struct ctl_softc *)arg;
13242         struct ctl_be_lun *be_lun;
13243         int retval;
13244
13245         CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13246
13247         for (;;) {
13248                 retval = 0;
13249                 mtx_lock(&softc->ctl_lock);
13250                 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13251                 if (be_lun != NULL) {
13252                         STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13253                         mtx_unlock(&softc->ctl_lock);
13254                         ctl_create_lun(be_lun);
13255                         continue;
13256                 }
13257
13258                 /* Sleep until we have something to do. */
13259                 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13260                     PDROP | PRIBIO, "-", 0);
13261         }
13262 }
13263
13264 static void
13265 ctl_thresh_thread(void *arg)
13266 {
13267         struct ctl_softc *softc = (struct ctl_softc *)arg;
13268         struct ctl_lun *lun;
13269         struct ctl_be_lun *be_lun;
13270         struct scsi_da_rw_recovery_page *rwpage;
13271         struct ctl_logical_block_provisioning_page *page;
13272         const char *attr;
13273         union ctl_ha_msg msg;
13274         uint64_t thres, val;
13275         int i, e, set;
13276
13277         CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13278
13279         for (;;) {
13280                 mtx_lock(&softc->ctl_lock);
13281                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13282                         be_lun = lun->be_lun;
13283                         if ((lun->flags & CTL_LUN_DISABLED) ||
13284                             (lun->flags & CTL_LUN_OFFLINE) ||
13285                             lun->backend->lun_attr == NULL)
13286                                 continue;
13287                         if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13288                             softc->ha_mode == CTL_HA_MODE_XFER)
13289                                 continue;
13290                         rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13291                         if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13292                                 continue;
13293                         e = 0;
13294                         page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13295                         for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13296                                 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13297                                         continue;
13298                                 thres = scsi_4btoul(page->descr[i].count);
13299                                 thres <<= CTL_LBP_EXPONENT;
13300                                 switch (page->descr[i].resource) {
13301                                 case 0x01:
13302                                         attr = "blocksavail";
13303                                         break;
13304                                 case 0x02:
13305                                         attr = "blocksused";
13306                                         break;
13307                                 case 0xf1:
13308                                         attr = "poolblocksavail";
13309                                         break;
13310                                 case 0xf2:
13311                                         attr = "poolblocksused";
13312                                         break;
13313                                 default:
13314                                         continue;
13315                                 }
13316                                 mtx_unlock(&softc->ctl_lock); // XXX
13317                                 val = lun->backend->lun_attr(
13318                                     lun->be_lun->be_lun, attr);
13319                                 mtx_lock(&softc->ctl_lock);
13320                                 if (val == UINT64_MAX)
13321                                         continue;
13322                                 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13323                                     == SLBPPD_ARMING_INC)
13324                                         e |= (val >= thres);
13325                                 else
13326                                         e |= (val <= thres);
13327                         }
13328                         mtx_lock(&lun->lun_lock);
13329                         if (e) {
13330                                 if (lun->lasttpt == 0 ||
13331                                     time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13332                                         lun->lasttpt = time_uptime;
13333                                         ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13334                                         set = 1;
13335                                 } else
13336                                         set = 0;
13337                         } else {
13338                                 lun->lasttpt = 0;
13339                                 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13340                                 set = -1;
13341                         }
13342                         mtx_unlock(&lun->lun_lock);
13343                         if (set != 0 &&
13344                             lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13345                                 /* Send msg to other side. */
13346                                 bzero(&msg.ua, sizeof(msg.ua));
13347                                 msg.hdr.msg_type = CTL_MSG_UA;
13348                                 msg.hdr.nexus.initid = -1;
13349                                 msg.hdr.nexus.targ_port = -1;
13350                                 msg.hdr.nexus.targ_lun = lun->lun;
13351                                 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13352                                 msg.ua.ua_all = 1;
13353                                 msg.ua.ua_set = (set > 0);
13354                                 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13355                                 mtx_unlock(&softc->ctl_lock); // XXX
13356                                 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13357                                     sizeof(msg.ua), M_WAITOK);
13358                                 mtx_lock(&softc->ctl_lock);
13359                         }
13360                 }
13361                 mtx_unlock(&softc->ctl_lock);
13362                 pause("-", CTL_LBP_PERIOD * hz);
13363         }
13364 }
13365
13366 static void
13367 ctl_enqueue_incoming(union ctl_io *io)
13368 {
13369         struct ctl_softc *softc = control_softc;
13370         struct ctl_thread *thr;
13371         u_int idx;
13372
13373         idx = (io->io_hdr.nexus.targ_port * 127 +
13374                io->io_hdr.nexus.initid) % worker_threads;
13375         thr = &softc->threads[idx];
13376         mtx_lock(&thr->queue_lock);
13377         STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13378         mtx_unlock(&thr->queue_lock);
13379         wakeup(thr);
13380 }
13381
13382 static void
13383 ctl_enqueue_rtr(union ctl_io *io)
13384 {
13385         struct ctl_softc *softc = control_softc;
13386         struct ctl_thread *thr;
13387
13388         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13389         mtx_lock(&thr->queue_lock);
13390         STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13391         mtx_unlock(&thr->queue_lock);
13392         wakeup(thr);
13393 }
13394
13395 static void
13396 ctl_enqueue_done(union ctl_io *io)
13397 {
13398         struct ctl_softc *softc = control_softc;
13399         struct ctl_thread *thr;
13400
13401         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13402         mtx_lock(&thr->queue_lock);
13403         STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13404         mtx_unlock(&thr->queue_lock);
13405         wakeup(thr);
13406 }
13407
13408 static void
13409 ctl_enqueue_isc(union ctl_io *io)
13410 {
13411         struct ctl_softc *softc = control_softc;
13412         struct ctl_thread *thr;
13413
13414         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13415         mtx_lock(&thr->queue_lock);
13416         STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13417         mtx_unlock(&thr->queue_lock);
13418         wakeup(thr);
13419 }
13420
13421 /*
13422  *  vim: ts=8
13423  */