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