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