2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
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>
53 #include <sys/fcntl.h>
55 #include <sys/mutex.h>
56 #include <sys/condvar.h>
57 #include <sys/malloc.h>
59 #include <sys/ioccom.h>
60 #include <sys/queue.h>
62 #include <sys/endian.h>
63 #include <sys/sysctl.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_da.h>
68 #include <cam/ctl/ctl_io.h>
69 #include <cam/ctl/ctl.h>
70 #include <cam/ctl/ctl_frontend.h>
71 #include <cam/ctl/ctl_frontend_internal.h>
72 #include <cam/ctl/ctl_util.h>
73 #include <cam/ctl/ctl_backend.h>
74 #include <cam/ctl/ctl_ioctl.h>
75 #include <cam/ctl/ctl_ha.h>
76 #include <cam/ctl/ctl_private.h>
77 #include <cam/ctl/ctl_debug.h>
78 #include <cam/ctl/ctl_scsi_all.h>
79 #include <cam/ctl/ctl_error.h>
81 struct ctl_softc *control_softc = NULL;
84 * The default is to run with CTL_DONE_THREAD turned on. Completed
85 * transactions are queued for processing by the CTL work thread. When
86 * CTL_DONE_THREAD is not defined, completed transactions are processed in
87 * the caller's context.
89 #define CTL_DONE_THREAD
92 * * Use the serial number and device ID provided by the backend, rather than
93 * * making up our own.
95 #define CTL_USE_BACKEND_SN
98 * Size and alignment macros needed for Copan-specific HA hardware. These
99 * can go away when the HA code is re-written, and uses busdma for any
102 #define CTL_ALIGN_8B(target, source, type) \
103 if (((uint32_t)source & 0x7) != 0) \
104 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
106 target = (type)source;
108 #define CTL_SIZE_8B(target, size) \
109 if ((size & 0x7) != 0) \
110 target = size + (0x8 - (size & 0x7)); \
114 #define CTL_ALIGN_8B_MARGIN 16
117 * Template mode pages.
121 * Note that these are default values only. The actual values will be
122 * filled in when the user does a mode sense.
124 static struct copan_power_subpage power_page_default = {
125 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
126 /*subpage*/ PWR_SUBPAGE_CODE,
127 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
128 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
129 /*page_version*/ PWR_VERSION,
131 /* max_active_luns*/ PWR_DFLT_MAX_LUNS,
132 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137 static struct copan_power_subpage power_page_changeable = {
138 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
139 /*subpage*/ PWR_SUBPAGE_CODE,
140 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
141 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
144 /* max_active_luns*/ 0,
145 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
150 static struct copan_aps_subpage aps_page_default = {
151 APS_PAGE_CODE | SMPH_SPF, //page_code
152 APS_SUBPAGE_CODE, //subpage
153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
155 APS_VERSION, //page_version
157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0} //reserved
162 static struct copan_aps_subpage aps_page_changeable = {
163 APS_PAGE_CODE | SMPH_SPF, //page_code
164 APS_SUBPAGE_CODE, //subpage
165 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
166 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
169 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0} //reserved
174 static struct copan_debugconf_subpage debugconf_page_default = {
175 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
176 DBGCNF_SUBPAGE_CODE, /* subpage */
177 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
178 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
179 DBGCNF_VERSION, /* page_version */
180 {CTL_TIME_IO_DEFAULT_SECS>>8,
181 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
184 static struct copan_debugconf_subpage debugconf_page_changeable = {
185 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
186 DBGCNF_SUBPAGE_CODE, /* subpage */
187 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
188 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
189 0, /* page_version */
190 {0xff,0xff}, /* ctl_time_io_secs */
193 static struct scsi_format_page format_page_default = {
194 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
195 /*page_length*/sizeof(struct scsi_format_page) - 2,
196 /*tracks_per_zone*/ {0, 0},
197 /*alt_sectors_per_zone*/ {0, 0},
198 /*alt_tracks_per_zone*/ {0, 0},
199 /*alt_tracks_per_lun*/ {0, 0},
200 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
201 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
202 /*bytes_per_sector*/ {0, 0},
203 /*interleave*/ {0, 0},
204 /*track_skew*/ {0, 0},
205 /*cylinder_skew*/ {0, 0},
207 /*reserved*/ {0, 0, 0}
210 static struct scsi_format_page format_page_changeable = {
211 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
212 /*page_length*/sizeof(struct scsi_format_page) - 2,
213 /*tracks_per_zone*/ {0, 0},
214 /*alt_sectors_per_zone*/ {0, 0},
215 /*alt_tracks_per_zone*/ {0, 0},
216 /*alt_tracks_per_lun*/ {0, 0},
217 /*sectors_per_track*/ {0, 0},
218 /*bytes_per_sector*/ {0, 0},
219 /*interleave*/ {0, 0},
220 /*track_skew*/ {0, 0},
221 /*cylinder_skew*/ {0, 0},
223 /*reserved*/ {0, 0, 0}
226 static struct scsi_rigid_disk_page rigid_disk_page_default = {
227 /*page_code*/SMS_RIGID_DISK_PAGE,
228 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
229 /*cylinders*/ {0, 0, 0},
230 /*heads*/ CTL_DEFAULT_HEADS,
231 /*start_write_precomp*/ {0, 0, 0},
232 /*start_reduced_current*/ {0, 0, 0},
233 /*step_rate*/ {0, 0},
234 /*landing_zone_cylinder*/ {0, 0, 0},
235 /*rpl*/ SRDP_RPL_DISABLED,
236 /*rotational_offset*/ 0,
238 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
239 CTL_DEFAULT_ROTATION_RATE & 0xff},
243 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
244 /*page_code*/SMS_RIGID_DISK_PAGE,
245 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
246 /*cylinders*/ {0, 0, 0},
248 /*start_write_precomp*/ {0, 0, 0},
249 /*start_reduced_current*/ {0, 0, 0},
250 /*step_rate*/ {0, 0},
251 /*landing_zone_cylinder*/ {0, 0, 0},
253 /*rotational_offset*/ 0,
255 /*rotation_rate*/ {0, 0},
259 static struct scsi_caching_page caching_page_default = {
260 /*page_code*/SMS_CACHING_PAGE,
261 /*page_length*/sizeof(struct scsi_caching_page) - 2,
262 /*flags1*/ SCP_DISC | SCP_WCE,
264 /*disable_pf_transfer_len*/ {0xff, 0xff},
265 /*min_prefetch*/ {0, 0},
266 /*max_prefetch*/ {0xff, 0xff},
267 /*max_pf_ceiling*/ {0xff, 0xff},
269 /*cache_segments*/ 0,
270 /*cache_seg_size*/ {0, 0},
272 /*non_cache_seg_size*/ {0, 0, 0}
275 static struct scsi_caching_page caching_page_changeable = {
276 /*page_code*/SMS_CACHING_PAGE,
277 /*page_length*/sizeof(struct scsi_caching_page) - 2,
280 /*disable_pf_transfer_len*/ {0, 0},
281 /*min_prefetch*/ {0, 0},
282 /*max_prefetch*/ {0, 0},
283 /*max_pf_ceiling*/ {0, 0},
285 /*cache_segments*/ 0,
286 /*cache_seg_size*/ {0, 0},
288 /*non_cache_seg_size*/ {0, 0, 0}
291 static struct scsi_control_page control_page_default = {
292 /*page_code*/SMS_CONTROL_MODE_PAGE,
293 /*page_length*/sizeof(struct scsi_control_page) - 2,
298 /*aen_holdoff_period*/{0, 0}
301 static struct scsi_control_page control_page_changeable = {
302 /*page_code*/SMS_CONTROL_MODE_PAGE,
303 /*page_length*/sizeof(struct scsi_control_page) - 2,
308 /*aen_holdoff_period*/{0, 0}
311 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
314 * XXX KDM move these into the softc.
316 static int rcv_sync_msg;
317 static int persis_offset;
318 static uint8_t ctl_pause_rtr;
319 static int ctl_is_single;
320 static int index_to_aps_page;
324 * Serial number (0x80), device id (0x83), and supported pages (0x00)
326 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 3
328 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
330 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
331 static void ctl_init(void);
332 void ctl_shutdown(void);
333 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
334 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
335 static void ctl_ioctl_online(void *arg);
336 static void ctl_ioctl_offline(void *arg);
337 static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
338 static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
339 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
340 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
341 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
342 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
343 static int ctl_ioctl_submit_wait(union ctl_io *io);
344 static void ctl_ioctl_datamove(union ctl_io *io);
345 static void ctl_ioctl_done(union ctl_io *io);
346 static void ctl_ioctl_hard_startstop_callback(void *arg,
347 struct cfi_metatask *metatask);
348 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
349 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
350 struct ctl_ooa *ooa_hdr);
351 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
353 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
354 uint32_t ctl_port_idx(int port_num);
356 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
357 uint32_t targ_target, uint32_t targ_lun,
359 static void ctl_kfree_io(union ctl_io *io);
361 static void ctl_free_io_internal(union ctl_io *io, int have_lock);
362 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
363 struct ctl_be_lun *be_lun, struct ctl_id target_id);
364 static int ctl_free_lun(struct ctl_lun *lun);
365 static void ctl_create_lun(struct ctl_be_lun *be_lun);
367 static void ctl_failover_change_pages(struct ctl_softc *softc,
368 struct ctl_scsiio *ctsio, int master);
371 static int ctl_do_mode_select(union ctl_io *io);
372 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
373 uint64_t res_key, uint64_t sa_res_key,
374 uint8_t type, uint32_t residx,
375 struct ctl_scsiio *ctsio,
376 struct scsi_per_res_out *cdb,
377 struct scsi_per_res_out_parms* param);
378 static void ctl_pro_preempt_other(struct ctl_lun *lun,
379 union ctl_ha_msg *msg);
380 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
381 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
382 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
383 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
384 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
385 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
386 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
387 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
388 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
389 union ctl_io *ooa_io);
390 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
391 union ctl_io *starting_io);
392 static int ctl_check_blocked(struct ctl_lun *lun);
393 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
395 struct ctl_cmd_entry *entry,
396 struct ctl_scsiio *ctsio);
397 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
398 static void ctl_failover(void);
399 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
400 struct ctl_scsiio *ctsio);
401 static int ctl_scsiio(struct ctl_scsiio *ctsio);
403 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
404 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
405 ctl_ua_type ua_type);
406 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
407 ctl_ua_type ua_type);
408 static int ctl_abort_task(union ctl_io *io);
409 static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
411 static void ctl_datamove_timer_wakeup(void *arg);
412 static void ctl_done_timer_wakeup(void *arg);
413 #endif /* CTL_IO_DELAY */
415 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
416 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
417 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
418 static void ctl_datamove_remote_write(union ctl_io *io);
419 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
420 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
421 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
422 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
423 ctl_ha_dt_cb callback);
424 static void ctl_datamove_remote_read(union ctl_io *io);
425 static void ctl_datamove_remote(union ctl_io *io);
426 static int ctl_process_done(union ctl_io *io, int have_lock);
427 static void ctl_work_thread(void *arg);
430 * Load the serialization table. This isn't very pretty, but is probably
431 * the easiest way to do it.
433 #include "ctl_ser_table.c"
436 * We only need to define open, close and ioctl routines for this driver.
438 static struct cdevsw ctl_cdevsw = {
439 .d_version = D_VERSION,
442 .d_close = ctl_close,
443 .d_ioctl = ctl_ioctl,
448 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
451 * If we have the CAM SIM, we may or may not have another SIM that will
452 * cause CTL to get initialized. If not, we need to initialize it.
454 SYSINIT(ctl_init, SI_SUB_CONFIGURE, SI_ORDER_THIRD, ctl_init, NULL);
457 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
458 union ctl_ha_msg *msg_info)
460 struct ctl_scsiio *ctsio;
462 if (msg_info->hdr.original_sc == NULL) {
463 printf("%s: original_sc == NULL!\n", __func__);
464 /* XXX KDM now what? */
468 ctsio = &msg_info->hdr.original_sc->scsiio;
469 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
470 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
471 ctsio->io_hdr.status = msg_info->hdr.status;
472 ctsio->scsi_status = msg_info->scsi.scsi_status;
473 ctsio->sense_len = msg_info->scsi.sense_len;
474 ctsio->sense_residual = msg_info->scsi.sense_residual;
475 ctsio->residual = msg_info->scsi.residual;
476 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
477 sizeof(ctsio->sense_data));
478 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
479 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));;
480 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
485 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
486 union ctl_ha_msg *msg_info)
488 struct ctl_scsiio *ctsio;
490 if (msg_info->hdr.serializing_sc == NULL) {
491 printf("%s: serializing_sc == NULL!\n", __func__);
492 /* XXX KDM now what? */
496 ctsio = &msg_info->hdr.serializing_sc->scsiio;
499 * Attempt to catch the situation where an I/O has
500 * been freed, and we're using it again.
502 if (ctsio->io_hdr.io_type == 0xff) {
503 union ctl_io *tmp_io;
504 tmp_io = (union ctl_io *)ctsio;
505 printf("%s: %p use after free!\n", __func__,
507 printf("%s: type %d msg %d cdb %x iptl: "
508 "%d:%d:%d:%d tag 0x%04x "
509 "flag %#x status %x\n",
511 tmp_io->io_hdr.io_type,
512 tmp_io->io_hdr.msg_type,
513 tmp_io->scsiio.cdb[0],
514 tmp_io->io_hdr.nexus.initid.id,
515 tmp_io->io_hdr.nexus.targ_port,
516 tmp_io->io_hdr.nexus.targ_target.id,
517 tmp_io->io_hdr.nexus.targ_lun,
518 (tmp_io->io_hdr.io_type ==
520 tmp_io->taskio.tag_num :
521 tmp_io->scsiio.tag_num,
522 tmp_io->io_hdr.flags,
523 tmp_io->io_hdr.status);
526 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
527 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
532 * ISC (Inter Shelf Communication) event handler. Events from the HA
533 * subsystem come in here.
536 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
538 struct ctl_softc *ctl_softc;
540 struct ctl_prio *presio;
541 ctl_ha_status isc_status;
543 ctl_softc = control_softc;
548 printf("CTL: Isc Msg event %d\n", event);
550 if (event == CTL_HA_EVT_MSG_RECV) {
551 union ctl_ha_msg msg_info;
553 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
554 sizeof(msg_info), /*wait*/ 0);
556 printf("CTL: msg_type %d\n", msg_info.msg_type);
558 if (isc_status != 0) {
559 printf("Error receiving message, status = %d\n",
563 mtx_lock(&ctl_softc->ctl_lock);
565 switch (msg_info.hdr.msg_type) {
566 case CTL_MSG_SERIALIZE:
568 printf("Serialize\n");
570 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
572 printf("ctl_isc_event_handler: can't allocate "
575 /* Need to set busy and send msg back */
576 mtx_unlock(&ctl_softc->ctl_lock);
577 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
578 msg_info.hdr.status = CTL_SCSI_ERROR;
579 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
580 msg_info.scsi.sense_len = 0;
581 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
582 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
587 // populate ctsio from msg_info
588 io->io_hdr.io_type = CTL_IO_SCSI;
589 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
590 io->io_hdr.original_sc = msg_info.hdr.original_sc;
592 printf("pOrig %x\n", (int)msg_info.original_sc);
594 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
597 * If we're in serialization-only mode, we don't
598 * want to go through full done processing. Thus
601 * XXX KDM add another flag that is more specific.
603 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
604 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
605 io->io_hdr.nexus = msg_info.hdr.nexus;
607 printf("targ %d, port %d, iid %d, lun %d\n",
608 io->io_hdr.nexus.targ_target.id,
609 io->io_hdr.nexus.targ_port,
610 io->io_hdr.nexus.initid.id,
611 io->io_hdr.nexus.targ_lun);
613 io->scsiio.tag_num = msg_info.scsi.tag_num;
614 io->scsiio.tag_type = msg_info.scsi.tag_type;
615 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
617 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
618 struct ctl_cmd_entry *entry;
621 opcode = io->scsiio.cdb[0];
622 entry = &ctl_cmd_table[opcode];
623 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
625 entry->flags & CTL_FLAG_DATA_MASK;
627 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
632 /* Performed on the Originating SC, XFER mode only */
633 case CTL_MSG_DATAMOVE: {
634 struct ctl_sg_entry *sgl;
637 io = msg_info.hdr.original_sc;
639 printf("%s: original_sc == NULL!\n", __func__);
640 /* XXX KDM do something here */
643 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
644 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
646 * Keep track of this, we need to send it back over
647 * when the datamove is complete.
649 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
651 if (msg_info.dt.sg_sequence == 0) {
653 * XXX KDM we use the preallocated S/G list
654 * here, but we'll need to change this to
655 * dynamic allocation if we need larger S/G
658 if (msg_info.dt.kern_sg_entries >
659 sizeof(io->io_hdr.remote_sglist) /
660 sizeof(io->io_hdr.remote_sglist[0])) {
661 printf("%s: number of S/G entries "
662 "needed %u > allocated num %zd\n",
664 msg_info.dt.kern_sg_entries,
665 sizeof(io->io_hdr.remote_sglist)/
666 sizeof(io->io_hdr.remote_sglist[0]));
669 * XXX KDM send a message back to
670 * the other side to shut down the
671 * DMA. The error will come back
672 * through via the normal channel.
676 sgl = io->io_hdr.remote_sglist;
678 sizeof(io->io_hdr.remote_sglist));
680 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
682 io->scsiio.kern_sg_entries =
683 msg_info.dt.kern_sg_entries;
684 io->scsiio.rem_sg_entries =
685 msg_info.dt.kern_sg_entries;
686 io->scsiio.kern_data_len =
687 msg_info.dt.kern_data_len;
688 io->scsiio.kern_total_len =
689 msg_info.dt.kern_total_len;
690 io->scsiio.kern_data_resid =
691 msg_info.dt.kern_data_resid;
692 io->scsiio.kern_rel_offset =
693 msg_info.dt.kern_rel_offset;
695 * Clear out per-DMA flags.
697 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
699 * Add per-DMA flags that are set for this
700 * particular DMA request.
702 io->io_hdr.flags |= msg_info.dt.flags &
705 sgl = (struct ctl_sg_entry *)
706 io->scsiio.kern_data_ptr;
708 for (i = msg_info.dt.sent_sg_entries, j = 0;
709 i < (msg_info.dt.sent_sg_entries +
710 msg_info.dt.cur_sg_entries); i++, j++) {
711 sgl[i].addr = msg_info.dt.sg_list[j].addr;
712 sgl[i].len = msg_info.dt.sg_list[j].len;
715 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
717 msg_info.dt.sg_list[j].addr,
718 msg_info.dt.sg_list[j].len,
719 sgl[i].addr, sgl[i].len, j, i);
723 memcpy(&sgl[msg_info.dt.sent_sg_entries],
725 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
729 * If this is the last piece of the I/O, we've got
730 * the full S/G list. Queue processing in the thread.
731 * Otherwise wait for the next piece.
733 if (msg_info.dt.sg_last != 0) {
734 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
740 /* Performed on the Serializing (primary) SC, XFER mode only */
741 case CTL_MSG_DATAMOVE_DONE: {
742 if (msg_info.hdr.serializing_sc == NULL) {
743 printf("%s: serializing_sc == NULL!\n",
745 /* XXX KDM now what? */
749 * We grab the sense information here in case
750 * there was a failure, so we can return status
751 * back to the initiator.
753 io = msg_info.hdr.serializing_sc;
754 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
755 io->io_hdr.status = msg_info.hdr.status;
756 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
757 io->scsiio.sense_len = msg_info.scsi.sense_len;
758 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
759 io->io_hdr.port_status = msg_info.scsi.fetd_status;
760 io->scsiio.residual = msg_info.scsi.residual;
761 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
762 sizeof(io->scsiio.sense_data));
764 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
770 /* Preformed on Originating SC, SER_ONLY mode */
772 io = msg_info.hdr.original_sc;
774 printf("%s: Major Bummer\n", __func__);
775 mtx_unlock(&ctl_softc->ctl_lock);
779 printf("pOrig %x\n",(int) ctsio);
782 io->io_hdr.msg_type = CTL_MSG_R2R;
783 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
784 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
790 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
792 * Performed on the Originating (i.e. secondary) SC in XFER
795 case CTL_MSG_FINISH_IO:
796 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
797 ctl_isc_handler_finish_xfer(ctl_softc,
800 ctl_isc_handler_finish_ser_only(ctl_softc,
804 /* Preformed on Originating SC */
805 case CTL_MSG_BAD_JUJU:
806 io = msg_info.hdr.original_sc;
808 printf("%s: Bad JUJU!, original_sc is NULL!\n",
812 ctl_copy_sense_data(&msg_info, io);
814 * IO should have already been cleaned up on other
815 * SC so clear this flag so we won't send a message
816 * back to finish the IO there.
818 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
819 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
821 /* io = msg_info.hdr.serializing_sc; */
822 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
823 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
828 /* Handle resets sent from the other side */
829 case CTL_MSG_MANAGE_TASKS: {
830 struct ctl_taskio *taskio;
831 taskio = (struct ctl_taskio *)ctl_alloc_io(
832 (void *)ctl_softc->othersc_pool);
833 if (taskio == NULL) {
834 printf("ctl_isc_event_handler: can't allocate "
837 /* should I just call the proper reset func
839 mtx_unlock(&ctl_softc->ctl_lock);
842 ctl_zero_io((union ctl_io *)taskio);
843 taskio->io_hdr.io_type = CTL_IO_TASK;
844 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
845 taskio->io_hdr.nexus = msg_info.hdr.nexus;
846 taskio->task_action = msg_info.task.task_action;
847 taskio->tag_num = msg_info.task.tag_num;
848 taskio->tag_type = msg_info.task.tag_type;
850 taskio->io_hdr.start_time = time_uptime;
851 getbintime(&taskio->io_hdr.start_bt);
853 cs_prof_gettime(&taskio->io_hdr.start_ticks);
855 #endif /* CTL_TIME_IO */
856 STAILQ_INSERT_TAIL(&ctl_softc->task_queue,
857 &taskio->io_hdr, links);
858 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
862 /* Persistent Reserve action which needs attention */
863 case CTL_MSG_PERS_ACTION:
864 presio = (struct ctl_prio *)ctl_alloc_io(
865 (void *)ctl_softc->othersc_pool);
866 if (presio == NULL) {
867 printf("ctl_isc_event_handler: can't allocate "
870 /* Need to set busy and send msg back */
871 mtx_unlock(&ctl_softc->ctl_lock);
874 ctl_zero_io((union ctl_io *)presio);
875 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
876 presio->pr_msg = msg_info.pr;
877 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
878 &presio->io_hdr, links);
881 case CTL_MSG_SYNC_FE:
884 case CTL_MSG_APS_LOCK: {
885 // It's quicker to execute this then to
888 struct ctl_page_index *page_index;
889 struct copan_aps_subpage *current_sp;
891 lun = ctl_softc->ctl_luns[msg_info.hdr.nexus.targ_lun];
892 page_index = &lun->mode_pages.index[index_to_aps_page];
893 current_sp = (struct copan_aps_subpage *)
894 (page_index->page_data +
895 (page_index->page_len * CTL_PAGE_CURRENT));
897 current_sp->lock_active = msg_info.aps.lock_flag;
901 printf("How did I get here?\n");
903 mtx_unlock(&ctl_softc->ctl_lock);
904 } else if (event == CTL_HA_EVT_MSG_SENT) {
905 if (param != CTL_HA_STATUS_SUCCESS) {
906 printf("Bad status from ctl_ha_msg_send status %d\n",
910 } else if (event == CTL_HA_EVT_DISCONNECT) {
911 printf("CTL: Got a disconnect from Isc\n");
914 printf("ctl_isc_event_handler: Unknown event %d\n", event);
923 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
925 struct scsi_sense_data *sense;
927 sense = &dest->scsiio.sense_data;
928 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
929 dest->scsiio.scsi_status = src->scsi.scsi_status;
930 dest->scsiio.sense_len = src->scsi.sense_len;
931 dest->io_hdr.status = src->hdr.status;
937 struct ctl_softc *softc;
938 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
939 struct ctl_frontend *fe;
952 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, M_WAITOK);
953 softc = control_softc;
955 memset(softc, 0, sizeof(*softc));
957 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
960 softc->dev->si_drv1 = softc;
962 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
963 softc->open_count = 0;
966 * Default to actually sending a SYNCHRONIZE CACHE command down to
969 softc->flags = CTL_FLAG_REAL_SYNC;
972 * In Copan's HA scheme, the "master" and "slave" roles are
973 * figured out through the slot the controller is in. Although it
974 * is an active/active system, someone has to be in charge.
977 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
981 softc->flags |= CTL_FLAG_MASTER_SHELF;
984 persis_offset = CTL_MAX_INITIATORS;
987 * XXX KDM need to figure out where we want to get our target ID
988 * and WWID. Is it different on each port?
990 softc->target.id = 0;
991 softc->target.wwid[0] = 0x12345678;
992 softc->target.wwid[1] = 0x87654321;
993 STAILQ_INIT(&softc->lun_list);
994 STAILQ_INIT(&softc->pending_lun_queue);
995 STAILQ_INIT(&softc->task_queue);
996 STAILQ_INIT(&softc->incoming_queue);
997 STAILQ_INIT(&softc->rtr_queue);
998 STAILQ_INIT(&softc->done_queue);
999 STAILQ_INIT(&softc->isc_queue);
1000 STAILQ_INIT(&softc->fe_list);
1001 STAILQ_INIT(&softc->be_list);
1002 STAILQ_INIT(&softc->io_pools);
1007 * We don't bother calling these with ctl_lock held here, because,
1008 * in theory, no one else can try to do anything while we're in our
1009 * module init routine.
1011 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1012 &internal_pool)!= 0){
1013 printf("ctl: can't allocate %d entry internal pool, "
1014 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1018 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1019 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1020 printf("ctl: can't allocate %d entry emergency pool, "
1021 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1022 ctl_pool_free(softc, internal_pool);
1026 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1029 printf("ctl: can't allocate %d entry other SC pool, "
1030 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1031 ctl_pool_free(softc, internal_pool);
1032 ctl_pool_free(softc, emergency_pool);
1036 softc->internal_pool = internal_pool;
1037 softc->emergency_pool = emergency_pool;
1038 softc->othersc_pool = other_pool;
1040 ctl_pool_acquire(internal_pool);
1041 ctl_pool_acquire(emergency_pool);
1042 ctl_pool_acquire(other_pool);
1045 * We used to allocate a processor LUN here. The new scheme is to
1046 * just let the user allocate LUNs as he sees fit.
1049 mtx_lock(&softc->ctl_lock);
1050 ctl_alloc_lun(softc, lun, /*be_lun*/NULL, /*target*/softc->target);
1051 mtx_unlock(&softc->ctl_lock);
1054 if (kproc_create(ctl_work_thread, softc, &softc->work_thread, 0, 0,
1056 printf("error creating CTL work thread!\n");
1058 ctl_pool_free(softc, internal_pool);
1059 ctl_pool_free(softc, emergency_pool);
1060 ctl_pool_free(softc, other_pool);
1063 printf("ctl: CAM Target Layer loaded\n");
1066 * Initialize the initiator and portname mappings
1068 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1071 * Initialize the ioctl front end.
1073 fe = &softc->ioctl_info.fe;
1074 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1075 fe->port_type = CTL_PORT_IOCTL;
1076 fe->num_requested_ctl_io = 100;
1077 fe->port_name = softc->ioctl_info.port_name;
1078 fe->port_online = ctl_ioctl_online;
1079 fe->port_offline = ctl_ioctl_offline;
1080 fe->onoff_arg = &softc->ioctl_info;
1081 fe->targ_enable = ctl_ioctl_targ_enable;
1082 fe->targ_disable = ctl_ioctl_targ_disable;
1083 fe->lun_enable = ctl_ioctl_lun_enable;
1084 fe->lun_disable = ctl_ioctl_lun_disable;
1085 fe->targ_lun_arg = &softc->ioctl_info;
1086 fe->fe_datamove = ctl_ioctl_datamove;
1087 fe->fe_done = ctl_ioctl_done;
1088 fe->max_targets = 15;
1089 fe->max_target_id = 15;
1091 if (ctl_frontend_register(&softc->ioctl_info.fe,
1092 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1093 printf("ctl: ioctl front end registration failed, will "
1094 "continue anyway\n");
1098 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1099 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1100 sizeof(struct callout), CTL_TIMER_BYTES);
1103 #endif /* CTL_IO_DELAY */
1110 struct ctl_softc *softc;
1111 struct ctl_lun *lun, *next_lun;
1112 struct ctl_io_pool *pool, *next_pool;
1114 softc = (struct ctl_softc *)control_softc;
1116 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1117 printf("ctl: ioctl front end deregistration failed\n");
1119 mtx_lock(&softc->ctl_lock);
1124 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1125 next_lun = STAILQ_NEXT(lun, links);
1130 * This will rip the rug out from under any FETDs or anyone else
1131 * that has a pool allocated. Since we increment our module
1132 * refcount any time someone outside the main CTL module allocates
1133 * a pool, we shouldn't have any problems here. The user won't be
1134 * able to unload the CTL module until client modules have
1135 * successfully unloaded.
1137 for (pool = STAILQ_FIRST(&softc->io_pools); pool != NULL;
1139 next_pool = STAILQ_NEXT(pool, links);
1140 ctl_pool_free(softc, pool);
1143 mtx_unlock(&softc->ctl_lock);
1146 ctl_shutdown_thread(softc->work_thread);
1149 mtx_destroy(&softc->ctl_lock);
1151 destroy_dev(softc->dev);
1153 printf("ctl: CAM Target Layer unloaded\n");
1157 * XXX KDM should we do some access checks here? Bump a reference count to
1158 * prevent a CTL module from being unloaded while someone has it open?
1161 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1167 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1173 ctl_port_enable(ctl_port_type port_type)
1175 struct ctl_softc *softc;
1176 struct ctl_frontend *fe;
1178 if (ctl_is_single == 0) {
1179 union ctl_ha_msg msg_info;
1183 printf("%s: HA mode, synchronizing frontend enable\n",
1186 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1187 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1188 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1189 printf("Sync msg send error retval %d\n", isc_retval);
1191 if (!rcv_sync_msg) {
1192 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1193 sizeof(msg_info), 1);
1196 printf("CTL:Frontend Enable\n");
1198 printf("%s: single mode, skipping frontend synchronization\n",
1203 softc = control_softc;
1205 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1206 if (port_type & fe->port_type)
1209 printf("port %d\n", fe->targ_port);
1211 ctl_frontend_online(fe);
1219 ctl_port_disable(ctl_port_type port_type)
1221 struct ctl_softc *softc;
1222 struct ctl_frontend *fe;
1224 softc = control_softc;
1226 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1227 if (port_type & fe->port_type)
1228 ctl_frontend_offline(fe);
1235 * Returns 0 for success, 1 for failure.
1236 * Currently the only failure mode is if there aren't enough entries
1237 * allocated. So, in case of a failure, look at num_entries_dropped,
1238 * reallocate and try again.
1241 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1242 int *num_entries_filled, int *num_entries_dropped,
1243 ctl_port_type port_type, int no_virtual)
1245 struct ctl_softc *softc;
1246 struct ctl_frontend *fe;
1247 int entries_dropped, entries_filled;
1251 softc = control_softc;
1255 entries_dropped = 0;
1258 mtx_lock(&softc->ctl_lock);
1259 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1260 struct ctl_port_entry *entry;
1262 if ((fe->port_type & port_type) == 0)
1265 if ((no_virtual != 0)
1266 && (fe->virtual_port != 0))
1269 if (entries_filled >= num_entries_alloced) {
1273 entry = &entries[i];
1275 entry->port_type = fe->port_type;
1276 strlcpy(entry->port_name, fe->port_name,
1277 sizeof(entry->port_name));
1278 entry->physical_port = fe->physical_port;
1279 entry->virtual_port = fe->virtual_port;
1280 entry->wwnn = fe->wwnn;
1281 entry->wwpn = fe->wwpn;
1287 mtx_unlock(&softc->ctl_lock);
1289 if (entries_dropped > 0)
1292 *num_entries_dropped = entries_dropped;
1293 *num_entries_filled = entries_filled;
1299 ctl_ioctl_online(void *arg)
1301 struct ctl_ioctl_info *ioctl_info;
1303 ioctl_info = (struct ctl_ioctl_info *)arg;
1305 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1309 ctl_ioctl_offline(void *arg)
1311 struct ctl_ioctl_info *ioctl_info;
1313 ioctl_info = (struct ctl_ioctl_info *)arg;
1315 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1319 * Remove an initiator by port number and initiator ID.
1320 * Returns 0 for success, 1 for failure.
1321 * Assumes the caller does NOT hold the CTL lock.
1324 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1326 struct ctl_softc *softc;
1328 softc = control_softc;
1331 || (targ_port > CTL_MAX_PORTS)) {
1332 printf("%s: invalid port number %d\n", __func__, targ_port);
1335 if (iid > CTL_MAX_INIT_PER_PORT) {
1336 printf("%s: initiator ID %u > maximun %u!\n",
1337 __func__, iid, CTL_MAX_INIT_PER_PORT);
1341 mtx_lock(&softc->ctl_lock);
1343 softc->wwpn_iid[targ_port][iid].in_use = 0;
1345 mtx_unlock(&softc->ctl_lock);
1351 * Add an initiator to the initiator map.
1352 * Returns 0 for success, 1 for failure.
1353 * Assumes the caller does NOT hold the CTL lock.
1356 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1358 struct ctl_softc *softc;
1361 softc = control_softc;
1366 || (targ_port > CTL_MAX_PORTS)) {
1367 printf("%s: invalid port number %d\n", __func__, targ_port);
1370 if (iid > CTL_MAX_INIT_PER_PORT) {
1371 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1372 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1376 mtx_lock(&softc->ctl_lock);
1378 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1380 * We don't treat this as an error.
1382 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1383 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1384 __func__, targ_port, iid, (uintmax_t)wwpn);
1389 * This is an error, but what do we do about it? The
1390 * driver is telling us we have a new WWPN for this
1391 * initiator ID, so we pretty much need to use it.
1393 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1394 "still at that address\n", __func__, targ_port, iid,
1396 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1399 * XXX KDM clear have_ca and ua_pending on each LUN for
1403 softc->wwpn_iid[targ_port][iid].in_use = 1;
1404 softc->wwpn_iid[targ_port][iid].iid = iid;
1405 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1406 softc->wwpn_iid[targ_port][iid].port = targ_port;
1410 mtx_unlock(&softc->ctl_lock);
1416 * XXX KDM should we pretend to do something in the target/lun
1417 * enable/disable functions?
1420 ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1426 ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1432 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1438 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1444 * Data movement routine for the CTL ioctl frontend port.
1447 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1449 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1450 struct ctl_sg_entry ext_entry, kern_entry;
1451 int ext_sglen, ext_sg_entries, kern_sg_entries;
1452 int ext_sg_start, ext_offset;
1453 int len_to_copy, len_copied;
1454 int kern_watermark, ext_watermark;
1455 int ext_sglist_malloced;
1458 ext_sglist_malloced = 0;
1462 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1465 * If this flag is set, fake the data transfer.
1467 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1468 ctsio->ext_data_filled = ctsio->ext_data_len;
1473 * To simplify things here, if we have a single buffer, stick it in
1474 * a S/G entry and just make it a single entry S/G list.
1476 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1479 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1481 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1483 if (ext_sglist == NULL) {
1484 ctl_set_internal_failure(ctsio,
1487 return (CTL_RETVAL_COMPLETE);
1489 ext_sglist_malloced = 1;
1490 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1492 ctl_set_internal_failure(ctsio,
1497 ext_sg_entries = ctsio->ext_sg_entries;
1499 for (i = 0; i < ext_sg_entries; i++) {
1500 if ((len_seen + ext_sglist[i].len) >=
1501 ctsio->ext_data_filled) {
1503 ext_offset = ctsio->ext_data_filled - len_seen;
1506 len_seen += ext_sglist[i].len;
1509 ext_sglist = &ext_entry;
1510 ext_sglist->addr = ctsio->ext_data_ptr;
1511 ext_sglist->len = ctsio->ext_data_len;
1514 ext_offset = ctsio->ext_data_filled;
1517 if (ctsio->kern_sg_entries > 0) {
1518 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1519 kern_sg_entries = ctsio->kern_sg_entries;
1521 kern_sglist = &kern_entry;
1522 kern_sglist->addr = ctsio->kern_data_ptr;
1523 kern_sglist->len = ctsio->kern_data_len;
1524 kern_sg_entries = 1;
1529 ext_watermark = ext_offset;
1531 for (i = ext_sg_start, j = 0;
1532 i < ext_sg_entries && j < kern_sg_entries;) {
1533 uint8_t *ext_ptr, *kern_ptr;
1535 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1536 kern_sglist[j].len - kern_watermark);
1538 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1539 ext_ptr = ext_ptr + ext_watermark;
1540 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1544 panic("need to implement bus address support");
1546 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1549 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1550 kern_ptr = kern_ptr + kern_watermark;
1552 kern_watermark += len_to_copy;
1553 ext_watermark += len_to_copy;
1555 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1557 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1558 "bytes to user\n", len_to_copy));
1559 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1560 "to %p\n", kern_ptr, ext_ptr));
1561 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1562 ctl_set_internal_failure(ctsio,
1568 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1569 "bytes from user\n", len_to_copy));
1570 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1571 "to %p\n", ext_ptr, kern_ptr));
1572 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1573 ctl_set_internal_failure(ctsio,
1580 len_copied += len_to_copy;
1582 if (ext_sglist[i].len == ext_watermark) {
1587 if (kern_sglist[j].len == kern_watermark) {
1593 ctsio->ext_data_filled += len_copied;
1595 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1596 "kern_sg_entries: %d\n", ext_sg_entries,
1598 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1599 "kern_data_len = %d\n", ctsio->ext_data_len,
1600 ctsio->kern_data_len));
1603 /* XXX KDM set residual?? */
1606 if (ext_sglist_malloced != 0)
1607 free(ext_sglist, M_CTL);
1609 return (CTL_RETVAL_COMPLETE);
1613 * Serialize a command that went down the "wrong" side, and so was sent to
1614 * this controller for execution. The logic is a little different than the
1615 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1616 * sent back to the other side, but in the success case, we execute the
1617 * command on this side (XFER mode) or tell the other side to execute it
1621 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1623 struct ctl_softc *ctl_softc;
1624 union ctl_ha_msg msg_info;
1625 struct ctl_lun *lun;
1628 ctl_softc = control_softc;
1630 mtx_lock(&ctl_softc->ctl_lock);
1632 lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun];
1636 * Why isn't LUN defined? The other side wouldn't
1637 * send a cmd if the LUN is undefined.
1639 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1641 /* "Logical unit not supported" */
1642 ctl_set_sense_data(&msg_info.scsi.sense_data,
1644 /*sense_format*/SSD_TYPE_NONE,
1645 /*current_error*/ 1,
1646 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1651 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1652 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1653 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1654 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1655 msg_info.hdr.serializing_sc = NULL;
1656 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1657 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1658 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1661 mtx_unlock(&ctl_softc->ctl_lock);
1666 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1668 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1669 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1671 case CTL_ACTION_BLOCK:
1672 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1673 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1676 case CTL_ACTION_PASS:
1677 case CTL_ACTION_SKIP:
1678 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1679 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1680 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1681 &ctsio->io_hdr, links);
1684 /* send msg back to other side */
1685 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1686 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1687 msg_info.hdr.msg_type = CTL_MSG_R2R;
1689 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1691 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1692 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1696 case CTL_ACTION_OVERLAP:
1697 /* OVERLAPPED COMMANDS ATTEMPTED */
1698 ctl_set_sense_data(&msg_info.scsi.sense_data,
1700 /*sense_format*/SSD_TYPE_NONE,
1701 /*current_error*/ 1,
1702 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1707 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1708 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1709 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1710 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1711 msg_info.hdr.serializing_sc = NULL;
1712 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1714 printf("BAD JUJU:Major Bummer Overlap\n");
1716 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1718 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1719 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1722 case CTL_ACTION_OVERLAP_TAG:
1723 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1724 ctl_set_sense_data(&msg_info.scsi.sense_data,
1726 /*sense_format*/SSD_TYPE_NONE,
1727 /*current_error*/ 1,
1728 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1730 /*ascq*/ ctsio->tag_num & 0xff,
1733 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1734 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1735 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1736 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1737 msg_info.hdr.serializing_sc = NULL;
1738 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1740 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1742 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1744 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1745 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1748 case CTL_ACTION_ERROR:
1750 /* "Internal target failure" */
1751 ctl_set_sense_data(&msg_info.scsi.sense_data,
1753 /*sense_format*/SSD_TYPE_NONE,
1754 /*current_error*/ 1,
1755 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1760 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1761 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1762 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1763 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1764 msg_info.hdr.serializing_sc = NULL;
1765 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1767 printf("BAD JUJU:Major Bummer HW Error\n");
1769 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1771 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1772 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1777 mtx_unlock(&ctl_softc->ctl_lock);
1782 ctl_ioctl_submit_wait(union ctl_io *io)
1784 struct ctl_fe_ioctl_params params;
1785 ctl_fe_ioctl_state last_state;
1790 bzero(¶ms, sizeof(params));
1792 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1793 cv_init(¶ms.sem, "ctlioccv");
1794 params.state = CTL_IOCTL_INPROG;
1795 last_state = params.state;
1797 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1799 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1801 /* This shouldn't happen */
1802 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1808 mtx_lock(¶ms.ioctl_mtx);
1810 * Check the state here, and don't sleep if the state has
1811 * already changed (i.e. wakeup has already occured, but we
1812 * weren't waiting yet).
1814 if (params.state == last_state) {
1815 /* XXX KDM cv_wait_sig instead? */
1816 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1818 last_state = params.state;
1820 switch (params.state) {
1821 case CTL_IOCTL_INPROG:
1822 /* Why did we wake up? */
1823 /* XXX KDM error here? */
1824 mtx_unlock(¶ms.ioctl_mtx);
1826 case CTL_IOCTL_DATAMOVE:
1827 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1830 * change last_state back to INPROG to avoid
1831 * deadlock on subsequent data moves.
1833 params.state = last_state = CTL_IOCTL_INPROG;
1835 mtx_unlock(¶ms.ioctl_mtx);
1836 ctl_ioctl_do_datamove(&io->scsiio);
1838 * Note that in some cases, most notably writes,
1839 * this will queue the I/O and call us back later.
1840 * In other cases, generally reads, this routine
1841 * will immediately call back and wake us up,
1842 * probably using our own context.
1844 io->scsiio.be_move_done(io);
1846 case CTL_IOCTL_DONE:
1847 mtx_unlock(¶ms.ioctl_mtx);
1848 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1852 mtx_unlock(¶ms.ioctl_mtx);
1853 /* XXX KDM error here? */
1856 } while (done == 0);
1858 mtx_destroy(¶ms.ioctl_mtx);
1859 cv_destroy(¶ms.sem);
1861 return (CTL_RETVAL_COMPLETE);
1865 ctl_ioctl_datamove(union ctl_io *io)
1867 struct ctl_fe_ioctl_params *params;
1869 params = (struct ctl_fe_ioctl_params *)
1870 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1872 mtx_lock(¶ms->ioctl_mtx);
1873 params->state = CTL_IOCTL_DATAMOVE;
1874 cv_broadcast(¶ms->sem);
1875 mtx_unlock(¶ms->ioctl_mtx);
1879 ctl_ioctl_done(union ctl_io *io)
1881 struct ctl_fe_ioctl_params *params;
1883 params = (struct ctl_fe_ioctl_params *)
1884 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1886 mtx_lock(¶ms->ioctl_mtx);
1887 params->state = CTL_IOCTL_DONE;
1888 cv_broadcast(¶ms->sem);
1889 mtx_unlock(¶ms->ioctl_mtx);
1893 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1895 struct ctl_fe_ioctl_startstop_info *sd_info;
1897 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1899 sd_info->hs_info.status = metatask->status;
1900 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1901 sd_info->hs_info.luns_complete =
1902 metatask->taskinfo.startstop.luns_complete;
1903 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1905 cv_broadcast(&sd_info->sem);
1909 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1911 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1913 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1915 mtx_lock(fe_bbr_info->lock);
1916 fe_bbr_info->bbr_info->status = metatask->status;
1917 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1918 fe_bbr_info->wakeup_done = 1;
1919 mtx_unlock(fe_bbr_info->lock);
1921 cv_broadcast(&fe_bbr_info->sem);
1925 * Must be called with the ctl_lock held.
1926 * Returns 0 for success, errno for failure.
1929 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1930 struct ctl_ooa *ooa_hdr)
1937 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1938 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1940 struct ctl_ooa_entry *cur_entry, entry;
1943 * If we've got more than we can fit, just count the
1944 * remaining entries.
1946 if (*cur_fill_num >= ooa_hdr->alloc_num)
1949 cur_entry = &ooa_hdr->entries[*cur_fill_num];
1951 bzero(&entry, sizeof(entry));
1953 entry.tag_num = io->scsiio.tag_num;
1954 entry.lun_num = lun->lun;
1956 entry.start_bt = io->io_hdr.start_bt;
1958 bcopy(io->scsiio.cdb, entry.cdb, io->scsiio.cdb_len);
1959 entry.cdb_len = io->scsiio.cdb_len;
1960 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1961 entry.cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1963 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1964 entry.cmd_flags |= CTL_OOACMD_FLAG_DMA;
1966 if (io->io_hdr.flags & CTL_FLAG_ABORT)
1967 entry.cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1969 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1970 entry.cmd_flags |= CTL_OOACMD_FLAG_RTR;
1972 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1973 entry.cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
1975 retval = copyout(&entry, cur_entry, sizeof(entry));
1985 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
1986 size_t error_str_len)
1990 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
1992 snprintf(error_str, error_str_len, "Cannot allocate %d bytes",
1997 if (copyin(user_addr, kptr, len) != 0) {
1998 snprintf(error_str, error_str_len, "Error copying %d bytes "
1999 "from user address %p to kernel address %p", len,
2009 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2013 if (be_args == NULL)
2016 for (i = 0; i < num_be_args; i++) {
2017 free(be_args[i].kname, M_CTL);
2018 free(be_args[i].kvalue, M_CTL);
2021 free(be_args, M_CTL);
2024 static struct ctl_be_arg *
2025 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2026 char *error_str, size_t error_str_len)
2028 struct ctl_be_arg *args;
2031 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2032 error_str, error_str_len);
2037 for (i = 0; i < num_be_args; i++) {
2040 args[i].kname = ctl_copyin_alloc(args[i].name,
2041 args[i].namelen, error_str, error_str_len);
2042 if (args[i].kname == NULL)
2045 if (args[i].kname[args[i].namelen - 1] != '\0') {
2046 snprintf(error_str, error_str_len, "Argument %d "
2047 "name is not NUL-terminated", i);
2051 args[i].kvalue = NULL;
2053 tmpptr = ctl_copyin_alloc(args[i].value,
2054 args[i].vallen, error_str, error_str_len);
2058 args[i].kvalue = tmpptr;
2060 if ((args[i].flags & CTL_BEARG_ASCII)
2061 && (tmpptr[args[i].vallen - 1] != '\0')) {
2062 snprintf(error_str, error_str_len, "Argument %d "
2063 "value is not NUL-terminated", i);
2071 ctl_free_args(num_be_args, args);
2077 * Escape characters that are illegal or not recommended in XML.
2080 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2086 for (; *str; str++) {
2089 retval = sbuf_printf(sb, "&");
2092 retval = sbuf_printf(sb, ">");
2095 retval = sbuf_printf(sb, "<");
2098 retval = sbuf_putc(sb, *str);
2111 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2114 struct ctl_softc *softc;
2117 softc = control_softc;
2127 * If we haven't been "enabled", don't allow any SCSI I/O
2130 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2135 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2137 printf("ctl_ioctl: can't allocate ctl_io!\n");
2143 * Need to save the pool reference so it doesn't get
2144 * spammed by the user's ctl_io.
2146 pool_tmp = io->io_hdr.pool;
2148 memcpy(io, (void *)addr, sizeof(*io));
2150 io->io_hdr.pool = pool_tmp;
2152 * No status yet, so make sure the status is set properly.
2154 io->io_hdr.status = CTL_STATUS_NONE;
2157 * The user sets the initiator ID, target and LUN IDs.
2159 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2160 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2161 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2162 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2163 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2165 retval = ctl_ioctl_submit_wait(io);
2172 memcpy((void *)addr, io, sizeof(*io));
2174 /* return this to our pool */
2179 case CTL_ENABLE_PORT:
2180 case CTL_DISABLE_PORT:
2181 case CTL_SET_PORT_WWNS: {
2182 struct ctl_frontend *fe;
2183 struct ctl_port_entry *entry;
2185 entry = (struct ctl_port_entry *)addr;
2187 mtx_lock(&softc->ctl_lock);
2188 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2194 if ((entry->port_type == CTL_PORT_NONE)
2195 && (entry->targ_port == fe->targ_port)) {
2197 * If the user only wants to enable or
2198 * disable or set WWNs on a specific port,
2199 * do the operation and we're done.
2203 } else if (entry->port_type & fe->port_type) {
2205 * Compare the user's type mask with the
2206 * particular frontend type to see if we
2213 * Make sure the user isn't trying to set
2214 * WWNs on multiple ports at the same time.
2216 if (cmd == CTL_SET_PORT_WWNS) {
2217 printf("%s: Can't set WWNs on "
2218 "multiple ports\n", __func__);
2225 * XXX KDM we have to drop the lock here,
2226 * because the online/offline operations
2227 * can potentially block. We need to
2228 * reference count the frontends so they
2231 mtx_unlock(&softc->ctl_lock);
2233 if (cmd == CTL_ENABLE_PORT)
2234 ctl_frontend_online(fe);
2235 else if (cmd == CTL_DISABLE_PORT)
2236 ctl_frontend_offline(fe);
2238 mtx_lock(&softc->ctl_lock);
2240 if (cmd == CTL_SET_PORT_WWNS)
2241 ctl_frontend_set_wwns(fe,
2242 (entry->flags & CTL_PORT_WWNN_VALID) ?
2244 (entry->flags & CTL_PORT_WWPN_VALID) ?
2245 1 : 0, entry->wwpn);
2250 mtx_unlock(&softc->ctl_lock);
2253 case CTL_GET_PORT_LIST: {
2254 struct ctl_frontend *fe;
2255 struct ctl_port_list *list;
2258 list = (struct ctl_port_list *)addr;
2260 if (list->alloc_len != (list->alloc_num *
2261 sizeof(struct ctl_port_entry))) {
2262 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2263 "alloc_num %u * sizeof(struct ctl_port_entry) "
2264 "%zu\n", __func__, list->alloc_len,
2265 list->alloc_num, sizeof(struct ctl_port_entry));
2271 list->dropped_num = 0;
2273 mtx_lock(&softc->ctl_lock);
2274 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2275 struct ctl_port_entry entry, *list_entry;
2277 if (list->fill_num >= list->alloc_num) {
2278 list->dropped_num++;
2282 entry.port_type = fe->port_type;
2283 strlcpy(entry.port_name, fe->port_name,
2284 sizeof(entry.port_name));
2285 entry.targ_port = fe->targ_port;
2286 entry.physical_port = fe->physical_port;
2287 entry.virtual_port = fe->virtual_port;
2288 entry.wwnn = fe->wwnn;
2289 entry.wwpn = fe->wwpn;
2290 if (fe->status & CTL_PORT_STATUS_ONLINE)
2295 list_entry = &list->entries[i];
2297 retval = copyout(&entry, list_entry, sizeof(entry));
2299 printf("%s: CTL_GET_PORT_LIST: copyout "
2300 "returned %d\n", __func__, retval);
2305 list->fill_len += sizeof(entry);
2307 mtx_unlock(&softc->ctl_lock);
2310 * If this is non-zero, we had a copyout fault, so there's
2311 * probably no point in attempting to set the status inside
2317 if (list->dropped_num > 0)
2318 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2320 list->status = CTL_PORT_LIST_OK;
2323 case CTL_DUMP_OOA: {
2324 struct ctl_lun *lun;
2329 mtx_lock(&softc->ctl_lock);
2330 printf("Dumping OOA queues:\n");
2331 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2332 for (io = (union ctl_io *)TAILQ_FIRST(
2333 &lun->ooa_queue); io != NULL;
2334 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2336 sbuf_new(&sb, printbuf, sizeof(printbuf),
2338 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2342 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2344 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2346 CTL_FLAG_ABORT) ? " ABORT" : "",
2348 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2349 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2351 printf("%s\n", sbuf_data(&sb));
2354 printf("OOA queues dump done\n");
2355 mtx_unlock(&softc->ctl_lock);
2359 struct ctl_lun *lun;
2360 struct ctl_ooa *ooa_hdr;
2361 uint32_t cur_fill_num;
2363 ooa_hdr = (struct ctl_ooa *)addr;
2365 if ((ooa_hdr->alloc_len == 0)
2366 || (ooa_hdr->alloc_num == 0)) {
2367 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2368 "must be non-zero\n", __func__,
2369 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2374 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2375 sizeof(struct ctl_ooa_entry))) {
2376 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2377 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2378 __func__, ooa_hdr->alloc_len,
2379 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2384 mtx_lock(&softc->ctl_lock);
2385 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2386 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2387 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2388 mtx_unlock(&softc->ctl_lock);
2389 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2390 __func__, (uintmax_t)ooa_hdr->lun_num);
2397 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2398 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2399 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2405 mtx_unlock(&softc->ctl_lock);
2409 lun = softc->ctl_luns[ooa_hdr->lun_num];
2411 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr);
2413 mtx_unlock(&softc->ctl_lock);
2415 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2416 ooa_hdr->fill_len = ooa_hdr->fill_num *
2417 sizeof(struct ctl_ooa_entry);
2419 getbintime(&ooa_hdr->cur_bt);
2421 if (cur_fill_num > ooa_hdr->alloc_num) {
2422 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2423 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2425 ooa_hdr->dropped_num = 0;
2426 ooa_hdr->status = CTL_OOA_OK;
2430 case CTL_CHECK_OOA: {
2432 struct ctl_lun *lun;
2433 struct ctl_ooa_info *ooa_info;
2436 ooa_info = (struct ctl_ooa_info *)addr;
2438 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2439 ooa_info->status = CTL_OOA_INVALID_LUN;
2442 mtx_lock(&softc->ctl_lock);
2443 lun = softc->ctl_luns[ooa_info->lun_id];
2445 mtx_unlock(&softc->ctl_lock);
2446 ooa_info->status = CTL_OOA_INVALID_LUN;
2450 ooa_info->num_entries = 0;
2451 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2452 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2453 &io->io_hdr, ooa_links)) {
2454 ooa_info->num_entries++;
2457 mtx_unlock(&softc->ctl_lock);
2458 ooa_info->status = CTL_OOA_SUCCESS;
2462 case CTL_HARD_START:
2463 case CTL_HARD_STOP: {
2464 struct ctl_fe_ioctl_startstop_info ss_info;
2465 struct cfi_metatask *metatask;
2468 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2470 cv_init(&ss_info.sem, "hard start/stop cv" );
2472 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2473 if (metatask == NULL) {
2475 mtx_destroy(&hs_mtx);
2479 if (cmd == CTL_HARD_START)
2480 metatask->tasktype = CFI_TASK_STARTUP;
2482 metatask->tasktype = CFI_TASK_SHUTDOWN;
2484 metatask->callback = ctl_ioctl_hard_startstop_callback;
2485 metatask->callback_arg = &ss_info;
2487 cfi_action(metatask);
2489 /* Wait for the callback */
2491 cv_wait_sig(&ss_info.sem, &hs_mtx);
2492 mtx_unlock(&hs_mtx);
2495 * All information has been copied from the metatask by the
2496 * time cv_broadcast() is called, so we free the metatask here.
2498 cfi_free_metatask(metatask);
2500 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2502 mtx_destroy(&hs_mtx);
2506 struct ctl_bbrread_info *bbr_info;
2507 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2509 struct cfi_metatask *metatask;
2511 bbr_info = (struct ctl_bbrread_info *)addr;
2513 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2515 bzero(&bbr_mtx, sizeof(bbr_mtx));
2516 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2518 fe_bbr_info.bbr_info = bbr_info;
2519 fe_bbr_info.lock = &bbr_mtx;
2521 cv_init(&fe_bbr_info.sem, "BBR read cv");
2522 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2524 if (metatask == NULL) {
2525 mtx_destroy(&bbr_mtx);
2526 cv_destroy(&fe_bbr_info.sem);
2530 metatask->tasktype = CFI_TASK_BBRREAD;
2531 metatask->callback = ctl_ioctl_bbrread_callback;
2532 metatask->callback_arg = &fe_bbr_info;
2533 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2534 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2535 metatask->taskinfo.bbrread.len = bbr_info->len;
2537 cfi_action(metatask);
2540 while (fe_bbr_info.wakeup_done == 0)
2541 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2542 mtx_unlock(&bbr_mtx);
2544 bbr_info->status = metatask->status;
2545 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2546 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2547 memcpy(&bbr_info->sense_data,
2548 &metatask->taskinfo.bbrread.sense_data,
2549 ctl_min(sizeof(bbr_info->sense_data),
2550 sizeof(metatask->taskinfo.bbrread.sense_data)));
2552 cfi_free_metatask(metatask);
2554 mtx_destroy(&bbr_mtx);
2555 cv_destroy(&fe_bbr_info.sem);
2559 case CTL_DELAY_IO: {
2560 struct ctl_io_delay_info *delay_info;
2562 struct ctl_lun *lun;
2563 #endif /* CTL_IO_DELAY */
2565 delay_info = (struct ctl_io_delay_info *)addr;
2568 mtx_lock(&softc->ctl_lock);
2570 if ((delay_info->lun_id > CTL_MAX_LUNS)
2571 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2572 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2574 lun = softc->ctl_luns[delay_info->lun_id];
2576 delay_info->status = CTL_DELAY_STATUS_OK;
2578 switch (delay_info->delay_type) {
2579 case CTL_DELAY_TYPE_CONT:
2581 case CTL_DELAY_TYPE_ONESHOT:
2584 delay_info->status =
2585 CTL_DELAY_STATUS_INVALID_TYPE;
2589 switch (delay_info->delay_loc) {
2590 case CTL_DELAY_LOC_DATAMOVE:
2591 lun->delay_info.datamove_type =
2592 delay_info->delay_type;
2593 lun->delay_info.datamove_delay =
2594 delay_info->delay_secs;
2596 case CTL_DELAY_LOC_DONE:
2597 lun->delay_info.done_type =
2598 delay_info->delay_type;
2599 lun->delay_info.done_delay =
2600 delay_info->delay_secs;
2603 delay_info->status =
2604 CTL_DELAY_STATUS_INVALID_LOC;
2609 mtx_unlock(&softc->ctl_lock);
2611 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2612 #endif /* CTL_IO_DELAY */
2615 case CTL_REALSYNC_SET: {
2618 syncstate = (int *)addr;
2620 mtx_lock(&softc->ctl_lock);
2621 switch (*syncstate) {
2623 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2626 softc->flags |= CTL_FLAG_REAL_SYNC;
2632 mtx_unlock(&softc->ctl_lock);
2635 case CTL_REALSYNC_GET: {
2638 syncstate = (int*)addr;
2640 mtx_lock(&softc->ctl_lock);
2641 if (softc->flags & CTL_FLAG_REAL_SYNC)
2645 mtx_unlock(&softc->ctl_lock);
2651 struct ctl_sync_info *sync_info;
2652 struct ctl_lun *lun;
2654 sync_info = (struct ctl_sync_info *)addr;
2656 mtx_lock(&softc->ctl_lock);
2657 lun = softc->ctl_luns[sync_info->lun_id];
2659 mtx_unlock(&softc->ctl_lock);
2660 sync_info->status = CTL_GS_SYNC_NO_LUN;
2663 * Get or set the sync interval. We're not bounds checking
2664 * in the set case, hopefully the user won't do something
2667 if (cmd == CTL_GETSYNC)
2668 sync_info->sync_interval = lun->sync_interval;
2670 lun->sync_interval = sync_info->sync_interval;
2672 mtx_unlock(&softc->ctl_lock);
2674 sync_info->status = CTL_GS_SYNC_OK;
2678 case CTL_GETSTATS: {
2679 struct ctl_stats *stats;
2680 struct ctl_lun *lun;
2683 stats = (struct ctl_stats *)addr;
2685 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2687 stats->status = CTL_SS_NEED_MORE_SPACE;
2688 stats->num_luns = softc->num_luns;
2692 * XXX KDM no locking here. If the LUN list changes,
2693 * things can blow up.
2695 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2696 i++, lun = STAILQ_NEXT(lun, links)) {
2697 retval = copyout(&lun->stats, &stats->lun_stats[i],
2698 sizeof(lun->stats));
2702 stats->num_luns = softc->num_luns;
2703 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2705 stats->status = CTL_SS_OK;
2707 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2709 stats->flags = CTL_STATS_FLAG_NONE;
2711 getnanouptime(&stats->timestamp);
2714 case CTL_ERROR_INJECT: {
2715 struct ctl_error_desc *err_desc, *new_err_desc;
2716 struct ctl_lun *lun;
2718 err_desc = (struct ctl_error_desc *)addr;
2720 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2722 if (new_err_desc == NULL) {
2723 printf("%s: CTL_ERROR_INJECT: error allocating %zu "
2724 "bytes\n", __func__, sizeof(*new_err_desc));
2728 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2730 mtx_lock(&softc->ctl_lock);
2731 lun = softc->ctl_luns[err_desc->lun_id];
2733 mtx_unlock(&softc->ctl_lock);
2734 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2735 __func__, (uintmax_t)err_desc->lun_id);
2741 * We could do some checking here to verify the validity
2742 * of the request, but given the complexity of error
2743 * injection requests, the checking logic would be fairly
2746 * For now, if the request is invalid, it just won't get
2747 * executed and might get deleted.
2749 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2752 * XXX KDM check to make sure the serial number is unique,
2753 * in case we somehow manage to wrap. That shouldn't
2754 * happen for a very long time, but it's the right thing to
2757 new_err_desc->serial = lun->error_serial;
2758 err_desc->serial = lun->error_serial;
2759 lun->error_serial++;
2761 mtx_unlock(&softc->ctl_lock);
2764 case CTL_ERROR_INJECT_DELETE: {
2765 struct ctl_error_desc *delete_desc, *desc, *desc2;
2766 struct ctl_lun *lun;
2769 delete_desc = (struct ctl_error_desc *)addr;
2772 mtx_lock(&softc->ctl_lock);
2773 lun = softc->ctl_luns[delete_desc->lun_id];
2775 mtx_unlock(&softc->ctl_lock);
2776 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2777 __func__, (uintmax_t)delete_desc->lun_id);
2781 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2782 if (desc->serial != delete_desc->serial)
2785 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2790 mtx_unlock(&softc->ctl_lock);
2791 if (delete_done == 0) {
2792 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2793 "error serial %ju on LUN %u\n", __func__,
2794 delete_desc->serial, delete_desc->lun_id);
2800 case CTL_DUMP_STRUCTS: {
2802 struct ctl_frontend *fe;
2804 printf("CTL IID to WWPN map start:\n");
2805 for (i = 0; i < CTL_MAX_PORTS; i++) {
2806 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2807 if (softc->wwpn_iid[i][j].in_use == 0)
2810 printf("port %d iid %u WWPN %#jx\n",
2811 softc->wwpn_iid[i][j].port,
2812 softc->wwpn_iid[i][j].iid,
2813 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2816 printf("CTL IID to WWPN map end\n");
2817 printf("CTL Persistent Reservation information start:\n");
2818 for (i = 0; i < CTL_MAX_LUNS; i++) {
2819 struct ctl_lun *lun;
2821 lun = softc->ctl_luns[i];
2824 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2827 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2828 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2829 if (lun->per_res[j+k].registered == 0)
2831 printf("LUN %d port %d iid %d key "
2833 (uintmax_t)scsi_8btou64(
2834 lun->per_res[j+k].res_key.key));
2838 printf("CTL Persistent Reservation information end\n");
2839 printf("CTL Frontends:\n");
2841 * XXX KDM calling this without a lock. We'd likely want
2842 * to drop the lock before calling the frontend's dump
2845 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2846 printf("Frontend %s Type %u pport %d vport %d WWNN "
2847 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2848 fe->physical_port, fe->virtual_port,
2849 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2852 * Frontends are not required to support the dump
2855 if (fe->fe_dump == NULL)
2860 printf("CTL Frontend information end\n");
2864 struct ctl_lun_req *lun_req;
2865 struct ctl_backend_driver *backend;
2867 lun_req = (struct ctl_lun_req *)addr;
2869 backend = ctl_backend_find(lun_req->backend);
2870 if (backend == NULL) {
2871 lun_req->status = CTL_LUN_ERROR;
2872 snprintf(lun_req->error_str,
2873 sizeof(lun_req->error_str),
2874 "Backend \"%s\" not found.",
2878 if (lun_req->num_be_args > 0) {
2879 lun_req->kern_be_args = ctl_copyin_args(
2880 lun_req->num_be_args,
2883 sizeof(lun_req->error_str));
2884 if (lun_req->kern_be_args == NULL) {
2885 lun_req->status = CTL_LUN_ERROR;
2890 retval = backend->ioctl(dev, cmd, addr, flag, td);
2892 if (lun_req->num_be_args > 0) {
2893 ctl_free_args(lun_req->num_be_args,
2894 lun_req->kern_be_args);
2898 case CTL_LUN_LIST: {
2900 struct ctl_lun *lun;
2901 struct ctl_lun_list *list;
2903 list = (struct ctl_lun_list *)addr;
2906 * Allocate a fixed length sbuf here, based on the length
2907 * of the user's buffer. We could allocate an auto-extending
2908 * buffer, and then tell the user how much larger our
2909 * amount of data is than his buffer, but that presents
2912 * 1. The sbuf(9) routines use a blocking malloc, and so
2913 * we can't hold a lock while calling them with an
2914 * auto-extending buffer.
2916 * 2. There is not currently a LUN reference counting
2917 * mechanism, outside of outstanding transactions on
2918 * the LUN's OOA queue. So a LUN could go away on us
2919 * while we're getting the LUN number, backend-specific
2920 * information, etc. Thus, given the way things
2921 * currently work, we need to hold the CTL lock while
2922 * grabbing LUN information.
2924 * So, from the user's standpoint, the best thing to do is
2925 * allocate what he thinks is a reasonable buffer length,
2926 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2927 * double the buffer length and try again. (And repeat
2928 * that until he succeeds.)
2930 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2932 list->status = CTL_LUN_LIST_ERROR;
2933 snprintf(list->error_str, sizeof(list->error_str),
2934 "Unable to allocate %d bytes for LUN list",
2939 sbuf_printf(sb, "<ctllunlist>\n");
2941 mtx_lock(&softc->ctl_lock);
2943 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2944 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
2945 (uintmax_t)lun->lun);
2948 * Bail out as soon as we see that we've overfilled
2954 retval = sbuf_printf(sb, "<backend_type>%s"
2955 "</backend_type>\n",
2956 (lun->backend == NULL) ? "none" :
2957 lun->backend->name);
2962 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
2963 lun->be_lun->lun_type);
2968 if (lun->backend == NULL) {
2969 retval = sbuf_printf(sb, "</lun>\n");
2975 retval = sbuf_printf(sb, "<size>%ju</size>\n",
2976 (lun->be_lun->maxlba > 0) ?
2977 lun->be_lun->maxlba + 1 : 0);
2982 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
2983 lun->be_lun->blocksize);
2988 retval = sbuf_printf(sb, "<serial_number>");
2993 retval = ctl_sbuf_printf_esc(sb,
2994 lun->be_lun->serial_num);
2999 retval = sbuf_printf(sb, "</serial_number>\n");
3004 retval = sbuf_printf(sb, "<device_id>");
3009 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3014 retval = sbuf_printf(sb, "</device_id>\n");
3019 if (lun->backend->lun_info == NULL) {
3020 retval = sbuf_printf(sb, "</lun>\n");
3026 retval =lun->backend->lun_info(lun->be_lun->be_lun, sb);
3031 retval = sbuf_printf(sb, "</lun>\n");
3036 mtx_unlock(&softc->ctl_lock);
3039 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3042 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3043 snprintf(list->error_str, sizeof(list->error_str),
3044 "Out of space, %d bytes is too small",
3051 retval = copyout(sbuf_data(sb), list->lun_xml,
3054 list->fill_len = sbuf_len(sb) + 1;
3055 list->status = CTL_LUN_LIST_OK;
3060 /* XXX KDM should we fix this? */
3062 struct ctl_backend_driver *backend;
3069 * We encode the backend type as the ioctl type for backend
3070 * ioctls. So parse it out here, and then search for a
3071 * backend of this type.
3073 type = _IOC_TYPE(cmd);
3075 STAILQ_FOREACH(backend, &softc->be_list, links) {
3076 if (backend->type == type) {
3082 printf("ctl: unknown ioctl command %#lx or backend "
3087 retval = backend->ioctl(dev, cmd, addr, flag, td);
3097 ctl_get_initindex(struct ctl_nexus *nexus)
3099 if (nexus->targ_port < CTL_MAX_PORTS)
3100 return (nexus->initid.id +
3101 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3103 return (nexus->initid.id +
3104 ((nexus->targ_port - CTL_MAX_PORTS) *
3105 CTL_MAX_INIT_PER_PORT));
3109 ctl_get_resindex(struct ctl_nexus *nexus)
3111 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3115 ctl_port_idx(int port_num)
3117 if (port_num < CTL_MAX_PORTS)
3120 return(port_num - CTL_MAX_PORTS);
3124 * Note: This only works for bitmask sizes that are at least 32 bits, and
3125 * that are a power of 2.
3128 ctl_ffz(uint32_t *mask, uint32_t size)
3130 uint32_t num_chunks, num_pieces;
3133 num_chunks = (size >> 5);
3134 if (num_chunks == 0)
3136 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3138 for (i = 0; i < num_chunks; i++) {
3139 for (j = 0; j < num_pieces; j++) {
3140 if ((mask[i] & (1 << j)) == 0)
3141 return ((i << 5) + j);
3149 ctl_set_mask(uint32_t *mask, uint32_t bit)
3151 uint32_t chunk, piece;
3154 piece = bit % (sizeof(uint32_t) * 8);
3156 if ((mask[chunk] & (1 << piece)) != 0)
3159 mask[chunk] |= (1 << piece);
3165 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3167 uint32_t chunk, piece;
3170 piece = bit % (sizeof(uint32_t) * 8);
3172 if ((mask[chunk] & (1 << piece)) == 0)
3175 mask[chunk] &= ~(1 << piece);
3181 ctl_is_set(uint32_t *mask, uint32_t bit)
3183 uint32_t chunk, piece;
3186 piece = bit % (sizeof(uint32_t) * 8);
3188 if ((mask[chunk] & (1 << piece)) == 0)
3196 * The bus, target and lun are optional, they can be filled in later.
3197 * can_wait is used to determine whether we can wait on the malloc or not.
3200 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3201 uint32_t targ_lun, int can_wait)
3206 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3208 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3211 io->io_hdr.io_type = io_type;
3212 io->io_hdr.targ_port = targ_port;
3214 * XXX KDM this needs to change/go away. We need to move
3215 * to a preallocated pool of ctl_scsiio structures.
3217 io->io_hdr.nexus.targ_target.id = targ_target;
3218 io->io_hdr.nexus.targ_lun = targ_lun;
3225 ctl_kfree_io(union ctl_io *io)
3232 * ctl_softc, pool_type, total_ctl_io are passed in.
3233 * npool is passed out.
3236 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3237 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3240 union ctl_io *cur_io, *next_io;
3241 struct ctl_io_pool *pool;
3246 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, M_NOWAIT);
3252 memset(pool, 0, sizeof(*pool));
3254 pool->type = pool_type;
3255 pool->ctl_softc = ctl_softc;
3257 mtx_lock(&ctl_softc->ctl_lock);
3258 pool->id = ctl_softc->cur_pool_id++;
3259 mtx_unlock(&ctl_softc->ctl_lock);
3261 pool->flags = CTL_POOL_FLAG_NONE;
3262 STAILQ_INIT(&pool->free_queue);
3265 * XXX KDM other options here:
3266 * - allocate a page at a time
3267 * - allocate one big chunk of memory.
3268 * Page allocation might work well, but would take a little more
3271 for (i = 0; i < total_ctl_io; i++) {
3272 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3274 if (cur_io == NULL) {
3278 cur_io->io_hdr.pool = pool;
3279 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3280 pool->total_ctl_io++;
3281 pool->free_ctl_io++;
3285 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3286 cur_io != NULL; cur_io = next_io) {
3287 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3289 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3291 free(cur_io, M_CTL);
3297 mtx_lock(&ctl_softc->ctl_lock);
3298 ctl_softc->num_pools++;
3299 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3301 * Increment our usage count if this is an external consumer, so we
3302 * can't get unloaded until the external consumer (most likely a
3303 * FETD) unloads and frees his pool.
3305 * XXX KDM will this increment the caller's module use count, or
3309 if ((pool_type != CTL_POOL_EMERGENCY)
3310 && (pool_type != CTL_POOL_INTERNAL)
3311 && (pool_type != CTL_POOL_IOCTL)
3312 && (pool_type != CTL_POOL_4OTHERSC))
3316 mtx_unlock(&ctl_softc->ctl_lock);
3326 * Caller must hold ctl_softc->ctl_lock.
3329 ctl_pool_acquire(struct ctl_io_pool *pool)
3334 if (pool->flags & CTL_POOL_FLAG_INVALID)
3343 * Caller must hold ctl_softc->ctl_lock.
3346 ctl_pool_invalidate(struct ctl_io_pool *pool)
3351 pool->flags |= CTL_POOL_FLAG_INVALID;
3357 * Caller must hold ctl_softc->ctl_lock.
3360 ctl_pool_release(struct ctl_io_pool *pool)
3365 if ((--pool->refcount == 0)
3366 && (pool->flags & CTL_POOL_FLAG_INVALID)) {
3367 ctl_pool_free(pool->ctl_softc, pool);
3374 * Must be called with ctl_softc->ctl_lock held.
3377 ctl_pool_free(struct ctl_softc *ctl_softc, struct ctl_io_pool *pool)
3379 union ctl_io *cur_io, *next_io;
3381 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3382 cur_io != NULL; cur_io = next_io) {
3383 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3385 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, ctl_io_hdr,
3387 free(cur_io, M_CTL);
3390 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3391 ctl_softc->num_pools--;
3394 * XXX KDM will this decrement the caller's usage count or mine?
3397 if ((pool->type != CTL_POOL_EMERGENCY)
3398 && (pool->type != CTL_POOL_INTERNAL)
3399 && (pool->type != CTL_POOL_IOCTL))
3407 * This routine does not block (except for spinlocks of course).
3408 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3412 ctl_alloc_io(void *pool_ref)
3415 struct ctl_softc *ctl_softc;
3416 struct ctl_io_pool *pool, *npool;
3417 struct ctl_io_pool *emergency_pool;
3419 pool = (struct ctl_io_pool *)pool_ref;
3422 printf("%s: pool is NULL\n", __func__);
3426 emergency_pool = NULL;
3428 ctl_softc = pool->ctl_softc;
3430 mtx_lock(&ctl_softc->ctl_lock);
3432 * First, try to get the io structure from the user's pool.
3434 if (ctl_pool_acquire(pool) == 0) {
3435 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3437 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3438 pool->total_allocated++;
3439 pool->free_ctl_io--;
3440 mtx_unlock(&ctl_softc->ctl_lock);
3443 ctl_pool_release(pool);
3446 * If he doesn't have any io structures left, search for an
3447 * emergency pool and grab one from there.
3449 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3450 if (npool->type != CTL_POOL_EMERGENCY)
3453 if (ctl_pool_acquire(npool) != 0)
3456 emergency_pool = npool;
3458 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3460 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3461 npool->total_allocated++;
3462 npool->free_ctl_io--;
3463 mtx_unlock(&ctl_softc->ctl_lock);
3466 ctl_pool_release(npool);
3469 /* Drop the spinlock before we malloc */
3470 mtx_unlock(&ctl_softc->ctl_lock);
3473 * The emergency pool (if it exists) didn't have one, so try an
3474 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3476 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3479 * If the emergency pool exists but is empty, add this
3480 * ctl_io to its list when it gets freed.
3482 if (emergency_pool != NULL) {
3483 mtx_lock(&ctl_softc->ctl_lock);
3484 if (ctl_pool_acquire(emergency_pool) == 0) {
3485 io->io_hdr.pool = emergency_pool;
3486 emergency_pool->total_ctl_io++;
3488 * Need to bump this, otherwise
3489 * total_allocated and total_freed won't
3490 * match when we no longer have anything
3493 emergency_pool->total_allocated++;
3495 mtx_unlock(&ctl_softc->ctl_lock);
3497 io->io_hdr.pool = NULL;
3504 ctl_free_io_internal(union ctl_io *io, int have_lock)
3510 * If this ctl_io has a pool, return it to that pool.
3512 if (io->io_hdr.pool != NULL) {
3513 struct ctl_io_pool *pool;
3515 struct ctl_softc *ctl_softc;
3516 union ctl_io *tmp_io;
3517 unsigned long xflags;
3520 ctl_softc = control_softc;
3523 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3526 mtx_lock(&pool->ctl_softc->ctl_lock);
3530 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3531 &ctl_softc->task_queue); tmp_io != NULL; i++,
3532 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3535 printf("%s: %p is still on the task queue!\n",
3537 printf("%s: (%d): type %d "
3538 "msg %d cdb %x iptl: "
3539 "%d:%d:%d:%d tag 0x%04x "
3542 tmp_io->io_hdr.io_type,
3543 tmp_io->io_hdr.msg_type,
3544 tmp_io->scsiio.cdb[0],
3545 tmp_io->io_hdr.nexus.initid.id,
3546 tmp_io->io_hdr.nexus.targ_port,
3547 tmp_io->io_hdr.nexus.targ_target.id,
3548 tmp_io->io_hdr.nexus.targ_lun,
3549 (tmp_io->io_hdr.io_type ==
3551 tmp_io->taskio.tag_num :
3552 tmp_io->scsiio.tag_num,
3554 panic("I/O still on the task queue!");
3558 io->io_hdr.io_type = 0xff;
3559 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3560 pool->total_freed++;
3561 pool->free_ctl_io++;
3562 ctl_pool_release(pool);
3564 mtx_unlock(&pool->ctl_softc->ctl_lock);
3567 * Otherwise, just free it. We probably malloced it and
3568 * the emergency pool wasn't available.
3576 ctl_free_io(union ctl_io *io)
3578 ctl_free_io_internal(io, /*have_lock*/ 0);
3582 ctl_zero_io(union ctl_io *io)
3590 * May need to preserve linked list pointers at some point too.
3592 pool_ref = io->io_hdr.pool;
3594 memset(io, 0, sizeof(*io));
3596 io->io_hdr.pool = pool_ref;
3600 * This routine is currently used for internal copies of ctl_ios that need
3601 * to persist for some reason after we've already returned status to the
3602 * FETD. (Thus the flag set.)
3605 * Note that this makes a blind copy of all fields in the ctl_io, except
3606 * for the pool reference. This includes any memory that has been
3607 * allocated! That memory will no longer be valid after done has been
3608 * called, so this would be VERY DANGEROUS for command that actually does
3609 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3610 * start and stop commands, which don't transfer any data, so this is not a
3611 * problem. If it is used for anything else, the caller would also need to
3612 * allocate data buffer space and this routine would need to be modified to
3613 * copy the data buffer(s) as well.
3616 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3625 * May need to preserve linked list pointers at some point too.
3627 pool_ref = dest->io_hdr.pool;
3629 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3631 dest->io_hdr.pool = pool_ref;
3633 * We need to know that this is an internal copy, and doesn't need
3634 * to get passed back to the FETD that allocated it.
3636 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3641 ctl_update_power_subpage(struct copan_power_subpage *page)
3643 int num_luns, num_partitions, config_type;
3644 struct ctl_softc *softc;
3645 cs_BOOL_t aor_present, shelf_50pct_power;
3646 cs_raidset_personality_t rs_type;
3647 int max_active_luns;
3649 softc = control_softc;
3651 /* subtract out the processor LUN */
3652 num_luns = softc->num_luns - 1;
3654 * Default to 7 LUNs active, which was the only number we allowed
3657 max_active_luns = 7;
3659 num_partitions = config_GetRsPartitionInfo();
3660 config_type = config_GetConfigType();
3661 shelf_50pct_power = config_GetShelfPowerMode();
3662 aor_present = config_IsAorRsPresent();
3664 rs_type = ddb_GetRsRaidType(1);
3665 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3666 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3667 EPRINT(0, "Unsupported RS type %d!", rs_type);
3671 page->total_luns = num_luns;
3673 switch (config_type) {
3676 * In a 40 drive configuration, it doesn't matter what DC
3677 * cards we have, whether we have AOR enabled or not,
3678 * partitioning or not, or what type of RAIDset we have.
3679 * In that scenario, we can power up every LUN we present
3682 max_active_luns = num_luns;
3686 if (shelf_50pct_power == CS_FALSE) {
3688 if (aor_present == CS_TRUE) {
3690 CS_RAIDSET_PERSONALITY_RAID5) {
3691 max_active_luns = 7;
3692 } else if (rs_type ==
3693 CS_RAIDSET_PERSONALITY_RAID1){
3694 max_active_luns = 14;
3696 /* XXX KDM now what?? */
3700 CS_RAIDSET_PERSONALITY_RAID5) {
3701 max_active_luns = 8;
3702 } else if (rs_type ==
3703 CS_RAIDSET_PERSONALITY_RAID1){
3704 max_active_luns = 16;
3706 /* XXX KDM now what?? */
3712 * With 50% power in a 64 drive configuration, we
3713 * can power all LUNs we present.
3715 max_active_luns = num_luns;
3719 if (shelf_50pct_power == CS_FALSE) {
3721 if (aor_present == CS_TRUE) {
3723 CS_RAIDSET_PERSONALITY_RAID5) {
3724 max_active_luns = 7;
3725 } else if (rs_type ==
3726 CS_RAIDSET_PERSONALITY_RAID1){
3727 max_active_luns = 14;
3729 /* XXX KDM now what?? */
3733 CS_RAIDSET_PERSONALITY_RAID5) {
3734 max_active_luns = 8;
3735 } else if (rs_type ==
3736 CS_RAIDSET_PERSONALITY_RAID1){
3737 max_active_luns = 16;
3739 /* XXX KDM now what?? */
3744 if (aor_present == CS_TRUE) {
3746 CS_RAIDSET_PERSONALITY_RAID5) {
3747 max_active_luns = 14;
3748 } else if (rs_type ==
3749 CS_RAIDSET_PERSONALITY_RAID1){
3751 * We're assuming here that disk
3752 * caching is enabled, and so we're
3753 * able to power up half of each
3754 * LUN, and cache all writes.
3756 max_active_luns = num_luns;
3758 /* XXX KDM now what?? */
3762 CS_RAIDSET_PERSONALITY_RAID5) {
3763 max_active_luns = 15;
3764 } else if (rs_type ==
3765 CS_RAIDSET_PERSONALITY_RAID1){
3766 max_active_luns = 30;
3768 /* XXX KDM now what?? */
3775 * In this case, we have an unknown configuration, so we
3776 * just use the default from above.
3781 page->max_active_luns = max_active_luns;
3783 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3784 page->total_luns, page->max_active_luns);
3787 #endif /* NEEDTOPORT */
3790 * This routine could be used in the future to load default and/or saved
3791 * mode page parameters for a particuar lun.
3794 ctl_init_page_index(struct ctl_lun *lun)
3797 struct ctl_page_index *page_index;
3798 struct ctl_softc *softc;
3800 memcpy(&lun->mode_pages.index, page_index_template,
3801 sizeof(page_index_template));
3803 softc = lun->ctl_softc;
3805 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3807 page_index = &lun->mode_pages.index[i];
3809 * If this is a disk-only mode page, there's no point in
3810 * setting it up. For some pages, we have to have some
3811 * basic information about the disk in order to calculate the
3814 if ((lun->be_lun->lun_type != T_DIRECT)
3815 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3818 switch (page_index->page_code & SMPH_PC_MASK) {
3819 case SMS_FORMAT_DEVICE_PAGE: {
3820 struct scsi_format_page *format_page;
3822 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3823 panic("subpage is incorrect!");
3826 * Sectors per track are set above. Bytes per
3827 * sector need to be set here on a per-LUN basis.
3829 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3830 &format_page_default,
3831 sizeof(format_page_default));
3832 memcpy(&lun->mode_pages.format_page[
3833 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3834 sizeof(format_page_changeable));
3835 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3836 &format_page_default,
3837 sizeof(format_page_default));
3838 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3839 &format_page_default,
3840 sizeof(format_page_default));
3842 format_page = &lun->mode_pages.format_page[
3844 scsi_ulto2b(lun->be_lun->blocksize,
3845 format_page->bytes_per_sector);
3847 format_page = &lun->mode_pages.format_page[
3849 scsi_ulto2b(lun->be_lun->blocksize,
3850 format_page->bytes_per_sector);
3852 format_page = &lun->mode_pages.format_page[
3854 scsi_ulto2b(lun->be_lun->blocksize,
3855 format_page->bytes_per_sector);
3857 page_index->page_data =
3858 (uint8_t *)lun->mode_pages.format_page;
3861 case SMS_RIGID_DISK_PAGE: {
3862 struct scsi_rigid_disk_page *rigid_disk_page;
3863 uint32_t sectors_per_cylinder;
3867 #endif /* !__XSCALE__ */
3869 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3870 panic("invalid subpage value %d",
3871 page_index->subpage);
3874 * Rotation rate and sectors per track are set
3875 * above. We calculate the cylinders here based on
3876 * capacity. Due to the number of heads and
3877 * sectors per track we're using, smaller arrays
3878 * may turn out to have 0 cylinders. Linux and
3879 * FreeBSD don't pay attention to these mode pages
3880 * to figure out capacity, but Solaris does. It
3881 * seems to deal with 0 cylinders just fine, and
3882 * works out a fake geometry based on the capacity.
3884 memcpy(&lun->mode_pages.rigid_disk_page[
3885 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3886 sizeof(rigid_disk_page_default));
3887 memcpy(&lun->mode_pages.rigid_disk_page[
3888 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3889 sizeof(rigid_disk_page_changeable));
3890 memcpy(&lun->mode_pages.rigid_disk_page[
3891 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3892 sizeof(rigid_disk_page_default));
3893 memcpy(&lun->mode_pages.rigid_disk_page[
3894 CTL_PAGE_SAVED], &rigid_disk_page_default,
3895 sizeof(rigid_disk_page_default));
3897 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3901 * The divide method here will be more accurate,
3902 * probably, but results in floating point being
3903 * used in the kernel on i386 (__udivdi3()). On the
3904 * XScale, though, __udivdi3() is implemented in
3907 * The shift method for cylinder calculation is
3908 * accurate if sectors_per_cylinder is a power of
3909 * 2. Otherwise it might be slightly off -- you
3910 * might have a bit of a truncation problem.
3913 cylinders = (lun->be_lun->maxlba + 1) /
3914 sectors_per_cylinder;
3916 for (shift = 31; shift > 0; shift--) {
3917 if (sectors_per_cylinder & (1 << shift))
3920 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3924 * We've basically got 3 bytes, or 24 bits for the
3925 * cylinder size in the mode page. If we're over,
3926 * just round down to 2^24.
3928 if (cylinders > 0xffffff)
3929 cylinders = 0xffffff;
3931 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3933 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3935 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3937 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3939 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3941 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3943 page_index->page_data =
3944 (uint8_t *)lun->mode_pages.rigid_disk_page;
3947 case SMS_CACHING_PAGE: {
3949 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3950 panic("invalid subpage value %d",
3951 page_index->subpage);
3953 * Defaults should be okay here, no calculations
3956 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3957 &caching_page_default,
3958 sizeof(caching_page_default));
3959 memcpy(&lun->mode_pages.caching_page[
3960 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3961 sizeof(caching_page_changeable));
3962 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3963 &caching_page_default,
3964 sizeof(caching_page_default));
3965 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3966 &caching_page_default,
3967 sizeof(caching_page_default));
3968 page_index->page_data =
3969 (uint8_t *)lun->mode_pages.caching_page;
3972 case SMS_CONTROL_MODE_PAGE: {
3974 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3975 panic("invalid subpage value %d",
3976 page_index->subpage);
3979 * Defaults should be okay here, no calculations
3982 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
3983 &control_page_default,
3984 sizeof(control_page_default));
3985 memcpy(&lun->mode_pages.control_page[
3986 CTL_PAGE_CHANGEABLE], &control_page_changeable,
3987 sizeof(control_page_changeable));
3988 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
3989 &control_page_default,
3990 sizeof(control_page_default));
3991 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
3992 &control_page_default,
3993 sizeof(control_page_default));
3994 page_index->page_data =
3995 (uint8_t *)lun->mode_pages.control_page;
3999 case SMS_VENDOR_SPECIFIC_PAGE:{
4000 switch (page_index->subpage) {
4001 case PWR_SUBPAGE_CODE: {
4002 struct copan_power_subpage *current_page,
4005 memcpy(&lun->mode_pages.power_subpage[
4007 &power_page_default,
4008 sizeof(power_page_default));
4009 memcpy(&lun->mode_pages.power_subpage[
4010 CTL_PAGE_CHANGEABLE],
4011 &power_page_changeable,
4012 sizeof(power_page_changeable));
4013 memcpy(&lun->mode_pages.power_subpage[
4015 &power_page_default,
4016 sizeof(power_page_default));
4017 memcpy(&lun->mode_pages.power_subpage[
4019 &power_page_default,
4020 sizeof(power_page_default));
4021 page_index->page_data =
4022 (uint8_t *)lun->mode_pages.power_subpage;
4024 current_page = (struct copan_power_subpage *)
4025 (page_index->page_data +
4026 (page_index->page_len *
4028 saved_page = (struct copan_power_subpage *)
4029 (page_index->page_data +
4030 (page_index->page_len *
4034 case APS_SUBPAGE_CODE: {
4035 struct copan_aps_subpage *current_page,
4038 // This gets set multiple times but
4039 // it should always be the same. It's
4040 // only done during init so who cares.
4041 index_to_aps_page = i;
4043 memcpy(&lun->mode_pages.aps_subpage[
4046 sizeof(aps_page_default));
4047 memcpy(&lun->mode_pages.aps_subpage[
4048 CTL_PAGE_CHANGEABLE],
4049 &aps_page_changeable,
4050 sizeof(aps_page_changeable));
4051 memcpy(&lun->mode_pages.aps_subpage[
4054 sizeof(aps_page_default));
4055 memcpy(&lun->mode_pages.aps_subpage[
4058 sizeof(aps_page_default));
4059 page_index->page_data =
4060 (uint8_t *)lun->mode_pages.aps_subpage;
4062 current_page = (struct copan_aps_subpage *)
4063 (page_index->page_data +
4064 (page_index->page_len *
4066 saved_page = (struct copan_aps_subpage *)
4067 (page_index->page_data +
4068 (page_index->page_len *
4072 case DBGCNF_SUBPAGE_CODE: {
4073 struct copan_debugconf_subpage *current_page,
4076 memcpy(&lun->mode_pages.debugconf_subpage[
4078 &debugconf_page_default,
4079 sizeof(debugconf_page_default));
4080 memcpy(&lun->mode_pages.debugconf_subpage[
4081 CTL_PAGE_CHANGEABLE],
4082 &debugconf_page_changeable,
4083 sizeof(debugconf_page_changeable));
4084 memcpy(&lun->mode_pages.debugconf_subpage[
4086 &debugconf_page_default,
4087 sizeof(debugconf_page_default));
4088 memcpy(&lun->mode_pages.debugconf_subpage[
4090 &debugconf_page_default,
4091 sizeof(debugconf_page_default));
4092 page_index->page_data =
4093 (uint8_t *)lun->mode_pages.debugconf_subpage;
4095 current_page = (struct copan_debugconf_subpage *)
4096 (page_index->page_data +
4097 (page_index->page_len *
4099 saved_page = (struct copan_debugconf_subpage *)
4100 (page_index->page_data +
4101 (page_index->page_len *
4106 panic("invalid subpage value %d",
4107 page_index->subpage);
4113 panic("invalid page value %d",
4114 page_index->page_code & SMPH_PC_MASK);
4119 return (CTL_RETVAL_COMPLETE);
4126 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4127 * wants us to allocate the LUN and he can block.
4128 * - ctl_softc is always set
4129 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4131 * Returns 0 for success, non-zero (errno) for failure.
4134 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4135 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4137 struct ctl_lun *nlun, *lun;
4138 struct ctl_frontend *fe;
4145 * We currently only support Direct Access or Processor LUN types.
4147 switch (be_lun->lun_type) {
4155 be_lun->lun_config_status(be_lun->be_lun,
4156 CTL_LUN_CONFIG_FAILURE);
4159 if (ctl_lun == NULL) {
4160 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4162 be_lun->lun_config_status(lun->be_lun->be_lun,
4163 CTL_LUN_CONFIG_FAILURE);
4166 lun->flags = CTL_LUN_MALLOCED;
4170 memset(lun, 0, sizeof(*lun));
4172 mtx_lock(&ctl_softc->ctl_lock);
4174 * See if the caller requested a particular LUN number. If so, see
4175 * if it is available. Otherwise, allocate the first available LUN.
4177 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4178 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4179 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4180 mtx_unlock(&ctl_softc->ctl_lock);
4181 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4182 printf("ctl: requested LUN ID %d is higher "
4183 "than CTL_MAX_LUNS - 1 (%d)\n",
4184 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4187 * XXX KDM return an error, or just assign
4188 * another LUN ID in this case??
4190 printf("ctl: requested LUN ID %d is already "
4191 "in use\n", be_lun->req_lun_id);
4193 if (lun->flags & CTL_LUN_MALLOCED)
4195 be_lun->lun_config_status(be_lun->be_lun,
4196 CTL_LUN_CONFIG_FAILURE);
4199 lun_number = be_lun->req_lun_id;
4201 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4202 if (lun_number == -1) {
4203 mtx_unlock(&ctl_softc->ctl_lock);
4204 printf("ctl: can't allocate LUN on target %ju, out of "
4205 "LUNs\n", (uintmax_t)target_id.id);
4206 if (lun->flags & CTL_LUN_MALLOCED)
4208 be_lun->lun_config_status(be_lun->be_lun,
4209 CTL_LUN_CONFIG_FAILURE);
4213 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4215 lun->target = target_id;
4216 lun->lun = lun_number;
4217 lun->be_lun = be_lun;
4219 * The processor LUN is always enabled. Disk LUNs come on line
4220 * disabled, and must be enabled by the backend.
4222 lun->flags = CTL_LUN_DISABLED;
4223 lun->backend = be_lun->be;
4224 be_lun->ctl_lun = lun;
4225 be_lun->lun_id = lun_number;
4226 atomic_add_int(&be_lun->be->num_luns, 1);
4227 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4228 lun->flags |= CTL_LUN_STOPPED;
4230 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4231 lun->flags |= CTL_LUN_INOPERABLE;
4233 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4234 lun->flags |= CTL_LUN_PRIMARY_SC;
4236 lun->ctl_softc = ctl_softc;
4237 TAILQ_INIT(&lun->ooa_queue);
4238 TAILQ_INIT(&lun->blocked_queue);
4239 STAILQ_INIT(&lun->error_list);
4242 * Initialize the mode page index.
4244 ctl_init_page_index(lun);
4247 * Set the poweron UA for all initiators on this LUN only.
4249 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4250 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4253 * Now, before we insert this lun on the lun list, set the lun
4254 * inventory changed UA for all other luns.
4256 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4257 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4258 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4262 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4264 ctl_softc->ctl_luns[lun_number] = lun;
4266 ctl_softc->num_luns++;
4268 /* Setup statistics gathering */
4269 lun->stats.device_type = be_lun->lun_type;
4270 lun->stats.lun_number = lun_number;
4271 if (lun->stats.device_type == T_DIRECT)
4272 lun->stats.blocksize = be_lun->blocksize;
4274 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4275 for (i = 0;i < CTL_MAX_PORTS;i++)
4276 lun->stats.ports[i].targ_port = i;
4278 mtx_unlock(&ctl_softc->ctl_lock);
4280 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4283 * Run through each registered FETD and bring it online if it isn't
4284 * already. Enable the target ID if it hasn't been enabled, and
4285 * enable this particular LUN.
4287 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4291 * XXX KDM this only works for ONE TARGET ID. We'll need
4292 * to do things differently if we go to a multiple target
4295 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4297 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4299 printf("ctl_alloc_lun: FETD %s port %d "
4300 "returned error %d for targ_enable on "
4301 "target %ju\n", fe->port_name,
4302 fe->targ_port, retval,
4303 (uintmax_t)target_id.id);
4305 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4308 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4310 printf("ctl_alloc_lun: FETD %s port %d returned error "
4311 "%d for lun_enable on target %ju lun %d\n",
4312 fe->port_name, fe->targ_port, retval,
4313 (uintmax_t)target_id.id, lun_number);
4315 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4323 * - caller holds ctl_softc->ctl_lock.
4324 * - LUN has already been marked invalid and any pending I/O has been taken
4328 ctl_free_lun(struct ctl_lun *lun)
4330 struct ctl_softc *softc;
4332 struct ctl_frontend *fe;
4334 struct ctl_lun *nlun;
4335 union ctl_io *io, *next_io;
4338 softc = lun->ctl_softc;
4340 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4342 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4344 softc->ctl_luns[lun->lun] = NULL;
4346 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4347 printf("ctl_free_lun: aieee!! freeing a LUN with "
4348 "outstanding I/O!!\n");
4352 * If we have anything pending on the RtR queue, remove it.
4354 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4356 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4357 if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4358 && (io->io_hdr.nexus.targ_lun == lun->lun))
4359 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4364 * Then remove everything from the blocked queue.
4366 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4368 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4369 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4370 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4374 * Now clear out the OOA queue, and free all the I/O.
4375 * XXX KDM should we notify the FETD here? We probably need to
4376 * quiesce the LUN before deleting it.
4378 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4380 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4381 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4382 ctl_free_io_internal(io, /*have_lock*/ 1);
4388 * XXX KDM this scheme only works for a single target/multiple LUN
4389 * setup. It needs to be revamped for a multiple target scheme.
4391 * XXX KDM this results in fe->lun_disable() getting called twice,
4392 * once when ctl_disable_lun() is called, and a second time here.
4393 * We really need to re-think the LUN disable semantics. There
4394 * should probably be several steps/levels to LUN removal:
4399 * Right now we only have a disable method when communicating to
4400 * the front end ports, at least for individual LUNs.
4403 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4406 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4409 printf("ctl_free_lun: FETD %s port %d returned error "
4410 "%d for lun_disable on target %ju lun %jd\n",
4411 fe->port_name, fe->targ_port, retval,
4412 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4415 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4416 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4418 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4420 printf("ctl_free_lun: FETD %s port %d "
4421 "returned error %d for targ_disable on "
4422 "target %ju\n", fe->port_name,
4423 fe->targ_port, retval,
4424 (uintmax_t)lun->target.id);
4426 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4428 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4432 fe->port_offline(fe->onoff_arg);
4433 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4440 * Tell the backend to free resources, if this LUN has a backend.
4442 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4443 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4445 if (lun->flags & CTL_LUN_MALLOCED)
4448 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4449 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4450 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4458 ctl_create_lun(struct ctl_be_lun *be_lun)
4460 struct ctl_softc *ctl_softc;
4462 ctl_softc = control_softc;
4465 * ctl_alloc_lun() should handle all potential failure cases.
4467 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4471 ctl_add_lun(struct ctl_be_lun *be_lun)
4473 struct ctl_softc *ctl_softc;
4475 ctl_softc = control_softc;
4477 mtx_lock(&ctl_softc->ctl_lock);
4478 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4479 mtx_unlock(&ctl_softc->ctl_lock);
4481 ctl_wakeup_thread();
4487 ctl_enable_lun(struct ctl_be_lun *be_lun)
4489 struct ctl_softc *ctl_softc;
4490 struct ctl_frontend *fe, *nfe;
4491 struct ctl_lun *lun;
4494 ctl_softc = control_softc;
4496 lun = (struct ctl_lun *)be_lun->ctl_lun;
4498 mtx_lock(&ctl_softc->ctl_lock);
4499 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4501 * eh? Why did we get called if the LUN is already
4504 mtx_unlock(&ctl_softc->ctl_lock);
4507 lun->flags &= ~CTL_LUN_DISABLED;
4509 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4510 nfe = STAILQ_NEXT(fe, links);
4513 * Drop the lock while we call the FETD's enable routine.
4514 * This can lead to a callback into CTL (at least in the
4515 * case of the internal initiator frontend.
4517 mtx_unlock(&ctl_softc->ctl_lock);
4518 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4519 mtx_lock(&ctl_softc->ctl_lock);
4521 printf("%s: FETD %s port %d returned error "
4522 "%d for lun_enable on target %ju lun %jd\n",
4523 __func__, fe->port_name, fe->targ_port, retval,
4524 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4528 /* NOTE: TODO: why does lun enable affect port status? */
4529 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4534 mtx_unlock(&ctl_softc->ctl_lock);
4540 ctl_disable_lun(struct ctl_be_lun *be_lun)
4542 struct ctl_softc *ctl_softc;
4543 struct ctl_frontend *fe;
4544 struct ctl_lun *lun;
4547 ctl_softc = control_softc;
4549 lun = (struct ctl_lun *)be_lun->ctl_lun;
4551 mtx_lock(&ctl_softc->ctl_lock);
4553 if (lun->flags & CTL_LUN_DISABLED) {
4554 mtx_unlock(&ctl_softc->ctl_lock);
4557 lun->flags |= CTL_LUN_DISABLED;
4559 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4560 mtx_unlock(&ctl_softc->ctl_lock);
4562 * Drop the lock before we call the frontend's disable
4563 * routine, to avoid lock order reversals.
4565 * XXX KDM what happens if the frontend list changes while
4566 * we're traversing it? It's unlikely, but should be handled.
4568 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4570 mtx_lock(&ctl_softc->ctl_lock);
4572 printf("ctl_alloc_lun: FETD %s port %d returned error "
4573 "%d for lun_disable on target %ju lun %jd\n",
4574 fe->port_name, fe->targ_port, retval,
4575 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4579 mtx_unlock(&ctl_softc->ctl_lock);
4585 ctl_start_lun(struct ctl_be_lun *be_lun)
4587 struct ctl_softc *ctl_softc;
4588 struct ctl_lun *lun;
4590 ctl_softc = control_softc;
4592 lun = (struct ctl_lun *)be_lun->ctl_lun;
4594 mtx_lock(&ctl_softc->ctl_lock);
4595 lun->flags &= ~CTL_LUN_STOPPED;
4596 mtx_unlock(&ctl_softc->ctl_lock);
4602 ctl_stop_lun(struct ctl_be_lun *be_lun)
4604 struct ctl_softc *ctl_softc;
4605 struct ctl_lun *lun;
4607 ctl_softc = control_softc;
4609 lun = (struct ctl_lun *)be_lun->ctl_lun;
4611 mtx_lock(&ctl_softc->ctl_lock);
4612 lun->flags |= CTL_LUN_STOPPED;
4613 mtx_unlock(&ctl_softc->ctl_lock);
4619 ctl_lun_offline(struct ctl_be_lun *be_lun)
4621 struct ctl_softc *ctl_softc;
4622 struct ctl_lun *lun;
4624 ctl_softc = control_softc;
4626 lun = (struct ctl_lun *)be_lun->ctl_lun;
4628 mtx_lock(&ctl_softc->ctl_lock);
4629 lun->flags |= CTL_LUN_OFFLINE;
4630 mtx_unlock(&ctl_softc->ctl_lock);
4636 ctl_lun_online(struct ctl_be_lun *be_lun)
4638 struct ctl_softc *ctl_softc;
4639 struct ctl_lun *lun;
4641 ctl_softc = control_softc;
4643 lun = (struct ctl_lun *)be_lun->ctl_lun;
4645 mtx_lock(&ctl_softc->ctl_lock);
4646 lun->flags &= ~CTL_LUN_OFFLINE;
4647 mtx_unlock(&ctl_softc->ctl_lock);
4653 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4655 struct ctl_softc *ctl_softc;
4656 struct ctl_lun *lun;
4658 ctl_softc = control_softc;
4660 lun = (struct ctl_lun *)be_lun->ctl_lun;
4662 mtx_lock(&ctl_softc->ctl_lock);
4665 * The LUN needs to be disabled before it can be marked invalid.
4667 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4668 mtx_unlock(&ctl_softc->ctl_lock);
4672 * Mark the LUN invalid.
4674 lun->flags |= CTL_LUN_INVALID;
4677 * If there is nothing in the OOA queue, go ahead and free the LUN.
4678 * If we have something in the OOA queue, we'll free it when the
4679 * last I/O completes.
4681 if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4683 mtx_unlock(&ctl_softc->ctl_lock);
4689 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4691 struct ctl_softc *ctl_softc;
4692 struct ctl_lun *lun;
4694 ctl_softc = control_softc;
4695 lun = (struct ctl_lun *)be_lun->ctl_lun;
4697 mtx_lock(&ctl_softc->ctl_lock);
4698 lun->flags |= CTL_LUN_INOPERABLE;
4699 mtx_unlock(&ctl_softc->ctl_lock);
4705 ctl_lun_operable(struct ctl_be_lun *be_lun)
4707 struct ctl_softc *ctl_softc;
4708 struct ctl_lun *lun;
4710 ctl_softc = control_softc;
4711 lun = (struct ctl_lun *)be_lun->ctl_lun;
4713 mtx_lock(&ctl_softc->ctl_lock);
4714 lun->flags &= ~CTL_LUN_INOPERABLE;
4715 mtx_unlock(&ctl_softc->ctl_lock);
4721 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4724 struct ctl_softc *softc;
4725 struct ctl_lun *lun;
4726 struct copan_aps_subpage *current_sp;
4727 struct ctl_page_index *page_index;
4730 softc = control_softc;
4732 mtx_lock(&softc->ctl_lock);
4734 lun = (struct ctl_lun *)be_lun->ctl_lun;
4737 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4738 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4742 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4744 page_index = &lun->mode_pages.index[i];
4747 if (page_index == NULL) {
4748 mtx_unlock(&softc->ctl_lock);
4749 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4750 (uintmax_t)lun->lun);
4754 if ((softc->aps_locked_lun != 0)
4755 && (softc->aps_locked_lun != lun->lun)) {
4756 printf("%s: attempt to lock LUN %llu when %llu is already "
4758 mtx_unlock(&softc->ctl_lock);
4763 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4764 (page_index->page_len * CTL_PAGE_CURRENT));
4767 current_sp->lock_active = APS_LOCK_ACTIVE;
4768 softc->aps_locked_lun = lun->lun;
4770 current_sp->lock_active = 0;
4771 softc->aps_locked_lun = 0;
4776 * If we're in HA mode, try to send the lock message to the other
4779 if (ctl_is_single == 0) {
4781 union ctl_ha_msg lock_msg;
4783 lock_msg.hdr.nexus = *nexus;
4784 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4786 lock_msg.aps.lock_flag = 1;
4788 lock_msg.aps.lock_flag = 0;
4789 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4790 sizeof(lock_msg), 0);
4791 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4792 printf("%s: APS (lock=%d) error returned from "
4793 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4794 mtx_unlock(&softc->ctl_lock);
4799 mtx_unlock(&softc->ctl_lock);
4805 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4807 struct ctl_lun *lun;
4808 struct ctl_softc *softc;
4811 softc = control_softc;
4813 mtx_lock(&softc->ctl_lock);
4815 lun = (struct ctl_lun *)be_lun->ctl_lun;
4817 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4818 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4820 mtx_unlock(&softc->ctl_lock);
4824 * Backend "memory move is complete" callback for requests that never
4825 * make it down to say RAIDCore's configuration code.
4828 ctl_config_move_done(union ctl_io *io)
4832 retval = CTL_RETVAL_COMPLETE;
4835 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4837 * XXX KDM this shouldn't happen, but what if it does?
4839 if (io->io_hdr.io_type != CTL_IO_SCSI)
4840 panic("I/O type isn't CTL_IO_SCSI!");
4842 if ((io->io_hdr.port_status == 0)
4843 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4844 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4845 io->io_hdr.status = CTL_SUCCESS;
4846 else if ((io->io_hdr.port_status != 0)
4847 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4848 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4850 * For hardware error sense keys, the sense key
4851 * specific value is defined to be a retry count,
4852 * but we use it to pass back an internal FETD
4853 * error code. XXX KDM Hopefully the FETD is only
4854 * using 16 bits for an error code, since that's
4855 * all the space we have in the sks field.
4857 ctl_set_internal_failure(&io->scsiio,
4860 io->io_hdr.port_status);
4861 free(io->scsiio.kern_data_ptr, M_CTL);
4866 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4867 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4868 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4870 * XXX KDM just assuming a single pointer here, and not a
4871 * S/G list. If we start using S/G lists for config data,
4872 * we'll need to know how to clean them up here as well.
4874 free(io->scsiio.kern_data_ptr, M_CTL);
4875 /* Hopefully the user has already set the status... */
4879 * XXX KDM now we need to continue data movement. Some
4881 * - call ctl_scsiio() again? We don't do this for data
4882 * writes, because for those at least we know ahead of
4883 * time where the write will go and how long it is. For
4884 * config writes, though, that information is largely
4885 * contained within the write itself, thus we need to
4886 * parse out the data again.
4888 * - Call some other function once the data is in?
4892 * XXX KDM call ctl_scsiio() again for now, and check flag
4893 * bits to see whether we're allocated or not.
4895 retval = ctl_scsiio(&io->scsiio);
4902 * This gets called by a backend driver when it is done with a
4903 * configuration write.
4906 ctl_config_write_done(union ctl_io *io)
4909 * If the IO_CONT flag is set, we need to call the supplied
4910 * function to continue processing the I/O, instead of completing
4913 * If there is an error, though, we don't want to keep processing.
4914 * Instead, just send status back to the initiator.
4916 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
4917 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
4918 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
4919 io->scsiio.io_cont(io);
4923 * Since a configuration write can be done for commands that actually
4924 * have data allocated, like write buffer, and commands that have
4925 * no data, like start/stop unit, we need to check here.
4927 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
4928 free(io->scsiio.kern_data_ptr, M_CTL);
4933 * SCSI release command.
4936 ctl_scsi_release(struct ctl_scsiio *ctsio)
4938 int length, longid, thirdparty_id, resv_id;
4939 struct ctl_softc *ctl_softc;
4940 struct ctl_lun *lun;
4945 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4947 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4948 ctl_softc = control_softc;
4950 switch (ctsio->cdb[0]) {
4952 struct scsi_release *cdb;
4954 cdb = (struct scsi_release *)ctsio->cdb;
4955 if ((cdb->byte2 & 0x1f) != 0) {
4956 ctl_set_invalid_field(ctsio,
4962 ctl_done((union ctl_io *)ctsio);
4963 return (CTL_RETVAL_COMPLETE);
4968 struct scsi_release_10 *cdb;
4970 cdb = (struct scsi_release_10 *)ctsio->cdb;
4972 if ((cdb->byte2 & SR10_EXTENT) != 0) {
4973 ctl_set_invalid_field(ctsio,
4979 ctl_done((union ctl_io *)ctsio);
4980 return (CTL_RETVAL_COMPLETE);
4984 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
4985 ctl_set_invalid_field(ctsio,
4991 ctl_done((union ctl_io *)ctsio);
4992 return (CTL_RETVAL_COMPLETE);
4995 if (cdb->byte2 & SR10_LONGID)
4998 thirdparty_id = cdb->thirdparty_id;
5000 resv_id = cdb->resv_id;
5001 length = scsi_2btoul(cdb->length);
5008 * XXX KDM right now, we only support LUN reservation. We don't
5009 * support 3rd party reservations, or extent reservations, which
5010 * might actually need the parameter list. If we've gotten this
5011 * far, we've got a LUN reservation. Anything else got kicked out
5012 * above. So, according to SPC, ignore the length.
5016 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5018 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5019 if (ctsio->kern_data_ptr == NULL) {
5020 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5021 ctsio->io_hdr.status = SCSI_STATUS_BUSY;
5022 ctl_done((union ctl_io *)ctsio);
5023 return (CTL_RETVAL_COMPLETE);
5025 ctsio->kern_data_len = length;
5026 ctsio->kern_total_len = length;
5027 ctsio->kern_data_resid = 0;
5028 ctsio->kern_rel_offset = 0;
5029 ctsio->kern_sg_entries = 0;
5030 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5031 ctsio->be_move_done = ctl_config_move_done;
5032 ctl_datamove((union ctl_io *)ctsio);
5034 return (CTL_RETVAL_COMPLETE);
5038 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5040 mtx_lock(&ctl_softc->ctl_lock);
5043 * According to SPC, it is not an error for an intiator to attempt
5044 * to release a reservation on a LUN that isn't reserved, or that
5045 * is reserved by another initiator. The reservation can only be
5046 * released, though, by the initiator who made it or by one of
5047 * several reset type events.
5049 if (lun->flags & CTL_LUN_RESERVED) {
5050 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5051 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5052 && (ctsio->io_hdr.nexus.targ_target.id ==
5053 lun->rsv_nexus.targ_target.id)) {
5054 lun->flags &= ~CTL_LUN_RESERVED;
5058 ctsio->scsi_status = SCSI_STATUS_OK;
5059 ctsio->io_hdr.status = CTL_SUCCESS;
5061 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5062 free(ctsio->kern_data_ptr, M_CTL);
5063 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5066 mtx_unlock(&ctl_softc->ctl_lock);
5068 ctl_done((union ctl_io *)ctsio);
5069 return (CTL_RETVAL_COMPLETE);
5073 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5075 int extent, thirdparty, longid;
5076 int resv_id, length;
5077 uint64_t thirdparty_id;
5078 struct ctl_softc *ctl_softc;
5079 struct ctl_lun *lun;
5088 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5090 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5091 ctl_softc = control_softc;
5093 switch (ctsio->cdb[0]) {
5095 struct scsi_reserve *cdb;
5097 cdb = (struct scsi_reserve *)ctsio->cdb;
5098 if ((cdb->byte2 & 0x1f) != 0) {
5099 ctl_set_invalid_field(ctsio,
5105 ctl_done((union ctl_io *)ctsio);
5106 return (CTL_RETVAL_COMPLETE);
5108 resv_id = cdb->resv_id;
5109 length = scsi_2btoul(cdb->length);
5113 struct scsi_reserve_10 *cdb;
5115 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5117 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5118 ctl_set_invalid_field(ctsio,
5124 ctl_done((union ctl_io *)ctsio);
5125 return (CTL_RETVAL_COMPLETE);
5127 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5128 ctl_set_invalid_field(ctsio,
5134 ctl_done((union ctl_io *)ctsio);
5135 return (CTL_RETVAL_COMPLETE);
5137 if (cdb->byte2 & SR10_LONGID)
5140 thirdparty_id = cdb->thirdparty_id;
5142 resv_id = cdb->resv_id;
5143 length = scsi_2btoul(cdb->length);
5149 * XXX KDM right now, we only support LUN reservation. We don't
5150 * support 3rd party reservations, or extent reservations, which
5151 * might actually need the parameter list. If we've gotten this
5152 * far, we've got a LUN reservation. Anything else got kicked out
5153 * above. So, according to SPC, ignore the length.
5157 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5159 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5160 if (ctsio->kern_data_ptr == NULL) {
5161 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5162 ctsio->io_hdr.status = SCSI_STATUS_BUSY;
5163 ctl_done((union ctl_io *)ctsio);
5164 return (CTL_RETVAL_COMPLETE);
5166 ctsio->kern_data_len = length;
5167 ctsio->kern_total_len = length;
5168 ctsio->kern_data_resid = 0;
5169 ctsio->kern_rel_offset = 0;
5170 ctsio->kern_sg_entries = 0;
5171 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5172 ctsio->be_move_done = ctl_config_move_done;
5173 ctl_datamove((union ctl_io *)ctsio);
5175 return (CTL_RETVAL_COMPLETE);
5179 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5181 mtx_lock(&ctl_softc->ctl_lock);
5182 if (lun->flags & CTL_LUN_RESERVED) {
5183 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5184 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5185 || (ctsio->io_hdr.nexus.targ_target.id !=
5186 lun->rsv_nexus.targ_target.id)) {
5187 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5188 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5193 lun->flags |= CTL_LUN_RESERVED;
5194 lun->rsv_nexus = ctsio->io_hdr.nexus;
5196 ctsio->scsi_status = SCSI_STATUS_OK;
5197 ctsio->io_hdr.status = CTL_SUCCESS;
5200 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5201 free(ctsio->kern_data_ptr, M_CTL);
5202 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5205 mtx_unlock(&ctl_softc->ctl_lock);
5207 ctl_done((union ctl_io *)ctsio);
5208 return (CTL_RETVAL_COMPLETE);
5212 ctl_start_stop(struct ctl_scsiio *ctsio)
5214 struct scsi_start_stop_unit *cdb;
5215 struct ctl_lun *lun;
5216 struct ctl_softc *ctl_softc;
5219 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5221 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5222 ctl_softc = control_softc;
5225 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5229 * We don't support the immediate bit on a stop unit. In order to
5230 * do that, we would need to code up a way to know that a stop is
5231 * pending, and hold off any new commands until it completes, one
5232 * way or another. Then we could accept or reject those commands
5233 * depending on its status. We would almost need to do the reverse
5234 * of what we do below for an immediate start -- return the copy of
5235 * the ctl_io to the FETD with status to send to the host (and to
5236 * free the copy!) and then free the original I/O once the stop
5237 * actually completes. That way, the OOA queue mechanism can work
5238 * to block commands that shouldn't proceed. Another alternative
5239 * would be to put the copy in the queue in place of the original,
5240 * and return the original back to the caller. That could be
5243 if ((cdb->byte2 & SSS_IMMED)
5244 && ((cdb->how & SSS_START) == 0)) {
5245 ctl_set_invalid_field(ctsio,
5251 ctl_done((union ctl_io *)ctsio);
5252 return (CTL_RETVAL_COMPLETE);
5256 * We don't support the power conditions field. We need to check
5257 * this prior to checking the load/eject and start/stop bits.
5259 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5260 ctl_set_invalid_field(ctsio,
5266 ctl_done((union ctl_io *)ctsio);
5267 return (CTL_RETVAL_COMPLETE);
5271 * Media isn't removable, so we can't load or eject it.
5273 if ((cdb->how & SSS_LOEJ) != 0) {
5274 ctl_set_invalid_field(ctsio,
5280 ctl_done((union ctl_io *)ctsio);
5281 return (CTL_RETVAL_COMPLETE);
5284 if ((lun->flags & CTL_LUN_PR_RESERVED)
5285 && ((cdb->how & SSS_START)==0)) {
5288 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5289 if (!lun->per_res[residx].registered
5290 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5292 ctl_set_reservation_conflict(ctsio);
5293 ctl_done((union ctl_io *)ctsio);
5294 return (CTL_RETVAL_COMPLETE);
5299 * If there is no backend on this device, we can't start or stop
5300 * it. In theory we shouldn't get any start/stop commands in the
5301 * first place at this level if the LUN doesn't have a backend.
5302 * That should get stopped by the command decode code.
5304 if (lun->backend == NULL) {
5305 ctl_set_invalid_opcode(ctsio);
5306 ctl_done((union ctl_io *)ctsio);
5307 return (CTL_RETVAL_COMPLETE);
5311 * XXX KDM Copan-specific offline behavior.
5312 * Figure out a reasonable way to port this?
5315 mtx_lock(&ctl_softc->ctl_lock);
5317 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5318 && (lun->flags & CTL_LUN_OFFLINE)) {
5320 * If the LUN is offline, and the on/offline bit isn't set,
5321 * reject the start or stop. Otherwise, let it through.
5323 mtx_unlock(&ctl_softc->ctl_lock);
5324 ctl_set_lun_not_ready(ctsio);
5325 ctl_done((union ctl_io *)ctsio);
5327 mtx_unlock(&ctl_softc->ctl_lock);
5328 #endif /* NEEDTOPORT */
5330 * This could be a start or a stop when we're online,
5331 * or a stop/offline or start/online. A start or stop when
5332 * we're offline is covered in the case above.
5335 * In the non-immediate case, we send the request to
5336 * the backend and return status to the user when
5339 * In the immediate case, we allocate a new ctl_io
5340 * to hold a copy of the request, and send that to
5341 * the backend. We then set good status on the
5342 * user's request and return it immediately.
5344 if (cdb->byte2 & SSS_IMMED) {
5345 union ctl_io *new_io;
5347 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5348 if (new_io == NULL) {
5349 ctl_set_busy(ctsio);
5350 ctl_done((union ctl_io *)ctsio);
5352 ctl_copy_io((union ctl_io *)ctsio,
5354 retval = lun->backend->config_write(new_io);
5355 ctl_set_success(ctsio);
5356 ctl_done((union ctl_io *)ctsio);
5359 retval = lun->backend->config_write(
5360 (union ctl_io *)ctsio);
5369 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5370 * we don't really do anything with the LBA and length fields if the user
5371 * passes them in. Instead we'll just flush out the cache for the entire
5375 ctl_sync_cache(struct ctl_scsiio *ctsio)
5377 struct ctl_lun *lun;
5378 struct ctl_softc *ctl_softc;
5379 uint64_t starting_lba;
5380 uint32_t block_count;
5384 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5386 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5387 ctl_softc = control_softc;
5392 switch (ctsio->cdb[0]) {
5393 case SYNCHRONIZE_CACHE: {
5394 struct scsi_sync_cache *cdb;
5395 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5397 if (cdb->byte2 & SSC_RELADR)
5400 if (cdb->byte2 & SSC_IMMED)
5403 starting_lba = scsi_4btoul(cdb->begin_lba);
5404 block_count = scsi_2btoul(cdb->lb_count);
5407 case SYNCHRONIZE_CACHE_16: {
5408 struct scsi_sync_cache_16 *cdb;
5409 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5411 if (cdb->byte2 & SSC_RELADR)
5414 if (cdb->byte2 & SSC_IMMED)
5417 starting_lba = scsi_8btou64(cdb->begin_lba);
5418 block_count = scsi_4btoul(cdb->lb_count);
5422 ctl_set_invalid_opcode(ctsio);
5423 ctl_done((union ctl_io *)ctsio);
5425 break; /* NOTREACHED */
5430 * We don't support the immediate bit. Since it's in the
5431 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5432 * commands, we can just return the same error in either
5435 ctl_set_invalid_field(ctsio,
5441 ctl_done((union ctl_io *)ctsio);
5447 * We don't support the reladr bit either. It can only be
5448 * used with linked commands, and we don't support linked
5449 * commands. Since the bit is in the same place for the
5450 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5451 * just return the same error in either case.
5453 ctl_set_invalid_field(ctsio,
5459 ctl_done((union ctl_io *)ctsio);
5464 * We check the LBA and length, but don't do anything with them.
5465 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5466 * get flushed. This check will just help satisfy anyone who wants
5467 * to see an error for an out of range LBA.
5469 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5470 ctl_set_lba_out_of_range(ctsio);
5471 ctl_done((union ctl_io *)ctsio);
5476 * If this LUN has no backend, we can't flush the cache anyway.
5478 if (lun->backend == NULL) {
5479 ctl_set_invalid_opcode(ctsio);
5480 ctl_done((union ctl_io *)ctsio);
5485 * Check to see whether we're configured to send the SYNCHRONIZE
5486 * CACHE command directly to the back end.
5488 mtx_lock(&ctl_softc->ctl_lock);
5489 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5490 && (++(lun->sync_count) >= lun->sync_interval)) {
5491 lun->sync_count = 0;
5492 mtx_unlock(&ctl_softc->ctl_lock);
5493 retval = lun->backend->config_write((union ctl_io *)ctsio);
5495 mtx_unlock(&ctl_softc->ctl_lock);
5496 ctl_set_success(ctsio);
5497 ctl_done((union ctl_io *)ctsio);
5506 ctl_format(struct ctl_scsiio *ctsio)
5508 struct scsi_format *cdb;
5509 struct ctl_lun *lun;
5510 struct ctl_softc *ctl_softc;
5511 int length, defect_list_len;
5513 CTL_DEBUG_PRINT(("ctl_format\n"));
5515 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5516 ctl_softc = control_softc;
5518 cdb = (struct scsi_format *)ctsio->cdb;
5521 if (cdb->byte2 & SF_FMTDATA) {
5522 if (cdb->byte2 & SF_LONGLIST)
5523 length = sizeof(struct scsi_format_header_long);
5525 length = sizeof(struct scsi_format_header_short);
5528 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5530 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5531 if (ctsio->kern_data_ptr == NULL) {
5532 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5533 ctsio->io_hdr.status = SCSI_STATUS_BUSY;
5534 ctl_done((union ctl_io *)ctsio);
5535 return (CTL_RETVAL_COMPLETE);
5537 ctsio->kern_data_len = length;
5538 ctsio->kern_total_len = length;
5539 ctsio->kern_data_resid = 0;
5540 ctsio->kern_rel_offset = 0;
5541 ctsio->kern_sg_entries = 0;
5542 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5543 ctsio->be_move_done = ctl_config_move_done;
5544 ctl_datamove((union ctl_io *)ctsio);
5546 return (CTL_RETVAL_COMPLETE);
5549 defect_list_len = 0;
5551 if (cdb->byte2 & SF_FMTDATA) {
5552 if (cdb->byte2 & SF_LONGLIST) {
5553 struct scsi_format_header_long *header;
5555 header = (struct scsi_format_header_long *)
5556 ctsio->kern_data_ptr;
5558 defect_list_len = scsi_4btoul(header->defect_list_len);
5559 if (defect_list_len != 0) {
5560 ctl_set_invalid_field(ctsio,
5569 struct scsi_format_header_short *header;
5571 header = (struct scsi_format_header_short *)
5572 ctsio->kern_data_ptr;
5574 defect_list_len = scsi_2btoul(header->defect_list_len);
5575 if (defect_list_len != 0) {
5576 ctl_set_invalid_field(ctsio,
5588 * The format command will clear out the "Medium format corrupted"
5589 * status if set by the configuration code. That status is really
5590 * just a way to notify the host that we have lost the media, and
5591 * get them to issue a command that will basically make them think
5592 * they're blowing away the media.
5594 mtx_lock(&ctl_softc->ctl_lock);
5595 lun->flags &= ~CTL_LUN_INOPERABLE;
5596 mtx_unlock(&ctl_softc->ctl_lock);
5598 ctsio->scsi_status = SCSI_STATUS_OK;
5599 ctsio->io_hdr.status = CTL_SUCCESS;
5602 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5603 free(ctsio->kern_data_ptr, M_CTL);
5604 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5607 ctl_done((union ctl_io *)ctsio);
5608 return (CTL_RETVAL_COMPLETE);
5612 ctl_write_buffer(struct ctl_scsiio *ctsio)
5614 struct scsi_write_buffer *cdb;
5615 struct copan_page_header *header;
5616 struct ctl_lun *lun;
5617 struct ctl_softc *ctl_softc;
5618 int buffer_offset, len;
5623 retval = CTL_RETVAL_COMPLETE;
5625 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5627 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5628 ctl_softc = control_softc;
5629 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5631 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5632 ctl_set_invalid_field(ctsio,
5638 ctl_done((union ctl_io *)ctsio);
5639 return (CTL_RETVAL_COMPLETE);
5641 if (cdb->buffer_id != 0) {
5642 ctl_set_invalid_field(ctsio,
5648 ctl_done((union ctl_io *)ctsio);
5649 return (CTL_RETVAL_COMPLETE);
5652 len = scsi_3btoul(cdb->length);
5653 buffer_offset = scsi_3btoul(cdb->offset);
5655 if (len > sizeof(lun->write_buffer)) {
5656 ctl_set_invalid_field(ctsio,
5662 ctl_done((union ctl_io *)ctsio);
5663 return (CTL_RETVAL_COMPLETE);
5666 if (buffer_offset != 0) {
5667 ctl_set_invalid_field(ctsio,
5673 ctl_done((union ctl_io *)ctsio);
5674 return (CTL_RETVAL_COMPLETE);
5678 * If we've got a kernel request that hasn't been malloced yet,
5679 * malloc it and tell the caller the data buffer is here.
5681 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5682 ctsio->kern_data_ptr = lun->write_buffer;
5683 ctsio->kern_data_len = len;
5684 ctsio->kern_total_len = len;
5685 ctsio->kern_data_resid = 0;
5686 ctsio->kern_rel_offset = 0;
5687 ctsio->kern_sg_entries = 0;
5688 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5689 ctsio->be_move_done = ctl_config_move_done;
5690 ctl_datamove((union ctl_io *)ctsio);
5692 return (CTL_RETVAL_COMPLETE);
5695 ctl_done((union ctl_io *)ctsio);
5697 return (CTL_RETVAL_COMPLETE);
5701 * Note that this function currently doesn't actually do anything inside
5702 * CTL to enforce things if the DQue bit is turned on.
5704 * Also note that this function can't be used in the default case, because
5705 * the DQue bit isn't set in the changeable mask for the control mode page
5706 * anyway. This is just here as an example for how to implement a page
5707 * handler, and a placeholder in case we want to allow the user to turn
5708 * tagged queueing on and off.
5710 * The D_SENSE bit handling is functional, however, and will turn
5711 * descriptor sense on and off for a given LUN.
5714 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5715 struct ctl_page_index *page_index, uint8_t *page_ptr)
5717 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5718 struct ctl_lun *lun;
5719 struct ctl_softc *softc;
5723 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5724 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5727 user_cp = (struct scsi_control_page *)page_ptr;
5728 current_cp = (struct scsi_control_page *)
5729 (page_index->page_data + (page_index->page_len *
5731 saved_cp = (struct scsi_control_page *)
5732 (page_index->page_data + (page_index->page_len *
5735 softc = control_softc;
5737 mtx_lock(&softc->ctl_lock);
5738 if (((current_cp->rlec & SCP_DSENSE) == 0)
5739 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5741 * Descriptor sense is currently turned off and the user
5742 * wants to turn it on.
5744 current_cp->rlec |= SCP_DSENSE;
5745 saved_cp->rlec |= SCP_DSENSE;
5746 lun->flags |= CTL_LUN_SENSE_DESC;
5748 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5749 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5751 * Descriptor sense is currently turned on, and the user
5752 * wants to turn it off.
5754 current_cp->rlec &= ~SCP_DSENSE;
5755 saved_cp->rlec &= ~SCP_DSENSE;
5756 lun->flags &= ~CTL_LUN_SENSE_DESC;
5759 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5760 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5762 csevent_log(CSC_CTL | CSC_SHELF_SW |
5764 csevent_LogType_Trace,
5765 csevent_Severity_Information,
5766 csevent_AlertLevel_Green,
5767 csevent_FRU_Firmware,
5768 csevent_FRU_Unknown,
5769 "Received untagged to untagged transition");
5770 #endif /* NEEDTOPORT */
5773 csevent_log(CSC_CTL | CSC_SHELF_SW |
5775 csevent_LogType_ConfigChange,
5776 csevent_Severity_Information,
5777 csevent_AlertLevel_Green,
5778 csevent_FRU_Firmware,
5779 csevent_FRU_Unknown,
5780 "Received untagged to tagged "
5781 "queueing transition");
5782 #endif /* NEEDTOPORT */
5784 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5785 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5789 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5791 csevent_log(CSC_CTL | CSC_SHELF_SW |
5793 csevent_LogType_ConfigChange,
5794 csevent_Severity_Warning,
5795 csevent_AlertLevel_Yellow,
5796 csevent_FRU_Firmware,
5797 csevent_FRU_Unknown,
5798 "Received tagged queueing to untagged "
5800 #endif /* NEEDTOPORT */
5802 current_cp->queue_flags |= SCP_QUEUE_DQUE;
5803 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
5807 csevent_log(CSC_CTL | CSC_SHELF_SW |
5809 csevent_LogType_Trace,
5810 csevent_Severity_Information,
5811 csevent_AlertLevel_Green,
5812 csevent_FRU_Firmware,
5813 csevent_FRU_Unknown,
5814 "Received tagged queueing to tagged "
5815 "queueing transition");
5816 #endif /* NEEDTOPORT */
5822 * Let other initiators know that the mode
5823 * parameters for this LUN have changed.
5825 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
5829 lun->pending_sense[i].ua_pending |=
5833 mtx_unlock(&softc->ctl_lock);
5839 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
5840 struct ctl_page_index *page_index, uint8_t *page_ptr)
5846 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
5847 struct ctl_page_index *page_index, int pc)
5849 struct copan_power_subpage *page;
5851 page = (struct copan_power_subpage *)page_index->page_data +
5852 (page_index->page_len * pc);
5855 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5857 * We don't update the changable bits for this page.
5860 case SMS_PAGE_CTRL_CURRENT >> 6:
5861 case SMS_PAGE_CTRL_DEFAULT >> 6:
5862 case SMS_PAGE_CTRL_SAVED >> 6:
5864 ctl_update_power_subpage(page);
5869 EPRINT(0, "Invalid PC %d!!", pc);
5878 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
5879 struct ctl_page_index *page_index, uint8_t *page_ptr)
5881 struct copan_aps_subpage *user_sp;
5882 struct copan_aps_subpage *current_sp;
5883 union ctl_modepage_info *modepage_info;
5884 struct ctl_softc *softc;
5885 struct ctl_lun *lun;
5888 retval = CTL_RETVAL_COMPLETE;
5889 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5890 (page_index->page_len * CTL_PAGE_CURRENT));
5891 softc = control_softc;
5892 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5894 user_sp = (struct copan_aps_subpage *)page_ptr;
5896 modepage_info = (union ctl_modepage_info *)
5897 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5899 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
5900 modepage_info->header.subpage = page_index->subpage;
5901 modepage_info->aps.lock_active = user_sp->lock_active;
5903 mtx_lock(&softc->ctl_lock);
5906 * If there is a request to lock the LUN and another LUN is locked
5907 * this is an error. If the requested LUN is already locked ignore
5908 * the request. If no LUN is locked attempt to lock it.
5909 * if there is a request to unlock the LUN and the LUN is currently
5910 * locked attempt to unlock it. Otherwise ignore the request. i.e.
5911 * if another LUN is locked or no LUN is locked.
5913 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
5914 if (softc->aps_locked_lun == lun->lun) {
5916 * This LUN is already locked, so we're done.
5918 retval = CTL_RETVAL_COMPLETE;
5919 } else if (softc->aps_locked_lun == 0) {
5921 * No one has the lock, pass the request to the
5924 retval = lun->backend->config_write(
5925 (union ctl_io *)ctsio);
5928 * Someone else has the lock, throw out the request.
5930 ctl_set_already_locked(ctsio);
5931 free(ctsio->kern_data_ptr, M_CTL);
5932 ctl_done((union ctl_io *)ctsio);
5935 * Set the return value so that ctl_do_mode_select()
5936 * won't try to complete the command. We already
5937 * completed it here.
5939 retval = CTL_RETVAL_ERROR;
5941 } else if (softc->aps_locked_lun == lun->lun) {
5943 * This LUN is locked, so pass the unlock request to the
5946 retval = lun->backend->config_write((union ctl_io *)ctsio);
5948 mtx_unlock(&softc->ctl_lock);
5954 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5955 struct ctl_page_index *page_index,
5961 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5966 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5967 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5968 printf("page data:");
5970 printf(" %.2x",page_ptr[i]);
5976 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5977 struct ctl_page_index *page_index,
5980 struct copan_debugconf_subpage *page;
5982 page = (struct copan_debugconf_subpage *)page_index->page_data +
5983 (page_index->page_len * pc);
5986 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5987 case SMS_PAGE_CTRL_DEFAULT >> 6:
5988 case SMS_PAGE_CTRL_SAVED >> 6:
5990 * We don't update the changable or default bits for this page.
5993 case SMS_PAGE_CTRL_CURRENT >> 6:
5994 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5995 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5999 EPRINT(0, "Invalid PC %d!!", pc);
6000 #endif /* NEEDTOPORT */
6008 ctl_do_mode_select(union ctl_io *io)
6010 struct scsi_mode_page_header *page_header;
6011 struct ctl_page_index *page_index;
6012 struct ctl_scsiio *ctsio;
6013 int control_dev, page_len;
6014 int page_len_offset, page_len_size;
6015 union ctl_modepage_info *modepage_info;
6016 struct ctl_lun *lun;
6017 int *len_left, *len_used;
6020 ctsio = &io->scsiio;
6023 retval = CTL_RETVAL_COMPLETE;
6025 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6027 if (lun->be_lun->lun_type != T_DIRECT)
6032 modepage_info = (union ctl_modepage_info *)
6033 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6034 len_left = &modepage_info->header.len_left;
6035 len_used = &modepage_info->header.len_used;
6039 page_header = (struct scsi_mode_page_header *)
6040 (ctsio->kern_data_ptr + *len_used);
6042 if (*len_left == 0) {
6043 free(ctsio->kern_data_ptr, M_CTL);
6044 ctl_set_success(ctsio);
6045 ctl_done((union ctl_io *)ctsio);
6046 return (CTL_RETVAL_COMPLETE);
6047 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6049 free(ctsio->kern_data_ptr, M_CTL);
6050 ctl_set_param_len_error(ctsio);
6051 ctl_done((union ctl_io *)ctsio);
6052 return (CTL_RETVAL_COMPLETE);
6054 } else if ((page_header->page_code & SMPH_SPF)
6055 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6057 free(ctsio->kern_data_ptr, M_CTL);
6058 ctl_set_param_len_error(ctsio);
6059 ctl_done((union ctl_io *)ctsio);
6060 return (CTL_RETVAL_COMPLETE);
6065 * XXX KDM should we do something with the block descriptor?
6067 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6069 if ((control_dev != 0)
6070 && (lun->mode_pages.index[i].page_flags &
6071 CTL_PAGE_FLAG_DISK_ONLY))
6074 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6075 (page_header->page_code & SMPH_PC_MASK))
6079 * If neither page has a subpage code, then we've got a
6082 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6083 && ((page_header->page_code & SMPH_SPF) == 0)) {
6084 page_index = &lun->mode_pages.index[i];
6085 page_len = page_header->page_length;
6090 * If both pages have subpages, then the subpage numbers
6093 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6094 && (page_header->page_code & SMPH_SPF)) {
6095 struct scsi_mode_page_header_sp *sph;
6097 sph = (struct scsi_mode_page_header_sp *)page_header;
6099 if (lun->mode_pages.index[i].subpage ==
6101 page_index = &lun->mode_pages.index[i];
6102 page_len = scsi_2btoul(sph->page_length);
6109 * If we couldn't find the page, or if we don't have a mode select
6110 * handler for it, send back an error to the user.
6112 if ((page_index == NULL)
6113 || (page_index->select_handler == NULL)) {
6114 ctl_set_invalid_field(ctsio,
6117 /*field*/ *len_used,
6120 free(ctsio->kern_data_ptr, M_CTL);
6121 ctl_done((union ctl_io *)ctsio);
6122 return (CTL_RETVAL_COMPLETE);
6125 if (page_index->page_code & SMPH_SPF) {
6126 page_len_offset = 2;
6130 page_len_offset = 1;
6134 * If the length the initiator gives us isn't the one we specify in
6135 * the mode page header, or if they didn't specify enough data in
6136 * the CDB to avoid truncating this page, kick out the request.
6138 if ((page_len != (page_index->page_len - page_len_offset -
6140 || (*len_left < page_index->page_len)) {
6143 ctl_set_invalid_field(ctsio,
6146 /*field*/ *len_used + page_len_offset,
6149 free(ctsio->kern_data_ptr, M_CTL);
6150 ctl_done((union ctl_io *)ctsio);
6151 return (CTL_RETVAL_COMPLETE);
6155 * Run through the mode page, checking to make sure that the bits
6156 * the user changed are actually legal for him to change.
6158 for (i = 0; i < page_index->page_len; i++) {
6159 uint8_t *user_byte, *change_mask, *current_byte;
6163 user_byte = (uint8_t *)page_header + i;
6164 change_mask = page_index->page_data +
6165 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6166 current_byte = page_index->page_data +
6167 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6170 * Check to see whether the user set any bits in this byte
6171 * that he is not allowed to set.
6173 if ((*user_byte & ~(*change_mask)) ==
6174 (*current_byte & ~(*change_mask)))
6178 * Go through bit by bit to determine which one is illegal.
6181 for (j = 7; j >= 0; j--) {
6182 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6183 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6188 ctl_set_invalid_field(ctsio,
6191 /*field*/ *len_used + i,
6194 free(ctsio->kern_data_ptr, M_CTL);
6195 ctl_done((union ctl_io *)ctsio);
6196 return (CTL_RETVAL_COMPLETE);
6200 * Decrement these before we call the page handler, since we may
6201 * end up getting called back one way or another before the handler
6202 * returns to this context.
6204 *len_left -= page_index->page_len;
6205 *len_used += page_index->page_len;
6207 retval = page_index->select_handler(ctsio, page_index,
6208 (uint8_t *)page_header);
6211 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6212 * wait until this queued command completes to finish processing
6213 * the mode page. If it returns anything other than
6214 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6215 * already set the sense information, freed the data pointer, and
6216 * completed the io for us.
6218 if (retval != CTL_RETVAL_COMPLETE)
6219 goto bailout_no_done;
6222 * If the initiator sent us more than one page, parse the next one.
6227 ctl_set_success(ctsio);
6228 free(ctsio->kern_data_ptr, M_CTL);
6229 ctl_done((union ctl_io *)ctsio);
6233 return (CTL_RETVAL_COMPLETE);
6238 ctl_mode_select(struct ctl_scsiio *ctsio)
6240 int param_len, pf, sp;
6241 int header_size, bd_len;
6242 int len_left, len_used;
6243 struct ctl_page_index *page_index;
6244 struct ctl_lun *lun;
6245 int control_dev, page_len;
6246 union ctl_modepage_info *modepage_info;
6258 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6260 if (lun->be_lun->lun_type != T_DIRECT)
6265 switch (ctsio->cdb[0]) {
6266 case MODE_SELECT_6: {
6267 struct scsi_mode_select_6 *cdb;
6269 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6271 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6272 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6274 param_len = cdb->length;
6275 header_size = sizeof(struct scsi_mode_header_6);
6278 case MODE_SELECT_10: {
6279 struct scsi_mode_select_10 *cdb;
6281 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6283 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6284 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6286 param_len = scsi_2btoul(cdb->length);
6287 header_size = sizeof(struct scsi_mode_header_10);
6291 ctl_set_invalid_opcode(ctsio);
6292 ctl_done((union ctl_io *)ctsio);
6293 return (CTL_RETVAL_COMPLETE);
6294 break; /* NOTREACHED */
6299 * "A parameter list length of zero indicates that the Data-Out Buffer
6300 * shall be empty. This condition shall not be considered as an error."
6302 if (param_len == 0) {
6303 ctl_set_success(ctsio);
6304 ctl_done((union ctl_io *)ctsio);
6305 return (CTL_RETVAL_COMPLETE);
6309 * Since we'll hit this the first time through, prior to
6310 * allocation, we don't need to free a data buffer here.
6312 if (param_len < header_size) {
6313 ctl_set_param_len_error(ctsio);
6314 ctl_done((union ctl_io *)ctsio);
6315 return (CTL_RETVAL_COMPLETE);
6319 * Allocate the data buffer and grab the user's data. In theory,
6320 * we shouldn't have to sanity check the parameter list length here
6321 * because the maximum size is 64K. We should be able to malloc
6322 * that much without too many problems.
6324 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6325 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6326 if (ctsio->kern_data_ptr == NULL) {
6327 ctl_set_busy(ctsio);
6328 ctl_done((union ctl_io *)ctsio);
6329 return (CTL_RETVAL_COMPLETE);
6331 ctsio->kern_data_len = param_len;
6332 ctsio->kern_total_len = param_len;
6333 ctsio->kern_data_resid = 0;
6334 ctsio->kern_rel_offset = 0;
6335 ctsio->kern_sg_entries = 0;
6336 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6337 ctsio->be_move_done = ctl_config_move_done;
6338 ctl_datamove((union ctl_io *)ctsio);
6340 return (CTL_RETVAL_COMPLETE);
6343 switch (ctsio->cdb[0]) {
6344 case MODE_SELECT_6: {
6345 struct scsi_mode_header_6 *mh6;
6347 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6348 bd_len = mh6->blk_desc_len;
6351 case MODE_SELECT_10: {
6352 struct scsi_mode_header_10 *mh10;
6354 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6355 bd_len = scsi_2btoul(mh10->blk_desc_len);
6359 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6363 if (param_len < (header_size + bd_len)) {
6364 free(ctsio->kern_data_ptr, M_CTL);
6365 ctl_set_param_len_error(ctsio);
6366 ctl_done((union ctl_io *)ctsio);
6367 return (CTL_RETVAL_COMPLETE);
6371 * Set the IO_CONT flag, so that if this I/O gets passed to
6372 * ctl_config_write_done(), it'll get passed back to
6373 * ctl_do_mode_select() for further processing, or completion if
6376 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6377 ctsio->io_cont = ctl_do_mode_select;
6379 modepage_info = (union ctl_modepage_info *)
6380 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6382 memset(modepage_info, 0, sizeof(*modepage_info));
6384 len_left = param_len - header_size - bd_len;
6385 len_used = header_size + bd_len;
6387 modepage_info->header.len_left = len_left;
6388 modepage_info->header.len_used = len_used;
6390 return (ctl_do_mode_select((union ctl_io *)ctsio));
6394 ctl_mode_sense(struct ctl_scsiio *ctsio)
6396 struct ctl_lun *lun;
6397 int pc, page_code, dbd, llba, subpage;
6398 int alloc_len, page_len, header_len, total_len;
6399 struct scsi_mode_block_descr *block_desc;
6400 struct ctl_page_index *page_index;
6408 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6410 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6412 if (lun->be_lun->lun_type != T_DIRECT)
6417 switch (ctsio->cdb[0]) {
6418 case MODE_SENSE_6: {
6419 struct scsi_mode_sense_6 *cdb;
6421 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6423 header_len = sizeof(struct scsi_mode_hdr_6);
6424 if (cdb->byte2 & SMS_DBD)
6427 header_len += sizeof(struct scsi_mode_block_descr);
6429 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6430 page_code = cdb->page & SMS_PAGE_CODE;
6431 subpage = cdb->subpage;
6432 alloc_len = cdb->length;
6435 case MODE_SENSE_10: {
6436 struct scsi_mode_sense_10 *cdb;
6438 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6440 header_len = sizeof(struct scsi_mode_hdr_10);
6442 if (cdb->byte2 & SMS_DBD)
6445 header_len += sizeof(struct scsi_mode_block_descr);
6446 if (cdb->byte2 & SMS10_LLBAA)
6448 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6449 page_code = cdb->page & SMS_PAGE_CODE;
6450 subpage = cdb->subpage;
6451 alloc_len = scsi_2btoul(cdb->length);
6455 ctl_set_invalid_opcode(ctsio);
6456 ctl_done((union ctl_io *)ctsio);
6457 return (CTL_RETVAL_COMPLETE);
6458 break; /* NOTREACHED */
6462 * We have to make a first pass through to calculate the size of
6463 * the pages that match the user's query. Then we allocate enough
6464 * memory to hold it, and actually copy the data into the buffer.
6466 switch (page_code) {
6467 case SMS_ALL_PAGES_PAGE: {
6473 * At the moment, values other than 0 and 0xff here are
6474 * reserved according to SPC-3.
6476 if ((subpage != SMS_SUBPAGE_PAGE_0)
6477 && (subpage != SMS_SUBPAGE_ALL)) {
6478 ctl_set_invalid_field(ctsio,
6484 ctl_done((union ctl_io *)ctsio);
6485 return (CTL_RETVAL_COMPLETE);
6488 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6489 if ((control_dev != 0)
6490 && (lun->mode_pages.index[i].page_flags &
6491 CTL_PAGE_FLAG_DISK_ONLY))
6495 * We don't use this subpage if the user didn't
6496 * request all subpages.
6498 if ((lun->mode_pages.index[i].subpage != 0)
6499 && (subpage == SMS_SUBPAGE_PAGE_0))
6503 printf("found page %#x len %d\n",
6504 lun->mode_pages.index[i].page_code &
6506 lun->mode_pages.index[i].page_len);
6508 page_len += lun->mode_pages.index[i].page_len;
6517 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6518 /* Look for the right page code */
6519 if ((lun->mode_pages.index[i].page_code &
6520 SMPH_PC_MASK) != page_code)
6523 /* Look for the right subpage or the subpage wildcard*/
6524 if ((lun->mode_pages.index[i].subpage != subpage)
6525 && (subpage != SMS_SUBPAGE_ALL))
6528 /* Make sure the page is supported for this dev type */
6529 if ((control_dev != 0)
6530 && (lun->mode_pages.index[i].page_flags &
6531 CTL_PAGE_FLAG_DISK_ONLY))
6535 printf("found page %#x len %d\n",
6536 lun->mode_pages.index[i].page_code &
6538 lun->mode_pages.index[i].page_len);
6541 page_len += lun->mode_pages.index[i].page_len;
6544 if (page_len == 0) {
6545 ctl_set_invalid_field(ctsio,
6551 ctl_done((union ctl_io *)ctsio);
6552 return (CTL_RETVAL_COMPLETE);
6558 total_len = header_len + page_len;
6560 printf("header_len = %d, page_len = %d, total_len = %d\n",
6561 header_len, page_len, total_len);
6564 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK);
6565 if (ctsio->kern_data_ptr == NULL) {
6566 ctsio->io_hdr.status = CTL_SCSI_ERROR;
6567 ctsio->scsi_status = SCSI_STATUS_BUSY;
6568 ctl_done((union ctl_io *)ctsio);
6569 return (CTL_RETVAL_COMPLETE);
6571 ctsio->kern_sg_entries = 0;
6572 ctsio->kern_data_resid = 0;
6573 ctsio->kern_rel_offset = 0;
6574 if (total_len < alloc_len) {
6575 ctsio->residual = alloc_len - total_len;
6576 ctsio->kern_data_len = total_len;
6577 ctsio->kern_total_len = total_len;
6579 ctsio->residual = 0;
6580 ctsio->kern_data_len = alloc_len;
6581 ctsio->kern_total_len = alloc_len;
6583 memset(ctsio->kern_data_ptr, 0, total_len);
6585 switch (ctsio->cdb[0]) {
6586 case MODE_SENSE_6: {
6587 struct scsi_mode_hdr_6 *header;
6589 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6591 header->datalen = ctl_min(total_len - 1, 254);
6594 header->block_descr_len = 0;
6596 header->block_descr_len =
6597 sizeof(struct scsi_mode_block_descr);
6598 block_desc = (struct scsi_mode_block_descr *)&header[1];
6601 case MODE_SENSE_10: {
6602 struct scsi_mode_hdr_10 *header;
6605 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6607 datalen = ctl_min(total_len - 2, 65533);
6608 scsi_ulto2b(datalen, header->datalen);
6610 scsi_ulto2b(0, header->block_descr_len);
6612 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6613 header->block_descr_len);
6614 block_desc = (struct scsi_mode_block_descr *)&header[1];
6618 panic("invalid CDB type %#x", ctsio->cdb[0]);
6619 break; /* NOTREACHED */
6623 * If we've got a disk, use its blocksize in the block
6624 * descriptor. Otherwise, just set it to 0.
6627 if (control_dev != 0)
6628 scsi_ulto3b(lun->be_lun->blocksize,
6629 block_desc->block_len);
6631 scsi_ulto3b(0, block_desc->block_len);
6634 switch (page_code) {
6635 case SMS_ALL_PAGES_PAGE: {
6638 data_used = header_len;
6639 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6640 struct ctl_page_index *page_index;
6642 page_index = &lun->mode_pages.index[i];
6644 if ((control_dev != 0)
6645 && (page_index->page_flags &
6646 CTL_PAGE_FLAG_DISK_ONLY))
6650 * We don't use this subpage if the user didn't
6651 * request all subpages. We already checked (above)
6652 * to make sure the user only specified a subpage
6653 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6655 if ((page_index->subpage != 0)
6656 && (subpage == SMS_SUBPAGE_PAGE_0))
6660 * Call the handler, if it exists, to update the
6661 * page to the latest values.
6663 if (page_index->sense_handler != NULL)
6664 page_index->sense_handler(ctsio, page_index,pc);
6666 memcpy(ctsio->kern_data_ptr + data_used,
6667 page_index->page_data +
6668 (page_index->page_len * pc),
6669 page_index->page_len);
6670 data_used += page_index->page_len;
6677 data_used = header_len;
6679 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6680 struct ctl_page_index *page_index;
6682 page_index = &lun->mode_pages.index[i];
6684 /* Look for the right page code */
6685 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6688 /* Look for the right subpage or the subpage wildcard*/
6689 if ((page_index->subpage != subpage)
6690 && (subpage != SMS_SUBPAGE_ALL))
6693 /* Make sure the page is supported for this dev type */
6694 if ((control_dev != 0)
6695 && (page_index->page_flags &
6696 CTL_PAGE_FLAG_DISK_ONLY))
6700 * Call the handler, if it exists, to update the
6701 * page to the latest values.
6703 if (page_index->sense_handler != NULL)
6704 page_index->sense_handler(ctsio, page_index,pc);
6706 memcpy(ctsio->kern_data_ptr + data_used,
6707 page_index->page_data +
6708 (page_index->page_len * pc),
6709 page_index->page_len);
6710 data_used += page_index->page_len;
6716 ctsio->scsi_status = SCSI_STATUS_OK;
6718 ctsio->be_move_done = ctl_config_move_done;
6719 ctl_datamove((union ctl_io *)ctsio);
6721 return (CTL_RETVAL_COMPLETE);
6725 ctl_read_capacity(struct ctl_scsiio *ctsio)
6727 struct scsi_read_capacity *cdb;
6728 struct scsi_read_capacity_data *data;
6729 struct ctl_lun *lun;
6732 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6734 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6736 lba = scsi_4btoul(cdb->addr);
6737 if (((cdb->pmi & SRC_PMI) == 0)
6739 ctl_set_invalid_field(/*ctsio*/ ctsio,
6745 ctl_done((union ctl_io *)ctsio);
6746 return (CTL_RETVAL_COMPLETE);
6749 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6751 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK);
6752 if (ctsio->kern_data_ptr == NULL) {
6753 ctsio->io_hdr.status = CTL_SCSI_ERROR;
6754 ctsio->scsi_status = SCSI_STATUS_BUSY;
6755 ctl_done((union ctl_io *)ctsio);
6756 return (CTL_RETVAL_COMPLETE);
6758 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6759 ctsio->residual = 0;
6760 ctsio->kern_data_len = sizeof(*data);
6761 ctsio->kern_total_len = sizeof(*data);
6762 ctsio->kern_data_resid = 0;
6763 ctsio->kern_rel_offset = 0;
6764 ctsio->kern_sg_entries = 0;
6766 memset(data, 0, sizeof(*data));
6769 * If the maximum LBA is greater than 0xfffffffe, the user must
6770 * issue a SERVICE ACTION IN (16) command, with the read capacity
6771 * serivce action set.
6773 if (lun->be_lun->maxlba > 0xfffffffe)
6774 scsi_ulto4b(0xffffffff, data->addr);
6776 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6779 * XXX KDM this may not be 512 bytes...
6781 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6783 ctsio->scsi_status = SCSI_STATUS_OK;
6785 ctsio->be_move_done = ctl_config_move_done;
6786 ctl_datamove((union ctl_io *)ctsio);
6788 return (CTL_RETVAL_COMPLETE);
6792 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6794 struct scsi_read_capacity_16 *cdb;
6795 struct scsi_read_capacity_data_long *data;
6796 struct ctl_lun *lun;
6800 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6802 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6804 alloc_len = scsi_4btoul(cdb->alloc_len);
6805 lba = scsi_8btou64(cdb->addr);
6807 if ((cdb->reladr & SRC16_PMI)
6809 ctl_set_invalid_field(/*ctsio*/ ctsio,
6815 ctl_done((union ctl_io *)ctsio);
6816 return (CTL_RETVAL_COMPLETE);
6819 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6821 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK);
6822 if (ctsio->kern_data_ptr == NULL) {
6823 ctsio->io_hdr.status = CTL_SCSI_ERROR;
6824 ctsio->scsi_status = SCSI_STATUS_BUSY;
6825 ctl_done((union ctl_io *)ctsio);
6826 return (CTL_RETVAL_COMPLETE);
6828 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6830 if (sizeof(*data) < alloc_len) {
6831 ctsio->residual = alloc_len - sizeof(*data);
6832 ctsio->kern_data_len = sizeof(*data);
6833 ctsio->kern_total_len = sizeof(*data);
6835 ctsio->residual = 0;
6836 ctsio->kern_data_len = alloc_len;
6837 ctsio->kern_total_len = alloc_len;
6839 ctsio->kern_data_resid = 0;
6840 ctsio->kern_rel_offset = 0;
6841 ctsio->kern_sg_entries = 0;
6843 memset(data, 0, sizeof(*data));
6845 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6846 /* XXX KDM this may not be 512 bytes... */
6847 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6849 ctsio->scsi_status = SCSI_STATUS_OK;
6851 ctsio->be_move_done = ctl_config_move_done;
6852 ctl_datamove((union ctl_io *)ctsio);
6854 return (CTL_RETVAL_COMPLETE);
6858 ctl_service_action_in(struct ctl_scsiio *ctsio)
6860 struct scsi_service_action_in *cdb;
6863 CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
6865 cdb = (struct scsi_service_action_in *)ctsio->cdb;
6867 retval = CTL_RETVAL_COMPLETE;
6869 switch (cdb->service_action) {
6870 case SRC16_SERVICE_ACTION:
6871 retval = ctl_read_capacity_16(ctsio);
6874 ctl_set_invalid_field(/*ctsio*/ ctsio,
6880 ctl_done((union ctl_io *)ctsio);
6888 ctl_maintenance_in(struct ctl_scsiio *ctsio)
6890 struct scsi_maintenance_in *cdb;
6892 int alloc_len, total_len = 0;
6893 int num_target_port_groups;
6894 struct ctl_lun *lun;
6895 struct ctl_softc *softc;
6896 struct scsi_target_group_data *rtg_ptr;
6897 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
6898 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
6899 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
6901 CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
6903 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
6904 softc = control_softc;
6905 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6907 retval = CTL_RETVAL_COMPLETE;
6908 mtx_lock(&softc->ctl_lock);
6910 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
6911 ctl_set_invalid_field(/*ctsio*/ ctsio,
6917 ctl_done((union ctl_io *)ctsio);
6922 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
6924 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
6926 total_len = sizeof(struct scsi_target_group_data) +
6927 sizeof(struct scsi_target_port_group_descriptor) *
6928 num_target_port_groups +
6929 sizeof(struct scsi_target_port_descriptor) *
6930 NUM_PORTS_PER_GRP * num_target_port_groups;
6932 alloc_len = scsi_4btoul(cdb->length);
6934 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK);
6935 if (ctsio->kern_data_ptr == NULL) {
6936 ctsio->io_hdr.status = CTL_SCSI_ERROR;
6937 ctsio->scsi_status = SCSI_STATUS_BUSY;
6938 ctl_done((union ctl_io *)ctsio);
6939 return (CTL_RETVAL_COMPLETE);
6941 memset(ctsio->kern_data_ptr, 0, total_len);
6943 ctsio->kern_sg_entries = 0;
6945 if (total_len < alloc_len) {
6946 ctsio->residual = alloc_len - total_len;
6947 ctsio->kern_data_len = total_len;
6948 ctsio->kern_total_len = total_len;
6950 ctsio->residual = 0;
6951 ctsio->kern_data_len = alloc_len;
6952 ctsio->kern_total_len = alloc_len;
6954 ctsio->kern_data_resid = 0;
6955 ctsio->kern_rel_offset = 0;
6957 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
6959 tpg_desc_ptr1 = &rtg_ptr->groups[0];
6960 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
6961 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
6962 &tp_desc_ptr1_1->desc_list[0];
6966 if (ctl_is_single == 0) {
6967 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
6968 &tp_desc_ptr1_2->desc_list[0];
6969 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
6970 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
6971 &tp_desc_ptr2_1->desc_list[0];
6973 tpg_desc_ptr2 = NULL;
6974 tp_desc_ptr2_1 = NULL;
6975 tp_desc_ptr2_2 = NULL;
6978 scsi_ulto4b(total_len - 4, rtg_ptr->length);
6979 if (ctl_is_single == 0) {
6980 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
6981 if (lun->flags & CTL_LUN_PRIMARY_SC) {
6982 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
6983 tpg_desc_ptr2->pref_state =
6984 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
6986 tpg_desc_ptr1->pref_state =
6987 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
6988 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
6991 if (lun->flags & CTL_LUN_PRIMARY_SC) {
6992 tpg_desc_ptr1->pref_state =
6993 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
6994 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
6996 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
6997 tpg_desc_ptr2->pref_state =
6998 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7002 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7004 tpg_desc_ptr1->support = 0;
7005 tpg_desc_ptr1->target_port_group[1] = 1;
7006 tpg_desc_ptr1->status = TPG_IMPLICIT;
7007 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7009 if (ctl_is_single == 0) {
7010 tpg_desc_ptr2->support = 0;
7011 tpg_desc_ptr2->target_port_group[1] = 2;
7012 tpg_desc_ptr2->status = TPG_IMPLICIT;
7013 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7015 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7016 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7018 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7019 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7021 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7022 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7023 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7025 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7026 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7030 mtx_unlock(&softc->ctl_lock);
7032 ctsio->be_move_done = ctl_config_move_done;
7034 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7035 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7036 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7037 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7038 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7040 ctl_datamove((union ctl_io *)ctsio);
7045 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7047 struct scsi_per_res_in *cdb;
7048 int alloc_len, total_len = 0;
7049 /* struct scsi_per_res_in_rsrv in_data; */
7050 struct ctl_lun *lun;
7051 struct ctl_softc *softc;
7053 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7055 softc = control_softc;
7057 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7059 alloc_len = scsi_2btoul(cdb->length);
7061 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7064 mtx_lock(&softc->ctl_lock);
7065 switch (cdb->action) {
7066 case SPRI_RK: /* read keys */
7067 total_len = sizeof(struct scsi_per_res_in_keys) +
7069 sizeof(struct scsi_per_res_key);
7071 case SPRI_RR: /* read reservation */
7072 if (lun->flags & CTL_LUN_PR_RESERVED)
7073 total_len = sizeof(struct scsi_per_res_in_rsrv);
7075 total_len = sizeof(struct scsi_per_res_in_header);
7077 case SPRI_RC: /* report capabilities */
7078 total_len = sizeof(struct scsi_per_res_cap);
7080 case SPRI_RS: /* read full status */
7082 mtx_unlock(&softc->ctl_lock);
7083 ctl_set_invalid_field(ctsio,
7089 ctl_done((union ctl_io *)ctsio);
7090 return (CTL_RETVAL_COMPLETE);
7091 break; /* NOTREACHED */
7093 mtx_unlock(&softc->ctl_lock);
7095 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK);
7096 if (ctsio->kern_data_ptr == NULL) {
7097 ctsio->io_hdr.status = CTL_SCSI_ERROR;
7098 ctsio->scsi_status = SCSI_STATUS_BUSY;
7099 ctl_done((union ctl_io *)ctsio);
7100 return (CTL_RETVAL_COMPLETE);
7103 if (total_len < alloc_len) {
7104 ctsio->residual = alloc_len - total_len;
7105 ctsio->kern_data_len = total_len;
7106 ctsio->kern_total_len = total_len;
7108 ctsio->residual = 0;
7109 ctsio->kern_data_len = alloc_len;
7110 ctsio->kern_total_len = alloc_len;
7113 ctsio->kern_data_resid = 0;
7114 ctsio->kern_rel_offset = 0;
7115 ctsio->kern_sg_entries = 0;
7117 memset(ctsio->kern_data_ptr, 0, total_len);
7119 mtx_lock(&softc->ctl_lock);
7120 switch (cdb->action) {
7121 case SPRI_RK: { // read keys
7122 struct scsi_per_res_in_keys *res_keys;
7125 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7128 * We had to drop the lock to allocate our buffer, which
7129 * leaves time for someone to come in with another
7130 * persistent reservation. (That is unlikely, though,
7131 * since this should be the only persistent reservation
7132 * command active right now.)
7134 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7135 (lun->pr_key_count *
7136 sizeof(struct scsi_per_res_key)))){
7137 mtx_unlock(&softc->ctl_lock);
7138 free(ctsio->kern_data_ptr, M_CTL);
7139 printf("%s: reservation length changed, retrying\n",
7144 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7146 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7147 lun->pr_key_count, res_keys->header.length);
7149 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7150 if (!lun->per_res[i].registered)
7154 * We used lun->pr_key_count to calculate the
7155 * size to allocate. If it turns out the number of
7156 * initiators with the registered flag set is
7157 * larger than that (i.e. they haven't been kept in
7158 * sync), we've got a problem.
7160 if (key_count >= lun->pr_key_count) {
7162 csevent_log(CSC_CTL | CSC_SHELF_SW |
7164 csevent_LogType_Fault,
7165 csevent_AlertLevel_Yellow,
7166 csevent_FRU_ShelfController,
7167 csevent_FRU_Firmware,
7168 csevent_FRU_Unknown,
7169 "registered keys %d >= key "
7170 "count %d", key_count,
7176 memcpy(res_keys->keys[key_count].key,
7177 lun->per_res[i].res_key.key,
7178 ctl_min(sizeof(res_keys->keys[key_count].key),
7179 sizeof(lun->per_res[i].res_key)));
7184 case SPRI_RR: { // read reservation
7185 struct scsi_per_res_in_rsrv *res;
7186 int tmp_len, header_only;
7188 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7190 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7192 if (lun->flags & CTL_LUN_PR_RESERVED)
7194 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7195 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7196 res->header.length);
7199 tmp_len = sizeof(struct scsi_per_res_in_header);
7200 scsi_ulto4b(0, res->header.length);
7205 * We had to drop the lock to allocate our buffer, which
7206 * leaves time for someone to come in with another
7207 * persistent reservation. (That is unlikely, though,
7208 * since this should be the only persistent reservation
7209 * command active right now.)
7211 if (tmp_len != total_len) {
7212 mtx_unlock(&softc->ctl_lock);
7213 free(ctsio->kern_data_ptr, M_CTL);
7214 printf("%s: reservation status changed, retrying\n",
7220 * No reservation held, so we're done.
7222 if (header_only != 0)
7226 * If the registration is an All Registrants type, the key
7227 * is 0, since it doesn't really matter.
7229 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7230 memcpy(res->data.reservation,
7231 &lun->per_res[lun->pr_res_idx].res_key,
7232 sizeof(struct scsi_per_res_key));
7234 res->data.scopetype = lun->res_type;
7237 case SPRI_RC: //report capabilities
7239 struct scsi_per_res_cap *res_cap;
7242 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7243 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7244 res_cap->flags2 |= SPRI_TMV;
7245 type_mask = SPRI_TM_WR_EX_AR |
7251 scsi_ulto2b(type_mask, res_cap->type_mask);
7254 case SPRI_RS: //read full status
7257 * This is a bug, because we just checked for this above,
7258 * and should have returned an error.
7260 panic("Invalid PR type %x", cdb->action);
7261 break; /* NOTREACHED */
7263 mtx_unlock(&softc->ctl_lock);
7265 ctsio->be_move_done = ctl_config_move_done;
7267 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7268 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7269 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7270 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7271 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7273 ctl_datamove((union ctl_io *)ctsio);
7275 return (CTL_RETVAL_COMPLETE);
7279 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7283 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7284 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7285 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7286 struct scsi_per_res_out_parms* param)
7288 union ctl_ha_msg persis_io;
7294 if (sa_res_key == 0) {
7295 mtx_lock(&softc->ctl_lock);
7296 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7297 /* validate scope and type */
7298 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7300 mtx_unlock(&softc->ctl_lock);
7301 ctl_set_invalid_field(/*ctsio*/ ctsio,
7307 ctl_done((union ctl_io *)ctsio);
7311 if (type>8 || type==2 || type==4 || type==0) {
7312 mtx_unlock(&softc->ctl_lock);
7313 ctl_set_invalid_field(/*ctsio*/ ctsio,
7319 ctl_done((union ctl_io *)ctsio);
7323 /* temporarily unregister this nexus */
7324 lun->per_res[residx].registered = 0;
7327 * Unregister everybody else and build UA for
7330 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7331 if (lun->per_res[i].registered == 0)
7335 && i <CTL_MAX_INITIATORS)
7336 lun->pending_sense[i].ua_pending |=
7338 else if (persis_offset
7339 && i >= persis_offset)
7340 lun->pending_sense[i-persis_offset
7343 lun->per_res[i].registered = 0;
7344 memset(&lun->per_res[i].res_key, 0,
7345 sizeof(struct scsi_per_res_key));
7347 lun->per_res[residx].registered = 1;
7348 lun->pr_key_count = 1;
7349 lun->res_type = type;
7350 if (lun->res_type != SPR_TYPE_WR_EX_AR
7351 && lun->res_type != SPR_TYPE_EX_AC_AR)
7352 lun->pr_res_idx = residx;
7354 mtx_unlock(&softc->ctl_lock);
7355 /* send msg to other side */
7356 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7357 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7358 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7359 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7360 persis_io.pr.pr_info.res_type = type;
7361 memcpy(persis_io.pr.pr_info.sa_res_key,
7362 param->serv_act_res_key,
7363 sizeof(param->serv_act_res_key));
7364 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7365 &persis_io, sizeof(persis_io), 0)) >
7366 CTL_HA_STATUS_SUCCESS) {
7367 printf("CTL:Persis Out error returned "
7368 "from ctl_ha_msg_send %d\n",
7372 /* not all registrants */
7373 mtx_unlock(&softc->ctl_lock);
7374 free(ctsio->kern_data_ptr, M_CTL);
7375 ctl_set_invalid_field(ctsio,
7381 ctl_done((union ctl_io *)ctsio);
7384 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7385 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7388 mtx_lock(&softc->ctl_lock);
7389 if (res_key == sa_res_key) {
7392 * The spec implies this is not good but doesn't
7393 * say what to do. There are two choices either
7394 * generate a res conflict or check condition
7395 * with illegal field in parameter data. Since
7396 * that is what is done when the sa_res_key is
7397 * zero I'll take that approach since this has
7398 * to do with the sa_res_key.
7400 mtx_unlock(&softc->ctl_lock);
7401 free(ctsio->kern_data_ptr, M_CTL);
7402 ctl_set_invalid_field(ctsio,
7408 ctl_done((union ctl_io *)ctsio);
7412 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7413 if (lun->per_res[i].registered
7414 && memcmp(param->serv_act_res_key,
7415 lun->per_res[i].res_key.key,
7416 sizeof(struct scsi_per_res_key)) != 0)
7420 lun->per_res[i].registered = 0;
7421 memset(&lun->per_res[i].res_key, 0,
7422 sizeof(struct scsi_per_res_key));
7423 lun->pr_key_count--;
7426 && i < CTL_MAX_INITIATORS)
7427 lun->pending_sense[i].ua_pending |=
7429 else if (persis_offset
7430 && i >= persis_offset)
7431 lun->pending_sense[i-persis_offset].ua_pending|=
7434 mtx_unlock(&softc->ctl_lock);
7436 free(ctsio->kern_data_ptr, M_CTL);
7437 ctl_set_reservation_conflict(ctsio);
7438 ctl_done((union ctl_io *)ctsio);
7439 return (CTL_RETVAL_COMPLETE);
7441 /* send msg to other side */
7442 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7443 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7444 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7445 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7446 persis_io.pr.pr_info.res_type = type;
7447 memcpy(persis_io.pr.pr_info.sa_res_key,
7448 param->serv_act_res_key,
7449 sizeof(param->serv_act_res_key));
7450 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7451 &persis_io, sizeof(persis_io), 0)) >
7452 CTL_HA_STATUS_SUCCESS) {
7453 printf("CTL:Persis Out error returned from "
7454 "ctl_ha_msg_send %d\n", isc_retval);
7457 /* Reserved but not all registrants */
7458 /* sa_res_key is res holder */
7459 if (memcmp(param->serv_act_res_key,
7460 lun->per_res[lun->pr_res_idx].res_key.key,
7461 sizeof(struct scsi_per_res_key)) == 0) {
7462 /* validate scope and type */
7463 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7465 ctl_set_invalid_field(/*ctsio*/ ctsio,
7471 ctl_done((union ctl_io *)ctsio);
7475 if (type>8 || type==2 || type==4 || type==0) {
7476 ctl_set_invalid_field(/*ctsio*/ ctsio,
7482 ctl_done((union ctl_io *)ctsio);
7488 * if sa_res_key != res_key remove all
7489 * registrants w/sa_res_key and generate UA
7490 * for these registrants(Registrations
7491 * Preempted) if it wasn't an exclusive
7492 * reservation generate UA(Reservations
7493 * Preempted) for all other registered nexuses
7494 * if the type has changed. Establish the new
7495 * reservation and holder. If res_key and
7496 * sa_res_key are the same do the above
7497 * except don't unregister the res holder.
7501 * Temporarily unregister so it won't get
7502 * removed or UA generated
7504 lun->per_res[residx].registered = 0;
7505 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7506 if (lun->per_res[i].registered == 0)
7509 if (memcmp(param->serv_act_res_key,
7510 lun->per_res[i].res_key.key,
7511 sizeof(struct scsi_per_res_key)) == 0) {
7512 lun->per_res[i].registered = 0;
7513 memset(&lun->per_res[i].res_key,
7515 sizeof(struct scsi_per_res_key));
7516 lun->pr_key_count--;
7519 && i < CTL_MAX_INITIATORS)
7520 lun->pending_sense[i
7523 else if (persis_offset
7524 && i >= persis_offset)
7526 i-persis_offset].ua_pending |=
7528 } else if (type != lun->res_type
7529 && (lun->res_type == SPR_TYPE_WR_EX_RO
7530 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7532 && i < CTL_MAX_INITIATORS)
7533 lun->pending_sense[i
7536 else if (persis_offset
7537 && i >= persis_offset)
7544 lun->per_res[residx].registered = 1;
7545 lun->res_type = type;
7546 if (lun->res_type != SPR_TYPE_WR_EX_AR
7547 && lun->res_type != SPR_TYPE_EX_AC_AR)
7548 lun->pr_res_idx = residx;
7551 CTL_PR_ALL_REGISTRANTS;
7553 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7554 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7555 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7556 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7557 persis_io.pr.pr_info.res_type = type;
7558 memcpy(persis_io.pr.pr_info.sa_res_key,
7559 param->serv_act_res_key,
7560 sizeof(param->serv_act_res_key));
7561 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7562 &persis_io, sizeof(persis_io), 0)) >
7563 CTL_HA_STATUS_SUCCESS) {
7564 printf("CTL:Persis Out error returned "
7565 "from ctl_ha_msg_send %d\n",
7570 * sa_res_key is not the res holder just
7571 * remove registrants
7574 mtx_lock(&softc->ctl_lock);
7576 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7577 if (memcmp(param->serv_act_res_key,
7578 lun->per_res[i].res_key.key,
7579 sizeof(struct scsi_per_res_key)) != 0)
7583 lun->per_res[i].registered = 0;
7584 memset(&lun->per_res[i].res_key, 0,
7585 sizeof(struct scsi_per_res_key));
7586 lun->pr_key_count--;
7589 && i < CTL_MAX_INITIATORS)
7590 lun->pending_sense[i].ua_pending |=
7592 else if (persis_offset
7593 && i >= persis_offset)
7595 i-persis_offset].ua_pending |=
7600 mtx_unlock(&softc->ctl_lock);
7601 free(ctsio->kern_data_ptr, M_CTL);
7602 ctl_set_reservation_conflict(ctsio);
7603 ctl_done((union ctl_io *)ctsio);
7606 mtx_unlock(&softc->ctl_lock);
7607 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7608 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7609 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7610 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7611 persis_io.pr.pr_info.res_type = type;
7612 memcpy(persis_io.pr.pr_info.sa_res_key,
7613 param->serv_act_res_key,
7614 sizeof(param->serv_act_res_key));
7615 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7616 &persis_io, sizeof(persis_io), 0)) >
7617 CTL_HA_STATUS_SUCCESS) {
7618 printf("CTL:Persis Out error returned "
7619 "from ctl_ha_msg_send %d\n",
7625 lun->PRGeneration++;
7631 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7635 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7636 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7637 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7638 msg->pr.pr_info.sa_res_key,
7639 sizeof(struct scsi_per_res_key)) != 0) {
7640 uint64_t sa_res_key;
7641 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7643 if (sa_res_key == 0) {
7644 /* temporarily unregister this nexus */
7645 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7648 * Unregister everybody else and build UA for
7651 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7652 if (lun->per_res[i].registered == 0)
7656 && i < CTL_MAX_INITIATORS)
7657 lun->pending_sense[i].ua_pending |=
7659 else if (persis_offset && i >= persis_offset)
7660 lun->pending_sense[i -
7661 persis_offset].ua_pending |=
7663 lun->per_res[i].registered = 0;
7664 memset(&lun->per_res[i].res_key, 0,
7665 sizeof(struct scsi_per_res_key));
7668 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7669 lun->pr_key_count = 1;
7670 lun->res_type = msg->pr.pr_info.res_type;
7671 if (lun->res_type != SPR_TYPE_WR_EX_AR
7672 && lun->res_type != SPR_TYPE_EX_AC_AR)
7673 lun->pr_res_idx = msg->pr.pr_info.residx;
7675 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7676 if (memcmp(msg->pr.pr_info.sa_res_key,
7677 lun->per_res[i].res_key.key,
7678 sizeof(struct scsi_per_res_key)) != 0)
7681 lun->per_res[i].registered = 0;
7682 memset(&lun->per_res[i].res_key, 0,
7683 sizeof(struct scsi_per_res_key));
7684 lun->pr_key_count--;
7687 && i < persis_offset)
7688 lun->pending_sense[i].ua_pending |=
7690 else if (persis_offset
7691 && i >= persis_offset)
7692 lun->pending_sense[i -
7693 persis_offset].ua_pending |=
7699 * Temporarily unregister so it won't get removed
7702 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7703 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7704 if (lun->per_res[i].registered == 0)
7707 if (memcmp(msg->pr.pr_info.sa_res_key,
7708 lun->per_res[i].res_key.key,
7709 sizeof(struct scsi_per_res_key)) == 0) {
7710 lun->per_res[i].registered = 0;
7711 memset(&lun->per_res[i].res_key, 0,
7712 sizeof(struct scsi_per_res_key));
7713 lun->pr_key_count--;
7715 && i < CTL_MAX_INITIATORS)
7716 lun->pending_sense[i].ua_pending |=
7718 else if (persis_offset
7719 && i >= persis_offset)
7720 lun->pending_sense[i -
7721 persis_offset].ua_pending |=
7723 } else if (msg->pr.pr_info.res_type != lun->res_type
7724 && (lun->res_type == SPR_TYPE_WR_EX_RO
7725 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7727 && i < persis_offset)
7728 lun->pending_sense[i
7731 else if (persis_offset
7732 && i >= persis_offset)
7733 lun->pending_sense[i -
7734 persis_offset].ua_pending |=
7738 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7739 lun->res_type = msg->pr.pr_info.res_type;
7740 if (lun->res_type != SPR_TYPE_WR_EX_AR
7741 && lun->res_type != SPR_TYPE_EX_AC_AR)
7742 lun->pr_res_idx = msg->pr.pr_info.residx;
7744 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7746 lun->PRGeneration++;
7752 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
7756 u_int32_t param_len;
7757 struct scsi_per_res_out *cdb;
7758 struct ctl_lun *lun;
7759 struct scsi_per_res_out_parms* param;
7760 struct ctl_softc *softc;
7762 uint64_t res_key, sa_res_key;
7764 union ctl_ha_msg persis_io;
7767 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
7769 retval = CTL_RETVAL_COMPLETE;
7771 softc = control_softc;
7773 cdb = (struct scsi_per_res_out *)ctsio->cdb;
7774 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7777 * We only support whole-LUN scope. The scope & type are ignored for
7778 * register, register and ignore existing key and clear.
7779 * We sometimes ignore scope and type on preempts too!!
7780 * Verify reservation type here as well.
7782 type = cdb->scope_type & SPR_TYPE_MASK;
7783 if ((cdb->action == SPRO_RESERVE)
7784 || (cdb->action == SPRO_RELEASE)) {
7785 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
7786 ctl_set_invalid_field(/*ctsio*/ ctsio,
7792 ctl_done((union ctl_io *)ctsio);
7793 return (CTL_RETVAL_COMPLETE);
7796 if (type>8 || type==2 || type==4 || type==0) {
7797 ctl_set_invalid_field(/*ctsio*/ ctsio,
7803 ctl_done((union ctl_io *)ctsio);
7804 return (CTL_RETVAL_COMPLETE);
7808 switch (cdb->action & SPRO_ACTION_MASK) {
7819 ctl_set_invalid_field(/*ctsio*/ ctsio,
7825 ctl_done((union ctl_io *)ctsio);
7826 return (CTL_RETVAL_COMPLETE);
7827 break; /* NOTREACHED */
7830 param_len = scsi_4btoul(cdb->length);
7832 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
7833 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
7834 if (ctsio->kern_data_ptr == NULL) {
7835 ctl_set_busy(ctsio);
7836 ctl_done((union ctl_io *)ctsio);
7837 return (CTL_RETVAL_COMPLETE);
7839 ctsio->kern_data_len = param_len;
7840 ctsio->kern_total_len = param_len;
7841 ctsio->kern_data_resid = 0;
7842 ctsio->kern_rel_offset = 0;
7843 ctsio->kern_sg_entries = 0;
7844 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7845 ctsio->be_move_done = ctl_config_move_done;
7846 ctl_datamove((union ctl_io *)ctsio);
7848 return (CTL_RETVAL_COMPLETE);
7851 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
7853 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
7854 res_key = scsi_8btou64(param->res_key.key);
7855 sa_res_key = scsi_8btou64(param->serv_act_res_key);
7858 * Validate the reservation key here except for SPRO_REG_IGNO
7859 * This must be done for all other service actions
7861 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
7862 mtx_lock(&softc->ctl_lock);
7863 if (lun->per_res[residx].registered) {
7864 if (memcmp(param->res_key.key,
7865 lun->per_res[residx].res_key.key,
7866 ctl_min(sizeof(param->res_key),
7867 sizeof(lun->per_res[residx].res_key))) != 0) {
7869 * The current key passed in doesn't match
7870 * the one the initiator previously
7873 mtx_unlock(&softc->ctl_lock);
7874 free(ctsio->kern_data_ptr, M_CTL);
7875 ctl_set_reservation_conflict(ctsio);
7876 ctl_done((union ctl_io *)ctsio);
7877 return (CTL_RETVAL_COMPLETE);
7879 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
7881 * We are not registered
7883 mtx_unlock(&softc->ctl_lock);
7884 free(ctsio->kern_data_ptr, M_CTL);
7885 ctl_set_reservation_conflict(ctsio);
7886 ctl_done((union ctl_io *)ctsio);
7887 return (CTL_RETVAL_COMPLETE);
7888 } else if (res_key != 0) {
7890 * We are not registered and trying to register but
7891 * the register key isn't zero.
7893 mtx_unlock(&softc->ctl_lock);
7894 free(ctsio->kern_data_ptr, M_CTL);
7895 ctl_set_reservation_conflict(ctsio);
7896 ctl_done((union ctl_io *)ctsio);
7897 return (CTL_RETVAL_COMPLETE);
7899 mtx_unlock(&softc->ctl_lock);
7902 switch (cdb->action & SPRO_ACTION_MASK) {
7904 case SPRO_REG_IGNO: {
7907 printf("Registration received\n");
7911 * We don't support any of these options, as we report in
7912 * the read capabilities request (see
7913 * ctl_persistent_reserve_in(), above).
7915 if ((param->flags & SPR_SPEC_I_PT)
7916 || (param->flags & SPR_ALL_TG_PT)
7917 || (param->flags & SPR_APTPL)) {
7920 if (param->flags & SPR_APTPL)
7922 else if (param->flags & SPR_ALL_TG_PT)
7924 else /* SPR_SPEC_I_PT */
7927 free(ctsio->kern_data_ptr, M_CTL);
7928 ctl_set_invalid_field(ctsio,
7934 ctl_done((union ctl_io *)ctsio);
7935 return (CTL_RETVAL_COMPLETE);
7938 mtx_lock(&softc->ctl_lock);
7941 * The initiator wants to clear the
7944 if (sa_res_key == 0) {
7946 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
7947 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
7948 && !lun->per_res[residx].registered)) {
7949 mtx_unlock(&softc->ctl_lock);
7953 lun->per_res[residx].registered = 0;
7954 memset(&lun->per_res[residx].res_key,
7955 0, sizeof(lun->per_res[residx].res_key));
7956 lun->pr_key_count--;
7958 if (residx == lun->pr_res_idx) {
7959 lun->flags &= ~CTL_LUN_PR_RESERVED;
7960 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
7962 if ((lun->res_type == SPR_TYPE_WR_EX_RO
7963 || lun->res_type == SPR_TYPE_EX_AC_RO)
7964 && lun->pr_key_count) {
7966 * If the reservation is a registrants
7967 * only type we need to generate a UA
7968 * for other registered inits. The
7969 * sense code should be RESERVATIONS
7973 for (i = 0; i < CTL_MAX_INITIATORS;i++){
7975 i+persis_offset].registered
7978 lun->pending_sense[i
7984 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7985 if (lun->pr_key_count==0) {
7986 lun->flags &= ~CTL_LUN_PR_RESERVED;
7988 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
7991 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7992 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7993 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
7994 persis_io.pr.pr_info.residx = residx;
7995 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7996 &persis_io, sizeof(persis_io), 0 )) >
7997 CTL_HA_STATUS_SUCCESS) {
7998 printf("CTL:Persis Out error returned from "
7999 "ctl_ha_msg_send %d\n", isc_retval);
8001 mtx_unlock(&softc->ctl_lock);
8002 } else /* sa_res_key != 0 */ {
8005 * If we aren't registered currently then increment
8006 * the key count and set the registered flag.
8008 if (!lun->per_res[residx].registered) {
8009 lun->pr_key_count++;
8010 lun->per_res[residx].registered = 1;
8013 memcpy(&lun->per_res[residx].res_key,
8014 param->serv_act_res_key,
8015 ctl_min(sizeof(param->serv_act_res_key),
8016 sizeof(lun->per_res[residx].res_key)));
8018 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8019 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8020 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8021 persis_io.pr.pr_info.residx = residx;
8022 memcpy(persis_io.pr.pr_info.sa_res_key,
8023 param->serv_act_res_key,
8024 sizeof(param->serv_act_res_key));
8025 mtx_unlock(&softc->ctl_lock);
8026 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8027 &persis_io, sizeof(persis_io), 0)) >
8028 CTL_HA_STATUS_SUCCESS) {
8029 printf("CTL:Persis Out error returned from "
8030 "ctl_ha_msg_send %d\n", isc_retval);
8033 lun->PRGeneration++;
8039 printf("Reserve executed type %d\n", type);
8041 mtx_lock(&softc->ctl_lock);
8042 if (lun->flags & CTL_LUN_PR_RESERVED) {
8044 * if this isn't the reservation holder and it's
8045 * not a "all registrants" type or if the type is
8046 * different then we have a conflict
8048 if ((lun->pr_res_idx != residx
8049 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8050 || lun->res_type != type) {
8051 mtx_unlock(&softc->ctl_lock);
8052 free(ctsio->kern_data_ptr, M_CTL);
8053 ctl_set_reservation_conflict(ctsio);
8054 ctl_done((union ctl_io *)ctsio);
8055 return (CTL_RETVAL_COMPLETE);
8057 } else /* create a reservation */ {
8059 * If it's not an "all registrants" type record
8060 * reservation holder
8062 if (type != SPR_TYPE_WR_EX_AR
8063 && type != SPR_TYPE_EX_AC_AR)
8064 lun->pr_res_idx = residx; /* Res holder */
8066 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8068 lun->flags |= CTL_LUN_PR_RESERVED;
8069 lun->res_type = type;
8071 mtx_unlock(&softc->ctl_lock);
8073 /* send msg to other side */
8074 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8075 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8076 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8077 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8078 persis_io.pr.pr_info.res_type = type;
8079 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8080 &persis_io, sizeof(persis_io), 0)) >
8081 CTL_HA_STATUS_SUCCESS) {
8082 printf("CTL:Persis Out error returned from "
8083 "ctl_ha_msg_send %d\n", isc_retval);
8089 mtx_lock(&softc->ctl_lock);
8090 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8091 /* No reservation exists return good status */
8092 mtx_unlock(&softc->ctl_lock);
8096 * Is this nexus a reservation holder?
8098 if (lun->pr_res_idx != residx
8099 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8101 * not a res holder return good status but
8104 mtx_unlock(&softc->ctl_lock);
8108 if (lun->res_type != type) {
8109 mtx_unlock(&softc->ctl_lock);
8110 free(ctsio->kern_data_ptr, M_CTL);
8111 ctl_set_illegal_pr_release(ctsio);
8112 ctl_done((union ctl_io *)ctsio);
8113 return (CTL_RETVAL_COMPLETE);
8116 /* okay to release */
8117 lun->flags &= ~CTL_LUN_PR_RESERVED;
8118 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8122 * if this isn't an exclusive access
8123 * res generate UA for all other
8126 if (type != SPR_TYPE_EX_AC
8127 && type != SPR_TYPE_WR_EX) {
8129 * temporarily unregister so we don't generate UA
8131 lun->per_res[residx].registered = 0;
8133 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8134 if (lun->per_res[i+persis_offset].registered
8137 lun->pending_sense[i].ua_pending |=
8141 lun->per_res[residx].registered = 1;
8143 mtx_unlock(&softc->ctl_lock);
8144 /* Send msg to other side */
8145 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8146 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8147 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8148 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8149 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8150 printf("CTL:Persis Out error returned from "
8151 "ctl_ha_msg_send %d\n", isc_retval);
8156 /* send msg to other side */
8158 mtx_lock(&softc->ctl_lock);
8159 lun->flags &= ~CTL_LUN_PR_RESERVED;
8161 lun->pr_key_count = 0;
8162 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8165 memset(&lun->per_res[residx].res_key,
8166 0, sizeof(lun->per_res[residx].res_key));
8167 lun->per_res[residx].registered = 0;
8169 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8170 if (lun->per_res[i].registered) {
8171 if (!persis_offset && i < CTL_MAX_INITIATORS)
8172 lun->pending_sense[i].ua_pending |=
8174 else if (persis_offset && i >= persis_offset)
8175 lun->pending_sense[i-persis_offset
8176 ].ua_pending |= CTL_UA_RES_PREEMPT;
8178 memset(&lun->per_res[i].res_key,
8179 0, sizeof(struct scsi_per_res_key));
8180 lun->per_res[i].registered = 0;
8182 lun->PRGeneration++;
8183 mtx_unlock(&softc->ctl_lock);
8184 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8185 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8186 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8187 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8188 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8189 printf("CTL:Persis Out error returned from "
8190 "ctl_ha_msg_send %d\n", isc_retval);
8194 case SPRO_PREEMPT: {
8197 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8198 residx, ctsio, cdb, param);
8200 return (CTL_RETVAL_COMPLETE);
8206 free(ctsio->kern_data_ptr, M_CTL);
8207 ctl_set_invalid_field(/*ctsio*/ ctsio,
8213 ctl_done((union ctl_io *)ctsio);
8214 return (CTL_RETVAL_COMPLETE);
8215 break; /* NOTREACHED */
8219 free(ctsio->kern_data_ptr, M_CTL);
8220 ctl_set_success(ctsio);
8221 ctl_done((union ctl_io *)ctsio);
8227 * This routine is for handling a message from the other SC pertaining to
8228 * persistent reserve out. All the error checking will have been done
8229 * so only perorming the action need be done here to keep the two
8233 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8235 struct ctl_lun *lun;
8236 struct ctl_softc *softc;
8239 softc = control_softc;
8241 mtx_lock(&softc->ctl_lock);
8243 lun = softc->ctl_luns[msg->hdr.nexus.targ_lun];
8244 switch(msg->pr.pr_info.action) {
8245 case CTL_PR_REG_KEY:
8246 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8247 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8248 lun->pr_key_count++;
8250 lun->PRGeneration++;
8251 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8252 msg->pr.pr_info.sa_res_key,
8253 sizeof(struct scsi_per_res_key));
8256 case CTL_PR_UNREG_KEY:
8257 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8258 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8259 0, sizeof(struct scsi_per_res_key));
8260 lun->pr_key_count--;
8262 /* XXX Need to see if the reservation has been released */
8263 /* if so do we need to generate UA? */
8264 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8265 lun->flags &= ~CTL_LUN_PR_RESERVED;
8266 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8268 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8269 || lun->res_type == SPR_TYPE_EX_AC_RO)
8270 && lun->pr_key_count) {
8272 * If the reservation is a registrants
8273 * only type we need to generate a UA
8274 * for other registered inits. The
8275 * sense code should be RESERVATIONS
8279 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8281 persis_offset].registered == 0)
8284 lun->pending_sense[i
8290 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8291 if (lun->pr_key_count==0) {
8292 lun->flags &= ~CTL_LUN_PR_RESERVED;
8294 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8297 lun->PRGeneration++;
8300 case CTL_PR_RESERVE:
8301 lun->flags |= CTL_LUN_PR_RESERVED;
8302 lun->res_type = msg->pr.pr_info.res_type;
8303 lun->pr_res_idx = msg->pr.pr_info.residx;
8307 case CTL_PR_RELEASE:
8309 * if this isn't an exclusive access res generate UA for all
8310 * other registrants.
8312 if (lun->res_type != SPR_TYPE_EX_AC
8313 && lun->res_type != SPR_TYPE_WR_EX) {
8314 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8315 if (lun->per_res[i+persis_offset].registered)
8316 lun->pending_sense[i].ua_pending |=
8320 lun->flags &= ~CTL_LUN_PR_RESERVED;
8321 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8325 case CTL_PR_PREEMPT:
8326 ctl_pro_preempt_other(lun, msg);
8329 lun->flags &= ~CTL_LUN_PR_RESERVED;
8331 lun->pr_key_count = 0;
8332 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8334 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8335 if (lun->per_res[i].registered == 0)
8338 && i < CTL_MAX_INITIATORS)
8339 lun->pending_sense[i].ua_pending |=
8341 else if (persis_offset
8342 && i >= persis_offset)
8343 lun->pending_sense[i-persis_offset].ua_pending|=
8345 memset(&lun->per_res[i].res_key, 0,
8346 sizeof(struct scsi_per_res_key));
8347 lun->per_res[i].registered = 0;
8349 lun->PRGeneration++;
8353 mtx_unlock(&softc->ctl_lock);
8357 ctl_read_write(struct ctl_scsiio *ctsio)
8359 struct ctl_lun *lun;
8360 struct ctl_lba_len lbalen;
8362 uint32_t num_blocks;
8363 int reladdr, fua, dpo, ebp;
8367 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8369 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8376 retval = CTL_RETVAL_COMPLETE;
8378 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8379 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8380 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8384 * XXX KDM need a lock here.
8386 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8387 if ((lun->res_type == SPR_TYPE_EX_AC
8388 && residx != lun->pr_res_idx)
8389 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8390 || lun->res_type == SPR_TYPE_EX_AC_AR)
8391 && !lun->per_res[residx].registered)) {
8392 ctl_set_reservation_conflict(ctsio);
8393 ctl_done((union ctl_io *)ctsio);
8394 return (CTL_RETVAL_COMPLETE);
8398 switch (ctsio->cdb[0]) {
8401 struct scsi_rw_6 *cdb;
8403 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8405 lba = scsi_3btoul(cdb->addr);
8406 /* only 5 bits are valid in the most significant address byte */
8408 num_blocks = cdb->length;
8410 * This is correct according to SBC-2.
8412 if (num_blocks == 0)
8418 struct scsi_rw_10 *cdb;
8420 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8422 if (cdb->byte2 & SRW10_RELADDR)
8424 if (cdb->byte2 & SRW10_FUA)
8426 if (cdb->byte2 & SRW10_DPO)
8429 if ((cdb->opcode == WRITE_10)
8430 && (cdb->byte2 & SRW10_EBP))
8433 lba = scsi_4btoul(cdb->addr);
8434 num_blocks = scsi_2btoul(cdb->length);
8437 case WRITE_VERIFY_10: {
8438 struct scsi_write_verify_10 *cdb;
8440 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8443 * XXX KDM we should do actual write verify support at some
8444 * point. This is obviously fake, we're just translating
8445 * things to a write. So we don't even bother checking the
8446 * BYTCHK field, since we don't do any verification. If
8447 * the user asks for it, we'll just pretend we did it.
8449 if (cdb->byte2 & SWV_DPO)
8452 lba = scsi_4btoul(cdb->addr);
8453 num_blocks = scsi_2btoul(cdb->length);
8458 struct scsi_rw_12 *cdb;
8460 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8462 if (cdb->byte2 & SRW12_RELADDR)
8464 if (cdb->byte2 & SRW12_FUA)
8466 if (cdb->byte2 & SRW12_DPO)
8468 lba = scsi_4btoul(cdb->addr);
8469 num_blocks = scsi_4btoul(cdb->length);
8472 case WRITE_VERIFY_12: {
8473 struct scsi_write_verify_12 *cdb;
8475 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8477 if (cdb->byte2 & SWV_DPO)
8480 lba = scsi_4btoul(cdb->addr);
8481 num_blocks = scsi_4btoul(cdb->length);
8487 struct scsi_rw_16 *cdb;
8489 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8491 if (cdb->byte2 & SRW12_RELADDR)
8493 if (cdb->byte2 & SRW12_FUA)
8495 if (cdb->byte2 & SRW12_DPO)
8498 lba = scsi_8btou64(cdb->addr);
8499 num_blocks = scsi_4btoul(cdb->length);
8502 case WRITE_VERIFY_16: {
8503 struct scsi_write_verify_16 *cdb;
8505 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8507 if (cdb->byte2 & SWV_DPO)
8510 lba = scsi_8btou64(cdb->addr);
8511 num_blocks = scsi_4btoul(cdb->length);
8516 * We got a command we don't support. This shouldn't
8517 * happen, commands should be filtered out above us.
8519 ctl_set_invalid_opcode(ctsio);
8520 ctl_done((union ctl_io *)ctsio);
8522 return (CTL_RETVAL_COMPLETE);
8523 break; /* NOTREACHED */
8527 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8528 * interesting for us, but if RAIDCore is in write-back mode,
8529 * getting it to do write-through for a particular transaction may
8533 * We don't support relative addressing. That also requires
8534 * supporting linked commands, which we don't do.
8537 ctl_set_invalid_field(ctsio,
8543 ctl_done((union ctl_io *)ctsio);
8544 return (CTL_RETVAL_COMPLETE);
8548 * The first check is to make sure we're in bounds, the second
8549 * check is to catch wrap-around problems. If the lba + num blocks
8550 * is less than the lba, then we've wrapped around and the block
8551 * range is invalid anyway.
8553 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8554 || ((lba + num_blocks) < lba)) {
8555 ctl_set_lba_out_of_range(ctsio);
8556 ctl_done((union ctl_io *)ctsio);
8557 return (CTL_RETVAL_COMPLETE);
8561 * According to SBC-3, a transfer length of 0 is not an error.
8562 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8563 * translates to 256 blocks for those commands.
8565 if (num_blocks == 0) {
8566 ctl_set_success(ctsio);
8567 ctl_done((union ctl_io *)ctsio);
8568 return (CTL_RETVAL_COMPLETE);
8572 lbalen.len = num_blocks;
8573 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
8576 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8578 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8584 ctl_report_luns(struct ctl_scsiio *ctsio)
8586 struct scsi_report_luns *cdb;
8587 struct scsi_report_luns_data *lun_data;
8588 struct ctl_lun *lun, *request_lun;
8589 int num_luns, retval;
8590 uint32_t alloc_len, lun_datalen;
8591 int num_filled, well_known;
8594 retval = CTL_RETVAL_COMPLETE;
8597 cdb = (struct scsi_report_luns *)ctsio->cdb;
8599 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8601 mtx_lock(&control_softc->ctl_lock);
8602 num_luns = control_softc->num_luns;
8603 mtx_unlock(&control_softc->ctl_lock);
8605 switch (cdb->select_report) {
8606 case RPL_REPORT_DEFAULT:
8607 case RPL_REPORT_ALL:
8609 case RPL_REPORT_WELLKNOWN:
8614 ctl_set_invalid_field(ctsio,
8620 ctl_done((union ctl_io *)ctsio);
8622 break; /* NOTREACHED */
8625 alloc_len = scsi_4btoul(cdb->length);
8627 * The initiator has to allocate at least 16 bytes for this request,
8628 * so he can at least get the header and the first LUN. Otherwise
8629 * we reject the request (per SPC-3 rev 14, section 6.21).
8631 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
8632 sizeof(struct scsi_report_luns_lundata))) {
8633 ctl_set_invalid_field(ctsio,
8639 ctl_done((union ctl_io *)ctsio);
8643 request_lun = (struct ctl_lun *)
8644 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8646 lun_datalen = sizeof(*lun_data) +
8647 (num_luns * sizeof(struct scsi_report_luns_lundata));
8649 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK);
8650 if (ctsio->kern_data_ptr == NULL) {
8651 ctsio->io_hdr.status = CTL_SCSI_ERROR;
8652 ctsio->scsi_status = SCSI_STATUS_BUSY;
8653 ctl_done((union ctl_io *)ctsio);
8654 return (CTL_RETVAL_COMPLETE);
8657 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
8658 ctsio->kern_sg_entries = 0;
8660 if (lun_datalen < alloc_len) {
8661 ctsio->residual = alloc_len - lun_datalen;
8662 ctsio->kern_data_len = lun_datalen;
8663 ctsio->kern_total_len = lun_datalen;
8665 ctsio->residual = 0;
8666 ctsio->kern_data_len = alloc_len;
8667 ctsio->kern_total_len = alloc_len;
8669 ctsio->kern_data_resid = 0;
8670 ctsio->kern_rel_offset = 0;
8671 ctsio->kern_sg_entries = 0;
8673 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8675 memset(lun_data, 0, lun_datalen);
8678 * We set this to the actual data length, regardless of how much
8679 * space we actually have to return results. If the user looks at
8680 * this value, he'll know whether or not he allocated enough space
8681 * and reissue the command if necessary. We don't support well
8682 * known logical units, so if the user asks for that, return none.
8684 scsi_ulto4b(lun_datalen - 8, lun_data->length);
8686 mtx_lock(&control_softc->ctl_lock);
8687 for (num_filled = 0, lun = STAILQ_FIRST(&control_softc->lun_list);
8688 (lun != NULL) && (num_filled < num_luns);
8689 lun = STAILQ_NEXT(lun, links)) {
8691 if (lun->lun <= 0xff) {
8693 * Peripheral addressing method, bus number 0.
8695 lun_data->luns[num_filled].lundata[0] =
8696 RPL_LUNDATA_ATYP_PERIPH;
8697 lun_data->luns[num_filled].lundata[1] = lun->lun;
8699 } else if (lun->lun <= 0x3fff) {
8701 * Flat addressing method.
8703 lun_data->luns[num_filled].lundata[0] =
8704 RPL_LUNDATA_ATYP_FLAT |
8705 (lun->lun & RPL_LUNDATA_FLAT_LUN_MASK);
8706 #ifdef OLDCTLHEADERS
8707 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
8708 (lun->lun & SRLD_BUS_LUN_MASK);
8710 lun_data->luns[num_filled].lundata[1] =
8711 #ifdef OLDCTLHEADERS
8712 lun->lun >> SRLD_BUS_LUN_BITS;
8714 lun->lun >> RPL_LUNDATA_FLAT_LUN_BITS;
8717 printf("ctl_report_luns: bogus LUN number %jd, "
8718 "skipping\n", (intmax_t)lun->lun);
8721 * According to SPC-3, rev 14 section 6.21:
8723 * "The execution of a REPORT LUNS command to any valid and
8724 * installed logical unit shall clear the REPORTED LUNS DATA
8725 * HAS CHANGED unit attention condition for all logical
8726 * units of that target with respect to the requesting
8727 * initiator. A valid and installed logical unit is one
8728 * having a PERIPHERAL QUALIFIER of 000b in the standard
8729 * INQUIRY data (see 6.4.2)."
8731 * If request_lun is NULL, the LUN this report luns command
8732 * was issued to is either disabled or doesn't exist. In that
8733 * case, we shouldn't clear any pending lun change unit
8736 if (request_lun != NULL)
8737 lun->pending_sense[initidx].ua_pending &=
8740 mtx_unlock(&control_softc->ctl_lock);
8743 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
8746 ctsio->scsi_status = SCSI_STATUS_OK;
8748 ctsio->be_move_done = ctl_config_move_done;
8749 ctl_datamove((union ctl_io *)ctsio);
8755 ctl_request_sense(struct ctl_scsiio *ctsio)
8757 struct scsi_request_sense *cdb;
8758 struct scsi_sense_data *sense_ptr;
8759 struct ctl_lun *lun;
8762 scsi_sense_data_type sense_format;
8764 cdb = (struct scsi_request_sense *)ctsio->cdb;
8766 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8768 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
8771 * Determine which sense format the user wants.
8773 if (cdb->byte2 & SRS_DESC)
8774 sense_format = SSD_TYPE_DESC;
8776 sense_format = SSD_TYPE_FIXED;
8778 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
8779 if (ctsio->kern_data_ptr == NULL) {
8780 ctsio->io_hdr.status = CTL_SCSI_ERROR;
8781 ctsio->scsi_status = SCSI_STATUS_BUSY;
8782 ctl_done((union ctl_io *)ctsio);
8783 return (CTL_RETVAL_COMPLETE);
8785 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
8786 ctsio->kern_sg_entries = 0;
8789 * struct scsi_sense_data, which is currently set to 256 bytes, is
8790 * larger than the largest allowed value for the length field in the
8791 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
8793 ctsio->residual = 0;
8794 ctsio->kern_data_len = cdb->length;
8795 ctsio->kern_total_len = cdb->length;
8797 ctsio->kern_data_resid = 0;
8798 ctsio->kern_rel_offset = 0;
8799 ctsio->kern_sg_entries = 0;
8802 * If we don't have a LUN, we don't have any pending sense.
8808 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8810 * Check for pending sense, and then for pending unit attentions.
8811 * Pending sense gets returned first, then pending unit attentions.
8813 mtx_lock(&lun->ctl_softc->ctl_lock);
8814 if (ctl_is_set(lun->have_ca, initidx)) {
8815 scsi_sense_data_type stored_format;
8818 * Check to see which sense format was used for the stored
8821 stored_format = scsi_sense_type(
8822 &lun->pending_sense[initidx].sense);
8825 * If the user requested a different sense format than the
8826 * one we stored, then we need to convert it to the other
8827 * format. If we're going from descriptor to fixed format
8828 * sense data, we may lose things in translation, depending
8829 * on what options were used.
8831 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
8832 * for some reason we'll just copy it out as-is.
8834 if ((stored_format == SSD_TYPE_FIXED)
8835 && (sense_format == SSD_TYPE_DESC))
8836 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
8837 &lun->pending_sense[initidx].sense,
8838 (struct scsi_sense_data_desc *)sense_ptr);
8839 else if ((stored_format == SSD_TYPE_DESC)
8840 && (sense_format == SSD_TYPE_FIXED))
8841 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
8842 &lun->pending_sense[initidx].sense,
8843 (struct scsi_sense_data_fixed *)sense_ptr);
8845 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
8846 ctl_min(sizeof(*sense_ptr),
8847 sizeof(lun->pending_sense[initidx].sense)));
8849 ctl_clear_mask(lun->have_ca, initidx);
8851 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
8852 ctl_ua_type ua_type;
8854 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
8855 sense_ptr, sense_format);
8856 if (ua_type != CTL_UA_NONE) {
8858 /* We're reporting this UA, so clear it */
8859 lun->pending_sense[initidx].ua_pending &= ~ua_type;
8862 mtx_unlock(&lun->ctl_softc->ctl_lock);
8865 * We already have a pending error, return it.
8867 if (have_error != 0) {
8869 * We report the SCSI status as OK, since the status of the
8870 * request sense command itself is OK.
8872 ctsio->scsi_status = SCSI_STATUS_OK;
8875 * We report 0 for the sense length, because we aren't doing
8876 * autosense in this case. We're reporting sense as
8879 ctsio->sense_len = 0;
8881 ctsio->be_move_done = ctl_config_move_done;
8882 ctl_datamove((union ctl_io *)ctsio);
8884 return (CTL_RETVAL_COMPLETE);
8890 * No sense information to report, so we report that everything is
8893 ctl_set_sense_data(sense_ptr,
8896 /*current_error*/ 1,
8897 /*sense_key*/ SSD_KEY_NO_SENSE,
8902 ctsio->scsi_status = SCSI_STATUS_OK;
8905 * We report 0 for the sense length, because we aren't doing
8906 * autosense in this case. We're reporting sense as parameter data.
8908 ctsio->sense_len = 0;
8909 ctsio->be_move_done = ctl_config_move_done;
8910 ctl_datamove((union ctl_io *)ctsio);
8912 return (CTL_RETVAL_COMPLETE);
8916 ctl_tur(struct ctl_scsiio *ctsio)
8918 struct ctl_lun *lun;
8920 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8922 CTL_DEBUG_PRINT(("ctl_tur\n"));
8927 ctsio->scsi_status = SCSI_STATUS_OK;
8928 ctsio->io_hdr.status = CTL_SUCCESS;
8930 ctl_done((union ctl_io *)ctsio);
8932 return (CTL_RETVAL_COMPLETE);
8937 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
8944 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
8946 struct scsi_vpd_supported_pages *pages;
8948 struct ctl_lun *lun;
8950 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8952 sup_page_size = sizeof(struct scsi_vpd_supported_pages) +
8953 SCSI_EVPD_NUM_SUPPORTED_PAGES;
8955 * XXX KDM GFP_??? We probably don't want to wait here,
8956 * unless we end up having a process/thread context.
8958 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK);
8959 if (ctsio->kern_data_ptr == NULL) {
8960 ctsio->io_hdr.status = CTL_SCSI_ERROR;
8961 ctsio->scsi_status = SCSI_STATUS_BUSY;
8962 ctl_done((union ctl_io *)ctsio);
8963 return (CTL_RETVAL_COMPLETE);
8965 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
8966 ctsio->kern_sg_entries = 0;
8968 if (sup_page_size < alloc_len) {
8969 ctsio->residual = alloc_len - sup_page_size;
8970 ctsio->kern_data_len = sup_page_size;
8971 ctsio->kern_total_len = sup_page_size;
8973 ctsio->residual = 0;
8974 ctsio->kern_data_len = alloc_len;
8975 ctsio->kern_total_len = alloc_len;
8977 ctsio->kern_data_resid = 0;
8978 ctsio->kern_rel_offset = 0;
8979 ctsio->kern_sg_entries = 0;
8981 memset(pages, 0, sup_page_size);
8984 * The control device is always connected. The disk device, on the
8985 * other hand, may not be online all the time. Need to change this
8986 * to figure out whether the disk device is actually online or not.
8989 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
8990 lun->be_lun->lun_type;
8992 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
8994 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
8995 /* Supported VPD pages */
8996 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
8998 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
8999 /* Device Identification */
9000 pages->page_list[2] = SVPD_DEVICE_ID;
9002 ctsio->scsi_status = SCSI_STATUS_OK;
9004 ctsio->be_move_done = ctl_config_move_done;
9005 ctl_datamove((union ctl_io *)ctsio);
9007 return (CTL_RETVAL_COMPLETE);
9011 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9013 struct scsi_vpd_unit_serial_number *sn_ptr;
9014 struct ctl_lun *lun;
9015 #ifndef CTL_USE_BACKEND_SN
9019 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9021 /* XXX KDM which malloc flags here?? */
9022 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK);
9023 if (ctsio->kern_data_ptr == NULL) {
9024 ctsio->io_hdr.status = CTL_SCSI_ERROR;
9025 ctsio->scsi_status = SCSI_STATUS_BUSY;
9026 ctl_done((union ctl_io *)ctsio);
9027 return (CTL_RETVAL_COMPLETE);
9029 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9030 ctsio->kern_sg_entries = 0;
9032 if (sizeof(*sn_ptr) < alloc_len) {
9033 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9034 ctsio->kern_data_len = sizeof(*sn_ptr);
9035 ctsio->kern_total_len = sizeof(*sn_ptr);
9037 ctsio->residual = 0;
9038 ctsio->kern_data_len = alloc_len;
9039 ctsio->kern_total_len = alloc_len;
9041 ctsio->kern_data_resid = 0;
9042 ctsio->kern_rel_offset = 0;
9043 ctsio->kern_sg_entries = 0;
9045 memset(sn_ptr, 0, sizeof(*sn_ptr));
9048 * The control device is always connected. The disk device, on the
9049 * other hand, may not be online all the time. Need to change this
9050 * to figure out whether the disk device is actually online or not.
9053 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9054 lun->be_lun->lun_type;
9056 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9058 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9059 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9060 #ifdef CTL_USE_BACKEND_SN
9062 * If we don't have a LUN, we just leave the serial number as
9065 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9067 strncpy((char *)sn_ptr->serial_num,
9068 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9072 * Note that we're using a non-unique serial number here,
9074 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9075 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9076 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9077 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9079 ctsio->scsi_status = SCSI_STATUS_OK;
9081 ctsio->be_move_done = ctl_config_move_done;
9082 ctl_datamove((union ctl_io *)ctsio);
9084 return (CTL_RETVAL_COMPLETE);
9089 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9091 struct scsi_vpd_device_id *devid_ptr;
9092 struct scsi_vpd_id_descriptor *desc, *desc1;
9093 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9094 struct scsi_vpd_id_t10 *t10id;
9095 struct ctl_softc *ctl_softc;
9096 struct ctl_lun *lun;
9097 struct ctl_frontend *fe;
9098 #ifndef CTL_USE_BACKEND_SN
9100 #endif /* CTL_USE_BACKEND_SN */
9103 ctl_softc = control_softc;
9104 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9106 devid_len = sizeof(struct scsi_vpd_device_id) +
9107 sizeof(struct scsi_vpd_id_descriptor) +
9108 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9109 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9110 sizeof(struct scsi_vpd_id_descriptor) +
9111 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9112 sizeof(struct scsi_vpd_id_descriptor) +
9113 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9115 /* XXX KDM which malloc flags here ?? */
9116 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK);
9117 if (ctsio->kern_data_ptr == NULL) {
9118 ctsio->io_hdr.status = CTL_SCSI_ERROR;
9119 ctsio->scsi_status = SCSI_STATUS_BUSY;
9120 ctl_done((union ctl_io *)ctsio);
9121 return (CTL_RETVAL_COMPLETE);
9123 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9124 ctsio->kern_sg_entries = 0;
9126 if (devid_len < alloc_len) {
9127 ctsio->residual = alloc_len - devid_len;
9128 ctsio->kern_data_len = devid_len;
9129 ctsio->kern_total_len = devid_len;
9131 ctsio->residual = 0;
9132 ctsio->kern_data_len = alloc_len;
9133 ctsio->kern_total_len = alloc_len;
9135 ctsio->kern_data_resid = 0;
9136 ctsio->kern_rel_offset = 0;
9137 ctsio->kern_sg_entries = 0;
9139 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9140 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9141 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9142 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9143 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9145 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9146 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9147 memset(devid_ptr, 0, devid_len);
9150 * The control device is always connected. The disk device, on the
9151 * other hand, may not be online all the time.
9154 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9155 lun->be_lun->lun_type;
9157 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9159 devid_ptr->page_code = SVPD_DEVICE_ID;
9161 scsi_ulto2b(devid_len - 4, devid_ptr->length);
9163 mtx_lock(&ctl_softc->ctl_lock);
9165 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9168 * For Fibre channel,
9170 if (fe->port_type == CTL_PORT_FC)
9172 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9173 SVPD_ID_CODESET_ASCII;
9174 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9175 SVPD_ID_CODESET_BINARY;
9179 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9180 SVPD_ID_CODESET_ASCII;
9181 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9182 SVPD_ID_CODESET_BINARY;
9184 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9185 mtx_unlock(&ctl_softc->ctl_lock);
9188 * We're using a LUN association here. i.e., this device ID is a
9189 * per-LUN identifier.
9191 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9192 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9193 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9196 * desc1 is for the WWPN which is a port asscociation.
9198 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9199 desc1->length = CTL_WWPN_LEN;
9200 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9201 /* For testing just create the WWPN */
9203 ddb_GetWWNN((char *)desc1->identifier);
9205 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9206 /* This is so Copancontrol will return something sane */
9207 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9208 ctsio->io_hdr.nexus.targ_port!=8)
9209 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9211 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9214 be64enc(desc1->identifier, fe->wwpn);
9217 * desc2 is for the Relative Target Port(type 4h) identifier
9219 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9220 | SVPD_ID_TYPE_RELTARG;
9223 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9224 /* This is so Copancontrol will return something sane */
9225 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9226 ctsio->io_hdr.nexus.targ_port!=8)
9227 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9229 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9233 * desc3 is for the Target Port Group(type 5h) identifier
9235 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9236 | SVPD_ID_TYPE_TPORTGRP;
9238 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9239 desc3->identifier[3] = 1;
9241 desc3->identifier[3] = 2;
9243 #ifdef CTL_USE_BACKEND_SN
9245 * If we've actually got a backend, copy the device id from the
9246 * per-LUN data. Otherwise, set it to all spaces.
9250 * Copy the backend's LUN ID.
9252 strncpy((char *)t10id->vendor_spec_id,
9253 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9256 * No backend, set this to spaces.
9258 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9261 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9262 (lun != NULL) ? (int)lun->lun : 0);
9263 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9267 ctsio->scsi_status = SCSI_STATUS_OK;
9269 ctsio->be_move_done = ctl_config_move_done;
9270 ctl_datamove((union ctl_io *)ctsio);
9272 return (CTL_RETVAL_COMPLETE);
9276 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9278 struct scsi_inquiry *cdb;
9279 int alloc_len, retval;
9281 cdb = (struct scsi_inquiry *)ctsio->cdb;
9283 retval = CTL_RETVAL_COMPLETE;
9285 alloc_len = scsi_2btoul(cdb->length);
9287 switch (cdb->page_code) {
9288 case SVPD_SUPPORTED_PAGES:
9289 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9291 case SVPD_UNIT_SERIAL_NUMBER:
9292 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9294 case SVPD_DEVICE_ID:
9295 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9298 ctl_set_invalid_field(ctsio,
9304 ctl_done((union ctl_io *)ctsio);
9305 retval = CTL_RETVAL_COMPLETE;
9313 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9315 struct scsi_inquiry_data *inq_ptr;
9316 struct scsi_inquiry *cdb;
9317 struct ctl_softc *ctl_softc;
9318 struct ctl_lun *lun;
9322 ctl_softc = control_softc;
9325 * Figure out whether we're talking to a Fibre Channel port or not.
9326 * We treat the ioctl front end, and any SCSI adapters, as packetized
9329 mtx_lock(&ctl_softc->ctl_lock);
9330 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9335 mtx_unlock(&ctl_softc->ctl_lock);
9337 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9338 cdb = (struct scsi_inquiry *)ctsio->cdb;
9339 alloc_len = scsi_2btoul(cdb->length);
9342 * We malloc the full inquiry data size here and fill it
9343 * in. If the user only asks for less, we'll give him
9346 /* XXX KDM what malloc flags should we use here?? */
9347 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK);
9348 if (ctsio->kern_data_ptr == NULL) {
9349 ctsio->io_hdr.status = CTL_SCSI_ERROR;
9350 ctsio->scsi_status = SCSI_STATUS_BUSY;
9351 ctl_done((union ctl_io *)ctsio);
9352 return (CTL_RETVAL_COMPLETE);
9354 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9355 ctsio->kern_sg_entries = 0;
9356 ctsio->kern_data_resid = 0;
9357 ctsio->kern_rel_offset = 0;
9359 if (sizeof(*inq_ptr) < alloc_len) {
9360 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9361 ctsio->kern_data_len = sizeof(*inq_ptr);
9362 ctsio->kern_total_len = sizeof(*inq_ptr);
9364 ctsio->residual = 0;
9365 ctsio->kern_data_len = alloc_len;
9366 ctsio->kern_total_len = alloc_len;
9369 memset(inq_ptr, 0, sizeof(*inq_ptr));
9372 * The control device is always connected. The disk device, on the
9373 * other hand, may not be online all the time. If we don't have a
9374 * LUN mapping, we'll just say it's offline.
9377 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9378 lun->be_lun->lun_type;
9380 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9382 /* RMB in byte 2 is 0 */
9383 inq_ptr->version = SCSI_REV_SPC3;
9386 * According to SAM-3, even if a device only supports a single
9387 * level of LUN addressing, it should still set the HISUP bit:
9389 * 4.9.1 Logical unit numbers overview
9391 * All logical unit number formats described in this standard are
9392 * hierarchical in structure even when only a single level in that
9393 * hierarchy is used. The HISUP bit shall be set to one in the
9394 * standard INQUIRY data (see SPC-2) when any logical unit number
9395 * format described in this standard is used. Non-hierarchical
9396 * formats are outside the scope of this standard.
9398 * Therefore we set the HiSup bit here.
9400 * The reponse format is 2, per SPC-3.
9402 inq_ptr->response_format = SID_HiSup | 2;
9404 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9405 CTL_DEBUG_PRINT(("additional_length = %d\n",
9406 inq_ptr->additional_length));
9408 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9409 /* 16 bit addressing */
9411 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9412 /* XXX set the SID_MultiP bit here if we're actually going to
9413 respond on multiple ports */
9414 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9416 /* 16 bit data bus, synchronous transfers */
9417 /* XXX these flags don't apply for FC */
9419 inq_ptr->flags = SID_WBus16 | SID_Sync;
9421 * XXX KDM do we want to support tagged queueing on the control
9425 || (lun->be_lun->lun_type != T_PROCESSOR))
9426 inq_ptr->flags |= SID_CmdQue;
9428 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9429 * We have 8 bytes for the vendor name, and 16 bytes for the device
9430 * name and 4 bytes for the revision.
9432 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
9434 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9436 switch (lun->be_lun->lun_type) {
9438 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9441 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
9444 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
9450 * XXX make this a macro somewhere so it automatically gets
9451 * incremented when we make changes.
9453 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
9456 * For parallel SCSI, we support double transition and single
9457 * transition clocking. We also support QAS (Quick Arbitration
9458 * and Selection) and Information Unit transfers on both the
9459 * control and array devices.
9462 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
9466 scsi_ulto2b(0x0060, inq_ptr->version1);
9467 /* SPC-3 (no version claimed) XXX should we claim a version? */
9468 scsi_ulto2b(0x0300, inq_ptr->version2);
9470 /* FCP-2 ANSI INCITS.350:2003 */
9471 scsi_ulto2b(0x0917, inq_ptr->version3);
9473 /* SPI-4 ANSI INCITS.362:200x */
9474 scsi_ulto2b(0x0B56, inq_ptr->version3);
9478 /* SBC-2 (no version claimed) XXX should we claim a version? */
9479 scsi_ulto2b(0x0320, inq_ptr->version4);
9481 switch (lun->be_lun->lun_type) {
9484 * SBC-2 (no version claimed) XXX should we claim a
9487 scsi_ulto2b(0x0320, inq_ptr->version4);
9494 sprintf((char *)inq_ptr->vendor_specific1, "Copyright (C) 2004, COPAN "
9495 "Systems, Inc. All Rights Reserved.");
9497 ctsio->scsi_status = SCSI_STATUS_OK;
9498 if (ctsio->kern_data_len > 0) {
9499 ctsio->be_move_done = ctl_config_move_done;
9500 ctl_datamove((union ctl_io *)ctsio);
9502 ctsio->io_hdr.status = CTL_SUCCESS;
9503 ctl_done((union ctl_io *)ctsio);
9506 return (CTL_RETVAL_COMPLETE);
9510 ctl_inquiry(struct ctl_scsiio *ctsio)
9512 struct scsi_inquiry *cdb;
9515 cdb = (struct scsi_inquiry *)ctsio->cdb;
9519 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
9522 * Right now, we don't support the CmdDt inquiry information.
9523 * This would be nice to support in the future. When we do
9524 * support it, we should change this test so that it checks to make
9525 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
9528 if (((cdb->byte2 & SI_EVPD)
9529 && (cdb->byte2 & SI_CMDDT)))
9531 if (cdb->byte2 & SI_CMDDT) {
9533 * Point to the SI_CMDDT bit. We might change this
9534 * when we support SI_CMDDT, but since both bits would be
9535 * "wrong", this should probably just stay as-is then.
9537 ctl_set_invalid_field(ctsio,
9543 ctl_done((union ctl_io *)ctsio);
9544 return (CTL_RETVAL_COMPLETE);
9546 if (cdb->byte2 & SI_EVPD)
9547 retval = ctl_inquiry_evpd(ctsio);
9549 else if (cdb->byte2 & SI_CMDDT)
9550 retval = ctl_inquiry_cmddt(ctsio);
9553 retval = ctl_inquiry_std(ctsio);
9559 * For known CDB types, parse the LBA and length.
9562 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
9564 if (io->io_hdr.io_type != CTL_IO_SCSI)
9567 switch (io->scsiio.cdb[0]) {
9570 struct scsi_rw_6 *cdb;
9572 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
9574 *lba = scsi_3btoul(cdb->addr);
9575 /* only 5 bits are valid in the most significant address byte */
9582 struct scsi_rw_10 *cdb;
9584 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
9586 *lba = scsi_4btoul(cdb->addr);
9587 *len = scsi_2btoul(cdb->length);
9590 case WRITE_VERIFY_10: {
9591 struct scsi_write_verify_10 *cdb;
9593 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
9595 *lba = scsi_4btoul(cdb->addr);
9596 *len = scsi_2btoul(cdb->length);
9601 struct scsi_rw_12 *cdb;
9603 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
9605 *lba = scsi_4btoul(cdb->addr);
9606 *len = scsi_4btoul(cdb->length);
9609 case WRITE_VERIFY_12: {
9610 struct scsi_write_verify_12 *cdb;
9612 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
9614 *lba = scsi_4btoul(cdb->addr);
9615 *len = scsi_4btoul(cdb->length);
9620 struct scsi_rw_16 *cdb;
9622 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
9624 *lba = scsi_8btou64(cdb->addr);
9625 *len = scsi_4btoul(cdb->length);
9628 case WRITE_VERIFY_16: {
9629 struct scsi_write_verify_16 *cdb;
9631 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
9634 *lba = scsi_8btou64(cdb->addr);
9635 *len = scsi_4btoul(cdb->length);
9640 break; /* NOTREACHED */
9647 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
9649 uint64_t endlba1, endlba2;
9651 endlba1 = lba1 + len1 - 1;
9652 endlba2 = lba2 + len2 - 1;
9654 if ((endlba1 < lba2)
9655 || (endlba2 < lba1))
9656 return (CTL_ACTION_PASS);
9658 return (CTL_ACTION_BLOCK);
9662 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
9664 uint64_t lba1, lba2;
9665 uint32_t len1, len2;
9668 retval = ctl_get_lba_len(io1, &lba1, &len1);
9670 return (CTL_ACTION_ERROR);
9672 retval = ctl_get_lba_len(io2, &lba2, &len2);
9674 return (CTL_ACTION_ERROR);
9676 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
9680 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
9682 struct ctl_cmd_entry *pending_entry, *ooa_entry;
9683 ctl_serialize_action *serialize_row;
9686 * The initiator attempted multiple untagged commands at the same
9687 * time. Can't do that.
9689 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9690 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9691 && ((pending_io->io_hdr.nexus.targ_port ==
9692 ooa_io->io_hdr.nexus.targ_port)
9693 && (pending_io->io_hdr.nexus.initid.id ==
9694 ooa_io->io_hdr.nexus.initid.id))
9695 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9696 return (CTL_ACTION_OVERLAP);
9699 * The initiator attempted to send multiple tagged commands with
9700 * the same ID. (It's fine if different initiators have the same
9703 * Even if all of those conditions are true, we don't kill the I/O
9704 * if the command ahead of us has been aborted. We won't end up
9705 * sending it to the FETD, and it's perfectly legal to resend a
9706 * command with the same tag number as long as the previous
9707 * instance of this tag number has been aborted somehow.
9709 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9710 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9711 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
9712 && ((pending_io->io_hdr.nexus.targ_port ==
9713 ooa_io->io_hdr.nexus.targ_port)
9714 && (pending_io->io_hdr.nexus.initid.id ==
9715 ooa_io->io_hdr.nexus.initid.id))
9716 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9717 return (CTL_ACTION_OVERLAP_TAG);
9720 * If we get a head of queue tag, SAM-3 says that we should
9721 * immediately execute it.
9723 * What happens if this command would normally block for some other
9724 * reason? e.g. a request sense with a head of queue tag
9725 * immediately after a write. Normally that would block, but this
9726 * will result in its getting executed immediately...
9728 * We currently return "pass" instead of "skip", so we'll end up
9729 * going through the rest of the queue to check for overlapped tags.
9731 * XXX KDM check for other types of blockage first??
9733 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9734 return (CTL_ACTION_PASS);
9737 * Ordered tags have to block until all items ahead of them
9738 * have completed. If we get called with an ordered tag, we always
9739 * block, if something else is ahead of us in the queue.
9741 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
9742 return (CTL_ACTION_BLOCK);
9745 * Simple tags get blocked until all head of queue and ordered tags
9746 * ahead of them have completed. I'm lumping untagged commands in
9747 * with simple tags here. XXX KDM is that the right thing to do?
9749 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9750 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
9751 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9752 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
9753 return (CTL_ACTION_BLOCK);
9755 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
9756 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
9758 serialize_row = ctl_serialize_table[ooa_entry->seridx];
9760 switch (serialize_row[pending_entry->seridx]) {
9762 return (CTL_ACTION_BLOCK);
9763 break; /* NOTREACHED */
9764 case CTL_SER_EXTENT:
9765 return (ctl_extent_check(pending_io, ooa_io));
9766 break; /* NOTREACHED */
9768 return (CTL_ACTION_PASS);
9769 break; /* NOTREACHED */
9771 return (CTL_ACTION_SKIP);
9774 panic("invalid serialization value %d",
9775 serialize_row[pending_entry->seridx]);
9776 break; /* NOTREACHED */
9779 return (CTL_ACTION_ERROR);
9783 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
9785 * - caller holds ctl_lock
9786 * - pending_io is generally either incoming, or on the blocked queue
9787 * - starting I/O is the I/O we want to start the check with.
9790 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
9791 union ctl_io *starting_io)
9793 union ctl_io *ooa_io;
9797 * Run back along the OOA queue, starting with the current
9798 * blocked I/O and going through every I/O before it on the
9799 * queue. If starting_io is NULL, we'll just end up returning
9802 for (ooa_io = starting_io; ooa_io != NULL;
9803 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
9807 * This routine just checks to see whether
9808 * cur_blocked is blocked by ooa_io, which is ahead
9809 * of it in the queue. It doesn't queue/dequeue
9812 action = ctl_check_for_blockage(pending_io, ooa_io);
9814 case CTL_ACTION_BLOCK:
9815 case CTL_ACTION_OVERLAP:
9816 case CTL_ACTION_OVERLAP_TAG:
9817 case CTL_ACTION_SKIP:
9818 case CTL_ACTION_ERROR:
9820 break; /* NOTREACHED */
9821 case CTL_ACTION_PASS:
9824 panic("invalid action %d", action);
9825 break; /* NOTREACHED */
9829 return (CTL_ACTION_PASS);
9834 * - An I/O has just completed, and has been removed from the per-LUN OOA
9835 * queue, so some items on the blocked queue may now be unblocked.
9836 * - The caller holds ctl_softc->ctl_lock
9839 ctl_check_blocked(struct ctl_lun *lun)
9841 union ctl_io *cur_blocked, *next_blocked;
9844 * Run forward from the head of the blocked queue, checking each
9845 * entry against the I/Os prior to it on the OOA queue to see if
9846 * there is still any blockage.
9848 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
9849 * with our removing a variable on it while it is traversing the
9852 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
9853 cur_blocked != NULL; cur_blocked = next_blocked) {
9854 union ctl_io *prev_ooa;
9857 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
9860 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
9861 ctl_ooaq, ooa_links);
9864 * If cur_blocked happens to be the first item in the OOA
9865 * queue now, prev_ooa will be NULL, and the action
9866 * returned will just be CTL_ACTION_PASS.
9868 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
9871 case CTL_ACTION_BLOCK:
9872 /* Nothing to do here, still blocked */
9874 case CTL_ACTION_OVERLAP:
9875 case CTL_ACTION_OVERLAP_TAG:
9877 * This shouldn't happen! In theory we've already
9878 * checked this command for overlap...
9881 case CTL_ACTION_PASS:
9882 case CTL_ACTION_SKIP: {
9883 struct ctl_softc *softc;
9884 struct ctl_cmd_entry *entry;
9890 * The skip case shouldn't happen, this transaction
9891 * should have never made it onto the blocked queue.
9894 * This I/O is no longer blocked, we can remove it
9895 * from the blocked queue. Since this is a TAILQ
9896 * (doubly linked list), we can do O(1) removals
9897 * from any place on the list.
9899 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
9901 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
9903 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
9905 * Need to send IO back to original side to
9908 union ctl_ha_msg msg_info;
9910 msg_info.hdr.original_sc =
9911 cur_blocked->io_hdr.original_sc;
9912 msg_info.hdr.serializing_sc = cur_blocked;
9913 msg_info.hdr.msg_type = CTL_MSG_R2R;
9914 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
9915 &msg_info, sizeof(msg_info), 0)) >
9916 CTL_HA_STATUS_SUCCESS) {
9917 printf("CTL:Check Blocked error from "
9918 "ctl_ha_msg_send %d\n",
9923 opcode = cur_blocked->scsiio.cdb[0];
9924 entry = &ctl_cmd_table[opcode];
9925 softc = control_softc;
9927 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
9930 * Check this I/O for LUN state changes that may
9931 * have happened while this command was blocked.
9932 * The LUN state may have been changed by a command
9933 * ahead of us in the queue, so we need to re-check
9934 * for any states that can be caused by SCSI
9937 if (ctl_scsiio_lun_check(softc, lun, entry,
9938 &cur_blocked->scsiio) == 0) {
9939 cur_blocked->io_hdr.flags |=
9940 CTL_FLAG_IS_WAS_ON_RTR;
9941 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
9942 &cur_blocked->io_hdr, links);
9944 * In the non CTL_DONE_THREAD case, we need
9945 * to wake up the work thread here. When
9946 * we're processing completed requests from
9947 * the work thread context, we'll pop back
9948 * around and end up pulling things off the
9949 * RtR queue. When we aren't processing
9950 * things from the work thread context,
9951 * though, we won't ever check the RtR queue.
9952 * So we need to wake up the thread to clear
9953 * things off the queue. Otherwise this
9954 * transaction will just sit on the RtR queue
9955 * until a new I/O comes in. (Which may or
9956 * may not happen...)
9958 #ifndef CTL_DONE_THREAD
9959 ctl_wakeup_thread();
9962 ctl_done_lock(cur_blocked, /*have_lock*/ 1);
9967 * This probably shouldn't happen -- we shouldn't
9968 * get CTL_ACTION_ERROR, or anything else.
9974 return (CTL_RETVAL_COMPLETE);
9978 * This routine (with one exception) checks LUN flags that can be set by
9979 * commands ahead of us in the OOA queue. These flags have to be checked
9980 * when a command initially comes in, and when we pull a command off the
9981 * blocked queue and are preparing to execute it. The reason we have to
9982 * check these flags for commands on the blocked queue is that the LUN
9983 * state may have been changed by a command ahead of us while we're on the
9986 * Ordering is somewhat important with these checks, so please pay
9987 * careful attention to the placement of any new checks.
9990 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
9991 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
9998 * If this shelf is a secondary shelf controller, we have to reject
9999 * any media access commands.
10002 /* No longer needed for HA */
10003 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10004 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10005 ctl_set_lun_standby(ctsio);
10012 * Check for a reservation conflict. If this command isn't allowed
10013 * even on reserved LUNs, and if this initiator isn't the one who
10014 * reserved us, reject the command with a reservation conflict.
10016 if ((lun->flags & CTL_LUN_RESERVED)
10017 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10018 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10019 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10020 || (ctsio->io_hdr.nexus.targ_target.id !=
10021 lun->rsv_nexus.targ_target.id)) {
10022 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10023 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10029 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10030 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10033 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10035 * if we aren't registered or it's a res holder type
10036 * reservation and this isn't the res holder then set a
10038 * NOTE: Commands which might be allowed on write exclusive
10039 * type reservations are checked in the particular command
10040 * for a conflict. Read and SSU are the only ones.
10042 if (!lun->per_res[residx].registered
10043 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10044 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10045 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10052 if ((lun->flags & CTL_LUN_OFFLINE)
10053 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10054 ctl_set_lun_not_ready(ctsio);
10060 * If the LUN is stopped, see if this particular command is allowed
10061 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10063 if ((lun->flags & CTL_LUN_STOPPED)
10064 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10065 /* "Logical unit not ready, initializing cmd. required" */
10066 ctl_set_lun_stopped(ctsio);
10071 if ((lun->flags & CTL_LUN_INOPERABLE)
10072 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10073 /* "Medium format corrupted" */
10074 ctl_set_medium_format_corrupted(ctsio);
10085 ctl_failover_io(union ctl_io *io, int have_lock)
10087 ctl_set_busy(&io->scsiio);
10088 ctl_done_lock(io, have_lock);
10094 struct ctl_lun *lun;
10095 struct ctl_softc *ctl_softc;
10096 union ctl_io *next_io, *pending_io;
10101 ctl_softc = control_softc;
10103 mtx_lock(&ctl_softc->ctl_lock);
10105 * Remove any cmds from the other SC from the rtr queue. These
10106 * will obviously only be for LUNs for which we're the primary.
10107 * We can't send status or get/send data for these commands.
10108 * Since they haven't been executed yet, we can just remove them.
10109 * We'll either abort them or delete them below, depending on
10110 * which HA mode we're in.
10112 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10113 io != NULL; io = next_io) {
10114 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10115 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10116 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10117 ctl_io_hdr, links);
10120 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10121 lun = ctl_softc->ctl_luns[lun_idx];
10126 * Processor LUNs are primary on both sides.
10127 * XXX will this always be true?
10129 if (lun->be_lun->lun_type == T_PROCESSOR)
10132 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10133 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10134 printf("FAILOVER: primary lun %d\n", lun_idx);
10136 * Remove all commands from the other SC. First from the
10137 * blocked queue then from the ooa queue. Once we have
10138 * removed them. Call ctl_check_blocked to see if there
10139 * is anything that can run.
10141 for (io = (union ctl_io *)TAILQ_FIRST(
10142 &lun->blocked_queue); io != NULL; io = next_io) {
10144 next_io = (union ctl_io *)TAILQ_NEXT(
10145 &io->io_hdr, blocked_links);
10147 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10148 TAILQ_REMOVE(&lun->blocked_queue,
10149 &io->io_hdr,blocked_links);
10150 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10151 TAILQ_REMOVE(&lun->ooa_queue,
10152 &io->io_hdr, ooa_links);
10154 ctl_free_io_internal(io, 1);
10158 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10159 io != NULL; io = next_io) {
10161 next_io = (union ctl_io *)TAILQ_NEXT(
10162 &io->io_hdr, ooa_links);
10164 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10166 TAILQ_REMOVE(&lun->ooa_queue,
10170 ctl_free_io_internal(io, 1);
10173 ctl_check_blocked(lun);
10174 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10175 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10177 printf("FAILOVER: primary lun %d\n", lun_idx);
10179 * Abort all commands from the other SC. We can't
10180 * send status back for them now. These should get
10181 * cleaned up when they are completed or come out
10182 * for a datamove operation.
10184 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10185 io != NULL; io = next_io) {
10186 next_io = (union ctl_io *)TAILQ_NEXT(
10187 &io->io_hdr, ooa_links);
10189 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10190 io->io_hdr.flags |= CTL_FLAG_ABORT;
10192 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10193 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10195 printf("FAILOVER: secondary lun %d\n", lun_idx);
10197 lun->flags |= CTL_LUN_PRIMARY_SC;
10200 * We send all I/O that was sent to this controller
10201 * and redirected to the other side back with
10202 * busy status, and have the initiator retry it.
10203 * Figuring out how much data has been transferred,
10204 * etc. and picking up where we left off would be
10207 * XXX KDM need to remove I/O from the blocked
10210 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10211 &lun->ooa_queue); pending_io != NULL;
10212 pending_io = next_io) {
10214 next_io = (union ctl_io *)TAILQ_NEXT(
10215 &pending_io->io_hdr, ooa_links);
10217 pending_io->io_hdr.flags &=
10218 ~CTL_FLAG_SENT_2OTHER_SC;
10220 if (pending_io->io_hdr.flags &
10221 CTL_FLAG_IO_ACTIVE) {
10222 pending_io->io_hdr.flags |=
10225 ctl_set_busy(&pending_io->scsiio);
10226 ctl_done_lock(pending_io,
10232 * Build Unit Attention
10234 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10235 lun->pending_sense[i].ua_pending |=
10236 CTL_UA_ASYM_ACC_CHANGE;
10238 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10239 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10240 printf("FAILOVER: secondary lun %d\n", lun_idx);
10242 * if the first io on the OOA is not on the RtR queue
10245 lun->flags |= CTL_LUN_PRIMARY_SC;
10247 pending_io = (union ctl_io *)TAILQ_FIRST(
10249 if (pending_io==NULL) {
10250 printf("Nothing on OOA queue\n");
10254 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10255 if ((pending_io->io_hdr.flags &
10256 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10257 pending_io->io_hdr.flags |=
10258 CTL_FLAG_IS_WAS_ON_RTR;
10259 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
10260 &pending_io->io_hdr, links);
10265 printf("Tag 0x%04x is running\n",
10266 pending_io->scsiio.tag_num);
10270 next_io = (union ctl_io *)TAILQ_NEXT(
10271 &pending_io->io_hdr, ooa_links);
10272 for (pending_io=next_io; pending_io != NULL;
10273 pending_io = next_io) {
10274 pending_io->io_hdr.flags &=
10275 ~CTL_FLAG_SENT_2OTHER_SC;
10276 next_io = (union ctl_io *)TAILQ_NEXT(
10277 &pending_io->io_hdr, ooa_links);
10278 if (pending_io->io_hdr.flags &
10279 CTL_FLAG_IS_WAS_ON_RTR) {
10281 printf("Tag 0x%04x is running\n",
10282 pending_io->scsiio.tag_num);
10287 switch (ctl_check_ooa(lun, pending_io,
10288 (union ctl_io *)TAILQ_PREV(
10289 &pending_io->io_hdr, ctl_ooaq,
10292 case CTL_ACTION_BLOCK:
10293 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10294 &pending_io->io_hdr,
10296 pending_io->io_hdr.flags |=
10299 case CTL_ACTION_PASS:
10300 case CTL_ACTION_SKIP:
10301 pending_io->io_hdr.flags |=
10302 CTL_FLAG_IS_WAS_ON_RTR;
10303 STAILQ_INSERT_TAIL(
10304 &ctl_softc->rtr_queue,
10305 &pending_io->io_hdr, links);
10307 case CTL_ACTION_OVERLAP:
10308 ctl_set_overlapped_cmd(
10309 (struct ctl_scsiio *)pending_io);
10310 ctl_done_lock(pending_io,
10313 case CTL_ACTION_OVERLAP_TAG:
10314 ctl_set_overlapped_tag(
10315 (struct ctl_scsiio *)pending_io,
10316 pending_io->scsiio.tag_num & 0xff);
10317 ctl_done_lock(pending_io,
10320 case CTL_ACTION_ERROR:
10322 ctl_set_internal_failure(
10323 (struct ctl_scsiio *)pending_io,
10326 ctl_done_lock(pending_io,
10333 * Build Unit Attention
10335 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10336 lun->pending_sense[i].ua_pending |=
10337 CTL_UA_ASYM_ACC_CHANGE;
10340 panic("Unhandled HA mode failover, LUN flags = %#x, "
10341 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10345 mtx_unlock(&ctl_softc->ctl_lock);
10349 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10351 struct ctl_lun *lun;
10352 struct ctl_cmd_entry *entry;
10361 opcode = ctsio->cdb[0];
10363 mtx_lock(&ctl_softc->ctl_lock);
10365 if ((ctsio->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
10366 && (ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun] != NULL)) {
10367 lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun];
10369 * If the LUN is invalid, pretend that it doesn't exist.
10370 * It will go away as soon as all pending I/O has been
10373 if (lun->flags & CTL_LUN_DISABLED) {
10376 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10377 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10379 if (lun->be_lun->lun_type == T_PROCESSOR) {
10380 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10384 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10385 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
10388 entry = &ctl_cmd_table[opcode];
10390 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
10391 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
10394 * Check to see whether we can send this command to LUNs that don't
10395 * exist. This should pretty much only be the case for inquiry
10396 * and request sense. Further checks, below, really require having
10397 * a LUN, so we can't really check the command anymore. Just put
10398 * it on the rtr queue.
10401 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10404 ctl_set_unsupported_lun(ctsio);
10405 mtx_unlock(&ctl_softc->ctl_lock);
10406 ctl_done((union ctl_io *)ctsio);
10410 * Every I/O goes into the OOA queue for a particular LUN, and
10411 * stays there until completion.
10413 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
10416 * Make sure we support this particular command on this LUN.
10417 * e.g., we don't support writes to the control LUN.
10419 switch (lun->be_lun->lun_type) {
10421 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
10422 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10424 ctl_set_invalid_opcode(ctsio);
10425 mtx_unlock(&ctl_softc->ctl_lock);
10426 ctl_done((union ctl_io *)ctsio);
10431 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
10432 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10434 ctl_set_invalid_opcode(ctsio);
10435 mtx_unlock(&ctl_softc->ctl_lock);
10436 ctl_done((union ctl_io *)ctsio);
10441 printf("Unsupported CTL LUN type %d\n",
10442 lun->be_lun->lun_type);
10443 panic("Unsupported CTL LUN type %d\n",
10444 lun->be_lun->lun_type);
10445 break; /* NOTREACHED */
10449 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10452 * If we've got a request sense, it'll clear the contingent
10453 * allegiance condition. Otherwise, if we have a CA condition for
10454 * this initiator, clear it, because it sent down a command other
10455 * than request sense.
10457 if ((opcode != REQUEST_SENSE)
10458 && (ctl_is_set(lun->have_ca, initidx)))
10459 ctl_clear_mask(lun->have_ca, initidx);
10462 * If the command has this flag set, it handles its own unit
10463 * attention reporting, we shouldn't do anything. Otherwise we
10464 * check for any pending unit attentions, and send them back to the
10465 * initiator. We only do this when a command initially comes in,
10466 * not when we pull it off the blocked queue.
10468 * According to SAM-3, section 5.3.2, the order that things get
10469 * presented back to the host is basically unit attentions caused
10470 * by some sort of reset event, busy status, reservation conflicts
10471 * or task set full, and finally any other status.
10473 * One issue here is that some of the unit attentions we report
10474 * don't fall into the "reset" category (e.g. "reported luns data
10475 * has changed"). So reporting it here, before the reservation
10476 * check, may be technically wrong. I guess the only thing to do
10477 * would be to check for and report the reset events here, and then
10478 * check for the other unit attention types after we check for a
10479 * reservation conflict.
10481 * XXX KDM need to fix this
10483 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
10484 ctl_ua_type ua_type;
10486 ua_type = lun->pending_sense[initidx].ua_pending;
10487 if (ua_type != CTL_UA_NONE) {
10488 scsi_sense_data_type sense_format;
10491 sense_format = (lun->flags &
10492 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
10495 sense_format = SSD_TYPE_FIXED;
10497 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
10499 if (ua_type != CTL_UA_NONE) {
10500 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
10501 ctsio->io_hdr.status = CTL_SCSI_ERROR |
10503 ctsio->sense_len = SSD_FULL_SIZE;
10504 lun->pending_sense[initidx].ua_pending &=
10506 mtx_unlock(&ctl_softc->ctl_lock);
10507 ctl_done((union ctl_io *)ctsio);
10514 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
10515 mtx_unlock(&ctl_softc->ctl_lock);
10516 ctl_done((union ctl_io *)ctsio);
10521 * XXX CHD this is where we want to send IO to other side if
10522 * this LUN is secondary on this SC. We will need to make a copy
10523 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
10524 * the copy we send as FROM_OTHER.
10525 * We also need to stuff the address of the original IO so we can
10526 * find it easily. Something similar will need be done on the other
10527 * side so when we are done we can find the copy.
10529 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10530 union ctl_ha_msg msg_info;
10533 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10535 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
10536 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
10538 printf("1. ctsio %p\n", ctsio);
10540 msg_info.hdr.serializing_sc = NULL;
10541 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
10542 msg_info.scsi.tag_num = ctsio->tag_num;
10543 msg_info.scsi.tag_type = ctsio->tag_type;
10544 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
10546 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10548 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10549 (void *)&msg_info, sizeof(msg_info), 0)) >
10550 CTL_HA_STATUS_SUCCESS) {
10551 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
10553 printf("CTL:opcode is %x\n",opcode);
10556 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
10561 * XXX KDM this I/O is off the incoming queue, but hasn't
10562 * been inserted on any other queue. We may need to come
10563 * up with a holding queue while we wait for serialization
10564 * so that we have an idea of what we're waiting for from
10567 goto bailout_unlock;
10570 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
10571 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
10572 ctl_ooaq, ooa_links))) {
10573 case CTL_ACTION_BLOCK:
10574 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
10575 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
10577 goto bailout_unlock;
10578 break; /* NOTREACHED */
10579 case CTL_ACTION_PASS:
10580 case CTL_ACTION_SKIP:
10582 break; /* NOTREACHED */
10583 case CTL_ACTION_OVERLAP:
10584 ctl_set_overlapped_cmd(ctsio);
10585 mtx_unlock(&ctl_softc->ctl_lock);
10586 ctl_done((union ctl_io *)ctsio);
10588 break; /* NOTREACHED */
10589 case CTL_ACTION_OVERLAP_TAG:
10590 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
10591 mtx_unlock(&ctl_softc->ctl_lock);
10592 ctl_done((union ctl_io *)ctsio);
10594 break; /* NOTREACHED */
10595 case CTL_ACTION_ERROR:
10597 ctl_set_internal_failure(ctsio,
10599 /*retry_count*/ 0);
10600 mtx_unlock(&ctl_softc->ctl_lock);
10601 ctl_done((union ctl_io *)ctsio);
10603 break; /* NOTREACHED */
10606 goto bailout_unlock;
10609 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
10610 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
10613 mtx_unlock(&ctl_softc->ctl_lock);
10620 ctl_scsiio(struct ctl_scsiio *ctsio)
10623 struct ctl_cmd_entry *entry;
10625 retval = CTL_RETVAL_COMPLETE;
10627 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
10629 entry = &ctl_cmd_table[ctsio->cdb[0]];
10632 * If this I/O has been aborted, just send it straight to
10633 * ctl_done() without executing it.
10635 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
10636 ctl_done((union ctl_io *)ctsio);
10641 * All the checks should have been handled by ctl_scsiio_precheck().
10642 * We should be clear now to just execute the I/O.
10644 retval = entry->execute(ctsio);
10651 * Since we only implement one target right now, a bus reset simply resets
10652 * our single target.
10655 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
10657 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
10661 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
10662 ctl_ua_type ua_type)
10664 struct ctl_lun *lun;
10667 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
10668 union ctl_ha_msg msg_info;
10670 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10671 msg_info.hdr.nexus = io->io_hdr.nexus;
10672 if (ua_type==CTL_UA_TARG_RESET)
10673 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
10675 msg_info.task.task_action = CTL_TASK_BUS_RESET;
10676 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
10677 msg_info.hdr.original_sc = NULL;
10678 msg_info.hdr.serializing_sc = NULL;
10679 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10680 (void *)&msg_info, sizeof(msg_info), 0)) {
10685 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
10686 retval += ctl_lun_reset(lun, io, ua_type);
10692 * The LUN should always be set. The I/O is optional, and is used to
10693 * distinguish between I/Os sent by this initiator, and by other
10694 * initiators. We set unit attention for initiators other than this one.
10695 * SAM-3 is vague on this point. It does say that a unit attention should
10696 * be established for other initiators when a LUN is reset (see section
10697 * 5.7.3), but it doesn't specifically say that the unit attention should
10698 * be established for this particular initiator when a LUN is reset. Here
10699 * is the relevant text, from SAM-3 rev 8:
10701 * 5.7.2 When a SCSI initiator port aborts its own tasks
10703 * When a SCSI initiator port causes its own task(s) to be aborted, no
10704 * notification that the task(s) have been aborted shall be returned to
10705 * the SCSI initiator port other than the completion response for the
10706 * command or task management function action that caused the task(s) to
10707 * be aborted and notification(s) associated with related effects of the
10708 * action (e.g., a reset unit attention condition).
10710 * XXX KDM for now, we're setting unit attention for all initiators.
10713 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
10717 uint32_t initindex;
10722 * Run through the OOA queue and abort each I/O.
10725 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10727 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10728 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10729 xio->io_hdr.flags |= CTL_FLAG_ABORT;
10733 * This version sets unit attention for every
10736 initindex = ctl_get_initindex(&io->io_hdr.nexus);
10737 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10738 if (initindex == i)
10740 lun->pending_sense[i].ua_pending |= ua_type;
10745 * A reset (any kind, really) clears reservations established with
10746 * RESERVE/RELEASE. It does not clear reservations established
10747 * with PERSISTENT RESERVE OUT, but we don't support that at the
10748 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
10749 * reservations made with the RESERVE/RELEASE commands, because
10750 * those commands are obsolete in SPC-3.
10752 lun->flags &= ~CTL_LUN_RESERVED;
10754 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10755 ctl_clear_mask(lun->have_ca, i);
10756 lun->pending_sense[i].ua_pending |= ua_type;
10763 ctl_abort_task(union ctl_io *io)
10766 struct ctl_lun *lun;
10767 struct ctl_softc *ctl_softc;
10770 char printbuf[128];
10774 ctl_softc = control_softc;
10780 if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
10781 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL))
10782 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
10787 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
10788 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
10792 * Run through the OOA queue and attempt to find the given I/O.
10793 * The target port, initiator ID, tag type and tag number have to
10794 * match the values that we got from the initiator. If we have an
10795 * untagged command to abort, simply abort the first untagged command
10796 * we come to. We only allow one untagged command at a time of course.
10799 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10801 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10802 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10804 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
10806 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
10807 lun->lun, xio->scsiio.tag_num,
10808 xio->scsiio.tag_type,
10809 (xio->io_hdr.blocked_links.tqe_prev
10810 == NULL) ? "" : " BLOCKED",
10811 (xio->io_hdr.flags &
10812 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
10813 (xio->io_hdr.flags &
10814 CTL_FLAG_ABORT) ? " ABORT" : ""),
10815 (xio->io_hdr.flags &
10816 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "");
10817 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
10819 printf("%s\n", sbuf_data(&sb));
10822 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
10823 && (xio->io_hdr.nexus.initid.id ==
10824 io->io_hdr.nexus.initid.id)) {
10826 * If the abort says that the task is untagged, the
10827 * task in the queue must be untagged. Otherwise,
10828 * we just check to see whether the tag numbers
10829 * match. This is because the QLogic firmware
10830 * doesn't pass back the tag type in an abort
10834 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
10835 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
10836 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
10839 * XXX KDM we've got problems with FC, because it
10840 * doesn't send down a tag type with aborts. So we
10841 * can only really go by the tag number...
10842 * This may cause problems with parallel SCSI.
10843 * Need to figure that out!!
10845 if (xio->scsiio.tag_num == io->taskio.tag_num) {
10846 xio->io_hdr.flags |= CTL_FLAG_ABORT;
10848 if ((io->io_hdr.flags &
10849 CTL_FLAG_FROM_OTHER_SC) == 0 &&
10850 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
10851 union ctl_ha_msg msg_info;
10853 io->io_hdr.flags |=
10854 CTL_FLAG_SENT_2OTHER_SC;
10855 msg_info.hdr.nexus = io->io_hdr.nexus;
10856 msg_info.task.task_action =
10857 CTL_TASK_ABORT_TASK;
10858 msg_info.task.tag_num =
10859 io->taskio.tag_num;
10860 msg_info.task.tag_type =
10861 io->taskio.tag_type;
10862 msg_info.hdr.msg_type =
10863 CTL_MSG_MANAGE_TASKS;
10864 msg_info.hdr.original_sc = NULL;
10865 msg_info.hdr.serializing_sc = NULL;
10867 printf("Sent Abort to other side\n");
10869 if (CTL_HA_STATUS_SUCCESS !=
10870 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10872 sizeof(msg_info), 0)) {
10876 printf("ctl_abort_task: found I/O to abort\n");
10887 * This isn't really an error. It's entirely possible for
10888 * the abort and command completion to cross on the wire.
10889 * This is more of an informative/diagnostic error.
10892 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
10893 "%d:%d:%d:%d tag %d type %d\n",
10894 io->io_hdr.nexus.initid.id,
10895 io->io_hdr.nexus.targ_port,
10896 io->io_hdr.nexus.targ_target.id,
10897 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
10898 io->taskio.tag_type);
10906 * Assumptions: caller holds ctl_softc->ctl_lock
10908 * This routine cannot block! It must be callable from an interrupt
10909 * handler as well as from the work thread.
10912 ctl_run_task_queue(struct ctl_softc *ctl_softc)
10914 union ctl_io *io, *next_io;
10916 CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
10918 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
10919 io != NULL; io = next_io) {
10921 const char *task_desc;
10923 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10927 switch (io->io_hdr.io_type) {
10928 case CTL_IO_TASK: {
10929 task_desc = ctl_scsi_task_string(&io->taskio);
10930 if (task_desc != NULL) {
10932 csevent_log(CSC_CTL | CSC_SHELF_SW |
10934 csevent_LogType_Trace,
10935 csevent_Severity_Information,
10936 csevent_AlertLevel_Green,
10937 csevent_FRU_Firmware,
10938 csevent_FRU_Unknown,
10939 "CTL: received task: %s",task_desc);
10943 csevent_log(CSC_CTL | CSC_SHELF_SW |
10945 csevent_LogType_Trace,
10946 csevent_Severity_Information,
10947 csevent_AlertLevel_Green,
10948 csevent_FRU_Firmware,
10949 csevent_FRU_Unknown,
10950 "CTL: received unknown task "
10952 io->taskio.task_action,
10953 io->taskio.task_action);
10956 switch (io->taskio.task_action) {
10957 case CTL_TASK_ABORT_TASK:
10958 retval = ctl_abort_task(io);
10960 case CTL_TASK_ABORT_TASK_SET:
10962 case CTL_TASK_CLEAR_ACA:
10964 case CTL_TASK_CLEAR_TASK_SET:
10966 case CTL_TASK_LUN_RESET: {
10967 struct ctl_lun *lun;
10971 targ_lun = io->io_hdr.nexus.targ_lun;
10973 if ((targ_lun < CTL_MAX_LUNS)
10974 && (ctl_softc->ctl_luns[targ_lun] != NULL))
10975 lun = ctl_softc->ctl_luns[targ_lun];
10981 if (!(io->io_hdr.flags &
10982 CTL_FLAG_FROM_OTHER_SC)) {
10983 union ctl_ha_msg msg_info;
10985 io->io_hdr.flags |=
10986 CTL_FLAG_SENT_2OTHER_SC;
10987 msg_info.hdr.msg_type =
10988 CTL_MSG_MANAGE_TASKS;
10989 msg_info.hdr.nexus = io->io_hdr.nexus;
10990 msg_info.task.task_action =
10991 CTL_TASK_LUN_RESET;
10992 msg_info.hdr.original_sc = NULL;
10993 msg_info.hdr.serializing_sc = NULL;
10994 if (CTL_HA_STATUS_SUCCESS !=
10995 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10997 sizeof(msg_info), 0)) {
11001 retval = ctl_lun_reset(lun, io,
11005 case CTL_TASK_TARGET_RESET:
11006 retval = ctl_target_reset(ctl_softc, io,
11007 CTL_UA_TARG_RESET);
11009 case CTL_TASK_BUS_RESET:
11010 retval = ctl_bus_reset(ctl_softc, io);
11012 case CTL_TASK_PORT_LOGIN:
11014 case CTL_TASK_PORT_LOGOUT:
11017 printf("ctl_run_task_queue: got unknown task "
11018 "management event %d\n",
11019 io->taskio.task_action);
11023 io->io_hdr.status = CTL_SUCCESS;
11025 io->io_hdr.status = CTL_ERROR;
11027 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11028 ctl_io_hdr, links);
11030 * This will queue this I/O to the done queue, but the
11031 * work thread won't be able to process it until we
11032 * return and the lock is released.
11034 ctl_done_lock(io, /*have_lock*/ 1);
11039 printf("%s: invalid I/O type %d msg %d cdb %x"
11040 " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11041 __func__, io->io_hdr.io_type,
11042 io->io_hdr.msg_type, io->scsiio.cdb[0],
11043 (uintmax_t)io->io_hdr.nexus.initid.id,
11044 io->io_hdr.nexus.targ_port,
11045 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11046 io->io_hdr.nexus.targ_lun,
11047 (io->io_hdr.io_type == CTL_IO_TASK) ?
11048 io->taskio.tag_num : io->scsiio.tag_num);
11049 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11050 ctl_io_hdr, links);
11051 ctl_free_io_internal(io, 1);
11057 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11061 * For HA operation. Handle commands that come in from the other
11065 ctl_handle_isc(union ctl_io *io)
11068 struct ctl_lun *lun;
11069 struct ctl_softc *ctl_softc;
11071 ctl_softc = control_softc;
11073 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
11075 switch (io->io_hdr.msg_type) {
11076 case CTL_MSG_SERIALIZE:
11077 free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11080 case CTL_MSG_R2R: {
11082 struct ctl_cmd_entry *entry;
11085 * This is only used in SER_ONLY mode.
11088 opcode = io->scsiio.cdb[0];
11089 entry = &ctl_cmd_table[opcode];
11090 mtx_lock(&ctl_softc->ctl_lock);
11091 if (ctl_scsiio_lun_check(ctl_softc, lun,
11092 entry, (struct ctl_scsiio *)io) != 0) {
11093 ctl_done_lock(io, /*have_lock*/ 1);
11094 mtx_unlock(&ctl_softc->ctl_lock);
11097 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11098 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11099 &io->io_hdr, links);
11100 mtx_unlock(&ctl_softc->ctl_lock);
11103 case CTL_MSG_FINISH_IO:
11104 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11106 ctl_done_lock(io, /*have_lock*/ 0);
11109 mtx_lock(&ctl_softc->ctl_lock);
11110 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11112 STAILQ_REMOVE(&ctl_softc->task_queue,
11113 &io->io_hdr, ctl_io_hdr, links);
11114 ctl_check_blocked(lun);
11115 mtx_unlock(&ctl_softc->ctl_lock);
11118 case CTL_MSG_PERS_ACTION:
11119 ctl_hndl_per_res_out_on_other_sc(
11120 (union ctl_ha_msg *)&io->presio.pr_msg);
11123 case CTL_MSG_BAD_JUJU:
11125 ctl_done_lock(io, /*have_lock*/ 0);
11127 case CTL_MSG_DATAMOVE:
11128 /* Only used in XFER mode */
11130 ctl_datamove_remote(io);
11132 case CTL_MSG_DATAMOVE_DONE:
11133 /* Only used in XFER mode */
11135 io->scsiio.be_move_done(io);
11139 printf("%s: Invalid message type %d\n",
11140 __func__, io->io_hdr.msg_type);
11144 ctl_free_io_internal(io, 0);
11150 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11151 * there is no match.
11153 static ctl_lun_error_pattern
11154 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11156 struct ctl_cmd_entry *entry;
11157 ctl_lun_error_pattern filtered_pattern, pattern;
11160 pattern = desc->error_pattern;
11163 * XXX KDM we need more data passed into this function to match a
11164 * custom pattern, and we actually need to implement custom pattern
11167 if (pattern & CTL_LUN_PAT_CMD)
11168 return (CTL_LUN_PAT_CMD);
11170 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11171 return (CTL_LUN_PAT_ANY);
11173 opcode = ctsio->cdb[0];
11174 entry = &ctl_cmd_table[opcode];
11176 filtered_pattern = entry->pattern & pattern;
11179 * If the user requested specific flags in the pattern (e.g.
11180 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11183 * If the user did not specify any flags, it doesn't matter whether
11184 * or not the command supports the flags.
11186 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11187 (pattern & ~CTL_LUN_PAT_MASK))
11188 return (CTL_LUN_PAT_NONE);
11191 * If the user asked for a range check, see if the requested LBA
11192 * range overlaps with this command's LBA range.
11194 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11200 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11202 return (CTL_LUN_PAT_NONE);
11204 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11205 desc->lba_range.len);
11207 * A "pass" means that the LBA ranges don't overlap, so
11208 * this doesn't match the user's range criteria.
11210 if (action == CTL_ACTION_PASS)
11211 return (CTL_LUN_PAT_NONE);
11214 return (filtered_pattern);
11218 * Called with the CTL lock held.
11221 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11223 struct ctl_error_desc *desc, *desc2;
11225 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11226 ctl_lun_error_pattern pattern;
11228 * Check to see whether this particular command matches
11229 * the pattern in the descriptor.
11231 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11232 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11235 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11236 case CTL_LUN_INJ_ABORTED:
11237 ctl_set_aborted(&io->scsiio);
11239 case CTL_LUN_INJ_MEDIUM_ERR:
11240 ctl_set_medium_error(&io->scsiio);
11242 case CTL_LUN_INJ_UA:
11243 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11245 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11247 case CTL_LUN_INJ_CUSTOM:
11249 * We're assuming the user knows what he is doing.
11250 * Just copy the sense information without doing
11253 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11254 ctl_min(sizeof(desc->custom_sense),
11255 sizeof(io->scsiio.sense_data)));
11256 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11257 io->scsiio.sense_len = SSD_FULL_SIZE;
11258 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11260 case CTL_LUN_INJ_NONE:
11263 * If this is an error injection type we don't know
11264 * about, clear the continuous flag (if it is set)
11265 * so it will get deleted below.
11267 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11271 * By default, each error injection action is a one-shot
11273 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11276 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11282 #ifdef CTL_IO_DELAY
11284 ctl_datamove_timer_wakeup(void *arg)
11288 io = (union ctl_io *)arg;
11292 #endif /* CTL_IO_DELAY */
11295 * Assumption: caller does NOT hold ctl_lock
11298 ctl_datamove(union ctl_io *io)
11300 void (*fe_datamove)(union ctl_io *io);
11302 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11305 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11310 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11311 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11313 sbuf_cat(&sb, path_str);
11314 switch (io->io_hdr.io_type) {
11316 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11317 sbuf_printf(&sb, "\n");
11318 sbuf_cat(&sb, path_str);
11319 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11320 io->scsiio.tag_num, io->scsiio.tag_type);
11323 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11324 "Tag Type: %d\n", io->taskio.task_action,
11325 io->taskio.tag_num, io->taskio.tag_type);
11328 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11329 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11332 sbuf_cat(&sb, path_str);
11333 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11334 (intmax_t)time_uptime - io->io_hdr.start_time);
11336 printf("%s", sbuf_data(&sb));
11338 #endif /* CTL_TIME_IO */
11340 mtx_lock(&control_softc->ctl_lock);
11341 #ifdef CTL_IO_DELAY
11342 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11343 struct ctl_lun *lun;
11345 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11347 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11349 struct ctl_lun *lun;
11351 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11353 && (lun->delay_info.datamove_delay > 0)) {
11354 struct callout *callout;
11356 callout = (struct callout *)&io->io_hdr.timer_bytes;
11357 callout_init(callout, /*mpsafe*/ 1);
11358 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11359 callout_reset(callout,
11360 lun->delay_info.datamove_delay * hz,
11361 ctl_datamove_timer_wakeup, io);
11362 if (lun->delay_info.datamove_type ==
11363 CTL_DELAY_TYPE_ONESHOT)
11364 lun->delay_info.datamove_delay = 0;
11365 mtx_unlock(&control_softc->ctl_lock);
11371 * If we have any pending task management commands, process them
11372 * first. This is necessary to eliminate a race condition with the
11375 * - FETD submits a task management command, like an abort.
11376 * - Back end calls fe_datamove() to move the data for the aborted
11377 * command. The FETD can't really accept it, but if it did, it
11378 * would end up transmitting data for a command that the initiator
11379 * told us to abort.
11381 * We close the race by processing all pending task management
11382 * commands here (we can't block!), and then check this I/O to see
11383 * if it has been aborted. If so, return it to the back end with
11384 * bad status, so the back end can say return an error to the back end
11385 * and then when the back end returns an error, we can return the
11386 * aborted command to the FETD, so it can clean up its resources.
11388 if (control_softc->flags & CTL_FLAG_TASK_PENDING)
11389 ctl_run_task_queue(control_softc);
11392 * This command has been aborted. Set the port status, so we fail
11395 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
11396 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
11397 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
11398 io->io_hdr.nexus.targ_port,
11399 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11400 io->io_hdr.nexus.targ_lun);
11401 io->io_hdr.status = CTL_CMD_ABORTED;
11402 io->io_hdr.port_status = 31337;
11403 mtx_unlock(&control_softc->ctl_lock);
11405 * Note that the backend, in this case, will get the
11406 * callback in its context. In other cases it may get
11407 * called in the frontend's interrupt thread context.
11409 io->scsiio.be_move_done(io);
11414 * If we're in XFER mode and this I/O is from the other shelf
11415 * controller, we need to send the DMA to the other side to
11416 * actually transfer the data to/from the host. In serialize only
11417 * mode the transfer happens below CTL and ctl_datamove() is only
11418 * called on the machine that originally received the I/O.
11420 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
11421 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11422 union ctl_ha_msg msg;
11423 uint32_t sg_entries_sent;
11427 memset(&msg, 0, sizeof(msg));
11428 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
11429 msg.hdr.original_sc = io->io_hdr.original_sc;
11430 msg.hdr.serializing_sc = io;
11431 msg.hdr.nexus = io->io_hdr.nexus;
11432 msg.dt.flags = io->io_hdr.flags;
11434 * We convert everything into a S/G list here. We can't
11435 * pass by reference, only by value between controllers.
11436 * So we can't pass a pointer to the S/G list, only as many
11437 * S/G entries as we can fit in here. If it's possible for
11438 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
11439 * then we need to break this up into multiple transfers.
11441 if (io->scsiio.kern_sg_entries == 0) {
11442 msg.dt.kern_sg_entries = 1;
11444 * If this is in cached memory, flush the cache
11445 * before we send the DMA request to the other
11446 * controller. We want to do this in either the
11447 * read or the write case. The read case is
11448 * straightforward. In the write case, we want to
11449 * make sure nothing is in the local cache that
11450 * could overwrite the DMAed data.
11452 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11454 * XXX KDM use bus_dmamap_sync() here.
11459 * Convert to a physical address if this is a
11462 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
11463 msg.dt.sg_list[0].addr =
11464 io->scsiio.kern_data_ptr;
11467 * XXX KDM use busdma here!
11470 msg.dt.sg_list[0].addr = (void *)
11471 vtophys(io->scsiio.kern_data_ptr);
11475 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
11478 struct ctl_sg_entry *sgl;
11481 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
11482 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
11483 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11485 * XXX KDM use bus_dmamap_sync() here.
11490 msg.dt.kern_data_len = io->scsiio.kern_data_len;
11491 msg.dt.kern_total_len = io->scsiio.kern_total_len;
11492 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
11493 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
11494 msg.dt.sg_sequence = 0;
11497 * Loop until we've sent all of the S/G entries. On the
11498 * other end, we'll recompose these S/G entries into one
11499 * contiguous list before passing it to the
11501 for (sg_entries_sent = 0; sg_entries_sent <
11502 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
11503 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
11504 sizeof(msg.dt.sg_list[0])),
11505 msg.dt.kern_sg_entries - sg_entries_sent);
11507 if (do_sg_copy != 0) {
11508 struct ctl_sg_entry *sgl;
11511 sgl = (struct ctl_sg_entry *)
11512 io->scsiio.kern_data_ptr;
11514 * If this is in cached memory, flush the cache
11515 * before we send the DMA request to the other
11516 * controller. We want to do this in either
11517 * the * read or the write case. The read
11518 * case is straightforward. In the write
11519 * case, we want to make sure nothing is
11520 * in the local cache that could overwrite
11524 for (i = sg_entries_sent, j = 0;
11525 i < msg.dt.cur_sg_entries; i++, j++) {
11526 if ((io->io_hdr.flags &
11527 CTL_FLAG_NO_DATASYNC) == 0) {
11529 * XXX KDM use bus_dmamap_sync()
11532 if ((io->io_hdr.flags &
11533 CTL_FLAG_BUS_ADDR) == 0) {
11535 * XXX KDM use busdma.
11538 msg.dt.sg_list[j].addr =(void *)
11539 vtophys(sgl[i].addr);
11542 msg.dt.sg_list[j].addr =
11545 msg.dt.sg_list[j].len = sgl[i].len;
11549 sg_entries_sent += msg.dt.cur_sg_entries;
11550 if (sg_entries_sent >= msg.dt.kern_sg_entries)
11551 msg.dt.sg_last = 1;
11553 msg.dt.sg_last = 0;
11556 * XXX KDM drop and reacquire the lock here?
11558 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
11559 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
11561 * XXX do something here.
11565 msg.dt.sent_sg_entries = sg_entries_sent;
11567 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11568 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
11569 ctl_failover_io(io, /*have_lock*/ 1);
11574 * Lookup the fe_datamove() function for this particular
11578 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11579 mtx_unlock(&control_softc->ctl_lock);
11586 ctl_send_datamove_done(union ctl_io *io, int have_lock)
11588 union ctl_ha_msg msg;
11591 memset(&msg, 0, sizeof(msg));
11593 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
11594 msg.hdr.original_sc = io;
11595 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
11596 msg.hdr.nexus = io->io_hdr.nexus;
11597 msg.hdr.status = io->io_hdr.status;
11598 msg.scsi.tag_num = io->scsiio.tag_num;
11599 msg.scsi.tag_type = io->scsiio.tag_type;
11600 msg.scsi.scsi_status = io->scsiio.scsi_status;
11601 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
11602 sizeof(io->scsiio.sense_data));
11603 msg.scsi.sense_len = io->scsiio.sense_len;
11604 msg.scsi.sense_residual = io->scsiio.sense_residual;
11605 msg.scsi.fetd_status = io->io_hdr.port_status;
11606 msg.scsi.residual = io->scsiio.residual;
11607 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11609 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
11610 ctl_failover_io(io, /*have_lock*/ have_lock);
11614 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
11615 if (isc_status > CTL_HA_STATUS_SUCCESS) {
11616 /* XXX do something if this fails */
11622 * The DMA to the remote side is done, now we need to tell the other side
11623 * we're done so it can continue with its data movement.
11626 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
11632 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11633 printf("%s: ISC DMA write failed with error %d", __func__,
11635 ctl_set_internal_failure(&io->scsiio,
11637 /*retry_count*/ rq->ret);
11640 ctl_dt_req_free(rq);
11643 * In this case, we had to malloc the memory locally. Free it.
11645 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11647 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11648 free(io->io_hdr.local_sglist[i].addr, M_CTL);
11651 * The data is in local and remote memory, so now we need to send
11652 * status (good or back) back to the other side.
11654 ctl_send_datamove_done(io, /*have_lock*/ 0);
11658 * We've moved the data from the host/controller into local memory. Now we
11659 * need to push it over to the remote controller's memory.
11662 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
11668 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
11669 ctl_datamove_remote_write_cb);
11675 ctl_datamove_remote_write(union ctl_io *io)
11678 void (*fe_datamove)(union ctl_io *io);
11681 * - Get the data from the host/HBA into local memory.
11682 * - DMA memory from the local controller to the remote controller.
11683 * - Send status back to the remote controller.
11686 retval = ctl_datamove_remote_sgl_setup(io);
11690 /* Switch the pointer over so the FETD knows what to do */
11691 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11694 * Use a custom move done callback, since we need to send completion
11695 * back to the other controller, not to the backend on this side.
11697 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
11699 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11708 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
11717 * In this case, we had to malloc the memory locally. Free it.
11719 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11721 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11722 free(io->io_hdr.local_sglist[i].addr, M_CTL);
11726 scsi_path_string(io, path_str, sizeof(path_str));
11727 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11728 sbuf_cat(&sb, path_str);
11729 scsi_command_string(&io->scsiio, NULL, &sb);
11730 sbuf_printf(&sb, "\n");
11731 sbuf_cat(&sb, path_str);
11732 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11733 io->scsiio.tag_num, io->scsiio.tag_type);
11734 sbuf_cat(&sb, path_str);
11735 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
11736 io->io_hdr.flags, io->io_hdr.status);
11738 printk("%s", sbuf_data(&sb));
11743 * The read is done, now we need to send status (good or bad) back
11744 * to the other side.
11746 ctl_send_datamove_done(io, /*have_lock*/ 0);
11752 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
11755 void (*fe_datamove)(union ctl_io *io);
11759 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11760 printf("%s: ISC DMA read failed with error %d", __func__,
11762 ctl_set_internal_failure(&io->scsiio,
11764 /*retry_count*/ rq->ret);
11767 ctl_dt_req_free(rq);
11769 /* Switch the pointer over so the FETD knows what to do */
11770 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11773 * Use a custom move done callback, since we need to send completion
11774 * back to the other controller, not to the backend on this side.
11776 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
11778 /* XXX KDM add checks like the ones in ctl_datamove? */
11780 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11786 ctl_datamove_remote_sgl_setup(union ctl_io *io)
11788 struct ctl_sg_entry *local_sglist, *remote_sglist;
11789 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
11790 struct ctl_softc *softc;
11795 softc = control_softc;
11797 local_sglist = io->io_hdr.local_sglist;
11798 local_dma_sglist = io->io_hdr.local_dma_sglist;
11799 remote_sglist = io->io_hdr.remote_sglist;
11800 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
11802 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
11803 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
11804 local_sglist[i].len = remote_sglist[i].len;
11807 * XXX Detect the situation where the RS-level I/O
11808 * redirector on the other side has already read the
11809 * data off of the AOR RS on this side, and
11810 * transferred it to remote (mirror) memory on the
11811 * other side. Since we already have the data in
11812 * memory here, we just need to use it.
11814 * XXX KDM this can probably be removed once we
11815 * get the cache device code in and take the
11816 * current AOR implementation out.
11819 if ((remote_sglist[i].addr >=
11820 (void *)vtophys(softc->mirr->addr))
11821 && (remote_sglist[i].addr <
11822 ((void *)vtophys(softc->mirr->addr) +
11823 CacheMirrorOffset))) {
11824 local_sglist[i].addr = remote_sglist[i].addr -
11826 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
11828 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
11830 local_sglist[i].addr = remote_sglist[i].addr +
11835 printf("%s: local %p, remote %p, len %d\n",
11836 __func__, local_sglist[i].addr,
11837 remote_sglist[i].addr, local_sglist[i].len);
11841 uint32_t len_to_go;
11844 * In this case, we don't have automatically allocated
11845 * memory for this I/O on this controller. This typically
11846 * happens with internal CTL I/O -- e.g. inquiry, mode
11847 * sense, etc. Anything coming from RAIDCore will have
11848 * a mirror area available.
11850 len_to_go = io->scsiio.kern_data_len;
11853 * Clear the no datasync flag, we have to use malloced
11856 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
11859 * The difficult thing here is that the size of the various
11860 * S/G segments may be different than the size from the
11861 * remote controller. That'll make it harder when DMAing
11862 * the data back to the other side.
11864 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
11865 sizeof(io->io_hdr.remote_sglist[0])) &&
11866 (len_to_go > 0); i++) {
11867 local_sglist[i].len = ctl_min(len_to_go, 131072);
11868 CTL_SIZE_8B(local_dma_sglist[i].len,
11869 local_sglist[i].len);
11870 local_sglist[i].addr =
11871 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
11873 local_dma_sglist[i].addr = local_sglist[i].addr;
11875 if (local_sglist[i].addr == NULL) {
11878 printf("malloc failed for %zd bytes!",
11879 local_dma_sglist[i].len);
11880 for (j = 0; j < i; j++) {
11881 free(local_sglist[j].addr, M_CTL);
11883 ctl_set_internal_failure(&io->scsiio,
11885 /*retry_count*/ 4857);
11887 goto bailout_error;
11890 /* XXX KDM do we need a sync here? */
11892 len_to_go -= local_sglist[i].len;
11895 * Reset the number of S/G entries accordingly. The
11896 * original number of S/G entries is available in
11899 io->scsiio.kern_sg_entries = i;
11902 printf("%s: kern_sg_entries = %d\n", __func__,
11903 io->scsiio.kern_sg_entries);
11904 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11905 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
11906 local_sglist[i].addr, local_sglist[i].len,
11907 local_dma_sglist[i].len);
11916 ctl_send_datamove_done(io, /*have_lock*/ 0);
11922 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
11923 ctl_ha_dt_cb callback)
11925 struct ctl_ha_dt_req *rq;
11926 struct ctl_sg_entry *remote_sglist, *local_sglist;
11927 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
11928 uint32_t local_used, remote_used, total_used;
11934 rq = ctl_dt_req_alloc();
11937 * If we failed to allocate the request, and if the DMA didn't fail
11938 * anyway, set busy status. This is just a resource allocation
11942 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
11943 ctl_set_busy(&io->scsiio);
11945 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
11948 ctl_dt_req_free(rq);
11951 * The data move failed. We need to return status back
11952 * to the other controller. No point in trying to DMA
11953 * data to the remote controller.
11956 ctl_send_datamove_done(io, /*have_lock*/ 0);
11963 local_sglist = io->io_hdr.local_sglist;
11964 local_dma_sglist = io->io_hdr.local_dma_sglist;
11965 remote_sglist = io->io_hdr.remote_sglist;
11966 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
11971 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
11972 rq->ret = CTL_HA_STATUS_SUCCESS;
11979 * Pull/push the data over the wire from/to the other controller.
11980 * This takes into account the possibility that the local and
11981 * remote sglists may not be identical in terms of the size of
11982 * the elements and the number of elements.
11984 * One fundamental assumption here is that the length allocated for
11985 * both the local and remote sglists is identical. Otherwise, we've
11986 * essentially got a coding error of some sort.
11988 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
11990 uint32_t cur_len, dma_length;
11993 rq->id = CTL_HA_DATA_CTL;
11994 rq->command = command;
11998 * Both pointers should be aligned. But it is possible
11999 * that the allocation length is not. They should both
12000 * also have enough slack left over at the end, though,
12001 * to round up to the next 8 byte boundary.
12003 cur_len = ctl_min(local_sglist[i].len - local_used,
12004 remote_sglist[j].len - remote_used);
12007 * In this case, we have a size issue and need to decrease
12008 * the size, except in the case where we actually have less
12009 * than 8 bytes left. In that case, we need to increase
12010 * the DMA length to get the last bit.
12012 if ((cur_len & 0x7) != 0) {
12013 if (cur_len > 0x7) {
12014 cur_len = cur_len - (cur_len & 0x7);
12015 dma_length = cur_len;
12017 CTL_SIZE_8B(dma_length, cur_len);
12021 dma_length = cur_len;
12024 * If we had to allocate memory for this I/O, instead of using
12025 * the non-cached mirror memory, we'll need to flush the cache
12026 * before trying to DMA to the other controller.
12028 * We could end up doing this multiple times for the same
12029 * segment if we have a larger local segment than remote
12030 * segment. That shouldn't be an issue.
12032 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12034 * XXX KDM use bus_dmamap_sync() here.
12038 rq->size = dma_length;
12040 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12041 tmp_ptr += local_used;
12043 /* Use physical addresses when talking to ISC hardware */
12044 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12045 /* XXX KDM use busdma */
12047 rq->local = vtophys(tmp_ptr);
12050 rq->local = tmp_ptr;
12052 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12053 tmp_ptr += remote_used;
12054 rq->remote = tmp_ptr;
12056 rq->callback = NULL;
12058 local_used += cur_len;
12059 if (local_used >= local_sglist[i].len) {
12064 remote_used += cur_len;
12065 if (remote_used >= remote_sglist[j].len) {
12069 total_used += cur_len;
12071 if (total_used >= io->scsiio.kern_data_len)
12072 rq->callback = callback;
12074 if ((rq->size & 0x7) != 0) {
12075 printf("%s: warning: size %d is not on 8b boundary\n",
12076 __func__, rq->size);
12078 if (((uintptr_t)rq->local & 0x7) != 0) {
12079 printf("%s: warning: local %p not on 8b boundary\n",
12080 __func__, rq->local);
12082 if (((uintptr_t)rq->remote & 0x7) != 0) {
12083 printf("%s: warning: remote %p not on 8b boundary\n",
12084 __func__, rq->local);
12087 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12088 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12089 rq->local, rq->remote, rq->size);
12092 isc_ret = ctl_dt_single(rq);
12093 if (isc_ret == CTL_HA_STATUS_WAIT)
12096 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12097 rq->ret = CTL_HA_STATUS_SUCCESS;
12111 ctl_datamove_remote_read(union ctl_io *io)
12117 * This will send an error to the other controller in the case of a
12120 retval = ctl_datamove_remote_sgl_setup(io);
12124 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12125 ctl_datamove_remote_read_cb);
12127 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12129 * Make sure we free memory if there was an error.. The
12130 * ctl_datamove_remote_xfer() function will send the
12131 * datamove done message, or call the callback with an
12132 * error if there is a problem.
12134 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12135 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12142 * Process a datamove request from the other controller. This is used for
12143 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12144 * first. Once that is complete, the data gets DMAed into the remote
12145 * controller's memory. For reads, we DMA from the remote controller's
12146 * memory into our memory first, and then move it out to the FETD.
12148 * Should be called without the ctl_lock held.
12151 ctl_datamove_remote(union ctl_io *io)
12153 struct ctl_softc *softc;
12155 softc = control_softc;
12158 * Note that we look for an aborted I/O here, but don't do some of
12159 * the other checks that ctl_datamove() normally does. We don't
12160 * need to run the task queue, because this I/O is on the ISC
12161 * queue, which is executed by the work thread after the task queue.
12162 * We don't need to run the datamove delay code, since that should
12163 * have been done if need be on the other controller.
12165 mtx_lock(&softc->ctl_lock);
12167 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12169 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12170 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12171 io->io_hdr.nexus.targ_port,
12172 io->io_hdr.nexus.targ_target.id,
12173 io->io_hdr.nexus.targ_lun);
12174 io->io_hdr.status = CTL_CMD_ABORTED;
12175 io->io_hdr.port_status = 31338;
12177 mtx_unlock(&softc->ctl_lock);
12179 ctl_send_datamove_done(io, /*have_lock*/ 0);
12184 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12185 mtx_unlock(&softc->ctl_lock);
12186 ctl_datamove_remote_write(io);
12187 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12188 mtx_unlock(&softc->ctl_lock);
12189 ctl_datamove_remote_read(io);
12191 union ctl_ha_msg msg;
12192 struct scsi_sense_data *sense;
12196 memset(&msg, 0, sizeof(msg));
12198 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12199 msg.hdr.status = CTL_SCSI_ERROR;
12200 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12202 retry_count = 4243;
12204 sense = &msg.scsi.sense_data;
12205 sks[0] = SSD_SCS_VALID;
12206 sks[1] = (retry_count >> 8) & 0xff;
12207 sks[2] = retry_count & 0xff;
12209 /* "Internal target failure" */
12210 scsi_set_sense_data(sense,
12211 /*sense_format*/ SSD_TYPE_NONE,
12212 /*current_error*/ 1,
12213 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12216 /*type*/ SSD_ELEM_SKS,
12217 /*size*/ sizeof(sks),
12221 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12222 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12223 ctl_failover_io(io, /*have_lock*/ 1);
12224 mtx_unlock(&softc->ctl_lock);
12228 mtx_unlock(&softc->ctl_lock);
12230 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12231 CTL_HA_STATUS_SUCCESS) {
12232 /* XXX KDM what to do if this fails? */
12240 ctl_process_done(union ctl_io *io, int have_lock)
12242 struct ctl_lun *lun;
12243 struct ctl_softc *ctl_softc;
12244 void (*fe_done)(union ctl_io *io);
12245 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12247 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12250 control_softc->ctl_ports[targ_port]->fe_done;
12253 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12258 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12259 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12261 sbuf_cat(&sb, path_str);
12262 switch (io->io_hdr.io_type) {
12264 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12265 sbuf_printf(&sb, "\n");
12266 sbuf_cat(&sb, path_str);
12267 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12268 io->scsiio.tag_num, io->scsiio.tag_type);
12271 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12272 "Tag Type: %d\n", io->taskio.task_action,
12273 io->taskio.tag_num, io->taskio.tag_type);
12276 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12277 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12280 sbuf_cat(&sb, path_str);
12281 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12282 (intmax_t)time_uptime - io->io_hdr.start_time);
12284 printf("%s", sbuf_data(&sb));
12286 #endif /* CTL_TIME_IO */
12288 switch (io->io_hdr.io_type) {
12292 ctl_io_error_print(io, NULL);
12293 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12294 ctl_free_io_internal(io, /*have_lock*/ 0);
12297 return (CTL_RETVAL_COMPLETE);
12300 printf("ctl_process_done: invalid io type %d\n",
12301 io->io_hdr.io_type);
12302 panic("ctl_process_done: invalid io type %d\n",
12303 io->io_hdr.io_type);
12304 break; /* NOTREACHED */
12307 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12309 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12310 io->io_hdr.nexus.targ_lun));
12314 ctl_softc = lun->ctl_softc;
12317 * Remove this from the OOA queue.
12319 if (have_lock == 0)
12320 mtx_lock(&ctl_softc->ctl_lock);
12323 * Check to see if we have any errors to inject here. We only
12324 * inject errors for commands that don't already have errors set.
12326 if ((STAILQ_FIRST(&lun->error_list) != NULL)
12327 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12328 ctl_inject_error(lun, io);
12331 * XXX KDM how do we treat commands that aren't completed
12334 * XXX KDM should we also track I/O latency?
12336 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
12337 uint32_t blocksize;
12339 struct bintime cur_bt;
12342 if ((lun->be_lun != NULL)
12343 && (lun->be_lun->blocksize != 0))
12344 blocksize = lun->be_lun->blocksize;
12348 switch (io->io_hdr.io_type) {
12349 case CTL_IO_SCSI: {
12351 struct ctl_lba_len lbalen;
12354 switch (io->scsiio.cdb[0]) {
12365 case WRITE_VERIFY_10:
12366 case WRITE_VERIFY_12:
12367 case WRITE_VERIFY_16:
12368 memcpy(&lbalen, io->io_hdr.ctl_private[
12369 CTL_PRIV_LBA_LEN].bytes, sizeof(lbalen));
12372 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
12373 lbalen.len * blocksize;
12374 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
12378 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
12379 &io->io_hdr.dma_bt);
12380 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
12381 io->io_hdr.num_dmas;
12382 getbintime(&cur_bt);
12383 bintime_sub(&cur_bt,
12384 &io->io_hdr.start_bt);
12387 &lun->stats.ports[targ_port].time[CTL_STATS_READ],
12391 cs_prof_gettime(&cur_ticks);
12392 lun->stats.time[CTL_STATS_READ] +=
12394 io->io_hdr.start_ticks;
12397 lun->stats.time[CTL_STATS_READ] +=
12398 jiffies - io->io_hdr.start_time;
12400 #endif /* CTL_TIME_IO */
12402 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
12403 lbalen.len * blocksize;
12404 lun->stats.ports[targ_port].operations[
12405 CTL_STATS_WRITE]++;
12409 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
12410 &io->io_hdr.dma_bt);
12411 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
12412 io->io_hdr.num_dmas;
12413 getbintime(&cur_bt);
12414 bintime_sub(&cur_bt,
12415 &io->io_hdr.start_bt);
12418 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
12421 cs_prof_gettime(&cur_ticks);
12422 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12424 io->io_hdr.start_ticks;
12425 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12426 jiffies - io->io_hdr.start_time;
12428 #endif /* CTL_TIME_IO */
12432 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
12436 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
12437 &io->io_hdr.dma_bt);
12438 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
12439 io->io_hdr.num_dmas;
12440 getbintime(&cur_bt);
12441 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12443 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
12447 cs_prof_gettime(&cur_ticks);
12448 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12450 io->io_hdr.start_ticks;
12451 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12452 jiffies - io->io_hdr.start_time;
12454 #endif /* CTL_TIME_IO */
12464 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12467 * Run through the blocked queue on this LUN and see if anything
12468 * has become unblocked, now that this transaction is done.
12470 ctl_check_blocked(lun);
12473 * If the LUN has been invalidated, free it if there is nothing
12474 * left on its OOA queue.
12476 if ((lun->flags & CTL_LUN_INVALID)
12477 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
12481 * If this command has been aborted, make sure we set the status
12482 * properly. The FETD is responsible for freeing the I/O and doing
12483 * whatever it needs to do to clean up its state.
12485 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12486 io->io_hdr.status = CTL_CMD_ABORTED;
12489 * We print out status for every task management command. For SCSI
12490 * commands, we filter out any unit attention errors; they happen
12491 * on every boot, and would clutter up the log. Note: task
12492 * management commands aren't printed here, they are printed above,
12493 * since they should never even make it down here.
12495 switch (io->io_hdr.io_type) {
12496 case CTL_IO_SCSI: {
12497 int error_code, sense_key, asc, ascq;
12501 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
12502 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
12504 * Since this is just for printing, no need to
12505 * show errors here.
12507 scsi_extract_sense_len(&io->scsiio.sense_data,
12508 io->scsiio.sense_len,
12513 /*show_errors*/ 0);
12516 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
12517 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
12518 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
12519 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
12521 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
12522 ctl_softc->skipped_prints++;
12523 if (have_lock == 0)
12524 mtx_unlock(&ctl_softc->ctl_lock);
12526 uint32_t skipped_prints;
12528 skipped_prints = ctl_softc->skipped_prints;
12530 ctl_softc->skipped_prints = 0;
12531 ctl_softc->last_print_jiffies = time_uptime;
12533 if (have_lock == 0)
12534 mtx_unlock(&ctl_softc->ctl_lock);
12535 if (skipped_prints > 0) {
12537 csevent_log(CSC_CTL | CSC_SHELF_SW |
12539 csevent_LogType_Trace,
12540 csevent_Severity_Information,
12541 csevent_AlertLevel_Green,
12542 csevent_FRU_Firmware,
12543 csevent_FRU_Unknown,
12544 "High CTL error volume, %d prints "
12545 "skipped", skipped_prints);
12548 ctl_io_error_print(io, NULL);
12551 if (have_lock == 0)
12552 mtx_unlock(&ctl_softc->ctl_lock);
12557 if (have_lock == 0)
12558 mtx_unlock(&ctl_softc->ctl_lock);
12559 ctl_io_error_print(io, NULL);
12562 if (have_lock == 0)
12563 mtx_unlock(&ctl_softc->ctl_lock);
12568 * Tell the FETD or the other shelf controller we're done with this
12569 * command. Note that only SCSI commands get to this point. Task
12570 * management commands are completed above.
12572 * We only send status to the other controller if we're in XFER
12573 * mode. In SER_ONLY mode, the I/O is done on the controller that
12574 * received the I/O (from CTL's perspective), and so the status is
12577 * XXX KDM if we hold the lock here, we could cause a deadlock
12578 * if the frontend comes back in in this context to queue
12581 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
12582 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12583 union ctl_ha_msg msg;
12585 memset(&msg, 0, sizeof(msg));
12586 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12587 msg.hdr.original_sc = io->io_hdr.original_sc;
12588 msg.hdr.nexus = io->io_hdr.nexus;
12589 msg.hdr.status = io->io_hdr.status;
12590 msg.scsi.scsi_status = io->scsiio.scsi_status;
12591 msg.scsi.tag_num = io->scsiio.tag_num;
12592 msg.scsi.tag_type = io->scsiio.tag_type;
12593 msg.scsi.sense_len = io->scsiio.sense_len;
12594 msg.scsi.sense_residual = io->scsiio.sense_residual;
12595 msg.scsi.residual = io->scsiio.residual;
12596 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12597 sizeof(io->scsiio.sense_data));
12599 * We copy this whether or not this is an I/O-related
12600 * command. Otherwise, we'd have to go and check to see
12601 * whether it's a read/write command, and it really isn't
12604 memcpy(&msg.scsi.lbalen,
12605 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
12606 sizeof(msg.scsi.lbalen));;
12608 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12609 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12610 /* XXX do something here */
12613 ctl_free_io_internal(io, /*have_lock*/ 0);
12619 return (CTL_RETVAL_COMPLETE);
12623 * Front end should call this if it doesn't do autosense. When the request
12624 * sense comes back in from the initiator, we'll dequeue this and send it.
12627 ctl_queue_sense(union ctl_io *io)
12629 struct ctl_lun *lun;
12630 struct ctl_softc *ctl_softc;
12633 ctl_softc = control_softc;
12635 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
12638 * LUN lookup will likely move to the ctl_work_thread() once we
12639 * have our new queueing infrastructure (that doesn't put things on
12640 * a per-LUN queue initially). That is so that we can handle
12641 * things like an INQUIRY to a LUN that we don't have enabled. We
12642 * can't deal with that right now.
12644 mtx_lock(&ctl_softc->ctl_lock);
12647 * If we don't have a LUN for this, just toss the sense
12650 if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
12651 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL))
12652 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
12656 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12659 * Already have CA set for this LUN...toss the sense information.
12661 if (ctl_is_set(lun->have_ca, initidx))
12664 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
12665 ctl_min(sizeof(lun->pending_sense[initidx].sense),
12666 sizeof(io->scsiio.sense_data)));
12667 ctl_set_mask(lun->have_ca, initidx);
12670 mtx_unlock(&ctl_softc->ctl_lock);
12674 return (CTL_RETVAL_COMPLETE);
12678 * Primary command inlet from frontend ports. All SCSI and task I/O
12679 * requests must go through this function.
12682 ctl_queue(union ctl_io *io)
12684 struct ctl_softc *ctl_softc;
12686 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
12688 ctl_softc = control_softc;
12691 io->io_hdr.start_time = time_uptime;
12692 getbintime(&io->io_hdr.start_bt);
12693 #endif /* CTL_TIME_IO */
12695 mtx_lock(&ctl_softc->ctl_lock);
12697 switch (io->io_hdr.io_type) {
12699 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
12703 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
12705 * Set the task pending flag. This is necessary to close a
12706 * race condition with the FETD:
12708 * - FETD submits a task management command, like an abort.
12709 * - Back end calls fe_datamove() to move the data for the
12710 * aborted command. The FETD can't really accept it, but
12711 * if it did, it would end up transmitting data for a
12712 * command that the initiator told us to abort.
12714 * We close the race condition by setting the flag here,
12715 * and checking it in ctl_datamove(), before calling the
12716 * FETD's fe_datamove routine. If we've got a task
12717 * pending, we run the task queue and then check to see
12718 * whether our particular I/O has been aborted.
12720 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
12723 mtx_unlock(&ctl_softc->ctl_lock);
12724 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
12726 break; /* NOTREACHED */
12728 mtx_unlock(&ctl_softc->ctl_lock);
12730 ctl_wakeup_thread();
12732 return (CTL_RETVAL_COMPLETE);
12735 #ifdef CTL_IO_DELAY
12737 ctl_done_timer_wakeup(void *arg)
12741 io = (union ctl_io *)arg;
12742 ctl_done_lock(io, /*have_lock*/ 0);
12744 #endif /* CTL_IO_DELAY */
12747 ctl_done_lock(union ctl_io *io, int have_lock)
12749 struct ctl_softc *ctl_softc;
12750 #ifndef CTL_DONE_THREAD
12752 #endif /* !CTL_DONE_THREAD */
12754 ctl_softc = control_softc;
12756 if (have_lock == 0)
12757 mtx_lock(&ctl_softc->ctl_lock);
12760 * Enable this to catch duplicate completion issues.
12763 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
12764 printf("%s: type %d msg %d cdb %x iptl: "
12765 "%d:%d:%d:%d tag 0x%04x "
12766 "flag %#x status %x\n",
12768 io->io_hdr.io_type,
12769 io->io_hdr.msg_type,
12771 io->io_hdr.nexus.initid.id,
12772 io->io_hdr.nexus.targ_port,
12773 io->io_hdr.nexus.targ_target.id,
12774 io->io_hdr.nexus.targ_lun,
12775 (io->io_hdr.io_type ==
12777 io->taskio.tag_num :
12778 io->scsiio.tag_num,
12780 io->io_hdr.status);
12782 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
12786 * This is an internal copy of an I/O, and should not go through
12787 * the normal done processing logic.
12789 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
12790 if (have_lock == 0)
12791 mtx_unlock(&ctl_softc->ctl_lock);
12796 * We need to send a msg to the serializing shelf to finish the IO
12797 * as well. We don't send a finish message to the other shelf if
12798 * this is a task management command. Task management commands
12799 * aren't serialized in the OOA queue, but rather just executed on
12800 * both shelf controllers for commands that originated on that
12803 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
12804 && (io->io_hdr.io_type != CTL_IO_TASK)) {
12805 union ctl_ha_msg msg_io;
12807 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
12808 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
12809 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
12810 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
12812 /* continue on to finish IO */
12814 #ifdef CTL_IO_DELAY
12815 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12816 struct ctl_lun *lun;
12818 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12820 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12822 struct ctl_lun *lun;
12824 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12827 && (lun->delay_info.done_delay > 0)) {
12828 struct callout *callout;
12830 callout = (struct callout *)&io->io_hdr.timer_bytes;
12831 callout_init(callout, /*mpsafe*/ 1);
12832 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12833 callout_reset(callout,
12834 lun->delay_info.done_delay * hz,
12835 ctl_done_timer_wakeup, io);
12836 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
12837 lun->delay_info.done_delay = 0;
12838 if (have_lock == 0)
12839 mtx_unlock(&ctl_softc->ctl_lock);
12843 #endif /* CTL_IO_DELAY */
12845 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
12847 #ifdef CTL_DONE_THREAD
12848 if (have_lock == 0)
12849 mtx_unlock(&ctl_softc->ctl_lock);
12851 ctl_wakeup_thread();
12852 #else /* CTL_DONE_THREAD */
12853 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
12855 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
12857 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
12859 ctl_process_done(xio, /*have_lock*/ 1);
12861 if (have_lock == 0)
12862 mtx_unlock(&ctl_softc->ctl_lock);
12863 #endif /* CTL_DONE_THREAD */
12867 ctl_done(union ctl_io *io)
12869 ctl_done_lock(io, /*have_lock*/ 0);
12873 ctl_isc(struct ctl_scsiio *ctsio)
12875 struct ctl_lun *lun;
12878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12880 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
12882 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
12884 retval = lun->backend->data_submit((union ctl_io *)ctsio);
12891 ctl_work_thread(void *arg)
12893 struct ctl_softc *softc;
12895 struct ctl_be_lun *be_lun;
12898 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
12900 softc = (struct ctl_softc *)arg;
12904 mtx_lock(&softc->ctl_lock);
12909 * We handle the queues in this order:
12910 * - task management
12912 * - done queue (to free up resources, unblock other commands)
12916 * If those queues are empty, we break out of the loop and
12919 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
12921 ctl_run_task_queue(softc);
12924 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
12926 STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
12927 ctl_handle_isc(io);
12930 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
12932 STAILQ_REMOVE_HEAD(&softc->done_queue, links);
12933 /* clear any blocked commands, call fe_done */
12934 mtx_unlock(&softc->ctl_lock);
12937 * Call this without a lock for now. This will
12938 * depend on whether there is any way the FETD can
12939 * sleep or deadlock if called with the CTL lock
12942 retval = ctl_process_done(io, /*have_lock*/ 0);
12943 mtx_lock(&softc->ctl_lock);
12946 if (!ctl_pause_rtr) {
12947 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
12949 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
12950 mtx_unlock(&softc->ctl_lock);
12954 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
12956 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
12957 mtx_unlock(&softc->ctl_lock);
12958 ctl_scsiio_precheck(softc, &io->scsiio);
12959 mtx_lock(&softc->ctl_lock);
12963 * We might want to move this to a separate thread, so that
12964 * configuration requests (in this case LUN creations)
12965 * won't impact the I/O path.
12967 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
12968 if (be_lun != NULL) {
12969 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
12970 mtx_unlock(&softc->ctl_lock);
12971 ctl_create_lun(be_lun);
12972 mtx_lock(&softc->ctl_lock);
12976 /* XXX KDM use the PDROP flag?? */
12977 /* Sleep until we have something to do. */
12978 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "ctl_work", 0);
12980 /* Back to the top of the loop to see what woke us up. */
12984 retval = ctl_scsiio(&io->scsiio);
12986 case CTL_RETVAL_COMPLETE:
12990 * Probably need to make sure this doesn't happen.
12994 mtx_lock(&softc->ctl_lock);
12999 ctl_wakeup_thread()
13001 struct ctl_softc *softc;
13003 softc = control_softc;
13008 /* Initialization and failover */
13011 ctl_init_isc_msg(void)
13013 printf("CTL: Still calling this thing\n");
13018 * Initializes component into configuration defined by bootMode
13020 * returns hasc_Status:
13022 * ERROR - fatal error
13024 static ctl_ha_comp_status
13025 ctl_isc_init(struct ctl_ha_component *c)
13027 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13034 * Starts component in state requested. If component starts successfully,
13035 * it must set its own state to the requestrd state
13036 * When requested state is HASC_STATE_HA, the component may refine it
13037 * by adding _SLAVE or _MASTER flags.
13038 * Currently allowed state transitions are:
13039 * UNKNOWN->HA - initial startup
13040 * UNKNOWN->SINGLE - initial startup when no parter detected
13041 * HA->SINGLE - failover
13042 * returns ctl_ha_comp_status:
13043 * OK - component successfully started in requested state
13044 * FAILED - could not start the requested state, failover may
13046 * ERROR - fatal error detected, no future startup possible
13048 static ctl_ha_comp_status
13049 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13051 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13053 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13054 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13056 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13057 != CTL_HA_STATUS_SUCCESS) {
13058 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13059 ret = CTL_HA_COMP_STATUS_ERROR;
13061 } else if (CTL_HA_STATE_IS_HA(c->state)
13062 && CTL_HA_STATE_IS_SINGLE(state)){
13063 // HA->SINGLE transition
13067 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13069 ret = CTL_HA_COMP_STATUS_ERROR;
13071 if (CTL_HA_STATE_IS_SINGLE(state))
13080 * Quiesce component
13081 * The component must clear any error conditions (set status to OK) and
13082 * prepare itself to another Start call
13083 * returns ctl_ha_comp_status:
13087 static ctl_ha_comp_status
13088 ctl_isc_quiesce(struct ctl_ha_component *c)
13090 int ret = CTL_HA_COMP_STATUS_OK;
13097 struct ctl_ha_component ctl_ha_component_ctlisc =
13100 .state = CTL_HA_STATE_UNKNOWN,
13101 .init = ctl_isc_init,
13102 .start = ctl_isc_start,
13103 .quiesce = ctl_isc_quiesce