]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/ctl/ctl.c
Add support for REPORT TIMESTAMP command.
[FreeBSD/FreeBSD.git] / sys / cam / ctl / ctl.c
1 /*-
2  * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3  * Copyright (c) 2012 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Edward Tomasz Napierala
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16  *    substantially similar to the "NO WARRANTY" disclaimer below
17  *    ("Disclaimer") and any redistribution must be conditioned upon
18  *    including a substantially similar Disclaimer requirement for further
19  *    binary redistribution.
20  *
21  * NO WARRANTY
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGES.
33  *
34  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
35  */
36 /*
37  * CAM Target Layer, a SCSI device emulation subsystem.
38  *
39  * Author: Ken Merry <ken@FreeBSD.org>
40  */
41
42 #define _CTL_C
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
52 #include <sys/bio.h>
53 #include <sys/fcntl.h>
54 #include <sys/lock.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
59 #include <sys/conf.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
62 #include <sys/sbuf.h>
63 #include <sys/smp.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
66
67 #include <cam/cam.h>
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_da.h>
70 #include <cam/ctl/ctl_io.h>
71 #include <cam/ctl/ctl.h>
72 #include <cam/ctl/ctl_frontend.h>
73 #include <cam/ctl/ctl_frontend_internal.h>
74 #include <cam/ctl/ctl_util.h>
75 #include <cam/ctl/ctl_backend.h>
76 #include <cam/ctl/ctl_ioctl.h>
77 #include <cam/ctl/ctl_ha.h>
78 #include <cam/ctl/ctl_private.h>
79 #include <cam/ctl/ctl_debug.h>
80 #include <cam/ctl/ctl_scsi_all.h>
81 #include <cam/ctl/ctl_error.h>
82
83 struct ctl_softc *control_softc = NULL;
84
85 /*
86  * Size and alignment macros needed for Copan-specific HA hardware.  These
87  * can go away when the HA code is re-written, and uses busdma for any
88  * hardware.
89  */
90 #define CTL_ALIGN_8B(target, source, type)                              \
91         if (((uint32_t)source & 0x7) != 0)                              \
92                 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
93         else                                                            \
94                 target = (type)source;
95
96 #define CTL_SIZE_8B(target, size)                                       \
97         if ((size & 0x7) != 0)                                          \
98                 target = size + (0x8 - (size & 0x7));                   \
99         else                                                            \
100                 target = size;
101
102 #define CTL_ALIGN_8B_MARGIN     16
103
104 /*
105  * Template mode pages.
106  */
107
108 /*
109  * Note that these are default values only.  The actual values will be
110  * filled in when the user does a mode sense.
111  */
112 static struct copan_power_subpage power_page_default = {
113         /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
114         /*subpage*/ PWR_SUBPAGE_CODE,
115         /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
116                          (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
117         /*page_version*/ PWR_VERSION,
118         /* total_luns */ 26,
119         /* max_active_luns*/ PWR_DFLT_MAX_LUNS,
120         /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
121                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122                       0, 0, 0, 0, 0, 0}
123 };
124
125 static struct copan_power_subpage power_page_changeable = {
126         /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
127         /*subpage*/ PWR_SUBPAGE_CODE,
128         /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
129                          (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
130         /*page_version*/ 0,
131         /* total_luns */ 0,
132         /* max_active_luns*/ 0,
133         /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
134                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
135                       0, 0, 0, 0, 0, 0}
136 };
137
138 static struct copan_aps_subpage aps_page_default = {
139         APS_PAGE_CODE | SMPH_SPF, //page_code
140         APS_SUBPAGE_CODE, //subpage
141         {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
142          (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
143         APS_VERSION, //page_version
144         0, //lock_active
145         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147         0, 0, 0, 0, 0} //reserved
148 };
149
150 static struct copan_aps_subpage aps_page_changeable = {
151         APS_PAGE_CODE | SMPH_SPF, //page_code
152         APS_SUBPAGE_CODE, //subpage
153         {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
154          (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
155         0, //page_version
156         0, //lock_active
157         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159         0, 0, 0, 0, 0} //reserved
160 };
161
162 static struct copan_debugconf_subpage debugconf_page_default = {
163         DBGCNF_PAGE_CODE | SMPH_SPF,    /* page_code */
164         DBGCNF_SUBPAGE_CODE,            /* subpage */
165         {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
166          (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
167         DBGCNF_VERSION,                 /* page_version */
168         {CTL_TIME_IO_DEFAULT_SECS>>8,
169          CTL_TIME_IO_DEFAULT_SECS>>0},  /* ctl_time_io_secs */
170 };
171
172 static struct copan_debugconf_subpage debugconf_page_changeable = {
173         DBGCNF_PAGE_CODE | SMPH_SPF,    /* page_code */
174         DBGCNF_SUBPAGE_CODE,            /* subpage */
175         {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
176          (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
177         0,                              /* page_version */
178         {0xff,0xff},                    /* ctl_time_io_secs */
179 };
180
181 static struct scsi_format_page format_page_default = {
182         /*page_code*/SMS_FORMAT_DEVICE_PAGE,
183         /*page_length*/sizeof(struct scsi_format_page) - 2,
184         /*tracks_per_zone*/ {0, 0},
185         /*alt_sectors_per_zone*/ {0, 0},
186         /*alt_tracks_per_zone*/ {0, 0},
187         /*alt_tracks_per_lun*/ {0, 0},
188         /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
189                                 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
190         /*bytes_per_sector*/ {0, 0},
191         /*interleave*/ {0, 0},
192         /*track_skew*/ {0, 0},
193         /*cylinder_skew*/ {0, 0},
194         /*flags*/ SFP_HSEC,
195         /*reserved*/ {0, 0, 0}
196 };
197
198 static struct scsi_format_page format_page_changeable = {
199         /*page_code*/SMS_FORMAT_DEVICE_PAGE,
200         /*page_length*/sizeof(struct scsi_format_page) - 2,
201         /*tracks_per_zone*/ {0, 0},
202         /*alt_sectors_per_zone*/ {0, 0},
203         /*alt_tracks_per_zone*/ {0, 0},
204         /*alt_tracks_per_lun*/ {0, 0},
205         /*sectors_per_track*/ {0, 0},
206         /*bytes_per_sector*/ {0, 0},
207         /*interleave*/ {0, 0},
208         /*track_skew*/ {0, 0},
209         /*cylinder_skew*/ {0, 0},
210         /*flags*/ 0,
211         /*reserved*/ {0, 0, 0}
212 };
213
214 static struct scsi_rigid_disk_page rigid_disk_page_default = {
215         /*page_code*/SMS_RIGID_DISK_PAGE,
216         /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
217         /*cylinders*/ {0, 0, 0},
218         /*heads*/ CTL_DEFAULT_HEADS,
219         /*start_write_precomp*/ {0, 0, 0},
220         /*start_reduced_current*/ {0, 0, 0},
221         /*step_rate*/ {0, 0},
222         /*landing_zone_cylinder*/ {0, 0, 0},
223         /*rpl*/ SRDP_RPL_DISABLED,
224         /*rotational_offset*/ 0,
225         /*reserved1*/ 0,
226         /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
227                            CTL_DEFAULT_ROTATION_RATE & 0xff},
228         /*reserved2*/ {0, 0}
229 };
230
231 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
232         /*page_code*/SMS_RIGID_DISK_PAGE,
233         /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
234         /*cylinders*/ {0, 0, 0},
235         /*heads*/ 0,
236         /*start_write_precomp*/ {0, 0, 0},
237         /*start_reduced_current*/ {0, 0, 0},
238         /*step_rate*/ {0, 0},
239         /*landing_zone_cylinder*/ {0, 0, 0},
240         /*rpl*/ 0,
241         /*rotational_offset*/ 0,
242         /*reserved1*/ 0,
243         /*rotation_rate*/ {0, 0},
244         /*reserved2*/ {0, 0}
245 };
246
247 static struct scsi_caching_page caching_page_default = {
248         /*page_code*/SMS_CACHING_PAGE,
249         /*page_length*/sizeof(struct scsi_caching_page) - 2,
250         /*flags1*/ SCP_DISC | SCP_WCE,
251         /*ret_priority*/ 0,
252         /*disable_pf_transfer_len*/ {0xff, 0xff},
253         /*min_prefetch*/ {0, 0},
254         /*max_prefetch*/ {0xff, 0xff},
255         /*max_pf_ceiling*/ {0xff, 0xff},
256         /*flags2*/ 0,
257         /*cache_segments*/ 0,
258         /*cache_seg_size*/ {0, 0},
259         /*reserved*/ 0,
260         /*non_cache_seg_size*/ {0, 0, 0}
261 };
262
263 static struct scsi_caching_page caching_page_changeable = {
264         /*page_code*/SMS_CACHING_PAGE,
265         /*page_length*/sizeof(struct scsi_caching_page) - 2,
266         /*flags1*/ 0,
267         /*ret_priority*/ 0,
268         /*disable_pf_transfer_len*/ {0, 0},
269         /*min_prefetch*/ {0, 0},
270         /*max_prefetch*/ {0, 0},
271         /*max_pf_ceiling*/ {0, 0},
272         /*flags2*/ 0,
273         /*cache_segments*/ 0,
274         /*cache_seg_size*/ {0, 0},
275         /*reserved*/ 0,
276         /*non_cache_seg_size*/ {0, 0, 0}
277 };
278
279 static struct scsi_control_page control_page_default = {
280         /*page_code*/SMS_CONTROL_MODE_PAGE,
281         /*page_length*/sizeof(struct scsi_control_page) - 2,
282         /*rlec*/0,
283         /*queue_flags*/0,
284         /*eca_and_aen*/0,
285         /*reserved*/0,
286         /*aen_holdoff_period*/{0, 0}
287 };
288
289 static struct scsi_control_page control_page_changeable = {
290         /*page_code*/SMS_CONTROL_MODE_PAGE,
291         /*page_length*/sizeof(struct scsi_control_page) - 2,
292         /*rlec*/SCP_DSENSE,
293         /*queue_flags*/0,
294         /*eca_and_aen*/0,
295         /*reserved*/0,
296         /*aen_holdoff_period*/{0, 0}
297 };
298
299
300 /*
301  * XXX KDM move these into the softc.
302  */
303 static int rcv_sync_msg;
304 static int persis_offset;
305 static uint8_t ctl_pause_rtr;
306 static int     ctl_is_single = 1;
307 static int     index_to_aps_page;
308
309 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
310 static int worker_threads = -1;
311 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
312     &worker_threads, 1, "Number of worker threads");
313 static int verbose = 0;
314 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
315     &verbose, 0, "Show SCSI errors returned to initiator");
316
317 /*
318  * Serial number (0x80), device id (0x83), supported pages (0x00),
319  * Block limits (0xB0) and Logical Block Provisioning (0xB2)
320  */
321 #define SCSI_EVPD_NUM_SUPPORTED_PAGES   5
322
323 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
324                                   int param);
325 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
326 static int ctl_init(void);
327 void ctl_shutdown(void);
328 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
329 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
330 static void ctl_ioctl_online(void *arg);
331 static void ctl_ioctl_offline(void *arg);
332 static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
333 static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
334 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
335 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
336 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
337 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
338 static int ctl_ioctl_submit_wait(union ctl_io *io);
339 static void ctl_ioctl_datamove(union ctl_io *io);
340 static void ctl_ioctl_done(union ctl_io *io);
341 static void ctl_ioctl_hard_startstop_callback(void *arg,
342                                               struct cfi_metatask *metatask);
343 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
344 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
345                               struct ctl_ooa *ooa_hdr,
346                               struct ctl_ooa_entry *kern_entries);
347 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
348                      struct thread *td);
349 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
350 uint32_t ctl_port_idx(int port_num);
351 #ifdef unused
352 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
353                                    uint32_t targ_target, uint32_t targ_lun,
354                                    int can_wait);
355 static void ctl_kfree_io(union ctl_io *io);
356 #endif /* unused */
357 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
358                          struct ctl_be_lun *be_lun, struct ctl_id target_id);
359 static int ctl_free_lun(struct ctl_lun *lun);
360 static void ctl_create_lun(struct ctl_be_lun *be_lun);
361 /**
362 static void ctl_failover_change_pages(struct ctl_softc *softc,
363                                       struct ctl_scsiio *ctsio, int master);
364 **/
365
366 static int ctl_do_mode_select(union ctl_io *io);
367 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
368                            uint64_t res_key, uint64_t sa_res_key,
369                            uint8_t type, uint32_t residx,
370                            struct ctl_scsiio *ctsio,
371                            struct scsi_per_res_out *cdb,
372                            struct scsi_per_res_out_parms* param);
373 static void ctl_pro_preempt_other(struct ctl_lun *lun,
374                                   union ctl_ha_msg *msg);
375 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
376 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
377 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
378 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
379 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
380                                          int alloc_len);
381 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
382 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
383 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
384 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
385 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
386 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
387                                          union ctl_io *ooa_io);
388 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
389                                 union ctl_io *starting_io);
390 static int ctl_check_blocked(struct ctl_lun *lun);
391 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
392                                 struct ctl_lun *lun,
393                                 const struct ctl_cmd_entry *entry,
394                                 struct ctl_scsiio *ctsio);
395 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
396 static void ctl_failover(void);
397 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
398                                struct ctl_scsiio *ctsio);
399 static int ctl_scsiio(struct ctl_scsiio *ctsio);
400
401 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
402 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
403                             ctl_ua_type ua_type);
404 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
405                          ctl_ua_type ua_type);
406 static int ctl_abort_task(union ctl_io *io);
407 static void ctl_run_task(union ctl_io *io);
408 #ifdef CTL_IO_DELAY
409 static void ctl_datamove_timer_wakeup(void *arg);
410 static void ctl_done_timer_wakeup(void *arg);
411 #endif /* CTL_IO_DELAY */
412
413 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
414 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
415 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
416 static void ctl_datamove_remote_write(union ctl_io *io);
417 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
418 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
419 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
420 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
421                                     ctl_ha_dt_cb callback);
422 static void ctl_datamove_remote_read(union ctl_io *io);
423 static void ctl_datamove_remote(union ctl_io *io);
424 static int ctl_process_done(union ctl_io *io);
425 static void ctl_lun_thread(void *arg);
426 static void ctl_work_thread(void *arg);
427 static void ctl_enqueue_incoming(union ctl_io *io);
428 static void ctl_enqueue_rtr(union ctl_io *io);
429 static void ctl_enqueue_done(union ctl_io *io);
430 static void ctl_enqueue_isc(union ctl_io *io);
431 static const struct ctl_cmd_entry *
432     ctl_get_cmd_entry(struct ctl_scsiio *ctsio);
433 static const struct ctl_cmd_entry *
434     ctl_validate_command(struct ctl_scsiio *ctsio);
435 static int ctl_cmd_applicable(uint8_t lun_type,
436     const struct ctl_cmd_entry *entry);
437
438 /*
439  * Load the serialization table.  This isn't very pretty, but is probably
440  * the easiest way to do it.
441  */
442 #include "ctl_ser_table.c"
443
444 /*
445  * We only need to define open, close and ioctl routines for this driver.
446  */
447 static struct cdevsw ctl_cdevsw = {
448         .d_version =    D_VERSION,
449         .d_flags =      0,
450         .d_open =       ctl_open,
451         .d_close =      ctl_close,
452         .d_ioctl =      ctl_ioctl,
453         .d_name =       "ctl",
454 };
455
456
457 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
458
459 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
460
461 static moduledata_t ctl_moduledata = {
462         "ctl",
463         ctl_module_event_handler,
464         NULL
465 };
466
467 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
468 MODULE_VERSION(ctl, 1);
469
470 static void
471 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
472                             union ctl_ha_msg *msg_info)
473 {
474         struct ctl_scsiio *ctsio;
475
476         if (msg_info->hdr.original_sc == NULL) {
477                 printf("%s: original_sc == NULL!\n", __func__);
478                 /* XXX KDM now what? */
479                 return;
480         }
481
482         ctsio = &msg_info->hdr.original_sc->scsiio;
483         ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
484         ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
485         ctsio->io_hdr.status = msg_info->hdr.status;
486         ctsio->scsi_status = msg_info->scsi.scsi_status;
487         ctsio->sense_len = msg_info->scsi.sense_len;
488         ctsio->sense_residual = msg_info->scsi.sense_residual;
489         ctsio->residual = msg_info->scsi.residual;
490         memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
491                sizeof(ctsio->sense_data));
492         memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
493                &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
494         ctl_enqueue_isc((union ctl_io *)ctsio);
495 }
496
497 static void
498 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
499                                 union ctl_ha_msg *msg_info)
500 {
501         struct ctl_scsiio *ctsio;
502
503         if (msg_info->hdr.serializing_sc == NULL) {
504                 printf("%s: serializing_sc == NULL!\n", __func__);
505                 /* XXX KDM now what? */
506                 return;
507         }
508
509         ctsio = &msg_info->hdr.serializing_sc->scsiio;
510 #if 0
511         /*
512          * Attempt to catch the situation where an I/O has
513          * been freed, and we're using it again.
514          */
515         if (ctsio->io_hdr.io_type == 0xff) {
516                 union ctl_io *tmp_io;
517                 tmp_io = (union ctl_io *)ctsio;
518                 printf("%s: %p use after free!\n", __func__,
519                        ctsio);
520                 printf("%s: type %d msg %d cdb %x iptl: "
521                        "%d:%d:%d:%d tag 0x%04x "
522                        "flag %#x status %x\n",
523                         __func__,
524                         tmp_io->io_hdr.io_type,
525                         tmp_io->io_hdr.msg_type,
526                         tmp_io->scsiio.cdb[0],
527                         tmp_io->io_hdr.nexus.initid.id,
528                         tmp_io->io_hdr.nexus.targ_port,
529                         tmp_io->io_hdr.nexus.targ_target.id,
530                         tmp_io->io_hdr.nexus.targ_lun,
531                         (tmp_io->io_hdr.io_type ==
532                         CTL_IO_TASK) ?
533                         tmp_io->taskio.tag_num :
534                         tmp_io->scsiio.tag_num,
535                         tmp_io->io_hdr.flags,
536                         tmp_io->io_hdr.status);
537         }
538 #endif
539         ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
540         ctl_enqueue_isc((union ctl_io *)ctsio);
541 }
542
543 /*
544  * ISC (Inter Shelf Communication) event handler.  Events from the HA
545  * subsystem come in here.
546  */
547 static void
548 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
549 {
550         struct ctl_softc *ctl_softc;
551         union ctl_io *io;
552         struct ctl_prio *presio;
553         ctl_ha_status isc_status;
554
555         ctl_softc = control_softc;
556         io = NULL;
557
558
559 #if 0
560         printf("CTL: Isc Msg event %d\n", event);
561 #endif
562         if (event == CTL_HA_EVT_MSG_RECV) {
563                 union ctl_ha_msg msg_info;
564
565                 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
566                                              sizeof(msg_info), /*wait*/ 0);
567 #if 0
568                 printf("CTL: msg_type %d\n", msg_info.msg_type);
569 #endif
570                 if (isc_status != 0) {
571                         printf("Error receiving message, status = %d\n",
572                                isc_status);
573                         return;
574                 }
575
576                 switch (msg_info.hdr.msg_type) {
577                 case CTL_MSG_SERIALIZE:
578 #if 0
579                         printf("Serialize\n");
580 #endif
581                         io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
582                         if (io == NULL) {
583                                 printf("ctl_isc_event_handler: can't allocate "
584                                        "ctl_io!\n");
585                                 /* Bad Juju */
586                                 /* Need to set busy and send msg back */
587                                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
588                                 msg_info.hdr.status = CTL_SCSI_ERROR;
589                                 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
590                                 msg_info.scsi.sense_len = 0;
591                                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
592                                     sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
593                                 }
594                                 goto bailout;
595                         }
596                         ctl_zero_io(io);
597                         // populate ctsio from msg_info
598                         io->io_hdr.io_type = CTL_IO_SCSI;
599                         io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
600                         io->io_hdr.original_sc = msg_info.hdr.original_sc;
601 #if 0
602                         printf("pOrig %x\n", (int)msg_info.original_sc);
603 #endif
604                         io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
605                                             CTL_FLAG_IO_ACTIVE;
606                         /*
607                          * If we're in serialization-only mode, we don't
608                          * want to go through full done processing.  Thus
609                          * the COPY flag.
610                          *
611                          * XXX KDM add another flag that is more specific.
612                          */
613                         if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
614                                 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
615                         io->io_hdr.nexus = msg_info.hdr.nexus;
616 #if 0
617                         printf("targ %d, port %d, iid %d, lun %d\n",
618                                io->io_hdr.nexus.targ_target.id,
619                                io->io_hdr.nexus.targ_port,
620                                io->io_hdr.nexus.initid.id,
621                                io->io_hdr.nexus.targ_lun);
622 #endif
623                         io->scsiio.tag_num = msg_info.scsi.tag_num;
624                         io->scsiio.tag_type = msg_info.scsi.tag_type;
625                         memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
626                                CTL_MAX_CDBLEN);
627                         if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
628                                 const struct ctl_cmd_entry *entry;
629
630                                 entry = ctl_get_cmd_entry(&io->scsiio);
631                                 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
632                                 io->io_hdr.flags |=
633                                         entry->flags & CTL_FLAG_DATA_MASK;
634                         }
635                         ctl_enqueue_isc(io);
636                         break;
637
638                 /* Performed on the Originating SC, XFER mode only */
639                 case CTL_MSG_DATAMOVE: {
640                         struct ctl_sg_entry *sgl;
641                         int i, j;
642
643                         io = msg_info.hdr.original_sc;
644                         if (io == NULL) {
645                                 printf("%s: original_sc == NULL!\n", __func__);
646                                 /* XXX KDM do something here */
647                                 break;
648                         }
649                         io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
650                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
651                         /*
652                          * Keep track of this, we need to send it back over
653                          * when the datamove is complete.
654                          */
655                         io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
656
657                         if (msg_info.dt.sg_sequence == 0) {
658                                 /*
659                                  * XXX KDM we use the preallocated S/G list
660                                  * here, but we'll need to change this to
661                                  * dynamic allocation if we need larger S/G
662                                  * lists.
663                                  */
664                                 if (msg_info.dt.kern_sg_entries >
665                                     sizeof(io->io_hdr.remote_sglist) /
666                                     sizeof(io->io_hdr.remote_sglist[0])) {
667                                         printf("%s: number of S/G entries "
668                                             "needed %u > allocated num %zd\n",
669                                             __func__,
670                                             msg_info.dt.kern_sg_entries,
671                                             sizeof(io->io_hdr.remote_sglist)/
672                                             sizeof(io->io_hdr.remote_sglist[0]));
673                                 
674                                         /*
675                                          * XXX KDM send a message back to
676                                          * the other side to shut down the
677                                          * DMA.  The error will come back
678                                          * through via the normal channel.
679                                          */
680                                         break;
681                                 }
682                                 sgl = io->io_hdr.remote_sglist;
683                                 memset(sgl, 0,
684                                        sizeof(io->io_hdr.remote_sglist));
685
686                                 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
687
688                                 io->scsiio.kern_sg_entries =
689                                         msg_info.dt.kern_sg_entries;
690                                 io->scsiio.rem_sg_entries =
691                                         msg_info.dt.kern_sg_entries;
692                                 io->scsiio.kern_data_len =
693                                         msg_info.dt.kern_data_len;
694                                 io->scsiio.kern_total_len =
695                                         msg_info.dt.kern_total_len;
696                                 io->scsiio.kern_data_resid =
697                                         msg_info.dt.kern_data_resid;
698                                 io->scsiio.kern_rel_offset =
699                                         msg_info.dt.kern_rel_offset;
700                                 /*
701                                  * Clear out per-DMA flags.
702                                  */
703                                 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
704                                 /*
705                                  * Add per-DMA flags that are set for this
706                                  * particular DMA request.
707                                  */
708                                 io->io_hdr.flags |= msg_info.dt.flags &
709                                                     CTL_FLAG_RDMA_MASK;
710                         } else
711                                 sgl = (struct ctl_sg_entry *)
712                                         io->scsiio.kern_data_ptr;
713
714                         for (i = msg_info.dt.sent_sg_entries, j = 0;
715                              i < (msg_info.dt.sent_sg_entries +
716                              msg_info.dt.cur_sg_entries); i++, j++) {
717                                 sgl[i].addr = msg_info.dt.sg_list[j].addr;
718                                 sgl[i].len = msg_info.dt.sg_list[j].len;
719
720 #if 0
721                                 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
722                                        __func__,
723                                        msg_info.dt.sg_list[j].addr,
724                                        msg_info.dt.sg_list[j].len,
725                                        sgl[i].addr, sgl[i].len, j, i);
726 #endif
727                         }
728 #if 0
729                         memcpy(&sgl[msg_info.dt.sent_sg_entries],
730                                msg_info.dt.sg_list,
731                                sizeof(*sgl) * msg_info.dt.cur_sg_entries);
732 #endif
733
734                         /*
735                          * If this is the last piece of the I/O, we've got
736                          * the full S/G list.  Queue processing in the thread.
737                          * Otherwise wait for the next piece.
738                          */
739                         if (msg_info.dt.sg_last != 0)
740                                 ctl_enqueue_isc(io);
741                         break;
742                 }
743                 /* Performed on the Serializing (primary) SC, XFER mode only */
744                 case CTL_MSG_DATAMOVE_DONE: {
745                         if (msg_info.hdr.serializing_sc == NULL) {
746                                 printf("%s: serializing_sc == NULL!\n",
747                                        __func__);
748                                 /* XXX KDM now what? */
749                                 break;
750                         }
751                         /*
752                          * We grab the sense information here in case
753                          * there was a failure, so we can return status
754                          * back to the initiator.
755                          */
756                         io = msg_info.hdr.serializing_sc;
757                         io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
758                         io->io_hdr.status = msg_info.hdr.status;
759                         io->scsiio.scsi_status = msg_info.scsi.scsi_status;
760                         io->scsiio.sense_len = msg_info.scsi.sense_len;
761                         io->scsiio.sense_residual =msg_info.scsi.sense_residual;
762                         io->io_hdr.port_status = msg_info.scsi.fetd_status;
763                         io->scsiio.residual = msg_info.scsi.residual;
764                         memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
765                                sizeof(io->scsiio.sense_data));
766                         ctl_enqueue_isc(io);
767                         break;
768                 }
769
770                 /* Preformed on Originating SC, SER_ONLY mode */
771                 case CTL_MSG_R2R:
772                         io = msg_info.hdr.original_sc;
773                         if (io == NULL) {
774                                 printf("%s: Major Bummer\n", __func__);
775                                 return;
776                         } else {
777 #if 0
778                                 printf("pOrig %x\n",(int) ctsio);
779 #endif
780                         }
781                         io->io_hdr.msg_type = CTL_MSG_R2R;
782                         io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
783                         ctl_enqueue_isc(io);
784                         break;
785
786                 /*
787                  * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
788                  * mode.
789                  * Performed on the Originating (i.e. secondary) SC in XFER
790                  * mode
791                  */
792                 case CTL_MSG_FINISH_IO:
793                         if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
794                                 ctl_isc_handler_finish_xfer(ctl_softc,
795                                                             &msg_info);
796                         else
797                                 ctl_isc_handler_finish_ser_only(ctl_softc,
798                                                                 &msg_info);
799                         break;
800
801                 /* Preformed on Originating SC */
802                 case CTL_MSG_BAD_JUJU:
803                         io = msg_info.hdr.original_sc;
804                         if (io == NULL) {
805                                 printf("%s: Bad JUJU!, original_sc is NULL!\n",
806                                        __func__);
807                                 break;
808                         }
809                         ctl_copy_sense_data(&msg_info, io);
810                         /*
811                          * IO should have already been cleaned up on other
812                          * SC so clear this flag so we won't send a message
813                          * back to finish the IO there.
814                          */
815                         io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
816                         io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
817
818                         /* io = msg_info.hdr.serializing_sc; */
819                         io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
820                         ctl_enqueue_isc(io);
821                         break;
822
823                 /* Handle resets sent from the other side */
824                 case CTL_MSG_MANAGE_TASKS: {
825                         struct ctl_taskio *taskio;
826                         taskio = (struct ctl_taskio *)ctl_alloc_io(
827                                 (void *)ctl_softc->othersc_pool);
828                         if (taskio == NULL) {
829                                 printf("ctl_isc_event_handler: can't allocate "
830                                        "ctl_io!\n");
831                                 /* Bad Juju */
832                                 /* should I just call the proper reset func
833                                    here??? */
834                                 goto bailout;
835                         }
836                         ctl_zero_io((union ctl_io *)taskio);
837                         taskio->io_hdr.io_type = CTL_IO_TASK;
838                         taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
839                         taskio->io_hdr.nexus = msg_info.hdr.nexus;
840                         taskio->task_action = msg_info.task.task_action;
841                         taskio->tag_num = msg_info.task.tag_num;
842                         taskio->tag_type = msg_info.task.tag_type;
843 #ifdef CTL_TIME_IO
844                         taskio->io_hdr.start_time = time_uptime;
845                         getbintime(&taskio->io_hdr.start_bt);
846 #if 0
847                         cs_prof_gettime(&taskio->io_hdr.start_ticks);
848 #endif
849 #endif /* CTL_TIME_IO */
850                         ctl_run_task((union ctl_io *)taskio);
851                         break;
852                 }
853                 /* Persistent Reserve action which needs attention */
854                 case CTL_MSG_PERS_ACTION:
855                         presio = (struct ctl_prio *)ctl_alloc_io(
856                                 (void *)ctl_softc->othersc_pool);
857                         if (presio == NULL) {
858                                 printf("ctl_isc_event_handler: can't allocate "
859                                        "ctl_io!\n");
860                                 /* Bad Juju */
861                                 /* Need to set busy and send msg back */
862                                 goto bailout;
863                         }
864                         ctl_zero_io((union ctl_io *)presio);
865                         presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
866                         presio->pr_msg = msg_info.pr;
867                         ctl_enqueue_isc((union ctl_io *)presio);
868                         break;
869                 case CTL_MSG_SYNC_FE:
870                         rcv_sync_msg = 1;
871                         break;
872                 case CTL_MSG_APS_LOCK: {
873                         // It's quicker to execute this then to
874                         // queue it.
875                         struct ctl_lun *lun;
876                         struct ctl_page_index *page_index;
877                         struct copan_aps_subpage *current_sp;
878                         uint32_t targ_lun;
879
880                         targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
881                         lun = ctl_softc->ctl_luns[targ_lun];
882                         mtx_lock(&lun->lun_lock);
883                         page_index = &lun->mode_pages.index[index_to_aps_page];
884                         current_sp = (struct copan_aps_subpage *)
885                                      (page_index->page_data +
886                                      (page_index->page_len * CTL_PAGE_CURRENT));
887
888                         current_sp->lock_active = msg_info.aps.lock_flag;
889                         mtx_unlock(&lun->lun_lock);
890                         break;
891                 }
892                 default:
893                         printf("How did I get here?\n");
894                 }
895         } else if (event == CTL_HA_EVT_MSG_SENT) {
896                 if (param != CTL_HA_STATUS_SUCCESS) {
897                         printf("Bad status from ctl_ha_msg_send status %d\n",
898                                param);
899                 }
900                 return;
901         } else if (event == CTL_HA_EVT_DISCONNECT) {
902                 printf("CTL: Got a disconnect from Isc\n");
903                 return;
904         } else {
905                 printf("ctl_isc_event_handler: Unknown event %d\n", event);
906                 return;
907         }
908
909 bailout:
910         return;
911 }
912
913 static void
914 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
915 {
916         struct scsi_sense_data *sense;
917
918         sense = &dest->scsiio.sense_data;
919         bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
920         dest->scsiio.scsi_status = src->scsi.scsi_status;
921         dest->scsiio.sense_len = src->scsi.sense_len;
922         dest->io_hdr.status = src->hdr.status;
923 }
924
925 static int
926 ctl_init(void)
927 {
928         struct ctl_softc *softc;
929         struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
930         struct ctl_frontend *fe;
931         uint8_t sc_id =0;
932         int i, error, retval;
933         //int isc_retval;
934
935         retval = 0;
936         ctl_pause_rtr = 0;
937         rcv_sync_msg = 0;
938
939         control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
940                                M_WAITOK | M_ZERO);
941         softc = control_softc;
942
943         softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
944                               "cam/ctl");
945
946         softc->dev->si_drv1 = softc;
947
948         /*
949          * By default, return a "bad LUN" peripheral qualifier for unknown
950          * LUNs.  The user can override this default using the tunable or
951          * sysctl.  See the comment in ctl_inquiry_std() for more details.
952          */
953         softc->inquiry_pq_no_lun = 1;
954         TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
955                           &softc->inquiry_pq_no_lun);
956         sysctl_ctx_init(&softc->sysctl_ctx);
957         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
958                 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
959                 CTLFLAG_RD, 0, "CAM Target Layer");
960
961         if (softc->sysctl_tree == NULL) {
962                 printf("%s: unable to allocate sysctl tree\n", __func__);
963                 destroy_dev(softc->dev);
964                 free(control_softc, M_DEVBUF);
965                 control_softc = NULL;
966                 return (ENOMEM);
967         }
968
969         SYSCTL_ADD_INT(&softc->sysctl_ctx,
970                        SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
971                        "inquiry_pq_no_lun", CTLFLAG_RW,
972                        &softc->inquiry_pq_no_lun, 0,
973                        "Report no lun possible for invalid LUNs");
974
975         mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
976         mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
977         softc->open_count = 0;
978
979         /*
980          * Default to actually sending a SYNCHRONIZE CACHE command down to
981          * the drive.
982          */
983         softc->flags = CTL_FLAG_REAL_SYNC;
984
985         /*
986          * In Copan's HA scheme, the "master" and "slave" roles are
987          * figured out through the slot the controller is in.  Although it
988          * is an active/active system, someone has to be in charge.
989          */
990 #ifdef NEEDTOPORT
991         scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
992 #endif
993
994         if (sc_id == 0) {
995                 softc->flags |= CTL_FLAG_MASTER_SHELF;
996                 persis_offset = 0;
997         } else
998                 persis_offset = CTL_MAX_INITIATORS;
999
1000         /*
1001          * XXX KDM need to figure out where we want to get our target ID
1002          * and WWID.  Is it different on each port?
1003          */
1004         softc->target.id = 0;
1005         softc->target.wwid[0] = 0x12345678;
1006         softc->target.wwid[1] = 0x87654321;
1007         STAILQ_INIT(&softc->lun_list);
1008         STAILQ_INIT(&softc->pending_lun_queue);
1009         STAILQ_INIT(&softc->fe_list);
1010         STAILQ_INIT(&softc->be_list);
1011         STAILQ_INIT(&softc->io_pools);
1012
1013         if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1014                             &internal_pool)!= 0){
1015                 printf("ctl: can't allocate %d entry internal pool, "
1016                        "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1017                 return (ENOMEM);
1018         }
1019
1020         if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1021                             CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1022                 printf("ctl: can't allocate %d entry emergency pool, "
1023                        "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1024                 ctl_pool_free(internal_pool);
1025                 return (ENOMEM);
1026         }
1027
1028         if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1029                             &other_pool) != 0)
1030         {
1031                 printf("ctl: can't allocate %d entry other SC pool, "
1032                        "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1033                 ctl_pool_free(internal_pool);
1034                 ctl_pool_free(emergency_pool);
1035                 return (ENOMEM);
1036         }
1037
1038         softc->internal_pool = internal_pool;
1039         softc->emergency_pool = emergency_pool;
1040         softc->othersc_pool = other_pool;
1041
1042         if (worker_threads <= 0)
1043                 worker_threads = max(1, mp_ncpus / 4);
1044         if (worker_threads > CTL_MAX_THREADS)
1045                 worker_threads = CTL_MAX_THREADS;
1046
1047         for (i = 0; i < worker_threads; i++) {
1048                 struct ctl_thread *thr = &softc->threads[i];
1049
1050                 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1051                 thr->ctl_softc = softc;
1052                 STAILQ_INIT(&thr->incoming_queue);
1053                 STAILQ_INIT(&thr->rtr_queue);
1054                 STAILQ_INIT(&thr->done_queue);
1055                 STAILQ_INIT(&thr->isc_queue);
1056
1057                 error = kproc_kthread_add(ctl_work_thread, thr,
1058                     &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1059                 if (error != 0) {
1060                         printf("error creating CTL work thread!\n");
1061                         ctl_pool_free(internal_pool);
1062                         ctl_pool_free(emergency_pool);
1063                         ctl_pool_free(other_pool);
1064                         return (error);
1065                 }
1066         }
1067         error = kproc_kthread_add(ctl_lun_thread, softc,
1068             &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1069         if (error != 0) {
1070                 printf("error creating CTL lun thread!\n");
1071                 ctl_pool_free(internal_pool);
1072                 ctl_pool_free(emergency_pool);
1073                 ctl_pool_free(other_pool);
1074                 return (error);
1075         }
1076         if (bootverbose)
1077                 printf("ctl: CAM Target Layer loaded\n");
1078
1079         /*
1080          * Initialize the initiator and portname mappings
1081          */
1082         memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1083
1084         /*
1085          * Initialize the ioctl front end.
1086          */
1087         fe = &softc->ioctl_info.fe;
1088         sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1089         fe->port_type = CTL_PORT_IOCTL;
1090         fe->num_requested_ctl_io = 100;
1091         fe->port_name = softc->ioctl_info.port_name;
1092         fe->port_online = ctl_ioctl_online;
1093         fe->port_offline = ctl_ioctl_offline;
1094         fe->onoff_arg = &softc->ioctl_info;
1095         fe->targ_enable = ctl_ioctl_targ_enable;
1096         fe->targ_disable = ctl_ioctl_targ_disable;
1097         fe->lun_enable = ctl_ioctl_lun_enable;
1098         fe->lun_disable = ctl_ioctl_lun_disable;
1099         fe->targ_lun_arg = &softc->ioctl_info;
1100         fe->fe_datamove = ctl_ioctl_datamove;
1101         fe->fe_done = ctl_ioctl_done;
1102         fe->max_targets = 15;
1103         fe->max_target_id = 15;
1104
1105         if (ctl_frontend_register(&softc->ioctl_info.fe,
1106                           (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1107                 printf("ctl: ioctl front end registration failed, will "
1108                        "continue anyway\n");
1109         }
1110
1111 #ifdef CTL_IO_DELAY
1112         if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1113                 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1114                        sizeof(struct callout), CTL_TIMER_BYTES);
1115                 return (EINVAL);
1116         }
1117 #endif /* CTL_IO_DELAY */
1118
1119         return (0);
1120 }
1121
1122 void
1123 ctl_shutdown(void)
1124 {
1125         struct ctl_softc *softc;
1126         struct ctl_lun *lun, *next_lun;
1127         struct ctl_io_pool *pool;
1128
1129         softc = (struct ctl_softc *)control_softc;
1130
1131         if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1132                 printf("ctl: ioctl front end deregistration failed\n");
1133
1134         mtx_lock(&softc->ctl_lock);
1135
1136         /*
1137          * Free up each LUN.
1138          */
1139         for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1140                 next_lun = STAILQ_NEXT(lun, links);
1141                 ctl_free_lun(lun);
1142         }
1143
1144         mtx_unlock(&softc->ctl_lock);
1145
1146         /*
1147          * This will rip the rug out from under any FETDs or anyone else
1148          * that has a pool allocated.  Since we increment our module
1149          * refcount any time someone outside the main CTL module allocates
1150          * a pool, we shouldn't have any problems here.  The user won't be
1151          * able to unload the CTL module until client modules have
1152          * successfully unloaded.
1153          */
1154         while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1155                 ctl_pool_free(pool);
1156
1157 #if 0
1158         ctl_shutdown_thread(softc->work_thread);
1159         mtx_destroy(&softc->queue_lock);
1160 #endif
1161
1162         mtx_destroy(&softc->pool_lock);
1163         mtx_destroy(&softc->ctl_lock);
1164
1165         destroy_dev(softc->dev);
1166
1167         sysctl_ctx_free(&softc->sysctl_ctx);
1168
1169         free(control_softc, M_DEVBUF);
1170         control_softc = NULL;
1171
1172         if (bootverbose)
1173                 printf("ctl: CAM Target Layer unloaded\n");
1174 }
1175
1176 static int
1177 ctl_module_event_handler(module_t mod, int what, void *arg)
1178 {
1179
1180         switch (what) {
1181         case MOD_LOAD:
1182                 return (ctl_init());
1183         case MOD_UNLOAD:
1184                 return (EBUSY);
1185         default:
1186                 return (EOPNOTSUPP);
1187         }
1188 }
1189
1190 /*
1191  * XXX KDM should we do some access checks here?  Bump a reference count to
1192  * prevent a CTL module from being unloaded while someone has it open?
1193  */
1194 static int
1195 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1196 {
1197         return (0);
1198 }
1199
1200 static int
1201 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1202 {
1203         return (0);
1204 }
1205
1206 int
1207 ctl_port_enable(ctl_port_type port_type)
1208 {
1209         struct ctl_softc *softc;
1210         struct ctl_frontend *fe;
1211
1212         if (ctl_is_single == 0) {
1213                 union ctl_ha_msg msg_info;
1214                 int isc_retval;
1215
1216 #if 0
1217                 printf("%s: HA mode, synchronizing frontend enable\n",
1218                         __func__);
1219 #endif
1220                 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1221                 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1222                         sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1223                         printf("Sync msg send error retval %d\n", isc_retval);
1224                 }
1225                 if (!rcv_sync_msg) {
1226                         isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1227                                 sizeof(msg_info), 1);
1228                 }
1229 #if 0
1230                 printf("CTL:Frontend Enable\n");
1231         } else {
1232                 printf("%s: single mode, skipping frontend synchronization\n",
1233                         __func__);
1234 #endif
1235         }
1236
1237         softc = control_softc;
1238
1239         STAILQ_FOREACH(fe, &softc->fe_list, links) {
1240                 if (port_type & fe->port_type)
1241                 {
1242 #if 0
1243                         printf("port %d\n", fe->targ_port);
1244 #endif
1245                         ctl_frontend_online(fe);
1246                 }
1247         }
1248
1249         return (0);
1250 }
1251
1252 int
1253 ctl_port_disable(ctl_port_type port_type)
1254 {
1255         struct ctl_softc *softc;
1256         struct ctl_frontend *fe;
1257
1258         softc = control_softc;
1259
1260         STAILQ_FOREACH(fe, &softc->fe_list, links) {
1261                 if (port_type & fe->port_type)
1262                         ctl_frontend_offline(fe);
1263         }
1264
1265         return (0);
1266 }
1267
1268 /*
1269  * Returns 0 for success, 1 for failure.
1270  * Currently the only failure mode is if there aren't enough entries
1271  * allocated.  So, in case of a failure, look at num_entries_dropped,
1272  * reallocate and try again.
1273  */
1274 int
1275 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1276               int *num_entries_filled, int *num_entries_dropped,
1277               ctl_port_type port_type, int no_virtual)
1278 {
1279         struct ctl_softc *softc;
1280         struct ctl_frontend *fe;
1281         int entries_dropped, entries_filled;
1282         int retval;
1283         int i;
1284
1285         softc = control_softc;
1286
1287         retval = 0;
1288         entries_filled = 0;
1289         entries_dropped = 0;
1290
1291         i = 0;
1292         mtx_lock(&softc->ctl_lock);
1293         STAILQ_FOREACH(fe, &softc->fe_list, links) {
1294                 struct ctl_port_entry *entry;
1295
1296                 if ((fe->port_type & port_type) == 0)
1297                         continue;
1298
1299                 if ((no_virtual != 0)
1300                  && (fe->virtual_port != 0))
1301                         continue;
1302
1303                 if (entries_filled >= num_entries_alloced) {
1304                         entries_dropped++;
1305                         continue;
1306                 }
1307                 entry = &entries[i];
1308
1309                 entry->port_type = fe->port_type;
1310                 strlcpy(entry->port_name, fe->port_name,
1311                         sizeof(entry->port_name));
1312                 entry->physical_port = fe->physical_port;
1313                 entry->virtual_port = fe->virtual_port;
1314                 entry->wwnn = fe->wwnn;
1315                 entry->wwpn = fe->wwpn;
1316
1317                 i++;
1318                 entries_filled++;
1319         }
1320
1321         mtx_unlock(&softc->ctl_lock);
1322
1323         if (entries_dropped > 0)
1324                 retval = 1;
1325
1326         *num_entries_dropped = entries_dropped;
1327         *num_entries_filled = entries_filled;
1328
1329         return (retval);
1330 }
1331
1332 static void
1333 ctl_ioctl_online(void *arg)
1334 {
1335         struct ctl_ioctl_info *ioctl_info;
1336
1337         ioctl_info = (struct ctl_ioctl_info *)arg;
1338
1339         ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1340 }
1341
1342 static void
1343 ctl_ioctl_offline(void *arg)
1344 {
1345         struct ctl_ioctl_info *ioctl_info;
1346
1347         ioctl_info = (struct ctl_ioctl_info *)arg;
1348
1349         ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1350 }
1351
1352 /*
1353  * Remove an initiator by port number and initiator ID.
1354  * Returns 0 for success, 1 for failure.
1355  */
1356 int
1357 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1358 {
1359         struct ctl_softc *softc;
1360
1361         softc = control_softc;
1362
1363         mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1364
1365         if ((targ_port < 0)
1366          || (targ_port > CTL_MAX_PORTS)) {
1367                 printf("%s: invalid port number %d\n", __func__, targ_port);
1368                 return (1);
1369         }
1370         if (iid > CTL_MAX_INIT_PER_PORT) {
1371                 printf("%s: initiator ID %u > maximun %u!\n",
1372                        __func__, iid, CTL_MAX_INIT_PER_PORT);
1373                 return (1);
1374         }
1375
1376         mtx_lock(&softc->ctl_lock);
1377
1378         softc->wwpn_iid[targ_port][iid].in_use = 0;
1379
1380         mtx_unlock(&softc->ctl_lock);
1381
1382         return (0);
1383 }
1384
1385 /*
1386  * Add an initiator to the initiator map.
1387  * Returns 0 for success, 1 for failure.
1388  */
1389 int
1390 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1391 {
1392         struct ctl_softc *softc;
1393         int retval;
1394
1395         softc = control_softc;
1396
1397         mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1398
1399         retval = 0;
1400
1401         if ((targ_port < 0)
1402          || (targ_port > CTL_MAX_PORTS)) {
1403                 printf("%s: invalid port number %d\n", __func__, targ_port);
1404                 return (1);
1405         }
1406         if (iid > CTL_MAX_INIT_PER_PORT) {
1407                 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1408                        __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1409                 return (1);
1410         }
1411
1412         mtx_lock(&softc->ctl_lock);
1413
1414         if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1415                 /*
1416                  * We don't treat this as an error.
1417                  */
1418                 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1419                         printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1420                                __func__, targ_port, iid, (uintmax_t)wwpn);
1421                         goto bailout;
1422                 }
1423
1424                 /*
1425                  * This is an error, but what do we do about it?  The
1426                  * driver is telling us we have a new WWPN for this
1427                  * initiator ID, so we pretty much need to use it.
1428                  */
1429                 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1430                        "still at that address\n", __func__, targ_port, iid,
1431                        (uintmax_t)wwpn,
1432                        (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1433
1434                 /*
1435                  * XXX KDM clear have_ca and ua_pending on each LUN for
1436                  * this initiator.
1437                  */
1438         }
1439         softc->wwpn_iid[targ_port][iid].in_use = 1;
1440         softc->wwpn_iid[targ_port][iid].iid = iid;
1441         softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1442         softc->wwpn_iid[targ_port][iid].port = targ_port;
1443
1444 bailout:
1445
1446         mtx_unlock(&softc->ctl_lock);
1447
1448         return (retval);
1449 }
1450
1451 /*
1452  * XXX KDM should we pretend to do something in the target/lun
1453  * enable/disable functions?
1454  */
1455 static int
1456 ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1457 {
1458         return (0);
1459 }
1460
1461 static int
1462 ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1463 {
1464         return (0);
1465 }
1466
1467 static int
1468 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1469 {
1470         return (0);
1471 }
1472
1473 static int
1474 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1475 {
1476         return (0);
1477 }
1478
1479 /*
1480  * Data movement routine for the CTL ioctl frontend port.
1481  */
1482 static int
1483 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1484 {
1485         struct ctl_sg_entry *ext_sglist, *kern_sglist;
1486         struct ctl_sg_entry ext_entry, kern_entry;
1487         int ext_sglen, ext_sg_entries, kern_sg_entries;
1488         int ext_sg_start, ext_offset;
1489         int len_to_copy, len_copied;
1490         int kern_watermark, ext_watermark;
1491         int ext_sglist_malloced;
1492         int i, j;
1493
1494         ext_sglist_malloced = 0;
1495         ext_sg_start = 0;
1496         ext_offset = 0;
1497
1498         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1499
1500         /*
1501          * If this flag is set, fake the data transfer.
1502          */
1503         if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1504                 ctsio->ext_data_filled = ctsio->ext_data_len;
1505                 goto bailout;
1506         }
1507
1508         /*
1509          * To simplify things here, if we have a single buffer, stick it in
1510          * a S/G entry and just make it a single entry S/G list.
1511          */
1512         if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1513                 int len_seen;
1514
1515                 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1516
1517                 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1518                                                            M_WAITOK);
1519                 ext_sglist_malloced = 1;
1520                 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1521                                    ext_sglen) != 0) {
1522                         ctl_set_internal_failure(ctsio,
1523                                                  /*sks_valid*/ 0,
1524                                                  /*retry_count*/ 0);
1525                         goto bailout;
1526                 }
1527                 ext_sg_entries = ctsio->ext_sg_entries;
1528                 len_seen = 0;
1529                 for (i = 0; i < ext_sg_entries; i++) {
1530                         if ((len_seen + ext_sglist[i].len) >=
1531                              ctsio->ext_data_filled) {
1532                                 ext_sg_start = i;
1533                                 ext_offset = ctsio->ext_data_filled - len_seen;
1534                                 break;
1535                         }
1536                         len_seen += ext_sglist[i].len;
1537                 }
1538         } else {
1539                 ext_sglist = &ext_entry;
1540                 ext_sglist->addr = ctsio->ext_data_ptr;
1541                 ext_sglist->len = ctsio->ext_data_len;
1542                 ext_sg_entries = 1;
1543                 ext_sg_start = 0;
1544                 ext_offset = ctsio->ext_data_filled;
1545         }
1546
1547         if (ctsio->kern_sg_entries > 0) {
1548                 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1549                 kern_sg_entries = ctsio->kern_sg_entries;
1550         } else {
1551                 kern_sglist = &kern_entry;
1552                 kern_sglist->addr = ctsio->kern_data_ptr;
1553                 kern_sglist->len = ctsio->kern_data_len;
1554                 kern_sg_entries = 1;
1555         }
1556
1557
1558         kern_watermark = 0;
1559         ext_watermark = ext_offset;
1560         len_copied = 0;
1561         for (i = ext_sg_start, j = 0;
1562              i < ext_sg_entries && j < kern_sg_entries;) {
1563                 uint8_t *ext_ptr, *kern_ptr;
1564
1565                 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1566                                       kern_sglist[j].len - kern_watermark);
1567
1568                 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1569                 ext_ptr = ext_ptr + ext_watermark;
1570                 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1571                         /*
1572                          * XXX KDM fix this!
1573                          */
1574                         panic("need to implement bus address support");
1575 #if 0
1576                         kern_ptr = bus_to_virt(kern_sglist[j].addr);
1577 #endif
1578                 } else
1579                         kern_ptr = (uint8_t *)kern_sglist[j].addr;
1580                 kern_ptr = kern_ptr + kern_watermark;
1581
1582                 kern_watermark += len_to_copy;
1583                 ext_watermark += len_to_copy;
1584
1585                 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1586                      CTL_FLAG_DATA_IN) {
1587                         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1588                                          "bytes to user\n", len_to_copy));
1589                         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1590                                          "to %p\n", kern_ptr, ext_ptr));
1591                         if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1592                                 ctl_set_internal_failure(ctsio,
1593                                                          /*sks_valid*/ 0,
1594                                                          /*retry_count*/ 0);
1595                                 goto bailout;
1596                         }
1597                 } else {
1598                         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1599                                          "bytes from user\n", len_to_copy));
1600                         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1601                                          "to %p\n", ext_ptr, kern_ptr));
1602                         if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1603                                 ctl_set_internal_failure(ctsio,
1604                                                          /*sks_valid*/ 0,
1605                                                          /*retry_count*/0);
1606                                 goto bailout;
1607                         }
1608                 }
1609
1610                 len_copied += len_to_copy;
1611
1612                 if (ext_sglist[i].len == ext_watermark) {
1613                         i++;
1614                         ext_watermark = 0;
1615                 }
1616
1617                 if (kern_sglist[j].len == kern_watermark) {
1618                         j++;
1619                         kern_watermark = 0;
1620                 }
1621         }
1622
1623         ctsio->ext_data_filled += len_copied;
1624
1625         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1626                          "kern_sg_entries: %d\n", ext_sg_entries,
1627                          kern_sg_entries));
1628         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1629                          "kern_data_len = %d\n", ctsio->ext_data_len,
1630                          ctsio->kern_data_len));
1631
1632
1633         /* XXX KDM set residual?? */
1634 bailout:
1635
1636         if (ext_sglist_malloced != 0)
1637                 free(ext_sglist, M_CTL);
1638
1639         return (CTL_RETVAL_COMPLETE);
1640 }
1641
1642 /*
1643  * Serialize a command that went down the "wrong" side, and so was sent to
1644  * this controller for execution.  The logic is a little different than the
1645  * standard case in ctl_scsiio_precheck().  Errors in this case need to get
1646  * sent back to the other side, but in the success case, we execute the
1647  * command on this side (XFER mode) or tell the other side to execute it
1648  * (SER_ONLY mode).
1649  */
1650 static int
1651 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1652 {
1653         struct ctl_softc *ctl_softc;
1654         union ctl_ha_msg msg_info;
1655         struct ctl_lun *lun;
1656         int retval = 0;
1657         uint32_t targ_lun;
1658
1659         ctl_softc = control_softc;
1660
1661         targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1662         lun = ctl_softc->ctl_luns[targ_lun];
1663         if (lun==NULL)
1664         {
1665                 /*
1666                  * Why isn't LUN defined? The other side wouldn't
1667                  * send a cmd if the LUN is undefined.
1668                  */
1669                 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1670
1671                 /* "Logical unit not supported" */
1672                 ctl_set_sense_data(&msg_info.scsi.sense_data,
1673                                    lun,
1674                                    /*sense_format*/SSD_TYPE_NONE,
1675                                    /*current_error*/ 1,
1676                                    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1677                                    /*asc*/ 0x25,
1678                                    /*ascq*/ 0x00,
1679                                    SSD_ELEM_NONE);
1680
1681                 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1682                 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1683                 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1684                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1685                 msg_info.hdr.serializing_sc = NULL;
1686                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1687                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1688                                 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1689                 }
1690                 return(1);
1691
1692         }
1693
1694         mtx_lock(&lun->lun_lock);
1695         TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1696
1697         switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1698                 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1699                  ooa_links))) {
1700         case CTL_ACTION_BLOCK:
1701                 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1702                 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1703                                   blocked_links);
1704                 break;
1705         case CTL_ACTION_PASS:
1706         case CTL_ACTION_SKIP:
1707                 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1708                         ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1709                         ctl_enqueue_rtr((union ctl_io *)ctsio);
1710                 } else {
1711
1712                         /* send msg back to other side */
1713                         msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1714                         msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1715                         msg_info.hdr.msg_type = CTL_MSG_R2R;
1716 #if 0
1717                         printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1718 #endif
1719                         if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1720                             sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1721                         }
1722                 }
1723                 break;
1724         case CTL_ACTION_OVERLAP:
1725                 /* OVERLAPPED COMMANDS ATTEMPTED */
1726                 ctl_set_sense_data(&msg_info.scsi.sense_data,
1727                                    lun,
1728                                    /*sense_format*/SSD_TYPE_NONE,
1729                                    /*current_error*/ 1,
1730                                    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1731                                    /*asc*/ 0x4E,
1732                                    /*ascq*/ 0x00,
1733                                    SSD_ELEM_NONE);
1734
1735                 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1736                 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1737                 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1738                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1739                 msg_info.hdr.serializing_sc = NULL;
1740                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1741 #if 0
1742                 printf("BAD JUJU:Major Bummer Overlap\n");
1743 #endif
1744                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1745                 retval = 1;
1746                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1747                     sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1748                 }
1749                 break;
1750         case CTL_ACTION_OVERLAP_TAG:
1751                 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1752                 ctl_set_sense_data(&msg_info.scsi.sense_data,
1753                                    lun,
1754                                    /*sense_format*/SSD_TYPE_NONE,
1755                                    /*current_error*/ 1,
1756                                    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1757                                    /*asc*/ 0x4D,
1758                                    /*ascq*/ ctsio->tag_num & 0xff,
1759                                    SSD_ELEM_NONE);
1760
1761                 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1762                 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1763                 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1764                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1765                 msg_info.hdr.serializing_sc = NULL;
1766                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1767 #if 0
1768                 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1769 #endif
1770                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1771                 retval = 1;
1772                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1773                     sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1774                 }
1775                 break;
1776         case CTL_ACTION_ERROR:
1777         default:
1778                 /* "Internal target failure" */
1779                 ctl_set_sense_data(&msg_info.scsi.sense_data,
1780                                    lun,
1781                                    /*sense_format*/SSD_TYPE_NONE,
1782                                    /*current_error*/ 1,
1783                                    /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1784                                    /*asc*/ 0x44,
1785                                    /*ascq*/ 0x00,
1786                                    SSD_ELEM_NONE);
1787
1788                 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1789                 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1790                 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1791                 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1792                 msg_info.hdr.serializing_sc = NULL;
1793                 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1794 #if 0
1795                 printf("BAD JUJU:Major Bummer HW Error\n");
1796 #endif
1797                 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1798                 retval = 1;
1799                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1800                     sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1801                 }
1802                 break;
1803         }
1804         mtx_unlock(&lun->lun_lock);
1805         return (retval);
1806 }
1807
1808 static int
1809 ctl_ioctl_submit_wait(union ctl_io *io)
1810 {
1811         struct ctl_fe_ioctl_params params;
1812         ctl_fe_ioctl_state last_state;
1813         int done, retval;
1814
1815         retval = 0;
1816
1817         bzero(&params, sizeof(params));
1818
1819         mtx_init(&params.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1820         cv_init(&params.sem, "ctlioccv");
1821         params.state = CTL_IOCTL_INPROG;
1822         last_state = params.state;
1823
1824         io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = &params;
1825
1826         CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1827
1828         /* This shouldn't happen */
1829         if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1830                 return (retval);
1831
1832         done = 0;
1833
1834         do {
1835                 mtx_lock(&params.ioctl_mtx);
1836                 /*
1837                  * Check the state here, and don't sleep if the state has
1838                  * already changed (i.e. wakeup has already occured, but we
1839                  * weren't waiting yet).
1840                  */
1841                 if (params.state == last_state) {
1842                         /* XXX KDM cv_wait_sig instead? */
1843                         cv_wait(&params.sem, &params.ioctl_mtx);
1844                 }
1845                 last_state = params.state;
1846
1847                 switch (params.state) {
1848                 case CTL_IOCTL_INPROG:
1849                         /* Why did we wake up? */
1850                         /* XXX KDM error here? */
1851                         mtx_unlock(&params.ioctl_mtx);
1852                         break;
1853                 case CTL_IOCTL_DATAMOVE:
1854                         CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1855
1856                         /*
1857                          * change last_state back to INPROG to avoid
1858                          * deadlock on subsequent data moves.
1859                          */
1860                         params.state = last_state = CTL_IOCTL_INPROG;
1861
1862                         mtx_unlock(&params.ioctl_mtx);
1863                         ctl_ioctl_do_datamove(&io->scsiio);
1864                         /*
1865                          * Note that in some cases, most notably writes,
1866                          * this will queue the I/O and call us back later.
1867                          * In other cases, generally reads, this routine
1868                          * will immediately call back and wake us up,
1869                          * probably using our own context.
1870                          */
1871                         io->scsiio.be_move_done(io);
1872                         break;
1873                 case CTL_IOCTL_DONE:
1874                         mtx_unlock(&params.ioctl_mtx);
1875                         CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1876                         done = 1;
1877                         break;
1878                 default:
1879                         mtx_unlock(&params.ioctl_mtx);
1880                         /* XXX KDM error here? */
1881                         break;
1882                 }
1883         } while (done == 0);
1884
1885         mtx_destroy(&params.ioctl_mtx);
1886         cv_destroy(&params.sem);
1887
1888         return (CTL_RETVAL_COMPLETE);
1889 }
1890
1891 static void
1892 ctl_ioctl_datamove(union ctl_io *io)
1893 {
1894         struct ctl_fe_ioctl_params *params;
1895
1896         params = (struct ctl_fe_ioctl_params *)
1897                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1898
1899         mtx_lock(&params->ioctl_mtx);
1900         params->state = CTL_IOCTL_DATAMOVE;
1901         cv_broadcast(&params->sem);
1902         mtx_unlock(&params->ioctl_mtx);
1903 }
1904
1905 static void
1906 ctl_ioctl_done(union ctl_io *io)
1907 {
1908         struct ctl_fe_ioctl_params *params;
1909
1910         params = (struct ctl_fe_ioctl_params *)
1911                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1912
1913         mtx_lock(&params->ioctl_mtx);
1914         params->state = CTL_IOCTL_DONE;
1915         cv_broadcast(&params->sem);
1916         mtx_unlock(&params->ioctl_mtx);
1917 }
1918
1919 static void
1920 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1921 {
1922         struct ctl_fe_ioctl_startstop_info *sd_info;
1923
1924         sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1925
1926         sd_info->hs_info.status = metatask->status;
1927         sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1928         sd_info->hs_info.luns_complete =
1929                 metatask->taskinfo.startstop.luns_complete;
1930         sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1931
1932         cv_broadcast(&sd_info->sem);
1933 }
1934
1935 static void
1936 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1937 {
1938         struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1939
1940         fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1941
1942         mtx_lock(fe_bbr_info->lock);
1943         fe_bbr_info->bbr_info->status = metatask->status;
1944         fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1945         fe_bbr_info->wakeup_done = 1;
1946         mtx_unlock(fe_bbr_info->lock);
1947
1948         cv_broadcast(&fe_bbr_info->sem);
1949 }
1950
1951 /*
1952  * Returns 0 for success, errno for failure.
1953  */
1954 static int
1955 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1956                    struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1957 {
1958         union ctl_io *io;
1959         int retval;
1960
1961         retval = 0;
1962
1963         mtx_lock(&lun->lun_lock);
1964         for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1965              (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1966              ooa_links)) {
1967                 struct ctl_ooa_entry *entry;
1968
1969                 /*
1970                  * If we've got more than we can fit, just count the
1971                  * remaining entries.
1972                  */
1973                 if (*cur_fill_num >= ooa_hdr->alloc_num)
1974                         continue;
1975
1976                 entry = &kern_entries[*cur_fill_num];
1977
1978                 entry->tag_num = io->scsiio.tag_num;
1979                 entry->lun_num = lun->lun;
1980 #ifdef CTL_TIME_IO
1981                 entry->start_bt = io->io_hdr.start_bt;
1982 #endif
1983                 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
1984                 entry->cdb_len = io->scsiio.cdb_len;
1985                 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1986                         entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1987
1988                 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1989                         entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
1990
1991                 if (io->io_hdr.flags & CTL_FLAG_ABORT)
1992                         entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1993
1994                 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1995                         entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
1996
1997                 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1998                         entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
1999         }
2000         mtx_unlock(&lun->lun_lock);
2001
2002         return (retval);
2003 }
2004
2005 static void *
2006 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2007                  size_t error_str_len)
2008 {
2009         void *kptr;
2010
2011         kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2012
2013         if (copyin(user_addr, kptr, len) != 0) {
2014                 snprintf(error_str, error_str_len, "Error copying %d bytes "
2015                          "from user address %p to kernel address %p", len,
2016                          user_addr, kptr);
2017                 free(kptr, M_CTL);
2018                 return (NULL);
2019         }
2020
2021         return (kptr);
2022 }
2023
2024 static void
2025 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2026 {
2027         int i;
2028
2029         if (be_args == NULL)
2030                 return;
2031
2032         for (i = 0; i < num_be_args; i++) {
2033                 free(be_args[i].kname, M_CTL);
2034                 free(be_args[i].kvalue, M_CTL);
2035         }
2036
2037         free(be_args, M_CTL);
2038 }
2039
2040 static struct ctl_be_arg *
2041 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2042                 char *error_str, size_t error_str_len)
2043 {
2044         struct ctl_be_arg *args;
2045         int i;
2046
2047         args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2048                                 error_str, error_str_len);
2049
2050         if (args == NULL)
2051                 goto bailout;
2052
2053         for (i = 0; i < num_be_args; i++) {
2054                 args[i].kname = NULL;
2055                 args[i].kvalue = NULL;
2056         }
2057
2058         for (i = 0; i < num_be_args; i++) {
2059                 uint8_t *tmpptr;
2060
2061                 args[i].kname = ctl_copyin_alloc(args[i].name,
2062                         args[i].namelen, error_str, error_str_len);
2063                 if (args[i].kname == NULL)
2064                         goto bailout;
2065
2066                 if (args[i].kname[args[i].namelen - 1] != '\0') {
2067                         snprintf(error_str, error_str_len, "Argument %d "
2068                                  "name is not NUL-terminated", i);
2069                         goto bailout;
2070                 }
2071
2072                 args[i].kvalue = NULL;
2073
2074                 tmpptr = ctl_copyin_alloc(args[i].value,
2075                         args[i].vallen, error_str, error_str_len);
2076                 if (tmpptr == NULL)
2077                         goto bailout;
2078
2079                 args[i].kvalue = tmpptr;
2080
2081                 if ((args[i].flags & CTL_BEARG_ASCII)
2082                  && (tmpptr[args[i].vallen - 1] != '\0')) {
2083                         snprintf(error_str, error_str_len, "Argument %d "
2084                                  "value is not NUL-terminated", i);
2085                         goto bailout;
2086                 }
2087         }
2088
2089         return (args);
2090 bailout:
2091
2092         ctl_free_args(num_be_args, args);
2093
2094         return (NULL);
2095 }
2096
2097 /*
2098  * Escape characters that are illegal or not recommended in XML.
2099  */
2100 int
2101 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2102 {
2103         int retval;
2104
2105         retval = 0;
2106
2107         for (; *str; str++) {
2108                 switch (*str) {
2109                 case '&':
2110                         retval = sbuf_printf(sb, "&amp;");
2111                         break;
2112                 case '>':
2113                         retval = sbuf_printf(sb, "&gt;");
2114                         break;
2115                 case '<':
2116                         retval = sbuf_printf(sb, "&lt;");
2117                         break;
2118                 default:
2119                         retval = sbuf_putc(sb, *str);
2120                         break;
2121                 }
2122
2123                 if (retval != 0)
2124                         break;
2125
2126         }
2127
2128         return (retval);
2129 }
2130
2131 static int
2132 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2133           struct thread *td)
2134 {
2135         struct ctl_softc *softc;
2136         int retval;
2137
2138         softc = control_softc;
2139
2140         retval = 0;
2141
2142         switch (cmd) {
2143         case CTL_IO: {
2144                 union ctl_io *io;
2145                 void *pool_tmp;
2146
2147                 /*
2148                  * If we haven't been "enabled", don't allow any SCSI I/O
2149                  * to this FETD.
2150                  */
2151                 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2152                         retval = EPERM;
2153                         break;
2154                 }
2155
2156                 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2157                 if (io == NULL) {
2158                         printf("ctl_ioctl: can't allocate ctl_io!\n");
2159                         retval = ENOSPC;
2160                         break;
2161                 }
2162
2163                 /*
2164                  * Need to save the pool reference so it doesn't get
2165                  * spammed by the user's ctl_io.
2166                  */
2167                 pool_tmp = io->io_hdr.pool;
2168
2169                 memcpy(io, (void *)addr, sizeof(*io));
2170
2171                 io->io_hdr.pool = pool_tmp;
2172                 /*
2173                  * No status yet, so make sure the status is set properly.
2174                  */
2175                 io->io_hdr.status = CTL_STATUS_NONE;
2176
2177                 /*
2178                  * The user sets the initiator ID, target and LUN IDs.
2179                  */
2180                 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2181                 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2182                 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2183                  && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2184                         io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2185
2186                 retval = ctl_ioctl_submit_wait(io);
2187
2188                 if (retval != 0) {
2189                         ctl_free_io(io);
2190                         break;
2191                 }
2192
2193                 memcpy((void *)addr, io, sizeof(*io));
2194
2195                 /* return this to our pool */
2196                 ctl_free_io(io);
2197
2198                 break;
2199         }
2200         case CTL_ENABLE_PORT:
2201         case CTL_DISABLE_PORT:
2202         case CTL_SET_PORT_WWNS: {
2203                 struct ctl_frontend *fe;
2204                 struct ctl_port_entry *entry;
2205
2206                 entry = (struct ctl_port_entry *)addr;
2207                 
2208                 mtx_lock(&softc->ctl_lock);
2209                 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2210                         int action, done;
2211
2212                         action = 0;
2213                         done = 0;
2214
2215                         if ((entry->port_type == CTL_PORT_NONE)
2216                          && (entry->targ_port == fe->targ_port)) {
2217                                 /*
2218                                  * If the user only wants to enable or
2219                                  * disable or set WWNs on a specific port,
2220                                  * do the operation and we're done.
2221                                  */
2222                                 action = 1;
2223                                 done = 1;
2224                         } else if (entry->port_type & fe->port_type) {
2225                                 /*
2226                                  * Compare the user's type mask with the
2227                                  * particular frontend type to see if we
2228                                  * have a match.
2229                                  */
2230                                 action = 1;
2231                                 done = 0;
2232
2233                                 /*
2234                                  * Make sure the user isn't trying to set
2235                                  * WWNs on multiple ports at the same time.
2236                                  */
2237                                 if (cmd == CTL_SET_PORT_WWNS) {
2238                                         printf("%s: Can't set WWNs on "
2239                                                "multiple ports\n", __func__);
2240                                         retval = EINVAL;
2241                                         break;
2242                                 }
2243                         }
2244                         if (action != 0) {
2245                                 /*
2246                                  * XXX KDM we have to drop the lock here,
2247                                  * because the online/offline operations
2248                                  * can potentially block.  We need to
2249                                  * reference count the frontends so they
2250                                  * can't go away,
2251                                  */
2252                                 mtx_unlock(&softc->ctl_lock);
2253
2254                                 if (cmd == CTL_ENABLE_PORT) {
2255                                         struct ctl_lun *lun;
2256
2257                                         STAILQ_FOREACH(lun, &softc->lun_list,
2258                                                        links) {
2259                                                 fe->lun_enable(fe->targ_lun_arg,
2260                                                     lun->target,
2261                                                     lun->lun);
2262                                         }
2263
2264                                         ctl_frontend_online(fe);
2265                                 } else if (cmd == CTL_DISABLE_PORT) {
2266                                         struct ctl_lun *lun;
2267
2268                                         ctl_frontend_offline(fe);
2269
2270                                         STAILQ_FOREACH(lun, &softc->lun_list,
2271                                                        links) {
2272                                                 fe->lun_disable(
2273                                                     fe->targ_lun_arg,
2274                                                     lun->target,
2275                                                     lun->lun);
2276                                         }
2277                                 }
2278
2279                                 mtx_lock(&softc->ctl_lock);
2280
2281                                 if (cmd == CTL_SET_PORT_WWNS)
2282                                         ctl_frontend_set_wwns(fe,
2283                                             (entry->flags & CTL_PORT_WWNN_VALID) ?
2284                                             1 : 0, entry->wwnn,
2285                                             (entry->flags & CTL_PORT_WWPN_VALID) ?
2286                                             1 : 0, entry->wwpn);
2287                         }
2288                         if (done != 0)
2289                                 break;
2290                 }
2291                 mtx_unlock(&softc->ctl_lock);
2292                 break;
2293         }
2294         case CTL_GET_PORT_LIST: {
2295                 struct ctl_frontend *fe;
2296                 struct ctl_port_list *list;
2297                 int i;
2298
2299                 list = (struct ctl_port_list *)addr;
2300
2301                 if (list->alloc_len != (list->alloc_num *
2302                     sizeof(struct ctl_port_entry))) {
2303                         printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2304                                "alloc_num %u * sizeof(struct ctl_port_entry) "
2305                                "%zu\n", __func__, list->alloc_len,
2306                                list->alloc_num, sizeof(struct ctl_port_entry));
2307                         retval = EINVAL;
2308                         break;
2309                 }
2310                 list->fill_len = 0;
2311                 list->fill_num = 0;
2312                 list->dropped_num = 0;
2313                 i = 0;
2314                 mtx_lock(&softc->ctl_lock);
2315                 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2316                         struct ctl_port_entry entry, *list_entry;
2317
2318                         if (list->fill_num >= list->alloc_num) {
2319                                 list->dropped_num++;
2320                                 continue;
2321                         }
2322
2323                         entry.port_type = fe->port_type;
2324                         strlcpy(entry.port_name, fe->port_name,
2325                                 sizeof(entry.port_name));
2326                         entry.targ_port = fe->targ_port;
2327                         entry.physical_port = fe->physical_port;
2328                         entry.virtual_port = fe->virtual_port;
2329                         entry.wwnn = fe->wwnn;
2330                         entry.wwpn = fe->wwpn;
2331                         if (fe->status & CTL_PORT_STATUS_ONLINE)
2332                                 entry.online = 1;
2333                         else
2334                                 entry.online = 0;
2335
2336                         list_entry = &list->entries[i];
2337
2338                         retval = copyout(&entry, list_entry, sizeof(entry));
2339                         if (retval != 0) {
2340                                 printf("%s: CTL_GET_PORT_LIST: copyout "
2341                                        "returned %d\n", __func__, retval);
2342                                 break;
2343                         }
2344                         i++;
2345                         list->fill_num++;
2346                         list->fill_len += sizeof(entry);
2347                 }
2348                 mtx_unlock(&softc->ctl_lock);
2349
2350                 /*
2351                  * If this is non-zero, we had a copyout fault, so there's
2352                  * probably no point in attempting to set the status inside
2353                  * the structure.
2354                  */
2355                 if (retval != 0)
2356                         break;
2357
2358                 if (list->dropped_num > 0)
2359                         list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2360                 else
2361                         list->status = CTL_PORT_LIST_OK;
2362                 break;
2363         }
2364         case CTL_DUMP_OOA: {
2365                 struct ctl_lun *lun;
2366                 union ctl_io *io;
2367                 char printbuf[128];
2368                 struct sbuf sb;
2369
2370                 mtx_lock(&softc->ctl_lock);
2371                 printf("Dumping OOA queues:\n");
2372                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2373                         mtx_lock(&lun->lun_lock);
2374                         for (io = (union ctl_io *)TAILQ_FIRST(
2375                              &lun->ooa_queue); io != NULL;
2376                              io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2377                              ooa_links)) {
2378                                 sbuf_new(&sb, printbuf, sizeof(printbuf),
2379                                          SBUF_FIXEDLEN);
2380                                 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2381                                             (intmax_t)lun->lun,
2382                                             io->scsiio.tag_num,
2383                                             (io->io_hdr.flags &
2384                                             CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2385                                             (io->io_hdr.flags &
2386                                             CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2387                                             (io->io_hdr.flags &
2388                                             CTL_FLAG_ABORT) ? " ABORT" : "",
2389                                             (io->io_hdr.flags &
2390                                         CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2391                                 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2392                                 sbuf_finish(&sb);
2393                                 printf("%s\n", sbuf_data(&sb));
2394                         }
2395                         mtx_unlock(&lun->lun_lock);
2396                 }
2397                 printf("OOA queues dump done\n");
2398                 mtx_unlock(&softc->ctl_lock);
2399                 break;
2400         }
2401         case CTL_GET_OOA: {
2402                 struct ctl_lun *lun;
2403                 struct ctl_ooa *ooa_hdr;
2404                 struct ctl_ooa_entry *entries;
2405                 uint32_t cur_fill_num;
2406
2407                 ooa_hdr = (struct ctl_ooa *)addr;
2408
2409                 if ((ooa_hdr->alloc_len == 0)
2410                  || (ooa_hdr->alloc_num == 0)) {
2411                         printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2412                                "must be non-zero\n", __func__,
2413                                ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2414                         retval = EINVAL;
2415                         break;
2416                 }
2417
2418                 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2419                     sizeof(struct ctl_ooa_entry))) {
2420                         printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2421                                "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2422                                __func__, ooa_hdr->alloc_len,
2423                                ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2424                         retval = EINVAL;
2425                         break;
2426                 }
2427
2428                 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2429                 if (entries == NULL) {
2430                         printf("%s: could not allocate %d bytes for OOA "
2431                                "dump\n", __func__, ooa_hdr->alloc_len);
2432                         retval = ENOMEM;
2433                         break;
2434                 }
2435
2436                 mtx_lock(&softc->ctl_lock);
2437                 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2438                  && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2439                   || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2440                         mtx_unlock(&softc->ctl_lock);
2441                         free(entries, M_CTL);
2442                         printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2443                                __func__, (uintmax_t)ooa_hdr->lun_num);
2444                         retval = EINVAL;
2445                         break;
2446                 }
2447
2448                 cur_fill_num = 0;
2449
2450                 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2451                         STAILQ_FOREACH(lun, &softc->lun_list, links) {
2452                                 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2453                                         ooa_hdr, entries);
2454                                 if (retval != 0)
2455                                         break;
2456                         }
2457                         if (retval != 0) {
2458                                 mtx_unlock(&softc->ctl_lock);
2459                                 free(entries, M_CTL);
2460                                 break;
2461                         }
2462                 } else {
2463                         lun = softc->ctl_luns[ooa_hdr->lun_num];
2464
2465                         retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2466                                                     entries);
2467                 }
2468                 mtx_unlock(&softc->ctl_lock);
2469
2470                 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2471                 ooa_hdr->fill_len = ooa_hdr->fill_num *
2472                         sizeof(struct ctl_ooa_entry);
2473                 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2474                 if (retval != 0) {
2475                         printf("%s: error copying out %d bytes for OOA dump\n", 
2476                                __func__, ooa_hdr->fill_len);
2477                 }
2478
2479                 getbintime(&ooa_hdr->cur_bt);
2480
2481                 if (cur_fill_num > ooa_hdr->alloc_num) {
2482                         ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2483                         ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2484                 } else {
2485                         ooa_hdr->dropped_num = 0;
2486                         ooa_hdr->status = CTL_OOA_OK;
2487                 }
2488
2489                 free(entries, M_CTL);
2490                 break;
2491         }
2492         case CTL_CHECK_OOA: {
2493                 union ctl_io *io;
2494                 struct ctl_lun *lun;
2495                 struct ctl_ooa_info *ooa_info;
2496
2497
2498                 ooa_info = (struct ctl_ooa_info *)addr;
2499
2500                 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2501                         ooa_info->status = CTL_OOA_INVALID_LUN;
2502                         break;
2503                 }
2504                 mtx_lock(&softc->ctl_lock);
2505                 lun = softc->ctl_luns[ooa_info->lun_id];
2506                 if (lun == NULL) {
2507                         mtx_unlock(&softc->ctl_lock);
2508                         ooa_info->status = CTL_OOA_INVALID_LUN;
2509                         break;
2510                 }
2511                 mtx_lock(&lun->lun_lock);
2512                 mtx_unlock(&softc->ctl_lock);
2513                 ooa_info->num_entries = 0;
2514                 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2515                      io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2516                      &io->io_hdr, ooa_links)) {
2517                         ooa_info->num_entries++;
2518                 }
2519                 mtx_unlock(&lun->lun_lock);
2520
2521                 ooa_info->status = CTL_OOA_SUCCESS;
2522
2523                 break;
2524         }
2525         case CTL_HARD_START:
2526         case CTL_HARD_STOP: {
2527                 struct ctl_fe_ioctl_startstop_info ss_info;
2528                 struct cfi_metatask *metatask;
2529                 struct mtx hs_mtx;
2530
2531                 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2532
2533                 cv_init(&ss_info.sem, "hard start/stop cv" );
2534
2535                 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2536                 if (metatask == NULL) {
2537                         retval = ENOMEM;
2538                         mtx_destroy(&hs_mtx);
2539                         break;
2540                 }
2541
2542                 if (cmd == CTL_HARD_START)
2543                         metatask->tasktype = CFI_TASK_STARTUP;
2544                 else
2545                         metatask->tasktype = CFI_TASK_SHUTDOWN;
2546
2547                 metatask->callback = ctl_ioctl_hard_startstop_callback;
2548                 metatask->callback_arg = &ss_info;
2549
2550                 cfi_action(metatask);
2551
2552                 /* Wait for the callback */
2553                 mtx_lock(&hs_mtx);
2554                 cv_wait_sig(&ss_info.sem, &hs_mtx);
2555                 mtx_unlock(&hs_mtx);
2556
2557                 /*
2558                  * All information has been copied from the metatask by the
2559                  * time cv_broadcast() is called, so we free the metatask here.
2560                  */
2561                 cfi_free_metatask(metatask);
2562
2563                 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2564
2565                 mtx_destroy(&hs_mtx);
2566                 break;
2567         }
2568         case CTL_BBRREAD: {
2569                 struct ctl_bbrread_info *bbr_info;
2570                 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2571                 struct mtx bbr_mtx;
2572                 struct cfi_metatask *metatask;
2573
2574                 bbr_info = (struct ctl_bbrread_info *)addr;
2575
2576                 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2577
2578                 bzero(&bbr_mtx, sizeof(bbr_mtx));
2579                 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2580
2581                 fe_bbr_info.bbr_info = bbr_info;
2582                 fe_bbr_info.lock = &bbr_mtx;
2583
2584                 cv_init(&fe_bbr_info.sem, "BBR read cv");
2585                 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2586
2587                 if (metatask == NULL) {
2588                         mtx_destroy(&bbr_mtx);
2589                         cv_destroy(&fe_bbr_info.sem);
2590                         retval = ENOMEM;
2591                         break;
2592                 }
2593                 metatask->tasktype = CFI_TASK_BBRREAD;
2594                 metatask->callback = ctl_ioctl_bbrread_callback;
2595                 metatask->callback_arg = &fe_bbr_info;
2596                 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2597                 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2598                 metatask->taskinfo.bbrread.len = bbr_info->len;
2599
2600                 cfi_action(metatask);
2601
2602                 mtx_lock(&bbr_mtx);
2603                 while (fe_bbr_info.wakeup_done == 0)
2604                         cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2605                 mtx_unlock(&bbr_mtx);
2606
2607                 bbr_info->status = metatask->status;
2608                 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2609                 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2610                 memcpy(&bbr_info->sense_data,
2611                        &metatask->taskinfo.bbrread.sense_data,
2612                        ctl_min(sizeof(bbr_info->sense_data),
2613                                sizeof(metatask->taskinfo.bbrread.sense_data)));
2614
2615                 cfi_free_metatask(metatask);
2616
2617                 mtx_destroy(&bbr_mtx);
2618                 cv_destroy(&fe_bbr_info.sem);
2619
2620                 break;
2621         }
2622         case CTL_DELAY_IO: {
2623                 struct ctl_io_delay_info *delay_info;
2624 #ifdef CTL_IO_DELAY
2625                 struct ctl_lun *lun;
2626 #endif /* CTL_IO_DELAY */
2627
2628                 delay_info = (struct ctl_io_delay_info *)addr;
2629
2630 #ifdef CTL_IO_DELAY
2631                 mtx_lock(&softc->ctl_lock);
2632
2633                 if ((delay_info->lun_id > CTL_MAX_LUNS)
2634                  || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2635                         delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2636                 } else {
2637                         lun = softc->ctl_luns[delay_info->lun_id];
2638                         mtx_lock(&lun->lun_lock);
2639
2640                         delay_info->status = CTL_DELAY_STATUS_OK;
2641
2642                         switch (delay_info->delay_type) {
2643                         case CTL_DELAY_TYPE_CONT:
2644                                 break;
2645                         case CTL_DELAY_TYPE_ONESHOT:
2646                                 break;
2647                         default:
2648                                 delay_info->status =
2649                                         CTL_DELAY_STATUS_INVALID_TYPE;
2650                                 break;
2651                         }
2652
2653                         switch (delay_info->delay_loc) {
2654                         case CTL_DELAY_LOC_DATAMOVE:
2655                                 lun->delay_info.datamove_type =
2656                                         delay_info->delay_type;
2657                                 lun->delay_info.datamove_delay =
2658                                         delay_info->delay_secs;
2659                                 break;
2660                         case CTL_DELAY_LOC_DONE:
2661                                 lun->delay_info.done_type =
2662                                         delay_info->delay_type;
2663                                 lun->delay_info.done_delay =
2664                                         delay_info->delay_secs;
2665                                 break;
2666                         default:
2667                                 delay_info->status =
2668                                         CTL_DELAY_STATUS_INVALID_LOC;
2669                                 break;
2670                         }
2671                         mtx_unlock(&lun->lun_lock);
2672                 }
2673
2674                 mtx_unlock(&softc->ctl_lock);
2675 #else
2676                 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2677 #endif /* CTL_IO_DELAY */
2678                 break;
2679         }
2680         case CTL_REALSYNC_SET: {
2681                 int *syncstate;
2682
2683                 syncstate = (int *)addr;
2684
2685                 mtx_lock(&softc->ctl_lock);
2686                 switch (*syncstate) {
2687                 case 0:
2688                         softc->flags &= ~CTL_FLAG_REAL_SYNC;
2689                         break;
2690                 case 1:
2691                         softc->flags |= CTL_FLAG_REAL_SYNC;
2692                         break;
2693                 default:
2694                         retval = EINVAL;
2695                         break;
2696                 }
2697                 mtx_unlock(&softc->ctl_lock);
2698                 break;
2699         }
2700         case CTL_REALSYNC_GET: {
2701                 int *syncstate;
2702
2703                 syncstate = (int*)addr;
2704
2705                 mtx_lock(&softc->ctl_lock);
2706                 if (softc->flags & CTL_FLAG_REAL_SYNC)
2707                         *syncstate = 1;
2708                 else
2709                         *syncstate = 0;
2710                 mtx_unlock(&softc->ctl_lock);
2711
2712                 break;
2713         }
2714         case CTL_SETSYNC:
2715         case CTL_GETSYNC: {
2716                 struct ctl_sync_info *sync_info;
2717                 struct ctl_lun *lun;
2718
2719                 sync_info = (struct ctl_sync_info *)addr;
2720
2721                 mtx_lock(&softc->ctl_lock);
2722                 lun = softc->ctl_luns[sync_info->lun_id];
2723                 if (lun == NULL) {
2724                         mtx_unlock(&softc->ctl_lock);
2725                         sync_info->status = CTL_GS_SYNC_NO_LUN;
2726                 }
2727                 /*
2728                  * Get or set the sync interval.  We're not bounds checking
2729                  * in the set case, hopefully the user won't do something
2730                  * silly.
2731                  */
2732                 mtx_lock(&lun->lun_lock);
2733                 mtx_unlock(&softc->ctl_lock);
2734                 if (cmd == CTL_GETSYNC)
2735                         sync_info->sync_interval = lun->sync_interval;
2736                 else
2737                         lun->sync_interval = sync_info->sync_interval;
2738                 mtx_unlock(&lun->lun_lock);
2739
2740                 sync_info->status = CTL_GS_SYNC_OK;
2741
2742                 break;
2743         }
2744         case CTL_GETSTATS: {
2745                 struct ctl_stats *stats;
2746                 struct ctl_lun *lun;
2747                 int i;
2748
2749                 stats = (struct ctl_stats *)addr;
2750
2751                 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2752                      stats->alloc_len) {
2753                         stats->status = CTL_SS_NEED_MORE_SPACE;
2754                         stats->num_luns = softc->num_luns;
2755                         break;
2756                 }
2757                 /*
2758                  * XXX KDM no locking here.  If the LUN list changes,
2759                  * things can blow up.
2760                  */
2761                 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2762                      i++, lun = STAILQ_NEXT(lun, links)) {
2763                         retval = copyout(&lun->stats, &stats->lun_stats[i],
2764                                          sizeof(lun->stats));
2765                         if (retval != 0)
2766                                 break;
2767                 }
2768                 stats->num_luns = softc->num_luns;
2769                 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2770                                  softc->num_luns;
2771                 stats->status = CTL_SS_OK;
2772 #ifdef CTL_TIME_IO
2773                 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2774 #else
2775                 stats->flags = CTL_STATS_FLAG_NONE;
2776 #endif
2777                 getnanouptime(&stats->timestamp);
2778                 break;
2779         }
2780         case CTL_ERROR_INJECT: {
2781                 struct ctl_error_desc *err_desc, *new_err_desc;
2782                 struct ctl_lun *lun;
2783
2784                 err_desc = (struct ctl_error_desc *)addr;
2785
2786                 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2787                                       M_WAITOK | M_ZERO);
2788                 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2789
2790                 mtx_lock(&softc->ctl_lock);
2791                 lun = softc->ctl_luns[err_desc->lun_id];
2792                 if (lun == NULL) {
2793                         mtx_unlock(&softc->ctl_lock);
2794                         printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2795                                __func__, (uintmax_t)err_desc->lun_id);
2796                         retval = EINVAL;
2797                         break;
2798                 }
2799                 mtx_lock(&lun->lun_lock);
2800                 mtx_unlock(&softc->ctl_lock);
2801
2802                 /*
2803                  * We could do some checking here to verify the validity
2804                  * of the request, but given the complexity of error
2805                  * injection requests, the checking logic would be fairly
2806                  * complex.
2807                  *
2808                  * For now, if the request is invalid, it just won't get
2809                  * executed and might get deleted.
2810                  */
2811                 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2812
2813                 /*
2814                  * XXX KDM check to make sure the serial number is unique,
2815                  * in case we somehow manage to wrap.  That shouldn't
2816                  * happen for a very long time, but it's the right thing to
2817                  * do.
2818                  */
2819                 new_err_desc->serial = lun->error_serial;
2820                 err_desc->serial = lun->error_serial;
2821                 lun->error_serial++;
2822
2823                 mtx_unlock(&lun->lun_lock);
2824                 break;
2825         }
2826         case CTL_ERROR_INJECT_DELETE: {
2827                 struct ctl_error_desc *delete_desc, *desc, *desc2;
2828                 struct ctl_lun *lun;
2829                 int delete_done;
2830
2831                 delete_desc = (struct ctl_error_desc *)addr;
2832                 delete_done = 0;
2833
2834                 mtx_lock(&softc->ctl_lock);
2835                 lun = softc->ctl_luns[delete_desc->lun_id];
2836                 if (lun == NULL) {
2837                         mtx_unlock(&softc->ctl_lock);
2838                         printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2839                                __func__, (uintmax_t)delete_desc->lun_id);
2840                         retval = EINVAL;
2841                         break;
2842                 }
2843                 mtx_lock(&lun->lun_lock);
2844                 mtx_unlock(&softc->ctl_lock);
2845                 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2846                         if (desc->serial != delete_desc->serial)
2847                                 continue;
2848
2849                         STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2850                                       links);
2851                         free(desc, M_CTL);
2852                         delete_done = 1;
2853                 }
2854                 mtx_unlock(&lun->lun_lock);
2855                 if (delete_done == 0) {
2856                         printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2857                                "error serial %ju on LUN %u\n", __func__, 
2858                                delete_desc->serial, delete_desc->lun_id);
2859                         retval = EINVAL;
2860                         break;
2861                 }
2862                 break;
2863         }
2864         case CTL_DUMP_STRUCTS: {
2865                 int i, j, k;
2866                 struct ctl_frontend *fe;
2867
2868                 printf("CTL IID to WWPN map start:\n");
2869                 for (i = 0; i < CTL_MAX_PORTS; i++) {
2870                         for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2871                                 if (softc->wwpn_iid[i][j].in_use == 0)
2872                                         continue;
2873
2874                                 printf("port %d iid %u WWPN %#jx\n",
2875                                        softc->wwpn_iid[i][j].port,
2876                                        softc->wwpn_iid[i][j].iid, 
2877                                        (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2878                         }
2879                 }
2880                 printf("CTL IID to WWPN map end\n");
2881                 printf("CTL Persistent Reservation information start:\n");
2882                 for (i = 0; i < CTL_MAX_LUNS; i++) {
2883                         struct ctl_lun *lun;
2884
2885                         lun = softc->ctl_luns[i];
2886
2887                         if ((lun == NULL)
2888                          || ((lun->flags & CTL_LUN_DISABLED) != 0))
2889                                 continue;
2890
2891                         for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2892                                 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2893                                         if (lun->per_res[j+k].registered == 0)
2894                                                 continue;
2895                                         printf("LUN %d port %d iid %d key "
2896                                                "%#jx\n", i, j, k,
2897                                                (uintmax_t)scsi_8btou64(
2898                                                lun->per_res[j+k].res_key.key));
2899                                 }
2900                         }
2901                 }
2902                 printf("CTL Persistent Reservation information end\n");
2903                 printf("CTL Frontends:\n");
2904                 /*
2905                  * XXX KDM calling this without a lock.  We'd likely want
2906                  * to drop the lock before calling the frontend's dump
2907                  * routine anyway.
2908                  */
2909                 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2910                         printf("Frontend %s Type %u pport %d vport %d WWNN "
2911                                "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2912                                fe->physical_port, fe->virtual_port,
2913                                (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2914
2915                         /*
2916                          * Frontends are not required to support the dump
2917                          * routine.
2918                          */
2919                         if (fe->fe_dump == NULL)
2920                                 continue;
2921
2922                         fe->fe_dump();
2923                 }
2924                 printf("CTL Frontend information end\n");
2925                 break;
2926         }
2927         case CTL_LUN_REQ: {
2928                 struct ctl_lun_req *lun_req;
2929                 struct ctl_backend_driver *backend;
2930
2931                 lun_req = (struct ctl_lun_req *)addr;
2932
2933                 backend = ctl_backend_find(lun_req->backend);
2934                 if (backend == NULL) {
2935                         lun_req->status = CTL_LUN_ERROR;
2936                         snprintf(lun_req->error_str,
2937                                  sizeof(lun_req->error_str),
2938                                  "Backend \"%s\" not found.",
2939                                  lun_req->backend);
2940                         break;
2941                 }
2942                 if (lun_req->num_be_args > 0) {
2943                         lun_req->kern_be_args = ctl_copyin_args(
2944                                 lun_req->num_be_args,
2945                                 lun_req->be_args,
2946                                 lun_req->error_str,
2947                                 sizeof(lun_req->error_str));
2948                         if (lun_req->kern_be_args == NULL) {
2949                                 lun_req->status = CTL_LUN_ERROR;
2950                                 break;
2951                         }
2952                 }
2953
2954                 retval = backend->ioctl(dev, cmd, addr, flag, td);
2955
2956                 if (lun_req->num_be_args > 0) {
2957                         ctl_free_args(lun_req->num_be_args,
2958                                       lun_req->kern_be_args);
2959                 }
2960                 break;
2961         }
2962         case CTL_LUN_LIST: {
2963                 struct sbuf *sb;
2964                 struct ctl_lun *lun;
2965                 struct ctl_lun_list *list;
2966                 struct ctl_be_lun_option *opt;
2967
2968                 list = (struct ctl_lun_list *)addr;
2969
2970                 /*
2971                  * Allocate a fixed length sbuf here, based on the length
2972                  * of the user's buffer.  We could allocate an auto-extending
2973                  * buffer, and then tell the user how much larger our
2974                  * amount of data is than his buffer, but that presents
2975                  * some problems:
2976                  *
2977                  * 1.  The sbuf(9) routines use a blocking malloc, and so
2978                  *     we can't hold a lock while calling them with an
2979                  *     auto-extending buffer.
2980                  *
2981                  * 2.  There is not currently a LUN reference counting
2982                  *     mechanism, outside of outstanding transactions on
2983                  *     the LUN's OOA queue.  So a LUN could go away on us
2984                  *     while we're getting the LUN number, backend-specific
2985                  *     information, etc.  Thus, given the way things
2986                  *     currently work, we need to hold the CTL lock while
2987                  *     grabbing LUN information.
2988                  *
2989                  * So, from the user's standpoint, the best thing to do is
2990                  * allocate what he thinks is a reasonable buffer length,
2991                  * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2992                  * double the buffer length and try again.  (And repeat
2993                  * that until he succeeds.)
2994                  */
2995                 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2996                 if (sb == NULL) {
2997                         list->status = CTL_LUN_LIST_ERROR;
2998                         snprintf(list->error_str, sizeof(list->error_str),
2999                                  "Unable to allocate %d bytes for LUN list",
3000                                  list->alloc_len);
3001                         break;
3002                 }
3003
3004                 sbuf_printf(sb, "<ctllunlist>\n");
3005
3006                 mtx_lock(&softc->ctl_lock);
3007                 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3008                         mtx_lock(&lun->lun_lock);
3009                         retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3010                                              (uintmax_t)lun->lun);
3011
3012                         /*
3013                          * Bail out as soon as we see that we've overfilled
3014                          * the buffer.
3015                          */
3016                         if (retval != 0)
3017                                 break;
3018
3019                         retval = sbuf_printf(sb, "<backend_type>%s"
3020                                              "</backend_type>\n",
3021                                              (lun->backend == NULL) ?  "none" :
3022                                              lun->backend->name);
3023
3024                         if (retval != 0)
3025                                 break;
3026
3027                         retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3028                                              lun->be_lun->lun_type);
3029
3030                         if (retval != 0)
3031                                 break;
3032
3033                         if (lun->backend == NULL) {
3034                                 retval = sbuf_printf(sb, "</lun>\n");
3035                                 if (retval != 0)
3036                                         break;
3037                                 continue;
3038                         }
3039
3040                         retval = sbuf_printf(sb, "<size>%ju</size>\n",
3041                                              (lun->be_lun->maxlba > 0) ?
3042                                              lun->be_lun->maxlba + 1 : 0);
3043
3044                         if (retval != 0)
3045                                 break;
3046
3047                         retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3048                                              lun->be_lun->blocksize);
3049
3050                         if (retval != 0)
3051                                 break;
3052
3053                         retval = sbuf_printf(sb, "<serial_number>");
3054
3055                         if (retval != 0)
3056                                 break;
3057
3058                         retval = ctl_sbuf_printf_esc(sb,
3059                                                      lun->be_lun->serial_num);
3060
3061                         if (retval != 0)
3062                                 break;
3063
3064                         retval = sbuf_printf(sb, "</serial_number>\n");
3065                 
3066                         if (retval != 0)
3067                                 break;
3068
3069                         retval = sbuf_printf(sb, "<device_id>");
3070
3071                         if (retval != 0)
3072                                 break;
3073
3074                         retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3075
3076                         if (retval != 0)
3077                                 break;
3078
3079                         retval = sbuf_printf(sb, "</device_id>\n");
3080
3081                         if (retval != 0)
3082                                 break;
3083
3084                         if (lun->backend->lun_info != NULL) {
3085                                 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3086                                 if (retval != 0)
3087                                         break;
3088                         }
3089                         STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3090                                 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name);
3091                                 if (retval != 0)
3092                                         break;
3093                         }
3094
3095                         retval = sbuf_printf(sb, "</lun>\n");
3096
3097                         if (retval != 0)
3098                                 break;
3099                         mtx_unlock(&lun->lun_lock);
3100                 }
3101                 if (lun != NULL)
3102                         mtx_unlock(&lun->lun_lock);
3103                 mtx_unlock(&softc->ctl_lock);
3104
3105                 if ((retval != 0)
3106                  || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3107                         retval = 0;
3108                         sbuf_delete(sb);
3109                         list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3110                         snprintf(list->error_str, sizeof(list->error_str),
3111                                  "Out of space, %d bytes is too small",
3112                                  list->alloc_len);
3113                         break;
3114                 }
3115
3116                 sbuf_finish(sb);
3117
3118                 retval = copyout(sbuf_data(sb), list->lun_xml,
3119                                  sbuf_len(sb) + 1);
3120
3121                 list->fill_len = sbuf_len(sb) + 1;
3122                 list->status = CTL_LUN_LIST_OK;
3123                 sbuf_delete(sb);
3124                 break;
3125         }
3126         case CTL_ISCSI: {
3127                 struct ctl_iscsi *ci;
3128                 struct ctl_frontend *fe;
3129
3130                 ci = (struct ctl_iscsi *)addr;
3131
3132                 mtx_lock(&softc->ctl_lock);
3133                 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3134                         if (strcmp(fe->port_name, "iscsi") == 0)
3135                                 break;
3136                 }
3137                 mtx_unlock(&softc->ctl_lock);
3138
3139                 if (fe == NULL) {
3140                         ci->status = CTL_ISCSI_ERROR;
3141                         snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found.");
3142                         break;
3143                 }
3144
3145                 retval = fe->ioctl(dev, cmd, addr, flag, td);
3146                 break;
3147         }
3148         default: {
3149                 /* XXX KDM should we fix this? */
3150 #if 0
3151                 struct ctl_backend_driver *backend;
3152                 unsigned int type;
3153                 int found;
3154
3155                 found = 0;
3156
3157                 /*
3158                  * We encode the backend type as the ioctl type for backend
3159                  * ioctls.  So parse it out here, and then search for a
3160                  * backend of this type.
3161                  */
3162                 type = _IOC_TYPE(cmd);
3163
3164                 STAILQ_FOREACH(backend, &softc->be_list, links) {
3165                         if (backend->type == type) {
3166                                 found = 1;
3167                                 break;
3168                         }
3169                 }
3170                 if (found == 0) {
3171                         printf("ctl: unknown ioctl command %#lx or backend "
3172                                "%d\n", cmd, type);
3173                         retval = EINVAL;
3174                         break;
3175                 }
3176                 retval = backend->ioctl(dev, cmd, addr, flag, td);
3177 #endif
3178                 retval = ENOTTY;
3179                 break;
3180         }
3181         }
3182         return (retval);
3183 }
3184
3185 uint32_t
3186 ctl_get_initindex(struct ctl_nexus *nexus)
3187 {
3188         if (nexus->targ_port < CTL_MAX_PORTS)
3189                 return (nexus->initid.id +
3190                         (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3191         else
3192                 return (nexus->initid.id +
3193                        ((nexus->targ_port - CTL_MAX_PORTS) *
3194                         CTL_MAX_INIT_PER_PORT));
3195 }
3196
3197 uint32_t
3198 ctl_get_resindex(struct ctl_nexus *nexus)
3199 {
3200         return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3201 }
3202
3203 uint32_t
3204 ctl_port_idx(int port_num)
3205 {
3206         if (port_num < CTL_MAX_PORTS)
3207                 return(port_num);
3208         else
3209                 return(port_num - CTL_MAX_PORTS);
3210 }
3211
3212 /*
3213  * Note:  This only works for bitmask sizes that are at least 32 bits, and
3214  * that are a power of 2.
3215  */
3216 int
3217 ctl_ffz(uint32_t *mask, uint32_t size)
3218 {
3219         uint32_t num_chunks, num_pieces;
3220         int i, j;
3221
3222         num_chunks = (size >> 5);
3223         if (num_chunks == 0)
3224                 num_chunks++;
3225         num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3226
3227         for (i = 0; i < num_chunks; i++) {
3228                 for (j = 0; j < num_pieces; j++) {
3229                         if ((mask[i] & (1 << j)) == 0)
3230                                 return ((i << 5) + j);
3231                 }
3232         }
3233
3234         return (-1);
3235 }
3236
3237 int
3238 ctl_set_mask(uint32_t *mask, uint32_t bit)
3239 {
3240         uint32_t chunk, piece;
3241
3242         chunk = bit >> 5;
3243         piece = bit % (sizeof(uint32_t) * 8);
3244
3245         if ((mask[chunk] & (1 << piece)) != 0)
3246                 return (-1);
3247         else
3248                 mask[chunk] |= (1 << piece);
3249
3250         return (0);
3251 }
3252
3253 int
3254 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3255 {
3256         uint32_t chunk, piece;
3257
3258         chunk = bit >> 5;
3259         piece = bit % (sizeof(uint32_t) * 8);
3260
3261         if ((mask[chunk] & (1 << piece)) == 0)
3262                 return (-1);
3263         else
3264                 mask[chunk] &= ~(1 << piece);
3265
3266         return (0);
3267 }
3268
3269 int
3270 ctl_is_set(uint32_t *mask, uint32_t bit)
3271 {
3272         uint32_t chunk, piece;
3273
3274         chunk = bit >> 5;
3275         piece = bit % (sizeof(uint32_t) * 8);
3276
3277         if ((mask[chunk] & (1 << piece)) == 0)
3278                 return (0);
3279         else
3280                 return (1);
3281 }
3282
3283 #ifdef unused
3284 /*
3285  * The bus, target and lun are optional, they can be filled in later.
3286  * can_wait is used to determine whether we can wait on the malloc or not.
3287  */
3288 union ctl_io*
3289 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3290               uint32_t targ_lun, int can_wait)
3291 {
3292         union ctl_io *io;
3293
3294         if (can_wait)
3295                 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3296         else
3297                 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3298
3299         if (io != NULL) {
3300                 io->io_hdr.io_type = io_type;
3301                 io->io_hdr.targ_port = targ_port;
3302                 /*
3303                  * XXX KDM this needs to change/go away.  We need to move
3304                  * to a preallocated pool of ctl_scsiio structures.
3305                  */
3306                 io->io_hdr.nexus.targ_target.id = targ_target;
3307                 io->io_hdr.nexus.targ_lun = targ_lun;
3308         }
3309
3310         return (io);
3311 }
3312
3313 void
3314 ctl_kfree_io(union ctl_io *io)
3315 {
3316         free(io, M_CTL);
3317 }
3318 #endif /* unused */
3319
3320 /*
3321  * ctl_softc, pool_type, total_ctl_io are passed in.
3322  * npool is passed out.
3323  */
3324 int
3325 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3326                 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3327 {
3328         uint32_t i;
3329         union ctl_io *cur_io, *next_io;
3330         struct ctl_io_pool *pool;
3331         int retval;
3332
3333         retval = 0;
3334
3335         pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3336                                             M_NOWAIT | M_ZERO);
3337         if (pool == NULL) {
3338                 retval = ENOMEM;
3339                 goto bailout;
3340         }
3341
3342         pool->type = pool_type;
3343         pool->ctl_softc = ctl_softc;
3344
3345         mtx_lock(&ctl_softc->pool_lock);
3346         pool->id = ctl_softc->cur_pool_id++;
3347         mtx_unlock(&ctl_softc->pool_lock);
3348
3349         pool->flags = CTL_POOL_FLAG_NONE;
3350         pool->refcount = 1;             /* Reference for validity. */
3351         STAILQ_INIT(&pool->free_queue);
3352
3353         /*
3354          * XXX KDM other options here:
3355          * - allocate a page at a time
3356          * - allocate one big chunk of memory.
3357          * Page allocation might work well, but would take a little more
3358          * tracking.
3359          */
3360         for (i = 0; i < total_ctl_io; i++) {
3361                 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3362                                                 M_NOWAIT);
3363                 if (cur_io == NULL) {
3364                         retval = ENOMEM;
3365                         break;
3366                 }
3367                 cur_io->io_hdr.pool = pool;
3368                 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3369                 pool->total_ctl_io++;
3370                 pool->free_ctl_io++;
3371         }
3372
3373         if (retval != 0) {
3374                 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3375                      cur_io != NULL; cur_io = next_io) {
3376                         next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3377                                                               links);
3378                         STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3379                                       ctl_io_hdr, links);
3380                         free(cur_io, M_CTL);
3381                 }
3382
3383                 free(pool, M_CTL);
3384                 goto bailout;
3385         }
3386         mtx_lock(&ctl_softc->pool_lock);
3387         ctl_softc->num_pools++;
3388         STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3389         /*
3390          * Increment our usage count if this is an external consumer, so we
3391          * can't get unloaded until the external consumer (most likely a
3392          * FETD) unloads and frees his pool.
3393          *
3394          * XXX KDM will this increment the caller's module use count, or
3395          * mine?
3396          */
3397 #if 0
3398         if ((pool_type != CTL_POOL_EMERGENCY)
3399          && (pool_type != CTL_POOL_INTERNAL)
3400          && (pool_type != CTL_POOL_IOCTL)
3401          && (pool_type != CTL_POOL_4OTHERSC))
3402                 MOD_INC_USE_COUNT;
3403 #endif
3404
3405         mtx_unlock(&ctl_softc->pool_lock);
3406
3407         *npool = pool;
3408
3409 bailout:
3410
3411         return (retval);
3412 }
3413
3414 static int
3415 ctl_pool_acquire(struct ctl_io_pool *pool)
3416 {
3417
3418         mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3419
3420         if (pool->flags & CTL_POOL_FLAG_INVALID)
3421                 return (EINVAL);
3422
3423         pool->refcount++;
3424
3425         return (0);
3426 }
3427
3428 static void
3429 ctl_pool_release(struct ctl_io_pool *pool)
3430 {
3431         struct ctl_softc *ctl_softc = pool->ctl_softc;
3432         union ctl_io *io;
3433
3434         mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3435
3436         if (--pool->refcount != 0)
3437                 return;
3438
3439         while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3440                 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3441                               links);
3442                 free(io, M_CTL);
3443         }
3444
3445         STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3446         ctl_softc->num_pools--;
3447
3448         /*
3449          * XXX KDM will this decrement the caller's usage count or mine?
3450          */
3451 #if 0
3452         if ((pool->type != CTL_POOL_EMERGENCY)
3453          && (pool->type != CTL_POOL_INTERNAL)
3454          && (pool->type != CTL_POOL_IOCTL))
3455                 MOD_DEC_USE_COUNT;
3456 #endif
3457
3458         free(pool, M_CTL);
3459 }
3460
3461 void
3462 ctl_pool_free(struct ctl_io_pool *pool)
3463 {
3464         struct ctl_softc *ctl_softc;
3465
3466         if (pool == NULL)
3467                 return;
3468
3469         ctl_softc = pool->ctl_softc;
3470         mtx_lock(&ctl_softc->pool_lock);
3471         pool->flags |= CTL_POOL_FLAG_INVALID;
3472         ctl_pool_release(pool);
3473         mtx_unlock(&ctl_softc->pool_lock);
3474 }
3475
3476 /*
3477  * This routine does not block (except for spinlocks of course).
3478  * It tries to allocate a ctl_io union from the caller's pool as quickly as
3479  * possible.
3480  */
3481 union ctl_io *
3482 ctl_alloc_io(void *pool_ref)
3483 {
3484         union ctl_io *io;
3485         struct ctl_softc *ctl_softc;
3486         struct ctl_io_pool *pool, *npool;
3487         struct ctl_io_pool *emergency_pool;
3488
3489         pool = (struct ctl_io_pool *)pool_ref;
3490
3491         if (pool == NULL) {
3492                 printf("%s: pool is NULL\n", __func__);
3493                 return (NULL);
3494         }
3495
3496         emergency_pool = NULL;
3497
3498         ctl_softc = pool->ctl_softc;
3499
3500         mtx_lock(&ctl_softc->pool_lock);
3501         /*
3502          * First, try to get the io structure from the user's pool.
3503          */
3504         if (ctl_pool_acquire(pool) == 0) {
3505                 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3506                 if (io != NULL) {
3507                         STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3508                         pool->total_allocated++;
3509                         pool->free_ctl_io--;
3510                         mtx_unlock(&ctl_softc->pool_lock);
3511                         return (io);
3512                 } else
3513                         ctl_pool_release(pool);
3514         }
3515         /*
3516          * If he doesn't have any io structures left, search for an
3517          * emergency pool and grab one from there.
3518          */
3519         STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3520                 if (npool->type != CTL_POOL_EMERGENCY)
3521                         continue;
3522
3523                 if (ctl_pool_acquire(npool) != 0)
3524                         continue;
3525
3526                 emergency_pool = npool;
3527
3528                 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3529                 if (io != NULL) {
3530                         STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3531                         npool->total_allocated++;
3532                         npool->free_ctl_io--;
3533                         mtx_unlock(&ctl_softc->pool_lock);
3534                         return (io);
3535                 } else
3536                         ctl_pool_release(npool);
3537         }
3538
3539         /* Drop the spinlock before we malloc */
3540         mtx_unlock(&ctl_softc->pool_lock);
3541
3542         /*
3543          * The emergency pool (if it exists) didn't have one, so try an
3544          * atomic (i.e. nonblocking) malloc and see if we get lucky.
3545          */
3546         io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3547         if (io != NULL) {
3548                 /*
3549                  * If the emergency pool exists but is empty, add this
3550                  * ctl_io to its list when it gets freed.
3551                  */
3552                 if (emergency_pool != NULL) {
3553                         mtx_lock(&ctl_softc->pool_lock);
3554                         if (ctl_pool_acquire(emergency_pool) == 0) {
3555                                 io->io_hdr.pool = emergency_pool;
3556                                 emergency_pool->total_ctl_io++;
3557                                 /*
3558                                  * Need to bump this, otherwise
3559                                  * total_allocated and total_freed won't
3560                                  * match when we no longer have anything
3561                                  * outstanding.
3562                                  */
3563                                 emergency_pool->total_allocated++;
3564                         }
3565                         mtx_unlock(&ctl_softc->pool_lock);
3566                 } else
3567                         io->io_hdr.pool = NULL;
3568         }
3569
3570         return (io);
3571 }
3572
3573 void
3574 ctl_free_io(union ctl_io *io)
3575 {
3576         if (io == NULL)
3577                 return;
3578
3579         /*
3580          * If this ctl_io has a pool, return it to that pool.
3581          */
3582         if (io->io_hdr.pool != NULL) {
3583                 struct ctl_io_pool *pool;
3584
3585                 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3586                 mtx_lock(&pool->ctl_softc->pool_lock);
3587                 io->io_hdr.io_type = 0xff;
3588                 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3589                 pool->total_freed++;
3590                 pool->free_ctl_io++;
3591                 ctl_pool_release(pool);
3592                 mtx_unlock(&pool->ctl_softc->pool_lock);
3593         } else {
3594                 /*
3595                  * Otherwise, just free it.  We probably malloced it and
3596                  * the emergency pool wasn't available.
3597                  */
3598                 free(io, M_CTL);
3599         }
3600
3601 }
3602
3603 void
3604 ctl_zero_io(union ctl_io *io)
3605 {
3606         void *pool_ref;
3607
3608         if (io == NULL)
3609                 return;
3610
3611         /*
3612          * May need to preserve linked list pointers at some point too.
3613          */
3614         pool_ref = io->io_hdr.pool;
3615
3616         memset(io, 0, sizeof(*io));
3617
3618         io->io_hdr.pool = pool_ref;
3619 }
3620
3621 /*
3622  * This routine is currently used for internal copies of ctl_ios that need
3623  * to persist for some reason after we've already returned status to the
3624  * FETD.  (Thus the flag set.)
3625  *
3626  * XXX XXX
3627  * Note that this makes a blind copy of all fields in the ctl_io, except
3628  * for the pool reference.  This includes any memory that has been
3629  * allocated!  That memory will no longer be valid after done has been
3630  * called, so this would be VERY DANGEROUS for command that actually does
3631  * any reads or writes.  Right now (11/7/2005), this is only used for immediate
3632  * start and stop commands, which don't transfer any data, so this is not a
3633  * problem.  If it is used for anything else, the caller would also need to
3634  * allocate data buffer space and this routine would need to be modified to
3635  * copy the data buffer(s) as well.
3636  */
3637 void
3638 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3639 {
3640         void *pool_ref;
3641
3642         if ((src == NULL)
3643          || (dest == NULL))
3644                 return;
3645
3646         /*
3647          * May need to preserve linked list pointers at some point too.
3648          */
3649         pool_ref = dest->io_hdr.pool;
3650
3651         memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3652
3653         dest->io_hdr.pool = pool_ref;
3654         /*
3655          * We need to know that this is an internal copy, and doesn't need
3656          * to get passed back to the FETD that allocated it.
3657          */
3658         dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3659 }
3660
3661 #ifdef NEEDTOPORT
3662 static void
3663 ctl_update_power_subpage(struct copan_power_subpage *page)
3664 {
3665         int num_luns, num_partitions, config_type;
3666         struct ctl_softc *softc;
3667         cs_BOOL_t aor_present, shelf_50pct_power;
3668         cs_raidset_personality_t rs_type;
3669         int max_active_luns;
3670
3671         softc = control_softc;
3672
3673         /* subtract out the processor LUN */
3674         num_luns = softc->num_luns - 1;
3675         /*
3676          * Default to 7 LUNs active, which was the only number we allowed
3677          * in the past.
3678          */
3679         max_active_luns = 7;
3680
3681         num_partitions = config_GetRsPartitionInfo();
3682         config_type = config_GetConfigType();
3683         shelf_50pct_power = config_GetShelfPowerMode();
3684         aor_present = config_IsAorRsPresent();
3685
3686         rs_type = ddb_GetRsRaidType(1);
3687         if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3688          && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3689                 EPRINT(0, "Unsupported RS type %d!", rs_type);
3690         }
3691
3692
3693         page->total_luns = num_luns;
3694
3695         switch (config_type) {
3696         case 40:
3697                 /*
3698                  * In a 40 drive configuration, it doesn't matter what DC
3699                  * cards we have, whether we have AOR enabled or not,
3700                  * partitioning or not, or what type of RAIDset we have.
3701                  * In that scenario, we can power up every LUN we present
3702                  * to the user.
3703                  */
3704                 max_active_luns = num_luns;
3705
3706                 break;
3707         case 64:
3708                 if (shelf_50pct_power == CS_FALSE) {
3709                         /* 25% power */
3710                         if (aor_present == CS_TRUE) {
3711                                 if (rs_type ==
3712                                      CS_RAIDSET_PERSONALITY_RAID5) {
3713                                         max_active_luns = 7;
3714                                 } else if (rs_type ==
3715                                          CS_RAIDSET_PERSONALITY_RAID1){
3716                                         max_active_luns = 14;
3717                                 } else {
3718                                         /* XXX KDM now what?? */
3719                                 }
3720                         } else {
3721                                 if (rs_type ==
3722                                      CS_RAIDSET_PERSONALITY_RAID5) {
3723                                         max_active_luns = 8;
3724                                 } else if (rs_type ==
3725                                          CS_RAIDSET_PERSONALITY_RAID1){
3726                                         max_active_luns = 16;
3727                                 } else {
3728                                         /* XXX KDM now what?? */
3729                                 }
3730                         }
3731                 } else {
3732                         /* 50% power */
3733                         /*
3734                          * With 50% power in a 64 drive configuration, we
3735                          * can power all LUNs we present.
3736                          */
3737                         max_active_luns = num_luns;
3738                 }
3739                 break;
3740         case 112:
3741                 if (shelf_50pct_power == CS_FALSE) {
3742                         /* 25% power */
3743                         if (aor_present == CS_TRUE) {
3744                                 if (rs_type ==
3745                                      CS_RAIDSET_PERSONALITY_RAID5) {
3746                                         max_active_luns = 7;
3747                                 } else if (rs_type ==
3748                                          CS_RAIDSET_PERSONALITY_RAID1){
3749                                         max_active_luns = 14;
3750                                 } else {
3751                                         /* XXX KDM now what?? */
3752                                 }
3753                         } else {
3754                                 if (rs_type ==
3755                                      CS_RAIDSET_PERSONALITY_RAID5) {
3756                                         max_active_luns = 8;
3757                                 } else if (rs_type ==
3758                                          CS_RAIDSET_PERSONALITY_RAID1){
3759                                         max_active_luns = 16;
3760                                 } else {
3761                                         /* XXX KDM now what?? */
3762                                 }
3763                         }
3764                 } else {
3765                         /* 50% power */
3766                         if (aor_present == CS_TRUE) {
3767                                 if (rs_type ==
3768                                      CS_RAIDSET_PERSONALITY_RAID5) {
3769                                         max_active_luns = 14;
3770                                 } else if (rs_type ==
3771                                          CS_RAIDSET_PERSONALITY_RAID1){
3772                                         /*
3773                                          * We're assuming here that disk
3774                                          * caching is enabled, and so we're
3775                                          * able to power up half of each
3776                                          * LUN, and cache all writes.
3777                                          */
3778                                         max_active_luns = num_luns;
3779                                 } else {
3780                                         /* XXX KDM now what?? */
3781                                 }
3782                         } else {
3783                                 if (rs_type ==
3784                                      CS_RAIDSET_PERSONALITY_RAID5) {
3785                                         max_active_luns = 15;
3786                                 } else if (rs_type ==
3787                                          CS_RAIDSET_PERSONALITY_RAID1){
3788                                         max_active_luns = 30;
3789                                 } else {
3790                                         /* XXX KDM now what?? */
3791                                 }
3792                         }
3793                 }
3794                 break;
3795         default:
3796                 /*
3797                  * In this case, we have an unknown configuration, so we
3798                  * just use the default from above.
3799                  */
3800                 break;
3801         }
3802
3803         page->max_active_luns = max_active_luns;
3804 #if 0
3805         printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3806                page->total_luns, page->max_active_luns);
3807 #endif
3808 }
3809 #endif /* NEEDTOPORT */
3810
3811 /*
3812  * This routine could be used in the future to load default and/or saved
3813  * mode page parameters for a particuar lun.
3814  */
3815 static int
3816 ctl_init_page_index(struct ctl_lun *lun)
3817 {
3818         int i;
3819         struct ctl_page_index *page_index;
3820         struct ctl_softc *softc;
3821
3822         memcpy(&lun->mode_pages.index, page_index_template,
3823                sizeof(page_index_template));
3824
3825         softc = lun->ctl_softc;
3826
3827         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3828
3829                 page_index = &lun->mode_pages.index[i];
3830                 /*
3831                  * If this is a disk-only mode page, there's no point in
3832                  * setting it up.  For some pages, we have to have some
3833                  * basic information about the disk in order to calculate the
3834                  * mode page data.
3835                  */
3836                 if ((lun->be_lun->lun_type != T_DIRECT)
3837                  && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3838                         continue;
3839
3840                 switch (page_index->page_code & SMPH_PC_MASK) {
3841                 case SMS_FORMAT_DEVICE_PAGE: {
3842                         struct scsi_format_page *format_page;
3843
3844                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3845                                 panic("subpage is incorrect!");
3846
3847                         /*
3848                          * Sectors per track are set above.  Bytes per
3849                          * sector need to be set here on a per-LUN basis.
3850                          */
3851                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3852                                &format_page_default,
3853                                sizeof(format_page_default));
3854                         memcpy(&lun->mode_pages.format_page[
3855                                CTL_PAGE_CHANGEABLE], &format_page_changeable,
3856                                sizeof(format_page_changeable));
3857                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3858                                &format_page_default,
3859                                sizeof(format_page_default));
3860                         memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3861                                &format_page_default,
3862                                sizeof(format_page_default));
3863
3864                         format_page = &lun->mode_pages.format_page[
3865                                 CTL_PAGE_CURRENT];
3866                         scsi_ulto2b(lun->be_lun->blocksize,
3867                                     format_page->bytes_per_sector);
3868
3869                         format_page = &lun->mode_pages.format_page[
3870                                 CTL_PAGE_DEFAULT];
3871                         scsi_ulto2b(lun->be_lun->blocksize,
3872                                     format_page->bytes_per_sector);
3873
3874                         format_page = &lun->mode_pages.format_page[
3875                                 CTL_PAGE_SAVED];
3876                         scsi_ulto2b(lun->be_lun->blocksize,
3877                                     format_page->bytes_per_sector);
3878
3879                         page_index->page_data =
3880                                 (uint8_t *)lun->mode_pages.format_page;
3881                         break;
3882                 }
3883                 case SMS_RIGID_DISK_PAGE: {
3884                         struct scsi_rigid_disk_page *rigid_disk_page;
3885                         uint32_t sectors_per_cylinder;
3886                         uint64_t cylinders;
3887 #ifndef __XSCALE__
3888                         int shift;
3889 #endif /* !__XSCALE__ */
3890
3891                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3892                                 panic("invalid subpage value %d",
3893                                       page_index->subpage);
3894
3895                         /*
3896                          * Rotation rate and sectors per track are set
3897                          * above.  We calculate the cylinders here based on
3898                          * capacity.  Due to the number of heads and
3899                          * sectors per track we're using, smaller arrays
3900                          * may turn out to have 0 cylinders.  Linux and
3901                          * FreeBSD don't pay attention to these mode pages
3902                          * to figure out capacity, but Solaris does.  It
3903                          * seems to deal with 0 cylinders just fine, and
3904                          * works out a fake geometry based on the capacity.
3905                          */
3906                         memcpy(&lun->mode_pages.rigid_disk_page[
3907                                CTL_PAGE_CURRENT], &rigid_disk_page_default,
3908                                sizeof(rigid_disk_page_default));
3909                         memcpy(&lun->mode_pages.rigid_disk_page[
3910                                CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3911                                sizeof(rigid_disk_page_changeable));
3912                         memcpy(&lun->mode_pages.rigid_disk_page[
3913                                CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3914                                sizeof(rigid_disk_page_default));
3915                         memcpy(&lun->mode_pages.rigid_disk_page[
3916                                CTL_PAGE_SAVED], &rigid_disk_page_default,
3917                                sizeof(rigid_disk_page_default));
3918
3919                         sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3920                                 CTL_DEFAULT_HEADS;
3921
3922                         /*
3923                          * The divide method here will be more accurate,
3924                          * probably, but results in floating point being
3925                          * used in the kernel on i386 (__udivdi3()).  On the
3926                          * XScale, though, __udivdi3() is implemented in
3927                          * software.
3928                          *
3929                          * The shift method for cylinder calculation is
3930                          * accurate if sectors_per_cylinder is a power of
3931                          * 2.  Otherwise it might be slightly off -- you
3932                          * might have a bit of a truncation problem.
3933                          */
3934 #ifdef  __XSCALE__
3935                         cylinders = (lun->be_lun->maxlba + 1) /
3936                                 sectors_per_cylinder;
3937 #else
3938                         for (shift = 31; shift > 0; shift--) {
3939                                 if (sectors_per_cylinder & (1 << shift))
3940                                         break;
3941                         }
3942                         cylinders = (lun->be_lun->maxlba + 1) >> shift;
3943 #endif
3944
3945                         /*
3946                          * We've basically got 3 bytes, or 24 bits for the
3947                          * cylinder size in the mode page.  If we're over,
3948                          * just round down to 2^24.
3949                          */
3950                         if (cylinders > 0xffffff)
3951                                 cylinders = 0xffffff;
3952
3953                         rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3954                                 CTL_PAGE_CURRENT];
3955                         scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3956
3957                         rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3958                                 CTL_PAGE_DEFAULT];
3959                         scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3960
3961                         rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3962                                 CTL_PAGE_SAVED];
3963                         scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3964
3965                         page_index->page_data =
3966                                 (uint8_t *)lun->mode_pages.rigid_disk_page;
3967                         break;
3968                 }
3969                 case SMS_CACHING_PAGE: {
3970
3971                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3972                                 panic("invalid subpage value %d",
3973                                       page_index->subpage);
3974                         /*
3975                          * Defaults should be okay here, no calculations
3976                          * needed.
3977                          */
3978                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3979                                &caching_page_default,
3980                                sizeof(caching_page_default));
3981                         memcpy(&lun->mode_pages.caching_page[
3982                                CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3983                                sizeof(caching_page_changeable));
3984                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3985                                &caching_page_default,
3986                                sizeof(caching_page_default));
3987                         memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3988                                &caching_page_default,
3989                                sizeof(caching_page_default));
3990                         page_index->page_data =
3991                                 (uint8_t *)lun->mode_pages.caching_page;
3992                         break;
3993                 }
3994                 case SMS_CONTROL_MODE_PAGE: {
3995
3996                         if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3997                                 panic("invalid subpage value %d",
3998                                       page_index->subpage);
3999
4000                         /*
4001                          * Defaults should be okay here, no calculations
4002                          * needed.
4003                          */
4004                         memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4005                                &control_page_default,
4006                                sizeof(control_page_default));
4007                         memcpy(&lun->mode_pages.control_page[
4008                                CTL_PAGE_CHANGEABLE], &control_page_changeable,
4009                                sizeof(control_page_changeable));
4010                         memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4011                                &control_page_default,
4012                                sizeof(control_page_default));
4013                         memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4014                                &control_page_default,
4015                                sizeof(control_page_default));
4016                         page_index->page_data =
4017                                 (uint8_t *)lun->mode_pages.control_page;
4018                         break;
4019
4020                 }
4021                 case SMS_VENDOR_SPECIFIC_PAGE:{
4022                         switch (page_index->subpage) {
4023                         case PWR_SUBPAGE_CODE: {
4024                                 struct copan_power_subpage *current_page,
4025                                                            *saved_page;
4026
4027                                 memcpy(&lun->mode_pages.power_subpage[
4028                                        CTL_PAGE_CURRENT],
4029                                        &power_page_default,
4030                                        sizeof(power_page_default));
4031                                 memcpy(&lun->mode_pages.power_subpage[
4032                                        CTL_PAGE_CHANGEABLE],
4033                                        &power_page_changeable,
4034                                        sizeof(power_page_changeable));
4035                                 memcpy(&lun->mode_pages.power_subpage[
4036                                        CTL_PAGE_DEFAULT],
4037                                        &power_page_default,
4038                                        sizeof(power_page_default));
4039                                 memcpy(&lun->mode_pages.power_subpage[
4040                                        CTL_PAGE_SAVED],
4041                                        &power_page_default,
4042                                        sizeof(power_page_default));
4043                                 page_index->page_data =
4044                                     (uint8_t *)lun->mode_pages.power_subpage;
4045
4046                                 current_page = (struct copan_power_subpage *)
4047                                         (page_index->page_data +
4048                                          (page_index->page_len *
4049                                           CTL_PAGE_CURRENT));
4050                                 saved_page = (struct copan_power_subpage *)
4051                                         (page_index->page_data +
4052                                          (page_index->page_len *
4053                                           CTL_PAGE_SAVED));
4054                                 break;
4055                         }
4056                         case APS_SUBPAGE_CODE: {
4057                                 struct copan_aps_subpage *current_page,
4058                                                          *saved_page;
4059
4060                                 // This gets set multiple times but
4061                                 // it should always be the same. It's
4062                                 // only done during init so who cares.
4063                                 index_to_aps_page = i;
4064
4065                                 memcpy(&lun->mode_pages.aps_subpage[
4066                                        CTL_PAGE_CURRENT],
4067                                        &aps_page_default,
4068                                        sizeof(aps_page_default));
4069                                 memcpy(&lun->mode_pages.aps_subpage[
4070                                        CTL_PAGE_CHANGEABLE],
4071                                        &aps_page_changeable,
4072                                        sizeof(aps_page_changeable));
4073                                 memcpy(&lun->mode_pages.aps_subpage[
4074                                        CTL_PAGE_DEFAULT],
4075                                        &aps_page_default,
4076                                        sizeof(aps_page_default));
4077                                 memcpy(&lun->mode_pages.aps_subpage[
4078                                        CTL_PAGE_SAVED],
4079                                        &aps_page_default,
4080                                        sizeof(aps_page_default));
4081                                 page_index->page_data =
4082                                         (uint8_t *)lun->mode_pages.aps_subpage;
4083
4084                                 current_page = (struct copan_aps_subpage *)
4085                                         (page_index->page_data +
4086                                          (page_index->page_len *
4087                                           CTL_PAGE_CURRENT));
4088                                 saved_page = (struct copan_aps_subpage *)
4089                                         (page_index->page_data +
4090                                          (page_index->page_len *
4091                                           CTL_PAGE_SAVED));
4092                                 break;
4093                         }
4094                         case DBGCNF_SUBPAGE_CODE: {
4095                                 struct copan_debugconf_subpage *current_page,
4096                                                                *saved_page;
4097
4098                                 memcpy(&lun->mode_pages.debugconf_subpage[
4099                                        CTL_PAGE_CURRENT],
4100                                        &debugconf_page_default,
4101                                        sizeof(debugconf_page_default));
4102                                 memcpy(&lun->mode_pages.debugconf_subpage[
4103                                        CTL_PAGE_CHANGEABLE],
4104                                        &debugconf_page_changeable,
4105                                        sizeof(debugconf_page_changeable));
4106                                 memcpy(&lun->mode_pages.debugconf_subpage[
4107                                        CTL_PAGE_DEFAULT],
4108                                        &debugconf_page_default,
4109                                        sizeof(debugconf_page_default));
4110                                 memcpy(&lun->mode_pages.debugconf_subpage[
4111                                        CTL_PAGE_SAVED],
4112                                        &debugconf_page_default,
4113                                        sizeof(debugconf_page_default));
4114                                 page_index->page_data =
4115                                         (uint8_t *)lun->mode_pages.debugconf_subpage;
4116
4117                                 current_page = (struct copan_debugconf_subpage *)
4118                                         (page_index->page_data +
4119                                          (page_index->page_len *
4120                                           CTL_PAGE_CURRENT));
4121                                 saved_page = (struct copan_debugconf_subpage *)
4122                                         (page_index->page_data +
4123                                          (page_index->page_len *
4124                                           CTL_PAGE_SAVED));
4125                                 break;
4126                         }
4127                         default:
4128                                 panic("invalid subpage value %d",
4129                                       page_index->subpage);
4130                                 break;
4131                         }
4132                         break;
4133                 }
4134                 default:
4135                         panic("invalid page value %d",
4136                               page_index->page_code & SMPH_PC_MASK);
4137                         break;
4138         }
4139         }
4140
4141         return (CTL_RETVAL_COMPLETE);
4142 }
4143
4144 /*
4145  * LUN allocation.
4146  *
4147  * Requirements:
4148  * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4149  *   wants us to allocate the LUN and he can block.
4150  * - ctl_softc is always set
4151  * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4152  *
4153  * Returns 0 for success, non-zero (errno) for failure.
4154  */
4155 static int
4156 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4157               struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4158 {
4159         struct ctl_lun *nlun, *lun;
4160         struct ctl_frontend *fe;
4161         int lun_number, i, lun_malloced;
4162
4163         if (be_lun == NULL)
4164                 return (EINVAL);
4165
4166         /*
4167          * We currently only support Direct Access or Processor LUN types.
4168          */
4169         switch (be_lun->lun_type) {
4170         case T_DIRECT:
4171                 break;
4172         case T_PROCESSOR:
4173                 break;
4174         case T_SEQUENTIAL:
4175         case T_CHANGER:
4176         default:
4177                 be_lun->lun_config_status(be_lun->be_lun,
4178                                           CTL_LUN_CONFIG_FAILURE);
4179                 break;
4180         }
4181         if (ctl_lun == NULL) {
4182                 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4183                 lun_malloced = 1;
4184         } else {
4185                 lun_malloced = 0;
4186                 lun = ctl_lun;
4187         }
4188
4189         memset(lun, 0, sizeof(*lun));
4190         if (lun_malloced)
4191                 lun->flags = CTL_LUN_MALLOCED;
4192
4193         mtx_lock(&ctl_softc->ctl_lock);
4194         /*
4195          * See if the caller requested a particular LUN number.  If so, see
4196          * if it is available.  Otherwise, allocate the first available LUN.
4197          */
4198         if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4199                 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4200                  || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4201                         mtx_unlock(&ctl_softc->ctl_lock);
4202                         if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4203                                 printf("ctl: requested LUN ID %d is higher "
4204                                        "than CTL_MAX_LUNS - 1 (%d)\n",
4205                                        be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4206                         } else {
4207                                 /*
4208                                  * XXX KDM return an error, or just assign
4209                                  * another LUN ID in this case??
4210                                  */
4211                                 printf("ctl: requested LUN ID %d is already "
4212                                        "in use\n", be_lun->req_lun_id);
4213                         }
4214                         if (lun->flags & CTL_LUN_MALLOCED)
4215                                 free(lun, M_CTL);
4216                         be_lun->lun_config_status(be_lun->be_lun,
4217                                                   CTL_LUN_CONFIG_FAILURE);
4218                         return (ENOSPC);
4219                 }
4220                 lun_number = be_lun->req_lun_id;
4221         } else {
4222                 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4223                 if (lun_number == -1) {
4224                         mtx_unlock(&ctl_softc->ctl_lock);
4225                         printf("ctl: can't allocate LUN on target %ju, out of "
4226                                "LUNs\n", (uintmax_t)target_id.id);
4227                         if (lun->flags & CTL_LUN_MALLOCED)
4228                                 free(lun, M_CTL);
4229                         be_lun->lun_config_status(be_lun->be_lun,
4230                                                   CTL_LUN_CONFIG_FAILURE);
4231                         return (ENOSPC);
4232                 }
4233         }
4234         ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4235
4236         mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4237         lun->target = target_id;
4238         lun->lun = lun_number;
4239         lun->be_lun = be_lun;
4240         /*
4241          * The processor LUN is always enabled.  Disk LUNs come on line
4242          * disabled, and must be enabled by the backend.
4243          */
4244         lun->flags |= CTL_LUN_DISABLED;
4245         lun->backend = be_lun->be;
4246         be_lun->ctl_lun = lun;
4247         be_lun->lun_id = lun_number;
4248         atomic_add_int(&be_lun->be->num_luns, 1);
4249         if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4250                 lun->flags |= CTL_LUN_STOPPED;
4251
4252         if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4253                 lun->flags |= CTL_LUN_INOPERABLE;
4254
4255         if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4256                 lun->flags |= CTL_LUN_PRIMARY_SC;
4257
4258         lun->ctl_softc = ctl_softc;
4259         TAILQ_INIT(&lun->ooa_queue);
4260         TAILQ_INIT(&lun->blocked_queue);
4261         STAILQ_INIT(&lun->error_list);
4262
4263         /*
4264          * Initialize the mode page index.
4265          */
4266         ctl_init_page_index(lun);
4267
4268         /*
4269          * Set the poweron UA for all initiators on this LUN only.
4270          */
4271         for (i = 0; i < CTL_MAX_INITIATORS; i++)
4272                 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4273
4274         /*
4275          * Now, before we insert this lun on the lun list, set the lun
4276          * inventory changed UA for all other luns.
4277          */
4278         STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4279                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4280                         nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4281                 }
4282         }
4283
4284         STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4285
4286         ctl_softc->ctl_luns[lun_number] = lun;
4287
4288         ctl_softc->num_luns++;
4289
4290         /* Setup statistics gathering */
4291         lun->stats.device_type = be_lun->lun_type;
4292         lun->stats.lun_number = lun_number;
4293         if (lun->stats.device_type == T_DIRECT)
4294                 lun->stats.blocksize = be_lun->blocksize;
4295         else
4296                 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4297         for (i = 0;i < CTL_MAX_PORTS;i++)
4298                 lun->stats.ports[i].targ_port = i;
4299
4300         mtx_unlock(&ctl_softc->ctl_lock);
4301
4302         lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4303
4304         /*
4305          * Run through each registered FETD and bring it online if it isn't
4306          * already.  Enable the target ID if it hasn't been enabled, and
4307          * enable this particular LUN.
4308          */
4309         STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4310                 int retval;
4311
4312                 /*
4313                  * XXX KDM this only works for ONE TARGET ID.  We'll need
4314                  * to do things differently if we go to a multiple target
4315                  * ID scheme.
4316                  */
4317                 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4318
4319                         retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4320                         if (retval != 0) {
4321                                 printf("ctl_alloc_lun: FETD %s port %d "
4322                                        "returned error %d for targ_enable on "
4323                                        "target %ju\n", fe->port_name,
4324                                        fe->targ_port, retval,
4325                                        (uintmax_t)target_id.id);
4326                         } else
4327                                 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4328                 }
4329
4330                 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4331                 if (retval != 0) {
4332                         printf("ctl_alloc_lun: FETD %s port %d returned error "
4333                                "%d for lun_enable on target %ju lun %d\n",
4334                                fe->port_name, fe->targ_port, retval,
4335                                (uintmax_t)target_id.id, lun_number);
4336                 } else
4337                         fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4338         }
4339         return (0);
4340 }
4341
4342 /*
4343  * Delete a LUN.
4344  * Assumptions:
4345  * - LUN has already been marked invalid and any pending I/O has been taken
4346  *   care of.
4347  */
4348 static int
4349 ctl_free_lun(struct ctl_lun *lun)
4350 {
4351         struct ctl_softc *softc;
4352 #if 0
4353         struct ctl_frontend *fe;
4354 #endif
4355         struct ctl_lun *nlun;
4356         int i;
4357
4358         softc = lun->ctl_softc;
4359
4360         mtx_assert(&softc->ctl_lock, MA_OWNED);
4361
4362         STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4363
4364         ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4365
4366         softc->ctl_luns[lun->lun] = NULL;
4367
4368         if (!TAILQ_EMPTY(&lun->ooa_queue))
4369                 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4370
4371         softc->num_luns--;
4372
4373         /*
4374          * XXX KDM this scheme only works for a single target/multiple LUN
4375          * setup.  It needs to be revamped for a multiple target scheme.
4376          *
4377          * XXX KDM this results in fe->lun_disable() getting called twice,
4378          * once when ctl_disable_lun() is called, and a second time here.
4379          * We really need to re-think the LUN disable semantics.  There
4380          * should probably be several steps/levels to LUN removal:
4381          *  - disable
4382          *  - invalidate
4383          *  - free
4384          *
4385          * Right now we only have a disable method when communicating to
4386          * the front end ports, at least for individual LUNs.
4387          */
4388 #if 0
4389         STAILQ_FOREACH(fe, &softc->fe_list, links) {
4390                 int retval;
4391
4392                 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4393                                          lun->lun);
4394                 if (retval != 0) {
4395                         printf("ctl_free_lun: FETD %s port %d returned error "
4396                                "%d for lun_disable on target %ju lun %jd\n",
4397                                fe->port_name, fe->targ_port, retval,
4398                                (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4399                 }
4400
4401                 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4402                         fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4403
4404                         retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4405                         if (retval != 0) {
4406                                 printf("ctl_free_lun: FETD %s port %d "
4407                                        "returned error %d for targ_disable on "
4408                                        "target %ju\n", fe->port_name,
4409                                        fe->targ_port, retval,
4410                                        (uintmax_t)lun->target.id);
4411                         } else
4412                                 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4413
4414                         if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4415                                 continue;
4416
4417 #if 0
4418                         fe->port_offline(fe->onoff_arg);
4419                         fe->status &= ~CTL_PORT_STATUS_ONLINE;
4420 #endif
4421                 }
4422         }
4423 #endif
4424
4425         /*
4426          * Tell the backend to free resources, if this LUN has a backend.
4427          */
4428         atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4429         lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4430
4431         mtx_destroy(&lun->lun_lock);
4432         if (lun->flags & CTL_LUN_MALLOCED)
4433                 free(lun, M_CTL);
4434
4435         STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4436                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4437                         nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4438                 }
4439         }
4440
4441         return (0);
4442 }
4443
4444 static void
4445 ctl_create_lun(struct ctl_be_lun *be_lun)
4446 {
4447         struct ctl_softc *ctl_softc;
4448
4449         ctl_softc = control_softc;
4450
4451         /*
4452          * ctl_alloc_lun() should handle all potential failure cases.
4453          */
4454         ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4455 }
4456
4457 int
4458 ctl_add_lun(struct ctl_be_lun *be_lun)
4459 {
4460         struct ctl_softc *ctl_softc = control_softc;
4461
4462         mtx_lock(&ctl_softc->ctl_lock);
4463         STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4464         mtx_unlock(&ctl_softc->ctl_lock);
4465         wakeup(&ctl_softc->pending_lun_queue);
4466
4467         return (0);
4468 }
4469
4470 int
4471 ctl_enable_lun(struct ctl_be_lun *be_lun)
4472 {
4473         struct ctl_softc *ctl_softc;
4474         struct ctl_frontend *fe, *nfe;
4475         struct ctl_lun *lun;
4476         int retval;
4477
4478         ctl_softc = control_softc;
4479
4480         lun = (struct ctl_lun *)be_lun->ctl_lun;
4481
4482         mtx_lock(&ctl_softc->ctl_lock);
4483         mtx_lock(&lun->lun_lock);
4484         if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4485                 /*
4486                  * eh?  Why did we get called if the LUN is already
4487                  * enabled?
4488                  */
4489                 mtx_unlock(&lun->lun_lock);
4490                 mtx_unlock(&ctl_softc->ctl_lock);
4491                 return (0);
4492         }
4493         lun->flags &= ~CTL_LUN_DISABLED;
4494         mtx_unlock(&lun->lun_lock);
4495
4496         for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4497                 nfe = STAILQ_NEXT(fe, links);
4498
4499                 /*
4500                  * Drop the lock while we call the FETD's enable routine.
4501                  * This can lead to a callback into CTL (at least in the
4502                  * case of the internal initiator frontend.
4503                  */
4504                 mtx_unlock(&ctl_softc->ctl_lock);
4505                 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4506                 mtx_lock(&ctl_softc->ctl_lock);
4507                 if (retval != 0) {
4508                         printf("%s: FETD %s port %d returned error "
4509                                "%d for lun_enable on target %ju lun %jd\n",
4510                                __func__, fe->port_name, fe->targ_port, retval,
4511                                (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4512                 }
4513 #if 0
4514                  else {
4515             /* NOTE:  TODO:  why does lun enable affect port status? */
4516                         fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4517                 }
4518 #endif
4519         }
4520
4521         mtx_unlock(&ctl_softc->ctl_lock);
4522
4523         return (0);
4524 }
4525
4526 int
4527 ctl_disable_lun(struct ctl_be_lun *be_lun)
4528 {
4529         struct ctl_softc *ctl_softc;
4530         struct ctl_frontend *fe;
4531         struct ctl_lun *lun;
4532         int retval;
4533
4534         ctl_softc = control_softc;
4535
4536         lun = (struct ctl_lun *)be_lun->ctl_lun;
4537
4538         mtx_lock(&ctl_softc->ctl_lock);
4539         mtx_lock(&lun->lun_lock);
4540         if (lun->flags & CTL_LUN_DISABLED) {
4541                 mtx_unlock(&lun->lun_lock);
4542                 mtx_unlock(&ctl_softc->ctl_lock);
4543                 return (0);
4544         }
4545         lun->flags |= CTL_LUN_DISABLED;
4546         mtx_unlock(&lun->lun_lock);
4547
4548         STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4549                 mtx_unlock(&ctl_softc->ctl_lock);
4550                 /*
4551                  * Drop the lock before we call the frontend's disable
4552                  * routine, to avoid lock order reversals.
4553                  *
4554                  * XXX KDM what happens if the frontend list changes while
4555                  * we're traversing it?  It's unlikely, but should be handled.
4556                  */
4557                 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4558                                          lun->lun);
4559                 mtx_lock(&ctl_softc->ctl_lock);
4560                 if (retval != 0) {
4561                         printf("ctl_alloc_lun: FETD %s port %d returned error "
4562                                "%d for lun_disable on target %ju lun %jd\n",
4563                                fe->port_name, fe->targ_port, retval,
4564                                (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4565                 }
4566         }
4567
4568         mtx_unlock(&ctl_softc->ctl_lock);
4569
4570         return (0);
4571 }
4572
4573 int
4574 ctl_start_lun(struct ctl_be_lun *be_lun)
4575 {
4576         struct ctl_softc *ctl_softc;
4577         struct ctl_lun *lun;
4578
4579         ctl_softc = control_softc;
4580
4581         lun = (struct ctl_lun *)be_lun->ctl_lun;
4582
4583         mtx_lock(&lun->lun_lock);
4584         lun->flags &= ~CTL_LUN_STOPPED;
4585         mtx_unlock(&lun->lun_lock);
4586
4587         return (0);
4588 }
4589
4590 int
4591 ctl_stop_lun(struct ctl_be_lun *be_lun)
4592 {
4593         struct ctl_softc *ctl_softc;
4594         struct ctl_lun *lun;
4595
4596         ctl_softc = control_softc;
4597
4598         lun = (struct ctl_lun *)be_lun->ctl_lun;
4599
4600         mtx_lock(&lun->lun_lock);
4601         lun->flags |= CTL_LUN_STOPPED;
4602         mtx_unlock(&lun->lun_lock);
4603
4604         return (0);
4605 }
4606
4607 int
4608 ctl_lun_offline(struct ctl_be_lun *be_lun)
4609 {
4610         struct ctl_softc *ctl_softc;
4611         struct ctl_lun *lun;
4612
4613         ctl_softc = control_softc;
4614
4615         lun = (struct ctl_lun *)be_lun->ctl_lun;
4616
4617         mtx_lock(&lun->lun_lock);
4618         lun->flags |= CTL_LUN_OFFLINE;
4619         mtx_unlock(&lun->lun_lock);
4620
4621         return (0);
4622 }
4623
4624 int
4625 ctl_lun_online(struct ctl_be_lun *be_lun)
4626 {
4627         struct ctl_softc *ctl_softc;
4628         struct ctl_lun *lun;
4629
4630         ctl_softc = control_softc;
4631
4632         lun = (struct ctl_lun *)be_lun->ctl_lun;
4633
4634         mtx_lock(&lun->lun_lock);
4635         lun->flags &= ~CTL_LUN_OFFLINE;
4636         mtx_unlock(&lun->lun_lock);
4637
4638         return (0);
4639 }
4640
4641 int
4642 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4643 {
4644         struct ctl_softc *ctl_softc;
4645         struct ctl_lun *lun;
4646
4647         ctl_softc = control_softc;
4648
4649         lun = (struct ctl_lun *)be_lun->ctl_lun;
4650
4651         mtx_lock(&lun->lun_lock);
4652
4653         /*
4654          * The LUN needs to be disabled before it can be marked invalid.
4655          */
4656         if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4657                 mtx_unlock(&lun->lun_lock);
4658                 return (-1);
4659         }
4660         /*
4661          * Mark the LUN invalid.
4662          */
4663         lun->flags |= CTL_LUN_INVALID;
4664
4665         /*
4666          * If there is nothing in the OOA queue, go ahead and free the LUN.
4667          * If we have something in the OOA queue, we'll free it when the
4668          * last I/O completes.
4669          */
4670         if (TAILQ_EMPTY(&lun->ooa_queue)) {
4671                 mtx_unlock(&lun->lun_lock);
4672                 mtx_lock(&ctl_softc->ctl_lock);
4673                 ctl_free_lun(lun);
4674                 mtx_unlock(&ctl_softc->ctl_lock);
4675         } else
4676                 mtx_unlock(&lun->lun_lock);
4677
4678         return (0);
4679 }
4680
4681 int
4682 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4683 {
4684         struct ctl_softc *ctl_softc;
4685         struct ctl_lun *lun;
4686
4687         ctl_softc = control_softc;
4688         lun = (struct ctl_lun *)be_lun->ctl_lun;
4689
4690         mtx_lock(&lun->lun_lock);
4691         lun->flags |= CTL_LUN_INOPERABLE;
4692         mtx_unlock(&lun->lun_lock);
4693
4694         return (0);
4695 }
4696
4697 int
4698 ctl_lun_operable(struct ctl_be_lun *be_lun)
4699 {
4700         struct ctl_softc *ctl_softc;
4701         struct ctl_lun *lun;
4702
4703         ctl_softc = control_softc;
4704         lun = (struct ctl_lun *)be_lun->ctl_lun;
4705
4706         mtx_lock(&lun->lun_lock);
4707         lun->flags &= ~CTL_LUN_INOPERABLE;
4708         mtx_unlock(&lun->lun_lock);
4709
4710         return (0);
4711 }
4712
4713 int
4714 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4715                    int lock)
4716 {
4717         struct ctl_softc *softc;
4718         struct ctl_lun *lun;
4719         struct copan_aps_subpage *current_sp;
4720         struct ctl_page_index *page_index;
4721         int i;
4722
4723         softc = control_softc;
4724
4725         mtx_lock(&softc->ctl_lock);
4726
4727         lun = (struct ctl_lun *)be_lun->ctl_lun;
4728         mtx_lock(&lun->lun_lock);
4729
4730         page_index = NULL;
4731         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4732                 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4733                      APS_PAGE_CODE)
4734                         continue;
4735
4736                 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4737                         continue;
4738                 page_index = &lun->mode_pages.index[i];
4739         }
4740
4741         if (page_index == NULL) {
4742                 mtx_unlock(&lun->lun_lock);
4743                 mtx_unlock(&softc->ctl_lock);
4744                 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4745                        (uintmax_t)lun->lun);
4746                 return (1);
4747         }
4748 #if 0
4749         if ((softc->aps_locked_lun != 0)
4750          && (softc->aps_locked_lun != lun->lun)) {
4751                 printf("%s: attempt to lock LUN %llu when %llu is already "
4752                        "locked\n");
4753                 mtx_unlock(&lun->lun_lock);
4754                 mtx_unlock(&softc->ctl_lock);
4755                 return (1);
4756         }
4757 #endif
4758
4759         current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4760                 (page_index->page_len * CTL_PAGE_CURRENT));
4761
4762         if (lock != 0) {
4763                 current_sp->lock_active = APS_LOCK_ACTIVE;
4764                 softc->aps_locked_lun = lun->lun;
4765         } else {
4766                 current_sp->lock_active = 0;
4767                 softc->aps_locked_lun = 0;
4768         }
4769
4770
4771         /*
4772          * If we're in HA mode, try to send the lock message to the other
4773          * side.
4774          */
4775         if (ctl_is_single == 0) {
4776                 int isc_retval;
4777                 union ctl_ha_msg lock_msg;
4778
4779                 lock_msg.hdr.nexus = *nexus;
4780                 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4781                 if (lock != 0)
4782                         lock_msg.aps.lock_flag = 1;
4783                 else
4784                         lock_msg.aps.lock_flag = 0;
4785                 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4786                                          sizeof(lock_msg), 0);
4787                 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4788                         printf("%s: APS (lock=%d) error returned from "
4789                                "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4790                         mtx_unlock(&lun->lun_lock);
4791                         mtx_unlock(&softc->ctl_lock);
4792                         return (1);
4793                 }
4794         }
4795
4796         mtx_unlock(&lun->lun_lock);
4797         mtx_unlock(&softc->ctl_lock);
4798
4799         return (0);
4800 }
4801
4802 void
4803 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4804 {
4805         struct ctl_lun *lun;
4806         struct ctl_softc *softc;
4807         int i;
4808
4809         softc = control_softc;
4810
4811         lun = (struct ctl_lun *)be_lun->ctl_lun;
4812
4813         mtx_lock(&lun->lun_lock);
4814
4815         for (i = 0; i < CTL_MAX_INITIATORS; i++) 
4816                 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4817
4818         mtx_unlock(&lun->lun_lock);
4819 }
4820
4821 /*
4822  * Backend "memory move is complete" callback for requests that never
4823  * make it down to say RAIDCore's configuration code.
4824  */
4825 int
4826 ctl_config_move_done(union ctl_io *io)
4827 {
4828         int retval;
4829
4830         retval = CTL_RETVAL_COMPLETE;
4831
4832
4833         CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4834         /*
4835          * XXX KDM this shouldn't happen, but what if it does?
4836          */
4837         if (io->io_hdr.io_type != CTL_IO_SCSI)
4838                 panic("I/O type isn't CTL_IO_SCSI!");
4839
4840         if ((io->io_hdr.port_status == 0)
4841          && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4842          && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4843                 io->io_hdr.status = CTL_SUCCESS;
4844         else if ((io->io_hdr.port_status != 0)
4845               && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4846               && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4847                 /*
4848                  * For hardware error sense keys, the sense key
4849                  * specific value is defined to be a retry count,
4850                  * but we use it to pass back an internal FETD
4851                  * error code.  XXX KDM  Hopefully the FETD is only
4852                  * using 16 bits for an error code, since that's
4853                  * all the space we have in the sks field.
4854                  */
4855                 ctl_set_internal_failure(&io->scsiio,
4856                                          /*sks_valid*/ 1,
4857                                          /*retry_count*/
4858                                          io->io_hdr.port_status);
4859                 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4860                         free(io->scsiio.kern_data_ptr, M_CTL);
4861                 ctl_done(io);
4862                 goto bailout;
4863         }
4864
4865         if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4866          || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4867          || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4868                 /*
4869                  * XXX KDM just assuming a single pointer here, and not a
4870                  * S/G list.  If we start using S/G lists for config data,
4871                  * we'll need to know how to clean them up here as well.
4872                  */
4873                 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4874                         free(io->scsiio.kern_data_ptr, M_CTL);
4875                 /* Hopefully the user has already set the status... */
4876                 ctl_done(io);
4877         } else {
4878                 /*
4879                  * XXX KDM now we need to continue data movement.  Some
4880                  * options:
4881                  * - call ctl_scsiio() again?  We don't do this for data
4882                  *   writes, because for those at least we know ahead of
4883                  *   time where the write will go and how long it is.  For
4884                  *   config writes, though, that information is largely
4885                  *   contained within the write itself, thus we need to
4886                  *   parse out the data again.
4887                  *
4888                  * - Call some other function once the data is in?
4889                  */
4890
4891                 /*
4892                  * XXX KDM call ctl_scsiio() again for now, and check flag
4893                  * bits to see whether we're allocated or not.
4894                  */
4895                 retval = ctl_scsiio(&io->scsiio);
4896         }
4897 bailout:
4898         return (retval);
4899 }
4900
4901 /*
4902  * This gets called by a backend driver when it is done with a
4903  * data_submit method.
4904  */
4905 void
4906 ctl_data_submit_done(union ctl_io *io)
4907 {
4908         /*
4909          * If the IO_CONT flag is set, we need to call the supplied
4910          * function to continue processing the I/O, instead of completing
4911          * the I/O just yet.
4912          *
4913          * If there is an error, though, we don't want to keep processing.
4914          * Instead, just send status back to the initiator.
4915          */
4916         if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4917             (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4918             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4919              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4920                 io->scsiio.io_cont(io);
4921                 return;
4922         }
4923         ctl_done(io);
4924 }
4925
4926 /*
4927  * This gets called by a backend driver when it is done with a
4928  * configuration write.
4929  */
4930 void
4931 ctl_config_write_done(union ctl_io *io)
4932 {
4933         /*
4934          * If the IO_CONT flag is set, we need to call the supplied
4935          * function to continue processing the I/O, instead of completing
4936          * the I/O just yet.
4937          *
4938          * If there is an error, though, we don't want to keep processing.
4939          * Instead, just send status back to the initiator.
4940          */
4941         if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
4942          && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
4943           || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
4944                 io->scsiio.io_cont(io);
4945                 return;
4946         }
4947         /*
4948          * Since a configuration write can be done for commands that actually
4949          * have data allocated, like write buffer, and commands that have
4950          * no data, like start/stop unit, we need to check here.
4951          */
4952         if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
4953                 free(io->scsiio.kern_data_ptr, M_CTL);
4954         ctl_done(io);
4955 }
4956
4957 /*
4958  * SCSI release command.
4959  */
4960 int
4961 ctl_scsi_release(struct ctl_scsiio *ctsio)
4962 {
4963         int length, longid, thirdparty_id, resv_id;
4964         struct ctl_softc *ctl_softc;
4965         struct ctl_lun *lun;
4966
4967         length = 0;
4968         resv_id = 0;
4969
4970         CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4971
4972         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4973         ctl_softc = control_softc;
4974
4975         switch (ctsio->cdb[0]) {
4976         case RELEASE_10: {
4977                 struct scsi_release_10 *cdb;
4978
4979                 cdb = (struct scsi_release_10 *)ctsio->cdb;
4980
4981                 if (cdb->byte2 & SR10_LONGID)
4982                         longid = 1;
4983                 else
4984                         thirdparty_id = cdb->thirdparty_id;
4985
4986                 resv_id = cdb->resv_id;
4987                 length = scsi_2btoul(cdb->length);
4988                 break;
4989         }
4990         }
4991
4992
4993         /*
4994          * XXX KDM right now, we only support LUN reservation.  We don't
4995          * support 3rd party reservations, or extent reservations, which
4996          * might actually need the parameter list.  If we've gotten this
4997          * far, we've got a LUN reservation.  Anything else got kicked out
4998          * above.  So, according to SPC, ignore the length.
4999          */
5000         length = 0;
5001
5002         if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5003          && (length > 0)) {
5004                 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5005                 ctsio->kern_data_len = length;
5006                 ctsio->kern_total_len = length;
5007                 ctsio->kern_data_resid = 0;
5008                 ctsio->kern_rel_offset = 0;
5009                 ctsio->kern_sg_entries = 0;
5010                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5011                 ctsio->be_move_done = ctl_config_move_done;
5012                 ctl_datamove((union ctl_io *)ctsio);
5013
5014                 return (CTL_RETVAL_COMPLETE);
5015         }
5016
5017         if (length > 0)
5018                 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5019
5020         mtx_lock(&lun->lun_lock);
5021
5022         /*
5023          * According to SPC, it is not an error for an intiator to attempt
5024          * to release a reservation on a LUN that isn't reserved, or that
5025          * is reserved by another initiator.  The reservation can only be
5026          * released, though, by the initiator who made it or by one of
5027          * several reset type events.
5028          */
5029         if (lun->flags & CTL_LUN_RESERVED) {
5030                 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5031                  && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5032                  && (ctsio->io_hdr.nexus.targ_target.id ==
5033                      lun->rsv_nexus.targ_target.id)) {
5034                         lun->flags &= ~CTL_LUN_RESERVED;
5035                 }
5036         }
5037
5038         mtx_unlock(&lun->lun_lock);
5039
5040         ctsio->scsi_status = SCSI_STATUS_OK;
5041         ctsio->io_hdr.status = CTL_SUCCESS;
5042
5043         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5044                 free(ctsio->kern_data_ptr, M_CTL);
5045                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5046         }
5047
5048         ctl_done((union ctl_io *)ctsio);
5049         return (CTL_RETVAL_COMPLETE);
5050 }
5051
5052 int
5053 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5054 {
5055         int extent, thirdparty, longid;
5056         int resv_id, length;
5057         uint64_t thirdparty_id;
5058         struct ctl_softc *ctl_softc;
5059         struct ctl_lun *lun;
5060
5061         extent = 0;
5062         thirdparty = 0;
5063         longid = 0;
5064         resv_id = 0;
5065         length = 0;
5066         thirdparty_id = 0;
5067
5068         CTL_DEBUG_PRINT(("ctl_reserve\n"));
5069
5070         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5071         ctl_softc = control_softc;
5072
5073         switch (ctsio->cdb[0]) {
5074         case RESERVE_10: {
5075                 struct scsi_reserve_10 *cdb;
5076
5077                 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5078
5079                 if (cdb->byte2 & SR10_LONGID)
5080                         longid = 1;
5081                 else
5082                         thirdparty_id = cdb->thirdparty_id;
5083
5084                 resv_id = cdb->resv_id;
5085                 length = scsi_2btoul(cdb->length);
5086                 break;
5087         }
5088         }
5089
5090         /*
5091          * XXX KDM right now, we only support LUN reservation.  We don't
5092          * support 3rd party reservations, or extent reservations, which
5093          * might actually need the parameter list.  If we've gotten this
5094          * far, we've got a LUN reservation.  Anything else got kicked out
5095          * above.  So, according to SPC, ignore the length.
5096          */
5097         length = 0;
5098
5099         if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5100          && (length > 0)) {
5101                 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5102                 ctsio->kern_data_len = length;
5103                 ctsio->kern_total_len = length;
5104                 ctsio->kern_data_resid = 0;
5105                 ctsio->kern_rel_offset = 0;
5106                 ctsio->kern_sg_entries = 0;
5107                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5108                 ctsio->be_move_done = ctl_config_move_done;
5109                 ctl_datamove((union ctl_io *)ctsio);
5110
5111                 return (CTL_RETVAL_COMPLETE);
5112         }
5113
5114         if (length > 0)
5115                 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5116
5117         mtx_lock(&lun->lun_lock);
5118         if (lun->flags & CTL_LUN_RESERVED) {
5119                 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5120                  || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5121                  || (ctsio->io_hdr.nexus.targ_target.id !=
5122                      lun->rsv_nexus.targ_target.id)) {
5123                         ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5124                         ctsio->io_hdr.status = CTL_SCSI_ERROR;
5125                         goto bailout;
5126                 }
5127         }
5128
5129         lun->flags |= CTL_LUN_RESERVED;
5130         lun->rsv_nexus = ctsio->io_hdr.nexus;
5131
5132         ctsio->scsi_status = SCSI_STATUS_OK;
5133         ctsio->io_hdr.status = CTL_SUCCESS;
5134
5135 bailout:
5136         mtx_unlock(&lun->lun_lock);
5137
5138         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5139                 free(ctsio->kern_data_ptr, M_CTL);
5140                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5141         }
5142
5143         ctl_done((union ctl_io *)ctsio);
5144         return (CTL_RETVAL_COMPLETE);
5145 }
5146
5147 int
5148 ctl_start_stop(struct ctl_scsiio *ctsio)
5149 {
5150         struct scsi_start_stop_unit *cdb;
5151         struct ctl_lun *lun;
5152         struct ctl_softc *ctl_softc;
5153         int retval;
5154
5155         CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5156
5157         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5158         ctl_softc = control_softc;
5159         retval = 0;
5160
5161         cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5162
5163         /*
5164          * XXX KDM
5165          * We don't support the immediate bit on a stop unit.  In order to
5166          * do that, we would need to code up a way to know that a stop is
5167          * pending, and hold off any new commands until it completes, one
5168          * way or another.  Then we could accept or reject those commands
5169          * depending on its status.  We would almost need to do the reverse
5170          * of what we do below for an immediate start -- return the copy of
5171          * the ctl_io to the FETD with status to send to the host (and to
5172          * free the copy!) and then free the original I/O once the stop
5173          * actually completes.  That way, the OOA queue mechanism can work
5174          * to block commands that shouldn't proceed.  Another alternative
5175          * would be to put the copy in the queue in place of the original,
5176          * and return the original back to the caller.  That could be
5177          * slightly safer..
5178          */
5179         if ((cdb->byte2 & SSS_IMMED)
5180          && ((cdb->how & SSS_START) == 0)) {
5181                 ctl_set_invalid_field(ctsio,
5182                                       /*sks_valid*/ 1,
5183                                       /*command*/ 1,
5184                                       /*field*/ 1,
5185                                       /*bit_valid*/ 1,
5186                                       /*bit*/ 0);
5187                 ctl_done((union ctl_io *)ctsio);
5188                 return (CTL_RETVAL_COMPLETE);
5189         }
5190
5191         if ((lun->flags & CTL_LUN_PR_RESERVED)
5192          && ((cdb->how & SSS_START)==0)) {
5193                 uint32_t residx;
5194
5195                 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5196                 if (!lun->per_res[residx].registered
5197                  || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5198
5199                         ctl_set_reservation_conflict(ctsio);
5200                         ctl_done((union ctl_io *)ctsio);
5201                         return (CTL_RETVAL_COMPLETE);
5202                 }
5203         }
5204
5205         /*
5206          * If there is no backend on this device, we can't start or stop
5207          * it.  In theory we shouldn't get any start/stop commands in the
5208          * first place at this level if the LUN doesn't have a backend.
5209          * That should get stopped by the command decode code.
5210          */
5211         if (lun->backend == NULL) {
5212                 ctl_set_invalid_opcode(ctsio);
5213                 ctl_done((union ctl_io *)ctsio);
5214                 return (CTL_RETVAL_COMPLETE);
5215         }
5216
5217         /*
5218          * XXX KDM Copan-specific offline behavior.
5219          * Figure out a reasonable way to port this?
5220          */
5221 #ifdef NEEDTOPORT
5222         mtx_lock(&lun->lun_lock);
5223
5224         if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5225          && (lun->flags & CTL_LUN_OFFLINE)) {
5226                 /*
5227                  * If the LUN is offline, and the on/offline bit isn't set,
5228                  * reject the start or stop.  Otherwise, let it through.
5229                  */
5230                 mtx_unlock(&lun->lun_lock);
5231                 ctl_set_lun_not_ready(ctsio);
5232                 ctl_done((union ctl_io *)ctsio);
5233         } else {
5234                 mtx_unlock(&lun->lun_lock);
5235 #endif /* NEEDTOPORT */
5236                 /*
5237                  * This could be a start or a stop when we're online,
5238                  * or a stop/offline or start/online.  A start or stop when
5239                  * we're offline is covered in the case above.
5240                  */
5241                 /*
5242                  * In the non-immediate case, we send the request to
5243                  * the backend and return status to the user when
5244                  * it is done.
5245                  *
5246                  * In the immediate case, we allocate a new ctl_io
5247                  * to hold a copy of the request, and send that to
5248                  * the backend.  We then set good status on the
5249                  * user's request and return it immediately.
5250                  */
5251                 if (cdb->byte2 & SSS_IMMED) {
5252                         union ctl_io *new_io;
5253
5254                         new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5255                         if (new_io == NULL) {
5256                                 ctl_set_busy(ctsio);
5257                                 ctl_done((union ctl_io *)ctsio);
5258                         } else {
5259                                 ctl_copy_io((union ctl_io *)ctsio,
5260                                             new_io);
5261                                 retval = lun->backend->config_write(new_io);
5262                                 ctl_set_success(ctsio);
5263                                 ctl_done((union ctl_io *)ctsio);
5264                         }
5265                 } else {
5266                         retval = lun->backend->config_write(
5267                                 (union ctl_io *)ctsio);
5268                 }
5269 #ifdef NEEDTOPORT
5270         }
5271 #endif
5272         return (retval);
5273 }
5274
5275 /*
5276  * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5277  * we don't really do anything with the LBA and length fields if the user
5278  * passes them in.  Instead we'll just flush out the cache for the entire
5279  * LUN.
5280  */
5281 int
5282 ctl_sync_cache(struct ctl_scsiio *ctsio)
5283 {
5284         struct ctl_lun *lun;
5285         struct ctl_softc *ctl_softc;
5286         uint64_t starting_lba;
5287         uint32_t block_count;
5288         int retval;
5289
5290         CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5291
5292         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5293         ctl_softc = control_softc;
5294         retval = 0;
5295
5296         switch (ctsio->cdb[0]) {
5297         case SYNCHRONIZE_CACHE: {
5298                 struct scsi_sync_cache *cdb;
5299                 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5300
5301                 starting_lba = scsi_4btoul(cdb->begin_lba);
5302                 block_count = scsi_2btoul(cdb->lb_count);
5303                 break;
5304         }
5305         case SYNCHRONIZE_CACHE_16: {
5306                 struct scsi_sync_cache_16 *cdb;
5307                 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5308
5309                 starting_lba = scsi_8btou64(cdb->begin_lba);
5310                 block_count = scsi_4btoul(cdb->lb_count);
5311                 break;
5312         }
5313         default:
5314                 ctl_set_invalid_opcode(ctsio);
5315                 ctl_done((union ctl_io *)ctsio);
5316                 goto bailout;
5317                 break; /* NOTREACHED */
5318         }
5319
5320         /*
5321          * We check the LBA and length, but don't do anything with them.
5322          * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5323          * get flushed.  This check will just help satisfy anyone who wants
5324          * to see an error for an out of range LBA.
5325          */
5326         if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5327                 ctl_set_lba_out_of_range(ctsio);
5328                 ctl_done((union ctl_io *)ctsio);
5329                 goto bailout;
5330         }
5331
5332         /*
5333          * If this LUN has no backend, we can't flush the cache anyway.
5334          */
5335         if (lun->backend == NULL) {
5336                 ctl_set_invalid_opcode(ctsio);
5337                 ctl_done((union ctl_io *)ctsio);
5338                 goto bailout;
5339         }
5340
5341         /*
5342          * Check to see whether we're configured to send the SYNCHRONIZE
5343          * CACHE command directly to the back end.
5344          */
5345         mtx_lock(&lun->lun_lock);
5346         if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5347          && (++(lun->sync_count) >= lun->sync_interval)) {
5348                 lun->sync_count = 0;
5349                 mtx_unlock(&lun->lun_lock);
5350                 retval = lun->backend->config_write((union ctl_io *)ctsio);
5351         } else {
5352                 mtx_unlock(&lun->lun_lock);
5353                 ctl_set_success(ctsio);
5354                 ctl_done((union ctl_io *)ctsio);
5355         }
5356
5357 bailout:
5358
5359         return (retval);
5360 }
5361
5362 int
5363 ctl_format(struct ctl_scsiio *ctsio)
5364 {
5365         struct scsi_format *cdb;
5366         struct ctl_lun *lun;
5367         struct ctl_softc *ctl_softc;
5368         int length, defect_list_len;
5369
5370         CTL_DEBUG_PRINT(("ctl_format\n"));
5371
5372         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5373         ctl_softc = control_softc;
5374
5375         cdb = (struct scsi_format *)ctsio->cdb;
5376
5377         length = 0;
5378         if (cdb->byte2 & SF_FMTDATA) {
5379                 if (cdb->byte2 & SF_LONGLIST)
5380                         length = sizeof(struct scsi_format_header_long);
5381                 else
5382                         length = sizeof(struct scsi_format_header_short);
5383         }
5384
5385         if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5386          && (length > 0)) {
5387                 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5388                 ctsio->kern_data_len = length;
5389                 ctsio->kern_total_len = length;
5390                 ctsio->kern_data_resid = 0;
5391                 ctsio->kern_rel_offset = 0;
5392                 ctsio->kern_sg_entries = 0;
5393                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5394                 ctsio->be_move_done = ctl_config_move_done;
5395                 ctl_datamove((union ctl_io *)ctsio);
5396
5397                 return (CTL_RETVAL_COMPLETE);
5398         }
5399
5400         defect_list_len = 0;
5401
5402         if (cdb->byte2 & SF_FMTDATA) {
5403                 if (cdb->byte2 & SF_LONGLIST) {
5404                         struct scsi_format_header_long *header;
5405
5406                         header = (struct scsi_format_header_long *)
5407                                 ctsio->kern_data_ptr;
5408
5409                         defect_list_len = scsi_4btoul(header->defect_list_len);
5410                         if (defect_list_len != 0) {
5411                                 ctl_set_invalid_field(ctsio,
5412                                                       /*sks_valid*/ 1,
5413                                                       /*command*/ 0,
5414                                                       /*field*/ 2,
5415                                                       /*bit_valid*/ 0,
5416                                                       /*bit*/ 0);
5417                                 goto bailout;
5418                         }
5419                 } else {
5420                         struct scsi_format_header_short *header;
5421
5422                         header = (struct scsi_format_header_short *)
5423                                 ctsio->kern_data_ptr;
5424
5425                         defect_list_len = scsi_2btoul(header->defect_list_len);
5426                         if (defect_list_len != 0) {
5427                                 ctl_set_invalid_field(ctsio,
5428                                                       /*sks_valid*/ 1,
5429                                                       /*command*/ 0,
5430                                                       /*field*/ 2,
5431                                                       /*bit_valid*/ 0,
5432                                                       /*bit*/ 0);
5433                                 goto bailout;
5434                         }
5435                 }
5436         }
5437
5438         /*
5439          * The format command will clear out the "Medium format corrupted"
5440          * status if set by the configuration code.  That status is really
5441          * just a way to notify the host that we have lost the media, and
5442          * get them to issue a command that will basically make them think
5443          * they're blowing away the media.
5444          */
5445         mtx_lock(&lun->lun_lock);
5446         lun->flags &= ~CTL_LUN_INOPERABLE;
5447         mtx_unlock(&lun->lun_lock);
5448
5449         ctsio->scsi_status = SCSI_STATUS_OK;
5450         ctsio->io_hdr.status = CTL_SUCCESS;
5451 bailout:
5452
5453         if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5454                 free(ctsio->kern_data_ptr, M_CTL);
5455                 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5456         }
5457
5458         ctl_done((union ctl_io *)ctsio);
5459         return (CTL_RETVAL_COMPLETE);
5460 }
5461
5462 int
5463 ctl_read_buffer(struct ctl_scsiio *ctsio)
5464 {
5465         struct scsi_read_buffer *cdb;
5466         struct ctl_lun *lun;
5467         int buffer_offset, len;
5468         static uint8_t descr[4];
5469         static uint8_t echo_descr[4] = { 0 };
5470
5471         CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5472
5473         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5474         cdb = (struct scsi_read_buffer *)ctsio->cdb;
5475
5476         if (lun->flags & CTL_LUN_PR_RESERVED) {
5477                 uint32_t residx;
5478
5479                 /*
5480                  * XXX KDM need a lock here.
5481                  */
5482                 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5483                 if ((lun->res_type == SPR_TYPE_EX_AC
5484                   && residx != lun->pr_res_idx)
5485                  || ((lun->res_type == SPR_TYPE_EX_AC_RO
5486                    || lun->res_type == SPR_TYPE_EX_AC_AR)
5487                   && !lun->per_res[residx].registered)) {
5488                         ctl_set_reservation_conflict(ctsio);
5489                         ctl_done((union ctl_io *)ctsio);
5490                         return (CTL_RETVAL_COMPLETE);
5491                 }
5492         }
5493
5494         if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5495             (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5496             (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5497                 ctl_set_invalid_field(ctsio,
5498                                       /*sks_valid*/ 1,
5499                                       /*command*/ 1,
5500                                       /*field*/ 1,
5501                                       /*bit_valid*/ 1,
5502                                       /*bit*/ 4);
5503                 ctl_done((union ctl_io *)ctsio);
5504                 return (CTL_RETVAL_COMPLETE);
5505         }
5506
5507         len = scsi_3btoul(cdb->length);
5508         buffer_offset = scsi_3btoul(cdb->offset);
5509
5510         if (buffer_offset + len > sizeof(lun->write_buffer)) {
5511                 ctl_set_invalid_field(ctsio,
5512                                       /*sks_valid*/ 1,
5513                                       /*command*/ 1,
5514                                       /*field*/ 6,
5515                                       /*bit_valid*/ 0,
5516                                       /*bit*/ 0);
5517                 ctl_done((union ctl_io *)ctsio);
5518                 return (CTL_RETVAL_COMPLETE);
5519         }
5520
5521         if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5522                 descr[0] = 0;
5523                 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5524                 ctsio->kern_data_ptr = descr;
5525                 len = min(len, sizeof(descr));
5526         } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5527                 ctsio->kern_data_ptr = echo_descr;
5528                 len = min(len, sizeof(echo_descr));
5529         } else
5530                 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5531         ctsio->kern_data_len = len;
5532         ctsio->kern_total_len = len;
5533         ctsio->kern_data_resid = 0;
5534         ctsio->kern_rel_offset = 0;
5535         ctsio->kern_sg_entries = 0;
5536         ctsio->be_move_done = ctl_config_move_done;
5537         ctl_datamove((union ctl_io *)ctsio);
5538
5539         return (CTL_RETVAL_COMPLETE);
5540 }
5541
5542 int
5543 ctl_write_buffer(struct ctl_scsiio *ctsio)
5544 {
5545         struct scsi_write_buffer *cdb;
5546         struct ctl_lun *lun;
5547         int buffer_offset, len;
5548
5549         CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5550
5551         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5552         cdb = (struct scsi_write_buffer *)ctsio->cdb;
5553
5554         if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5555                 ctl_set_invalid_field(ctsio,
5556                                       /*sks_valid*/ 1,
5557                                       /*command*/ 1,
5558                                       /*field*/ 1,
5559                                       /*bit_valid*/ 1,
5560                                       /*bit*/ 4);
5561                 ctl_done((union ctl_io *)ctsio);
5562                 return (CTL_RETVAL_COMPLETE);
5563         }
5564
5565         len = scsi_3btoul(cdb->length);
5566         buffer_offset = scsi_3btoul(cdb->offset);
5567
5568         if (buffer_offset + len > sizeof(lun->write_buffer)) {
5569                 ctl_set_invalid_field(ctsio,
5570                                       /*sks_valid*/ 1,
5571                                       /*command*/ 1,
5572                                       /*field*/ 6,
5573                                       /*bit_valid*/ 0,
5574                                       /*bit*/ 0);
5575                 ctl_done((union ctl_io *)ctsio);
5576                 return (CTL_RETVAL_COMPLETE);
5577         }
5578
5579         /*
5580          * If we've got a kernel request that hasn't been malloced yet,
5581          * malloc it and tell the caller the data buffer is here.
5582          */
5583         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5584                 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5585                 ctsio->kern_data_len = len;
5586                 ctsio->kern_total_len = len;
5587                 ctsio->kern_data_resid = 0;
5588                 ctsio->kern_rel_offset = 0;
5589                 ctsio->kern_sg_entries = 0;
5590                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5591                 ctsio->be_move_done = ctl_config_move_done;
5592                 ctl_datamove((union ctl_io *)ctsio);
5593
5594                 return (CTL_RETVAL_COMPLETE);
5595         }
5596
5597         ctl_done((union ctl_io *)ctsio);
5598
5599         return (CTL_RETVAL_COMPLETE);
5600 }
5601
5602 int
5603 ctl_write_same(struct ctl_scsiio *ctsio)
5604 {
5605         struct ctl_lun *lun;
5606         struct ctl_lba_len_flags *lbalen;
5607         uint64_t lba;
5608         uint32_t num_blocks;
5609         int len, retval;
5610         uint8_t byte2;
5611
5612         retval = CTL_RETVAL_COMPLETE;
5613
5614         CTL_DEBUG_PRINT(("ctl_write_same\n"));
5615
5616         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5617
5618         switch (ctsio->cdb[0]) {
5619         case WRITE_SAME_10: {
5620                 struct scsi_write_same_10 *cdb;
5621
5622                 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5623
5624                 lba = scsi_4btoul(cdb->addr);
5625                 num_blocks = scsi_2btoul(cdb->length);
5626                 byte2 = cdb->byte2;
5627                 break;
5628         }
5629         case WRITE_SAME_16: {
5630                 struct scsi_write_same_16 *cdb;
5631
5632                 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5633
5634                 lba = scsi_8btou64(cdb->addr);
5635                 num_blocks = scsi_4btoul(cdb->length);
5636                 byte2 = cdb->byte2;
5637                 break;
5638         }
5639         default:
5640                 /*
5641                  * We got a command we don't support.  This shouldn't
5642                  * happen, commands should be filtered out above us.
5643                  */
5644                 ctl_set_invalid_opcode(ctsio);
5645                 ctl_done((union ctl_io *)ctsio);
5646
5647                 return (CTL_RETVAL_COMPLETE);
5648                 break; /* NOTREACHED */
5649         }
5650
5651         /*
5652          * The first check is to make sure we're in bounds, the second
5653          * check is to catch wrap-around problems.  If the lba + num blocks
5654          * is less than the lba, then we've wrapped around and the block
5655          * range is invalid anyway.
5656          */
5657         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5658          || ((lba + num_blocks) < lba)) {
5659                 ctl_set_lba_out_of_range(ctsio);
5660                 ctl_done((union ctl_io *)ctsio);
5661                 return (CTL_RETVAL_COMPLETE);
5662         }
5663
5664         /* Zero number of blocks means "to the last logical block" */
5665         if (num_blocks == 0) {
5666                 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5667                         ctl_set_invalid_field(ctsio,
5668                                               /*sks_valid*/ 0,
5669                                               /*command*/ 1,
5670                                               /*field*/ 0,
5671                                               /*bit_valid*/ 0,
5672                                               /*bit*/ 0);
5673                         ctl_done((union ctl_io *)ctsio);
5674                         return (CTL_RETVAL_COMPLETE);
5675                 }
5676                 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5677         }
5678
5679         len = lun->be_lun->blocksize;
5680
5681         /*
5682          * If we've got a kernel request that hasn't been malloced yet,
5683          * malloc it and tell the caller the data buffer is here.
5684          */
5685         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5686                 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5687                 ctsio->kern_data_len = len;
5688                 ctsio->kern_total_len = len;
5689                 ctsio->kern_data_resid = 0;
5690                 ctsio->kern_rel_offset = 0;
5691                 ctsio->kern_sg_entries = 0;
5692                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5693                 ctsio->be_move_done = ctl_config_move_done;
5694                 ctl_datamove((union ctl_io *)ctsio);
5695
5696                 return (CTL_RETVAL_COMPLETE);
5697         }
5698
5699         lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5700         lbalen->lba = lba;
5701         lbalen->len = num_blocks;
5702         lbalen->flags = byte2;
5703         retval = lun->backend->config_write((union ctl_io *)ctsio);
5704
5705         return (retval);
5706 }
5707
5708 int
5709 ctl_unmap(struct ctl_scsiio *ctsio)
5710 {
5711         struct ctl_lun *lun;
5712         struct scsi_unmap *cdb;
5713         struct ctl_ptr_len_flags *ptrlen;
5714         struct scsi_unmap_header *hdr;
5715         struct scsi_unmap_desc *buf, *end;
5716         uint64_t lba;
5717         uint32_t num_blocks;
5718         int len, retval;
5719         uint8_t byte2;
5720
5721         retval = CTL_RETVAL_COMPLETE;
5722
5723         CTL_DEBUG_PRINT(("ctl_unmap\n"));
5724
5725         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5726         cdb = (struct scsi_unmap *)ctsio->cdb;
5727
5728         len = scsi_2btoul(cdb->length);
5729         byte2 = cdb->byte2;
5730
5731         /*
5732          * If we've got a kernel request that hasn't been malloced yet,
5733          * malloc it and tell the caller the data buffer is here.
5734          */
5735         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5736                 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5737                 ctsio->kern_data_len = len;
5738                 ctsio->kern_total_len = len;
5739                 ctsio->kern_data_resid = 0;
5740                 ctsio->kern_rel_offset = 0;
5741                 ctsio->kern_sg_entries = 0;
5742                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5743                 ctsio->be_move_done = ctl_config_move_done;
5744                 ctl_datamove((union ctl_io *)ctsio);
5745
5746                 return (CTL_RETVAL_COMPLETE);
5747         }
5748
5749         len = ctsio->kern_total_len - ctsio->kern_data_resid;
5750         hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5751         if (len < sizeof (*hdr) ||
5752             len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5753             len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5754             scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5755                 ctl_set_invalid_field(ctsio,
5756                                       /*sks_valid*/ 0,
5757                                       /*command*/ 0,
5758                                       /*field*/ 0,
5759                                       /*bit_valid*/ 0,
5760                                       /*bit*/ 0);
5761                 ctl_done((union ctl_io *)ctsio);
5762                 return (CTL_RETVAL_COMPLETE);
5763         }
5764         len = scsi_2btoul(hdr->desc_length);
5765         buf = (struct scsi_unmap_desc *)(hdr + 1);
5766         end = buf + len / sizeof(*buf);
5767
5768         ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5769         ptrlen->ptr = (void *)buf;
5770         ptrlen->len = len;
5771         ptrlen->flags = byte2;
5772
5773         for (; buf < end; buf++) {
5774                 lba = scsi_8btou64(buf->lba);
5775                 num_blocks = scsi_4btoul(buf->length);
5776                 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5777                  || ((lba + num_blocks) < lba)) {
5778                         ctl_set_lba_out_of_range(ctsio);
5779                         ctl_done((union ctl_io *)ctsio);
5780                         return (CTL_RETVAL_COMPLETE);
5781                 }
5782         }
5783
5784         retval = lun->backend->config_write((union ctl_io *)ctsio);
5785
5786         return (retval);
5787 }
5788
5789 /*
5790  * Note that this function currently doesn't actually do anything inside
5791  * CTL to enforce things if the DQue bit is turned on.
5792  *
5793  * Also note that this function can't be used in the default case, because
5794  * the DQue bit isn't set in the changeable mask for the control mode page
5795  * anyway.  This is just here as an example for how to implement a page
5796  * handler, and a placeholder in case we want to allow the user to turn
5797  * tagged queueing on and off.
5798  *
5799  * The D_SENSE bit handling is functional, however, and will turn
5800  * descriptor sense on and off for a given LUN.
5801  */
5802 int
5803 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5804                          struct ctl_page_index *page_index, uint8_t *page_ptr)
5805 {
5806         struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5807         struct ctl_lun *lun;
5808         struct ctl_softc *softc;
5809         int set_ua;
5810         uint32_t initidx;
5811
5812         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5813         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5814         set_ua = 0;
5815
5816         user_cp = (struct scsi_control_page *)page_ptr;
5817         current_cp = (struct scsi_control_page *)
5818                 (page_index->page_data + (page_index->page_len *
5819                 CTL_PAGE_CURRENT));
5820         saved_cp = (struct scsi_control_page *)
5821                 (page_index->page_data + (page_index->page_len *
5822                 CTL_PAGE_SAVED));
5823
5824         softc = control_softc;
5825
5826         mtx_lock(&lun->lun_lock);
5827         if (((current_cp->rlec & SCP_DSENSE) == 0)
5828          && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5829                 /*
5830                  * Descriptor sense is currently turned off and the user
5831                  * wants to turn it on.
5832                  */
5833                 current_cp->rlec |= SCP_DSENSE;
5834                 saved_cp->rlec |= SCP_DSENSE;
5835                 lun->flags |= CTL_LUN_SENSE_DESC;
5836                 set_ua = 1;
5837         } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5838                 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5839                 /*
5840                  * Descriptor sense is currently turned on, and the user
5841                  * wants to turn it off.
5842                  */
5843                 current_cp->rlec &= ~SCP_DSENSE;
5844                 saved_cp->rlec &= ~SCP_DSENSE;
5845                 lun->flags &= ~CTL_LUN_SENSE_DESC;
5846                 set_ua = 1;
5847         }
5848         if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5849                 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5850 #ifdef NEEDTOPORT
5851                         csevent_log(CSC_CTL | CSC_SHELF_SW |
5852                                     CTL_UNTAG_TO_UNTAG,
5853                                     csevent_LogType_Trace,
5854                                     csevent_Severity_Information,
5855                                     csevent_AlertLevel_Green,
5856                                     csevent_FRU_Firmware,
5857                                     csevent_FRU_Unknown,
5858                                     "Received untagged to untagged transition");
5859 #endif /* NEEDTOPORT */
5860                 } else {
5861 #ifdef NEEDTOPORT
5862                         csevent_log(CSC_CTL | CSC_SHELF_SW |
5863                                     CTL_UNTAG_TO_TAG,
5864                                     csevent_LogType_ConfigChange,
5865                                     csevent_Severity_Information,
5866                                     csevent_AlertLevel_Green,
5867                                     csevent_FRU_Firmware,
5868                                     csevent_FRU_Unknown,
5869                                     "Received untagged to tagged "
5870                                     "queueing transition");
5871 #endif /* NEEDTOPORT */
5872
5873                         current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5874                         saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5875                         set_ua = 1;
5876                 }
5877         } else {
5878                 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5879 #ifdef NEEDTOPORT
5880                         csevent_log(CSC_CTL | CSC_SHELF_SW |
5881                                     CTL_TAG_TO_UNTAG,
5882                                     csevent_LogType_ConfigChange,
5883                                     csevent_Severity_Warning,
5884                                     csevent_AlertLevel_Yellow,
5885                                     csevent_FRU_Firmware,
5886                                     csevent_FRU_Unknown,
5887                                     "Received tagged queueing to untagged "
5888                                     "transition");
5889 #endif /* NEEDTOPORT */
5890
5891                         current_cp->queue_flags |= SCP_QUEUE_DQUE;
5892                         saved_cp->queue_flags |= SCP_QUEUE_DQUE;
5893                         set_ua = 1;
5894                 } else {
5895 #ifdef NEEDTOPORT
5896                         csevent_log(CSC_CTL | CSC_SHELF_SW |
5897                                     CTL_TAG_TO_TAG,
5898                                     csevent_LogType_Trace,
5899                                     csevent_Severity_Information,
5900                                     csevent_AlertLevel_Green,
5901                                     csevent_FRU_Firmware,
5902                                     csevent_FRU_Unknown,
5903                                     "Received tagged queueing to tagged "
5904                                     "queueing transition");
5905 #endif /* NEEDTOPORT */
5906                 }
5907         }
5908         if (set_ua != 0) {
5909                 int i;
5910                 /*
5911                  * Let other initiators know that the mode
5912                  * parameters for this LUN have changed.
5913                  */
5914                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
5915                         if (i == initidx)
5916                                 continue;
5917
5918                         lun->pending_sense[i].ua_pending |=
5919                                 CTL_UA_MODE_CHANGE;
5920                 }
5921         }
5922         mtx_unlock(&lun->lun_lock);
5923
5924         return (0);
5925 }
5926
5927 int
5928 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
5929                      struct ctl_page_index *page_index, uint8_t *page_ptr)
5930 {
5931         return (0);
5932 }
5933
5934 int
5935 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
5936                            struct ctl_page_index *page_index, int pc)
5937 {
5938         struct copan_power_subpage *page;
5939
5940         page = (struct copan_power_subpage *)page_index->page_data +
5941                 (page_index->page_len * pc);
5942
5943         switch (pc) {
5944         case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5945                 /*
5946                  * We don't update the changable bits for this page.
5947                  */
5948                 break;
5949         case SMS_PAGE_CTRL_CURRENT >> 6:
5950         case SMS_PAGE_CTRL_DEFAULT >> 6:
5951         case SMS_PAGE_CTRL_SAVED >> 6:
5952 #ifdef NEEDTOPORT
5953                 ctl_update_power_subpage(page);
5954 #endif
5955                 break;
5956         default:
5957 #ifdef NEEDTOPORT
5958                 EPRINT(0, "Invalid PC %d!!", pc);
5959 #endif
5960                 break;
5961         }
5962         return (0);
5963 }
5964
5965
5966 int
5967 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
5968                    struct ctl_page_index *page_index, uint8_t *page_ptr)
5969 {
5970         struct copan_aps_subpage *user_sp;
5971         struct copan_aps_subpage *current_sp;
5972         union ctl_modepage_info *modepage_info;
5973         struct ctl_softc *softc;
5974         struct ctl_lun *lun;
5975         int retval;
5976
5977         retval = CTL_RETVAL_COMPLETE;
5978         current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5979                      (page_index->page_len * CTL_PAGE_CURRENT));
5980         softc = control_softc;
5981         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5982
5983         user_sp = (struct copan_aps_subpage *)page_ptr;
5984
5985         modepage_info = (union ctl_modepage_info *)
5986                 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5987
5988         modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
5989         modepage_info->header.subpage = page_index->subpage;
5990         modepage_info->aps.lock_active = user_sp->lock_active;
5991
5992         mtx_lock(&softc->ctl_lock);
5993
5994         /*
5995          * If there is a request to lock the LUN and another LUN is locked
5996          * this is an error. If the requested LUN is already locked ignore
5997          * the request. If no LUN is locked attempt to lock it.
5998          * if there is a request to unlock the LUN and the LUN is currently
5999          * locked attempt to unlock it. Otherwise ignore the request. i.e.
6000          * if another LUN is locked or no LUN is locked.
6001          */
6002         if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6003                 if (softc->aps_locked_lun == lun->lun) {
6004                         /*
6005                          * This LUN is already locked, so we're done.
6006                          */
6007                         retval = CTL_RETVAL_COMPLETE;
6008                 } else if (softc->aps_locked_lun == 0) {
6009                         /*
6010                          * No one has the lock, pass the request to the
6011                          * backend.
6012                          */
6013                         retval = lun->backend->config_write(
6014                                 (union ctl_io *)ctsio);
6015                 } else {
6016                         /*
6017                          * Someone else has the lock, throw out the request.
6018                          */
6019                         ctl_set_already_locked(ctsio);
6020                         free(ctsio->kern_data_ptr, M_CTL);
6021                         ctl_done((union ctl_io *)ctsio);
6022
6023                         /*
6024                          * Set the return value so that ctl_do_mode_select()
6025                          * won't try to complete the command.  We already
6026                          * completed it here.
6027                          */
6028                         retval = CTL_RETVAL_ERROR;
6029                 }
6030         } else if (softc->aps_locked_lun == lun->lun) {
6031                 /*
6032                  * This LUN is locked, so pass the unlock request to the
6033                  * backend.
6034                  */
6035                 retval = lun->backend->config_write((union ctl_io *)ctsio);
6036         }
6037         mtx_unlock(&softc->ctl_lock);
6038
6039         return (retval);
6040 }
6041
6042 int
6043 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6044                                 struct ctl_page_index *page_index,
6045                                 uint8_t *page_ptr)
6046 {
6047         uint8_t *c;
6048         int i;
6049
6050         c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6051         ctl_time_io_secs =
6052                 (c[0] << 8) |
6053                 (c[1] << 0) |
6054                 0;
6055         CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6056         printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6057         printf("page data:");
6058         for (i=0; i<8; i++)
6059                 printf(" %.2x",page_ptr[i]);
6060         printf("\n");
6061         return (0);
6062 }
6063
6064 int
6065 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6066                                struct ctl_page_index *page_index,
6067                                int pc)
6068 {
6069         struct copan_debugconf_subpage *page;
6070
6071         page = (struct copan_debugconf_subpage *)page_index->page_data +
6072                 (page_index->page_len * pc);
6073
6074         switch (pc) {
6075         case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6076         case SMS_PAGE_CTRL_DEFAULT >> 6:
6077         case SMS_PAGE_CTRL_SAVED >> 6:
6078                 /*
6079                  * We don't update the changable or default bits for this page.
6080                  */
6081                 break;
6082         case SMS_PAGE_CTRL_CURRENT >> 6:
6083                 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6084                 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6085                 break;
6086         default:
6087 #ifdef NEEDTOPORT
6088                 EPRINT(0, "Invalid PC %d!!", pc);
6089 #endif /* NEEDTOPORT */
6090                 break;
6091         }
6092         return (0);
6093 }
6094
6095
6096 static int
6097 ctl_do_mode_select(union ctl_io *io)
6098 {
6099         struct scsi_mode_page_header *page_header;
6100         struct ctl_page_index *page_index;
6101         struct ctl_scsiio *ctsio;
6102         int control_dev, page_len;
6103         int page_len_offset, page_len_size;
6104         union ctl_modepage_info *modepage_info;
6105         struct ctl_lun *lun;
6106         int *len_left, *len_used;
6107         int retval, i;
6108
6109         ctsio = &io->scsiio;
6110         page_index = NULL;
6111         page_len = 0;
6112         retval = CTL_RETVAL_COMPLETE;
6113
6114         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6115
6116         if (lun->be_lun->lun_type != T_DIRECT)
6117                 control_dev = 1;
6118         else
6119                 control_dev = 0;
6120
6121         modepage_info = (union ctl_modepage_info *)
6122                 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6123         len_left = &modepage_info->header.len_left;
6124         len_used = &modepage_info->header.len_used;
6125
6126 do_next_page:
6127
6128         page_header = (struct scsi_mode_page_header *)
6129                 (ctsio->kern_data_ptr + *len_used);
6130
6131         if (*len_left == 0) {
6132                 free(ctsio->kern_data_ptr, M_CTL);
6133                 ctl_set_success(ctsio);
6134                 ctl_done((union ctl_io *)ctsio);
6135                 return (CTL_RETVAL_COMPLETE);
6136         } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6137
6138                 free(ctsio->kern_data_ptr, M_CTL);
6139                 ctl_set_param_len_error(ctsio);
6140                 ctl_done((union ctl_io *)ctsio);
6141                 return (CTL_RETVAL_COMPLETE);
6142
6143         } else if ((page_header->page_code & SMPH_SPF)
6144                 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6145
6146                 free(ctsio->kern_data_ptr, M_CTL);
6147                 ctl_set_param_len_error(ctsio);
6148                 ctl_done((union ctl_io *)ctsio);
6149                 return (CTL_RETVAL_COMPLETE);
6150         }
6151
6152
6153         /*
6154          * XXX KDM should we do something with the block descriptor?
6155          */
6156         for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6157
6158                 if ((control_dev != 0)
6159                  && (lun->mode_pages.index[i].page_flags &
6160                      CTL_PAGE_FLAG_DISK_ONLY))
6161                         continue;
6162
6163                 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6164                     (page_header->page_code & SMPH_PC_MASK))
6165                         continue;
6166
6167                 /*
6168                  * If neither page has a subpage code, then we've got a
6169                  * match.
6170                  */
6171                 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6172                  && ((page_header->page_code & SMPH_SPF) == 0)) {
6173                         page_index = &lun->mode_pages.index[i];
6174                         page_len = page_header->page_length;
6175                         break;
6176                 }
6177
6178                 /*
6179                  * If both pages have subpages, then the subpage numbers
6180                  * have to match.
6181                  */
6182                 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6183                   && (page_header->page_code & SMPH_SPF)) {
6184                         struct scsi_mode_page_header_sp *sph;
6185
6186                         sph = (struct scsi_mode_page_header_sp *)page_header;
6187
6188                         if (lun->mode_pages.index[i].subpage ==
6189                             sph->subpage) {
6190                                 page_index = &lun->mode_pages.index[i];
6191                                 page_len = scsi_2btoul(sph->page_length);
6192                                 break;
6193                         }
6194                 }
6195         }
6196
6197         /*
6198          * If we couldn't find the page, or if we don't have a mode select
6199          * handler for it, send back an error to the user.
6200          */
6201         if ((page_index == NULL)
6202          || (page_index->select_handler == NULL)) {
6203                 ctl_set_invalid_field(ctsio,
6204                                       /*sks_valid*/ 1,
6205                                       /*command*/ 0,
6206                                       /*field*/ *len_used,
6207                                       /*bit_valid*/ 0,
6208                                       /*bit*/ 0);
6209                 free(ctsio->kern_data_ptr, M_CTL);
6210                 ctl_done((union ctl_io *)ctsio);
6211                 return (CTL_RETVAL_COMPLETE);
6212         }
6213
6214         if (page_index->page_code & SMPH_SPF) {
6215                 page_len_offset = 2;
6216                 page_len_size = 2;
6217         } else {
6218                 page_len_size = 1;
6219                 page_len_offset = 1;
6220         }
6221
6222         /*
6223          * If the length the initiator gives us isn't the one we specify in
6224          * the mode page header, or if they didn't specify enough data in
6225          * the CDB to avoid truncating this page, kick out the request.
6226          */
6227         if ((page_len != (page_index->page_len - page_len_offset -
6228                           page_len_size))
6229          || (*len_left < page_index->page_len)) {
6230
6231
6232                 ctl_set_invalid_field(ctsio,
6233                                       /*sks_valid*/ 1,
6234                                       /*command*/ 0,
6235                                       /*field*/ *len_used + page_len_offset,
6236                                       /*bit_valid*/ 0,
6237                                       /*bit*/ 0);
6238                 free(ctsio->kern_data_ptr, M_CTL);
6239                 ctl_done((union ctl_io *)ctsio);
6240                 return (CTL_RETVAL_COMPLETE);
6241         }
6242
6243         /*
6244          * Run through the mode page, checking to make sure that the bits
6245          * the user changed are actually legal for him to change.
6246          */
6247         for (i = 0; i < page_index->page_len; i++) {
6248                 uint8_t *user_byte, *change_mask, *current_byte;
6249                 int bad_bit;
6250                 int j;
6251
6252                 user_byte = (uint8_t *)page_header + i;
6253                 change_mask = page_index->page_data +
6254                               (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6255                 current_byte = page_index->page_data +
6256                                (page_index->page_len * CTL_PAGE_CURRENT) + i;
6257
6258                 /*
6259                  * Check to see whether the user set any bits in this byte
6260                  * that he is not allowed to set.
6261                  */
6262                 if ((*user_byte & ~(*change_mask)) ==
6263                     (*current_byte & ~(*change_mask)))
6264                         continue;
6265
6266                 /*
6267                  * Go through bit by bit to determine which one is illegal.
6268                  */
6269                 bad_bit = 0;
6270                 for (j = 7; j >= 0; j--) {
6271                         if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6272                             (((1 << i) & ~(*change_mask)) & *current_byte)) {
6273                                 bad_bit = i;
6274                                 break;
6275                         }
6276                 }
6277                 ctl_set_invalid_field(ctsio,
6278                                       /*sks_valid*/ 1,
6279                                       /*command*/ 0,
6280                                       /*field*/ *len_used + i,
6281                                       /*bit_valid*/ 1,
6282                                       /*bit*/ bad_bit);
6283                 free(ctsio->kern_data_ptr, M_CTL);
6284                 ctl_done((union ctl_io *)ctsio);
6285                 return (CTL_RETVAL_COMPLETE);
6286         }
6287
6288         /*
6289          * Decrement these before we call the page handler, since we may
6290          * end up getting called back one way or another before the handler
6291          * returns to this context.
6292          */
6293         *len_left -= page_index->page_len;
6294         *len_used += page_index->page_len;
6295
6296         retval = page_index->select_handler(ctsio, page_index,
6297                                             (uint8_t *)page_header);
6298
6299         /*
6300          * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6301          * wait until this queued command completes to finish processing
6302          * the mode page.  If it returns anything other than
6303          * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6304          * already set the sense information, freed the data pointer, and
6305          * completed the io for us.
6306          */
6307         if (retval != CTL_RETVAL_COMPLETE)
6308                 goto bailout_no_done;
6309
6310         /*
6311          * If the initiator sent us more than one page, parse the next one.
6312          */
6313         if (*len_left > 0)
6314                 goto do_next_page;
6315
6316         ctl_set_success(ctsio);
6317         free(ctsio->kern_data_ptr, M_CTL);
6318         ctl_done((union ctl_io *)ctsio);
6319
6320 bailout_no_done:
6321
6322         return (CTL_RETVAL_COMPLETE);
6323
6324 }
6325
6326 int
6327 ctl_mode_select(struct ctl_scsiio *ctsio)
6328 {
6329         int param_len, pf, sp;
6330         int header_size, bd_len;
6331         int len_left, len_used;
6332         struct ctl_page_index *page_index;
6333         struct ctl_lun *lun;
6334         int control_dev, page_len;
6335         union ctl_modepage_info *modepage_info;
6336         int retval;
6337
6338         pf = 0;
6339         sp = 0;
6340         page_len = 0;
6341         len_used = 0;
6342         len_left = 0;
6343         retval = 0;
6344         bd_len = 0;
6345         page_index = NULL;
6346
6347         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6348
6349         if (lun->be_lun->lun_type != T_DIRECT)
6350                 control_dev = 1;
6351         else
6352                 control_dev = 0;
6353
6354         switch (ctsio->cdb[0]) {
6355         case MODE_SELECT_6: {
6356                 struct scsi_mode_select_6 *cdb;
6357
6358                 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6359
6360                 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6361                 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6362
6363                 param_len = cdb->length;
6364                 header_size = sizeof(struct scsi_mode_header_6);
6365                 break;
6366         }
6367         case MODE_SELECT_10: {
6368                 struct scsi_mode_select_10 *cdb;
6369
6370                 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6371
6372                 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6373                 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6374
6375                 param_len = scsi_2btoul(cdb->length);
6376                 header_size = sizeof(struct scsi_mode_header_10);
6377                 break;
6378         }
6379         default:
6380                 ctl_set_invalid_opcode(ctsio);
6381                 ctl_done((union ctl_io *)ctsio);
6382                 return (CTL_RETVAL_COMPLETE);
6383                 break; /* NOTREACHED */
6384         }
6385
6386         /*
6387          * From SPC-3:
6388          * "A parameter list length of zero indicates that the Data-Out Buffer
6389          * shall be empty. This condition shall not be considered as an error."
6390          */
6391         if (param_len == 0) {
6392                 ctl_set_success(ctsio);
6393                 ctl_done((union ctl_io *)ctsio);
6394                 return (CTL_RETVAL_COMPLETE);
6395         }
6396
6397         /*
6398          * Since we'll hit this the first time through, prior to
6399          * allocation, we don't need to free a data buffer here.
6400          */
6401         if (param_len < header_size) {
6402                 ctl_set_param_len_error(ctsio);
6403                 ctl_done((union ctl_io *)ctsio);
6404                 return (CTL_RETVAL_COMPLETE);
6405         }
6406
6407         /*
6408          * Allocate the data buffer and grab the user's data.  In theory,
6409          * we shouldn't have to sanity check the parameter list length here
6410          * because the maximum size is 64K.  We should be able to malloc
6411          * that much without too many problems.
6412          */
6413         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6414                 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6415                 ctsio->kern_data_len = param_len;
6416                 ctsio->kern_total_len = param_len;
6417                 ctsio->kern_data_resid = 0;
6418                 ctsio->kern_rel_offset = 0;
6419                 ctsio->kern_sg_entries = 0;
6420                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6421                 ctsio->be_move_done = ctl_config_move_done;
6422                 ctl_datamove((union ctl_io *)ctsio);
6423
6424                 return (CTL_RETVAL_COMPLETE);
6425         }
6426
6427         switch (ctsio->cdb[0]) {
6428         case MODE_SELECT_6: {
6429                 struct scsi_mode_header_6 *mh6;
6430
6431                 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6432                 bd_len = mh6->blk_desc_len;
6433                 break;
6434         }
6435         case MODE_SELECT_10: {
6436                 struct scsi_mode_header_10 *mh10;
6437
6438                 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6439                 bd_len = scsi_2btoul(mh10->blk_desc_len);
6440                 break;
6441         }
6442         default:
6443                 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6444                 break;
6445         }
6446
6447         if (param_len < (header_size + bd_len)) {
6448                 free(ctsio->kern_data_ptr, M_CTL);
6449                 ctl_set_param_len_error(ctsio);
6450                 ctl_done((union ctl_io *)ctsio);
6451                 return (CTL_RETVAL_COMPLETE);
6452         }
6453
6454         /*
6455          * Set the IO_CONT flag, so that if this I/O gets passed to
6456          * ctl_config_write_done(), it'll get passed back to
6457          * ctl_do_mode_select() for further processing, or completion if
6458          * we're all done.
6459          */
6460         ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6461         ctsio->io_cont = ctl_do_mode_select;
6462
6463         modepage_info = (union ctl_modepage_info *)
6464                 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6465
6466         memset(modepage_info, 0, sizeof(*modepage_info));
6467
6468         len_left = param_len - header_size - bd_len;
6469         len_used = header_size + bd_len;
6470
6471         modepage_info->header.len_left = len_left;
6472         modepage_info->header.len_used = len_used;
6473
6474         return (ctl_do_mode_select((union ctl_io *)ctsio));
6475 }
6476
6477 int
6478 ctl_mode_sense(struct ctl_scsiio *ctsio)
6479 {
6480         struct ctl_lun *lun;
6481         int pc, page_code, dbd, llba, subpage;
6482         int alloc_len, page_len, header_len, total_len;
6483         struct scsi_mode_block_descr *block_desc;
6484         struct ctl_page_index *page_index;
6485         int control_dev;
6486
6487         dbd = 0;
6488         llba = 0;
6489         block_desc = NULL;
6490         page_index = NULL;
6491
6492         CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6493
6494         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6495
6496         if (lun->be_lun->lun_type != T_DIRECT)
6497                 control_dev = 1;
6498         else
6499                 control_dev = 0;
6500
6501         if (lun->flags & CTL_LUN_PR_RESERVED) {
6502                 uint32_t residx;
6503
6504                 /*
6505                  * XXX KDM need a lock here.
6506                  */
6507                 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6508                 if ((lun->res_type == SPR_TYPE_EX_AC
6509                   && residx != lun->pr_res_idx)
6510                  || ((lun->res_type == SPR_TYPE_EX_AC_RO
6511                    || lun->res_type == SPR_TYPE_EX_AC_AR)
6512                   && !lun->per_res[residx].registered)) {
6513                         ctl_set_reservation_conflict(ctsio);
6514                         ctl_done((union ctl_io *)ctsio);
6515                         return (CTL_RETVAL_COMPLETE);
6516                 }
6517         }
6518
6519         switch (ctsio->cdb[0]) {
6520         case MODE_SENSE_6: {
6521                 struct scsi_mode_sense_6 *cdb;
6522
6523                 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6524
6525                 header_len = sizeof(struct scsi_mode_hdr_6);
6526                 if (cdb->byte2 & SMS_DBD)
6527                         dbd = 1;
6528                 else
6529                         header_len += sizeof(struct scsi_mode_block_descr);
6530
6531                 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6532                 page_code = cdb->page & SMS_PAGE_CODE;
6533                 subpage = cdb->subpage;
6534                 alloc_len = cdb->length;
6535                 break;
6536         }
6537         case MODE_SENSE_10: {
6538                 struct scsi_mode_sense_10 *cdb;
6539
6540                 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6541
6542                 header_len = sizeof(struct scsi_mode_hdr_10);
6543
6544                 if (cdb->byte2 & SMS_DBD)
6545                         dbd = 1;
6546                 else
6547                         header_len += sizeof(struct scsi_mode_block_descr);
6548                 if (cdb->byte2 & SMS10_LLBAA)
6549                         llba = 1;
6550                 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6551                 page_code = cdb->page & SMS_PAGE_CODE;
6552                 subpage = cdb->subpage;
6553                 alloc_len = scsi_2btoul(cdb->length);
6554                 break;
6555         }
6556         default:
6557                 ctl_set_invalid_opcode(ctsio);
6558                 ctl_done((union ctl_io *)ctsio);
6559                 return (CTL_RETVAL_COMPLETE);
6560                 break; /* NOTREACHED */
6561         }
6562
6563         /*
6564          * We have to make a first pass through to calculate the size of
6565          * the pages that match the user's query.  Then we allocate enough
6566          * memory to hold it, and actually copy the data into the buffer.
6567          */
6568         switch (page_code) {
6569         case SMS_ALL_PAGES_PAGE: {
6570                 int i;
6571
6572                 page_len = 0;
6573
6574                 /*
6575                  * At the moment, values other than 0 and 0xff here are
6576                  * reserved according to SPC-3.
6577                  */
6578                 if ((subpage != SMS_SUBPAGE_PAGE_0)
6579                  && (subpage != SMS_SUBPAGE_ALL)) {
6580                         ctl_set_invalid_field(ctsio,
6581                                               /*sks_valid*/ 1,
6582                                               /*command*/ 1,
6583                                               /*field*/ 3,
6584                                               /*bit_valid*/ 0,
6585                                               /*bit*/ 0);
6586                         ctl_done((union ctl_io *)ctsio);
6587                         return (CTL_RETVAL_COMPLETE);
6588                 }
6589
6590                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6591                         if ((control_dev != 0)
6592                          && (lun->mode_pages.index[i].page_flags &
6593                              CTL_PAGE_FLAG_DISK_ONLY))
6594                                 continue;
6595
6596                         /*
6597                          * We don't use this subpage if the user didn't
6598                          * request all subpages.
6599                          */
6600                         if ((lun->mode_pages.index[i].subpage != 0)
6601                          && (subpage == SMS_SUBPAGE_PAGE_0))
6602                                 continue;
6603
6604 #if 0
6605                         printf("found page %#x len %d\n",
6606                                lun->mode_pages.index[i].page_code &
6607                                SMPH_PC_MASK,
6608                                lun->mode_pages.index[i].page_len);
6609 #endif
6610                         page_len += lun->mode_pages.index[i].page_len;
6611                 }
6612                 break;
6613         }
6614         default: {
6615                 int i;
6616
6617                 page_len = 0;
6618
6619                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6620                         /* Look for the right page code */
6621                         if ((lun->mode_pages.index[i].page_code &
6622                              SMPH_PC_MASK) != page_code)
6623                                 continue;
6624
6625                         /* Look for the right subpage or the subpage wildcard*/
6626                         if ((lun->mode_pages.index[i].subpage != subpage)
6627                          && (subpage != SMS_SUBPAGE_ALL))
6628                                 continue;
6629
6630                         /* Make sure the page is supported for this dev type */
6631                         if ((control_dev != 0)
6632                          && (lun->mode_pages.index[i].page_flags &
6633                              CTL_PAGE_FLAG_DISK_ONLY))
6634                                 continue;
6635
6636 #if 0
6637                         printf("found page %#x len %d\n",
6638                                lun->mode_pages.index[i].page_code &
6639                                SMPH_PC_MASK,
6640                                lun->mode_pages.index[i].page_len);
6641 #endif
6642
6643                         page_len += lun->mode_pages.index[i].page_len;
6644                 }
6645
6646                 if (page_len == 0) {
6647                         ctl_set_invalid_field(ctsio,
6648                                               /*sks_valid*/ 1,
6649                                               /*command*/ 1,
6650                                               /*field*/ 2,
6651                                               /*bit_valid*/ 1,
6652                                               /*bit*/ 5);
6653                         ctl_done((union ctl_io *)ctsio);
6654                         return (CTL_RETVAL_COMPLETE);
6655                 }
6656                 break;
6657         }
6658         }
6659
6660         total_len = header_len + page_len;
6661 #if 0
6662         printf("header_len = %d, page_len = %d, total_len = %d\n",
6663                header_len, page_len, total_len);
6664 #endif
6665
6666         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6667         ctsio->kern_sg_entries = 0;
6668         ctsio->kern_data_resid = 0;
6669         ctsio->kern_rel_offset = 0;
6670         if (total_len < alloc_len) {
6671                 ctsio->residual = alloc_len - total_len;
6672                 ctsio->kern_data_len = total_len;
6673                 ctsio->kern_total_len = total_len;
6674         } else {
6675                 ctsio->residual = 0;
6676                 ctsio->kern_data_len = alloc_len;
6677                 ctsio->kern_total_len = alloc_len;
6678         }
6679
6680         switch (ctsio->cdb[0]) {
6681         case MODE_SENSE_6: {
6682                 struct scsi_mode_hdr_6 *header;
6683
6684                 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6685
6686                 header->datalen = ctl_min(total_len - 1, 254);
6687
6688                 if (dbd)
6689                         header->block_descr_len = 0;
6690                 else
6691                         header->block_descr_len =
6692                                 sizeof(struct scsi_mode_block_descr);
6693                 block_desc = (struct scsi_mode_block_descr *)&header[1];
6694                 break;
6695         }
6696         case MODE_SENSE_10: {
6697                 struct scsi_mode_hdr_10 *header;
6698                 int datalen;
6699
6700                 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6701
6702                 datalen = ctl_min(total_len - 2, 65533);
6703                 scsi_ulto2b(datalen, header->datalen);
6704                 if (dbd)
6705                         scsi_ulto2b(0, header->block_descr_len);
6706                 else
6707                         scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6708                                     header->block_descr_len);
6709                 block_desc = (struct scsi_mode_block_descr *)&header[1];
6710                 break;
6711         }
6712         default:
6713                 panic("invalid CDB type %#x", ctsio->cdb[0]);
6714                 break; /* NOTREACHED */
6715         }
6716
6717         /*
6718          * If we've got a disk, use its blocksize in the block
6719          * descriptor.  Otherwise, just set it to 0.
6720          */
6721         if (dbd == 0) {
6722                 if (control_dev != 0)
6723                         scsi_ulto3b(lun->be_lun->blocksize,
6724                                     block_desc->block_len);
6725                 else
6726                         scsi_ulto3b(0, block_desc->block_len);
6727         }
6728
6729         switch (page_code) {
6730         case SMS_ALL_PAGES_PAGE: {
6731                 int i, data_used;
6732
6733                 data_used = header_len;
6734                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6735                         struct ctl_page_index *page_index;
6736
6737                         page_index = &lun->mode_pages.index[i];
6738
6739                         if ((control_dev != 0)
6740                          && (page_index->page_flags &
6741                             CTL_PAGE_FLAG_DISK_ONLY))
6742                                 continue;
6743
6744                         /*
6745                          * We don't use this subpage if the user didn't
6746                          * request all subpages.  We already checked (above)
6747                          * to make sure the user only specified a subpage
6748                          * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6749                          */
6750                         if ((page_index->subpage != 0)
6751                          && (subpage == SMS_SUBPAGE_PAGE_0))
6752                                 continue;
6753
6754                         /*
6755                          * Call the handler, if it exists, to update the
6756                          * page to the latest values.
6757                          */
6758                         if (page_index->sense_handler != NULL)
6759                                 page_index->sense_handler(ctsio, page_index,pc);
6760
6761                         memcpy(ctsio->kern_data_ptr + data_used,
6762                                page_index->page_data +
6763                                (page_index->page_len * pc),
6764                                page_index->page_len);
6765                         data_used += page_index->page_len;
6766                 }
6767                 break;
6768         }
6769         default: {
6770                 int i, data_used;
6771
6772                 data_used = header_len;
6773
6774                 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6775                         struct ctl_page_index *page_index;
6776
6777                         page_index = &lun->mode_pages.index[i];
6778
6779                         /* Look for the right page code */
6780                         if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6781                                 continue;
6782
6783                         /* Look for the right subpage or the subpage wildcard*/
6784                         if ((page_index->subpage != subpage)
6785                          && (subpage != SMS_SUBPAGE_ALL))
6786                                 continue;
6787
6788                         /* Make sure the page is supported for this dev type */
6789                         if ((control_dev != 0)
6790                          && (page_index->page_flags &
6791                              CTL_PAGE_FLAG_DISK_ONLY))
6792                                 continue;
6793
6794                         /*
6795                          * Call the handler, if it exists, to update the
6796                          * page to the latest values.
6797                          */
6798                         if (page_index->sense_handler != NULL)
6799                                 page_index->sense_handler(ctsio, page_index,pc);
6800
6801                         memcpy(ctsio->kern_data_ptr + data_used,
6802                                page_index->page_data +
6803                                (page_index->page_len * pc),
6804                                page_index->page_len);
6805                         data_used += page_index->page_len;
6806                 }
6807                 break;
6808         }
6809         }
6810
6811         ctsio->scsi_status = SCSI_STATUS_OK;
6812
6813         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6814         ctsio->be_move_done = ctl_config_move_done;
6815         ctl_datamove((union ctl_io *)ctsio);
6816
6817         return (CTL_RETVAL_COMPLETE);
6818 }
6819
6820 int
6821 ctl_read_capacity(struct ctl_scsiio *ctsio)
6822 {
6823         struct scsi_read_capacity *cdb;
6824         struct scsi_read_capacity_data *data;
6825         struct ctl_lun *lun;
6826         uint32_t lba;
6827
6828         CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6829
6830         cdb = (struct scsi_read_capacity *)ctsio->cdb;
6831
6832         lba = scsi_4btoul(cdb->addr);
6833         if (((cdb->pmi & SRC_PMI) == 0)
6834          && (lba != 0)) {
6835                 ctl_set_invalid_field(/*ctsio*/ ctsio,
6836                                       /*sks_valid*/ 1,
6837                                       /*command*/ 1,
6838                                       /*field*/ 2,
6839                                       /*bit_valid*/ 0,
6840                                       /*bit*/ 0);
6841                 ctl_done((union ctl_io *)ctsio);
6842                 return (CTL_RETVAL_COMPLETE);
6843         }
6844
6845         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6846
6847         ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6848         data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6849         ctsio->residual = 0;
6850         ctsio->kern_data_len = sizeof(*data);
6851         ctsio->kern_total_len = sizeof(*data);
6852         ctsio->kern_data_resid = 0;
6853         ctsio->kern_rel_offset = 0;
6854         ctsio->kern_sg_entries = 0;
6855
6856         /*
6857          * If the maximum LBA is greater than 0xfffffffe, the user must
6858          * issue a SERVICE ACTION IN (16) command, with the read capacity
6859          * serivce action set.
6860          */
6861         if (lun->be_lun->maxlba > 0xfffffffe)
6862                 scsi_ulto4b(0xffffffff, data->addr);
6863         else
6864                 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6865
6866         /*
6867          * XXX KDM this may not be 512 bytes...
6868          */
6869         scsi_ulto4b(lun->be_lun->blocksize, data->length);
6870
6871         ctsio->scsi_status = SCSI_STATUS_OK;
6872
6873         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6874         ctsio->be_move_done = ctl_config_move_done;
6875         ctl_datamove((union ctl_io *)ctsio);
6876
6877         return (CTL_RETVAL_COMPLETE);
6878 }
6879
6880 int
6881 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6882 {
6883         struct scsi_read_capacity_16 *cdb;
6884         struct scsi_read_capacity_data_long *data;
6885         struct ctl_lun *lun;
6886         uint64_t lba;
6887         uint32_t alloc_len;
6888
6889         CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6890
6891         cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6892
6893         alloc_len = scsi_4btoul(cdb->alloc_len);
6894         lba = scsi_8btou64(cdb->addr);
6895
6896         if ((cdb->reladr & SRC16_PMI)
6897          && (lba != 0)) {
6898                 ctl_set_invalid_field(/*ctsio*/ ctsio,
6899                                       /*sks_valid*/ 1,
6900                                       /*command*/ 1,
6901                                       /*field*/ 2,
6902                                       /*bit_valid*/ 0,
6903                                       /*bit*/ 0);
6904                 ctl_done((union ctl_io *)ctsio);
6905                 return (CTL_RETVAL_COMPLETE);
6906         }
6907
6908         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6909
6910         ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6911         data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6912
6913         if (sizeof(*data) < alloc_len) {
6914                 ctsio->residual = alloc_len - sizeof(*data);
6915                 ctsio->kern_data_len = sizeof(*data);
6916                 ctsio->kern_total_len = sizeof(*data);
6917         } else {
6918                 ctsio->residual = 0;
6919                 ctsio->kern_data_len = alloc_len;
6920                 ctsio->kern_total_len = alloc_len;
6921         }
6922         ctsio->kern_data_resid = 0;
6923         ctsio->kern_rel_offset = 0;
6924         ctsio->kern_sg_entries = 0;
6925
6926         scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6927         /* XXX KDM this may not be 512 bytes... */
6928         scsi_ulto4b(lun->be_lun->blocksize, data->length);
6929         data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6930         scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6931         if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6932                 data->lalba_lbp[0] |= SRC16_LBPME;
6933
6934         ctsio->scsi_status = SCSI_STATUS_OK;
6935
6936         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6937         ctsio->be_move_done = ctl_config_move_done;
6938         ctl_datamove((union ctl_io *)ctsio);
6939
6940         return (CTL_RETVAL_COMPLETE);
6941 }
6942
6943 int
6944 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
6945 {
6946         struct scsi_maintenance_in *cdb;
6947         int retval;
6948         int alloc_len, total_len = 0;
6949         int num_target_port_groups, single;
6950         struct ctl_lun *lun;
6951         struct ctl_softc *softc;
6952         struct scsi_target_group_data *rtg_ptr;
6953         struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
6954         struct scsi_target_port_descriptor  *tp_desc_ptr1_1, *tp_desc_ptr1_2,
6955                                             *tp_desc_ptr2_1, *tp_desc_ptr2_2;
6956
6957         CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
6958
6959         cdb = (struct scsi_maintenance_in *)ctsio->cdb;
6960         softc = control_softc;
6961         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6962
6963         retval = CTL_RETVAL_COMPLETE;
6964
6965         single = ctl_is_single;
6966         if (single)
6967                 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
6968         else
6969                 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
6970
6971         total_len = sizeof(struct scsi_target_group_data) +
6972                 sizeof(struct scsi_target_port_group_descriptor) *
6973                 num_target_port_groups +
6974                 sizeof(struct scsi_target_port_descriptor) *
6975                 NUM_PORTS_PER_GRP * num_target_port_groups;
6976
6977         alloc_len = scsi_4btoul(cdb->length);
6978
6979         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6980
6981         ctsio->kern_sg_entries = 0;
6982
6983         if (total_len < alloc_len) {
6984                 ctsio->residual = alloc_len - total_len;
6985                 ctsio->kern_data_len = total_len;
6986                 ctsio->kern_total_len = total_len;
6987         } else {
6988                 ctsio->residual = 0;
6989                 ctsio->kern_data_len = alloc_len;
6990                 ctsio->kern_total_len = alloc_len;
6991         }
6992         ctsio->kern_data_resid = 0;
6993         ctsio->kern_rel_offset = 0;
6994
6995         rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
6996
6997         tpg_desc_ptr1 = &rtg_ptr->groups[0];
6998         tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
6999         tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7000                 &tp_desc_ptr1_1->desc_list[0];
7001
7002         if (single == 0) {
7003                 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7004                         &tp_desc_ptr1_2->desc_list[0];
7005                 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7006                 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7007                         &tp_desc_ptr2_1->desc_list[0];
7008         } else {
7009                 tpg_desc_ptr2 = NULL;
7010                 tp_desc_ptr2_1 = NULL;
7011                 tp_desc_ptr2_2 = NULL;
7012         }
7013
7014         scsi_ulto4b(total_len - 4, rtg_ptr->length);
7015         if (single == 0) {
7016                 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7017                         if (lun->flags & CTL_LUN_PRIMARY_SC) {
7018                                 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7019                                 tpg_desc_ptr2->pref_state =
7020                                         TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7021                         } else {
7022                                 tpg_desc_ptr1->pref_state =
7023                                         TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7024                                 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7025                         }
7026                 } else {
7027                         if (lun->flags & CTL_LUN_PRIMARY_SC) {
7028                                 tpg_desc_ptr1->pref_state =
7029                                         TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7030                                 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7031                         } else {
7032                                 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7033                                 tpg_desc_ptr2->pref_state =
7034                                         TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7035                         }
7036                 }
7037         } else {
7038                 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7039         }
7040         tpg_desc_ptr1->support = 0;
7041         tpg_desc_ptr1->target_port_group[1] = 1;
7042         tpg_desc_ptr1->status = TPG_IMPLICIT;
7043         tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7044
7045         if (single == 0) {
7046                 tpg_desc_ptr2->support = 0;
7047                 tpg_desc_ptr2->target_port_group[1] = 2;
7048                 tpg_desc_ptr2->status = TPG_IMPLICIT;
7049                 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7050
7051                 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7052                 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7053
7054                 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7055                 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7056         } else {
7057                 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7058                         tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7059                         tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7060                 } else {
7061                         tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7062                         tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7063                 }
7064         }
7065
7066         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7067         ctsio->be_move_done = ctl_config_move_done;
7068
7069         CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7070                          ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7071                          ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7072                          ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7073                          ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7074
7075         ctl_datamove((union ctl_io *)ctsio);
7076         return(retval);
7077 }
7078
7079 int
7080 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7081 {
7082         struct ctl_lun *lun;
7083         struct scsi_report_supported_opcodes *cdb;
7084         const struct ctl_cmd_entry *entry, *sentry;
7085         struct scsi_report_supported_opcodes_all *all;
7086         struct scsi_report_supported_opcodes_descr *descr;
7087         struct scsi_report_supported_opcodes_one *one;
7088         int retval;
7089         int alloc_len, total_len;
7090         int opcode, service_action, i, j, num;
7091
7092         CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7093
7094         cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7095         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7096
7097         retval = CTL_RETVAL_COMPLETE;
7098
7099         opcode = cdb->requested_opcode;
7100         service_action = scsi_2btoul(cdb->requested_service_action);
7101         switch (cdb->options & RSO_OPTIONS_MASK) {
7102         case RSO_OPTIONS_ALL:
7103                 num = 0;
7104                 for (i = 0; i < 256; i++) {
7105                         entry = &ctl_cmd_table[i];
7106                         if (entry->flags & CTL_CMD_FLAG_SA5) {
7107                                 for (j = 0; j < 32; j++) {
7108                                         sentry = &((const struct ctl_cmd_entry *)
7109                                             entry->execute)[j];
7110                                         if (ctl_cmd_applicable(
7111                                             lun->be_lun->lun_type, sentry))
7112                                                 num++;
7113                                 }
7114                         } else {
7115                                 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7116                                     entry))
7117                                         num++;
7118                         }
7119                 }
7120                 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7121                     num * sizeof(struct scsi_report_supported_opcodes_descr);
7122                 break;
7123         case RSO_OPTIONS_OC:
7124                 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7125                         ctl_set_invalid_field(/*ctsio*/ ctsio,
7126                                               /*sks_valid*/ 1,
7127                                               /*command*/ 1,
7128                                               /*field*/ 2,
7129                                               /*bit_valid*/ 1,
7130                                               /*bit*/ 2);
7131                         ctl_done((union ctl_io *)ctsio);
7132                         return (CTL_RETVAL_COMPLETE);
7133                 }
7134                 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7135                 break;
7136         case RSO_OPTIONS_OC_SA:
7137                 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7138                     service_action >= 32) {
7139                         ctl_set_invalid_field(/*ctsio*/ ctsio,
7140                                               /*sks_valid*/ 1,
7141                                               /*command*/ 1,
7142                                               /*field*/ 2,
7143                                               /*bit_valid*/ 1,
7144                                               /*bit*/ 2);
7145                         ctl_done((union ctl_io *)ctsio);
7146                         return (CTL_RETVAL_COMPLETE);
7147                 }
7148                 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7149                 break;
7150         default:
7151                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7152                                       /*sks_valid*/ 1,
7153                                       /*command*/ 1,
7154                                       /*field*/ 2,
7155                                       /*bit_valid*/ 1,
7156                                       /*bit*/ 2);
7157                 ctl_done((union ctl_io *)ctsio);
7158                 return (CTL_RETVAL_COMPLETE);
7159         }
7160
7161         alloc_len = scsi_4btoul(cdb->length);
7162
7163         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7164
7165         ctsio->kern_sg_entries = 0;
7166
7167         if (total_len < alloc_len) {
7168                 ctsio->residual = alloc_len - total_len;
7169                 ctsio->kern_data_len = total_len;
7170                 ctsio->kern_total_len = total_len;
7171         } else {
7172                 ctsio->residual = 0;
7173                 ctsio->kern_data_len = alloc_len;
7174                 ctsio->kern_total_len = alloc_len;
7175         }
7176         ctsio->kern_data_resid = 0;
7177         ctsio->kern_rel_offset = 0;
7178
7179         switch (cdb->options & RSO_OPTIONS_MASK) {
7180         case RSO_OPTIONS_ALL:
7181                 all = (struct scsi_report_supported_opcodes_all *)
7182                     ctsio->kern_data_ptr;
7183                 num = 0;
7184                 for (i = 0; i < 256; i++) {
7185                         entry = &ctl_cmd_table[i];
7186                         if (entry->flags & CTL_CMD_FLAG_SA5) {
7187                                 for (j = 0; j < 32; j++) {
7188                                         sentry = &((const struct ctl_cmd_entry *)
7189                                             entry->execute)[j];
7190                                         if (!ctl_cmd_applicable(
7191                                             lun->be_lun->lun_type, sentry))
7192                                                 continue;
7193                                         descr = &all->descr[num++];
7194                                         descr->opcode = i;
7195                                         scsi_ulto2b(j, descr->service_action);
7196                                         descr->flags = RSO_SERVACTV;
7197                                         scsi_ulto2b(sentry->length,
7198                                             descr->cdb_length);
7199                                 }
7200                         } else {
7201                                 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7202                                     entry))
7203                                         continue;
7204                                 descr = &all->descr[num++];
7205                                 descr->opcode = i;
7206                                 scsi_ulto2b(0, descr->service_action);
7207                                 descr->flags = 0;
7208                                 scsi_ulto2b(entry->length, descr->cdb_length);
7209                         }
7210                 }
7211                 scsi_ulto4b(
7212                     num * sizeof(struct scsi_report_supported_opcodes_descr),
7213                     all->length);
7214                 break;
7215         case RSO_OPTIONS_OC:
7216                 one = (struct scsi_report_supported_opcodes_one *)
7217                     ctsio->kern_data_ptr;
7218                 entry = &ctl_cmd_table[opcode];
7219                 goto fill_one;
7220         case RSO_OPTIONS_OC_SA:
7221                 one = (struct scsi_report_supported_opcodes_one *)
7222                     ctsio->kern_data_ptr;
7223                 entry = &ctl_cmd_table[opcode];
7224                 entry = &((const struct ctl_cmd_entry *)
7225                     entry->execute)[service_action];
7226 fill_one:
7227                 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7228                         one->support = 3;
7229                         scsi_ulto2b(entry->length, one->cdb_length);
7230                         one->cdb_usage[0] = opcode;
7231                         memcpy(&one->cdb_usage[1], entry->usage,
7232                             entry->length - 1);
7233                 } else
7234                         one->support = 1;
7235                 break;
7236         }
7237
7238         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7239         ctsio->be_move_done = ctl_config_move_done;
7240
7241         ctl_datamove((union ctl_io *)ctsio);
7242         return(retval);
7243 }
7244
7245 int
7246 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7247 {
7248         struct ctl_lun *lun;
7249         struct scsi_report_supported_tmf *cdb;
7250         struct scsi_report_supported_tmf_data *data;
7251         int retval;
7252         int alloc_len, total_len;
7253
7254         CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7255
7256         cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7257         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7258
7259         retval = CTL_RETVAL_COMPLETE;
7260
7261         total_len = sizeof(struct scsi_report_supported_tmf_data);
7262         alloc_len = scsi_4btoul(cdb->length);
7263
7264         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7265
7266         ctsio->kern_sg_entries = 0;
7267
7268         if (total_len < alloc_len) {
7269                 ctsio->residual = alloc_len - total_len;
7270                 ctsio->kern_data_len = total_len;
7271                 ctsio->kern_total_len = total_len;
7272         } else {
7273                 ctsio->residual = 0;
7274                 ctsio->kern_data_len = alloc_len;
7275                 ctsio->kern_total_len = alloc_len;
7276         }
7277         ctsio->kern_data_resid = 0;
7278         ctsio->kern_rel_offset = 0;
7279
7280         data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7281         data->byte1 |= RST_ATS | RST_LURS | RST_TRS;
7282
7283         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7284         ctsio->be_move_done = ctl_config_move_done;
7285
7286         ctl_datamove((union ctl_io *)ctsio);
7287         return (retval);
7288 }
7289
7290 int
7291 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7292 {
7293         struct ctl_lun *lun;
7294         struct scsi_report_timestamp *cdb;
7295         struct scsi_report_timestamp_data *data;
7296         struct timeval tv;
7297         int64_t timestamp;
7298         int retval;
7299         int alloc_len, total_len;
7300
7301         CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7302
7303         cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7304         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7305
7306         retval = CTL_RETVAL_COMPLETE;
7307
7308         total_len = sizeof(struct scsi_report_timestamp_data);
7309         alloc_len = scsi_4btoul(cdb->length);
7310
7311         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7312
7313         ctsio->kern_sg_entries = 0;
7314
7315         if (total_len < alloc_len) {
7316                 ctsio->residual = alloc_len - total_len;
7317                 ctsio->kern_data_len = total_len;
7318                 ctsio->kern_total_len = total_len;
7319         } else {
7320                 ctsio->residual = 0;
7321                 ctsio->kern_data_len = alloc_len;
7322                 ctsio->kern_total_len = alloc_len;
7323         }
7324         ctsio->kern_data_resid = 0;
7325         ctsio->kern_rel_offset = 0;
7326
7327         data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7328         scsi_ulto2b(sizeof(*data) - 2, data->length);
7329         data->origin = RTS_ORIG_OUTSIDE;
7330         getmicrotime(&tv);
7331         timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7332         scsi_ulto4b(timestamp >> 16, data->timestamp);
7333         scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7334
7335         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7336         ctsio->be_move_done = ctl_config_move_done;
7337
7338         ctl_datamove((union ctl_io *)ctsio);
7339         return (retval);
7340 }
7341
7342 int
7343 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7344 {
7345         struct scsi_per_res_in *cdb;
7346         int alloc_len, total_len = 0;
7347         /* struct scsi_per_res_in_rsrv in_data; */
7348         struct ctl_lun *lun;
7349         struct ctl_softc *softc;
7350
7351         CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7352
7353         softc = control_softc;
7354
7355         cdb = (struct scsi_per_res_in *)ctsio->cdb;
7356
7357         alloc_len = scsi_2btoul(cdb->length);
7358
7359         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7360
7361 retry:
7362         mtx_lock(&lun->lun_lock);
7363         switch (cdb->action) {
7364         case SPRI_RK: /* read keys */
7365                 total_len = sizeof(struct scsi_per_res_in_keys) +
7366                         lun->pr_key_count *
7367                         sizeof(struct scsi_per_res_key);
7368                 break;
7369         case SPRI_RR: /* read reservation */
7370                 if (lun->flags & CTL_LUN_PR_RESERVED)
7371                         total_len = sizeof(struct scsi_per_res_in_rsrv);
7372                 else
7373                         total_len = sizeof(struct scsi_per_res_in_header);
7374                 break;
7375         case SPRI_RC: /* report capabilities */
7376                 total_len = sizeof(struct scsi_per_res_cap);
7377                 break;
7378         default:
7379                 panic("Invalid PR type %x", cdb->action);
7380         }
7381         mtx_unlock(&lun->lun_lock);
7382
7383         ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7384
7385         if (total_len < alloc_len) {
7386                 ctsio->residual = alloc_len - total_len;
7387                 ctsio->kern_data_len = total_len;
7388                 ctsio->kern_total_len = total_len;
7389         } else {
7390                 ctsio->residual = 0;
7391                 ctsio->kern_data_len = alloc_len;
7392                 ctsio->kern_total_len = alloc_len;
7393         }
7394
7395         ctsio->kern_data_resid = 0;
7396         ctsio->kern_rel_offset = 0;
7397         ctsio->kern_sg_entries = 0;
7398
7399         mtx_lock(&lun->lun_lock);
7400         switch (cdb->action) {
7401         case SPRI_RK: { // read keys
7402         struct scsi_per_res_in_keys *res_keys;
7403                 int i, key_count;
7404
7405                 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7406
7407                 /*
7408                  * We had to drop the lock to allocate our buffer, which
7409                  * leaves time for someone to come in with another
7410                  * persistent reservation.  (That is unlikely, though,
7411                  * since this should be the only persistent reservation
7412                  * command active right now.)
7413                  */
7414                 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7415                     (lun->pr_key_count *
7416                      sizeof(struct scsi_per_res_key)))){
7417                         mtx_unlock(&lun->lun_lock);
7418                         free(ctsio->kern_data_ptr, M_CTL);
7419                         printf("%s: reservation length changed, retrying\n",
7420                                __func__);
7421                         goto retry;
7422                 }
7423
7424                 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7425
7426                 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7427                              lun->pr_key_count, res_keys->header.length);
7428
7429                 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7430                         if (!lun->per_res[i].registered)
7431                                 continue;
7432
7433                         /*
7434                          * We used lun->pr_key_count to calculate the
7435                          * size to allocate.  If it turns out the number of
7436                          * initiators with the registered flag set is
7437                          * larger than that (i.e. they haven't been kept in
7438                          * sync), we've got a problem.
7439                          */
7440                         if (key_count >= lun->pr_key_count) {
7441 #ifdef NEEDTOPORT
7442                                 csevent_log(CSC_CTL | CSC_SHELF_SW |
7443                                             CTL_PR_ERROR,
7444                                             csevent_LogType_Fault,
7445                                             csevent_AlertLevel_Yellow,
7446                                             csevent_FRU_ShelfController,
7447                                             csevent_FRU_Firmware,
7448                                         csevent_FRU_Unknown,
7449                                             "registered keys %d >= key "
7450                                             "count %d", key_count,
7451                                             lun->pr_key_count);
7452 #endif
7453                                 key_count++;
7454                                 continue;
7455                         }
7456                         memcpy(res_keys->keys[key_count].key,
7457                                lun->per_res[i].res_key.key,
7458                                ctl_min(sizeof(res_keys->keys[key_count].key),
7459                                sizeof(lun->per_res[i].res_key)));
7460                         key_count++;
7461                 }
7462                 break;
7463         }
7464         case SPRI_RR: { // read reservation
7465                 struct scsi_per_res_in_rsrv *res;
7466                 int tmp_len, header_only;
7467
7468                 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7469
7470                 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7471
7472                 if (lun->flags & CTL_LUN_PR_RESERVED)
7473                 {
7474                         tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7475                         scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7476                                     res->header.length);
7477                         header_only = 0;
7478                 } else {
7479                         tmp_len = sizeof(struct scsi_per_res_in_header);
7480                         scsi_ulto4b(0, res->header.length);
7481                         header_only = 1;
7482                 }
7483
7484                 /*
7485                  * We had to drop the lock to allocate our buffer, which
7486                  * leaves time for someone to come in with another
7487                  * persistent reservation.  (That is unlikely, though,
7488                  * since this should be the only persistent reservation
7489                  * command active right now.)
7490                  */
7491                 if (tmp_len != total_len) {
7492                         mtx_unlock(&lun->lun_lock);
7493                         free(ctsio->kern_data_ptr, M_CTL);
7494                         printf("%s: reservation status changed, retrying\n",
7495                                __func__);
7496                         goto retry;
7497                 }
7498
7499                 /*
7500                  * No reservation held, so we're done.
7501                  */
7502                 if (header_only != 0)
7503                         break;
7504
7505                 /*
7506                  * If the registration is an All Registrants type, the key
7507                  * is 0, since it doesn't really matter.
7508                  */
7509                 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7510                         memcpy(res->data.reservation,
7511                                &lun->per_res[lun->pr_res_idx].res_key,
7512                                sizeof(struct scsi_per_res_key));
7513                 }
7514                 res->data.scopetype = lun->res_type;
7515                 break;
7516         }
7517         case SPRI_RC:     //report capabilities
7518         {
7519                 struct scsi_per_res_cap *res_cap;
7520                 uint16_t type_mask;
7521
7522                 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7523                 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7524                 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7525                 type_mask = SPRI_TM_WR_EX_AR |
7526                             SPRI_TM_EX_AC_RO |
7527                             SPRI_TM_WR_EX_RO |
7528                             SPRI_TM_EX_AC |
7529                             SPRI_TM_WR_EX |
7530                             SPRI_TM_EX_AC_AR;
7531                 scsi_ulto2b(type_mask, res_cap->type_mask);
7532                 break;
7533         }
7534         case SPRI_RS: //read full status
7535         default:
7536                 /*
7537                  * This is a bug, because we just checked for this above,
7538                  * and should have returned an error.
7539                  */
7540                 panic("Invalid PR type %x", cdb->action);
7541                 break; /* NOTREACHED */
7542         }
7543         mtx_unlock(&lun->lun_lock);
7544
7545         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7546         ctsio->be_move_done = ctl_config_move_done;
7547
7548         CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7549                          ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7550                          ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7551                          ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7552                          ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7553
7554         ctl_datamove((union ctl_io *)ctsio);
7555
7556         return (CTL_RETVAL_COMPLETE);
7557 }
7558
7559 /*
7560  * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7561  * it should return.
7562  */
7563 static int
7564 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7565                 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7566                 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7567                 struct scsi_per_res_out_parms* param)
7568 {
7569         union ctl_ha_msg persis_io;
7570         int retval, i;
7571         int isc_retval;
7572
7573         retval = 0;
7574
7575         mtx_lock(&lun->lun_lock);
7576         if (sa_res_key == 0) {
7577                 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7578                         /* validate scope and type */
7579                         if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7580                              SPR_LU_SCOPE) {
7581                                 mtx_unlock(&lun->lun_lock);
7582                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7583                                                       /*sks_valid*/ 1,
7584                                                       /*command*/ 1,
7585                                                       /*field*/ 2,
7586                                                       /*bit_valid*/ 1,
7587                                                       /*bit*/ 4);
7588                                 ctl_done((union ctl_io *)ctsio);
7589                                 return (1);
7590                         }
7591
7592                         if (type>8 || type==2 || type==4 || type==0) {
7593                                 mtx_unlock(&lun->lun_lock);
7594                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7595                                                       /*sks_valid*/ 1,
7596                                                       /*command*/ 1,
7597                                                       /*field*/ 2,
7598                                                       /*bit_valid*/ 1,
7599                                                       /*bit*/ 0);
7600                                 ctl_done((union ctl_io *)ctsio);
7601                                 return (1);
7602                         }
7603
7604                         /* temporarily unregister this nexus */
7605                         lun->per_res[residx].registered = 0;
7606
7607                         /*
7608                          * Unregister everybody else and build UA for
7609                          * them
7610                          */
7611                         for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7612                                 if (lun->per_res[i].registered == 0)
7613                                         continue;
7614
7615                                 if (!persis_offset
7616                                  && i <CTL_MAX_INITIATORS)
7617                                         lun->pending_sense[i].ua_pending |=
7618                                                 CTL_UA_REG_PREEMPT;
7619                                 else if (persis_offset
7620                                       && i >= persis_offset)
7621                                         lun->pending_sense[i-persis_offset
7622                                                 ].ua_pending |=
7623                                                 CTL_UA_REG_PREEMPT;
7624                                 lun->per_res[i].registered = 0;
7625                                 memset(&lun->per_res[i].res_key, 0,
7626                                        sizeof(struct scsi_per_res_key));
7627                         }
7628                         lun->per_res[residx].registered = 1;
7629                         lun->pr_key_count = 1;
7630                         lun->res_type = type;
7631                         if (lun->res_type != SPR_TYPE_WR_EX_AR
7632                          && lun->res_type != SPR_TYPE_EX_AC_AR)
7633                                 lun->pr_res_idx = residx;
7634
7635                         /* send msg to other side */
7636                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7637                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7638                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7639                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
7640                         persis_io.pr.pr_info.res_type = type;
7641                         memcpy(persis_io.pr.pr_info.sa_res_key,
7642                                param->serv_act_res_key,
7643                                sizeof(param->serv_act_res_key));
7644                         if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7645                              &persis_io, sizeof(persis_io), 0)) >
7646                              CTL_HA_STATUS_SUCCESS) {
7647                                 printf("CTL:Persis Out error returned "
7648                                        "from ctl_ha_msg_send %d\n",
7649                                        isc_retval);
7650                         }
7651                 } else {
7652                         /* not all registrants */
7653                         mtx_unlock(&lun->lun_lock);
7654                         free(ctsio->kern_data_ptr, M_CTL);
7655                         ctl_set_invalid_field(ctsio,
7656                                               /*sks_valid*/ 1,
7657                                               /*command*/ 0,
7658                                               /*field*/ 8,
7659                                               /*bit_valid*/ 0,
7660                                               /*bit*/ 0);
7661                         ctl_done((union ctl_io *)ctsio);
7662                         return (1);
7663                 }
7664         } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7665                 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7666                 int found = 0;
7667
7668                 if (res_key == sa_res_key) {
7669                         /* special case */
7670                         /*
7671                          * The spec implies this is not good but doesn't
7672                          * say what to do. There are two choices either
7673                          * generate a res conflict or check condition
7674                          * with illegal field in parameter data. Since
7675                          * that is what is done when the sa_res_key is
7676                          * zero I'll take that approach since this has
7677                          * to do with the sa_res_key.
7678                          */
7679                         mtx_unlock(&lun->lun_lock);
7680                         free(ctsio->kern_data_ptr, M_CTL);
7681                         ctl_set_invalid_field(ctsio,
7682                                               /*sks_valid*/ 1,
7683                                               /*command*/ 0,
7684                                               /*field*/ 8,
7685                                               /*bit_valid*/ 0,
7686                                               /*bit*/ 0);
7687                         ctl_done((union ctl_io *)ctsio);
7688                         return (1);
7689                 }
7690
7691                 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7692                         if (lun->per_res[i].registered
7693                          && memcmp(param->serv_act_res_key,
7694                             lun->per_res[i].res_key.key,
7695                             sizeof(struct scsi_per_res_key)) != 0)
7696                                 continue;
7697
7698                         found = 1;
7699                         lun->per_res[i].registered = 0;
7700                         memset(&lun->per_res[i].res_key, 0,
7701                                sizeof(struct scsi_per_res_key));
7702                         lun->pr_key_count--;
7703
7704                         if (!persis_offset
7705                          && i < CTL_MAX_INITIATORS)
7706                                 lun->pending_sense[i].ua_pending |=
7707                                         CTL_UA_REG_PREEMPT;
7708                         else if (persis_offset
7709                               && i >= persis_offset)
7710                                 lun->pending_sense[i-persis_offset].ua_pending|=
7711                                         CTL_UA_REG_PREEMPT;
7712                 }
7713                 if (!found) {
7714                         mtx_unlock(&lun->lun_lock);
7715                         free(ctsio->kern_data_ptr, M_CTL);
7716                         ctl_set_reservation_conflict(ctsio);
7717                         ctl_done((union ctl_io *)ctsio);
7718                         return (CTL_RETVAL_COMPLETE);
7719                 }
7720                 /* send msg to other side */
7721                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7722                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7723                 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7724                 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7725                 persis_io.pr.pr_info.res_type = type;
7726                 memcpy(persis_io.pr.pr_info.sa_res_key,
7727                        param->serv_act_res_key,
7728                        sizeof(param->serv_act_res_key));
7729                 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7730                      &persis_io, sizeof(persis_io), 0)) >
7731                      CTL_HA_STATUS_SUCCESS) {
7732                         printf("CTL:Persis Out error returned from "
7733                                "ctl_ha_msg_send %d\n", isc_retval);
7734                 }
7735         } else {
7736                 /* Reserved but not all registrants */
7737                 /* sa_res_key is res holder */
7738                 if (memcmp(param->serv_act_res_key,
7739                    lun->per_res[lun->pr_res_idx].res_key.key,
7740                    sizeof(struct scsi_per_res_key)) == 0) {
7741                         /* validate scope and type */
7742                         if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7743                              SPR_LU_SCOPE) {
7744                                 mtx_unlock(&lun->lun_lock);
7745                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7746                                                       /*sks_valid*/ 1,
7747                                                       /*command*/ 1,
7748                                                       /*field*/ 2,
7749                                                       /*bit_valid*/ 1,
7750                                                       /*bit*/ 4);
7751                                 ctl_done((union ctl_io *)ctsio);
7752                                 return (1);
7753                         }
7754
7755                         if (type>8 || type==2 || type==4 || type==0) {
7756                                 mtx_unlock(&lun->lun_lock);
7757                                 ctl_set_invalid_field(/*ctsio*/ ctsio,
7758                                                       /*sks_valid*/ 1,
7759                                                       /*command*/ 1,
7760                                                       /*field*/ 2,
7761                                                       /*bit_valid*/ 1,
7762                                                       /*bit*/ 0);
7763                                 ctl_done((union ctl_io *)ctsio);
7764                                 return (1);
7765                         }
7766
7767                         /*
7768                          * Do the following:
7769                          * if sa_res_key != res_key remove all
7770                          * registrants w/sa_res_key and generate UA
7771                          * for these registrants(Registrations
7772                          * Preempted) if it wasn't an exclusive
7773                          * reservation generate UA(Reservations
7774                          * Preempted) for all other registered nexuses
7775                          * if the type has changed. Establish the new
7776                          * reservation and holder. If res_key and
7777                          * sa_res_key are the same do the above
7778                          * except don't unregister the res holder.
7779                          */
7780
7781                         /*
7782                          * Temporarily unregister so it won't get
7783                          * removed or UA generated
7784                          */
7785                         lun->per_res[residx].registered = 0;
7786                         for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7787                                 if (lun->per_res[i].registered == 0)
7788                                         continue;
7789
7790                                 if (memcmp(param->serv_act_res_key,
7791                                     lun->per_res[i].res_key.key,
7792                                     sizeof(struct scsi_per_res_key)) == 0) {
7793                                         lun->per_res[i].registered = 0;
7794                                         memset(&lun->per_res[i].res_key,
7795                                                0,
7796                                                sizeof(struct scsi_per_res_key));
7797                                         lun->pr_key_count--;
7798
7799                                         if (!persis_offset
7800                                          && i < CTL_MAX_INITIATORS)
7801                                                 lun->pending_sense[i
7802                                                         ].ua_pending |=
7803                                                         CTL_UA_REG_PREEMPT;
7804                                         else if (persis_offset
7805                                               && i >= persis_offset)
7806                                                 lun->pending_sense[
7807                                                   i-persis_offset].ua_pending |=
7808                                                   CTL_UA_REG_PREEMPT;
7809                                 } else if (type != lun->res_type
7810                                         && (lun->res_type == SPR_TYPE_WR_EX_RO
7811                                          || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7812                                                 if (!persis_offset
7813                                                  && i < CTL_MAX_INITIATORS)
7814                                                         lun->pending_sense[i
7815                                                         ].ua_pending |=
7816                                                         CTL_UA_RES_RELEASE;
7817                                                 else if (persis_offset
7818                                                       && i >= persis_offset)
7819                                                         lun->pending_sense[
7820                                                         i-persis_offset
7821                                                         ].ua_pending |=
7822                                                         CTL_UA_RES_RELEASE;
7823                                 }
7824                         }
7825                         lun->per_res[residx].registered = 1;
7826                         lun->res_type = type;
7827                         if (lun->res_type != SPR_TYPE_WR_EX_AR
7828                          && lun->res_type != SPR_TYPE_EX_AC_AR)
7829                                 lun->pr_res_idx = residx;
7830                         else
7831                                 lun->pr_res_idx =
7832                                         CTL_PR_ALL_REGISTRANTS;
7833
7834                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7835                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7836                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7837                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
7838                         persis_io.pr.pr_info.res_type = type;
7839                         memcpy(persis_io.pr.pr_info.sa_res_key,
7840                                param->serv_act_res_key,
7841                                sizeof(param->serv_act_res_key));
7842                         if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7843                              &persis_io, sizeof(persis_io), 0)) >
7844                              CTL_HA_STATUS_SUCCESS) {
7845                                 printf("CTL:Persis Out error returned "
7846                                        "from ctl_ha_msg_send %d\n",
7847                                        isc_retval);
7848                         }
7849                 } else {
7850                         /*
7851                          * sa_res_key is not the res holder just
7852                          * remove registrants
7853                          */
7854                         int found=0;
7855
7856                         for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7857                                 if (memcmp(param->serv_act_res_key,
7858                                     lun->per_res[i].res_key.key,
7859                                     sizeof(struct scsi_per_res_key)) != 0)
7860                                         continue;
7861
7862                                 found = 1;
7863                                 lun->per_res[i].registered = 0;
7864                                 memset(&lun->per_res[i].res_key, 0,
7865                                        sizeof(struct scsi_per_res_key));
7866                                 lun->pr_key_count--;
7867
7868                                 if (!persis_offset
7869                                  && i < CTL_MAX_INITIATORS)
7870                                         lun->pending_sense[i].ua_pending |=
7871                                                 CTL_UA_REG_PREEMPT;
7872                                 else if (persis_offset
7873                                       && i >= persis_offset)
7874                                         lun->pending_sense[
7875                                                 i-persis_offset].ua_pending |=
7876                                                 CTL_UA_REG_PREEMPT;
7877                         }
7878
7879                         if (!found) {
7880                                 mtx_unlock(&lun->lun_lock);
7881                                 free(ctsio->kern_data_ptr, M_CTL);
7882                                 ctl_set_reservation_conflict(ctsio);
7883                                 ctl_done((union ctl_io *)ctsio);
7884                                 return (1);
7885                         }
7886                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7887                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7888                         persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7889                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
7890                         persis_io.pr.pr_info.res_type = type;
7891                         memcpy(persis_io.pr.pr_info.sa_res_key,
7892                                param->serv_act_res_key,
7893                                sizeof(param->serv_act_res_key));
7894                         if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7895                              &persis_io, sizeof(persis_io), 0)) >
7896                              CTL_HA_STATUS_SUCCESS) {
7897                                 printf("CTL:Persis Out error returned "
7898                                        "from ctl_ha_msg_send %d\n",
7899                                 isc_retval);
7900                         }
7901                 }
7902         }
7903
7904         lun->PRGeneration++;
7905         mtx_unlock(&lun->lun_lock);
7906
7907         return (retval);
7908 }
7909
7910 static void
7911 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7912 {
7913         int i;
7914
7915         if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7916          || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7917          || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7918                    msg->pr.pr_info.sa_res_key,
7919                    sizeof(struct scsi_per_res_key)) != 0) {
7920                 uint64_t sa_res_key;
7921                 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7922
7923                 if (sa_res_key == 0) {
7924                         /* temporarily unregister this nexus */
7925                         lun->per_res[msg->pr.pr_info.residx].registered = 0;
7926
7927                         /*
7928                          * Unregister everybody else and build UA for
7929                          * them
7930                          */
7931                         for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7932                                 if (lun->per_res[i].registered == 0)
7933                                         continue;
7934
7935                                 if (!persis_offset
7936                                  && i < CTL_MAX_INITIATORS)
7937                                         lun->pending_sense[i].ua_pending |=
7938                                                 CTL_UA_REG_PREEMPT;
7939                                 else if (persis_offset && i >= persis_offset)
7940                                         lun->pending_sense[i -
7941                                                 persis_offset].ua_pending |=
7942                                                 CTL_UA_REG_PREEMPT;
7943                                 lun->per_res[i].registered = 0;
7944                                 memset(&lun->per_res[i].res_key, 0,
7945                                        sizeof(struct scsi_per_res_key));
7946                         }
7947
7948                         lun->per_res[msg->pr.pr_info.residx].registered = 1;
7949                         lun->pr_key_count = 1;
7950                         lun->res_type = msg->pr.pr_info.res_type;
7951                         if (lun->res_type != SPR_TYPE_WR_EX_AR
7952                          && lun->res_type != SPR_TYPE_EX_AC_AR)
7953                                 lun->pr_res_idx = msg->pr.pr_info.residx;
7954                 } else {
7955                         for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7956                                 if (memcmp(msg->pr.pr_info.sa_res_key,
7957                                    lun->per_res[i].res_key.key,
7958                                    sizeof(struct scsi_per_res_key)) != 0)
7959                                         continue;
7960
7961                                 lun->per_res[i].registered = 0;
7962                                 memset(&lun->per_res[i].res_key, 0,
7963                                        sizeof(struct scsi_per_res_key));
7964                                 lun->pr_key_count--;
7965
7966                                 if (!persis_offset
7967                                  && i < persis_offset)
7968                                         lun->pending_sense[i].ua_pending |=
7969                                                 CTL_UA_REG_PREEMPT;
7970                                 else if (persis_offset
7971                                       && i >= persis_offset)
7972                                         lun->pending_sense[i -
7973                                                 persis_offset].ua_pending |=
7974                                                 CTL_UA_REG_PREEMPT;
7975                         }
7976                 }
7977         } else {
7978                 /*
7979                  * Temporarily unregister so it won't get removed
7980                  * or UA generated
7981                  */
7982                 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7983                 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7984                         if (lun->per_res[i].registered == 0)
7985                                 continue;
7986
7987                         if (memcmp(msg->pr.pr_info.sa_res_key,
7988                            lun->per_res[i].res_key.key,
7989                            sizeof(struct scsi_per_res_key)) == 0) {
7990                                 lun->per_res[i].registered = 0;
7991                                 memset(&lun->per_res[i].res_key, 0,
7992                                        sizeof(struct scsi_per_res_key));
7993                                 lun->pr_key_count--;
7994                                 if (!persis_offset
7995                                  && i < CTL_MAX_INITIATORS)
7996                                         lun->pending_sense[i].ua_pending |=
7997                                                 CTL_UA_REG_PREEMPT;
7998                                 else if (persis_offset
7999                                       && i >= persis_offset)
8000                                         lun->pending_sense[i -
8001                                                 persis_offset].ua_pending |=
8002                                                 CTL_UA_REG_PREEMPT;
8003                         } else if (msg->pr.pr_info.res_type != lun->res_type
8004                                 && (lun->res_type == SPR_TYPE_WR_EX_RO
8005                                  || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8006                                         if (!persis_offset
8007                                          && i < persis_offset)
8008                                                 lun->pending_sense[i
8009                                                         ].ua_pending |=
8010                                                         CTL_UA_RES_RELEASE;
8011                                         else if (persis_offset
8012                                               && i >= persis_offset)
8013                                         lun->pending_sense[i -
8014                                                 persis_offset].ua_pending |=
8015                                                 CTL_UA_RES_RELEASE;
8016                         }
8017                 }
8018                 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8019                 lun->res_type = msg->pr.pr_info.res_type;
8020                 if (lun->res_type != SPR_TYPE_WR_EX_AR
8021                  && lun->res_type != SPR_TYPE_EX_AC_AR)
8022                         lun->pr_res_idx = msg->pr.pr_info.residx;
8023                 else
8024                         lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8025         }
8026         lun->PRGeneration++;
8027
8028 }
8029
8030
8031 int
8032 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8033 {
8034         int retval;
8035         int isc_retval;
8036         u_int32_t param_len;
8037         struct scsi_per_res_out *cdb;
8038         struct ctl_lun *lun;
8039         struct scsi_per_res_out_parms* param;
8040         struct ctl_softc *softc;
8041         uint32_t residx;
8042         uint64_t res_key, sa_res_key;
8043         uint8_t type;
8044         union ctl_ha_msg persis_io;
8045         int    i;
8046
8047         CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8048
8049         retval = CTL_RETVAL_COMPLETE;
8050
8051         softc = control_softc;
8052
8053         cdb = (struct scsi_per_res_out *)ctsio->cdb;
8054         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8055
8056         /*
8057          * We only support whole-LUN scope.  The scope & type are ignored for
8058          * register, register and ignore existing key and clear.
8059          * We sometimes ignore scope and type on preempts too!!
8060          * Verify reservation type here as well.
8061          */
8062         type = cdb->scope_type & SPR_TYPE_MASK;
8063         if ((cdb->action == SPRO_RESERVE)
8064          || (cdb->action == SPRO_RELEASE)) {
8065                 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8066                         ctl_set_invalid_field(/*ctsio*/ ctsio,
8067                                               /*sks_valid*/ 1,
8068                                               /*command*/ 1,
8069                                               /*field*/ 2,
8070                                               /*bit_valid*/ 1,
8071                                               /*bit*/ 4);
8072                         ctl_done((union ctl_io *)ctsio);
8073                         return (CTL_RETVAL_COMPLETE);
8074                 }
8075
8076                 if (type>8 || type==2 || type==4 || type==0) {
8077                         ctl_set_invalid_field(/*ctsio*/ ctsio,
8078                                               /*sks_valid*/ 1,
8079                                               /*command*/ 1,
8080                                               /*field*/ 2,
8081                                               /*bit_valid*/ 1,
8082                                               /*bit*/ 0);
8083                         ctl_done((union ctl_io *)ctsio);
8084                         return (CTL_RETVAL_COMPLETE);
8085                 }
8086         }
8087
8088         param_len = scsi_4btoul(cdb->length);
8089
8090         if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8091                 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8092                 ctsio->kern_data_len = param_len;
8093                 ctsio->kern_total_len = param_len;
8094                 ctsio->kern_data_resid = 0;
8095                 ctsio->kern_rel_offset = 0;
8096                 ctsio->kern_sg_entries = 0;
8097                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8098                 ctsio->be_move_done = ctl_config_move_done;
8099                 ctl_datamove((union ctl_io *)ctsio);
8100
8101                 return (CTL_RETVAL_COMPLETE);
8102         }
8103
8104         param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8105
8106         residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8107         res_key = scsi_8btou64(param->res_key.key);
8108         sa_res_key = scsi_8btou64(param->serv_act_res_key);
8109
8110         /*
8111          * Validate the reservation key here except for SPRO_REG_IGNO
8112          * This must be done for all other service actions
8113          */
8114         if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8115                 mtx_lock(&lun->lun_lock);
8116                 if (lun->per_res[residx].registered) {
8117                     if (memcmp(param->res_key.key,
8118                                lun->per_res[residx].res_key.key,
8119                                ctl_min(sizeof(param->res_key),
8120                                sizeof(lun->per_res[residx].res_key))) != 0) {
8121                                 /*
8122                                  * The current key passed in doesn't match
8123                                  * the one the initiator previously
8124                                  * registered.
8125                                  */
8126                                 mtx_unlock(&lun->lun_lock);
8127                                 free(ctsio->kern_data_ptr, M_CTL);
8128                                 ctl_set_reservation_conflict(ctsio);
8129                                 ctl_done((union ctl_io *)ctsio);
8130                                 return (CTL_RETVAL_COMPLETE);
8131                         }
8132                 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8133                         /*
8134                          * We are not registered
8135                          */
8136                         mtx_unlock(&lun->lun_lock);
8137                         free(ctsio->kern_data_ptr, M_CTL);
8138                         ctl_set_reservation_conflict(ctsio);
8139                         ctl_done((union ctl_io *)ctsio);
8140                         return (CTL_RETVAL_COMPLETE);
8141                 } else if (res_key != 0) {
8142                         /*
8143                          * We are not registered and trying to register but
8144                          * the register key isn't zero.
8145                          */
8146                         mtx_unlock(&lun->lun_lock);
8147                         free(ctsio->kern_data_ptr, M_CTL);
8148                         ctl_set_reservation_conflict(ctsio);
8149                         ctl_done((union ctl_io *)ctsio);
8150                         return (CTL_RETVAL_COMPLETE);
8151                 }
8152                 mtx_unlock(&lun->lun_lock);
8153         }
8154
8155         switch (cdb->action & SPRO_ACTION_MASK) {
8156         case SPRO_REGISTER:
8157         case SPRO_REG_IGNO: {
8158
8159 #if 0
8160                 printf("Registration received\n");
8161 #endif
8162
8163                 /*
8164                  * We don't support any of these options, as we report in
8165                  * the read capabilities request (see
8166                  * ctl_persistent_reserve_in(), above).
8167                  */
8168                 if ((param->flags & SPR_SPEC_I_PT)
8169                  || (param->flags & SPR_ALL_TG_PT)
8170                  || (param->flags & SPR_APTPL)) {
8171                         int bit_ptr;
8172
8173                         if (param->flags & SPR_APTPL)
8174                                 bit_ptr = 0;
8175                         else if (param->flags & SPR_ALL_TG_PT)
8176                                 bit_ptr = 2;
8177                         else /* SPR_SPEC_I_PT */
8178                                 bit_ptr = 3;
8179
8180                         free(ctsio->kern_data_ptr, M_CTL);
8181                         ctl_set_invalid_field(ctsio,
8182                                               /*sks_valid*/ 1,
8183                                               /*command*/ 0,
8184                                               /*field*/ 20,
8185                                               /*bit_valid*/ 1,
8186                                               /*bit*/ bit_ptr);
8187                         ctl_done((union ctl_io *)ctsio);
8188                         return (CTL_RETVAL_COMPLETE);
8189                 }
8190
8191                 mtx_lock(&lun->lun_lock);
8192
8193                 /*
8194                  * The initiator wants to clear the
8195                  * key/unregister.
8196                  */
8197                 if (sa_res_key == 0) {
8198                         if ((res_key == 0
8199                           && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8200                          || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8201                           && !lun->per_res[residx].registered)) {
8202                                 mtx_unlock(&lun->lun_lock);
8203                                 goto done;
8204                         }
8205
8206                         lun->per_res[residx].registered = 0;
8207                         memset(&lun->per_res[residx].res_key,
8208                                0, sizeof(lun->per_res[residx].res_key));
8209                         lun->pr_key_count--;
8210
8211                         if (residx == lun->pr_res_idx) {
8212                                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8213                                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8214
8215                                 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8216                                   || lun->res_type == SPR_TYPE_EX_AC_RO)
8217                                  && lun->pr_key_count) {
8218                                         /*
8219                                          * If the reservation is a registrants
8220                                          * only type we need to generate a UA
8221                                          * for other registered inits.  The
8222                                          * sense code should be RESERVATIONS
8223                                          * RELEASED
8224                                          */
8225
8226                                         for (i = 0; i < CTL_MAX_INITIATORS;i++){
8227                                                 if (lun->per_res[
8228                                                     i+persis_offset].registered
8229                                                     == 0)
8230                                                         continue;
8231                                                 lun->pending_sense[i
8232                                                         ].ua_pending |=
8233                                                         CTL_UA_RES_RELEASE;
8234                                         }
8235                                 }
8236                                 lun->res_type = 0;
8237                         } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8238                                 if (lun->pr_key_count==0) {
8239                                         lun->flags &= ~CTL_LUN_PR_RESERVED;
8240                                         lun->res_type = 0;
8241                                         lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8242                                 }
8243                         }
8244                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8245                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8246                         persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8247                         persis_io.pr.pr_info.residx = residx;
8248                         if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8249                              &persis_io, sizeof(persis_io), 0 )) >
8250                              CTL_HA_STATUS_SUCCESS) {
8251                                 printf("CTL:Persis Out error returned from "
8252                                        "ctl_ha_msg_send %d\n", isc_retval);
8253                         }
8254                 } else /* sa_res_key != 0 */ {
8255
8256                         /*
8257                          * If we aren't registered currently then increment
8258                          * the key count and set the registered flag.
8259                          */
8260                         if (!lun->per_res[residx].registered) {
8261                                 lun->pr_key_count++;
8262                                 lun->per_res[residx].registered = 1;
8263                         }
8264
8265                         memcpy(&lun->per_res[residx].res_key,
8266                                param->serv_act_res_key,
8267                                ctl_min(sizeof(param->serv_act_res_key),
8268                                sizeof(lun->per_res[residx].res_key)));
8269
8270                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8271                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8272                         persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8273                         persis_io.pr.pr_info.residx = residx;
8274                         memcpy(persis_io.pr.pr_info.sa_res_key,
8275                                param->serv_act_res_key,
8276                                sizeof(param->serv_act_res_key));
8277                         if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8278                              &persis_io, sizeof(persis_io), 0)) >
8279                              CTL_HA_STATUS_SUCCESS) {
8280                                 printf("CTL:Persis Out error returned from "
8281                                        "ctl_ha_msg_send %d\n", isc_retval);
8282                         }
8283                 }
8284                 lun->PRGeneration++;
8285                 mtx_unlock(&lun->lun_lock);
8286
8287                 break;
8288         }
8289         case SPRO_RESERVE:
8290 #if 0
8291                 printf("Reserve executed type %d\n", type);
8292 #endif
8293                 mtx_lock(&lun->lun_lock);
8294                 if (lun->flags & CTL_LUN_PR_RESERVED) {
8295                         /*
8296                          * if this isn't the reservation holder and it's
8297                          * not a "all registrants" type or if the type is
8298                          * different then we have a conflict
8299                          */
8300                         if ((lun->pr_res_idx != residx
8301                           && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8302                          || lun->res_type != type) {
8303                                 mtx_unlock(&lun->lun_lock);
8304                                 free(ctsio->kern_data_ptr, M_CTL);
8305                                 ctl_set_reservation_conflict(ctsio);
8306                                 ctl_done((union ctl_io *)ctsio);
8307                                 return (CTL_RETVAL_COMPLETE);
8308                         }
8309                         mtx_unlock(&lun->lun_lock);
8310                 } else /* create a reservation */ {
8311                         /*
8312                          * If it's not an "all registrants" type record
8313                          * reservation holder
8314                          */
8315                         if (type != SPR_TYPE_WR_EX_AR
8316                          && type != SPR_TYPE_EX_AC_AR)
8317                                 lun->pr_res_idx = residx; /* Res holder */
8318                         else
8319                                 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8320
8321                         lun->flags |= CTL_LUN_PR_RESERVED;
8322                         lun->res_type = type;
8323
8324                         mtx_unlock(&lun->lun_lock);
8325
8326                         /* send msg to other side */
8327                         persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8328                         persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8329                         persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8330                         persis_io.pr.pr_info.residx = lun->pr_res_idx;
8331                         persis_io.pr.pr_info.res_type = type;
8332                         if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8333                              &persis_io, sizeof(persis_io), 0)) >
8334                              CTL_HA_STATUS_SUCCESS) {
8335                                 printf("CTL:Persis Out error returned from "
8336                                        "ctl_ha_msg_send %d\n", isc_retval);
8337                         }
8338                 }
8339                 break;
8340
8341         case SPRO_RELEASE:
8342                 mtx_lock(&lun->lun_lock);
8343                 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8344                         /* No reservation exists return good status */
8345                         mtx_unlock(&lun->lun_lock);
8346                         goto done;
8347                 }
8348                 /*
8349                  * Is this nexus a reservation holder?
8350                  */
8351                 if (lun->pr_res_idx != residx
8352                  && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8353                         /*
8354                          * not a res holder return good status but
8355                          * do nothing
8356                          */
8357                         mtx_unlock(&lun->lun_lock);
8358                         goto done;
8359                 }
8360
8361                 if (lun->res_type != type) {
8362                         mtx_unlock(&lun->lun_lock);
8363                         free(ctsio->kern_data_ptr, M_CTL);
8364                         ctl_set_illegal_pr_release(ctsio);
8365                         ctl_done((union ctl_io *)ctsio);
8366                         return (CTL_RETVAL_COMPLETE);
8367                 }
8368
8369                 /* okay to release */
8370                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8371                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8372                 lun->res_type = 0;
8373
8374                 /*
8375                  * if this isn't an exclusive access
8376                  * res generate UA for all other
8377                  * registrants.
8378                  */
8379                 if (type != SPR_TYPE_EX_AC
8380                  && type != SPR_TYPE_WR_EX) {
8381                         /*
8382                          * temporarily unregister so we don't generate UA
8383                          */
8384                         lun->per_res[residx].registered = 0;
8385
8386                         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8387                                 if (lun->per_res[i+persis_offset].registered
8388                                     == 0)
8389                                         continue;
8390                                 lun->pending_sense[i].ua_pending |=
8391                                         CTL_UA_RES_RELEASE;
8392                         }
8393
8394                         lun->per_res[residx].registered = 1;
8395                 }
8396                 mtx_unlock(&lun->lun_lock);
8397                 /* Send msg to other side */
8398                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8399                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8400                 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8401                 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8402                      sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8403                         printf("CTL:Persis Out error returned from "
8404                                "ctl_ha_msg_send %d\n", isc_retval);
8405                 }
8406                 break;
8407
8408         case SPRO_CLEAR:
8409                 /* send msg to other side */
8410
8411                 mtx_lock(&lun->lun_lock);
8412                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8413                 lun->res_type = 0;
8414                 lun->pr_key_count = 0;
8415                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8416
8417
8418                 memset(&lun->per_res[residx].res_key,
8419                        0, sizeof(lun->per_res[residx].res_key));
8420                 lun->per_res[residx].registered = 0;
8421
8422                 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8423                         if (lun->per_res[i].registered) {
8424                                 if (!persis_offset && i < CTL_MAX_INITIATORS)
8425                                         lun->pending_sense[i].ua_pending |=
8426                                                 CTL_UA_RES_PREEMPT;
8427                                 else if (persis_offset && i >= persis_offset)
8428                                         lun->pending_sense[i-persis_offset
8429                                             ].ua_pending |= CTL_UA_RES_PREEMPT;
8430
8431                                 memset(&lun->per_res[i].res_key,
8432                                        0, sizeof(struct scsi_per_res_key));
8433                                 lun->per_res[i].registered = 0;
8434                         }
8435                 lun->PRGeneration++;
8436                 mtx_unlock(&lun->lun_lock);
8437                 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8438                 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8439                 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8440                 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8441                      sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8442                         printf("CTL:Persis Out error returned from "
8443                                "ctl_ha_msg_send %d\n", isc_retval);
8444                 }
8445                 break;
8446
8447         case SPRO_PREEMPT: {
8448                 int nretval;
8449
8450                 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8451                                           residx, ctsio, cdb, param);
8452                 if (nretval != 0)
8453                         return (CTL_RETVAL_COMPLETE);
8454                 break;
8455         }
8456         default:
8457                 panic("Invalid PR type %x", cdb->action);
8458         }
8459
8460 done:
8461         free(ctsio->kern_data_ptr, M_CTL);
8462         ctl_set_success(ctsio);
8463         ctl_done((union ctl_io *)ctsio);
8464
8465         return (retval);
8466 }
8467
8468 /*
8469  * This routine is for handling a message from the other SC pertaining to
8470  * persistent reserve out. All the error checking will have been done
8471  * so only perorming the action need be done here to keep the two
8472  * in sync.
8473  */
8474 static void
8475 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8476 {
8477         struct ctl_lun *lun;
8478         struct ctl_softc *softc;
8479         int i;
8480         uint32_t targ_lun;
8481
8482         softc = control_softc;
8483
8484         targ_lun = msg->hdr.nexus.targ_mapped_lun;
8485         lun = softc->ctl_luns[targ_lun];
8486         mtx_lock(&lun->lun_lock);
8487         switch(msg->pr.pr_info.action) {
8488         case CTL_PR_REG_KEY:
8489                 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8490                         lun->per_res[msg->pr.pr_info.residx].registered = 1;
8491                         lun->pr_key_count++;
8492                 }
8493                 lun->PRGeneration++;
8494                 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8495                        msg->pr.pr_info.sa_res_key,
8496                        sizeof(struct scsi_per_res_key));
8497                 break;
8498
8499         case CTL_PR_UNREG_KEY:
8500                 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8501                 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8502                        0, sizeof(struct scsi_per_res_key));
8503                 lun->pr_key_count--;
8504
8505                 /* XXX Need to see if the reservation has been released */
8506                 /* if so do we need to generate UA? */
8507                 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8508                         lun->flags &= ~CTL_LUN_PR_RESERVED;
8509                         lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8510
8511                         if ((lun->res_type == SPR_TYPE_WR_EX_RO
8512                           || lun->res_type == SPR_TYPE_EX_AC_RO)
8513                          && lun->pr_key_count) {
8514                                 /*
8515                                  * If the reservation is a registrants
8516                                  * only type we need to generate a UA
8517                                  * for other registered inits.  The
8518                                  * sense code should be RESERVATIONS
8519                                  * RELEASED
8520                                  */
8521
8522                                 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8523                                         if (lun->per_res[i+
8524                                             persis_offset].registered == 0)
8525                                                 continue;
8526
8527                                         lun->pending_sense[i
8528                                                 ].ua_pending |=
8529                                                 CTL_UA_RES_RELEASE;
8530                                 }
8531                         }
8532                         lun->res_type = 0;
8533                 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8534                         if (lun->pr_key_count==0) {
8535                                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8536                                 lun->res_type = 0;
8537                                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8538                         }
8539                 }
8540                 lun->PRGeneration++;
8541                 break;
8542
8543         case CTL_PR_RESERVE:
8544                 lun->flags |= CTL_LUN_PR_RESERVED;
8545                 lun->res_type = msg->pr.pr_info.res_type;
8546                 lun->pr_res_idx = msg->pr.pr_info.residx;
8547
8548                 break;
8549
8550         case CTL_PR_RELEASE:
8551                 /*
8552                  * if this isn't an exclusive access res generate UA for all
8553                  * other registrants.
8554                  */
8555                 if (lun->res_type != SPR_TYPE_EX_AC
8556                  && lun->res_type != SPR_TYPE_WR_EX) {
8557                         for (i = 0; i < CTL_MAX_INITIATORS; i++)
8558                                 if (lun->per_res[i+persis_offset].registered)
8559                                         lun->pending_sense[i].ua_pending |=
8560                                                 CTL_UA_RES_RELEASE;
8561                 }
8562
8563                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8564                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8565                 lun->res_type = 0;
8566                 break;
8567
8568         case CTL_PR_PREEMPT:
8569                 ctl_pro_preempt_other(lun, msg);
8570                 break;
8571         case CTL_PR_CLEAR:
8572                 lun->flags &= ~CTL_LUN_PR_RESERVED;
8573                 lun->res_type = 0;
8574                 lun->pr_key_count = 0;
8575                 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8576
8577                 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8578                         if (lun->per_res[i].registered == 0)
8579                                 continue;
8580                         if (!persis_offset
8581                          && i < CTL_MAX_INITIATORS)
8582                                 lun->pending_sense[i].ua_pending |=
8583                                         CTL_UA_RES_PREEMPT;
8584                         else if (persis_offset
8585                               && i >= persis_offset)
8586                                 lun->pending_sense[i-persis_offset].ua_pending|=
8587                                         CTL_UA_RES_PREEMPT;
8588                         memset(&lun->per_res[i].res_key, 0,
8589                                sizeof(struct scsi_per_res_key));
8590                         lun->per_res[i].registered = 0;
8591                 }
8592                 lun->PRGeneration++;
8593                 break;
8594         }
8595
8596         mtx_unlock(&lun->lun_lock);
8597 }
8598
8599 int
8600 ctl_read_write(struct ctl_scsiio *ctsio)
8601 {
8602         struct ctl_lun *lun;
8603         struct ctl_lba_len_flags *lbalen;
8604         uint64_t lba;
8605         uint32_t num_blocks;
8606         int fua, dpo;
8607         int retval;
8608         int isread;
8609
8610         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8611
8612         CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8613
8614         fua = 0;
8615         dpo = 0;
8616
8617         retval = CTL_RETVAL_COMPLETE;
8618
8619         isread = ctsio->cdb[0] == READ_6  || ctsio->cdb[0] == READ_10
8620               || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8621         if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8622                 uint32_t residx;
8623
8624                 /*
8625                  * XXX KDM need a lock here.
8626                  */
8627                 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8628                 if ((lun->res_type == SPR_TYPE_EX_AC
8629                   && residx != lun->pr_res_idx)
8630                  || ((lun->res_type == SPR_TYPE_EX_AC_RO
8631                    || lun->res_type == SPR_TYPE_EX_AC_AR)
8632                   && !lun->per_res[residx].registered)) {
8633                         ctl_set_reservation_conflict(ctsio);
8634                         ctl_done((union ctl_io *)ctsio);
8635                         return (CTL_RETVAL_COMPLETE);
8636                 }
8637         }
8638
8639         switch (ctsio->cdb[0]) {
8640         case READ_6:
8641         case WRITE_6: {
8642                 struct scsi_rw_6 *cdb;
8643
8644                 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8645
8646                 lba = scsi_3btoul(cdb->addr);
8647                 /* only 5 bits are valid in the most significant address byte */
8648                 lba &= 0x1fffff;
8649                 num_blocks = cdb->length;
8650                 /*
8651                  * This is correct according to SBC-2.
8652                  */
8653                 if (num_blocks == 0)
8654                         num_blocks = 256;
8655                 break;
8656         }
8657         case READ_10:
8658         case WRITE_10: {
8659                 struct scsi_rw_10 *cdb;
8660
8661                 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8662
8663                 if (cdb->byte2 & SRW10_FUA)
8664                         fua = 1;
8665                 if (cdb->byte2 & SRW10_DPO)
8666                         dpo = 1;
8667
8668                 lba = scsi_4btoul(cdb->addr);
8669                 num_blocks = scsi_2btoul(cdb->length);
8670                 break;
8671         }
8672         case WRITE_VERIFY_10: {
8673                 struct scsi_write_verify_10 *cdb;
8674
8675                 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8676
8677                 /*
8678                  * XXX KDM we should do actual write verify support at some
8679                  * point.  This is obviously fake, we're just translating
8680                  * things to a write.  So we don't even bother checking the
8681                  * BYTCHK field, since we don't do any verification.  If
8682                  * the user asks for it, we'll just pretend we did it.
8683                  */
8684                 if (cdb->byte2 & SWV_DPO)
8685                         dpo = 1;
8686
8687                 lba = scsi_4btoul(cdb->addr);
8688                 num_blocks = scsi_2btoul(cdb->length);
8689                 break;
8690         }
8691         case READ_12:
8692         case WRITE_12: {
8693                 struct scsi_rw_12 *cdb;
8694
8695                 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8696
8697                 if (cdb->byte2 & SRW12_FUA)
8698                         fua = 1;
8699                 if (cdb->byte2 & SRW12_DPO)
8700                         dpo = 1;
8701                 lba = scsi_4btoul(cdb->addr);
8702                 num_blocks = scsi_4btoul(cdb->length);
8703                 break;
8704         }
8705         case WRITE_VERIFY_12: {
8706                 struct scsi_write_verify_12 *cdb;
8707
8708                 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8709
8710                 if (cdb->byte2 & SWV_DPO)
8711                         dpo = 1;
8712                 
8713                 lba = scsi_4btoul(cdb->addr);
8714                 num_blocks = scsi_4btoul(cdb->length);
8715
8716                 break;
8717         }
8718         case READ_16:
8719         case WRITE_16: {
8720                 struct scsi_rw_16 *cdb;
8721
8722                 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8723
8724                 if (cdb->byte2 & SRW12_FUA)
8725                         fua = 1;
8726                 if (cdb->byte2 & SRW12_DPO)
8727                         dpo = 1;
8728
8729                 lba = scsi_8btou64(cdb->addr);
8730                 num_blocks = scsi_4btoul(cdb->length);
8731                 break;
8732         }
8733         case WRITE_VERIFY_16: {
8734                 struct scsi_write_verify_16 *cdb;
8735
8736                 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8737
8738                 if (cdb->byte2 & SWV_DPO)
8739                         dpo = 1;
8740
8741                 lba = scsi_8btou64(cdb->addr);
8742                 num_blocks = scsi_4btoul(cdb->length);
8743                 break;
8744         }
8745         default:
8746                 /*
8747                  * We got a command we don't support.  This shouldn't
8748                  * happen, commands should be filtered out above us.
8749                  */
8750                 ctl_set_invalid_opcode(ctsio);
8751                 ctl_done((union ctl_io *)ctsio);
8752
8753                 return (CTL_RETVAL_COMPLETE);
8754                 break; /* NOTREACHED */
8755         }
8756
8757         /*
8758          * XXX KDM what do we do with the DPO and FUA bits?  FUA might be
8759          * interesting for us, but if RAIDCore is in write-back mode,
8760          * getting it to do write-through for a particular transaction may
8761          * not be possible.
8762          */
8763
8764         /*
8765          * The first check is to make sure we're in bounds, the second
8766          * check is to catch wrap-around problems.  If the lba + num blocks
8767          * is less than the lba, then we've wrapped around and the block
8768          * range is invalid anyway.
8769          */
8770         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8771          || ((lba + num_blocks) < lba)) {
8772                 ctl_set_lba_out_of_range(ctsio);
8773                 ctl_done((union ctl_io *)ctsio);
8774                 return (CTL_RETVAL_COMPLETE);
8775         }
8776
8777         /*
8778          * According to SBC-3, a transfer length of 0 is not an error.
8779          * Note that this cannot happen with WRITE(6) or READ(6), since 0
8780          * translates to 256 blocks for those commands.
8781          */
8782         if (num_blocks == 0) {
8783                 ctl_set_success(ctsio);
8784                 ctl_done((union ctl_io *)ctsio);
8785                 return (CTL_RETVAL_COMPLETE);
8786         }
8787
8788         lbalen = (struct ctl_lba_len_flags *)
8789             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8790         lbalen->lba = lba;
8791         lbalen->len = num_blocks;
8792         lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
8793
8794         ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8795         ctsio->kern_rel_offset = 0;
8796
8797         CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8798
8799         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8800
8801         return (retval);
8802 }
8803
8804 static int
8805 ctl_cnw_cont(union ctl_io *io)
8806 {
8807         struct ctl_scsiio *ctsio;
8808         struct ctl_lun *lun;
8809         struct ctl_lba_len_flags *lbalen;
8810         int retval;
8811
8812         ctsio = &io->scsiio;
8813         ctsio->io_hdr.status = CTL_STATUS_NONE;
8814         ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8815         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8816         lbalen = (struct ctl_lba_len_flags *)
8817             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8818         lbalen->flags = CTL_LLF_WRITE;
8819
8820         CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8821         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8822         return (retval);
8823 }
8824
8825 int
8826 ctl_cnw(struct ctl_scsiio *ctsio)
8827 {
8828         struct ctl_lun *lun;
8829         struct ctl_lba_len_flags *lbalen;
8830         uint64_t lba;
8831         uint32_t num_blocks;
8832         int fua, dpo;
8833         int retval;
8834
8835         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8836
8837         CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8838
8839         fua = 0;
8840         dpo = 0;
8841
8842         retval = CTL_RETVAL_COMPLETE;
8843
8844         switch (ctsio->cdb[0]) {
8845         case COMPARE_AND_WRITE: {
8846                 struct scsi_compare_and_write *cdb;
8847
8848                 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8849
8850                 if (cdb->byte2 & SRW10_FUA)
8851                         fua = 1;
8852                 if (cdb->byte2 & SRW10_DPO)
8853                         dpo = 1;
8854                 lba = scsi_8btou64(cdb->addr);
8855                 num_blocks = cdb->length;
8856                 break;
8857         }
8858         default:
8859                 /*
8860                  * We got a command we don't support.  This shouldn't
8861                  * happen, commands should be filtered out above us.
8862                  */
8863                 ctl_set_invalid_opcode(ctsio);
8864                 ctl_done((union ctl_io *)ctsio);
8865
8866                 return (CTL_RETVAL_COMPLETE);
8867                 break; /* NOTREACHED */
8868         }
8869
8870         /*
8871          * XXX KDM what do we do with the DPO and FUA bits?  FUA might be
8872          * interesting for us, but if RAIDCore is in write-back mode,
8873          * getting it to do write-through for a particular transaction may
8874          * not be possible.
8875          */
8876
8877         /*
8878          * The first check is to make sure we're in bounds, the second
8879          * check is to catch wrap-around problems.  If the lba + num blocks
8880          * is less than the lba, then we've wrapped around and the block
8881          * range is invalid anyway.
8882          */
8883         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8884          || ((lba + num_blocks) < lba)) {
8885                 ctl_set_lba_out_of_range(ctsio);
8886                 ctl_done((union ctl_io *)ctsio);
8887                 return (CTL_RETVAL_COMPLETE);
8888         }
8889
8890         /*
8891          * According to SBC-3, a transfer length of 0 is not an error.
8892          */
8893         if (num_blocks == 0) {
8894                 ctl_set_success(ctsio);
8895                 ctl_done((union ctl_io *)ctsio);
8896                 return (CTL_RETVAL_COMPLETE);
8897         }
8898
8899         ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8900         ctsio->kern_rel_offset = 0;
8901
8902         /*
8903          * Set the IO_CONT flag, so that if this I/O gets passed to
8904          * ctl_data_submit_done(), it'll get passed back to
8905          * ctl_ctl_cnw_cont() for further processing.
8906          */
8907         ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8908         ctsio->io_cont = ctl_cnw_cont;
8909
8910         lbalen = (struct ctl_lba_len_flags *)
8911             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8912         lbalen->lba = lba;
8913         lbalen->len = num_blocks;
8914         lbalen->flags = CTL_LLF_COMPARE;
8915
8916         CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8917         retval = lun->backend->data_submit((union ctl_io *)ctsio);
8918         return (retval);
8919 }
8920
8921 int
8922 ctl_verify(struct ctl_scsiio *ctsio)
8923 {
8924         struct ctl_lun *lun;
8925         struct ctl_lba_len_flags *lbalen;
8926         uint64_t lba;
8927         uint32_t num_blocks;
8928         int bytchk, dpo;
8929         int retval;
8930
8931         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8932
8933         CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8934
8935         bytchk = 0;
8936         dpo = 0;
8937         retval = CTL_RETVAL_COMPLETE;
8938
8939         switch (ctsio->cdb[0]) {
8940         case VERIFY_10: {
8941                 struct scsi_verify_10 *cdb;
8942
8943                 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8944                 if (cdb->byte2 & SVFY_BYTCHK)
8945                         bytchk = 1;
8946                 if (cdb->byte2 & SVFY_DPO)
8947                         dpo = 1;
8948                 lba = scsi_4btoul(cdb->addr);
8949                 num_blocks = scsi_2btoul(cdb->length);
8950                 break;
8951         }
8952         case VERIFY_12: {
8953                 struct scsi_verify_12 *cdb;
8954
8955                 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8956                 if (cdb->byte2 & SVFY_BYTCHK)
8957                         bytchk = 1;
8958                 if (cdb->byte2 & SVFY_DPO)
8959                         dpo = 1;
8960                 lba = scsi_4btoul(cdb->addr);
8961                 num_blocks = scsi_4btoul(cdb->length);
8962                 break;
8963         }
8964         case VERIFY_16: {
8965                 struct scsi_rw_16 *cdb;
8966
8967                 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8968                 if (cdb->byte2 & SVFY_BYTCHK)
8969                         bytchk = 1;
8970                 if (cdb->byte2 & SVFY_DPO)
8971                         dpo = 1;
8972                 lba = scsi_8btou64(cdb->addr);
8973                 num_blocks = scsi_4btoul(cdb->length);
8974                 break;
8975         }
8976         default:
8977                 /*
8978                  * We got a command we don't support.  This shouldn't
8979                  * happen, commands should be filtered out above us.
8980                  */
8981                 ctl_set_invalid_opcode(ctsio);
8982                 ctl_done((union ctl_io *)ctsio);
8983                 return (CTL_RETVAL_COMPLETE);
8984         }
8985
8986         /*
8987          * The first check is to make sure we're in bounds, the second
8988          * check is to catch wrap-around problems.  If the lba + num blocks
8989          * is less than the lba, then we've wrapped around and the block
8990          * range is invalid anyway.
8991          */
8992         if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8993          || ((lba + num_blocks) < lba)) {
8994                 ctl_set_lba_out_of_range(ctsio);
8995                 ctl_done((union ctl_io *)ctsio);
8996                 return (CTL_RETVAL_COMPLETE);
8997         }
8998
8999         /*
9000          * According to SBC-3, a transfer length of 0 is not an error.
9001          */
9002         if (num_blocks == 0) {
9003                 ctl_set_success(ctsio);
9004                 ctl_done((union ctl_io *)ctsio);
9005                 return (CTL_RETVAL_COMPLETE);
9006         }
9007
9008         lbalen = (struct ctl_lba_len_flags *)
9009             &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9010         lbalen->lba = lba;
9011         lbalen->len = num_blocks;
9012         if (bytchk) {
9013                 lbalen->flags = CTL_LLF_COMPARE;
9014                 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9015         } else {
9016                 lbalen->flags = CTL_LLF_VERIFY;
9017                 ctsio->kern_total_len = 0;
9018         }
9019         ctsio->kern_rel_offset = 0;
9020
9021         CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9022         retval = lun->backend->data_submit((union ctl_io *)ctsio);
9023         return (retval);
9024 }
9025
9026 int
9027 ctl_report_luns(struct ctl_scsiio *ctsio)
9028 {
9029         struct scsi_report_luns *cdb;
9030         struct scsi_report_luns_data *lun_data;
9031         struct ctl_lun *lun, *request_lun;
9032         int num_luns, retval;
9033         uint32_t alloc_len, lun_datalen;
9034         int num_filled, well_known;
9035         uint32_t initidx, targ_lun_id, lun_id;
9036
9037         retval = CTL_RETVAL_COMPLETE;
9038         well_known = 0;
9039
9040         cdb = (struct scsi_report_luns *)ctsio->cdb;
9041
9042         CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9043
9044         mtx_lock(&control_softc->ctl_lock);
9045         num_luns = control_softc->num_luns;
9046         mtx_unlock(&control_softc->ctl_lock);
9047
9048         switch (cdb->select_report) {
9049         case RPL_REPORT_DEFAULT:
9050         case RPL_REPORT_ALL:
9051                 break;
9052         case RPL_REPORT_WELLKNOWN:
9053                 well_known = 1;
9054                 num_luns = 0;
9055                 break;
9056         default:
9057                 ctl_set_invalid_field(ctsio,
9058                                       /*sks_valid*/ 1,
9059                                       /*command*/ 1,
9060                                       /*field*/ 2,
9061                                       /*bit_valid*/ 0,
9062                                       /*bit*/ 0);
9063                 ctl_done((union ctl_io *)ctsio);
9064                 return (retval);
9065                 break; /* NOTREACHED */
9066         }
9067
9068         alloc_len = scsi_4btoul(cdb->length);
9069         /*
9070          * The initiator has to allocate at least 16 bytes for this request,
9071          * so he can at least get the header and the first LUN.  Otherwise
9072          * we reject the request (per SPC-3 rev 14, section 6.21).
9073          */
9074         if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9075             sizeof(struct scsi_report_luns_lundata))) {
9076                 ctl_set_invalid_field(ctsio,
9077                                       /*sks_valid*/ 1,
9078                                       /*command*/ 1,
9079                                       /*field*/ 6,
9080                                       /*bit_valid*/ 0,
9081                                       /*bit*/ 0);
9082                 ctl_done((union ctl_io *)ctsio);
9083                 return (retval);
9084         }
9085
9086         request_lun = (struct ctl_lun *)
9087                 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9088
9089         lun_datalen = sizeof(*lun_data) +
9090                 (num_luns * sizeof(struct scsi_report_luns_lundata));
9091
9092         ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9093         lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9094         ctsio->kern_sg_entries = 0;
9095
9096         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9097
9098         mtx_lock(&control_softc->ctl_lock);
9099         for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9100                 lun_id = targ_lun_id;
9101                 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
9102                         lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
9103                 if (lun_id >= CTL_MAX_LUNS)
9104                         continue;
9105                 lun = control_softc->ctl_luns[lun_id];
9106                 if (lun == NULL)
9107                         continue;
9108
9109                 if (targ_lun_id <= 0xff) {
9110                         /*
9111                          * Peripheral addressing method, bus number 0.
9112                          */
9113                         lun_data->luns[num_filled].lundata[0] =
9114                                 RPL_LUNDATA_ATYP_PERIPH;
9115                         lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9116                         num_filled++;
9117                 } else if (targ_lun_id <= 0x3fff) {
9118                         /*
9119                          * Flat addressing method.
9120                          */
9121                         lun_data->luns[num_filled].lundata[0] =
9122                                 RPL_LUNDATA_ATYP_FLAT |
9123                                 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9124 #ifdef OLDCTLHEADERS
9125                                 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9126                                 (targ_lun_id & SRLD_BUS_LUN_MASK);
9127 #endif
9128                         lun_data->luns[num_filled].lundata[1] =
9129 #ifdef OLDCTLHEADERS
9130                                 targ_lun_id >> SRLD_BUS_LUN_BITS;
9131 #endif
9132                                 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9133                         num_filled++;
9134                 } else {
9135                         printf("ctl_report_luns: bogus LUN number %jd, "
9136                                "skipping\n", (intmax_t)targ_lun_id);
9137                 }
9138                 /*
9139                  * According to SPC-3, rev 14 section 6.21:
9140                  *
9141                  * "The execution of a REPORT LUNS command to any valid and
9142                  * installed logical unit shall clear the REPORTED LUNS DATA
9143                  * HAS CHANGED unit attention condition for all logical
9144                  * units of that target with respect to the requesting
9145                  * initiator. A valid and installed logical unit is one
9146                  * having a PERIPHERAL QUALIFIER of 000b in the standard
9147                  * INQUIRY data (see 6.4.2)."
9148                  *
9149                  * If request_lun is NULL, the LUN this report luns command
9150                  * was issued to is either disabled or doesn't exist. In that
9151                  * case, we shouldn't clear any pending lun change unit
9152                  * attention.
9153                  */
9154                 if (request_lun != NULL) {
9155                         mtx_lock(&lun->lun_lock);
9156                         lun->pending_sense[initidx].ua_pending &=
9157                                 ~CTL_UA_LUN_CHANGE;
9158                         mtx_unlock(&lun->lun_lock);
9159                 }
9160         }
9161         mtx_unlock(&control_softc->ctl_lock);
9162
9163         /*
9164          * It's quite possible that we've returned fewer LUNs than we allocated
9165          * space for.  Trim it.
9166          */
9167         lun_datalen = sizeof(*lun_data) +
9168                 (num_filled * sizeof(struct scsi_report_luns_lundata));
9169
9170         if (lun_datalen < alloc_len) {
9171                 ctsio->residual = alloc_len - lun_datalen;
9172                 ctsio->kern_data_len = lun_datalen;
9173                 ctsio->kern_total_len = lun_datalen;
9174         } else {
9175                 ctsio->residual = 0;
9176                 ctsio->kern_data_len = alloc_len;
9177                 ctsio->kern_total_len = alloc_len;
9178         }
9179         ctsio->kern_data_resid = 0;
9180         ctsio->kern_rel_offset = 0;
9181         ctsio->kern_sg_entries = 0;
9182
9183         /*
9184          * We set this to the actual data length, regardless of how much
9185          * space we actually have to return results.  If the user looks at
9186          * this value, he'll know whether or not he allocated enough space
9187          * and reissue the command if necessary.  We don't support well
9188          * known logical units, so if the user asks for that, return none.
9189          */
9190         scsi_ulto4b(lun_datalen - 8, lun_data->length);
9191
9192         /*
9193          * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9194          * this request.
9195          */
9196         ctsio->scsi_status = SCSI_STATUS_OK;
9197
9198         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9199         ctsio->be_move_done = ctl_config_move_done;
9200         ctl_datamove((union ctl_io *)ctsio);
9201
9202         return (retval);
9203 }
9204
9205 int
9206 ctl_request_sense(struct ctl_scsiio *ctsio)
9207 {
9208         struct scsi_request_sense *cdb;
9209         struct scsi_sense_data *sense_ptr;
9210         struct ctl_lun *lun;
9211         uint32_t initidx;
9212         int have_error;
9213         scsi_sense_data_type sense_format;
9214
9215         cdb = (struct scsi_request_sense *)ctsio->cdb;
9216
9217         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9218
9219         CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9220
9221         /*
9222          * Determine which sense format the user wants.
9223          */
9224         if (cdb->byte2 & SRS_DESC)
9225                 sense_format = SSD_TYPE_DESC;
9226         else
9227                 sense_format = SSD_TYPE_FIXED;
9228
9229         ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9230         sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9231         ctsio->kern_sg_entries = 0;
9232
9233         /*
9234          * struct scsi_sense_data, which is currently set to 256 bytes, is
9235          * larger than the largest allowed value for the length field in the
9236          * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9237          */
9238         ctsio->residual = 0;
9239         ctsio->kern_data_len = cdb->length;
9240         ctsio->kern_total_len = cdb->length;
9241
9242         ctsio->kern_data_resid = 0;
9243         ctsio->kern_rel_offset = 0;
9244         ctsio->kern_sg_entries = 0;
9245
9246         /*
9247          * If we don't have a LUN, we don't have any pending sense.
9248          */
9249         if (lun == NULL)
9250                 goto no_sense;
9251
9252         have_error = 0;
9253         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9254         /*
9255          * Check for pending sense, and then for pending unit attentions.
9256          * Pending sense gets returned first, then pending unit attentions.
9257          */
9258         mtx_lock(&lun->lun_lock);
9259         if (ctl_is_set(lun->have_ca, initidx)) {
9260                 scsi_sense_data_type stored_format;
9261
9262                 /*
9263                  * Check to see which sense format was used for the stored
9264                  * sense data.
9265                  */
9266                 stored_format = scsi_sense_type(
9267                     &lun->pending_sense[initidx].sense);
9268
9269                 /*
9270                  * If the user requested a different sense format than the
9271                  * one we stored, then we need to convert it to the other
9272                  * format.  If we're going from descriptor to fixed format
9273                  * sense data, we may lose things in translation, depending
9274                  * on what options were used.
9275                  *
9276                  * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9277                  * for some reason we'll just copy it out as-is.
9278                  */
9279                 if ((stored_format == SSD_TYPE_FIXED)
9280                  && (sense_format == SSD_TYPE_DESC))
9281                         ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9282                             &lun->pending_sense[initidx].sense,
9283                             (struct scsi_sense_data_desc *)sense_ptr);
9284                 else if ((stored_format == SSD_TYPE_DESC)
9285                       && (sense_format == SSD_TYPE_FIXED))
9286                         ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9287                             &lun->pending_sense[initidx].sense,
9288                             (struct scsi_sense_data_fixed *)sense_ptr);
9289                 else
9290                         memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9291                                ctl_min(sizeof(*sense_ptr),
9292                                sizeof(lun->pending_sense[initidx].sense)));
9293
9294                 ctl_clear_mask(lun->have_ca, initidx);
9295                 have_error = 1;
9296         } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9297                 ctl_ua_type ua_type;
9298
9299                 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9300                                        sense_ptr, sense_format);
9301                 if (ua_type != CTL_UA_NONE) {
9302                         have_error = 1;
9303                         /* We're reporting this UA, so clear it */
9304                         lun->pending_sense[initidx].ua_pending &= ~ua_type;
9305                 }
9306         }
9307         mtx_unlock(&lun->lun_lock);
9308
9309         /*
9310          * We already have a pending error, return it.
9311          */
9312         if (have_error != 0) {
9313                 /*
9314                  * We report the SCSI status as OK, since the status of the
9315                  * request sense command itself is OK.
9316                  */
9317                 ctsio->scsi_status = SCSI_STATUS_OK;
9318
9319                 /*
9320                  * We report 0 for the sense length, because we aren't doing
9321                  * autosense in this case.  We're reporting sense as
9322                  * parameter data.
9323                  */
9324                 ctsio->sense_len = 0;
9325                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9326                 ctsio->be_move_done = ctl_config_move_done;
9327                 ctl_datamove((union ctl_io *)ctsio);
9328
9329                 return (CTL_RETVAL_COMPLETE);
9330         }
9331
9332 no_sense:
9333
9334         /*
9335          * No sense information to report, so we report that everything is
9336          * okay.
9337          */
9338         ctl_set_sense_data(sense_ptr,
9339                            lun,
9340                            sense_format,
9341                            /*current_error*/ 1,
9342                            /*sense_key*/ SSD_KEY_NO_SENSE,
9343                            /*asc*/ 0x00,
9344                            /*ascq*/ 0x00,
9345                            SSD_ELEM_NONE);
9346
9347         ctsio->scsi_status = SCSI_STATUS_OK;
9348
9349         /*
9350          * We report 0 for the sense length, because we aren't doing
9351          * autosense in this case.  We're reporting sense as parameter data.
9352          */
9353         ctsio->sense_len = 0;
9354         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9355         ctsio->be_move_done = ctl_config_move_done;
9356         ctl_datamove((union ctl_io *)ctsio);
9357
9358         return (CTL_RETVAL_COMPLETE);
9359 }
9360
9361 int
9362 ctl_tur(struct ctl_scsiio *ctsio)
9363 {
9364         struct ctl_lun *lun;
9365
9366         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9367
9368         CTL_DEBUG_PRINT(("ctl_tur\n"));
9369
9370         if (lun == NULL)
9371                 return (EINVAL);
9372
9373         ctsio->scsi_status = SCSI_STATUS_OK;
9374         ctsio->io_hdr.status = CTL_SUCCESS;
9375
9376         ctl_done((union ctl_io *)ctsio);
9377
9378         return (CTL_RETVAL_COMPLETE);
9379 }
9380
9381 #ifdef notyet
9382 static int
9383 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9384 {
9385
9386 }
9387 #endif
9388
9389 static int
9390 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9391 {
9392         struct scsi_vpd_supported_pages *pages;
9393         int sup_page_size;
9394         struct ctl_lun *lun;
9395
9396         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9397
9398         sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9399             SCSI_EVPD_NUM_SUPPORTED_PAGES;
9400         ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9401         pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9402         ctsio->kern_sg_entries = 0;
9403
9404         if (sup_page_size < alloc_len) {
9405                 ctsio->residual = alloc_len - sup_page_size;
9406                 ctsio->kern_data_len = sup_page_size;
9407                 ctsio->kern_total_len = sup_page_size;
9408         } else {
9409                 ctsio->residual = 0;
9410                 ctsio->kern_data_len = alloc_len;
9411                 ctsio->kern_total_len = alloc_len;
9412         }
9413         ctsio->kern_data_resid = 0;
9414         ctsio->kern_rel_offset = 0;
9415         ctsio->kern_sg_entries = 0;
9416
9417         /*
9418          * The control device is always connected.  The disk device, on the
9419          * other hand, may not be online all the time.  Need to change this
9420          * to figure out whether the disk device is actually online or not.
9421          */
9422         if (lun != NULL)
9423                 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9424                                 lun->be_lun->lun_type;
9425         else
9426                 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9427
9428         pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9429         /* Supported VPD pages */
9430         pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9431         /* Serial Number */
9432         pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9433         /* Device Identification */
9434         pages->page_list[2] = SVPD_DEVICE_ID;
9435         /* Block limits */
9436         pages->page_list[3] = SVPD_BLOCK_LIMITS;
9437         /* Logical Block Provisioning */
9438         pages->page_list[4] = SVPD_LBP;
9439
9440         ctsio->scsi_status = SCSI_STATUS_OK;
9441
9442         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9443         ctsio->be_move_done = ctl_config_move_done;
9444         ctl_datamove((union ctl_io *)ctsio);
9445
9446         return (CTL_RETVAL_COMPLETE);
9447 }
9448
9449 static int
9450 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9451 {
9452         struct scsi_vpd_unit_serial_number *sn_ptr;
9453         struct ctl_lun *lun;
9454
9455         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9456
9457         ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9458         sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9459         ctsio->kern_sg_entries = 0;
9460
9461         if (sizeof(*sn_ptr) < alloc_len) {
9462                 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9463                 ctsio->kern_data_len = sizeof(*sn_ptr);
9464                 ctsio->kern_total_len = sizeof(*sn_ptr);
9465         } else {
9466                 ctsio->residual = 0;
9467                 ctsio->kern_data_len = alloc_len;
9468                 ctsio->kern_total_len = alloc_len;
9469         }
9470         ctsio->kern_data_resid = 0;
9471         ctsio->kern_rel_offset = 0;
9472         ctsio->kern_sg_entries = 0;
9473
9474         /*
9475          * The control device is always connected.  The disk device, on the
9476          * other hand, may not be online all the time.  Need to change this
9477          * to figure out whether the disk device is actually online or not.
9478          */
9479         if (lun != NULL)
9480                 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9481                                   lun->be_lun->lun_type;
9482         else
9483                 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9484
9485         sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9486         sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9487         /*
9488          * If we don't have a LUN, we just leave the serial number as
9489          * all spaces.
9490          */
9491         memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9492         if (lun != NULL) {
9493                 strncpy((char *)sn_ptr->serial_num,
9494                         (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9495         }
9496         ctsio->scsi_status = SCSI_STATUS_OK;
9497
9498         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9499         ctsio->be_move_done = ctl_config_move_done;
9500         ctl_datamove((union ctl_io *)ctsio);
9501
9502         return (CTL_RETVAL_COMPLETE);
9503 }
9504
9505
9506 static int
9507 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9508 {
9509         struct scsi_vpd_device_id *devid_ptr;
9510         struct scsi_vpd_id_descriptor *desc, *desc1;
9511         struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9512         struct scsi_vpd_id_t10 *t10id;
9513         struct ctl_softc *ctl_softc;
9514         struct ctl_lun *lun;
9515         struct ctl_frontend *fe;
9516         char *val;
9517         int data_len, devid_len;
9518
9519         ctl_softc = control_softc;
9520
9521         fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9522
9523         if (fe->devid != NULL)
9524                 return ((fe->devid)(ctsio, alloc_len));
9525
9526         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9527
9528         if (lun == NULL) {
9529                 devid_len = CTL_DEVID_MIN_LEN;
9530         } else {
9531                 devid_len = max(CTL_DEVID_MIN_LEN,
9532                     strnlen(lun->be_lun->device_id, CTL_DEVID_LEN));
9533         }
9534
9535         data_len = sizeof(struct scsi_vpd_device_id) +
9536                 sizeof(struct scsi_vpd_id_descriptor) +
9537                 sizeof(struct scsi_vpd_id_t10) + devid_len +
9538                 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9539                 sizeof(struct scsi_vpd_id_descriptor) +
9540                 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9541                 sizeof(struct scsi_vpd_id_descriptor) +
9542                 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9543
9544         ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9545         devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9546         ctsio->kern_sg_entries = 0;
9547
9548         if (data_len < alloc_len) {
9549                 ctsio->residual = alloc_len - data_len;
9550                 ctsio->kern_data_len = data_len;
9551                 ctsio->kern_total_len = data_len;
9552         } else {
9553                 ctsio->residual = 0;
9554                 ctsio->kern_data_len = alloc_len;
9555                 ctsio->kern_total_len = alloc_len;
9556         }
9557         ctsio->kern_data_resid = 0;
9558         ctsio->kern_rel_offset = 0;
9559         ctsio->kern_sg_entries = 0;
9560
9561         desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9562         t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9563         desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9564                 sizeof(struct scsi_vpd_id_t10) + devid_len);
9565         desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9566                   CTL_WWPN_LEN);
9567         desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9568                  sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9569
9570         /*
9571          * The control device is always connected.  The disk device, on the
9572          * other hand, may not be online all the time.
9573          */
9574         if (lun != NULL)
9575                 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9576                                      lun->be_lun->lun_type;
9577         else
9578                 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9579
9580         devid_ptr->page_code = SVPD_DEVICE_ID;
9581
9582         scsi_ulto2b(data_len - 4, devid_ptr->length);
9583
9584         /*
9585          * For Fibre channel,
9586          */
9587         if (fe->port_type == CTL_PORT_FC)
9588         {
9589                 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9590                                       SVPD_ID_CODESET_ASCII;
9591                 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9592                               SVPD_ID_CODESET_BINARY;
9593         }
9594         else
9595         {
9596                 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9597                                       SVPD_ID_CODESET_ASCII;
9598                 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9599                               SVPD_ID_CODESET_BINARY;
9600         }
9601         desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9602
9603         /*
9604          * We're using a LUN association here.  i.e., this device ID is a
9605          * per-LUN identifier.
9606          */
9607         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9608         desc->length = sizeof(*t10id) + devid_len;
9609         if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
9610                 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9611         } else {
9612                 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
9613                 strncpy(t10id->vendor, val,
9614                     min(sizeof(t10id->vendor), strlen(val)));
9615         }
9616
9617         /*
9618          * desc1 is for the WWPN which is a port asscociation.
9619          */
9620         desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9621         desc1->length = CTL_WWPN_LEN;
9622         /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9623         /* For testing just create the WWPN */
9624 #if 0
9625         ddb_GetWWNN((char *)desc1->identifier);
9626
9627         /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9628         /* This is so Copancontrol will return something sane */
9629         if (ctsio->io_hdr.nexus.targ_port!=0 &&
9630             ctsio->io_hdr.nexus.targ_port!=8)
9631                 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9632         else
9633                 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9634 #endif
9635
9636         be64enc(desc1->identifier, fe->wwpn);
9637
9638         /*
9639          * desc2 is for the Relative Target Port(type 4h) identifier
9640          */
9641         desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9642                          | SVPD_ID_TYPE_RELTARG;
9643         desc2->length = 4;
9644 //#if 0
9645         /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9646         /* This is so Copancontrol will return something sane */
9647         if (ctsio->io_hdr.nexus.targ_port!=0 &&
9648             ctsio->io_hdr.nexus.targ_port!=8)
9649                 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9650         else
9651                 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9652 //#endif
9653
9654         /*
9655          * desc3 is for the Target Port Group(type 5h) identifier
9656          */
9657         desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9658                          | SVPD_ID_TYPE_TPORTGRP;
9659         desc3->length = 4;
9660         if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9661                 desc3->identifier[3] = 1;
9662         else
9663                 desc3->identifier[3] = 2;
9664
9665         /*
9666          * If we've actually got a backend, copy the device id from the
9667          * per-LUN data.  Otherwise, set it to all spaces.
9668          */
9669         if (lun != NULL) {
9670                 /*
9671                  * Copy the backend's LUN ID.
9672                  */
9673                 strncpy((char *)t10id->vendor_spec_id,
9674                         (char *)lun->be_lun->device_id, devid_len);
9675         } else {
9676                 /*
9677                  * No backend, set this to spaces.
9678                  */
9679                 memset(t10id->vendor_spec_id, 0x20, devid_len);
9680         }
9681
9682         ctsio->scsi_status = SCSI_STATUS_OK;
9683
9684         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9685         ctsio->be_move_done = ctl_config_move_done;
9686         ctl_datamove((union ctl_io *)ctsio);
9687
9688         return (CTL_RETVAL_COMPLETE);
9689 }
9690
9691 static int
9692 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9693 {
9694         struct scsi_vpd_block_limits *bl_ptr;
9695         struct ctl_lun *lun;
9696         int bs;
9697
9698         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9699         bs = lun->be_lun->blocksize;
9700
9701         ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9702         bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9703         ctsio->kern_sg_entries = 0;
9704
9705         if (sizeof(*bl_ptr) < alloc_len) {
9706                 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9707                 ctsio->kern_data_len = sizeof(*bl_ptr);
9708                 ctsio->kern_total_len = sizeof(*bl_ptr);
9709         } else {
9710                 ctsio->residual = 0;
9711                 ctsio->kern_data_len = alloc_len;
9712                 ctsio->kern_total_len = alloc_len;
9713         }
9714         ctsio->kern_data_resid = 0;
9715         ctsio->kern_rel_offset = 0;
9716         ctsio->kern_sg_entries = 0;
9717
9718         /*
9719          * The control device is always connected.  The disk device, on the
9720          * other hand, may not be online all the time.  Need to change this
9721          * to figure out whether the disk device is actually online or not.
9722          */
9723         if (lun != NULL)
9724                 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9725                                   lun->be_lun->lun_type;
9726         else
9727                 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9728
9729         bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9730         scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9731         bl_ptr->max_cmp_write_len = 0xff;
9732         scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9733         scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9734         if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9735                 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9736                 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9737         }
9738         scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9739
9740         ctsio->scsi_status = SCSI_STATUS_OK;
9741         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9742         ctsio->be_move_done = ctl_config_move_done;
9743         ctl_datamove((union ctl_io *)ctsio);
9744
9745         return (CTL_RETVAL_COMPLETE);
9746 }
9747
9748 static int
9749 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9750 {
9751         struct scsi_vpd_logical_block_prov *lbp_ptr;
9752         struct ctl_lun *lun;
9753         int bs;
9754
9755         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9756         bs = lun->be_lun->blocksize;
9757
9758         ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9759         lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9760         ctsio->kern_sg_entries = 0;
9761
9762         if (sizeof(*lbp_ptr) < alloc_len) {
9763                 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9764                 ctsio->kern_data_len = sizeof(*lbp_ptr);
9765                 ctsio->kern_total_len = sizeof(*lbp_ptr);
9766         } else {
9767                 ctsio->residual = 0;
9768                 ctsio->kern_data_len = alloc_len;
9769                 ctsio->kern_total_len = alloc_len;
9770         }
9771         ctsio->kern_data_resid = 0;
9772         ctsio->kern_rel_offset = 0;
9773         ctsio->kern_sg_entries = 0;
9774
9775         /*
9776          * The control device is always connected.  The disk device, on the
9777          * other hand, may not be online all the time.  Need to change this
9778          * to figure out whether the disk device is actually online or not.
9779          */
9780         if (lun != NULL)
9781                 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9782                                   lun->be_lun->lun_type;
9783         else
9784                 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9785
9786         lbp_ptr->page_code = SVPD_LBP;
9787         if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9788                 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9789
9790         ctsio->scsi_status = SCSI_STATUS_OK;
9791         ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9792         ctsio->be_move_done = ctl_config_move_done;
9793         ctl_datamove((union ctl_io *)ctsio);
9794
9795         return (CTL_RETVAL_COMPLETE);
9796 }
9797
9798 static int
9799 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9800 {
9801         struct scsi_inquiry *cdb;
9802         struct ctl_lun *lun;
9803         int alloc_len, retval;
9804
9805         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9806         cdb = (struct scsi_inquiry *)ctsio->cdb;
9807
9808         retval = CTL_RETVAL_COMPLETE;
9809
9810         alloc_len = scsi_2btoul(cdb->length);
9811
9812         switch (cdb->page_code) {
9813         case SVPD_SUPPORTED_PAGES:
9814                 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9815                 break;
9816         case SVPD_UNIT_SERIAL_NUMBER:
9817                 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9818                 break;
9819         case SVPD_DEVICE_ID:
9820                 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9821                 break;
9822         case SVPD_BLOCK_LIMITS:
9823                 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9824                 break;
9825         case SVPD_LBP:
9826                 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9827                 break;
9828         default:
9829                 ctl_set_invalid_field(ctsio,
9830                                       /*sks_valid*/ 1,
9831                                       /*command*/ 1,
9832                                       /*field*/ 2,
9833                                       /*bit_valid*/ 0,
9834                                       /*bit*/ 0);
9835                 ctl_done((union ctl_io *)ctsio);
9836                 retval = CTL_RETVAL_COMPLETE;
9837                 break;
9838         }
9839
9840         return (retval);
9841 }
9842
9843 static int
9844 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9845 {
9846         struct scsi_inquiry_data *inq_ptr;
9847         struct scsi_inquiry *cdb;
9848         struct ctl_softc *ctl_softc;
9849         struct ctl_lun *lun;
9850         char *val;
9851         uint32_t alloc_len;
9852         int is_fc;
9853
9854         ctl_softc = control_softc;
9855
9856         /*
9857          * Figure out whether we're talking to a Fibre Channel port or not.
9858          * We treat the ioctl front end, and any SCSI adapters, as packetized
9859          * SCSI front ends.
9860          */
9861         if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9862             CTL_PORT_FC)
9863                 is_fc = 0;
9864         else
9865                 is_fc = 1;
9866
9867         lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9868         cdb = (struct scsi_inquiry *)ctsio->cdb;
9869         alloc_len = scsi_2btoul(cdb->length);
9870
9871         /*
9872          * We malloc the full inquiry data size here and fill it
9873          * in.  If the user only asks for less, we'll give him
9874          * that much.
9875          */
9876         ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9877         inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9878         ctsio->kern_sg_entries = 0;
9879         ctsio->kern_data_resid = 0;
9880         ctsio->kern_rel_offset = 0;
9881
9882         if (sizeof(*inq_ptr) < alloc_len) {
9883                 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9884                 ctsio->kern_data_len = sizeof(*inq_ptr);
9885                 ctsio->kern_total_len = sizeof(*inq_ptr);
9886         } else {
9887                 ctsio->residual = 0;
9888                 ctsio->kern_data_len = alloc_len;
9889                 ctsio->kern_total_len = alloc_len;
9890         }
9891
9892         /*
9893          * If we have a LUN configured, report it as connected.  Otherwise,
9894          * report that it is offline or no device is supported, depending 
9895          * on the value of inquiry_pq_no_lun.
9896          *
9897          * According to the spec (SPC-4 r34), the peripheral qualifier
9898          * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9899          *
9900          * "A peripheral device having the specified peripheral device type 
9901          * is not connected to this logical unit. However, the device
9902          * server is capable of supporting the specified peripheral device
9903          * type on this logical unit."
9904          *
9905          * According to the same spec, the peripheral qualifier
9906          * SID_QUAL_BAD_LU (011b) is used in this scenario:
9907          *
9908          * "The device server is not capable of supporting a peripheral
9909          * device on this logical unit. For this peripheral qualifier the
9910          * peripheral device type shall be set to 1Fh. All other peripheral
9911          * device type values are reserved for this peripheral qualifier."
9912          *
9913          * Given the text, it would seem that we probably want to report that
9914          * the LUN is offline here.  There is no LUN connected, but we can
9915          * support a LUN at the given LUN number.
9916          *
9917          * In the real world, though, it sounds like things are a little
9918          * different:
9919          *
9920          * - Linux, when presented with a LUN with the offline peripheral
9921          *   qualifier, will create an sg driver instance for it.  So when
9922          *   you attach it to CTL, you wind up with a ton of sg driver
9923          *   instances.  (One for every LUN that Linux bothered to probe.)
9924          *   Linux does this despite the fact that it issues a REPORT LUNs
9925          *   to LUN 0 to get the inventory of supported LUNs.
9926          *
9927          * - There is other anecdotal evidence (from Emulex folks) about
9928          *   arrays that use the offline peripheral qualifier for LUNs that
9929          *   are on the "passive" path in an active/passive array.
9930          *
9931          * So the solution is provide a hopefully reasonable default
9932          * (return bad/no LUN) and allow the user to change the behavior
9933          * with a tunable/sysctl variable.
9934          */
9935         if (lun != NULL)
9936                 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9937                                   lun->be_lun->lun_type;
9938         else if (ctl_softc->inquiry_pq_no_lun == 0)
9939                 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9940         else
9941                 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9942
9943         /* RMB in byte 2 is 0 */
9944         inq_ptr->version = SCSI_REV_SPC3;
9945
9946         /*
9947          * According to SAM-3, even if a device only supports a single
9948          * level of LUN addressing, it should still set the HISUP bit:
9949          *
9950          * 4.9.1 Logical unit numbers overview
9951          *
9952          * All logical unit number formats described in this standard are
9953          * hierarchical in structure even when only a single level in that
9954          * hierarchy is used. The HISUP bit shall be set to one in the
9955          * standard INQUIRY data (see SPC-2) when any logical unit number
9956          * format described in this standard is used.  Non-hierarchical
9957          * formats are outside the scope of this standard.
9958          *
9959          * Therefore we set the HiSup bit here.
9960          *
9961          * The reponse format is 2, per SPC-3.
9962          */
9963         inq_ptr->response_format = SID_HiSup | 2;
9964
9965         inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9966         CTL_DEBUG_PRINT(("additional_length = %d\n",
9967                          inq_ptr->additional_length));
9968
9969         inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9970         /* 16 bit addressing */
9971         if (is_fc == 0)
9972                 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9973         /* XXX set the SID_MultiP bit here if we're actually going to
9974            respond on multiple ports */
9975         inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9976
9977         /* 16 bit data bus, synchronous transfers */
9978         /* XXX these flags don't apply for FC */
9979         if (is_fc == 0)
9980                 inq_ptr->flags = SID_WBus16 | SID_Sync;
9981         /*
9982          * XXX KDM do we want to support tagged queueing on the control
9983          * device at all?
9984          */
9985         if ((lun == NULL)
9986          || (lun->be_lun->lun_type != T_PROCESSOR))
9987                 inq_ptr->flags |= SID_CmdQue;
9988         /*
9989          * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9990          * We have 8 bytes for the vendor name, and 16 bytes for the device
9991          * name and 4 bytes for the revision.
9992          */
9993         if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
9994                 strcpy(inq_ptr->vendor, CTL_VENDOR);
9995         } else {
9996                 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
9997                 strncpy(inq_ptr->vendor, val,
9998                     min(sizeof(inq_ptr->vendor), strlen(val)));
9999         }
10000         if (lun == NULL) {
10001                 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10002         } else if ((val = ctl_get_opt(lun->be_lun, "product")) == NULL) {
10003                 switch (lun->be_lun->lun_type) {
10004                 case T_DIRECT:
10005                         strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10006                         break;
10007                 case T_PROCESSOR:
10008                         strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
10009                         break;
10010                 default:
10011                         strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
10012                         break;
10013                 }
10014         } else {
10015                 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10016                 strncpy(inq_ptr->product, val,
10017                     min(sizeof(inq_ptr->product), strlen(val)));
10018         }
10019
10020         /*
10021          * XXX make this a macro somewhere so it automatically gets
10022          * incremented when we make changes.
10023          */
10024         if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "revision")) == NULL) {
10025                 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10026         } else {
10027                 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10028                 strncpy(inq_ptr->revision, val,
10029                     min(sizeof(inq_ptr->revision), strlen(val)));
10030         }
10031
10032         /*
10033          * For parallel SCSI, we support double transition and single
10034          * transition clocking.  We also support QAS (Quick Arbitration
10035          * and Selection) and Information Unit transfers on both the
10036          * control and array devices.
10037          */
10038         if (is_fc == 0)
10039                 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10040                                     SID_SPI_IUS;
10041
10042         /* SAM-3 */
10043         scsi_ulto2b(0x0060, inq_ptr->version1);
10044         /* SPC-3 (no version claimed) XXX should we claim a version? */
10045         scsi_ulto2b(0x0300, inq_ptr->version2);
10046         if (is_fc) {
10047                 /* FCP-2 ANSI INCITS.350:2003 */
10048                 scsi_ulto2b(0x0917, inq_ptr->version3);
10049         } else {
10050                 /* SPI-4 ANSI INCITS.362:200x */
10051                 scsi_ulto2b(0x0B56, inq_ptr->version3);
10052         }
10053
10054         if (lun == NULL) {
10055                 /* SBC-2 (no version claimed) XXX should we claim a version? */
10056                 scsi_ulto2b(0x0320, inq_ptr->version4);
10057         } else {
10058                 switch (lun->be_lun->lun_type) {
10059                 case T_DIRECT:
10060                         /*
10061                          * SBC-2 (no version claimed) XXX should we claim a
10062                          * version?
10063                          */
10064                         scsi_ulto2b(0x0320, inq_ptr->version4);
10065                         break;
10066                 case T_PROCESSOR:
10067                 default:
10068                         break;
10069                 }
10070         }
10071
10072         ctsio->scsi_status = SCSI_STATUS_OK;
10073         if (ctsio->kern_data_len > 0) {
10074                 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10075                 ctsio->be_move_done = ctl_config_move_done;
10076                 ctl_datamove((union ctl_io *)ctsio);
10077         } else {
10078                 ctsio->io_hdr.status = CTL_SUCCESS;
10079                 ctl_done((union ctl_io *)ctsio);
10080         }
10081
10082         return (CTL_RETVAL_COMPLETE);
10083 }
10084
10085 int
10086 ctl_inquiry(struct ctl_scsiio *ctsio)
10087 {
10088         struct scsi_inquiry *cdb;
10089         int retval;
10090
10091         cdb = (struct scsi_inquiry *)ctsio->cdb;
10092
10093         retval = 0;
10094
10095         CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10096
10097         /*
10098          * Right now, we don't support the CmdDt inquiry information.
10099          * This would be nice to support in the future.  When we do
10100          * support it, we should change this test so that it checks to make
10101          * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10102          */
10103 #ifdef notyet
10104         if (((cdb->byte2 & SI_EVPD)
10105          && (cdb->byte2 & SI_CMDDT)))
10106 #endif
10107         if (cdb->byte2 & SI_CMDDT) {
10108                 /*
10109                  * Point to the SI_CMDDT bit.  We might change this
10110                  * when we support SI_CMDDT, but since both bits would be
10111                  * "wrong", this should probably just stay as-is then.
10112                  */
10113                 ctl_set_invalid_field(ctsio,
10114                                       /*sks_valid*/ 1,
10115                                       /*command*/ 1,
10116                                       /*field*/ 1,
10117                                       /*bit_valid*/ 1,
10118                                       /*bit*/ 1);
10119                 ctl_done((union ctl_io *)ctsio);
10120                 return (CTL_RETVAL_COMPLETE);
10121         }
10122         if (cdb->byte2 & SI_EVPD)
10123                 retval = ctl_inquiry_evpd(ctsio);
10124 #ifdef notyet
10125         else if (cdb->byte2 & SI_CMDDT)
10126                 retval = ctl_inquiry_cmddt(ctsio);
10127 #endif
10128         else
10129                 retval = ctl_inquiry_std(ctsio);
10130
10131         return (retval);
10132 }
10133
10134 /*
10135  * For known CDB types, parse the LBA and length.
10136  */
10137 static int
10138 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10139 {
10140         if (io->io_hdr.io_type != CTL_IO_SCSI)
10141                 return (1);
10142
10143         switch (io->scsiio.cdb[0]) {
10144         case COMPARE_AND_WRITE: {
10145                 struct scsi_compare_and_write *cdb;
10146
10147                 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10148
10149                 *lba = scsi_8btou64(cdb->addr);
10150                 *len = cdb->length;
10151                 break;
10152         }
10153         case READ_6:
10154         case WRITE_6: {
10155                 struct scsi_rw_6 *cdb;
10156
10157                 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10158
10159                 *lba = scsi_3btoul(cdb->addr);
10160                 /* only 5 bits are valid in the most significant address byte */
10161                 *lba &= 0x1fffff;
10162                 *len = cdb->length;
10163                 break;
10164         }
10165         case READ_10:
10166         case WRITE_10: {
10167                 struct scsi_rw_10 *cdb;
10168
10169                 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10170
10171                 *lba = scsi_4btoul(cdb->addr);
10172                 *len = scsi_2btoul(cdb->length);
10173                 break;
10174         }
10175         case WRITE_VERIFY_10: {
10176                 struct scsi_write_verify_10 *cdb;
10177
10178                 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10179
10180                 *lba = scsi_4btoul(cdb->addr);
10181                 *len = scsi_2btoul(cdb->length);
10182                 break;
10183         }
10184         case READ_12:
10185         case WRITE_12: {
10186                 struct scsi_rw_12 *cdb;
10187
10188                 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10189
10190                 *lba = scsi_4btoul(cdb->addr);
10191                 *len = scsi_4btoul(cdb->length);
10192                 break;
10193         }
10194         case WRITE_VERIFY_12: {
10195                 struct scsi_write_verify_12 *cdb;
10196
10197                 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10198
10199                 *lba = scsi_4btoul(cdb->addr);
10200                 *len = scsi_4btoul(cdb->length);
10201                 break;
10202         }
10203         case READ_16:
10204         case WRITE_16: {
10205                 struct scsi_rw_16 *cdb;
10206
10207                 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10208
10209                 *lba = scsi_8btou64(cdb->addr);
10210                 *len = scsi_4btoul(cdb->length);
10211                 break;
10212         }
10213         case WRITE_VERIFY_16: {
10214                 struct scsi_write_verify_16 *cdb;
10215
10216                 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10217
10218                 
10219                 *lba = scsi_8btou64(cdb->addr);
10220                 *len = scsi_4btoul(cdb->length);
10221                 break;
10222         }
10223         case WRITE_SAME_10: {
10224                 struct scsi_write_same_10 *cdb;
10225
10226                 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10227
10228                 *lba = scsi_4btoul(cdb->addr);
10229                 *len = scsi_2btoul(cdb->length);
10230                 break;
10231         }
10232         case WRITE_SAME_16: {
10233                 struct scsi_write_same_16 *cdb;
10234
10235                 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10236
10237                 *lba = scsi_8btou64(cdb->addr);
10238                 *len = scsi_4btoul(cdb->length);
10239                 break;
10240         }
10241         case VERIFY_10: {
10242                 struct scsi_verify_10 *cdb;
10243
10244                 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10245
10246                 *lba = scsi_4btoul(cdb->addr);
10247                 *len = scsi_2btoul(cdb->length);
10248                 break;
10249         }
10250         case VERIFY_12: {
10251                 struct scsi_verify_12 *cdb;
10252
10253                 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10254
10255                 *lba = scsi_4btoul(cdb->addr);
10256                 *len = scsi_4btoul(cdb->length);
10257                 break;
10258         }
10259         case VERIFY_16: {
10260                 struct scsi_verify_16 *cdb;
10261
10262                 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10263
10264                 *lba = scsi_8btou64(cdb->addr);
10265                 *len = scsi_4btoul(cdb->length);
10266                 break;
10267         }
10268         default:
10269                 return (1);
10270                 break; /* NOTREACHED */
10271         }
10272
10273         return (0);
10274 }
10275
10276 static ctl_action
10277 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10278 {
10279         uint64_t endlba1, endlba2;
10280
10281         endlba1 = lba1 + len1 - 1;
10282         endlba2 = lba2 + len2 - 1;
10283
10284         if ((endlba1 < lba2)
10285          || (endlba2 < lba1))
10286                 return (CTL_ACTION_PASS);
10287         else
10288                 return (CTL_ACTION_BLOCK);
10289 }
10290
10291 static ctl_action
10292 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10293 {
10294         uint64_t lba1, lba2;
10295         uint32_t len1, len2;
10296         int retval;
10297
10298         retval = ctl_get_lba_len(io1, &lba1, &len1);
10299         if (retval != 0)
10300                 return (CTL_ACTION_ERROR);
10301
10302         retval = ctl_get_lba_len(io2, &lba2, &len2);
10303         if (retval != 0)
10304                 return (CTL_ACTION_ERROR);
10305
10306         return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10307 }
10308
10309 static ctl_action
10310 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10311 {
10312         const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10313         ctl_serialize_action *serialize_row;
10314
10315         /*
10316          * The initiator attempted multiple untagged commands at the same
10317          * time.  Can't do that.
10318          */
10319         if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10320          && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10321          && ((pending_io->io_hdr.nexus.targ_port ==
10322               ooa_io->io_hdr.nexus.targ_port)
10323           && (pending_io->io_hdr.nexus.initid.id ==
10324               ooa_io->io_hdr.nexus.initid.id))
10325          && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10326                 return (CTL_ACTION_OVERLAP);
10327
10328         /*
10329          * The initiator attempted to send multiple tagged commands with
10330          * the same ID.  (It's fine if different initiators have the same
10331          * tag ID.)
10332          *
10333          * Even if all of those conditions are true, we don't kill the I/O
10334          * if the command ahead of us has been aborted.  We won't end up
10335          * sending it to the FETD, and it's perfectly legal to resend a
10336          * command with the same tag number as long as the previous
10337          * instance of this tag number has been aborted somehow.
10338          */
10339         if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10340          && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10341          && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10342          && ((pending_io->io_hdr.nexus.targ_port ==
10343               ooa_io->io_hdr.nexus.targ_port)
10344           && (pending_io->io_hdr.nexus.initid.id ==
10345               ooa_io->io_hdr.nexus.initid.id))
10346          && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10347                 return (CTL_ACTION_OVERLAP_TAG);
10348
10349         /*
10350          * If we get a head of queue tag, SAM-3 says that we should
10351          * immediately execute it.
10352          *
10353          * What happens if this command would normally block for some other
10354          * reason?  e.g. a request sense with a head of queue tag
10355          * immediately after a write.  Normally that would block, but this
10356          * will result in its getting executed immediately...
10357          *
10358          * We currently return "pass" instead of "skip", so we'll end up
10359          * going through the rest of the queue to check for overlapped tags.
10360          *
10361          * XXX KDM check for other types of blockage first??
10362          */
10363         if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10364                 return (CTL_ACTION_PASS);
10365
10366         /*
10367          * Ordered tags have to block until all items ahead of them
10368          * have completed.  If we get called with an ordered tag, we always
10369          * block, if something else is ahead of us in the queue.
10370          */
10371         if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10372                 return (CTL_ACTION_BLOCK);
10373
10374         /*
10375          * Simple tags get blocked until all head of queue and ordered tags
10376          * ahead of them have completed.  I'm lumping untagged commands in
10377          * with simple tags here.  XXX KDM is that the right thing to do?
10378          */
10379         if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10380           || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10381          && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10382           || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10383                 return (CTL_ACTION_BLOCK);
10384
10385         pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
10386         ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
10387
10388         serialize_row = ctl_serialize_table[ooa_entry->seridx];
10389
10390         switch (serialize_row[pending_entry->seridx]) {
10391         case CTL_SER_BLOCK:
10392                 return (CTL_ACTION_BLOCK);
10393                 break; /* NOTREACHED */
10394         case CTL_SER_EXTENT:
10395                 return (ctl_extent_check(pending_io, ooa_io));
10396                 break; /* NOTREACHED */
10397         case CTL_SER_PASS:
10398                 return (CTL_ACTION_PASS);
10399                 break; /* NOTREACHED */
10400         case CTL_SER_SKIP:
10401                 return (CTL_ACTION_SKIP);
10402                 break;
10403         default:
10404                 panic("invalid serialization value %d",
10405                       serialize_row[pending_entry->seridx]);
10406                 break; /* NOTREACHED */
10407         }
10408
10409         return (CTL_ACTION_ERROR);
10410 }
10411
10412 /*
10413  * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10414  * Assumptions:
10415  * - pending_io is generally either incoming, or on the blocked queue
10416  * - starting I/O is the I/O we want to start the check with.
10417  */
10418 static ctl_action
10419 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10420               union ctl_io *starting_io)
10421 {
10422         union ctl_io *ooa_io;
10423         ctl_action action;
10424
10425         mtx_assert(&lun->lun_lock, MA_OWNED);
10426
10427         /*
10428          * Run back along the OOA queue, starting with the current
10429          * blocked I/O and going through every I/O before it on the
10430          * queue.  If starting_io is NULL, we'll just end up returning
10431          * CTL_ACTION_PASS.
10432          */
10433         for (ooa_io = starting_io; ooa_io != NULL;
10434              ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10435              ooa_links)){
10436
10437                 /*
10438                  * This routine just checks to see whether
10439                  * cur_blocked is blocked by ooa_io, which is ahead
10440                  * of it in the queue.  It doesn't queue/dequeue
10441                  * cur_blocked.
10442                  */
10443                 action = ctl_check_for_blockage(pending_io, ooa_io);
10444                 switch (action) {
10445                 case CTL_ACTION_BLOCK:
10446                 case CTL_ACTION_OVERLAP:
10447                 case CTL_ACTION_OVERLAP_TAG:
10448                 case CTL_ACTION_SKIP:
10449                 case CTL_ACTION_ERROR:
10450                         return (action);
10451                         break; /* NOTREACHED */
10452                 case CTL_ACTION_PASS:
10453                         break;
10454                 default:
10455                         panic("invalid action %d", action);
10456                         break;  /* NOTREACHED */
10457                 }
10458         }
10459
10460         return (CTL_ACTION_PASS);
10461 }
10462
10463 /*
10464  * Assumptions:
10465  * - An I/O has just completed, and has been removed from the per-LUN OOA
10466  *   queue, so some items on the blocked queue may now be unblocked.
10467  */
10468 static int
10469 ctl_check_blocked(struct ctl_lun *lun)
10470 {
10471         union ctl_io *cur_blocked, *next_blocked;
10472
10473         mtx_assert(&lun->lun_lock, MA_OWNED);
10474
10475         /*
10476          * Run forward from the head of the blocked queue, checking each
10477          * entry against the I/Os prior to it on the OOA queue to see if
10478          * there is still any blockage.
10479          *
10480          * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10481          * with our removing a variable on it while it is traversing the
10482          * list.
10483          */
10484         for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10485              cur_blocked != NULL; cur_blocked = next_blocked) {
10486                 union ctl_io *prev_ooa;
10487                 ctl_action action;
10488
10489                 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10490                                                           blocked_links);
10491
10492                 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10493                                                       ctl_ooaq, ooa_links);
10494
10495                 /*
10496                  * If cur_blocked happens to be the first item in the OOA
10497                  * queue now, prev_ooa will be NULL, and the action
10498                  * returned will just be CTL_ACTION_PASS.
10499                  */
10500                 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10501
10502                 switch (action) {
10503                 case CTL_ACTION_BLOCK:
10504                         /* Nothing to do here, still blocked */
10505                         break;
10506                 case CTL_ACTION_OVERLAP:
10507                 case CTL_ACTION_OVERLAP_TAG:
10508                         /*
10509                          * This shouldn't happen!  In theory we've already
10510                          * checked this command for overlap...
10511                          */
10512                         break;
10513                 case CTL_ACTION_PASS:
10514                 case CTL_ACTION_SKIP: {
10515                         struct ctl_softc *softc;
10516                         const struct ctl_cmd_entry *entry;
10517                         uint32_t initidx;
10518                         int isc_retval;
10519
10520                         /*
10521                          * The skip case shouldn't happen, this transaction
10522                          * should have never made it onto the blocked queue.
10523                          */
10524                         /*
10525                          * This I/O is no longer blocked, we can remove it
10526                          * from the blocked queue.  Since this is a TAILQ
10527                          * (doubly linked list), we can do O(1) removals
10528                          * from any place on the list.
10529                          */
10530                         TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10531                                      blocked_links);
10532                         cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10533
10534                         if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10535                                 /*
10536                                  * Need to send IO back to original side to
10537                                  * run
10538                                  */
10539                                 union ctl_ha_msg msg_info;
10540
10541                                 msg_info.hdr.original_sc =
10542                                         cur_blocked->io_hdr.original_sc;
10543                                 msg_info.hdr.serializing_sc = cur_blocked;
10544                                 msg_info.hdr.msg_type = CTL_MSG_R2R;
10545                                 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10546                                      &msg_info, sizeof(msg_info), 0)) >
10547                                      CTL_HA_STATUS_SUCCESS) {
10548                                         printf("CTL:Check Blocked error from "
10549                                                "ctl_ha_msg_send %d\n",
10550                                                isc_retval);
10551                                 }
10552                                 break;
10553                         }
10554                         entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
10555                         softc = control_softc;
10556
10557                         initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10558
10559                         /*
10560                          * Check this I/O for LUN state changes that may
10561                          * have happened while this command was blocked.
10562                          * The LUN state may have been changed by a command
10563                          * ahead of us in the queue, so we need to re-check
10564                          * for any states that can be caused by SCSI
10565                          * commands.
10566                          */
10567                         if (ctl_scsiio_lun_check(softc, lun, entry,
10568                                                  &cur_blocked->scsiio) == 0) {
10569                                 cur_blocked->io_hdr.flags |=
10570                                                       CTL_FLAG_IS_WAS_ON_RTR;
10571                                 ctl_enqueue_rtr(cur_blocked);
10572                         } else
10573                                 ctl_done(cur_blocked);
10574                         break;
10575                 }
10576                 default:
10577                         /*
10578                          * This probably shouldn't happen -- we shouldn't
10579                          * get CTL_ACTION_ERROR, or anything else.
10580                          */
10581                         break;
10582                 }
10583         }
10584
10585         return (CTL_RETVAL_COMPLETE);
10586 }
10587
10588 /*
10589  * This routine (with one exception) checks LUN flags that can be set by
10590  * commands ahead of us in the OOA queue.  These flags have to be checked
10591  * when a command initially comes in, and when we pull a command off the
10592  * blocked queue and are preparing to execute it.  The reason we have to
10593  * check these flags for commands on the blocked queue is that the LUN
10594  * state may have been changed by a command ahead of us while we're on the
10595  * blocked queue.
10596  *
10597  * Ordering is somewhat important with these checks, so please pay
10598  * careful attention to the placement of any new checks.
10599  */
10600 static int
10601 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10602     const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10603 {
10604         int retval;
10605
10606         retval = 0;
10607
10608         mtx_assert(&lun->lun_lock, MA_OWNED);
10609
10610         /*
10611          * If this shelf is a secondary shelf controller, we have to reject
10612          * any media access commands.
10613          */
10614 #if 0
10615         /* No longer needed for HA */
10616         if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10617          && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10618                 ctl_set_lun_standby(ctsio);
10619                 retval = 1;
10620                 goto bailout;
10621         }
10622 #endif
10623
10624         /*
10625          * Check for a reservation conflict.  If this command isn't allowed
10626          * even on reserved LUNs, and if this initiator isn't the one who
10627          * reserved us, reject the command with a reservation conflict.
10628          */
10629         if ((lun->flags & CTL_LUN_RESERVED)
10630          && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10631                 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10632                  || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10633                  || (ctsio->io_hdr.nexus.targ_target.id !=
10634                      lun->rsv_nexus.targ_target.id)) {
10635                         ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10636                         ctsio->io_hdr.status = CTL_SCSI_ERROR;
10637                         retval = 1;
10638                         goto bailout;
10639                 }
10640         }
10641
10642         if ( (lun->flags & CTL_LUN_PR_RESERVED)
10643          && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10644                 uint32_t residx;
10645
10646                 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10647                 /*
10648                  * if we aren't registered or it's a res holder type
10649                  * reservation and this isn't the res holder then set a
10650                  * conflict.
10651                  * NOTE: Commands which might be allowed on write exclusive
10652                  * type reservations are checked in the particular command
10653                  * for a conflict. Read and SSU are the only ones.
10654                  */
10655                 if (!lun->per_res[residx].registered
10656                  || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10657                         ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10658                         ctsio->io_hdr.status = CTL_SCSI_ERROR;
10659                         retval = 1;
10660                         goto bailout;
10661                 }
10662
10663         }
10664
10665         if ((lun->flags & CTL_LUN_OFFLINE)
10666          && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10667                 ctl_set_lun_not_ready(ctsio);
10668                 retval = 1;
10669                 goto bailout;
10670         }
10671
10672         /*
10673          * If the LUN is stopped, see if this particular command is allowed
10674          * for a stopped lun.  Otherwise, reject it with 0x04,0x02.
10675          */
10676         if ((lun->flags & CTL_LUN_STOPPED)
10677          && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10678                 /* "Logical unit not ready, initializing cmd. required" */
10679                 ctl_set_lun_stopped(ctsio);
10680                 retval = 1;
10681                 goto bailout;
10682         }
10683
10684         if ((lun->flags & CTL_LUN_INOPERABLE)
10685          && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10686                 /* "Medium format corrupted" */
10687                 ctl_set_medium_format_corrupted(ctsio);
10688                 retval = 1;
10689                 goto bailout;
10690         }
10691
10692 bailout:
10693         return (retval);
10694
10695 }
10696
10697 static void
10698 ctl_failover_io(union ctl_io *io, int have_lock)
10699 {
10700         ctl_set_busy(&io->scsiio);
10701         ctl_done(io);
10702 }
10703
10704 static void
10705 ctl_failover(void)
10706 {
10707         struct ctl_lun *lun;
10708         struct ctl_softc *ctl_softc;
10709         union ctl_io *next_io, *pending_io;
10710         union ctl_io *io;
10711         int lun_idx;
10712         int i;
10713
10714         ctl_softc = control_softc;
10715
10716         mtx_lock(&ctl_softc->ctl_lock);
10717         /*
10718          * Remove any cmds from the other SC from the rtr queue.  These
10719          * will obviously only be for LUNs for which we're the primary.
10720          * We can't send status or get/send data for these commands.
10721          * Since they haven't been executed yet, we can just remove them.
10722          * We'll either abort them or delete them below, depending on
10723          * which HA mode we're in.
10724          */
10725 #ifdef notyet
10726         mtx_lock(&ctl_softc->queue_lock);
10727         for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10728              io != NULL; io = next_io) {
10729                 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10730                 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10731                         STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10732                                       ctl_io_hdr, links);
10733         }
10734         mtx_unlock(&ctl_softc->queue_lock);
10735 #endif
10736
10737         for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10738                 lun = ctl_softc->ctl_luns[lun_idx];
10739                 if (lun==NULL)
10740                         continue;
10741
10742                 /*
10743                  * Processor LUNs are primary on both sides.
10744                  * XXX will this always be true?
10745                  */
10746                 if (lun->be_lun->lun_type == T_PROCESSOR)
10747                         continue;
10748
10749                 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10750                  && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10751                         printf("FAILOVER: primary lun %d\n", lun_idx);
10752                         /*
10753                          * Remove all commands from the other SC. First from the
10754                          * blocked queue then from the ooa queue. Once we have
10755                          * removed them. Call ctl_check_blocked to see if there
10756                          * is anything that can run.
10757                          */
10758                         for (io = (union ctl_io *)TAILQ_FIRST(
10759                              &lun->blocked_queue); io != NULL; io = next_io) {
10760
10761                                 next_io = (union ctl_io *)TAILQ_NEXT(
10762                                     &io->io_hdr, blocked_links);
10763
10764                                 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10765                                         TAILQ_REMOVE(&lun->blocked_queue,
10766                                                      &io->io_hdr,blocked_links);
10767                                         io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10768                                         TAILQ_REMOVE(&lun->ooa_queue,
10769                                                      &io->io_hdr, ooa_links);
10770
10771                                         ctl_free_io(io);
10772                                 }
10773                         }
10774
10775                         for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10776                              io != NULL; io = next_io) {
10777
10778                                 next_io = (union ctl_io *)TAILQ_NEXT(
10779                                     &io->io_hdr, ooa_links);
10780
10781                                 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10782
10783                                         TAILQ_REMOVE(&lun->ooa_queue,
10784                                                 &io->io_hdr,
10785                                                 ooa_links);
10786
10787                                         ctl_free_io(io);
10788                                 }
10789                         }
10790                         ctl_check_blocked(lun);
10791                 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10792                         && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10793
10794                         printf("FAILOVER: primary lun %d\n", lun_idx);
10795                         /*
10796                          * Abort all commands from the other SC.  We can't
10797                          * send status back for them now.  These should get
10798                          * cleaned up when they are completed or come out
10799                          * for a datamove operation.
10800                          */
10801                         for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10802                              io != NULL; io = next_io) {
10803                                 next_io = (union ctl_io *)TAILQ_NEXT(
10804                                         &io->io_hdr, ooa_links);
10805
10806                                 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10807                                         io->io_hdr.flags |= CTL_FLAG_ABORT;
10808                         }
10809                 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10810                         && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10811
10812                         printf("FAILOVER: secondary lun %d\n", lun_idx);
10813
10814                         lun->flags |= CTL_LUN_PRIMARY_SC;
10815
10816                         /*
10817                          * We send all I/O that was sent to this controller
10818                          * and redirected to the other side back with
10819                          * busy status, and have the initiator retry it.
10820                          * Figuring out how much data has been transferred,
10821                          * etc. and picking up where we left off would be 
10822                          * very tricky.
10823                          *
10824                          * XXX KDM need to remove I/O from the blocked
10825                          * queue as well!
10826                          */
10827                         for (pending_io = (union ctl_io *)TAILQ_FIRST(
10828                              &lun->ooa_queue); pending_io != NULL;
10829                              pending_io = next_io) {
10830
10831                                 next_io =  (union ctl_io *)TAILQ_NEXT(
10832                                         &pending_io->io_hdr, ooa_links);
10833
10834                                 pending_io->io_hdr.flags &=
10835                                         ~CTL_FLAG_SENT_2OTHER_SC;
10836
10837                                 if (pending_io->io_hdr.flags &
10838                                     CTL_FLAG_IO_ACTIVE) {
10839                                         pending_io->io_hdr.flags |=
10840                                                 CTL_FLAG_FAILOVER;
10841                                 } else {
10842                                         ctl_set_busy(&pending_io->scsiio);
10843                                         ctl_done(pending_io);
10844                                 }
10845                         }
10846
10847                         /*
10848                          * Build Unit Attention
10849                          */
10850                         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10851                                 lun->pending_sense[i].ua_pending |=
10852                                                      CTL_UA_ASYM_ACC_CHANGE;
10853                         }
10854                 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10855                         && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10856                         printf("FAILOVER: secondary lun %d\n", lun_idx);
10857                         /*
10858                          * if the first io on the OOA is not on the RtR queue
10859                          * add it.
10860                          */
10861                         lun->flags |= CTL_LUN_PRIMARY_SC;
10862
10863                         pending_io = (union ctl_io *)TAILQ_FIRST(
10864                             &lun->ooa_queue);
10865                         if (pending_io==NULL) {
10866                                 printf("Nothing on OOA queue\n");
10867                                 continue;
10868                         }
10869
10870                         pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10871                         if ((pending_io->io_hdr.flags &
10872                              CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10873                                 pending_io->io_hdr.flags |=
10874                                     CTL_FLAG_IS_WAS_ON_RTR;
10875                                 ctl_enqueue_rtr(pending_io);
10876                         }
10877 #if 0
10878                         else
10879                         {
10880                                 printf("Tag 0x%04x is running\n",
10881                                       pending_io->scsiio.tag_num);
10882                         }
10883 #endif
10884
10885                         next_io = (union ctl_io *)TAILQ_NEXT(
10886                             &pending_io->io_hdr, ooa_links);
10887                         for (pending_io=next_io; pending_io != NULL;
10888                              pending_io = next_io) {
10889                                 pending_io->io_hdr.flags &=
10890                                     ~CTL_FLAG_SENT_2OTHER_SC;
10891                                 next_io = (union ctl_io *)TAILQ_NEXT(
10892                                         &pending_io->io_hdr, ooa_links);
10893                                 if (pending_io->io_hdr.flags &
10894                                     CTL_FLAG_IS_WAS_ON_RTR) {
10895 #if 0
10896                                         printf("Tag 0x%04x is running\n",
10897                                                 pending_io->scsiio.tag_num);
10898 #endif
10899                                         continue;
10900                                 }
10901
10902                                 switch (ctl_check_ooa(lun, pending_io,
10903                                     (union ctl_io *)TAILQ_PREV(
10904                                     &pending_io->io_hdr, ctl_ooaq,
10905                                     ooa_links))) {
10906
10907                                 case CTL_ACTION_BLOCK:
10908                                         TAILQ_INSERT_TAIL(&lun->blocked_queue,
10909                                                           &pending_io->io_hdr,
10910                                                           blocked_links);
10911                                         pending_io->io_hdr.flags |=
10912                                             CTL_FLAG_BLOCKED;
10913                                         break;
10914                                 case CTL_ACTION_PASS:
10915                                 case CTL_ACTION_SKIP:
10916                                         pending_io->io_hdr.flags |=
10917                                             CTL_FLAG_IS_WAS_ON_RTR;
10918                                         ctl_enqueue_rtr(pending_io);
10919                                         break;
10920                                 case CTL_ACTION_OVERLAP:
10921                                         ctl_set_overlapped_cmd(
10922                                             (struct ctl_scsiio *)pending_io);
10923                                         ctl_done(pending_io);
10924                                         break;
10925                                 case CTL_ACTION_OVERLAP_TAG:
10926                                         ctl_set_overlapped_tag(
10927                                             (struct ctl_scsiio *)pending_io,
10928                                             pending_io->scsiio.tag_num & 0xff);
10929                                         ctl_done(pending_io);
10930                                         break;
10931                                 case CTL_ACTION_ERROR:
10932                                 default:
10933                                         ctl_set_internal_failure(
10934                                                 (struct ctl_scsiio *)pending_io,
10935                                                 0,  // sks_valid
10936                                                 0); //retry count
10937                                         ctl_done(pending_io);
10938                                         break;
10939                                 }
10940                         }
10941
10942                         /*
10943                          * Build Unit Attention
10944                          */
10945                         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10946                                 lun->pending_sense[i].ua_pending |=
10947                                                      CTL_UA_ASYM_ACC_CHANGE;
10948                         }
10949                 } else {
10950                         panic("Unhandled HA mode failover, LUN flags = %#x, "
10951                               "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10952                 }
10953         }
10954         ctl_pause_rtr = 0;
10955         mtx_unlock(&ctl_softc->ctl_lock);
10956 }
10957
10958 static int
10959 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10960 {
10961         struct ctl_lun *lun;
10962         const struct ctl_cmd_entry *entry;
10963         uint32_t initidx, targ_lun;
10964         int retval;
10965
10966         retval = 0;
10967
10968         lun = NULL;
10969
10970         targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
10971         if ((targ_lun < CTL_MAX_LUNS)
10972          && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
10973                 lun = ctl_softc->ctl_luns[targ_lun];
10974                 /*
10975                  * If the LUN is invalid, pretend that it doesn't exist.
10976                  * It will go away as soon as all pending I/O has been
10977                  * completed.
10978                  */
10979                 if (lun->flags & CTL_LUN_DISABLED) {
10980                         lun = NULL;
10981                 } else {
10982                         ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10983                         ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10984                                 lun->be_lun;
10985                         if (lun->be_lun->lun_type == T_PROCESSOR) {
10986                                 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10987                         }
10988
10989                         /*
10990                          * Every I/O goes into the OOA queue for a
10991                          * particular LUN, and stays there until completion.
10992                          */
10993                         mtx_lock(&lun->lun_lock);
10994                         TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
10995                             ooa_links);
10996                 }
10997         } else {
10998                 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10999                 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11000         }
11001
11002         /* Get command entry and return error if it is unsuppotyed. */
11003         entry = ctl_validate_command(ctsio);
11004         if (entry == NULL) {
11005                 if (lun)
11006                         mtx_unlock(&lun->lun_lock);
11007                 return (retval);
11008         }
11009
11010         ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11011         ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11012
11013         /*
11014          * Check to see whether we can send this command to LUNs that don't
11015          * exist.  This should pretty much only be the case for inquiry
11016          * and request sense.  Further checks, below, really require having
11017          * a LUN, so we can't really check the command anymore.  Just put
11018          * it on the rtr queue.
11019          */
11020         if (lun == NULL) {
11021                 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11022                         ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11023                         ctl_enqueue_rtr((union ctl_io *)ctsio);
11024                         return (retval);
11025                 }
11026
11027                 ctl_set_unsupported_lun(ctsio);
11028                 ctl_done((union ctl_io *)ctsio);
11029                 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11030                 return (retval);
11031         } else {
11032                 /*
11033                  * Make sure we support this particular command on this LUN.
11034                  * e.g., we don't support writes to the control LUN.
11035                  */
11036                 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11037                         mtx_unlock(&lun->lun_lock);
11038                         ctl_set_invalid_opcode(ctsio);
11039                         ctl_done((union ctl_io *)ctsio);
11040                         return (retval);
11041                 }
11042         }
11043
11044         initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11045
11046         /*
11047          * If we've got a request sense, it'll clear the contingent
11048          * allegiance condition.  Otherwise, if we have a CA condition for
11049          * this initiator, clear it, because it sent down a command other
11050          * than request sense.
11051          */
11052         if ((ctsio->cdb[0] != REQUEST_SENSE)
11053          && (ctl_is_set(lun->have_ca, initidx)))
11054                 ctl_clear_mask(lun->have_ca, initidx);
11055
11056         /*
11057          * If the command has this flag set, it handles its own unit
11058          * attention reporting, we shouldn't do anything.  Otherwise we
11059          * check for any pending unit attentions, and send them back to the
11060          * initiator.  We only do this when a command initially comes in,
11061          * not when we pull it off the blocked queue.
11062          *
11063          * According to SAM-3, section 5.3.2, the order that things get
11064          * presented back to the host is basically unit attentions caused
11065          * by some sort of reset event, busy status, reservation conflicts
11066          * or task set full, and finally any other status.
11067          *
11068          * One issue here is that some of the unit attentions we report
11069          * don't fall into the "reset" category (e.g. "reported luns data
11070          * has changed").  So reporting it here, before the reservation
11071          * check, may be technically wrong.  I guess the only thing to do
11072          * would be to check for and report the reset events here, and then
11073          * check for the other unit attention types after we check for a
11074          * reservation conflict.
11075          *
11076          * XXX KDM need to fix this
11077          */
11078         if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11079                 ctl_ua_type ua_type;
11080
11081                 ua_type = lun->pending_sense[initidx].ua_pending;
11082                 if (ua_type != CTL_UA_NONE) {
11083                         scsi_sense_data_type sense_format;
11084
11085                         if (lun != NULL)
11086                                 sense_format = (lun->flags &
11087                                     CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11088                                     SSD_TYPE_FIXED;
11089                         else
11090                                 sense_format = SSD_TYPE_FIXED;
11091
11092                         ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11093                                                sense_format);
11094                         if (ua_type != CTL_UA_NONE) {
11095                                 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11096                                 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11097                                                        CTL_AUTOSENSE;
11098                                 ctsio->sense_len = SSD_FULL_SIZE;
11099                                 lun->pending_sense[initidx].ua_pending &=
11100                                         ~ua_type;
11101                                 mtx_unlock(&lun->lun_lock);
11102                                 ctl_done((union ctl_io *)ctsio);
11103                                 return (retval);
11104                         }
11105                 }
11106         }
11107
11108
11109         if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11110                 mtx_unlock(&lun->lun_lock);
11111                 ctl_done((union ctl_io *)ctsio);
11112                 return (retval);
11113         }
11114
11115         /*
11116          * XXX CHD this is where we want to send IO to other side if
11117          * this LUN is secondary on this SC. We will need to make a copy
11118          * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11119          * the copy we send as FROM_OTHER.
11120          * We also need to stuff the address of the original IO so we can
11121          * find it easily. Something similar will need be done on the other
11122          * side so when we are done we can find the copy.
11123          */
11124         if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11125                 union ctl_ha_msg msg_info;
11126                 int isc_retval;
11127
11128                 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11129
11130                 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11131                 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11132 #if 0
11133                 printf("1. ctsio %p\n", ctsio);
11134 #endif
11135                 msg_info.hdr.serializing_sc = NULL;
11136                 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11137                 msg_info.scsi.tag_num = ctsio->tag_num;
11138                 msg_info.scsi.tag_type = ctsio->tag_type;
11139                 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11140
11141                 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11142
11143                 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11144                     (void *)&msg_info, sizeof(msg_info), 0)) >
11145                     CTL_HA_STATUS_SUCCESS) {
11146                         printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11147                                isc_retval);
11148                         printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11149                 } else {
11150 #if 0
11151                         printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11152 #endif
11153                 }
11154
11155                 /*
11156                  * XXX KDM this I/O is off the incoming queue, but hasn't
11157                  * been inserted on any other queue.  We may need to come
11158                  * up with a holding queue while we wait for serialization
11159                  * so that we have an idea of what we're waiting for from
11160                  * the other side.
11161                  */
11162                 mtx_unlock(&lun->lun_lock);
11163                 return (retval);
11164         }
11165
11166         switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11167                               (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11168                               ctl_ooaq, ooa_links))) {
11169         case CTL_ACTION_BLOCK:
11170                 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11171                 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11172                                   blocked_links);
11173                 mtx_unlock(&lun->lun_lock);
11174                 return (retval);
11175         case CTL_ACTION_PASS:
11176         case CTL_ACTION_SKIP:
11177                 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11178                 mtx_unlock(&lun->lun_lock);
11179                 ctl_enqueue_rtr((union ctl_io *)ctsio);
11180                 break;
11181         case CTL_ACTION_OVERLAP:
11182                 mtx_unlock(&lun->lun_lock);
11183                 ctl_set_overlapped_cmd(ctsio);
11184                 ctl_done((union ctl_io *)ctsio);
11185                 break;
11186         case CTL_ACTION_OVERLAP_TAG:
11187                 mtx_unlock(&lun->lun_lock);
11188                 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11189                 ctl_done((union ctl_io *)ctsio);
11190                 break;
11191         case CTL_ACTION_ERROR:
11192         default:
11193                 mtx_unlock(&lun->lun_lock);
11194                 ctl_set_internal_failure(ctsio,
11195                                          /*sks_valid*/ 0,
11196                                          /*retry_count*/ 0);
11197                 ctl_done((union ctl_io *)ctsio);
11198                 break;
11199         }
11200         return (retval);
11201 }
11202
11203 const struct ctl_cmd_entry *
11204 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11205 {
11206         const struct ctl_cmd_entry *entry;
11207         int service_action;
11208
11209         entry = &ctl_cmd_table[ctsio->cdb[0]];
11210         if (entry->flags & CTL_CMD_FLAG_SA5) {
11211                 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11212                 entry = &((const struct ctl_cmd_entry *)
11213                     entry->execute)[service_action];
11214         }
11215         return (entry);
11216 }
11217
11218 const struct ctl_cmd_entry *
11219 ctl_validate_command(struct ctl_scsiio *ctsio)
11220 {
11221         const struct ctl_cmd_entry *entry;
11222         int i;
11223         uint8_t diff;
11224
11225         entry = ctl_get_cmd_entry(ctsio);
11226         if (entry->execute == NULL) {
11227                 ctl_set_invalid_opcode(ctsio);
11228                 ctl_done((union ctl_io *)ctsio);
11229                 return (NULL);
11230         }
11231         KASSERT(entry->length > 0,
11232             ("Not defined length for command 0x%02x/0x%02x",
11233              ctsio->cdb[0], ctsio->cdb[1]));
11234         for (i = 1; i < entry->length; i++) {
11235                 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11236                 if (diff == 0)
11237                         continue;
11238                 ctl_set_invalid_field(ctsio,
11239                                       /*sks_valid*/ 1,
11240                                       /*command*/ 1,
11241                                       /*field*/ i,
11242                                       /*bit_valid*/ 1,
11243                                       /*bit*/ fls(diff) - 1);
11244                 ctl_done((union ctl_io *)ctsio);
11245                 return (NULL);
11246         }
11247         return (entry);
11248 }
11249
11250 static int
11251 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11252 {
11253
11254         switch (lun_type) {
11255         case T_PROCESSOR:
11256                 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11257                     ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11258                         return (0);
11259                 break;
11260         case T_DIRECT:
11261                 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11262                     ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11263                         return (0);
11264                 break;
11265         default:
11266                 return (0);
11267         }
11268         return (1);
11269 }
11270
11271 static int
11272 ctl_scsiio(struct ctl_scsiio *ctsio)
11273 {
11274         int retval;
11275         const struct ctl_cmd_entry *entry;
11276
11277         retval = CTL_RETVAL_COMPLETE;
11278
11279         CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11280
11281         entry = ctl_get_cmd_entry(ctsio);
11282
11283         /*
11284          * If this I/O has been aborted, just send it straight to
11285          * ctl_done() without executing it.
11286          */
11287         if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11288                 ctl_done((union ctl_io *)ctsio);
11289                 goto bailout;
11290         }
11291
11292         /*
11293          * All the checks should have been handled by ctl_scsiio_precheck().
11294          * We should be clear now to just execute the I/O.
11295          */
11296         retval = entry->execute(ctsio);
11297
11298 bailout:
11299         return (retval);
11300 }
11301
11302 /*
11303  * Since we only implement one target right now, a bus reset simply resets
11304  * our single target.
11305  */
11306 static int
11307 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11308 {
11309         return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11310 }
11311
11312 static int
11313 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11314                  ctl_ua_type ua_type)
11315 {
11316         struct ctl_lun *lun;
11317         int retval;
11318
11319         if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11320                 union ctl_ha_msg msg_info;
11321
11322                 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11323                 msg_info.hdr.nexus = io->io_hdr.nexus;
11324                 if (ua_type==CTL_UA_TARG_RESET)
11325                         msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11326                 else
11327                         msg_info.task.task_action = CTL_TASK_BUS_RESET;
11328                 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11329                 msg_info.hdr.original_sc = NULL;
11330                 msg_info.hdr.serializing_sc = NULL;
11331                 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11332                     (void *)&msg_info, sizeof(msg_info), 0)) {
11333                 }
11334         }
11335         retval = 0;
11336
11337         mtx_lock(&ctl_softc->ctl_lock);
11338         STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11339                 retval += ctl_lun_reset(lun, io, ua_type);
11340         mtx_unlock(&ctl_softc->ctl_lock);
11341
11342         return (retval);
11343 }
11344
11345 /*
11346  * The LUN should always be set.  The I/O is optional, and is used to
11347  * distinguish between I/Os sent by this initiator, and by other
11348  * initiators.  We set unit attention for initiators other than this one.
11349  * SAM-3 is vague on this point.  It does say that a unit attention should
11350  * be established for other initiators when a LUN is reset (see section
11351  * 5.7.3), but it doesn't specifically say that the unit attention should
11352  * be established for this particular initiator when a LUN is reset.  Here
11353  * is the relevant text, from SAM-3 rev 8:
11354  *
11355  * 5.7.2 When a SCSI initiator port aborts its own tasks
11356  *
11357  * When a SCSI initiator port causes its own task(s) to be aborted, no
11358  * notification that the task(s) have been aborted shall be returned to
11359  * the SCSI initiator port other than the completion response for the
11360  * command or task management function action that caused the task(s) to
11361  * be aborted and notification(s) associated with related effects of the
11362  * action (e.g., a reset unit attention condition).
11363  *
11364  * XXX KDM for now, we're setting unit attention for all initiators.
11365  */
11366 static int
11367 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11368 {
11369         union ctl_io *xio;
11370 #if 0
11371         uint32_t initindex;
11372 #endif
11373         int i;
11374
11375         mtx_lock(&lun->lun_lock);
11376         /*
11377          * Run through the OOA queue and abort each I/O.
11378          */
11379 #if 0
11380         TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11381 #endif
11382         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11383              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11384                 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11385         }
11386
11387         /*
11388          * This version sets unit attention for every
11389          */
11390 #if 0
11391         initindex = ctl_get_initindex(&io->io_hdr.nexus);
11392         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11393                 if (initindex == i)
11394                         continue;
11395                 lun->pending_sense[i].ua_pending |= ua_type;
11396         }
11397 #endif
11398
11399         /*
11400          * A reset (any kind, really) clears reservations established with
11401          * RESERVE/RELEASE.  It does not clear reservations established
11402          * with PERSISTENT RESERVE OUT, but we don't support that at the
11403          * moment anyway.  See SPC-2, section 5.6.  SPC-3 doesn't address
11404          * reservations made with the RESERVE/RELEASE commands, because
11405          * those commands are obsolete in SPC-3.
11406          */
11407         lun->flags &= ~CTL_LUN_RESERVED;
11408
11409         for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11410                 ctl_clear_mask(lun->have_ca, i);
11411                 lun->pending_sense[i].ua_pending |= ua_type;
11412         }
11413         mtx_lock(&lun->lun_lock);
11414
11415         return (0);
11416 }
11417
11418 static int
11419 ctl_abort_task(union ctl_io *io)
11420 {
11421         union ctl_io *xio;
11422         struct ctl_lun *lun;
11423         struct ctl_softc *ctl_softc;
11424 #if 0
11425         struct sbuf sb;
11426         char printbuf[128];
11427 #endif
11428         int found;
11429         uint32_t targ_lun;
11430
11431         ctl_softc = control_softc;
11432         found = 0;
11433
11434         /*
11435          * Look up the LUN.
11436          */
11437         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11438         mtx_lock(&ctl_softc->ctl_lock);
11439         if ((targ_lun < CTL_MAX_LUNS)
11440          && (ctl_softc->ctl_luns[targ_lun] != NULL))
11441                 lun = ctl_softc->ctl_luns[targ_lun];
11442         else {
11443                 mtx_unlock(&ctl_softc->ctl_lock);
11444                 goto bailout;
11445         }
11446
11447 #if 0
11448         printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11449                lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11450 #endif
11451
11452         mtx_lock(&lun->lun_lock);
11453         mtx_unlock(&ctl_softc->ctl_lock);
11454         /*
11455          * Run through the OOA queue and attempt to find the given I/O.
11456          * The target port, initiator ID, tag type and tag number have to
11457          * match the values that we got from the initiator.  If we have an
11458          * untagged command to abort, simply abort the first untagged command
11459          * we come to.  We only allow one untagged command at a time of course.
11460          */
11461 #if 0
11462         TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11463 #endif
11464         for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11465              xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11466 #if 0
11467                 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11468
11469                 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11470                             lun->lun, xio->scsiio.tag_num,
11471                             xio->scsiio.tag_type,
11472                             (xio->io_hdr.blocked_links.tqe_prev
11473                             == NULL) ? "" : " BLOCKED",
11474                             (xio->io_hdr.flags &
11475                             CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11476                             (xio->io_hdr.flags &
11477                             CTL_FLAG_ABORT) ? " ABORT" : "",
11478                             (xio->io_hdr.flags &
11479                             CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11480                 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11481                 sbuf_finish(&sb);
11482                 printf("%s\n", sbuf_data(&sb));
11483 #endif
11484
11485                 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11486                  && (xio->io_hdr.nexus.initid.id ==
11487                      io->io_hdr.nexus.initid.id)) {
11488                         /*
11489                          * If the abort says that the task is untagged, the
11490                          * task in the queue must be untagged.  Otherwise,
11491                          * we just check to see whether the tag numbers
11492                          * match.  This is because the QLogic firmware
11493                          * doesn't pass back the tag type in an abort
11494                          * request.
11495                          */
11496 #if 0
11497                         if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11498                           && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11499                          || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11500 #endif
11501                         /*
11502                          * XXX KDM we've got problems with FC, because it
11503                          * doesn't send down a tag type with aborts.  So we
11504                          * can only really go by the tag number...
11505                          * This may cause problems with parallel SCSI.
11506                          * Need to figure that out!!
11507                          */
11508                         if (xio->scsiio.tag_num == io->taskio.tag_num) {
11509                                 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11510                                 found = 1;
11511                                 if ((io->io_hdr.flags &
11512                                      CTL_FLAG_FROM_OTHER_SC) == 0 &&
11513                                     !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11514                                         union ctl_ha_msg msg_info;
11515
11516                                         io->io_hdr.flags |=
11517                                                         CTL_FLAG_SENT_2OTHER_SC;
11518                                         msg_info.hdr.nexus = io->io_hdr.nexus;
11519                                         msg_info.task.task_action =
11520                                                 CTL_TASK_ABORT_TASK;
11521                                         msg_info.task.tag_num =
11522                                                 io->taskio.tag_num;
11523                                         msg_info.task.tag_type =
11524                                                 io->taskio.tag_type;
11525                                         msg_info.hdr.msg_type =
11526                                                 CTL_MSG_MANAGE_TASKS;
11527                                         msg_info.hdr.original_sc = NULL;
11528                                         msg_info.hdr.serializing_sc = NULL;
11529 #if 0
11530                                         printf("Sent Abort to other side\n");
11531 #endif
11532                                         if (CTL_HA_STATUS_SUCCESS !=
11533                                                 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11534                                                 (void *)&msg_info,
11535                                                 sizeof(msg_info), 0)) {
11536                                         }
11537                                 }
11538 #if 0
11539                                 printf("ctl_abort_task: found I/O to abort\n");
11540 #endif
11541                                 break;
11542                         }
11543                 }
11544         }
11545         mtx_unlock(&lun->lun_lock);
11546
11547 bailout:
11548
11549         if (found == 0) {
11550                 /*
11551                  * This isn't really an error.  It's entirely possible for
11552                  * the abort and command completion to cross on the wire.
11553                  * This is more of an informative/diagnostic error.
11554                  */
11555 #if 0
11556                 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11557                        "%d:%d:%d:%d tag %d type %d\n",
11558                        io->io_hdr.nexus.initid.id,
11559                        io->io_hdr.nexus.targ_port,
11560                        io->io_hdr.nexus.targ_target.id,
11561                        io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11562                        io->taskio.tag_type);
11563 #endif
11564                 return (1);
11565         } else
11566                 return (0);
11567 }
11568
11569 /*
11570  * This routine cannot block!  It must be callable from an interrupt
11571  * handler as well as from the work thread.
11572  */
11573 static void
11574 ctl_run_task(union ctl_io *io)
11575 {
11576         struct ctl_softc *ctl_softc;
11577         int retval;
11578         const char *task_desc;
11579
11580         CTL_DEBUG_PRINT(("ctl_run_task\n"));
11581
11582         ctl_softc = control_softc;
11583         retval = 0;
11584
11585         KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11586             ("ctl_run_task: Unextected io_type %d\n",
11587              io->io_hdr.io_type));
11588
11589         task_desc = ctl_scsi_task_string(&io->taskio);
11590         if (task_desc != NULL) {
11591 #ifdef NEEDTOPORT
11592                 csevent_log(CSC_CTL | CSC_SHELF_SW |
11593                             CTL_TASK_REPORT,
11594                             csevent_LogType_Trace,
11595                             csevent_Severity_Information,
11596                             csevent_AlertLevel_Green,
11597                             csevent_FRU_Firmware,
11598                             csevent_FRU_Unknown,
11599                             "CTL: received task: %s",task_desc);
11600 #endif
11601         } else {
11602 #ifdef NEEDTOPORT
11603                 csevent_log(CSC_CTL | CSC_SHELF_SW |
11604                             CTL_TASK_REPORT,
11605                             csevent_LogType_Trace,
11606                             csevent_Severity_Information,
11607                             csevent_AlertLevel_Green,
11608                             csevent_FRU_Firmware,
11609                             csevent_FRU_Unknown,
11610                             "CTL: received unknown task "
11611                             "type: %d (%#x)",
11612                             io->taskio.task_action,
11613                             io->taskio.task_action);
11614 #endif
11615         }
11616         switch (io->taskio.task_action) {
11617         case CTL_TASK_ABORT_TASK:
11618                 retval = ctl_abort_task(io);
11619                 break;
11620         case CTL_TASK_ABORT_TASK_SET:
11621                 break;
11622         case CTL_TASK_CLEAR_ACA:
11623                 break;
11624         case CTL_TASK_CLEAR_TASK_SET:
11625                 break;
11626         case CTL_TASK_LUN_RESET: {
11627                 struct ctl_lun *lun;
11628                 uint32_t targ_lun;
11629                 int retval;
11630
11631                 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11632                 mtx_lock(&ctl_softc->ctl_lock);
11633                 if ((targ_lun < CTL_MAX_LUNS)
11634                  && (ctl_softc->ctl_luns[targ_lun] != NULL))
11635                         lun = ctl_softc->ctl_luns[targ_lun];
11636                 else {
11637                         mtx_unlock(&ctl_softc->ctl_lock);
11638                         retval = 1;
11639                         break;
11640                 }
11641
11642                 if (!(io->io_hdr.flags &
11643                     CTL_FLAG_FROM_OTHER_SC)) {
11644                         union ctl_ha_msg msg_info;
11645
11646                         io->io_hdr.flags |=
11647                                 CTL_FLAG_SENT_2OTHER_SC;
11648                         msg_info.hdr.msg_type =
11649                                 CTL_MSG_MANAGE_TASKS;
11650                         msg_info.hdr.nexus = io->io_hdr.nexus;
11651                         msg_info.task.task_action =
11652                                 CTL_TASK_LUN_RESET;
11653                         msg_info.hdr.original_sc = NULL;
11654                         msg_info.hdr.serializing_sc = NULL;
11655                         if (CTL_HA_STATUS_SUCCESS !=
11656                             ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11657                             (void *)&msg_info,
11658                             sizeof(msg_info), 0)) {
11659                         }
11660                 }
11661
11662                 retval = ctl_lun_reset(lun, io,
11663                                        CTL_UA_LUN_RESET);
11664                 mtx_unlock(&ctl_softc->ctl_lock);
11665                 break;
11666         }
11667         case CTL_TASK_TARGET_RESET:
11668                 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
11669                 break;
11670         case CTL_TASK_BUS_RESET:
11671                 retval = ctl_bus_reset(ctl_softc, io);
11672                 break;
11673         case CTL_TASK_PORT_LOGIN:
11674                 break;
11675         case CTL_TASK_PORT_LOGOUT:
11676                 break;
11677         default:
11678                 printf("ctl_run_task: got unknown task management event %d\n",
11679                        io->taskio.task_action);
11680                 break;
11681         }
11682         if (retval == 0)
11683                 io->io_hdr.status = CTL_SUCCESS;
11684         else
11685                 io->io_hdr.status = CTL_ERROR;
11686
11687         /*
11688          * This will queue this I/O to the done queue, but the
11689          * work thread won't be able to process it until we
11690          * return and the lock is released.
11691          */
11692         ctl_done(io);
11693 }
11694
11695 /*
11696  * For HA operation.  Handle commands that come in from the other
11697  * controller.
11698  */
11699 static void
11700 ctl_handle_isc(union ctl_io *io)
11701 {
11702         int free_io;
11703         struct ctl_lun *lun;
11704         struct ctl_softc *ctl_softc;
11705         uint32_t targ_lun;
11706
11707         ctl_softc = control_softc;
11708
11709         targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11710         lun = ctl_softc->ctl_luns[targ_lun];
11711
11712         switch (io->io_hdr.msg_type) {
11713         case CTL_MSG_SERIALIZE:
11714                 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11715                 break;
11716         case CTL_MSG_R2R: {
11717                 const struct ctl_cmd_entry *entry;
11718
11719                 /*
11720                  * This is only used in SER_ONLY mode.
11721                  */
11722                 free_io = 0;
11723                 entry = ctl_get_cmd_entry(&io->scsiio);
11724                 mtx_lock(&lun->lun_lock);
11725                 if (ctl_scsiio_lun_check(ctl_softc, lun,
11726                     entry, (struct ctl_scsiio *)io) != 0) {
11727                         mtx_unlock(&lun->lun_lock);
11728                         ctl_done(io);
11729                         break;
11730                 }
11731                 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11732                 mtx_unlock(&lun->lun_lock);
11733                 ctl_enqueue_rtr(io);
11734                 break;
11735         }
11736         case CTL_MSG_FINISH_IO:
11737                 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11738                         free_io = 0;
11739                         ctl_done(io);
11740                 } else {
11741                         free_io = 1;
11742                         mtx_lock(&lun->lun_lock);
11743                         TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11744                                      ooa_links);
11745                         ctl_check_blocked(lun);
11746                         mtx_unlock(&lun->lun_lock);
11747                 }
11748                 break;
11749         case CTL_MSG_PERS_ACTION:
11750                 ctl_hndl_per_res_out_on_other_sc(
11751                         (union ctl_ha_msg *)&io->presio.pr_msg);
11752                 free_io = 1;
11753                 break;
11754         case CTL_MSG_BAD_JUJU:
11755                 free_io = 0;
11756                 ctl_done(io);
11757                 break;
11758         case CTL_MSG_DATAMOVE:
11759                 /* Only used in XFER mode */
11760                 free_io = 0;
11761                 ctl_datamove_remote(io);
11762                 break;
11763         case CTL_MSG_DATAMOVE_DONE:
11764                 /* Only used in XFER mode */
11765                 free_io = 0;
11766                 io->scsiio.be_move_done(io);
11767                 break;
11768         default:
11769                 free_io = 1;
11770                 printf("%s: Invalid message type %d\n",
11771                        __func__, io->io_hdr.msg_type);
11772                 break;
11773         }
11774         if (free_io)
11775                 ctl_free_io(io);
11776
11777 }
11778
11779
11780 /*
11781  * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11782  * there is no match.
11783  */
11784 static ctl_lun_error_pattern
11785 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11786 {
11787         const struct ctl_cmd_entry *entry;
11788         ctl_lun_error_pattern filtered_pattern, pattern;
11789
11790         pattern = desc->error_pattern;
11791
11792         /*
11793          * XXX KDM we need more data passed into this function to match a
11794          * custom pattern, and we actually need to implement custom pattern
11795          * matching.
11796          */
11797         if (pattern & CTL_LUN_PAT_CMD)
11798                 return (CTL_LUN_PAT_CMD);
11799
11800         if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11801                 return (CTL_LUN_PAT_ANY);
11802
11803         entry = ctl_get_cmd_entry(ctsio);
11804
11805         filtered_pattern = entry->pattern & pattern;
11806
11807         /*
11808          * If the user requested specific flags in the pattern (e.g.
11809          * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11810          * flags.
11811          *
11812          * If the user did not specify any flags, it doesn't matter whether
11813          * or not the command supports the flags.
11814          */
11815         if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11816              (pattern & ~CTL_LUN_PAT_MASK))
11817                 return (CTL_LUN_PAT_NONE);
11818
11819         /*
11820          * If the user asked for a range check, see if the requested LBA
11821          * range overlaps with this command's LBA range.
11822          */
11823         if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11824                 uint64_t lba1;
11825                 uint32_t len1;
11826                 ctl_action action;
11827                 int retval;
11828
11829                 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11830                 if (retval != 0)
11831                         return (CTL_LUN_PAT_NONE);
11832
11833                 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11834                                               desc->lba_range.len);
11835                 /*
11836                  * A "pass" means that the LBA ranges don't overlap, so
11837                  * this doesn't match the user's range criteria.
11838                  */
11839                 if (action == CTL_ACTION_PASS)
11840                         return (CTL_LUN_PAT_NONE);
11841         }
11842
11843         return (filtered_pattern);
11844 }
11845
11846 static void
11847 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11848 {
11849         struct ctl_error_desc *desc, *desc2;
11850
11851         mtx_assert(&lun->lun_lock, MA_OWNED);
11852
11853         STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11854                 ctl_lun_error_pattern pattern;
11855                 /*
11856                  * Check to see whether this particular command matches
11857                  * the pattern in the descriptor.
11858                  */
11859                 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11860                 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11861                         continue;
11862
11863                 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11864                 case CTL_LUN_INJ_ABORTED:
11865                         ctl_set_aborted(&io->scsiio);
11866                         break;
11867                 case CTL_LUN_INJ_MEDIUM_ERR:
11868                         ctl_set_medium_error(&io->scsiio);
11869                         break;
11870                 case CTL_LUN_INJ_UA:
11871                         /* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET
11872                          * OCCURRED */
11873                         ctl_set_ua(&io->scsiio, 0x29, 0x00);
11874                         break;
11875                 case CTL_LUN_INJ_CUSTOM:
11876                         /*
11877                          * We're assuming the user knows what he is doing.
11878                          * Just copy the sense information without doing
11879                          * checks.
11880                          */
11881                         bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11882                               ctl_min(sizeof(desc->custom_sense),
11883                                       sizeof(io->scsiio.sense_data)));
11884                         io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11885                         io->scsiio.sense_len = SSD_FULL_SIZE;
11886                         io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11887                         break;
11888                 case CTL_LUN_INJ_NONE:
11889                 default:
11890                         /*
11891                          * If this is an error injection type we don't know
11892                          * about, clear the continuous flag (if it is set)
11893                          * so it will get deleted below.
11894                          */
11895                         desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11896                         break;
11897                 }
11898                 /*
11899                  * By default, each error injection action is a one-shot
11900                  */
11901                 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11902                         continue;
11903
11904                 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11905
11906                 free(desc, M_CTL);
11907         }
11908 }
11909
11910 #ifdef CTL_IO_DELAY
11911 static void
11912 ctl_datamove_timer_wakeup(void *arg)
11913 {
11914         union ctl_io *io;
11915
11916         io = (union ctl_io *)arg;
11917
11918         ctl_datamove(io);
11919 }
11920 #endif /* CTL_IO_DELAY */
11921
11922 void
11923 ctl_datamove(union ctl_io *io)
11924 {
11925         void (*fe_datamove)(union ctl_io *io);
11926
11927         mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11928
11929         CTL_DEBUG_PRINT(("ctl_datamove\n"));
11930
11931 #ifdef CTL_TIME_IO
11932         if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11933                 char str[256];
11934                 char path_str[64];
11935                 struct sbuf sb;
11936
11937                 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11938                 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11939
11940                 sbuf_cat(&sb, path_str);
11941                 switch (io->io_hdr.io_type) {
11942                 case CTL_IO_SCSI:
11943                         ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11944                         sbuf_printf(&sb, "\n");
11945                         sbuf_cat(&sb, path_str);
11946                         sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11947                                     io->scsiio.tag_num, io->scsiio.tag_type);
11948                         break;
11949                 case CTL_IO_TASK:
11950                         sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11951                                     "Tag Type: %d\n", io->taskio.task_action,
11952                                     io->taskio.tag_num, io->taskio.tag_type);
11953                         break;
11954                 default:
11955                         printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11956                         panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11957                         break;
11958                 }
11959                 sbuf_cat(&sb, path_str);
11960                 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11961                             (intmax_t)time_uptime - io->io_hdr.start_time);
11962                 sbuf_finish(&sb);
11963                 printf("%s", sbuf_data(&sb));
11964         }
11965 #endif /* CTL_TIME_IO */
11966
11967 #ifdef CTL_IO_DELAY
11968         if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11969                 struct ctl_lun *lun;
11970
11971                 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11972
11973                 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11974         } else {
11975                 struct ctl_lun *lun;
11976
11977                 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11978                 if ((lun != NULL)
11979                  && (lun->delay_info.datamove_delay > 0)) {
11980                         struct callout *callout;
11981
11982                         callout = (struct callout *)&io->io_hdr.timer_bytes;
11983                         callout_init(callout, /*mpsafe*/ 1);
11984                         io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11985                         callout_reset(callout,
11986                                       lun->delay_info.datamove_delay * hz,
11987                                       ctl_datamove_timer_wakeup, io);
11988                         if (lun->delay_info.datamove_type ==
11989                             CTL_DELAY_TYPE_ONESHOT)
11990                                 lun->delay_info.datamove_delay = 0;
11991                         return;
11992                 }
11993         }
11994 #endif
11995
11996         /*
11997          * This command has been aborted.  Set the port status, so we fail
11998          * the data move.
11999          */
12000         if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12001                 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12002                        io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12003                        io->io_hdr.nexus.targ_port,
12004                        (uintmax_t)io->io_hdr.nexus.targ_target.id,
12005                        io->io_hdr.nexus.targ_lun);
12006                 io->io_hdr.status = CTL_CMD_ABORTED;
12007                 io->io_hdr.port_status = 31337;
12008                 /*
12009                  * Note that the backend, in this case, will get the
12010                  * callback in its context.  In other cases it may get
12011                  * called in the frontend's interrupt thread context.
12012                  */
12013                 io->scsiio.be_move_done(io);
12014                 return;
12015         }
12016
12017         /*
12018          * If we're in XFER mode and this I/O is from the other shelf
12019          * controller, we need to send the DMA to the other side to
12020          * actually transfer the data to/from the host.  In serialize only
12021          * mode the transfer happens below CTL and ctl_datamove() is only
12022          * called on the machine that originally received the I/O.
12023          */
12024         if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12025          && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12026                 union ctl_ha_msg msg;
12027                 uint32_t sg_entries_sent;
12028                 int do_sg_copy;
12029                 int i;
12030
12031                 memset(&msg, 0, sizeof(msg));
12032                 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12033                 msg.hdr.original_sc = io->io_hdr.original_sc;
12034                 msg.hdr.serializing_sc = io;
12035                 msg.hdr.nexus = io->io_hdr.nexus;
12036                 msg.dt.flags = io->io_hdr.flags;
12037                 /*
12038                  * We convert everything into a S/G list here.  We can't
12039                  * pass by reference, only by value between controllers.
12040                  * So we can't pass a pointer to the S/G list, only as many
12041                  * S/G entries as we can fit in here.  If it's possible for
12042                  * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12043                  * then we need to break this up into multiple transfers.
12044                  */
12045                 if (io->scsiio.kern_sg_entries == 0) {
12046                         msg.dt.kern_sg_entries = 1;
12047                         /*
12048                          * If this is in cached memory, flush the cache
12049                          * before we send the DMA request to the other
12050                          * controller.  We want to do this in either the
12051                          * read or the write case.  The read case is
12052                          * straightforward.  In the write case, we want to
12053                          * make sure nothing is in the local cache that
12054                          * could overwrite the DMAed data.
12055                          */
12056                         if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12057                                 /*
12058                                  * XXX KDM use bus_dmamap_sync() here.
12059                                  */
12060                         }
12061
12062                         /*
12063                          * Convert to a physical address if this is a
12064                          * virtual address.
12065                          */
12066                         if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12067                                 msg.dt.sg_list[0].addr =
12068                                         io->scsiio.kern_data_ptr;
12069                         } else {
12070                                 /*
12071                                  * XXX KDM use busdma here!
12072                                  */
12073 #if 0
12074                                 msg.dt.sg_list[0].addr = (void *)
12075                                         vtophys(io->scsiio.kern_data_ptr);
12076 #endif
12077                         }
12078
12079                         msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12080                         do_sg_copy = 0;
12081                 } else {
12082                         struct ctl_sg_entry *sgl;
12083
12084                         do_sg_copy = 1;
12085                         msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12086                         sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12087                         if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12088                                 /*
12089                                  * XXX KDM use bus_dmamap_sync() here.
12090                                  */
12091                         }
12092                 }
12093
12094                 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12095                 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12096                 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12097                 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12098                 msg.dt.sg_sequence = 0;
12099
12100                 /*
12101                  * Loop until we've sent all of the S/G entries.  On the
12102                  * other end, we'll recompose these S/G entries into one
12103                  * contiguous list before passing it to the
12104                  */
12105                 for (sg_entries_sent = 0; sg_entries_sent <
12106                      msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12107                         msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12108                                 sizeof(msg.dt.sg_list[0])),
12109                                 msg.dt.kern_sg_entries - sg_entries_sent);
12110
12111                         if (do_sg_copy != 0) {
12112                                 struct ctl_sg_entry *sgl;
12113                                 int j;
12114
12115                                 sgl = (struct ctl_sg_entry *)
12116                                         io->scsiio.kern_data_ptr;
12117                                 /*
12118                                  * If this is in cached memory, flush the cache
12119                                  * before we send the DMA request to the other
12120                                  * controller.  We want to do this in either
12121                                  * the * read or the write case.  The read
12122                                  * case is straightforward.  In the write
12123                                  * case, we want to make sure nothing is
12124                                  * in the local cache that could overwrite
12125                                  * the DMAed data.
12126                                  */
12127
12128                                 for (i = sg_entries_sent, j = 0;
12129                                      i < msg.dt.cur_sg_entries; i++, j++) {
12130                                         if ((io->io_hdr.flags &
12131                                              CTL_FLAG_NO_DATASYNC) == 0) {
12132                                                 /*
12133                                                  * XXX KDM use bus_dmamap_sync()
12134                                                  */
12135                                         }
12136                                         if ((io->io_hdr.flags &
12137                                              CTL_FLAG_BUS_ADDR) == 0) {
12138                                                 /*
12139                                                  * XXX KDM use busdma.
12140                                                  */
12141 #if 0
12142                                                 msg.dt.sg_list[j].addr =(void *)
12143                                                        vtophys(sgl[i].addr);
12144 #endif
12145                                         } else {
12146                                                 msg.dt.sg_list[j].addr =
12147                                                         sgl[i].addr;
12148                                         }
12149                                         msg.dt.sg_list[j].len = sgl[i].len;
12150                                 }
12151                         }
12152
12153                         sg_entries_sent += msg.dt.cur_sg_entries;
12154                         if (sg_entries_sent >= msg.dt.kern_sg_entries)
12155                                 msg.dt.sg_last = 1;
12156                         else
12157                                 msg.dt.sg_last = 0;
12158
12159                         /*
12160                          * XXX KDM drop and reacquire the lock here?
12161                          */
12162                         if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12163                             sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12164                                 /*
12165                                  * XXX do something here.
12166                                  */
12167                         }
12168
12169                         msg.dt.sent_sg_entries = sg_entries_sent;
12170                 }
12171                 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12172                 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12173                         ctl_failover_io(io, /*have_lock*/ 0);
12174
12175         } else {
12176
12177                 /*
12178                  * Lookup the fe_datamove() function for this particular
12179                  * front end.
12180                  */
12181                 fe_datamove =
12182                     control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12183
12184                 fe_datamove(io);
12185         }
12186 }
12187
12188 static void
12189 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12190 {
12191         union ctl_ha_msg msg;
12192         int isc_status;
12193
12194         memset(&msg, 0, sizeof(msg));
12195
12196         msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12197         msg.hdr.original_sc = io;
12198         msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12199         msg.hdr.nexus = io->io_hdr.nexus;
12200         msg.hdr.status = io->io_hdr.status;
12201         msg.scsi.tag_num = io->scsiio.tag_num;
12202         msg.scsi.tag_type = io->scsiio.tag_type;
12203         msg.scsi.scsi_status = io->scsiio.scsi_status;
12204         memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12205                sizeof(io->scsiio.sense_data));
12206         msg.scsi.sense_len = io->scsiio.sense_len;
12207         msg.scsi.sense_residual = io->scsiio.sense_residual;
12208         msg.scsi.fetd_status = io->io_hdr.port_status;
12209         msg.scsi.residual = io->scsiio.residual;
12210         io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12211
12212         if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12213                 ctl_failover_io(io, /*have_lock*/ have_lock);
12214                 return;
12215         }
12216
12217         isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12218         if (isc_status > CTL_HA_STATUS_SUCCESS) {
12219                 /* XXX do something if this fails */
12220         }
12221
12222 }
12223
12224 /*
12225  * The DMA to the remote side is done, now we need to tell the other side
12226  * we're done so it can continue with its data movement.
12227  */
12228 static void
12229 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12230 {
12231         union ctl_io *io;
12232
12233         io = rq->context;
12234
12235         if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12236                 printf("%s: ISC DMA write failed with error %d", __func__,
12237                        rq->ret);
12238                 ctl_set_internal_failure(&io->scsiio,
12239                                          /*sks_valid*/ 1,
12240                                          /*retry_count*/ rq->ret);
12241         }
12242
12243         ctl_dt_req_free(rq);
12244
12245         /*
12246          * In this case, we had to malloc the memory locally.  Free it.
12247          */
12248         if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12249                 int i;
12250                 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12251                         free(io->io_hdr.local_sglist[i].addr, M_CTL);
12252         }
12253         /*
12254          * The data is in local and remote memory, so now we need to send
12255          * status (good or back) back to the other side.
12256          */
12257         ctl_send_datamove_done(io, /*have_lock*/ 0);
12258 }
12259
12260 /*
12261  * We've moved the data from the host/controller into local memory.  Now we
12262  * need to push it over to the remote controller's memory.
12263  */
12264 static int
12265 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12266 {
12267         int retval;
12268
12269         retval = 0;
12270
12271         retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12272                                           ctl_datamove_remote_write_cb);
12273
12274         return (retval);
12275 }
12276
12277 static void
12278 ctl_datamove_remote_write(union ctl_io *io)
12279 {
12280         int retval;
12281         void (*fe_datamove)(union ctl_io *io);
12282
12283         /*
12284          * - Get the data from the host/HBA into local memory.
12285          * - DMA memory from the local controller to the remote controller.
12286          * - Send status back to the remote controller.
12287          */
12288
12289         retval = ctl_datamove_remote_sgl_setup(io);
12290         if (retval != 0)
12291                 return;
12292
12293         /* Switch the pointer over so the FETD knows what to do */
12294         io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12295
12296         /*
12297          * Use a custom move done callback, since we need to send completion
12298          * back to the other controller, not to the backend on this side.
12299          */
12300         io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12301
12302         fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12303
12304         fe_datamove(io);
12305
12306         return;
12307
12308 }
12309
12310 static int
12311 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12312 {
12313 #if 0
12314         char str[256];
12315         char path_str[64];
12316         struct sbuf sb;
12317 #endif
12318
12319         /*
12320          * In this case, we had to malloc the memory locally.  Free it.
12321          */
12322         if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12323                 int i;
12324                 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12325                         free(io->io_hdr.local_sglist[i].addr, M_CTL);
12326         }
12327
12328 #if 0
12329         scsi_path_string(io, path_str, sizeof(path_str));
12330         sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12331         sbuf_cat(&sb, path_str);
12332         scsi_command_string(&io->scsiio, NULL, &sb);
12333         sbuf_printf(&sb, "\n");
12334         sbuf_cat(&sb, path_str);
12335         sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12336                     io->scsiio.tag_num, io->scsiio.tag_type);
12337         sbuf_cat(&sb, path_str);
12338         sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12339                     io->io_hdr.flags, io->io_hdr.status);
12340         sbuf_finish(&sb);
12341         printk("%s", sbuf_data(&sb));
12342 #endif
12343
12344
12345         /*
12346          * The read is done, now we need to send status (good or bad) back
12347          * to the other side.
12348          */
12349         ctl_send_datamove_done(io, /*have_lock*/ 0);
12350
12351         return (0);
12352 }
12353
12354 static void
12355 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12356 {
12357         union ctl_io *io;
12358         void (*fe_datamove)(union ctl_io *io);
12359
12360         io = rq->context;
12361
12362         if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12363                 printf("%s: ISC DMA read failed with error %d", __func__,
12364                        rq->ret);
12365                 ctl_set_internal_failure(&io->scsiio,
12366                                          /*sks_valid*/ 1,
12367                                          /*retry_count*/ rq->ret);
12368         }
12369
12370         ctl_dt_req_free(rq);
12371
12372         /* Switch the pointer over so the FETD knows what to do */
12373         io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12374
12375         /*
12376          * Use a custom move done callback, since we need to send completion
12377          * back to the other controller, not to the backend on this side.
12378          */
12379         io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12380
12381         /* XXX KDM add checks like the ones in ctl_datamove? */
12382
12383         fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12384
12385         fe_datamove(io);
12386 }
12387
12388 static int
12389 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12390 {
12391         struct ctl_sg_entry *local_sglist, *remote_sglist;
12392         struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12393         struct ctl_softc *softc;
12394         int retval;
12395         int i;
12396
12397         retval = 0;
12398         softc = control_softc;
12399
12400         local_sglist = io->io_hdr.local_sglist;
12401         local_dma_sglist = io->io_hdr.local_dma_sglist;
12402         remote_sglist = io->io_hdr.remote_sglist;
12403         remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12404
12405         if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12406                 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12407                         local_sglist[i].len = remote_sglist[i].len;
12408
12409                         /*
12410                          * XXX Detect the situation where the RS-level I/O
12411                          * redirector on the other side has already read the
12412                          * data off of the AOR RS on this side, and
12413                          * transferred it to remote (mirror) memory on the
12414                          * other side.  Since we already have the data in
12415                          * memory here, we just need to use it.
12416                          *
12417                          * XXX KDM this can probably be removed once we
12418                          * get the cache device code in and take the
12419                          * current AOR implementation out.
12420                          */
12421 #ifdef NEEDTOPORT
12422                         if ((remote_sglist[i].addr >=
12423                              (void *)vtophys(softc->mirr->addr))
12424                          && (remote_sglist[i].addr <
12425                              ((void *)vtophys(softc->mirr->addr) +
12426                              CacheMirrorOffset))) {
12427                                 local_sglist[i].addr = remote_sglist[i].addr -
12428                                         CacheMirrorOffset;
12429                                 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12430                                      CTL_FLAG_DATA_IN)
12431                                         io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12432                         } else {
12433                                 local_sglist[i].addr = remote_sglist[i].addr +
12434                                         CacheMirrorOffset;
12435                         }
12436 #endif
12437 #if 0
12438                         printf("%s: local %p, remote %p, len %d\n",
12439                                __func__, local_sglist[i].addr,
12440                                remote_sglist[i].addr, local_sglist[i].len);
12441 #endif
12442                 }
12443         } else {
12444                 uint32_t len_to_go;
12445
12446                 /*
12447                  * In this case, we don't have automatically allocated
12448                  * memory for this I/O on this controller.  This typically
12449                  * happens with internal CTL I/O -- e.g. inquiry, mode
12450                  * sense, etc.  Anything coming from RAIDCore will have
12451                  * a mirror area available.
12452                  */
12453                 len_to_go = io->scsiio.kern_data_len;
12454
12455                 /*
12456                  * Clear the no datasync flag, we have to use malloced
12457                  * buffers.
12458                  */
12459                 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12460
12461                 /*
12462                  * The difficult thing here is that the size of the various
12463                  * S/G segments may be different than the size from the
12464                  * remote controller.  That'll make it harder when DMAing
12465                  * the data back to the other side.
12466                  */
12467                 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12468                      sizeof(io->io_hdr.remote_sglist[0])) &&
12469                      (len_to_go > 0); i++) {
12470                         local_sglist[i].len = ctl_min(len_to_go, 131072);
12471                         CTL_SIZE_8B(local_dma_sglist[i].len,
12472                                     local_sglist[i].len);
12473                         local_sglist[i].addr =
12474                                 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12475
12476                         local_dma_sglist[i].addr = local_sglist[i].addr;
12477
12478                         if (local_sglist[i].addr == NULL) {
12479                                 int j;
12480
12481                                 printf("malloc failed for %zd bytes!",
12482                                        local_dma_sglist[i].len);
12483                                 for (j = 0; j < i; j++) {
12484                                         free(local_sglist[j].addr, M_CTL);
12485                                 }
12486                                 ctl_set_internal_failure(&io->scsiio,
12487                                                          /*sks_valid*/ 1,
12488                                                          /*retry_count*/ 4857);
12489                                 retval = 1;
12490                                 goto bailout_error;
12491                                 
12492                         }
12493                         /* XXX KDM do we need a sync here? */
12494
12495                         len_to_go -= local_sglist[i].len;
12496                 }
12497                 /*
12498                  * Reset the number of S/G entries accordingly.  The
12499                  * original number of S/G entries is available in
12500                  * rem_sg_entries.
12501                  */
12502                 io->scsiio.kern_sg_entries = i;
12503
12504 #if 0
12505                 printf("%s: kern_sg_entries = %d\n", __func__,
12506                        io->scsiio.kern_sg_entries);
12507                 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12508                         printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12509                                local_sglist[i].addr, local_sglist[i].len,
12510                                local_dma_sglist[i].len);
12511 #endif
12512         }
12513
12514
12515         return (retval);
12516
12517 bailout_error:
12518
12519         ctl_send_datamove_done(io, /*have_lock*/ 0);
12520
12521         return (retval);
12522 }
12523
12524 static int
12525 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12526                          ctl_ha_dt_cb callback)
12527 {
12528         struct ctl_ha_dt_req *rq;
12529         struct ctl_sg_entry *remote_sglist, *local_sglist;
12530         struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12531         uint32_t local_used, remote_used, total_used;
12532         int retval;
12533         int i, j;
12534
12535         retval = 0;
12536
12537         rq = ctl_dt_req_alloc();
12538
12539         /*
12540          * If we failed to allocate the request, and if the DMA didn't fail
12541          * anyway, set busy status.  This is just a resource allocation
12542          * failure.
12543          */
12544         if ((rq == NULL)
12545          && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12546                 ctl_set_busy(&io->scsiio);
12547
12548         if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12549
12550                 if (rq != NULL)
12551                         ctl_dt_req_free(rq);
12552
12553                 /*
12554                  * The data move failed.  We need to return status back
12555                  * to the other controller.  No point in trying to DMA
12556                  * data to the remote controller.
12557                  */
12558
12559                 ctl_send_datamove_done(io, /*have_lock*/ 0);
12560
12561                 retval = 1;
12562
12563                 goto bailout;
12564         }
12565
12566         local_sglist = io->io_hdr.local_sglist;
12567         local_dma_sglist = io->io_hdr.local_dma_sglist;
12568         remote_sglist = io->io_hdr.remote_sglist;
12569         remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12570         local_used = 0;
12571         remote_used = 0;
12572         total_used = 0;
12573
12574         if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12575                 rq->ret = CTL_HA_STATUS_SUCCESS;
12576                 rq->context = io;
12577                 callback(rq);
12578                 goto bailout;
12579         }
12580
12581         /*
12582          * Pull/push the data over the wire from/to the other controller.
12583          * This takes into account the possibility that the local and
12584          * remote sglists may not be identical in terms of the size of
12585          * the elements and the number of elements.
12586          *
12587          * One fundamental assumption here is that the length allocated for
12588          * both the local and remote sglists is identical.  Otherwise, we've
12589          * essentially got a coding error of some sort.
12590          */
12591         for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12592                 int isc_ret;
12593                 uint32_t cur_len, dma_length;
12594                 uint8_t *tmp_ptr;
12595
12596                 rq->id = CTL_HA_DATA_CTL;
12597                 rq->command = command;
12598                 rq->context = io;
12599
12600                 /*
12601                  * Both pointers should be aligned.  But it is possible
12602                  * that the allocation length is not.  They should both
12603                  * also have enough slack left over at the end, though,
12604                  * to round up to the next 8 byte boundary.
12605                  */
12606                 cur_len = ctl_min(local_sglist[i].len - local_used,
12607                                   remote_sglist[j].len - remote_used);
12608
12609                 /*
12610                  * In this case, we have a size issue and need to decrease
12611                  * the size, except in the case where we actually have less
12612                  * than 8 bytes left.  In that case, we need to increase
12613                  * the DMA length to get the last bit.
12614                  */
12615                 if ((cur_len & 0x7) != 0) {
12616                         if (cur_len > 0x7) {
12617                                 cur_len = cur_len - (cur_len & 0x7);
12618                                 dma_length = cur_len;
12619                         } else {
12620                                 CTL_SIZE_8B(dma_length, cur_len);
12621                         }
12622
12623                 } else
12624                         dma_length = cur_len;
12625
12626                 /*
12627                  * If we had to allocate memory for this I/O, instead of using
12628                  * the non-cached mirror memory, we'll need to flush the cache
12629                  * before trying to DMA to the other controller.
12630                  *
12631                  * We could end up doing this multiple times for the same
12632                  * segment if we have a larger local segment than remote
12633                  * segment.  That shouldn't be an issue.
12634                  */
12635                 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12636                         /*
12637                          * XXX KDM use bus_dmamap_sync() here.
12638                          */
12639                 }
12640
12641                 rq->size = dma_length;
12642
12643                 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12644                 tmp_ptr += local_used;
12645
12646                 /* Use physical addresses when talking to ISC hardware */
12647                 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12648                         /* XXX KDM use busdma */
12649 #if 0
12650                         rq->local = vtophys(tmp_ptr);
12651 #endif
12652                 } else
12653                         rq->local = tmp_ptr;
12654
12655                 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12656                 tmp_ptr += remote_used;
12657                 rq->remote = tmp_ptr;
12658
12659                 rq->callback = NULL;
12660
12661                 local_used += cur_len;
12662                 if (local_used >= local_sglist[i].len) {
12663                         i++;
12664                         local_used = 0;
12665                 }
12666
12667                 remote_used += cur_len;
12668                 if (remote_used >= remote_sglist[j].len) {
12669                         j++;
12670                         remote_used = 0;
12671                 }
12672                 total_used += cur_len;
12673
12674                 if (total_used >= io->scsiio.kern_data_len)
12675                         rq->callback = callback;
12676
12677                 if ((rq->size & 0x7) != 0) {
12678                         printf("%s: warning: size %d is not on 8b boundary\n",
12679                                __func__, rq->size);
12680                 }
12681                 if (((uintptr_t)rq->local & 0x7) != 0) {
12682                         printf("%s: warning: local %p not on 8b boundary\n",
12683                                __func__, rq->local);
12684                 }
12685                 if (((uintptr_t)rq->remote & 0x7) != 0) {
12686                         printf("%s: warning: remote %p not on 8b boundary\n",
12687                                __func__, rq->local);
12688                 }
12689 #if 0
12690                 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12691                        (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12692                        rq->local, rq->remote, rq->size);
12693 #endif
12694
12695                 isc_ret = ctl_dt_single(rq);
12696                 if (isc_ret == CTL_HA_STATUS_WAIT)
12697                         continue;
12698
12699                 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12700                         rq->ret = CTL_HA_STATUS_SUCCESS;
12701                 } else {
12702                         rq->ret = isc_ret;
12703                 }
12704                 callback(rq);
12705                 goto bailout;
12706         }
12707
12708 bailout:
12709         return (retval);
12710
12711 }
12712
12713 static void
12714 ctl_datamove_remote_read(union ctl_io *io)
12715 {
12716         int retval;
12717         int i;
12718
12719         /*
12720          * This will send an error to the other controller in the case of a
12721          * failure.
12722          */
12723         retval = ctl_datamove_remote_sgl_setup(io);
12724         if (retval != 0)
12725                 return;
12726
12727         retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12728                                           ctl_datamove_remote_read_cb);
12729         if ((retval != 0)
12730          && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12731                 /*
12732                  * Make sure we free memory if there was an error..  The
12733                  * ctl_datamove_remote_xfer() function will send the
12734                  * datamove done message, or call the callback with an
12735                  * error if there is a problem.
12736                  */
12737                 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12738                         free(io->io_hdr.local_sglist[i].addr, M_CTL);
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         struct ctl_softc *softc;
12755
12756         softc = control_softc;
12757
12758         mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12759
12760         /*
12761          * Note that we look for an aborted I/O here, but don't do some of
12762          * the other checks that ctl_datamove() normally does.  We don't
12763          * need to run the task queue, because this I/O is on the ISC
12764          * queue, which is executed by the work thread after the task queue.
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
12770                 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12771                        io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12772                        io->io_hdr.nexus.targ_port,
12773                        io->io_hdr.nexus.targ_target.id,
12774                        io->io_hdr.nexus.targ_lun);
12775                 io->io_hdr.status = CTL_CMD_ABORTED;
12776                 io->io_hdr.port_status = 31338;
12777
12778                 ctl_send_datamove_done(io, /*have_lock*/ 0);
12779
12780                 return;
12781         }
12782
12783         if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12784                 ctl_datamove_remote_write(io);
12785         } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12786                 ctl_datamove_remote_read(io);
12787         } else {
12788                 union ctl_ha_msg msg;
12789                 struct scsi_sense_data *sense;
12790                 uint8_t sks[3];
12791                 int retry_count;
12792
12793                 memset(&msg, 0, sizeof(msg));
12794
12795                 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12796                 msg.hdr.status = CTL_SCSI_ERROR;
12797                 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12798
12799                 retry_count = 4243;
12800
12801                 sense = &msg.scsi.sense_data;
12802                 sks[0] = SSD_SCS_VALID;
12803                 sks[1] = (retry_count >> 8) & 0xff;
12804                 sks[2] = retry_count & 0xff;
12805
12806                 /* "Internal target failure" */
12807                 scsi_set_sense_data(sense,
12808                                     /*sense_format*/ SSD_TYPE_NONE,
12809                                     /*current_error*/ 1,
12810                                     /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12811                                     /*asc*/ 0x44,
12812                                     /*ascq*/ 0x00,
12813                                     /*type*/ SSD_ELEM_SKS,
12814                                     /*size*/ sizeof(sks),
12815                                     /*data*/ sks,
12816                                     SSD_ELEM_NONE);
12817
12818                 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12819                 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12820                         ctl_failover_io(io, /*have_lock*/ 1);
12821                         return;
12822                 }
12823
12824                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12825                     CTL_HA_STATUS_SUCCESS) {
12826                         /* XXX KDM what to do if this fails? */
12827                 }
12828                 return;
12829         }
12830         
12831 }
12832
12833 static int
12834 ctl_process_done(union ctl_io *io)
12835 {
12836         struct ctl_lun *lun;
12837         struct ctl_softc *ctl_softc;
12838         void (*fe_done)(union ctl_io *io);
12839         uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12840
12841         CTL_DEBUG_PRINT(("ctl_process_done\n"));
12842
12843         fe_done =
12844             control_softc->ctl_ports[targ_port]->fe_done;
12845
12846 #ifdef CTL_TIME_IO
12847         if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12848                 char str[256];
12849                 char path_str[64];
12850                 struct sbuf sb;
12851
12852                 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12853                 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12854
12855                 sbuf_cat(&sb, path_str);
12856                 switch (io->io_hdr.io_type) {
12857                 case CTL_IO_SCSI:
12858                         ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12859                         sbuf_printf(&sb, "\n");
12860                         sbuf_cat(&sb, path_str);
12861                         sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12862                                     io->scsiio.tag_num, io->scsiio.tag_type);
12863                         break;
12864                 case CTL_IO_TASK:
12865                         sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12866                                     "Tag Type: %d\n", io->taskio.task_action,
12867                                     io->taskio.tag_num, io->taskio.tag_type);
12868                         break;
12869                 default:
12870                         printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12871                         panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12872                         break;
12873                 }
12874                 sbuf_cat(&sb, path_str);
12875                 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12876                             (intmax_t)time_uptime - io->io_hdr.start_time);
12877                 sbuf_finish(&sb);
12878                 printf("%s", sbuf_data(&sb));
12879         }
12880 #endif /* CTL_TIME_IO */
12881
12882         switch (io->io_hdr.io_type) {
12883         case CTL_IO_SCSI:
12884                 break;
12885         case CTL_IO_TASK:
12886                 if (bootverbose || verbose > 0)
12887                         ctl_io_error_print(io, NULL);
12888                 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12889                         ctl_free_io(io);
12890                 else
12891                         fe_done(io);
12892                 return (CTL_RETVAL_COMPLETE);
12893                 break;
12894         default:
12895                 printf("ctl_process_done: invalid io type %d\n",
12896                        io->io_hdr.io_type);
12897                 panic("ctl_process_done: invalid io type %d\n",
12898                       io->io_hdr.io_type);
12899                 break; /* NOTREACHED */
12900         }
12901
12902         lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12903         if (lun == NULL) {
12904                 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12905                                  io->io_hdr.nexus.targ_mapped_lun));
12906                 fe_done(io);
12907                 goto bailout;
12908         }
12909         ctl_softc = lun->ctl_softc;
12910
12911         mtx_lock(&lun->lun_lock);
12912
12913         /*
12914          * Check to see if we have any errors to inject here.  We only
12915          * inject errors for commands that don't already have errors set.
12916          */
12917         if ((STAILQ_FIRST(&lun->error_list) != NULL)
12918          && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12919                 ctl_inject_error(lun, io);
12920
12921         /*
12922          * XXX KDM how do we treat commands that aren't completed
12923          * successfully?
12924          *
12925          * XXX KDM should we also track I/O latency?
12926          */
12927         if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12928             io->io_hdr.io_type == CTL_IO_SCSI) {
12929 #ifdef CTL_TIME_IO
12930                 struct bintime cur_bt;
12931 #endif
12932                 int type;
12933
12934                 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12935                     CTL_FLAG_DATA_IN)
12936                         type = CTL_STATS_READ;
12937                 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12938                     CTL_FLAG_DATA_OUT)
12939                         type = CTL_STATS_WRITE;
12940                 else
12941                         type = CTL_STATS_NO_IO;
12942
12943                 lun->stats.ports[targ_port].bytes[type] +=
12944                     io->scsiio.kern_total_len;
12945                 lun->stats.ports[targ_port].operations[type]++;
12946 #ifdef CTL_TIME_IO
12947                 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
12948                    &io->io_hdr.dma_bt);
12949                 lun->stats.ports[targ_port].num_dmas[type] +=
12950                     io->io_hdr.num_dmas;
12951                 getbintime(&cur_bt);
12952                 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12953                 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
12954 #endif
12955         }
12956
12957         /*
12958          * Remove this from the OOA queue.
12959          */
12960         TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12961
12962         /*
12963          * Run through the blocked queue on this LUN and see if anything
12964          * has become unblocked, now that this transaction is done.
12965          */
12966         ctl_check_blocked(lun);
12967
12968         /*
12969          * If the LUN has been invalidated, free it if there is nothing
12970          * left on its OOA queue.
12971          */
12972         if ((lun->flags & CTL_LUN_INVALID)
12973          && TAILQ_EMPTY(&lun->ooa_queue)) {
12974                 mtx_unlock(&lun->lun_lock);
12975                 mtx_lock(&ctl_softc->ctl_lock);
12976                 ctl_free_lun(lun);
12977                 mtx_unlock(&ctl_softc->ctl_lock);
12978         } else
12979                 mtx_unlock(&lun->lun_lock);
12980
12981         /*
12982          * If this command has been aborted, make sure we set the status
12983          * properly.  The FETD is responsible for freeing the I/O and doing
12984          * whatever it needs to do to clean up its state.
12985          */
12986         if (io->io_hdr.flags & CTL_FLAG_ABORT)
12987                 io->io_hdr.status = CTL_CMD_ABORTED;
12988
12989         /*
12990          * We print out status for every task management command.  For SCSI
12991          * commands, we filter out any unit attention errors; they happen
12992          * on every boot, and would clutter up the log.  Note:  task
12993          * management commands aren't printed here, they are printed above,
12994          * since they should never even make it down here.
12995          */
12996         switch (io->io_hdr.io_type) {
12997         case CTL_IO_SCSI: {
12998                 int error_code, sense_key, asc, ascq;
12999
13000                 sense_key = 0;
13001
13002                 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13003                  && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13004                         /*
13005                          * Since this is just for printing, no need to
13006                          * show errors here.
13007                          */
13008                         scsi_extract_sense_len(&io->scsiio.sense_data,
13009                                                io->scsiio.sense_len,
13010                                                &error_code,
13011                                                &sense_key,
13012                                                &asc,
13013                                                &ascq,
13014                                                /*show_errors*/ 0);
13015                 }
13016
13017                 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13018                  && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13019                   || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13020                   || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13021
13022                         if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13023                                 ctl_softc->skipped_prints++;
13024                         } else {
13025                                 uint32_t skipped_prints;
13026
13027                                 skipped_prints = ctl_softc->skipped_prints;
13028
13029                                 ctl_softc->skipped_prints = 0;
13030                                 ctl_softc->last_print_jiffies = time_uptime;
13031
13032                                 if (skipped_prints > 0) {
13033 #ifdef NEEDTOPORT
13034                                         csevent_log(CSC_CTL | CSC_SHELF_SW |
13035                                             CTL_ERROR_REPORT,
13036                                             csevent_LogType_Trace,
13037                                             csevent_Severity_Information,
13038                                             csevent_AlertLevel_Green,
13039                                             csevent_FRU_Firmware,
13040                                             csevent_FRU_Unknown,
13041                                             "High CTL error volume, %d prints "
13042                                             "skipped", skipped_prints);
13043 #endif
13044                                 }
13045                                 if (bootverbose || verbose > 0)
13046                                         ctl_io_error_print(io, NULL);
13047                         }
13048                 }
13049                 break;
13050         }
13051         case CTL_IO_TASK:
13052                 if (bootverbose || verbose > 0)
13053                         ctl_io_error_print(io, NULL);
13054                 break;
13055         default:
13056                 break;
13057         }
13058
13059         /*
13060          * Tell the FETD or the other shelf controller we're done with this
13061          * command.  Note that only SCSI commands get to this point.  Task
13062          * management commands are completed above.
13063          *
13064          * We only send status to the other controller if we're in XFER
13065          * mode.  In SER_ONLY mode, the I/O is done on the controller that
13066          * received the I/O (from CTL's perspective), and so the status is
13067          * generated there.
13068          * 
13069          * XXX KDM if we hold the lock here, we could cause a deadlock
13070          * if the frontend comes back in in this context to queue
13071          * something.
13072          */
13073         if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13074          && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13075                 union ctl_ha_msg msg;
13076
13077                 memset(&msg, 0, sizeof(msg));
13078                 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13079                 msg.hdr.original_sc = io->io_hdr.original_sc;
13080                 msg.hdr.nexus = io->io_hdr.nexus;
13081                 msg.hdr.status = io->io_hdr.status;
13082                 msg.scsi.scsi_status = io->scsiio.scsi_status;
13083                 msg.scsi.tag_num = io->scsiio.tag_num;
13084                 msg.scsi.tag_type = io->scsiio.tag_type;
13085                 msg.scsi.sense_len = io->scsiio.sense_len;
13086                 msg.scsi.sense_residual = io->scsiio.sense_residual;
13087                 msg.scsi.residual = io->scsiio.residual;
13088                 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13089                        sizeof(io->scsiio.sense_data));
13090                 /*
13091                  * We copy this whether or not this is an I/O-related
13092                  * command.  Otherwise, we'd have to go and check to see
13093                  * whether it's a read/write command, and it really isn't
13094                  * worth it.
13095                  */
13096                 memcpy(&msg.scsi.lbalen,
13097                        &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13098                        sizeof(msg.scsi.lbalen));
13099
13100                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13101                                 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13102                         /* XXX do something here */
13103                 }
13104
13105                 ctl_free_io(io);
13106         } else 
13107                 fe_done(io);
13108
13109 bailout:
13110
13111         return (CTL_RETVAL_COMPLETE);
13112 }
13113
13114 /*
13115  * Front end should call this if it doesn't do autosense.  When the request
13116  * sense comes back in from the initiator, we'll dequeue this and send it.
13117  */
13118 int
13119 ctl_queue_sense(union ctl_io *io)
13120 {
13121         struct ctl_lun *lun;
13122         struct ctl_softc *ctl_softc;
13123         uint32_t initidx, targ_lun;
13124
13125         ctl_softc = control_softc;
13126
13127         CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13128
13129         /*
13130          * LUN lookup will likely move to the ctl_work_thread() once we
13131          * have our new queueing infrastructure (that doesn't put things on
13132          * a per-LUN queue initially).  That is so that we can handle
13133          * things like an INQUIRY to a LUN that we don't have enabled.  We
13134          * can't deal with that right now.
13135          */
13136         mtx_lock(&ctl_softc->ctl_lock);
13137
13138         /*
13139          * If we don't have a LUN for this, just toss the sense
13140          * information.
13141          */
13142         targ_lun = io->io_hdr.nexus.targ_lun;
13143         if (io->io_hdr.nexus.lun_map_fn != NULL)
13144                 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13145         if ((targ_lun < CTL_MAX_LUNS)
13146          && (ctl_softc->ctl_luns[targ_lun] != NULL))
13147                 lun = ctl_softc->ctl_luns[targ_lun];
13148         else
13149                 goto bailout;
13150
13151         initidx = ctl_get_initindex(&io->io_hdr.nexus);
13152
13153         mtx_lock(&lun->lun_lock);
13154         /*
13155          * Already have CA set for this LUN...toss the sense information.
13156          */
13157         if (ctl_is_set(lun->have_ca, initidx)) {
13158                 mtx_unlock(&lun->lun_lock);
13159                 goto bailout;
13160         }
13161
13162         memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13163                ctl_min(sizeof(lun->pending_sense[initidx].sense),
13164                sizeof(io->scsiio.sense_data)));
13165         ctl_set_mask(lun->have_ca, initidx);
13166         mtx_unlock(&lun->lun_lock);
13167
13168 bailout:
13169         mtx_unlock(&ctl_softc->ctl_lock);
13170
13171         ctl_free_io(io);
13172
13173         return (CTL_RETVAL_COMPLETE);
13174 }
13175
13176 /*
13177  * Primary command inlet from frontend ports.  All SCSI and task I/O
13178  * requests must go through this function.
13179  */
13180 int
13181 ctl_queue(union ctl_io *io)
13182 {
13183         struct ctl_softc *ctl_softc;
13184
13185         CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13186
13187         ctl_softc = control_softc;
13188
13189 #ifdef CTL_TIME_IO
13190         io->io_hdr.start_time = time_uptime;
13191         getbintime(&io->io_hdr.start_bt);
13192 #endif /* CTL_TIME_IO */
13193
13194         /* Map FE-specific LUN ID into global one. */
13195         if (io->io_hdr.nexus.lun_map_fn != NULL)
13196                 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.lun_map_fn(
13197                     io->io_hdr.nexus.lun_map_arg, io->io_hdr.nexus.targ_lun);
13198         else
13199                 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.targ_lun;
13200
13201         switch (io->io_hdr.io_type) {
13202         case CTL_IO_SCSI:
13203                 ctl_enqueue_incoming(io);
13204                 break;
13205         case CTL_IO_TASK:
13206                 ctl_run_task(io);
13207                 break;
13208         default:
13209                 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13210                 return (EINVAL);
13211         }
13212
13213         return (CTL_RETVAL_COMPLETE);
13214 }
13215
13216 #ifdef CTL_IO_DELAY
13217 static void
13218 ctl_done_timer_wakeup(void *arg)
13219 {
13220         union ctl_io *io;
13221
13222         io = (union ctl_io *)arg;
13223         ctl_done(io);
13224 }
13225 #endif /* CTL_IO_DELAY */
13226
13227 void
13228 ctl_done(union ctl_io *io)
13229 {
13230         struct ctl_softc *ctl_softc;
13231
13232         ctl_softc = control_softc;
13233
13234         /*
13235          * Enable this to catch duplicate completion issues.
13236          */
13237 #if 0
13238         if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13239                 printf("%s: type %d msg %d cdb %x iptl: "
13240                        "%d:%d:%d:%d tag 0x%04x "
13241                        "flag %#x status %x\n",
13242                         __func__,
13243                         io->io_hdr.io_type,
13244                         io->io_hdr.msg_type,
13245                         io->scsiio.cdb[0],
13246                         io->io_hdr.nexus.initid.id,
13247                         io->io_hdr.nexus.targ_port,
13248                         io->io_hdr.nexus.targ_target.id,
13249                         io->io_hdr.nexus.targ_lun,
13250                         (io->io_hdr.io_type ==
13251                         CTL_IO_TASK) ?
13252                         io->taskio.tag_num :
13253                         io->scsiio.tag_num,
13254                         io->io_hdr.flags,
13255                         io->io_hdr.status);
13256         } else
13257                 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13258 #endif
13259
13260         /*
13261          * This is an internal copy of an I/O, and should not go through
13262          * the normal done processing logic.
13263          */
13264         if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13265                 return;
13266
13267         /*
13268          * We need to send a msg to the serializing shelf to finish the IO
13269          * as well.  We don't send a finish message to the other shelf if
13270          * this is a task management command.  Task management commands
13271          * aren't serialized in the OOA queue, but rather just executed on
13272          * both shelf controllers for commands that originated on that
13273          * controller.
13274          */
13275         if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13276          && (io->io_hdr.io_type != CTL_IO_TASK)) {
13277                 union ctl_ha_msg msg_io;
13278
13279                 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13280                 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13281                 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13282                     sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13283                 }
13284                 /* continue on to finish IO */
13285         }
13286 #ifdef CTL_IO_DELAY
13287         if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13288                 struct ctl_lun *lun;
13289
13290                 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13291
13292                 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13293         } else {
13294                 struct ctl_lun *lun;
13295
13296                 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13297
13298                 if ((lun != NULL)
13299                  && (lun->delay_info.done_delay > 0)) {
13300                         struct callout *callout;
13301
13302                         callout = (struct callout *)&io->io_hdr.timer_bytes;
13303                         callout_init(callout, /*mpsafe*/ 1);
13304                         io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13305                         callout_reset(callout,
13306                                       lun->delay_info.done_delay * hz,
13307                                       ctl_done_timer_wakeup, io);
13308                         if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13309                                 lun->delay_info.done_delay = 0;
13310                         return;
13311                 }
13312         }
13313 #endif /* CTL_IO_DELAY */
13314
13315         ctl_enqueue_done(io);
13316 }
13317
13318 int
13319 ctl_isc(struct ctl_scsiio *ctsio)
13320 {
13321         struct ctl_lun *lun;
13322         int retval;
13323
13324         lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13325
13326         CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13327
13328         CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13329
13330         retval = lun->backend->data_submit((union ctl_io *)ctsio);
13331
13332         return (retval);
13333 }
13334
13335
13336 static void
13337 ctl_work_thread(void *arg)
13338 {
13339         struct ctl_thread *thr = (struct ctl_thread *)arg;
13340         struct ctl_softc *softc = thr->ctl_softc;
13341         union ctl_io *io;
13342         int retval;
13343
13344         CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13345
13346         for (;;) {
13347                 retval = 0;
13348
13349                 /*
13350                  * We handle the queues in this order:
13351                  * - ISC
13352                  * - done queue (to free up resources, unblock other commands)
13353                  * - RtR queue
13354                  * - incoming queue
13355                  *
13356                  * If those queues are empty, we break out of the loop and
13357                  * go to sleep.
13358                  */
13359                 mtx_lock(&thr->queue_lock);
13360                 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13361                 if (io != NULL) {
13362                         STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13363                         mtx_unlock(&thr->queue_lock);
13364                         ctl_handle_isc(io);
13365                         continue;
13366                 }
13367                 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13368                 if (io != NULL) {
13369                         STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13370                         /* clear any blocked commands, call fe_done */
13371                         mtx_unlock(&thr->queue_lock);
13372                         retval = ctl_process_done(io);
13373                         continue;
13374                 }
13375                 if (!ctl_pause_rtr) {
13376                         io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13377                         if (io != NULL) {
13378                                 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13379                                 mtx_unlock(&thr->queue_lock);
13380                                 retval = ctl_scsiio(&io->scsiio);
13381                                 if (retval != CTL_RETVAL_COMPLETE)
13382                                         CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13383                                 continue;
13384                         }
13385                 }
13386                 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13387                 if (io != NULL) {
13388                         STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13389                         mtx_unlock(&thr->queue_lock);
13390                         ctl_scsiio_precheck(softc, &io->scsiio);
13391                         continue;
13392                 }
13393
13394                 /* Sleep until we have something to do. */
13395                 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13396         }
13397 }
13398
13399 static void
13400 ctl_lun_thread(void *arg)
13401 {
13402         struct ctl_softc *softc = (struct ctl_softc *)arg;
13403         struct ctl_be_lun *be_lun;
13404         int retval;
13405
13406         CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13407
13408         for (;;) {
13409                 retval = 0;
13410                 mtx_lock(&softc->ctl_lock);
13411                 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13412                 if (be_lun != NULL) {
13413                         STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13414                         mtx_unlock(&softc->ctl_lock);
13415                         ctl_create_lun(be_lun);
13416                         continue;
13417                 }
13418
13419                 /* Sleep until we have something to do. */
13420                 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13421                     PDROP | PRIBIO, "-", 0);
13422         }
13423 }
13424
13425 static void
13426 ctl_enqueue_incoming(union ctl_io *io)
13427 {
13428         struct ctl_softc *softc = control_softc;
13429         struct ctl_thread *thr;
13430
13431         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13432         mtx_lock(&thr->queue_lock);
13433         STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13434         mtx_unlock(&thr->queue_lock);
13435         wakeup(thr);
13436 }
13437
13438 static void
13439 ctl_enqueue_rtr(union ctl_io *io)
13440 {
13441         struct ctl_softc *softc = control_softc;
13442         struct ctl_thread *thr;
13443
13444         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13445         mtx_lock(&thr->queue_lock);
13446         STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13447         mtx_unlock(&thr->queue_lock);
13448         wakeup(thr);
13449 }
13450
13451 static void
13452 ctl_enqueue_done(union ctl_io *io)
13453 {
13454         struct ctl_softc *softc = control_softc;
13455         struct ctl_thread *thr;
13456
13457         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13458         mtx_lock(&thr->queue_lock);
13459         STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13460         mtx_unlock(&thr->queue_lock);
13461         wakeup(thr);
13462 }
13463
13464 static void
13465 ctl_enqueue_isc(union ctl_io *io)
13466 {
13467         struct ctl_softc *softc = control_softc;
13468         struct ctl_thread *thr;
13469
13470         thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13471         mtx_lock(&thr->queue_lock);
13472         STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13473         mtx_unlock(&thr->queue_lock);
13474         wakeup(thr);
13475 }
13476
13477 /* Initialization and failover */
13478
13479 void
13480 ctl_init_isc_msg(void)
13481 {
13482         printf("CTL: Still calling this thing\n");
13483 }
13484
13485 /*
13486  * Init component
13487  *      Initializes component into configuration defined by bootMode
13488  *      (see hasc-sv.c)
13489  *      returns hasc_Status:
13490  *              OK
13491  *              ERROR - fatal error
13492  */
13493 static ctl_ha_comp_status
13494 ctl_isc_init(struct ctl_ha_component *c)
13495 {
13496         ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13497
13498         c->status = ret;
13499         return ret;
13500 }
13501
13502 /* Start component
13503  *      Starts component in state requested. If component starts successfully,
13504  *      it must set its own state to the requestrd state
13505  *      When requested state is HASC_STATE_HA, the component may refine it
13506  *      by adding _SLAVE or _MASTER flags.
13507  *      Currently allowed state transitions are:
13508  *      UNKNOWN->HA             - initial startup
13509  *      UNKNOWN->SINGLE - initial startup when no parter detected
13510  *      HA->SINGLE              - failover
13511  * returns ctl_ha_comp_status:
13512  *              OK      - component successfully started in requested state
13513  *              FAILED  - could not start the requested state, failover may
13514  *                        be possible
13515  *              ERROR   - fatal error detected, no future startup possible
13516  */
13517 static ctl_ha_comp_status
13518 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13519 {
13520         ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13521
13522         printf("%s: go\n", __func__);
13523
13524         // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13525         if (c->state == CTL_HA_STATE_UNKNOWN ) {
13526                 ctl_is_single = 0;
13527                 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13528                     != CTL_HA_STATUS_SUCCESS) {
13529                         printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13530                         ret = CTL_HA_COMP_STATUS_ERROR;
13531                 }
13532         } else if (CTL_HA_STATE_IS_HA(c->state)
13533                 && CTL_HA_STATE_IS_SINGLE(state)){
13534                 // HA->SINGLE transition
13535                 ctl_failover();
13536                 ctl_is_single = 1;
13537         } else {
13538                 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13539                        c->state, state);
13540                 ret = CTL_HA_COMP_STATUS_ERROR;
13541         }
13542         if (CTL_HA_STATE_IS_SINGLE(state))
13543                 ctl_is_single = 1;
13544
13545         c->state = state;
13546         c->status = ret;
13547         return ret;
13548 }
13549
13550 /*
13551  * Quiesce component
13552  * The component must clear any error conditions (set status to OK) and
13553  * prepare itself to another Start call
13554  * returns ctl_ha_comp_status:
13555  *      OK
13556  *      ERROR
13557  */
13558 static ctl_ha_comp_status
13559 ctl_isc_quiesce(struct ctl_ha_component *c)
13560 {
13561         int ret = CTL_HA_COMP_STATUS_OK;
13562
13563         ctl_pause_rtr = 1;
13564         c->status = ret;
13565         return ret;
13566 }
13567
13568 struct ctl_ha_component ctl_ha_component_ctlisc =
13569 {
13570         .name = "CTL ISC",
13571         .state = CTL_HA_STATE_UNKNOWN,
13572         .init = ctl_isc_init,
13573         .start = ctl_isc_start,
13574         .quiesce = ctl_isc_quiesce
13575 };
13576
13577 /*
13578  *  vim: ts=8
13579  */