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/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_da.h>
70 #include <cam/ctl/ctl_io.h>
71 #include <cam/ctl/ctl.h>
72 #include <cam/ctl/ctl_frontend.h>
73 #include <cam/ctl/ctl_frontend_internal.h>
74 #include <cam/ctl/ctl_util.h>
75 #include <cam/ctl/ctl_backend.h>
76 #include <cam/ctl/ctl_ioctl.h>
77 #include <cam/ctl/ctl_ha.h>
78 #include <cam/ctl/ctl_private.h>
79 #include <cam/ctl/ctl_debug.h>
80 #include <cam/ctl/ctl_scsi_all.h>
81 #include <cam/ctl/ctl_error.h>
83 struct ctl_softc *control_softc = NULL;
86 * The default is to run with CTL_DONE_THREAD turned on. Completed
87 * transactions are queued for processing by the CTL work thread. When
88 * CTL_DONE_THREAD is not defined, completed transactions are processed in
89 * the caller's context.
91 #define CTL_DONE_THREAD
94 * Use the serial number and device ID provided by the backend, rather than
97 #define CTL_USE_BACKEND_SN
100 * Size and alignment macros needed for Copan-specific HA hardware. These
101 * can go away when the HA code is re-written, and uses busdma for any
104 #define CTL_ALIGN_8B(target, source, type) \
105 if (((uint32_t)source & 0x7) != 0) \
106 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
108 target = (type)source;
110 #define CTL_SIZE_8B(target, size) \
111 if ((size & 0x7) != 0) \
112 target = size + (0x8 - (size & 0x7)); \
116 #define CTL_ALIGN_8B_MARGIN 16
119 * Template mode pages.
123 * Note that these are default values only. The actual values will be
124 * filled in when the user does a mode sense.
126 static struct copan_power_subpage power_page_default = {
127 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
128 /*subpage*/ PWR_SUBPAGE_CODE,
129 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
130 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
131 /*page_version*/ PWR_VERSION,
133 /* max_active_luns*/ PWR_DFLT_MAX_LUNS,
134 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
139 static struct copan_power_subpage power_page_changeable = {
140 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
141 /*subpage*/ PWR_SUBPAGE_CODE,
142 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
143 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
146 /* max_active_luns*/ 0,
147 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
152 static struct copan_aps_subpage aps_page_default = {
153 APS_PAGE_CODE | SMPH_SPF, //page_code
154 APS_SUBPAGE_CODE, //subpage
155 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
156 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
157 APS_VERSION, //page_version
159 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161 0, 0, 0, 0, 0} //reserved
164 static struct copan_aps_subpage aps_page_changeable = {
165 APS_PAGE_CODE | SMPH_SPF, //page_code
166 APS_SUBPAGE_CODE, //subpage
167 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
168 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
171 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173 0, 0, 0, 0, 0} //reserved
176 static struct copan_debugconf_subpage debugconf_page_default = {
177 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
178 DBGCNF_SUBPAGE_CODE, /* subpage */
179 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
180 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
181 DBGCNF_VERSION, /* page_version */
182 {CTL_TIME_IO_DEFAULT_SECS>>8,
183 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
186 static struct copan_debugconf_subpage debugconf_page_changeable = {
187 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
188 DBGCNF_SUBPAGE_CODE, /* subpage */
189 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
190 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
191 0, /* page_version */
192 {0xff,0xff}, /* ctl_time_io_secs */
195 static struct scsi_format_page format_page_default = {
196 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
197 /*page_length*/sizeof(struct scsi_format_page) - 2,
198 /*tracks_per_zone*/ {0, 0},
199 /*alt_sectors_per_zone*/ {0, 0},
200 /*alt_tracks_per_zone*/ {0, 0},
201 /*alt_tracks_per_lun*/ {0, 0},
202 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
203 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
204 /*bytes_per_sector*/ {0, 0},
205 /*interleave*/ {0, 0},
206 /*track_skew*/ {0, 0},
207 /*cylinder_skew*/ {0, 0},
209 /*reserved*/ {0, 0, 0}
212 static struct scsi_format_page format_page_changeable = {
213 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
214 /*page_length*/sizeof(struct scsi_format_page) - 2,
215 /*tracks_per_zone*/ {0, 0},
216 /*alt_sectors_per_zone*/ {0, 0},
217 /*alt_tracks_per_zone*/ {0, 0},
218 /*alt_tracks_per_lun*/ {0, 0},
219 /*sectors_per_track*/ {0, 0},
220 /*bytes_per_sector*/ {0, 0},
221 /*interleave*/ {0, 0},
222 /*track_skew*/ {0, 0},
223 /*cylinder_skew*/ {0, 0},
225 /*reserved*/ {0, 0, 0}
228 static struct scsi_rigid_disk_page rigid_disk_page_default = {
229 /*page_code*/SMS_RIGID_DISK_PAGE,
230 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
231 /*cylinders*/ {0, 0, 0},
232 /*heads*/ CTL_DEFAULT_HEADS,
233 /*start_write_precomp*/ {0, 0, 0},
234 /*start_reduced_current*/ {0, 0, 0},
235 /*step_rate*/ {0, 0},
236 /*landing_zone_cylinder*/ {0, 0, 0},
237 /*rpl*/ SRDP_RPL_DISABLED,
238 /*rotational_offset*/ 0,
240 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
241 CTL_DEFAULT_ROTATION_RATE & 0xff},
245 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
246 /*page_code*/SMS_RIGID_DISK_PAGE,
247 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
248 /*cylinders*/ {0, 0, 0},
250 /*start_write_precomp*/ {0, 0, 0},
251 /*start_reduced_current*/ {0, 0, 0},
252 /*step_rate*/ {0, 0},
253 /*landing_zone_cylinder*/ {0, 0, 0},
255 /*rotational_offset*/ 0,
257 /*rotation_rate*/ {0, 0},
261 static struct scsi_caching_page caching_page_default = {
262 /*page_code*/SMS_CACHING_PAGE,
263 /*page_length*/sizeof(struct scsi_caching_page) - 2,
264 /*flags1*/ SCP_DISC | SCP_WCE,
266 /*disable_pf_transfer_len*/ {0xff, 0xff},
267 /*min_prefetch*/ {0, 0},
268 /*max_prefetch*/ {0xff, 0xff},
269 /*max_pf_ceiling*/ {0xff, 0xff},
271 /*cache_segments*/ 0,
272 /*cache_seg_size*/ {0, 0},
274 /*non_cache_seg_size*/ {0, 0, 0}
277 static struct scsi_caching_page caching_page_changeable = {
278 /*page_code*/SMS_CACHING_PAGE,
279 /*page_length*/sizeof(struct scsi_caching_page) - 2,
282 /*disable_pf_transfer_len*/ {0, 0},
283 /*min_prefetch*/ {0, 0},
284 /*max_prefetch*/ {0, 0},
285 /*max_pf_ceiling*/ {0, 0},
287 /*cache_segments*/ 0,
288 /*cache_seg_size*/ {0, 0},
290 /*non_cache_seg_size*/ {0, 0, 0}
293 static struct scsi_control_page control_page_default = {
294 /*page_code*/SMS_CONTROL_MODE_PAGE,
295 /*page_length*/sizeof(struct scsi_control_page) - 2,
300 /*aen_holdoff_period*/{0, 0}
303 static struct scsi_control_page control_page_changeable = {
304 /*page_code*/SMS_CONTROL_MODE_PAGE,
305 /*page_length*/sizeof(struct scsi_control_page) - 2,
310 /*aen_holdoff_period*/{0, 0}
315 * XXX KDM move these into the softc.
317 static int rcv_sync_msg;
318 static int persis_offset;
319 static uint8_t ctl_pause_rtr;
320 static int ctl_is_single = 1;
321 static int index_to_aps_page;
323 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
324 static int worker_threads = 1;
325 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
326 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
327 &worker_threads, 1, "Number of worker threads");
328 static int verbose = 0;
329 TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
330 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
331 &verbose, 0, "Show SCSI errors returned to initiator");
334 * Serial number (0x80), device id (0x83), supported pages (0x00),
335 * Block limits (0xB0) and Logical Block Provisioning (0xB2)
337 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 5
339 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
341 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
342 static int ctl_init(void);
343 void ctl_shutdown(void);
344 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
345 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
346 static void ctl_ioctl_online(void *arg);
347 static void ctl_ioctl_offline(void *arg);
348 static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
349 static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
350 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
351 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
352 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
353 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
354 static int ctl_ioctl_submit_wait(union ctl_io *io);
355 static void ctl_ioctl_datamove(union ctl_io *io);
356 static void ctl_ioctl_done(union ctl_io *io);
357 static void ctl_ioctl_hard_startstop_callback(void *arg,
358 struct cfi_metatask *metatask);
359 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
360 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
361 struct ctl_ooa *ooa_hdr,
362 struct ctl_ooa_entry *kern_entries);
363 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
365 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
366 uint32_t ctl_port_idx(int port_num);
368 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
369 uint32_t targ_target, uint32_t targ_lun,
371 static void ctl_kfree_io(union ctl_io *io);
373 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
374 struct ctl_be_lun *be_lun, struct ctl_id target_id);
375 static int ctl_free_lun(struct ctl_lun *lun);
376 static void ctl_create_lun(struct ctl_be_lun *be_lun);
378 static void ctl_failover_change_pages(struct ctl_softc *softc,
379 struct ctl_scsiio *ctsio, int master);
382 static int ctl_do_mode_select(union ctl_io *io);
383 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
384 uint64_t res_key, uint64_t sa_res_key,
385 uint8_t type, uint32_t residx,
386 struct ctl_scsiio *ctsio,
387 struct scsi_per_res_out *cdb,
388 struct scsi_per_res_out_parms* param);
389 static void ctl_pro_preempt_other(struct ctl_lun *lun,
390 union ctl_ha_msg *msg);
391 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
392 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
393 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
394 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
395 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
397 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
398 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
399 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
400 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
401 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
402 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
403 union ctl_io *ooa_io);
404 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
405 union ctl_io *starting_io);
406 static int ctl_check_blocked(struct ctl_lun *lun);
407 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
409 struct ctl_cmd_entry *entry,
410 struct ctl_scsiio *ctsio);
411 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
412 static void ctl_failover(void);
413 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
414 struct ctl_scsiio *ctsio);
415 static int ctl_scsiio(struct ctl_scsiio *ctsio);
417 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
418 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
419 ctl_ua_type ua_type);
420 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
421 ctl_ua_type ua_type);
422 static int ctl_abort_task(union ctl_io *io);
423 static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
425 static void ctl_datamove_timer_wakeup(void *arg);
426 static void ctl_done_timer_wakeup(void *arg);
427 #endif /* CTL_IO_DELAY */
429 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
430 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
431 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
432 static void ctl_datamove_remote_write(union ctl_io *io);
433 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
434 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
435 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
436 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
437 ctl_ha_dt_cb callback);
438 static void ctl_datamove_remote_read(union ctl_io *io);
439 static void ctl_datamove_remote(union ctl_io *io);
440 static int ctl_process_done(union ctl_io *io, int have_lock);
441 static void ctl_work_thread(void *arg);
444 * Load the serialization table. This isn't very pretty, but is probably
445 * the easiest way to do it.
447 #include "ctl_ser_table.c"
450 * We only need to define open, close and ioctl routines for this driver.
452 static struct cdevsw ctl_cdevsw = {
453 .d_version = D_VERSION,
456 .d_close = ctl_close,
457 .d_ioctl = ctl_ioctl,
462 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
464 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
466 static moduledata_t ctl_moduledata = {
468 ctl_module_event_handler,
472 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
473 MODULE_VERSION(ctl, 1);
476 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
477 union ctl_ha_msg *msg_info)
479 struct ctl_scsiio *ctsio;
481 if (msg_info->hdr.original_sc == NULL) {
482 printf("%s: original_sc == NULL!\n", __func__);
483 /* XXX KDM now what? */
487 ctsio = &msg_info->hdr.original_sc->scsiio;
488 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
489 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
490 ctsio->io_hdr.status = msg_info->hdr.status;
491 ctsio->scsi_status = msg_info->scsi.scsi_status;
492 ctsio->sense_len = msg_info->scsi.sense_len;
493 ctsio->sense_residual = msg_info->scsi.sense_residual;
494 ctsio->residual = msg_info->scsi.residual;
495 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
496 sizeof(ctsio->sense_data));
497 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
498 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
499 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
504 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
505 union ctl_ha_msg *msg_info)
507 struct ctl_scsiio *ctsio;
509 if (msg_info->hdr.serializing_sc == NULL) {
510 printf("%s: serializing_sc == NULL!\n", __func__);
511 /* XXX KDM now what? */
515 ctsio = &msg_info->hdr.serializing_sc->scsiio;
518 * Attempt to catch the situation where an I/O has
519 * been freed, and we're using it again.
521 if (ctsio->io_hdr.io_type == 0xff) {
522 union ctl_io *tmp_io;
523 tmp_io = (union ctl_io *)ctsio;
524 printf("%s: %p use after free!\n", __func__,
526 printf("%s: type %d msg %d cdb %x iptl: "
527 "%d:%d:%d:%d tag 0x%04x "
528 "flag %#x status %x\n",
530 tmp_io->io_hdr.io_type,
531 tmp_io->io_hdr.msg_type,
532 tmp_io->scsiio.cdb[0],
533 tmp_io->io_hdr.nexus.initid.id,
534 tmp_io->io_hdr.nexus.targ_port,
535 tmp_io->io_hdr.nexus.targ_target.id,
536 tmp_io->io_hdr.nexus.targ_lun,
537 (tmp_io->io_hdr.io_type ==
539 tmp_io->taskio.tag_num :
540 tmp_io->scsiio.tag_num,
541 tmp_io->io_hdr.flags,
542 tmp_io->io_hdr.status);
545 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
546 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
551 * ISC (Inter Shelf Communication) event handler. Events from the HA
552 * subsystem come in here.
555 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
557 struct ctl_softc *ctl_softc;
559 struct ctl_prio *presio;
560 ctl_ha_status isc_status;
562 ctl_softc = control_softc;
567 printf("CTL: Isc Msg event %d\n", event);
569 if (event == CTL_HA_EVT_MSG_RECV) {
570 union ctl_ha_msg msg_info;
572 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
573 sizeof(msg_info), /*wait*/ 0);
575 printf("CTL: msg_type %d\n", msg_info.msg_type);
577 if (isc_status != 0) {
578 printf("Error receiving message, status = %d\n",
582 mtx_lock(&ctl_softc->ctl_lock);
584 switch (msg_info.hdr.msg_type) {
585 case CTL_MSG_SERIALIZE:
587 printf("Serialize\n");
589 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
591 printf("ctl_isc_event_handler: can't allocate "
594 /* Need to set busy and send msg back */
595 mtx_unlock(&ctl_softc->ctl_lock);
596 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
597 msg_info.hdr.status = CTL_SCSI_ERROR;
598 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
599 msg_info.scsi.sense_len = 0;
600 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
601 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
606 // populate ctsio from msg_info
607 io->io_hdr.io_type = CTL_IO_SCSI;
608 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
609 io->io_hdr.original_sc = msg_info.hdr.original_sc;
611 printf("pOrig %x\n", (int)msg_info.original_sc);
613 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
616 * If we're in serialization-only mode, we don't
617 * want to go through full done processing. Thus
620 * XXX KDM add another flag that is more specific.
622 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
623 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
624 io->io_hdr.nexus = msg_info.hdr.nexus;
626 printf("targ %d, port %d, iid %d, lun %d\n",
627 io->io_hdr.nexus.targ_target.id,
628 io->io_hdr.nexus.targ_port,
629 io->io_hdr.nexus.initid.id,
630 io->io_hdr.nexus.targ_lun);
632 io->scsiio.tag_num = msg_info.scsi.tag_num;
633 io->scsiio.tag_type = msg_info.scsi.tag_type;
634 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
636 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
637 struct ctl_cmd_entry *entry;
640 opcode = io->scsiio.cdb[0];
641 entry = &ctl_cmd_table[opcode];
642 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
644 entry->flags & CTL_FLAG_DATA_MASK;
646 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
651 /* Performed on the Originating SC, XFER mode only */
652 case CTL_MSG_DATAMOVE: {
653 struct ctl_sg_entry *sgl;
656 io = msg_info.hdr.original_sc;
658 printf("%s: original_sc == NULL!\n", __func__);
659 /* XXX KDM do something here */
662 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
663 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
665 * Keep track of this, we need to send it back over
666 * when the datamove is complete.
668 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
670 if (msg_info.dt.sg_sequence == 0) {
672 * XXX KDM we use the preallocated S/G list
673 * here, but we'll need to change this to
674 * dynamic allocation if we need larger S/G
677 if (msg_info.dt.kern_sg_entries >
678 sizeof(io->io_hdr.remote_sglist) /
679 sizeof(io->io_hdr.remote_sglist[0])) {
680 printf("%s: number of S/G entries "
681 "needed %u > allocated num %zd\n",
683 msg_info.dt.kern_sg_entries,
684 sizeof(io->io_hdr.remote_sglist)/
685 sizeof(io->io_hdr.remote_sglist[0]));
688 * XXX KDM send a message back to
689 * the other side to shut down the
690 * DMA. The error will come back
691 * through via the normal channel.
695 sgl = io->io_hdr.remote_sglist;
697 sizeof(io->io_hdr.remote_sglist));
699 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
701 io->scsiio.kern_sg_entries =
702 msg_info.dt.kern_sg_entries;
703 io->scsiio.rem_sg_entries =
704 msg_info.dt.kern_sg_entries;
705 io->scsiio.kern_data_len =
706 msg_info.dt.kern_data_len;
707 io->scsiio.kern_total_len =
708 msg_info.dt.kern_total_len;
709 io->scsiio.kern_data_resid =
710 msg_info.dt.kern_data_resid;
711 io->scsiio.kern_rel_offset =
712 msg_info.dt.kern_rel_offset;
714 * Clear out per-DMA flags.
716 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
718 * Add per-DMA flags that are set for this
719 * particular DMA request.
721 io->io_hdr.flags |= msg_info.dt.flags &
724 sgl = (struct ctl_sg_entry *)
725 io->scsiio.kern_data_ptr;
727 for (i = msg_info.dt.sent_sg_entries, j = 0;
728 i < (msg_info.dt.sent_sg_entries +
729 msg_info.dt.cur_sg_entries); i++, j++) {
730 sgl[i].addr = msg_info.dt.sg_list[j].addr;
731 sgl[i].len = msg_info.dt.sg_list[j].len;
734 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
736 msg_info.dt.sg_list[j].addr,
737 msg_info.dt.sg_list[j].len,
738 sgl[i].addr, sgl[i].len, j, i);
742 memcpy(&sgl[msg_info.dt.sent_sg_entries],
744 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
748 * If this is the last piece of the I/O, we've got
749 * the full S/G list. Queue processing in the thread.
750 * Otherwise wait for the next piece.
752 if (msg_info.dt.sg_last != 0) {
753 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
759 /* Performed on the Serializing (primary) SC, XFER mode only */
760 case CTL_MSG_DATAMOVE_DONE: {
761 if (msg_info.hdr.serializing_sc == NULL) {
762 printf("%s: serializing_sc == NULL!\n",
764 /* XXX KDM now what? */
768 * We grab the sense information here in case
769 * there was a failure, so we can return status
770 * back to the initiator.
772 io = msg_info.hdr.serializing_sc;
773 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
774 io->io_hdr.status = msg_info.hdr.status;
775 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
776 io->scsiio.sense_len = msg_info.scsi.sense_len;
777 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
778 io->io_hdr.port_status = msg_info.scsi.fetd_status;
779 io->scsiio.residual = msg_info.scsi.residual;
780 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
781 sizeof(io->scsiio.sense_data));
783 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
789 /* Preformed on Originating SC, SER_ONLY mode */
791 io = msg_info.hdr.original_sc;
793 printf("%s: Major Bummer\n", __func__);
794 mtx_unlock(&ctl_softc->ctl_lock);
798 printf("pOrig %x\n",(int) ctsio);
801 io->io_hdr.msg_type = CTL_MSG_R2R;
802 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
803 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
809 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
811 * Performed on the Originating (i.e. secondary) SC in XFER
814 case CTL_MSG_FINISH_IO:
815 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
816 ctl_isc_handler_finish_xfer(ctl_softc,
819 ctl_isc_handler_finish_ser_only(ctl_softc,
823 /* Preformed on Originating SC */
824 case CTL_MSG_BAD_JUJU:
825 io = msg_info.hdr.original_sc;
827 printf("%s: Bad JUJU!, original_sc is NULL!\n",
831 ctl_copy_sense_data(&msg_info, io);
833 * IO should have already been cleaned up on other
834 * SC so clear this flag so we won't send a message
835 * back to finish the IO there.
837 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
838 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
840 /* io = msg_info.hdr.serializing_sc; */
841 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
842 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
847 /* Handle resets sent from the other side */
848 case CTL_MSG_MANAGE_TASKS: {
849 struct ctl_taskio *taskio;
850 taskio = (struct ctl_taskio *)ctl_alloc_io(
851 (void *)ctl_softc->othersc_pool);
852 if (taskio == NULL) {
853 printf("ctl_isc_event_handler: can't allocate "
856 /* should I just call the proper reset func
858 mtx_unlock(&ctl_softc->ctl_lock);
861 ctl_zero_io((union ctl_io *)taskio);
862 taskio->io_hdr.io_type = CTL_IO_TASK;
863 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
864 taskio->io_hdr.nexus = msg_info.hdr.nexus;
865 taskio->task_action = msg_info.task.task_action;
866 taskio->tag_num = msg_info.task.tag_num;
867 taskio->tag_type = msg_info.task.tag_type;
869 taskio->io_hdr.start_time = time_uptime;
870 getbintime(&taskio->io_hdr.start_bt);
872 cs_prof_gettime(&taskio->io_hdr.start_ticks);
874 #endif /* CTL_TIME_IO */
875 STAILQ_INSERT_TAIL(&ctl_softc->task_queue,
876 &taskio->io_hdr, links);
877 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
881 /* Persistent Reserve action which needs attention */
882 case CTL_MSG_PERS_ACTION:
883 presio = (struct ctl_prio *)ctl_alloc_io(
884 (void *)ctl_softc->othersc_pool);
885 if (presio == NULL) {
886 printf("ctl_isc_event_handler: can't allocate "
889 /* Need to set busy and send msg back */
890 mtx_unlock(&ctl_softc->ctl_lock);
893 ctl_zero_io((union ctl_io *)presio);
894 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
895 presio->pr_msg = msg_info.pr;
896 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
897 &presio->io_hdr, links);
900 case CTL_MSG_SYNC_FE:
903 case CTL_MSG_APS_LOCK: {
904 // It's quicker to execute this then to
907 struct ctl_page_index *page_index;
908 struct copan_aps_subpage *current_sp;
911 targ_lun = msg_info.hdr.nexus.targ_lun;
912 if (msg_info.hdr.nexus.lun_map_fn != NULL)
913 targ_lun = msg_info.hdr.nexus.lun_map_fn(msg_info.hdr.nexus.lun_map_arg, targ_lun);
915 lun = ctl_softc->ctl_luns[targ_lun];
916 page_index = &lun->mode_pages.index[index_to_aps_page];
917 current_sp = (struct copan_aps_subpage *)
918 (page_index->page_data +
919 (page_index->page_len * CTL_PAGE_CURRENT));
921 current_sp->lock_active = msg_info.aps.lock_flag;
925 printf("How did I get here?\n");
927 mtx_unlock(&ctl_softc->ctl_lock);
928 } else if (event == CTL_HA_EVT_MSG_SENT) {
929 if (param != CTL_HA_STATUS_SUCCESS) {
930 printf("Bad status from ctl_ha_msg_send status %d\n",
934 } else if (event == CTL_HA_EVT_DISCONNECT) {
935 printf("CTL: Got a disconnect from Isc\n");
938 printf("ctl_isc_event_handler: Unknown event %d\n", event);
947 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
949 struct scsi_sense_data *sense;
951 sense = &dest->scsiio.sense_data;
952 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
953 dest->scsiio.scsi_status = src->scsi.scsi_status;
954 dest->scsiio.sense_len = src->scsi.sense_len;
955 dest->io_hdr.status = src->hdr.status;
961 struct ctl_softc *softc;
962 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
963 struct ctl_frontend *fe;
965 int i, error, retval;
972 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
974 softc = control_softc;
976 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
979 softc->dev->si_drv1 = softc;
982 * By default, return a "bad LUN" peripheral qualifier for unknown
983 * LUNs. The user can override this default using the tunable or
984 * sysctl. See the comment in ctl_inquiry_std() for more details.
986 softc->inquiry_pq_no_lun = 1;
987 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
988 &softc->inquiry_pq_no_lun);
989 sysctl_ctx_init(&softc->sysctl_ctx);
990 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
991 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
992 CTLFLAG_RD, 0, "CAM Target Layer");
994 if (softc->sysctl_tree == NULL) {
995 printf("%s: unable to allocate sysctl tree\n", __func__);
996 destroy_dev(softc->dev);
997 free(control_softc, M_DEVBUF);
998 control_softc = NULL;
1002 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1003 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1004 "inquiry_pq_no_lun", CTLFLAG_RW,
1005 &softc->inquiry_pq_no_lun, 0,
1006 "Report no lun possible for invalid LUNs");
1008 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1009 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
1010 softc->open_count = 0;
1013 * Default to actually sending a SYNCHRONIZE CACHE command down to
1016 softc->flags = CTL_FLAG_REAL_SYNC;
1019 * In Copan's HA scheme, the "master" and "slave" roles are
1020 * figured out through the slot the controller is in. Although it
1021 * is an active/active system, someone has to be in charge.
1024 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1028 softc->flags |= CTL_FLAG_MASTER_SHELF;
1031 persis_offset = CTL_MAX_INITIATORS;
1034 * XXX KDM need to figure out where we want to get our target ID
1035 * and WWID. Is it different on each port?
1037 softc->target.id = 0;
1038 softc->target.wwid[0] = 0x12345678;
1039 softc->target.wwid[1] = 0x87654321;
1040 STAILQ_INIT(&softc->lun_list);
1041 STAILQ_INIT(&softc->pending_lun_queue);
1042 STAILQ_INIT(&softc->task_queue);
1043 STAILQ_INIT(&softc->incoming_queue);
1044 STAILQ_INIT(&softc->rtr_queue);
1045 STAILQ_INIT(&softc->done_queue);
1046 STAILQ_INIT(&softc->isc_queue);
1047 STAILQ_INIT(&softc->fe_list);
1048 STAILQ_INIT(&softc->be_list);
1049 STAILQ_INIT(&softc->io_pools);
1052 * We don't bother calling these with ctl_lock held here, because,
1053 * in theory, no one else can try to do anything while we're in our
1054 * module init routine.
1056 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1057 &internal_pool)!= 0){
1058 printf("ctl: can't allocate %d entry internal pool, "
1059 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1063 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1064 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1065 printf("ctl: can't allocate %d entry emergency pool, "
1066 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1067 ctl_pool_free(internal_pool);
1071 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1074 printf("ctl: can't allocate %d entry other SC pool, "
1075 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1076 ctl_pool_free(internal_pool);
1077 ctl_pool_free(emergency_pool);
1081 softc->internal_pool = internal_pool;
1082 softc->emergency_pool = emergency_pool;
1083 softc->othersc_pool = other_pool;
1085 if (worker_threads > MAXCPU || worker_threads == 0) {
1086 printf("invalid kern.cam.ctl.worker_threads value; "
1089 } else if (worker_threads < 0) {
1092 * Using more than two worker threads actually hurts
1093 * performance due to lock contention.
1101 for (i = 0; i < worker_threads; i++) {
1102 error = kproc_kthread_add(ctl_work_thread, softc,
1103 &softc->work_thread, NULL, 0, 0, "ctl", "work%d", i);
1105 printf("error creating CTL work thread!\n");
1106 ctl_pool_free(internal_pool);
1107 ctl_pool_free(emergency_pool);
1108 ctl_pool_free(other_pool);
1113 printf("ctl: CAM Target Layer loaded\n");
1116 * Initialize the initiator and portname mappings
1118 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1121 * Initialize the ioctl front end.
1123 fe = &softc->ioctl_info.fe;
1124 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1125 fe->port_type = CTL_PORT_IOCTL;
1126 fe->num_requested_ctl_io = 100;
1127 fe->port_name = softc->ioctl_info.port_name;
1128 fe->port_online = ctl_ioctl_online;
1129 fe->port_offline = ctl_ioctl_offline;
1130 fe->onoff_arg = &softc->ioctl_info;
1131 fe->targ_enable = ctl_ioctl_targ_enable;
1132 fe->targ_disable = ctl_ioctl_targ_disable;
1133 fe->lun_enable = ctl_ioctl_lun_enable;
1134 fe->lun_disable = ctl_ioctl_lun_disable;
1135 fe->targ_lun_arg = &softc->ioctl_info;
1136 fe->fe_datamove = ctl_ioctl_datamove;
1137 fe->fe_done = ctl_ioctl_done;
1138 fe->max_targets = 15;
1139 fe->max_target_id = 15;
1141 if (ctl_frontend_register(&softc->ioctl_info.fe,
1142 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1143 printf("ctl: ioctl front end registration failed, will "
1144 "continue anyway\n");
1148 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1149 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1150 sizeof(struct callout), CTL_TIMER_BYTES);
1153 #endif /* CTL_IO_DELAY */
1161 struct ctl_softc *softc;
1162 struct ctl_lun *lun, *next_lun;
1163 struct ctl_io_pool *pool;
1165 softc = (struct ctl_softc *)control_softc;
1167 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1168 printf("ctl: ioctl front end deregistration failed\n");
1170 mtx_lock(&softc->ctl_lock);
1175 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1176 next_lun = STAILQ_NEXT(lun, links);
1180 mtx_unlock(&softc->ctl_lock);
1183 * This will rip the rug out from under any FETDs or anyone else
1184 * that has a pool allocated. Since we increment our module
1185 * refcount any time someone outside the main CTL module allocates
1186 * a pool, we shouldn't have any problems here. The user won't be
1187 * able to unload the CTL module until client modules have
1188 * successfully unloaded.
1190 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1191 ctl_pool_free(pool);
1194 ctl_shutdown_thread(softc->work_thread);
1197 mtx_destroy(&softc->pool_lock);
1198 mtx_destroy(&softc->ctl_lock);
1200 destroy_dev(softc->dev);
1202 sysctl_ctx_free(&softc->sysctl_ctx);
1204 free(control_softc, M_DEVBUF);
1205 control_softc = NULL;
1208 printf("ctl: CAM Target Layer unloaded\n");
1212 ctl_module_event_handler(module_t mod, int what, void *arg)
1217 return (ctl_init());
1221 return (EOPNOTSUPP);
1226 * XXX KDM should we do some access checks here? Bump a reference count to
1227 * prevent a CTL module from being unloaded while someone has it open?
1230 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1236 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1242 ctl_port_enable(ctl_port_type port_type)
1244 struct ctl_softc *softc;
1245 struct ctl_frontend *fe;
1247 if (ctl_is_single == 0) {
1248 union ctl_ha_msg msg_info;
1252 printf("%s: HA mode, synchronizing frontend enable\n",
1255 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1256 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1257 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1258 printf("Sync msg send error retval %d\n", isc_retval);
1260 if (!rcv_sync_msg) {
1261 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1262 sizeof(msg_info), 1);
1265 printf("CTL:Frontend Enable\n");
1267 printf("%s: single mode, skipping frontend synchronization\n",
1272 softc = control_softc;
1274 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1275 if (port_type & fe->port_type)
1278 printf("port %d\n", fe->targ_port);
1280 ctl_frontend_online(fe);
1288 ctl_port_disable(ctl_port_type port_type)
1290 struct ctl_softc *softc;
1291 struct ctl_frontend *fe;
1293 softc = control_softc;
1295 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1296 if (port_type & fe->port_type)
1297 ctl_frontend_offline(fe);
1304 * Returns 0 for success, 1 for failure.
1305 * Currently the only failure mode is if there aren't enough entries
1306 * allocated. So, in case of a failure, look at num_entries_dropped,
1307 * reallocate and try again.
1310 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1311 int *num_entries_filled, int *num_entries_dropped,
1312 ctl_port_type port_type, int no_virtual)
1314 struct ctl_softc *softc;
1315 struct ctl_frontend *fe;
1316 int entries_dropped, entries_filled;
1320 softc = control_softc;
1324 entries_dropped = 0;
1327 mtx_lock(&softc->ctl_lock);
1328 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1329 struct ctl_port_entry *entry;
1331 if ((fe->port_type & port_type) == 0)
1334 if ((no_virtual != 0)
1335 && (fe->virtual_port != 0))
1338 if (entries_filled >= num_entries_alloced) {
1342 entry = &entries[i];
1344 entry->port_type = fe->port_type;
1345 strlcpy(entry->port_name, fe->port_name,
1346 sizeof(entry->port_name));
1347 entry->physical_port = fe->physical_port;
1348 entry->virtual_port = fe->virtual_port;
1349 entry->wwnn = fe->wwnn;
1350 entry->wwpn = fe->wwpn;
1356 mtx_unlock(&softc->ctl_lock);
1358 if (entries_dropped > 0)
1361 *num_entries_dropped = entries_dropped;
1362 *num_entries_filled = entries_filled;
1368 ctl_ioctl_online(void *arg)
1370 struct ctl_ioctl_info *ioctl_info;
1372 ioctl_info = (struct ctl_ioctl_info *)arg;
1374 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1378 ctl_ioctl_offline(void *arg)
1380 struct ctl_ioctl_info *ioctl_info;
1382 ioctl_info = (struct ctl_ioctl_info *)arg;
1384 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1388 * Remove an initiator by port number and initiator ID.
1389 * Returns 0 for success, 1 for failure.
1392 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1394 struct ctl_softc *softc;
1396 softc = control_softc;
1398 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1401 || (targ_port > CTL_MAX_PORTS)) {
1402 printf("%s: invalid port number %d\n", __func__, targ_port);
1405 if (iid > CTL_MAX_INIT_PER_PORT) {
1406 printf("%s: initiator ID %u > maximun %u!\n",
1407 __func__, iid, CTL_MAX_INIT_PER_PORT);
1411 mtx_lock(&softc->ctl_lock);
1413 softc->wwpn_iid[targ_port][iid].in_use = 0;
1415 mtx_unlock(&softc->ctl_lock);
1421 * Add an initiator to the initiator map.
1422 * Returns 0 for success, 1 for failure.
1425 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1427 struct ctl_softc *softc;
1430 softc = control_softc;
1432 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1437 || (targ_port > CTL_MAX_PORTS)) {
1438 printf("%s: invalid port number %d\n", __func__, targ_port);
1441 if (iid > CTL_MAX_INIT_PER_PORT) {
1442 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1443 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1447 mtx_lock(&softc->ctl_lock);
1449 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1451 * We don't treat this as an error.
1453 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1454 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1455 __func__, targ_port, iid, (uintmax_t)wwpn);
1460 * This is an error, but what do we do about it? The
1461 * driver is telling us we have a new WWPN for this
1462 * initiator ID, so we pretty much need to use it.
1464 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1465 "still at that address\n", __func__, targ_port, iid,
1467 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1470 * XXX KDM clear have_ca and ua_pending on each LUN for
1474 softc->wwpn_iid[targ_port][iid].in_use = 1;
1475 softc->wwpn_iid[targ_port][iid].iid = iid;
1476 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1477 softc->wwpn_iid[targ_port][iid].port = targ_port;
1481 mtx_unlock(&softc->ctl_lock);
1487 * XXX KDM should we pretend to do something in the target/lun
1488 * enable/disable functions?
1491 ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1497 ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1503 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1509 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1515 * Data movement routine for the CTL ioctl frontend port.
1518 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1520 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1521 struct ctl_sg_entry ext_entry, kern_entry;
1522 int ext_sglen, ext_sg_entries, kern_sg_entries;
1523 int ext_sg_start, ext_offset;
1524 int len_to_copy, len_copied;
1525 int kern_watermark, ext_watermark;
1526 int ext_sglist_malloced;
1529 ext_sglist_malloced = 0;
1533 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1536 * If this flag is set, fake the data transfer.
1538 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1539 ctsio->ext_data_filled = ctsio->ext_data_len;
1544 * To simplify things here, if we have a single buffer, stick it in
1545 * a S/G entry and just make it a single entry S/G list.
1547 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1550 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1552 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1554 ext_sglist_malloced = 1;
1555 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1557 ctl_set_internal_failure(ctsio,
1562 ext_sg_entries = ctsio->ext_sg_entries;
1564 for (i = 0; i < ext_sg_entries; i++) {
1565 if ((len_seen + ext_sglist[i].len) >=
1566 ctsio->ext_data_filled) {
1568 ext_offset = ctsio->ext_data_filled - len_seen;
1571 len_seen += ext_sglist[i].len;
1574 ext_sglist = &ext_entry;
1575 ext_sglist->addr = ctsio->ext_data_ptr;
1576 ext_sglist->len = ctsio->ext_data_len;
1579 ext_offset = ctsio->ext_data_filled;
1582 if (ctsio->kern_sg_entries > 0) {
1583 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1584 kern_sg_entries = ctsio->kern_sg_entries;
1586 kern_sglist = &kern_entry;
1587 kern_sglist->addr = ctsio->kern_data_ptr;
1588 kern_sglist->len = ctsio->kern_data_len;
1589 kern_sg_entries = 1;
1594 ext_watermark = ext_offset;
1596 for (i = ext_sg_start, j = 0;
1597 i < ext_sg_entries && j < kern_sg_entries;) {
1598 uint8_t *ext_ptr, *kern_ptr;
1600 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1601 kern_sglist[j].len - kern_watermark);
1603 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1604 ext_ptr = ext_ptr + ext_watermark;
1605 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1609 panic("need to implement bus address support");
1611 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1614 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1615 kern_ptr = kern_ptr + kern_watermark;
1617 kern_watermark += len_to_copy;
1618 ext_watermark += len_to_copy;
1620 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1622 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1623 "bytes to user\n", len_to_copy));
1624 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1625 "to %p\n", kern_ptr, ext_ptr));
1626 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1627 ctl_set_internal_failure(ctsio,
1633 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1634 "bytes from user\n", len_to_copy));
1635 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1636 "to %p\n", ext_ptr, kern_ptr));
1637 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1638 ctl_set_internal_failure(ctsio,
1645 len_copied += len_to_copy;
1647 if (ext_sglist[i].len == ext_watermark) {
1652 if (kern_sglist[j].len == kern_watermark) {
1658 ctsio->ext_data_filled += len_copied;
1660 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1661 "kern_sg_entries: %d\n", ext_sg_entries,
1663 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1664 "kern_data_len = %d\n", ctsio->ext_data_len,
1665 ctsio->kern_data_len));
1668 /* XXX KDM set residual?? */
1671 if (ext_sglist_malloced != 0)
1672 free(ext_sglist, M_CTL);
1674 return (CTL_RETVAL_COMPLETE);
1678 * Serialize a command that went down the "wrong" side, and so was sent to
1679 * this controller for execution. The logic is a little different than the
1680 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1681 * sent back to the other side, but in the success case, we execute the
1682 * command on this side (XFER mode) or tell the other side to execute it
1686 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1688 struct ctl_softc *ctl_softc;
1689 union ctl_ha_msg msg_info;
1690 struct ctl_lun *lun;
1694 ctl_softc = control_softc;
1696 mtx_lock(&ctl_softc->ctl_lock);
1698 targ_lun = ctsio->io_hdr.nexus.targ_lun;
1699 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
1700 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
1701 lun = ctl_softc->ctl_luns[targ_lun];
1705 * Why isn't LUN defined? The other side wouldn't
1706 * send a cmd if the LUN is undefined.
1708 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1710 /* "Logical unit not supported" */
1711 ctl_set_sense_data(&msg_info.scsi.sense_data,
1713 /*sense_format*/SSD_TYPE_NONE,
1714 /*current_error*/ 1,
1715 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1720 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1721 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1722 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1723 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1724 msg_info.hdr.serializing_sc = NULL;
1725 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1726 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1727 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1730 mtx_unlock(&ctl_softc->ctl_lock);
1735 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1737 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1738 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1740 case CTL_ACTION_BLOCK:
1741 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1742 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1745 case CTL_ACTION_PASS:
1746 case CTL_ACTION_SKIP:
1747 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1748 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1749 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1750 &ctsio->io_hdr, links);
1753 /* send msg back to other side */
1754 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1755 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1756 msg_info.hdr.msg_type = CTL_MSG_R2R;
1758 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1760 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1761 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1765 case CTL_ACTION_OVERLAP:
1766 /* OVERLAPPED COMMANDS ATTEMPTED */
1767 ctl_set_sense_data(&msg_info.scsi.sense_data,
1769 /*sense_format*/SSD_TYPE_NONE,
1770 /*current_error*/ 1,
1771 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1776 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1777 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1778 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1779 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1780 msg_info.hdr.serializing_sc = NULL;
1781 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1783 printf("BAD JUJU:Major Bummer Overlap\n");
1785 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1787 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1788 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1791 case CTL_ACTION_OVERLAP_TAG:
1792 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1793 ctl_set_sense_data(&msg_info.scsi.sense_data,
1795 /*sense_format*/SSD_TYPE_NONE,
1796 /*current_error*/ 1,
1797 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1799 /*ascq*/ ctsio->tag_num & 0xff,
1802 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1803 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1804 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1805 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1806 msg_info.hdr.serializing_sc = NULL;
1807 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1809 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1811 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1813 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1814 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1817 case CTL_ACTION_ERROR:
1819 /* "Internal target failure" */
1820 ctl_set_sense_data(&msg_info.scsi.sense_data,
1822 /*sense_format*/SSD_TYPE_NONE,
1823 /*current_error*/ 1,
1824 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1829 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1830 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1831 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1832 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1833 msg_info.hdr.serializing_sc = NULL;
1834 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1836 printf("BAD JUJU:Major Bummer HW Error\n");
1838 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1840 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1841 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1846 mtx_unlock(&ctl_softc->ctl_lock);
1851 ctl_ioctl_submit_wait(union ctl_io *io)
1853 struct ctl_fe_ioctl_params params;
1854 ctl_fe_ioctl_state last_state;
1859 bzero(¶ms, sizeof(params));
1861 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1862 cv_init(¶ms.sem, "ctlioccv");
1863 params.state = CTL_IOCTL_INPROG;
1864 last_state = params.state;
1866 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1868 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1870 /* This shouldn't happen */
1871 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1877 mtx_lock(¶ms.ioctl_mtx);
1879 * Check the state here, and don't sleep if the state has
1880 * already changed (i.e. wakeup has already occured, but we
1881 * weren't waiting yet).
1883 if (params.state == last_state) {
1884 /* XXX KDM cv_wait_sig instead? */
1885 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1887 last_state = params.state;
1889 switch (params.state) {
1890 case CTL_IOCTL_INPROG:
1891 /* Why did we wake up? */
1892 /* XXX KDM error here? */
1893 mtx_unlock(¶ms.ioctl_mtx);
1895 case CTL_IOCTL_DATAMOVE:
1896 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1899 * change last_state back to INPROG to avoid
1900 * deadlock on subsequent data moves.
1902 params.state = last_state = CTL_IOCTL_INPROG;
1904 mtx_unlock(¶ms.ioctl_mtx);
1905 ctl_ioctl_do_datamove(&io->scsiio);
1907 * Note that in some cases, most notably writes,
1908 * this will queue the I/O and call us back later.
1909 * In other cases, generally reads, this routine
1910 * will immediately call back and wake us up,
1911 * probably using our own context.
1913 io->scsiio.be_move_done(io);
1915 case CTL_IOCTL_DONE:
1916 mtx_unlock(¶ms.ioctl_mtx);
1917 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1921 mtx_unlock(¶ms.ioctl_mtx);
1922 /* XXX KDM error here? */
1925 } while (done == 0);
1927 mtx_destroy(¶ms.ioctl_mtx);
1928 cv_destroy(¶ms.sem);
1930 return (CTL_RETVAL_COMPLETE);
1934 ctl_ioctl_datamove(union ctl_io *io)
1936 struct ctl_fe_ioctl_params *params;
1938 params = (struct ctl_fe_ioctl_params *)
1939 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1941 mtx_lock(¶ms->ioctl_mtx);
1942 params->state = CTL_IOCTL_DATAMOVE;
1943 cv_broadcast(¶ms->sem);
1944 mtx_unlock(¶ms->ioctl_mtx);
1948 ctl_ioctl_done(union ctl_io *io)
1950 struct ctl_fe_ioctl_params *params;
1952 params = (struct ctl_fe_ioctl_params *)
1953 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1955 mtx_lock(¶ms->ioctl_mtx);
1956 params->state = CTL_IOCTL_DONE;
1957 cv_broadcast(¶ms->sem);
1958 mtx_unlock(¶ms->ioctl_mtx);
1962 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1964 struct ctl_fe_ioctl_startstop_info *sd_info;
1966 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1968 sd_info->hs_info.status = metatask->status;
1969 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1970 sd_info->hs_info.luns_complete =
1971 metatask->taskinfo.startstop.luns_complete;
1972 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1974 cv_broadcast(&sd_info->sem);
1978 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1980 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1982 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1984 mtx_lock(fe_bbr_info->lock);
1985 fe_bbr_info->bbr_info->status = metatask->status;
1986 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1987 fe_bbr_info->wakeup_done = 1;
1988 mtx_unlock(fe_bbr_info->lock);
1990 cv_broadcast(&fe_bbr_info->sem);
1994 * Returns 0 for success, errno for failure.
1997 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1998 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2005 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
2007 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2008 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2010 struct ctl_ooa_entry *entry;
2013 * If we've got more than we can fit, just count the
2014 * remaining entries.
2016 if (*cur_fill_num >= ooa_hdr->alloc_num)
2019 entry = &kern_entries[*cur_fill_num];
2021 entry->tag_num = io->scsiio.tag_num;
2022 entry->lun_num = lun->lun;
2024 entry->start_bt = io->io_hdr.start_bt;
2026 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2027 entry->cdb_len = io->scsiio.cdb_len;
2028 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2029 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2031 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2032 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2034 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2035 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2037 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2038 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2040 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2041 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2048 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2049 size_t error_str_len)
2053 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2055 if (copyin(user_addr, kptr, len) != 0) {
2056 snprintf(error_str, error_str_len, "Error copying %d bytes "
2057 "from user address %p to kernel address %p", len,
2067 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2071 if (be_args == NULL)
2074 for (i = 0; i < num_be_args; i++) {
2075 free(be_args[i].kname, M_CTL);
2076 free(be_args[i].kvalue, M_CTL);
2079 free(be_args, M_CTL);
2082 static struct ctl_be_arg *
2083 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2084 char *error_str, size_t error_str_len)
2086 struct ctl_be_arg *args;
2089 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2090 error_str, error_str_len);
2095 for (i = 0; i < num_be_args; i++) {
2096 args[i].kname = NULL;
2097 args[i].kvalue = NULL;
2100 for (i = 0; i < num_be_args; i++) {
2103 args[i].kname = ctl_copyin_alloc(args[i].name,
2104 args[i].namelen, error_str, error_str_len);
2105 if (args[i].kname == NULL)
2108 if (args[i].kname[args[i].namelen - 1] != '\0') {
2109 snprintf(error_str, error_str_len, "Argument %d "
2110 "name is not NUL-terminated", i);
2114 args[i].kvalue = NULL;
2116 tmpptr = ctl_copyin_alloc(args[i].value,
2117 args[i].vallen, error_str, error_str_len);
2121 args[i].kvalue = tmpptr;
2123 if ((args[i].flags & CTL_BEARG_ASCII)
2124 && (tmpptr[args[i].vallen - 1] != '\0')) {
2125 snprintf(error_str, error_str_len, "Argument %d "
2126 "value is not NUL-terminated", i);
2134 ctl_free_args(num_be_args, args);
2140 * Escape characters that are illegal or not recommended in XML.
2143 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2149 for (; *str; str++) {
2152 retval = sbuf_printf(sb, "&");
2155 retval = sbuf_printf(sb, ">");
2158 retval = sbuf_printf(sb, "<");
2161 retval = sbuf_putc(sb, *str);
2174 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2177 struct ctl_softc *softc;
2180 softc = control_softc;
2190 * If we haven't been "enabled", don't allow any SCSI I/O
2193 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2198 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2200 printf("ctl_ioctl: can't allocate ctl_io!\n");
2206 * Need to save the pool reference so it doesn't get
2207 * spammed by the user's ctl_io.
2209 pool_tmp = io->io_hdr.pool;
2211 memcpy(io, (void *)addr, sizeof(*io));
2213 io->io_hdr.pool = pool_tmp;
2215 * No status yet, so make sure the status is set properly.
2217 io->io_hdr.status = CTL_STATUS_NONE;
2220 * The user sets the initiator ID, target and LUN IDs.
2222 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2223 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2224 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2225 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2226 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2228 retval = ctl_ioctl_submit_wait(io);
2235 memcpy((void *)addr, io, sizeof(*io));
2237 /* return this to our pool */
2242 case CTL_ENABLE_PORT:
2243 case CTL_DISABLE_PORT:
2244 case CTL_SET_PORT_WWNS: {
2245 struct ctl_frontend *fe;
2246 struct ctl_port_entry *entry;
2248 entry = (struct ctl_port_entry *)addr;
2250 mtx_lock(&softc->ctl_lock);
2251 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2257 if ((entry->port_type == CTL_PORT_NONE)
2258 && (entry->targ_port == fe->targ_port)) {
2260 * If the user only wants to enable or
2261 * disable or set WWNs on a specific port,
2262 * do the operation and we're done.
2266 } else if (entry->port_type & fe->port_type) {
2268 * Compare the user's type mask with the
2269 * particular frontend type to see if we
2276 * Make sure the user isn't trying to set
2277 * WWNs on multiple ports at the same time.
2279 if (cmd == CTL_SET_PORT_WWNS) {
2280 printf("%s: Can't set WWNs on "
2281 "multiple ports\n", __func__);
2288 * XXX KDM we have to drop the lock here,
2289 * because the online/offline operations
2290 * can potentially block. We need to
2291 * reference count the frontends so they
2294 mtx_unlock(&softc->ctl_lock);
2296 if (cmd == CTL_ENABLE_PORT) {
2297 struct ctl_lun *lun;
2299 STAILQ_FOREACH(lun, &softc->lun_list,
2301 fe->lun_enable(fe->targ_lun_arg,
2306 ctl_frontend_online(fe);
2307 } else if (cmd == CTL_DISABLE_PORT) {
2308 struct ctl_lun *lun;
2310 ctl_frontend_offline(fe);
2312 STAILQ_FOREACH(lun, &softc->lun_list,
2321 mtx_lock(&softc->ctl_lock);
2323 if (cmd == CTL_SET_PORT_WWNS)
2324 ctl_frontend_set_wwns(fe,
2325 (entry->flags & CTL_PORT_WWNN_VALID) ?
2327 (entry->flags & CTL_PORT_WWPN_VALID) ?
2328 1 : 0, entry->wwpn);
2333 mtx_unlock(&softc->ctl_lock);
2336 case CTL_GET_PORT_LIST: {
2337 struct ctl_frontend *fe;
2338 struct ctl_port_list *list;
2341 list = (struct ctl_port_list *)addr;
2343 if (list->alloc_len != (list->alloc_num *
2344 sizeof(struct ctl_port_entry))) {
2345 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2346 "alloc_num %u * sizeof(struct ctl_port_entry) "
2347 "%zu\n", __func__, list->alloc_len,
2348 list->alloc_num, sizeof(struct ctl_port_entry));
2354 list->dropped_num = 0;
2356 mtx_lock(&softc->ctl_lock);
2357 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2358 struct ctl_port_entry entry, *list_entry;
2360 if (list->fill_num >= list->alloc_num) {
2361 list->dropped_num++;
2365 entry.port_type = fe->port_type;
2366 strlcpy(entry.port_name, fe->port_name,
2367 sizeof(entry.port_name));
2368 entry.targ_port = fe->targ_port;
2369 entry.physical_port = fe->physical_port;
2370 entry.virtual_port = fe->virtual_port;
2371 entry.wwnn = fe->wwnn;
2372 entry.wwpn = fe->wwpn;
2373 if (fe->status & CTL_PORT_STATUS_ONLINE)
2378 list_entry = &list->entries[i];
2380 retval = copyout(&entry, list_entry, sizeof(entry));
2382 printf("%s: CTL_GET_PORT_LIST: copyout "
2383 "returned %d\n", __func__, retval);
2388 list->fill_len += sizeof(entry);
2390 mtx_unlock(&softc->ctl_lock);
2393 * If this is non-zero, we had a copyout fault, so there's
2394 * probably no point in attempting to set the status inside
2400 if (list->dropped_num > 0)
2401 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2403 list->status = CTL_PORT_LIST_OK;
2406 case CTL_DUMP_OOA: {
2407 struct ctl_lun *lun;
2412 mtx_lock(&softc->ctl_lock);
2413 printf("Dumping OOA queues:\n");
2414 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2415 for (io = (union ctl_io *)TAILQ_FIRST(
2416 &lun->ooa_queue); io != NULL;
2417 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2419 sbuf_new(&sb, printbuf, sizeof(printbuf),
2421 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2425 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2427 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2429 CTL_FLAG_ABORT) ? " ABORT" : "",
2431 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2432 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2434 printf("%s\n", sbuf_data(&sb));
2437 printf("OOA queues dump done\n");
2438 mtx_unlock(&softc->ctl_lock);
2442 struct ctl_lun *lun;
2443 struct ctl_ooa *ooa_hdr;
2444 struct ctl_ooa_entry *entries;
2445 uint32_t cur_fill_num;
2447 ooa_hdr = (struct ctl_ooa *)addr;
2449 if ((ooa_hdr->alloc_len == 0)
2450 || (ooa_hdr->alloc_num == 0)) {
2451 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2452 "must be non-zero\n", __func__,
2453 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2458 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2459 sizeof(struct ctl_ooa_entry))) {
2460 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2461 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2462 __func__, ooa_hdr->alloc_len,
2463 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2468 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2469 if (entries == NULL) {
2470 printf("%s: could not allocate %d bytes for OOA "
2471 "dump\n", __func__, ooa_hdr->alloc_len);
2476 mtx_lock(&softc->ctl_lock);
2477 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2478 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2479 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2480 mtx_unlock(&softc->ctl_lock);
2481 free(entries, M_CTL);
2482 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2483 __func__, (uintmax_t)ooa_hdr->lun_num);
2490 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2491 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2492 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2498 mtx_unlock(&softc->ctl_lock);
2499 free(entries, M_CTL);
2503 lun = softc->ctl_luns[ooa_hdr->lun_num];
2505 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2508 mtx_unlock(&softc->ctl_lock);
2510 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2511 ooa_hdr->fill_len = ooa_hdr->fill_num *
2512 sizeof(struct ctl_ooa_entry);
2513 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2515 printf("%s: error copying out %d bytes for OOA dump\n",
2516 __func__, ooa_hdr->fill_len);
2519 getbintime(&ooa_hdr->cur_bt);
2521 if (cur_fill_num > ooa_hdr->alloc_num) {
2522 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2523 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2525 ooa_hdr->dropped_num = 0;
2526 ooa_hdr->status = CTL_OOA_OK;
2529 free(entries, M_CTL);
2532 case CTL_CHECK_OOA: {
2534 struct ctl_lun *lun;
2535 struct ctl_ooa_info *ooa_info;
2538 ooa_info = (struct ctl_ooa_info *)addr;
2540 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2541 ooa_info->status = CTL_OOA_INVALID_LUN;
2544 mtx_lock(&softc->ctl_lock);
2545 lun = softc->ctl_luns[ooa_info->lun_id];
2547 mtx_unlock(&softc->ctl_lock);
2548 ooa_info->status = CTL_OOA_INVALID_LUN;
2552 ooa_info->num_entries = 0;
2553 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2554 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2555 &io->io_hdr, ooa_links)) {
2556 ooa_info->num_entries++;
2559 mtx_unlock(&softc->ctl_lock);
2560 ooa_info->status = CTL_OOA_SUCCESS;
2564 case CTL_HARD_START:
2565 case CTL_HARD_STOP: {
2566 struct ctl_fe_ioctl_startstop_info ss_info;
2567 struct cfi_metatask *metatask;
2570 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2572 cv_init(&ss_info.sem, "hard start/stop cv" );
2574 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2575 if (metatask == NULL) {
2577 mtx_destroy(&hs_mtx);
2581 if (cmd == CTL_HARD_START)
2582 metatask->tasktype = CFI_TASK_STARTUP;
2584 metatask->tasktype = CFI_TASK_SHUTDOWN;
2586 metatask->callback = ctl_ioctl_hard_startstop_callback;
2587 metatask->callback_arg = &ss_info;
2589 cfi_action(metatask);
2591 /* Wait for the callback */
2593 cv_wait_sig(&ss_info.sem, &hs_mtx);
2594 mtx_unlock(&hs_mtx);
2597 * All information has been copied from the metatask by the
2598 * time cv_broadcast() is called, so we free the metatask here.
2600 cfi_free_metatask(metatask);
2602 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2604 mtx_destroy(&hs_mtx);
2608 struct ctl_bbrread_info *bbr_info;
2609 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2611 struct cfi_metatask *metatask;
2613 bbr_info = (struct ctl_bbrread_info *)addr;
2615 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2617 bzero(&bbr_mtx, sizeof(bbr_mtx));
2618 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2620 fe_bbr_info.bbr_info = bbr_info;
2621 fe_bbr_info.lock = &bbr_mtx;
2623 cv_init(&fe_bbr_info.sem, "BBR read cv");
2624 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2626 if (metatask == NULL) {
2627 mtx_destroy(&bbr_mtx);
2628 cv_destroy(&fe_bbr_info.sem);
2632 metatask->tasktype = CFI_TASK_BBRREAD;
2633 metatask->callback = ctl_ioctl_bbrread_callback;
2634 metatask->callback_arg = &fe_bbr_info;
2635 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2636 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2637 metatask->taskinfo.bbrread.len = bbr_info->len;
2639 cfi_action(metatask);
2642 while (fe_bbr_info.wakeup_done == 0)
2643 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2644 mtx_unlock(&bbr_mtx);
2646 bbr_info->status = metatask->status;
2647 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2648 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2649 memcpy(&bbr_info->sense_data,
2650 &metatask->taskinfo.bbrread.sense_data,
2651 ctl_min(sizeof(bbr_info->sense_data),
2652 sizeof(metatask->taskinfo.bbrread.sense_data)));
2654 cfi_free_metatask(metatask);
2656 mtx_destroy(&bbr_mtx);
2657 cv_destroy(&fe_bbr_info.sem);
2661 case CTL_DELAY_IO: {
2662 struct ctl_io_delay_info *delay_info;
2664 struct ctl_lun *lun;
2665 #endif /* CTL_IO_DELAY */
2667 delay_info = (struct ctl_io_delay_info *)addr;
2670 mtx_lock(&softc->ctl_lock);
2672 if ((delay_info->lun_id > CTL_MAX_LUNS)
2673 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2674 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2676 lun = softc->ctl_luns[delay_info->lun_id];
2678 delay_info->status = CTL_DELAY_STATUS_OK;
2680 switch (delay_info->delay_type) {
2681 case CTL_DELAY_TYPE_CONT:
2683 case CTL_DELAY_TYPE_ONESHOT:
2686 delay_info->status =
2687 CTL_DELAY_STATUS_INVALID_TYPE;
2691 switch (delay_info->delay_loc) {
2692 case CTL_DELAY_LOC_DATAMOVE:
2693 lun->delay_info.datamove_type =
2694 delay_info->delay_type;
2695 lun->delay_info.datamove_delay =
2696 delay_info->delay_secs;
2698 case CTL_DELAY_LOC_DONE:
2699 lun->delay_info.done_type =
2700 delay_info->delay_type;
2701 lun->delay_info.done_delay =
2702 delay_info->delay_secs;
2705 delay_info->status =
2706 CTL_DELAY_STATUS_INVALID_LOC;
2711 mtx_unlock(&softc->ctl_lock);
2713 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2714 #endif /* CTL_IO_DELAY */
2717 case CTL_REALSYNC_SET: {
2720 syncstate = (int *)addr;
2722 mtx_lock(&softc->ctl_lock);
2723 switch (*syncstate) {
2725 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2728 softc->flags |= CTL_FLAG_REAL_SYNC;
2734 mtx_unlock(&softc->ctl_lock);
2737 case CTL_REALSYNC_GET: {
2740 syncstate = (int*)addr;
2742 mtx_lock(&softc->ctl_lock);
2743 if (softc->flags & CTL_FLAG_REAL_SYNC)
2747 mtx_unlock(&softc->ctl_lock);
2753 struct ctl_sync_info *sync_info;
2754 struct ctl_lun *lun;
2756 sync_info = (struct ctl_sync_info *)addr;
2758 mtx_lock(&softc->ctl_lock);
2759 lun = softc->ctl_luns[sync_info->lun_id];
2761 mtx_unlock(&softc->ctl_lock);
2762 sync_info->status = CTL_GS_SYNC_NO_LUN;
2765 * Get or set the sync interval. We're not bounds checking
2766 * in the set case, hopefully the user won't do something
2769 if (cmd == CTL_GETSYNC)
2770 sync_info->sync_interval = lun->sync_interval;
2772 lun->sync_interval = sync_info->sync_interval;
2774 mtx_unlock(&softc->ctl_lock);
2776 sync_info->status = CTL_GS_SYNC_OK;
2780 case CTL_GETSTATS: {
2781 struct ctl_stats *stats;
2782 struct ctl_lun *lun;
2785 stats = (struct ctl_stats *)addr;
2787 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2789 stats->status = CTL_SS_NEED_MORE_SPACE;
2790 stats->num_luns = softc->num_luns;
2794 * XXX KDM no locking here. If the LUN list changes,
2795 * things can blow up.
2797 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2798 i++, lun = STAILQ_NEXT(lun, links)) {
2799 retval = copyout(&lun->stats, &stats->lun_stats[i],
2800 sizeof(lun->stats));
2804 stats->num_luns = softc->num_luns;
2805 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2807 stats->status = CTL_SS_OK;
2809 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2811 stats->flags = CTL_STATS_FLAG_NONE;
2813 getnanouptime(&stats->timestamp);
2816 case CTL_ERROR_INJECT: {
2817 struct ctl_error_desc *err_desc, *new_err_desc;
2818 struct ctl_lun *lun;
2820 err_desc = (struct ctl_error_desc *)addr;
2822 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2824 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2826 mtx_lock(&softc->ctl_lock);
2827 lun = softc->ctl_luns[err_desc->lun_id];
2829 mtx_unlock(&softc->ctl_lock);
2830 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2831 __func__, (uintmax_t)err_desc->lun_id);
2837 * We could do some checking here to verify the validity
2838 * of the request, but given the complexity of error
2839 * injection requests, the checking logic would be fairly
2842 * For now, if the request is invalid, it just won't get
2843 * executed and might get deleted.
2845 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2848 * XXX KDM check to make sure the serial number is unique,
2849 * in case we somehow manage to wrap. That shouldn't
2850 * happen for a very long time, but it's the right thing to
2853 new_err_desc->serial = lun->error_serial;
2854 err_desc->serial = lun->error_serial;
2855 lun->error_serial++;
2857 mtx_unlock(&softc->ctl_lock);
2860 case CTL_ERROR_INJECT_DELETE: {
2861 struct ctl_error_desc *delete_desc, *desc, *desc2;
2862 struct ctl_lun *lun;
2865 delete_desc = (struct ctl_error_desc *)addr;
2868 mtx_lock(&softc->ctl_lock);
2869 lun = softc->ctl_luns[delete_desc->lun_id];
2871 mtx_unlock(&softc->ctl_lock);
2872 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2873 __func__, (uintmax_t)delete_desc->lun_id);
2877 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2878 if (desc->serial != delete_desc->serial)
2881 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2886 mtx_unlock(&softc->ctl_lock);
2887 if (delete_done == 0) {
2888 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2889 "error serial %ju on LUN %u\n", __func__,
2890 delete_desc->serial, delete_desc->lun_id);
2896 case CTL_DUMP_STRUCTS: {
2898 struct ctl_frontend *fe;
2900 printf("CTL IID to WWPN map start:\n");
2901 for (i = 0; i < CTL_MAX_PORTS; i++) {
2902 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2903 if (softc->wwpn_iid[i][j].in_use == 0)
2906 printf("port %d iid %u WWPN %#jx\n",
2907 softc->wwpn_iid[i][j].port,
2908 softc->wwpn_iid[i][j].iid,
2909 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2912 printf("CTL IID to WWPN map end\n");
2913 printf("CTL Persistent Reservation information start:\n");
2914 for (i = 0; i < CTL_MAX_LUNS; i++) {
2915 struct ctl_lun *lun;
2917 lun = softc->ctl_luns[i];
2920 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2923 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2924 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2925 if (lun->per_res[j+k].registered == 0)
2927 printf("LUN %d port %d iid %d key "
2929 (uintmax_t)scsi_8btou64(
2930 lun->per_res[j+k].res_key.key));
2934 printf("CTL Persistent Reservation information end\n");
2935 printf("CTL Frontends:\n");
2937 * XXX KDM calling this without a lock. We'd likely want
2938 * to drop the lock before calling the frontend's dump
2941 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2942 printf("Frontend %s Type %u pport %d vport %d WWNN "
2943 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2944 fe->physical_port, fe->virtual_port,
2945 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2948 * Frontends are not required to support the dump
2951 if (fe->fe_dump == NULL)
2956 printf("CTL Frontend information end\n");
2960 struct ctl_lun_req *lun_req;
2961 struct ctl_backend_driver *backend;
2963 lun_req = (struct ctl_lun_req *)addr;
2965 backend = ctl_backend_find(lun_req->backend);
2966 if (backend == NULL) {
2967 lun_req->status = CTL_LUN_ERROR;
2968 snprintf(lun_req->error_str,
2969 sizeof(lun_req->error_str),
2970 "Backend \"%s\" not found.",
2974 if (lun_req->num_be_args > 0) {
2975 lun_req->kern_be_args = ctl_copyin_args(
2976 lun_req->num_be_args,
2979 sizeof(lun_req->error_str));
2980 if (lun_req->kern_be_args == NULL) {
2981 lun_req->status = CTL_LUN_ERROR;
2986 retval = backend->ioctl(dev, cmd, addr, flag, td);
2988 if (lun_req->num_be_args > 0) {
2989 ctl_free_args(lun_req->num_be_args,
2990 lun_req->kern_be_args);
2994 case CTL_LUN_LIST: {
2996 struct ctl_lun *lun;
2997 struct ctl_lun_list *list;
2998 struct ctl_be_lun_option *opt;
3000 list = (struct ctl_lun_list *)addr;
3003 * Allocate a fixed length sbuf here, based on the length
3004 * of the user's buffer. We could allocate an auto-extending
3005 * buffer, and then tell the user how much larger our
3006 * amount of data is than his buffer, but that presents
3009 * 1. The sbuf(9) routines use a blocking malloc, and so
3010 * we can't hold a lock while calling them with an
3011 * auto-extending buffer.
3013 * 2. There is not currently a LUN reference counting
3014 * mechanism, outside of outstanding transactions on
3015 * the LUN's OOA queue. So a LUN could go away on us
3016 * while we're getting the LUN number, backend-specific
3017 * information, etc. Thus, given the way things
3018 * currently work, we need to hold the CTL lock while
3019 * grabbing LUN information.
3021 * So, from the user's standpoint, the best thing to do is
3022 * allocate what he thinks is a reasonable buffer length,
3023 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3024 * double the buffer length and try again. (And repeat
3025 * that until he succeeds.)
3027 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3029 list->status = CTL_LUN_LIST_ERROR;
3030 snprintf(list->error_str, sizeof(list->error_str),
3031 "Unable to allocate %d bytes for LUN list",
3036 sbuf_printf(sb, "<ctllunlist>\n");
3038 mtx_lock(&softc->ctl_lock);
3040 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3041 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3042 (uintmax_t)lun->lun);
3045 * Bail out as soon as we see that we've overfilled
3051 retval = sbuf_printf(sb, "<backend_type>%s"
3052 "</backend_type>\n",
3053 (lun->backend == NULL) ? "none" :
3054 lun->backend->name);
3059 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3060 lun->be_lun->lun_type);
3065 if (lun->backend == NULL) {
3066 retval = sbuf_printf(sb, "</lun>\n");
3072 retval = sbuf_printf(sb, "<size>%ju</size>\n",
3073 (lun->be_lun->maxlba > 0) ?
3074 lun->be_lun->maxlba + 1 : 0);
3079 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3080 lun->be_lun->blocksize);
3085 retval = sbuf_printf(sb, "<serial_number>");
3090 retval = ctl_sbuf_printf_esc(sb,
3091 lun->be_lun->serial_num);
3096 retval = sbuf_printf(sb, "</serial_number>\n");
3101 retval = sbuf_printf(sb, "<device_id>");
3106 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3111 retval = sbuf_printf(sb, "</device_id>\n");
3116 if (lun->backend->lun_info != NULL) {
3117 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3121 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3122 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name);
3127 retval = sbuf_printf(sb, "</lun>\n");
3132 mtx_unlock(&softc->ctl_lock);
3135 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3138 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3139 snprintf(list->error_str, sizeof(list->error_str),
3140 "Out of space, %d bytes is too small",
3147 retval = copyout(sbuf_data(sb), list->lun_xml,
3150 list->fill_len = sbuf_len(sb) + 1;
3151 list->status = CTL_LUN_LIST_OK;
3156 struct ctl_iscsi *ci;
3157 struct ctl_frontend *fe;
3159 ci = (struct ctl_iscsi *)addr;
3161 mtx_lock(&softc->ctl_lock);
3162 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3163 if (strcmp(fe->port_name, "iscsi") == 0)
3166 mtx_unlock(&softc->ctl_lock);
3169 ci->status = CTL_ISCSI_ERROR;
3170 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found.");
3174 retval = fe->ioctl(dev, cmd, addr, flag, td);
3178 /* XXX KDM should we fix this? */
3180 struct ctl_backend_driver *backend;
3187 * We encode the backend type as the ioctl type for backend
3188 * ioctls. So parse it out here, and then search for a
3189 * backend of this type.
3191 type = _IOC_TYPE(cmd);
3193 STAILQ_FOREACH(backend, &softc->be_list, links) {
3194 if (backend->type == type) {
3200 printf("ctl: unknown ioctl command %#lx or backend "
3205 retval = backend->ioctl(dev, cmd, addr, flag, td);
3215 ctl_get_initindex(struct ctl_nexus *nexus)
3217 if (nexus->targ_port < CTL_MAX_PORTS)
3218 return (nexus->initid.id +
3219 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3221 return (nexus->initid.id +
3222 ((nexus->targ_port - CTL_MAX_PORTS) *
3223 CTL_MAX_INIT_PER_PORT));
3227 ctl_get_resindex(struct ctl_nexus *nexus)
3229 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3233 ctl_port_idx(int port_num)
3235 if (port_num < CTL_MAX_PORTS)
3238 return(port_num - CTL_MAX_PORTS);
3242 * Note: This only works for bitmask sizes that are at least 32 bits, and
3243 * that are a power of 2.
3246 ctl_ffz(uint32_t *mask, uint32_t size)
3248 uint32_t num_chunks, num_pieces;
3251 num_chunks = (size >> 5);
3252 if (num_chunks == 0)
3254 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3256 for (i = 0; i < num_chunks; i++) {
3257 for (j = 0; j < num_pieces; j++) {
3258 if ((mask[i] & (1 << j)) == 0)
3259 return ((i << 5) + j);
3267 ctl_set_mask(uint32_t *mask, uint32_t bit)
3269 uint32_t chunk, piece;
3272 piece = bit % (sizeof(uint32_t) * 8);
3274 if ((mask[chunk] & (1 << piece)) != 0)
3277 mask[chunk] |= (1 << piece);
3283 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3285 uint32_t chunk, piece;
3288 piece = bit % (sizeof(uint32_t) * 8);
3290 if ((mask[chunk] & (1 << piece)) == 0)
3293 mask[chunk] &= ~(1 << piece);
3299 ctl_is_set(uint32_t *mask, uint32_t bit)
3301 uint32_t chunk, piece;
3304 piece = bit % (sizeof(uint32_t) * 8);
3306 if ((mask[chunk] & (1 << piece)) == 0)
3314 * The bus, target and lun are optional, they can be filled in later.
3315 * can_wait is used to determine whether we can wait on the malloc or not.
3318 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3319 uint32_t targ_lun, int can_wait)
3324 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3326 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3329 io->io_hdr.io_type = io_type;
3330 io->io_hdr.targ_port = targ_port;
3332 * XXX KDM this needs to change/go away. We need to move
3333 * to a preallocated pool of ctl_scsiio structures.
3335 io->io_hdr.nexus.targ_target.id = targ_target;
3336 io->io_hdr.nexus.targ_lun = targ_lun;
3343 ctl_kfree_io(union ctl_io *io)
3350 * ctl_softc, pool_type, total_ctl_io are passed in.
3351 * npool is passed out.
3354 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3355 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3358 union ctl_io *cur_io, *next_io;
3359 struct ctl_io_pool *pool;
3364 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3371 pool->type = pool_type;
3372 pool->ctl_softc = ctl_softc;
3374 mtx_lock(&ctl_softc->pool_lock);
3375 pool->id = ctl_softc->cur_pool_id++;
3376 mtx_unlock(&ctl_softc->pool_lock);
3378 pool->flags = CTL_POOL_FLAG_NONE;
3379 pool->refcount = 1; /* Reference for validity. */
3380 STAILQ_INIT(&pool->free_queue);
3383 * XXX KDM other options here:
3384 * - allocate a page at a time
3385 * - allocate one big chunk of memory.
3386 * Page allocation might work well, but would take a little more
3389 for (i = 0; i < total_ctl_io; i++) {
3390 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3392 if (cur_io == NULL) {
3396 cur_io->io_hdr.pool = pool;
3397 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3398 pool->total_ctl_io++;
3399 pool->free_ctl_io++;
3403 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3404 cur_io != NULL; cur_io = next_io) {
3405 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3407 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3409 free(cur_io, M_CTL);
3415 mtx_lock(&ctl_softc->pool_lock);
3416 ctl_softc->num_pools++;
3417 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3419 * Increment our usage count if this is an external consumer, so we
3420 * can't get unloaded until the external consumer (most likely a
3421 * FETD) unloads and frees his pool.
3423 * XXX KDM will this increment the caller's module use count, or
3427 if ((pool_type != CTL_POOL_EMERGENCY)
3428 && (pool_type != CTL_POOL_INTERNAL)
3429 && (pool_type != CTL_POOL_IOCTL)
3430 && (pool_type != CTL_POOL_4OTHERSC))
3434 mtx_unlock(&ctl_softc->pool_lock);
3444 ctl_pool_acquire(struct ctl_io_pool *pool)
3447 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3449 if (pool->flags & CTL_POOL_FLAG_INVALID)
3458 ctl_pool_release(struct ctl_io_pool *pool)
3460 struct ctl_softc *ctl_softc = pool->ctl_softc;
3463 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3465 if (--pool->refcount != 0)
3468 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3469 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3474 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3475 ctl_softc->num_pools--;
3478 * XXX KDM will this decrement the caller's usage count or mine?
3481 if ((pool->type != CTL_POOL_EMERGENCY)
3482 && (pool->type != CTL_POOL_INTERNAL)
3483 && (pool->type != CTL_POOL_IOCTL))
3491 ctl_pool_free(struct ctl_io_pool *pool)
3493 struct ctl_softc *ctl_softc;
3498 ctl_softc = pool->ctl_softc;
3499 mtx_lock(&ctl_softc->pool_lock);
3500 pool->flags |= CTL_POOL_FLAG_INVALID;
3501 ctl_pool_release(pool);
3502 mtx_unlock(&ctl_softc->pool_lock);
3506 * This routine does not block (except for spinlocks of course).
3507 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3511 ctl_alloc_io(void *pool_ref)
3514 struct ctl_softc *ctl_softc;
3515 struct ctl_io_pool *pool, *npool;
3516 struct ctl_io_pool *emergency_pool;
3518 pool = (struct ctl_io_pool *)pool_ref;
3521 printf("%s: pool is NULL\n", __func__);
3525 emergency_pool = NULL;
3527 ctl_softc = pool->ctl_softc;
3529 mtx_lock(&ctl_softc->pool_lock);
3531 * First, try to get the io structure from the user's pool.
3533 if (ctl_pool_acquire(pool) == 0) {
3534 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3536 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3537 pool->total_allocated++;
3538 pool->free_ctl_io--;
3539 mtx_unlock(&ctl_softc->pool_lock);
3542 ctl_pool_release(pool);
3545 * If he doesn't have any io structures left, search for an
3546 * emergency pool and grab one from there.
3548 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3549 if (npool->type != CTL_POOL_EMERGENCY)
3552 if (ctl_pool_acquire(npool) != 0)
3555 emergency_pool = npool;
3557 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3559 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3560 npool->total_allocated++;
3561 npool->free_ctl_io--;
3562 mtx_unlock(&ctl_softc->pool_lock);
3565 ctl_pool_release(npool);
3568 /* Drop the spinlock before we malloc */
3569 mtx_unlock(&ctl_softc->pool_lock);
3572 * The emergency pool (if it exists) didn't have one, so try an
3573 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3578 * If the emergency pool exists but is empty, add this
3579 * ctl_io to its list when it gets freed.
3581 if (emergency_pool != NULL) {
3582 mtx_lock(&ctl_softc->pool_lock);
3583 if (ctl_pool_acquire(emergency_pool) == 0) {
3584 io->io_hdr.pool = emergency_pool;
3585 emergency_pool->total_ctl_io++;
3587 * Need to bump this, otherwise
3588 * total_allocated and total_freed won't
3589 * match when we no longer have anything
3592 emergency_pool->total_allocated++;
3594 mtx_unlock(&ctl_softc->pool_lock);
3596 io->io_hdr.pool = NULL;
3603 ctl_free_io(union ctl_io *io)
3609 * If this ctl_io has a pool, return it to that pool.
3611 if (io->io_hdr.pool != NULL) {
3612 struct ctl_io_pool *pool;
3614 struct ctl_softc *ctl_softc;
3615 union ctl_io *tmp_io;
3616 unsigned long xflags;
3619 ctl_softc = control_softc;
3622 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3624 mtx_lock(&pool->ctl_softc->pool_lock);
3628 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3629 &ctl_softc->task_queue); tmp_io != NULL; i++,
3630 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3633 printf("%s: %p is still on the task queue!\n",
3635 printf("%s: (%d): type %d "
3636 "msg %d cdb %x iptl: "
3637 "%d:%d:%d:%d tag 0x%04x "
3640 tmp_io->io_hdr.io_type,
3641 tmp_io->io_hdr.msg_type,
3642 tmp_io->scsiio.cdb[0],
3643 tmp_io->io_hdr.nexus.initid.id,
3644 tmp_io->io_hdr.nexus.targ_port,
3645 tmp_io->io_hdr.nexus.targ_target.id,
3646 tmp_io->io_hdr.nexus.targ_lun,
3647 (tmp_io->io_hdr.io_type ==
3649 tmp_io->taskio.tag_num :
3650 tmp_io->scsiio.tag_num,
3652 panic("I/O still on the task queue!");
3656 io->io_hdr.io_type = 0xff;
3657 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3658 pool->total_freed++;
3659 pool->free_ctl_io++;
3660 ctl_pool_release(pool);
3661 mtx_unlock(&pool->ctl_softc->pool_lock);
3664 * Otherwise, just free it. We probably malloced it and
3665 * the emergency pool wasn't available.
3673 ctl_zero_io(union ctl_io *io)
3681 * May need to preserve linked list pointers at some point too.
3683 pool_ref = io->io_hdr.pool;
3685 memset(io, 0, sizeof(*io));
3687 io->io_hdr.pool = pool_ref;
3691 * This routine is currently used for internal copies of ctl_ios that need
3692 * to persist for some reason after we've already returned status to the
3693 * FETD. (Thus the flag set.)
3696 * Note that this makes a blind copy of all fields in the ctl_io, except
3697 * for the pool reference. This includes any memory that has been
3698 * allocated! That memory will no longer be valid after done has been
3699 * called, so this would be VERY DANGEROUS for command that actually does
3700 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3701 * start and stop commands, which don't transfer any data, so this is not a
3702 * problem. If it is used for anything else, the caller would also need to
3703 * allocate data buffer space and this routine would need to be modified to
3704 * copy the data buffer(s) as well.
3707 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3716 * May need to preserve linked list pointers at some point too.
3718 pool_ref = dest->io_hdr.pool;
3720 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3722 dest->io_hdr.pool = pool_ref;
3724 * We need to know that this is an internal copy, and doesn't need
3725 * to get passed back to the FETD that allocated it.
3727 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3732 ctl_update_power_subpage(struct copan_power_subpage *page)
3734 int num_luns, num_partitions, config_type;
3735 struct ctl_softc *softc;
3736 cs_BOOL_t aor_present, shelf_50pct_power;
3737 cs_raidset_personality_t rs_type;
3738 int max_active_luns;
3740 softc = control_softc;
3742 /* subtract out the processor LUN */
3743 num_luns = softc->num_luns - 1;
3745 * Default to 7 LUNs active, which was the only number we allowed
3748 max_active_luns = 7;
3750 num_partitions = config_GetRsPartitionInfo();
3751 config_type = config_GetConfigType();
3752 shelf_50pct_power = config_GetShelfPowerMode();
3753 aor_present = config_IsAorRsPresent();
3755 rs_type = ddb_GetRsRaidType(1);
3756 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3757 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3758 EPRINT(0, "Unsupported RS type %d!", rs_type);
3762 page->total_luns = num_luns;
3764 switch (config_type) {
3767 * In a 40 drive configuration, it doesn't matter what DC
3768 * cards we have, whether we have AOR enabled or not,
3769 * partitioning or not, or what type of RAIDset we have.
3770 * In that scenario, we can power up every LUN we present
3773 max_active_luns = num_luns;
3777 if (shelf_50pct_power == CS_FALSE) {
3779 if (aor_present == CS_TRUE) {
3781 CS_RAIDSET_PERSONALITY_RAID5) {
3782 max_active_luns = 7;
3783 } else if (rs_type ==
3784 CS_RAIDSET_PERSONALITY_RAID1){
3785 max_active_luns = 14;
3787 /* XXX KDM now what?? */
3791 CS_RAIDSET_PERSONALITY_RAID5) {
3792 max_active_luns = 8;
3793 } else if (rs_type ==
3794 CS_RAIDSET_PERSONALITY_RAID1){
3795 max_active_luns = 16;
3797 /* XXX KDM now what?? */
3803 * With 50% power in a 64 drive configuration, we
3804 * can power all LUNs we present.
3806 max_active_luns = num_luns;
3810 if (shelf_50pct_power == CS_FALSE) {
3812 if (aor_present == CS_TRUE) {
3814 CS_RAIDSET_PERSONALITY_RAID5) {
3815 max_active_luns = 7;
3816 } else if (rs_type ==
3817 CS_RAIDSET_PERSONALITY_RAID1){
3818 max_active_luns = 14;
3820 /* XXX KDM now what?? */
3824 CS_RAIDSET_PERSONALITY_RAID5) {
3825 max_active_luns = 8;
3826 } else if (rs_type ==
3827 CS_RAIDSET_PERSONALITY_RAID1){
3828 max_active_luns = 16;
3830 /* XXX KDM now what?? */
3835 if (aor_present == CS_TRUE) {
3837 CS_RAIDSET_PERSONALITY_RAID5) {
3838 max_active_luns = 14;
3839 } else if (rs_type ==
3840 CS_RAIDSET_PERSONALITY_RAID1){
3842 * We're assuming here that disk
3843 * caching is enabled, and so we're
3844 * able to power up half of each
3845 * LUN, and cache all writes.
3847 max_active_luns = num_luns;
3849 /* XXX KDM now what?? */
3853 CS_RAIDSET_PERSONALITY_RAID5) {
3854 max_active_luns = 15;
3855 } else if (rs_type ==
3856 CS_RAIDSET_PERSONALITY_RAID1){
3857 max_active_luns = 30;
3859 /* XXX KDM now what?? */
3866 * In this case, we have an unknown configuration, so we
3867 * just use the default from above.
3872 page->max_active_luns = max_active_luns;
3874 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3875 page->total_luns, page->max_active_luns);
3878 #endif /* NEEDTOPORT */
3881 * This routine could be used in the future to load default and/or saved
3882 * mode page parameters for a particuar lun.
3885 ctl_init_page_index(struct ctl_lun *lun)
3888 struct ctl_page_index *page_index;
3889 struct ctl_softc *softc;
3891 memcpy(&lun->mode_pages.index, page_index_template,
3892 sizeof(page_index_template));
3894 softc = lun->ctl_softc;
3896 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3898 page_index = &lun->mode_pages.index[i];
3900 * If this is a disk-only mode page, there's no point in
3901 * setting it up. For some pages, we have to have some
3902 * basic information about the disk in order to calculate the
3905 if ((lun->be_lun->lun_type != T_DIRECT)
3906 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3909 switch (page_index->page_code & SMPH_PC_MASK) {
3910 case SMS_FORMAT_DEVICE_PAGE: {
3911 struct scsi_format_page *format_page;
3913 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3914 panic("subpage is incorrect!");
3917 * Sectors per track are set above. Bytes per
3918 * sector need to be set here on a per-LUN basis.
3920 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3921 &format_page_default,
3922 sizeof(format_page_default));
3923 memcpy(&lun->mode_pages.format_page[
3924 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3925 sizeof(format_page_changeable));
3926 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3927 &format_page_default,
3928 sizeof(format_page_default));
3929 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3930 &format_page_default,
3931 sizeof(format_page_default));
3933 format_page = &lun->mode_pages.format_page[
3935 scsi_ulto2b(lun->be_lun->blocksize,
3936 format_page->bytes_per_sector);
3938 format_page = &lun->mode_pages.format_page[
3940 scsi_ulto2b(lun->be_lun->blocksize,
3941 format_page->bytes_per_sector);
3943 format_page = &lun->mode_pages.format_page[
3945 scsi_ulto2b(lun->be_lun->blocksize,
3946 format_page->bytes_per_sector);
3948 page_index->page_data =
3949 (uint8_t *)lun->mode_pages.format_page;
3952 case SMS_RIGID_DISK_PAGE: {
3953 struct scsi_rigid_disk_page *rigid_disk_page;
3954 uint32_t sectors_per_cylinder;
3958 #endif /* !__XSCALE__ */
3960 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3961 panic("invalid subpage value %d",
3962 page_index->subpage);
3965 * Rotation rate and sectors per track are set
3966 * above. We calculate the cylinders here based on
3967 * capacity. Due to the number of heads and
3968 * sectors per track we're using, smaller arrays
3969 * may turn out to have 0 cylinders. Linux and
3970 * FreeBSD don't pay attention to these mode pages
3971 * to figure out capacity, but Solaris does. It
3972 * seems to deal with 0 cylinders just fine, and
3973 * works out a fake geometry based on the capacity.
3975 memcpy(&lun->mode_pages.rigid_disk_page[
3976 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3977 sizeof(rigid_disk_page_default));
3978 memcpy(&lun->mode_pages.rigid_disk_page[
3979 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3980 sizeof(rigid_disk_page_changeable));
3981 memcpy(&lun->mode_pages.rigid_disk_page[
3982 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3983 sizeof(rigid_disk_page_default));
3984 memcpy(&lun->mode_pages.rigid_disk_page[
3985 CTL_PAGE_SAVED], &rigid_disk_page_default,
3986 sizeof(rigid_disk_page_default));
3988 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3992 * The divide method here will be more accurate,
3993 * probably, but results in floating point being
3994 * used in the kernel on i386 (__udivdi3()). On the
3995 * XScale, though, __udivdi3() is implemented in
3998 * The shift method for cylinder calculation is
3999 * accurate if sectors_per_cylinder is a power of
4000 * 2. Otherwise it might be slightly off -- you
4001 * might have a bit of a truncation problem.
4004 cylinders = (lun->be_lun->maxlba + 1) /
4005 sectors_per_cylinder;
4007 for (shift = 31; shift > 0; shift--) {
4008 if (sectors_per_cylinder & (1 << shift))
4011 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4015 * We've basically got 3 bytes, or 24 bits for the
4016 * cylinder size in the mode page. If we're over,
4017 * just round down to 2^24.
4019 if (cylinders > 0xffffff)
4020 cylinders = 0xffffff;
4022 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4024 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4026 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4028 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4030 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4032 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4034 page_index->page_data =
4035 (uint8_t *)lun->mode_pages.rigid_disk_page;
4038 case SMS_CACHING_PAGE: {
4040 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4041 panic("invalid subpage value %d",
4042 page_index->subpage);
4044 * Defaults should be okay here, no calculations
4047 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4048 &caching_page_default,
4049 sizeof(caching_page_default));
4050 memcpy(&lun->mode_pages.caching_page[
4051 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4052 sizeof(caching_page_changeable));
4053 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4054 &caching_page_default,
4055 sizeof(caching_page_default));
4056 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4057 &caching_page_default,
4058 sizeof(caching_page_default));
4059 page_index->page_data =
4060 (uint8_t *)lun->mode_pages.caching_page;
4063 case SMS_CONTROL_MODE_PAGE: {
4065 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4066 panic("invalid subpage value %d",
4067 page_index->subpage);
4070 * Defaults should be okay here, no calculations
4073 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4074 &control_page_default,
4075 sizeof(control_page_default));
4076 memcpy(&lun->mode_pages.control_page[
4077 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4078 sizeof(control_page_changeable));
4079 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4080 &control_page_default,
4081 sizeof(control_page_default));
4082 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4083 &control_page_default,
4084 sizeof(control_page_default));
4085 page_index->page_data =
4086 (uint8_t *)lun->mode_pages.control_page;
4090 case SMS_VENDOR_SPECIFIC_PAGE:{
4091 switch (page_index->subpage) {
4092 case PWR_SUBPAGE_CODE: {
4093 struct copan_power_subpage *current_page,
4096 memcpy(&lun->mode_pages.power_subpage[
4098 &power_page_default,
4099 sizeof(power_page_default));
4100 memcpy(&lun->mode_pages.power_subpage[
4101 CTL_PAGE_CHANGEABLE],
4102 &power_page_changeable,
4103 sizeof(power_page_changeable));
4104 memcpy(&lun->mode_pages.power_subpage[
4106 &power_page_default,
4107 sizeof(power_page_default));
4108 memcpy(&lun->mode_pages.power_subpage[
4110 &power_page_default,
4111 sizeof(power_page_default));
4112 page_index->page_data =
4113 (uint8_t *)lun->mode_pages.power_subpage;
4115 current_page = (struct copan_power_subpage *)
4116 (page_index->page_data +
4117 (page_index->page_len *
4119 saved_page = (struct copan_power_subpage *)
4120 (page_index->page_data +
4121 (page_index->page_len *
4125 case APS_SUBPAGE_CODE: {
4126 struct copan_aps_subpage *current_page,
4129 // This gets set multiple times but
4130 // it should always be the same. It's
4131 // only done during init so who cares.
4132 index_to_aps_page = i;
4134 memcpy(&lun->mode_pages.aps_subpage[
4137 sizeof(aps_page_default));
4138 memcpy(&lun->mode_pages.aps_subpage[
4139 CTL_PAGE_CHANGEABLE],
4140 &aps_page_changeable,
4141 sizeof(aps_page_changeable));
4142 memcpy(&lun->mode_pages.aps_subpage[
4145 sizeof(aps_page_default));
4146 memcpy(&lun->mode_pages.aps_subpage[
4149 sizeof(aps_page_default));
4150 page_index->page_data =
4151 (uint8_t *)lun->mode_pages.aps_subpage;
4153 current_page = (struct copan_aps_subpage *)
4154 (page_index->page_data +
4155 (page_index->page_len *
4157 saved_page = (struct copan_aps_subpage *)
4158 (page_index->page_data +
4159 (page_index->page_len *
4163 case DBGCNF_SUBPAGE_CODE: {
4164 struct copan_debugconf_subpage *current_page,
4167 memcpy(&lun->mode_pages.debugconf_subpage[
4169 &debugconf_page_default,
4170 sizeof(debugconf_page_default));
4171 memcpy(&lun->mode_pages.debugconf_subpage[
4172 CTL_PAGE_CHANGEABLE],
4173 &debugconf_page_changeable,
4174 sizeof(debugconf_page_changeable));
4175 memcpy(&lun->mode_pages.debugconf_subpage[
4177 &debugconf_page_default,
4178 sizeof(debugconf_page_default));
4179 memcpy(&lun->mode_pages.debugconf_subpage[
4181 &debugconf_page_default,
4182 sizeof(debugconf_page_default));
4183 page_index->page_data =
4184 (uint8_t *)lun->mode_pages.debugconf_subpage;
4186 current_page = (struct copan_debugconf_subpage *)
4187 (page_index->page_data +
4188 (page_index->page_len *
4190 saved_page = (struct copan_debugconf_subpage *)
4191 (page_index->page_data +
4192 (page_index->page_len *
4197 panic("invalid subpage value %d",
4198 page_index->subpage);
4204 panic("invalid page value %d",
4205 page_index->page_code & SMPH_PC_MASK);
4210 return (CTL_RETVAL_COMPLETE);
4217 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4218 * wants us to allocate the LUN and he can block.
4219 * - ctl_softc is always set
4220 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4222 * Returns 0 for success, non-zero (errno) for failure.
4225 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4226 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4228 struct ctl_lun *nlun, *lun;
4229 struct ctl_frontend *fe;
4230 int lun_number, i, lun_malloced;
4236 * We currently only support Direct Access or Processor LUN types.
4238 switch (be_lun->lun_type) {
4246 be_lun->lun_config_status(be_lun->be_lun,
4247 CTL_LUN_CONFIG_FAILURE);
4250 if (ctl_lun == NULL) {
4251 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4258 memset(lun, 0, sizeof(*lun));
4260 lun->flags = CTL_LUN_MALLOCED;
4262 mtx_lock(&ctl_softc->ctl_lock);
4264 * See if the caller requested a particular LUN number. If so, see
4265 * if it is available. Otherwise, allocate the first available LUN.
4267 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4268 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4269 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4270 mtx_unlock(&ctl_softc->ctl_lock);
4271 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4272 printf("ctl: requested LUN ID %d is higher "
4273 "than CTL_MAX_LUNS - 1 (%d)\n",
4274 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4277 * XXX KDM return an error, or just assign
4278 * another LUN ID in this case??
4280 printf("ctl: requested LUN ID %d is already "
4281 "in use\n", be_lun->req_lun_id);
4283 if (lun->flags & CTL_LUN_MALLOCED)
4285 be_lun->lun_config_status(be_lun->be_lun,
4286 CTL_LUN_CONFIG_FAILURE);
4289 lun_number = be_lun->req_lun_id;
4291 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4292 if (lun_number == -1) {
4293 mtx_unlock(&ctl_softc->ctl_lock);
4294 printf("ctl: can't allocate LUN on target %ju, out of "
4295 "LUNs\n", (uintmax_t)target_id.id);
4296 if (lun->flags & CTL_LUN_MALLOCED)
4298 be_lun->lun_config_status(be_lun->be_lun,
4299 CTL_LUN_CONFIG_FAILURE);
4303 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4305 lun->target = target_id;
4306 lun->lun = lun_number;
4307 lun->be_lun = be_lun;
4309 * The processor LUN is always enabled. Disk LUNs come on line
4310 * disabled, and must be enabled by the backend.
4312 lun->flags |= CTL_LUN_DISABLED;
4313 lun->backend = be_lun->be;
4314 be_lun->ctl_lun = lun;
4315 be_lun->lun_id = lun_number;
4316 atomic_add_int(&be_lun->be->num_luns, 1);
4317 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4318 lun->flags |= CTL_LUN_STOPPED;
4320 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4321 lun->flags |= CTL_LUN_INOPERABLE;
4323 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4324 lun->flags |= CTL_LUN_PRIMARY_SC;
4326 lun->ctl_softc = ctl_softc;
4327 TAILQ_INIT(&lun->ooa_queue);
4328 TAILQ_INIT(&lun->blocked_queue);
4329 STAILQ_INIT(&lun->error_list);
4332 * Initialize the mode page index.
4334 ctl_init_page_index(lun);
4337 * Set the poweron UA for all initiators on this LUN only.
4339 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4340 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4343 * Now, before we insert this lun on the lun list, set the lun
4344 * inventory changed UA for all other luns.
4346 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4347 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4348 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4352 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4354 ctl_softc->ctl_luns[lun_number] = lun;
4356 ctl_softc->num_luns++;
4358 /* Setup statistics gathering */
4359 lun->stats.device_type = be_lun->lun_type;
4360 lun->stats.lun_number = lun_number;
4361 if (lun->stats.device_type == T_DIRECT)
4362 lun->stats.blocksize = be_lun->blocksize;
4364 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4365 for (i = 0;i < CTL_MAX_PORTS;i++)
4366 lun->stats.ports[i].targ_port = i;
4368 mtx_unlock(&ctl_softc->ctl_lock);
4370 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4373 * Run through each registered FETD and bring it online if it isn't
4374 * already. Enable the target ID if it hasn't been enabled, and
4375 * enable this particular LUN.
4377 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4381 * XXX KDM this only works for ONE TARGET ID. We'll need
4382 * to do things differently if we go to a multiple target
4385 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4387 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4389 printf("ctl_alloc_lun: FETD %s port %d "
4390 "returned error %d for targ_enable on "
4391 "target %ju\n", fe->port_name,
4392 fe->targ_port, retval,
4393 (uintmax_t)target_id.id);
4395 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4398 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4400 printf("ctl_alloc_lun: FETD %s port %d returned error "
4401 "%d for lun_enable on target %ju lun %d\n",
4402 fe->port_name, fe->targ_port, retval,
4403 (uintmax_t)target_id.id, lun_number);
4405 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4413 * - LUN has already been marked invalid and any pending I/O has been taken
4417 ctl_free_lun(struct ctl_lun *lun)
4419 struct ctl_softc *softc;
4421 struct ctl_frontend *fe;
4423 struct ctl_lun *nlun;
4424 union ctl_io *io, *next_io;
4427 softc = lun->ctl_softc;
4429 mtx_assert(&softc->ctl_lock, MA_OWNED);
4431 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4433 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4435 softc->ctl_luns[lun->lun] = NULL;
4437 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4438 printf("ctl_free_lun: aieee!! freeing a LUN with "
4439 "outstanding I/O!!\n");
4443 * If we have anything pending on the RtR queue, remove it.
4445 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4449 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4450 targ_lun = io->io_hdr.nexus.targ_lun;
4451 if (io->io_hdr.nexus.lun_map_fn != NULL)
4452 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
4453 if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4454 && (targ_lun == lun->lun))
4455 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4460 * Then remove everything from the blocked queue.
4462 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4464 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4465 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4466 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4470 * Now clear out the OOA queue, and free all the I/O.
4471 * XXX KDM should we notify the FETD here? We probably need to
4472 * quiesce the LUN before deleting it.
4474 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4476 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4477 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4484 * XXX KDM this scheme only works for a single target/multiple LUN
4485 * setup. It needs to be revamped for a multiple target scheme.
4487 * XXX KDM this results in fe->lun_disable() getting called twice,
4488 * once when ctl_disable_lun() is called, and a second time here.
4489 * We really need to re-think the LUN disable semantics. There
4490 * should probably be several steps/levels to LUN removal:
4495 * Right now we only have a disable method when communicating to
4496 * the front end ports, at least for individual LUNs.
4499 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4502 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4505 printf("ctl_free_lun: FETD %s port %d returned error "
4506 "%d for lun_disable on target %ju lun %jd\n",
4507 fe->port_name, fe->targ_port, retval,
4508 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4511 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4512 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4514 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4516 printf("ctl_free_lun: FETD %s port %d "
4517 "returned error %d for targ_disable on "
4518 "target %ju\n", fe->port_name,
4519 fe->targ_port, retval,
4520 (uintmax_t)lun->target.id);
4522 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4524 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4528 fe->port_offline(fe->onoff_arg);
4529 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4536 * Tell the backend to free resources, if this LUN has a backend.
4538 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4539 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4541 if (lun->flags & CTL_LUN_MALLOCED)
4544 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4545 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4546 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4554 ctl_create_lun(struct ctl_be_lun *be_lun)
4556 struct ctl_softc *ctl_softc;
4558 ctl_softc = control_softc;
4561 * ctl_alloc_lun() should handle all potential failure cases.
4563 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4567 ctl_add_lun(struct ctl_be_lun *be_lun)
4569 struct ctl_softc *ctl_softc;
4571 ctl_softc = control_softc;
4573 mtx_lock(&ctl_softc->ctl_lock);
4574 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4575 mtx_unlock(&ctl_softc->ctl_lock);
4577 ctl_wakeup_thread();
4583 ctl_enable_lun(struct ctl_be_lun *be_lun)
4585 struct ctl_softc *ctl_softc;
4586 struct ctl_frontend *fe, *nfe;
4587 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 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4597 * eh? Why did we get called if the LUN is already
4600 mtx_unlock(&ctl_softc->ctl_lock);
4603 lun->flags &= ~CTL_LUN_DISABLED;
4605 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4606 nfe = STAILQ_NEXT(fe, links);
4609 * Drop the lock while we call the FETD's enable routine.
4610 * This can lead to a callback into CTL (at least in the
4611 * case of the internal initiator frontend.
4613 mtx_unlock(&ctl_softc->ctl_lock);
4614 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4615 mtx_lock(&ctl_softc->ctl_lock);
4617 printf("%s: FETD %s port %d returned error "
4618 "%d for lun_enable on target %ju lun %jd\n",
4619 __func__, fe->port_name, fe->targ_port, retval,
4620 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4624 /* NOTE: TODO: why does lun enable affect port status? */
4625 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4630 mtx_unlock(&ctl_softc->ctl_lock);
4636 ctl_disable_lun(struct ctl_be_lun *be_lun)
4638 struct ctl_softc *ctl_softc;
4639 struct ctl_frontend *fe;
4640 struct ctl_lun *lun;
4643 ctl_softc = control_softc;
4645 lun = (struct ctl_lun *)be_lun->ctl_lun;
4647 mtx_lock(&ctl_softc->ctl_lock);
4649 if (lun->flags & CTL_LUN_DISABLED) {
4650 mtx_unlock(&ctl_softc->ctl_lock);
4653 lun->flags |= CTL_LUN_DISABLED;
4655 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4656 mtx_unlock(&ctl_softc->ctl_lock);
4658 * Drop the lock before we call the frontend's disable
4659 * routine, to avoid lock order reversals.
4661 * XXX KDM what happens if the frontend list changes while
4662 * we're traversing it? It's unlikely, but should be handled.
4664 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4666 mtx_lock(&ctl_softc->ctl_lock);
4668 printf("ctl_alloc_lun: FETD %s port %d returned error "
4669 "%d for lun_disable on target %ju lun %jd\n",
4670 fe->port_name, fe->targ_port, retval,
4671 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4675 mtx_unlock(&ctl_softc->ctl_lock);
4681 ctl_start_lun(struct ctl_be_lun *be_lun)
4683 struct ctl_softc *ctl_softc;
4684 struct ctl_lun *lun;
4686 ctl_softc = control_softc;
4688 lun = (struct ctl_lun *)be_lun->ctl_lun;
4690 mtx_lock(&ctl_softc->ctl_lock);
4691 lun->flags &= ~CTL_LUN_STOPPED;
4692 mtx_unlock(&ctl_softc->ctl_lock);
4698 ctl_stop_lun(struct ctl_be_lun *be_lun)
4700 struct ctl_softc *ctl_softc;
4701 struct ctl_lun *lun;
4703 ctl_softc = control_softc;
4705 lun = (struct ctl_lun *)be_lun->ctl_lun;
4707 mtx_lock(&ctl_softc->ctl_lock);
4708 lun->flags |= CTL_LUN_STOPPED;
4709 mtx_unlock(&ctl_softc->ctl_lock);
4715 ctl_lun_offline(struct ctl_be_lun *be_lun)
4717 struct ctl_softc *ctl_softc;
4718 struct ctl_lun *lun;
4720 ctl_softc = control_softc;
4722 lun = (struct ctl_lun *)be_lun->ctl_lun;
4724 mtx_lock(&ctl_softc->ctl_lock);
4725 lun->flags |= CTL_LUN_OFFLINE;
4726 mtx_unlock(&ctl_softc->ctl_lock);
4732 ctl_lun_online(struct ctl_be_lun *be_lun)
4734 struct ctl_softc *ctl_softc;
4735 struct ctl_lun *lun;
4737 ctl_softc = control_softc;
4739 lun = (struct ctl_lun *)be_lun->ctl_lun;
4741 mtx_lock(&ctl_softc->ctl_lock);
4742 lun->flags &= ~CTL_LUN_OFFLINE;
4743 mtx_unlock(&ctl_softc->ctl_lock);
4749 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4751 struct ctl_softc *ctl_softc;
4752 struct ctl_lun *lun;
4754 ctl_softc = control_softc;
4756 lun = (struct ctl_lun *)be_lun->ctl_lun;
4758 mtx_lock(&ctl_softc->ctl_lock);
4761 * The LUN needs to be disabled before it can be marked invalid.
4763 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4764 mtx_unlock(&ctl_softc->ctl_lock);
4768 * Mark the LUN invalid.
4770 lun->flags |= CTL_LUN_INVALID;
4773 * If there is nothing in the OOA queue, go ahead and free the LUN.
4774 * If we have something in the OOA queue, we'll free it when the
4775 * last I/O completes.
4777 if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4779 mtx_unlock(&ctl_softc->ctl_lock);
4785 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4787 struct ctl_softc *ctl_softc;
4788 struct ctl_lun *lun;
4790 ctl_softc = control_softc;
4791 lun = (struct ctl_lun *)be_lun->ctl_lun;
4793 mtx_lock(&ctl_softc->ctl_lock);
4794 lun->flags |= CTL_LUN_INOPERABLE;
4795 mtx_unlock(&ctl_softc->ctl_lock);
4801 ctl_lun_operable(struct ctl_be_lun *be_lun)
4803 struct ctl_softc *ctl_softc;
4804 struct ctl_lun *lun;
4806 ctl_softc = control_softc;
4807 lun = (struct ctl_lun *)be_lun->ctl_lun;
4809 mtx_lock(&ctl_softc->ctl_lock);
4810 lun->flags &= ~CTL_LUN_INOPERABLE;
4811 mtx_unlock(&ctl_softc->ctl_lock);
4817 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4820 struct ctl_softc *softc;
4821 struct ctl_lun *lun;
4822 struct copan_aps_subpage *current_sp;
4823 struct ctl_page_index *page_index;
4826 softc = control_softc;
4828 mtx_lock(&softc->ctl_lock);
4830 lun = (struct ctl_lun *)be_lun->ctl_lun;
4833 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4834 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4838 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4840 page_index = &lun->mode_pages.index[i];
4843 if (page_index == NULL) {
4844 mtx_unlock(&softc->ctl_lock);
4845 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4846 (uintmax_t)lun->lun);
4850 if ((softc->aps_locked_lun != 0)
4851 && (softc->aps_locked_lun != lun->lun)) {
4852 printf("%s: attempt to lock LUN %llu when %llu is already "
4854 mtx_unlock(&softc->ctl_lock);
4859 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4860 (page_index->page_len * CTL_PAGE_CURRENT));
4863 current_sp->lock_active = APS_LOCK_ACTIVE;
4864 softc->aps_locked_lun = lun->lun;
4866 current_sp->lock_active = 0;
4867 softc->aps_locked_lun = 0;
4872 * If we're in HA mode, try to send the lock message to the other
4875 if (ctl_is_single == 0) {
4877 union ctl_ha_msg lock_msg;
4879 lock_msg.hdr.nexus = *nexus;
4880 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4882 lock_msg.aps.lock_flag = 1;
4884 lock_msg.aps.lock_flag = 0;
4885 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4886 sizeof(lock_msg), 0);
4887 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4888 printf("%s: APS (lock=%d) error returned from "
4889 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4890 mtx_unlock(&softc->ctl_lock);
4895 mtx_unlock(&softc->ctl_lock);
4901 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4903 struct ctl_lun *lun;
4904 struct ctl_softc *softc;
4907 softc = control_softc;
4909 mtx_lock(&softc->ctl_lock);
4911 lun = (struct ctl_lun *)be_lun->ctl_lun;
4913 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4914 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4916 mtx_unlock(&softc->ctl_lock);
4920 * Backend "memory move is complete" callback for requests that never
4921 * make it down to say RAIDCore's configuration code.
4924 ctl_config_move_done(union ctl_io *io)
4928 retval = CTL_RETVAL_COMPLETE;
4931 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4933 * XXX KDM this shouldn't happen, but what if it does?
4935 if (io->io_hdr.io_type != CTL_IO_SCSI)
4936 panic("I/O type isn't CTL_IO_SCSI!");
4938 if ((io->io_hdr.port_status == 0)
4939 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4940 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4941 io->io_hdr.status = CTL_SUCCESS;
4942 else if ((io->io_hdr.port_status != 0)
4943 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4944 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4946 * For hardware error sense keys, the sense key
4947 * specific value is defined to be a retry count,
4948 * but we use it to pass back an internal FETD
4949 * error code. XXX KDM Hopefully the FETD is only
4950 * using 16 bits for an error code, since that's
4951 * all the space we have in the sks field.
4953 ctl_set_internal_failure(&io->scsiio,
4956 io->io_hdr.port_status);
4957 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4958 free(io->scsiio.kern_data_ptr, M_CTL);
4963 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4964 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4965 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4967 * XXX KDM just assuming a single pointer here, and not a
4968 * S/G list. If we start using S/G lists for config data,
4969 * we'll need to know how to clean them up here as well.
4971 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4972 free(io->scsiio.kern_data_ptr, M_CTL);
4973 /* Hopefully the user has already set the status... */
4977 * XXX KDM now we need to continue data movement. Some
4979 * - call ctl_scsiio() again? We don't do this for data
4980 * writes, because for those at least we know ahead of
4981 * time where the write will go and how long it is. For
4982 * config writes, though, that information is largely
4983 * contained within the write itself, thus we need to
4984 * parse out the data again.
4986 * - Call some other function once the data is in?
4990 * XXX KDM call ctl_scsiio() again for now, and check flag
4991 * bits to see whether we're allocated or not.
4993 retval = ctl_scsiio(&io->scsiio);
5000 * This gets called by a backend driver when it is done with a
5001 * data_submit method.
5004 ctl_data_submit_done(union ctl_io *io)
5007 * If the IO_CONT flag is set, we need to call the supplied
5008 * function to continue processing the I/O, instead of completing
5011 * If there is an error, though, we don't want to keep processing.
5012 * Instead, just send status back to the initiator.
5014 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5015 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5016 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5017 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5018 io->scsiio.io_cont(io);
5025 * This gets called by a backend driver when it is done with a
5026 * configuration write.
5029 ctl_config_write_done(union ctl_io *io)
5032 * If the IO_CONT flag is set, we need to call the supplied
5033 * function to continue processing the I/O, instead of completing
5036 * If there is an error, though, we don't want to keep processing.
5037 * Instead, just send status back to the initiator.
5039 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5040 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5041 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5042 io->scsiio.io_cont(io);
5046 * Since a configuration write can be done for commands that actually
5047 * have data allocated, like write buffer, and commands that have
5048 * no data, like start/stop unit, we need to check here.
5050 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5051 free(io->scsiio.kern_data_ptr, M_CTL);
5056 * SCSI release command.
5059 ctl_scsi_release(struct ctl_scsiio *ctsio)
5061 int length, longid, thirdparty_id, resv_id;
5062 struct ctl_softc *ctl_softc;
5063 struct ctl_lun *lun;
5068 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5071 ctl_softc = control_softc;
5073 switch (ctsio->cdb[0]) {
5075 struct scsi_release *cdb;
5077 cdb = (struct scsi_release *)ctsio->cdb;
5078 if ((cdb->byte2 & 0x1f) != 0) {
5079 ctl_set_invalid_field(ctsio,
5085 ctl_done((union ctl_io *)ctsio);
5086 return (CTL_RETVAL_COMPLETE);
5091 struct scsi_release_10 *cdb;
5093 cdb = (struct scsi_release_10 *)ctsio->cdb;
5095 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5096 ctl_set_invalid_field(ctsio,
5102 ctl_done((union ctl_io *)ctsio);
5103 return (CTL_RETVAL_COMPLETE);
5107 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5108 ctl_set_invalid_field(ctsio,
5114 ctl_done((union ctl_io *)ctsio);
5115 return (CTL_RETVAL_COMPLETE);
5118 if (cdb->byte2 & SR10_LONGID)
5121 thirdparty_id = cdb->thirdparty_id;
5123 resv_id = cdb->resv_id;
5124 length = scsi_2btoul(cdb->length);
5131 * XXX KDM right now, we only support LUN reservation. We don't
5132 * support 3rd party reservations, or extent reservations, which
5133 * might actually need the parameter list. If we've gotten this
5134 * far, we've got a LUN reservation. Anything else got kicked out
5135 * above. So, according to SPC, ignore the length.
5139 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5141 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5142 ctsio->kern_data_len = length;
5143 ctsio->kern_total_len = length;
5144 ctsio->kern_data_resid = 0;
5145 ctsio->kern_rel_offset = 0;
5146 ctsio->kern_sg_entries = 0;
5147 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5148 ctsio->be_move_done = ctl_config_move_done;
5149 ctl_datamove((union ctl_io *)ctsio);
5151 return (CTL_RETVAL_COMPLETE);
5155 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5157 mtx_lock(&ctl_softc->ctl_lock);
5160 * According to SPC, it is not an error for an intiator to attempt
5161 * to release a reservation on a LUN that isn't reserved, or that
5162 * is reserved by another initiator. The reservation can only be
5163 * released, though, by the initiator who made it or by one of
5164 * several reset type events.
5166 if (lun->flags & CTL_LUN_RESERVED) {
5167 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5168 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5169 && (ctsio->io_hdr.nexus.targ_target.id ==
5170 lun->rsv_nexus.targ_target.id)) {
5171 lun->flags &= ~CTL_LUN_RESERVED;
5175 ctsio->scsi_status = SCSI_STATUS_OK;
5176 ctsio->io_hdr.status = CTL_SUCCESS;
5178 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5179 free(ctsio->kern_data_ptr, M_CTL);
5180 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5183 mtx_unlock(&ctl_softc->ctl_lock);
5185 ctl_done((union ctl_io *)ctsio);
5186 return (CTL_RETVAL_COMPLETE);
5190 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5192 int extent, thirdparty, longid;
5193 int resv_id, length;
5194 uint64_t thirdparty_id;
5195 struct ctl_softc *ctl_softc;
5196 struct ctl_lun *lun;
5205 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5207 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5208 ctl_softc = control_softc;
5210 switch (ctsio->cdb[0]) {
5212 struct scsi_reserve *cdb;
5214 cdb = (struct scsi_reserve *)ctsio->cdb;
5215 if ((cdb->byte2 & 0x1f) != 0) {
5216 ctl_set_invalid_field(ctsio,
5222 ctl_done((union ctl_io *)ctsio);
5223 return (CTL_RETVAL_COMPLETE);
5225 resv_id = cdb->resv_id;
5226 length = scsi_2btoul(cdb->length);
5230 struct scsi_reserve_10 *cdb;
5232 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5234 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5235 ctl_set_invalid_field(ctsio,
5241 ctl_done((union ctl_io *)ctsio);
5242 return (CTL_RETVAL_COMPLETE);
5244 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5245 ctl_set_invalid_field(ctsio,
5251 ctl_done((union ctl_io *)ctsio);
5252 return (CTL_RETVAL_COMPLETE);
5254 if (cdb->byte2 & SR10_LONGID)
5257 thirdparty_id = cdb->thirdparty_id;
5259 resv_id = cdb->resv_id;
5260 length = scsi_2btoul(cdb->length);
5266 * XXX KDM right now, we only support LUN reservation. We don't
5267 * support 3rd party reservations, or extent reservations, which
5268 * might actually need the parameter list. If we've gotten this
5269 * far, we've got a LUN reservation. Anything else got kicked out
5270 * above. So, according to SPC, ignore the length.
5274 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5276 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5277 ctsio->kern_data_len = length;
5278 ctsio->kern_total_len = length;
5279 ctsio->kern_data_resid = 0;
5280 ctsio->kern_rel_offset = 0;
5281 ctsio->kern_sg_entries = 0;
5282 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5283 ctsio->be_move_done = ctl_config_move_done;
5284 ctl_datamove((union ctl_io *)ctsio);
5286 return (CTL_RETVAL_COMPLETE);
5290 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5292 mtx_lock(&ctl_softc->ctl_lock);
5293 if (lun->flags & CTL_LUN_RESERVED) {
5294 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5295 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5296 || (ctsio->io_hdr.nexus.targ_target.id !=
5297 lun->rsv_nexus.targ_target.id)) {
5298 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5299 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5304 lun->flags |= CTL_LUN_RESERVED;
5305 lun->rsv_nexus = ctsio->io_hdr.nexus;
5307 ctsio->scsi_status = SCSI_STATUS_OK;
5308 ctsio->io_hdr.status = CTL_SUCCESS;
5311 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5312 free(ctsio->kern_data_ptr, M_CTL);
5313 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5316 mtx_unlock(&ctl_softc->ctl_lock);
5318 ctl_done((union ctl_io *)ctsio);
5319 return (CTL_RETVAL_COMPLETE);
5323 ctl_start_stop(struct ctl_scsiio *ctsio)
5325 struct scsi_start_stop_unit *cdb;
5326 struct ctl_lun *lun;
5327 struct ctl_softc *ctl_softc;
5330 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5332 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5333 ctl_softc = control_softc;
5336 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5340 * We don't support the immediate bit on a stop unit. In order to
5341 * do that, we would need to code up a way to know that a stop is
5342 * pending, and hold off any new commands until it completes, one
5343 * way or another. Then we could accept or reject those commands
5344 * depending on its status. We would almost need to do the reverse
5345 * of what we do below for an immediate start -- return the copy of
5346 * the ctl_io to the FETD with status to send to the host (and to
5347 * free the copy!) and then free the original I/O once the stop
5348 * actually completes. That way, the OOA queue mechanism can work
5349 * to block commands that shouldn't proceed. Another alternative
5350 * would be to put the copy in the queue in place of the original,
5351 * and return the original back to the caller. That could be
5354 if ((cdb->byte2 & SSS_IMMED)
5355 && ((cdb->how & SSS_START) == 0)) {
5356 ctl_set_invalid_field(ctsio,
5362 ctl_done((union ctl_io *)ctsio);
5363 return (CTL_RETVAL_COMPLETE);
5367 * We don't support the power conditions field. We need to check
5368 * this prior to checking the load/eject and start/stop bits.
5370 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5371 ctl_set_invalid_field(ctsio,
5377 ctl_done((union ctl_io *)ctsio);
5378 return (CTL_RETVAL_COMPLETE);
5382 * Media isn't removable, so we can't load or eject it.
5384 if ((cdb->how & SSS_LOEJ) != 0) {
5385 ctl_set_invalid_field(ctsio,
5391 ctl_done((union ctl_io *)ctsio);
5392 return (CTL_RETVAL_COMPLETE);
5395 if ((lun->flags & CTL_LUN_PR_RESERVED)
5396 && ((cdb->how & SSS_START)==0)) {
5399 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5400 if (!lun->per_res[residx].registered
5401 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5403 ctl_set_reservation_conflict(ctsio);
5404 ctl_done((union ctl_io *)ctsio);
5405 return (CTL_RETVAL_COMPLETE);
5410 * If there is no backend on this device, we can't start or stop
5411 * it. In theory we shouldn't get any start/stop commands in the
5412 * first place at this level if the LUN doesn't have a backend.
5413 * That should get stopped by the command decode code.
5415 if (lun->backend == NULL) {
5416 ctl_set_invalid_opcode(ctsio);
5417 ctl_done((union ctl_io *)ctsio);
5418 return (CTL_RETVAL_COMPLETE);
5422 * XXX KDM Copan-specific offline behavior.
5423 * Figure out a reasonable way to port this?
5426 mtx_lock(&ctl_softc->ctl_lock);
5428 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5429 && (lun->flags & CTL_LUN_OFFLINE)) {
5431 * If the LUN is offline, and the on/offline bit isn't set,
5432 * reject the start or stop. Otherwise, let it through.
5434 mtx_unlock(&ctl_softc->ctl_lock);
5435 ctl_set_lun_not_ready(ctsio);
5436 ctl_done((union ctl_io *)ctsio);
5438 mtx_unlock(&ctl_softc->ctl_lock);
5439 #endif /* NEEDTOPORT */
5441 * This could be a start or a stop when we're online,
5442 * or a stop/offline or start/online. A start or stop when
5443 * we're offline is covered in the case above.
5446 * In the non-immediate case, we send the request to
5447 * the backend and return status to the user when
5450 * In the immediate case, we allocate a new ctl_io
5451 * to hold a copy of the request, and send that to
5452 * the backend. We then set good status on the
5453 * user's request and return it immediately.
5455 if (cdb->byte2 & SSS_IMMED) {
5456 union ctl_io *new_io;
5458 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5459 if (new_io == NULL) {
5460 ctl_set_busy(ctsio);
5461 ctl_done((union ctl_io *)ctsio);
5463 ctl_copy_io((union ctl_io *)ctsio,
5465 retval = lun->backend->config_write(new_io);
5466 ctl_set_success(ctsio);
5467 ctl_done((union ctl_io *)ctsio);
5470 retval = lun->backend->config_write(
5471 (union ctl_io *)ctsio);
5480 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5481 * we don't really do anything with the LBA and length fields if the user
5482 * passes them in. Instead we'll just flush out the cache for the entire
5486 ctl_sync_cache(struct ctl_scsiio *ctsio)
5488 struct ctl_lun *lun;
5489 struct ctl_softc *ctl_softc;
5490 uint64_t starting_lba;
5491 uint32_t block_count;
5495 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5497 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5498 ctl_softc = control_softc;
5503 switch (ctsio->cdb[0]) {
5504 case SYNCHRONIZE_CACHE: {
5505 struct scsi_sync_cache *cdb;
5506 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5508 if (cdb->byte2 & SSC_RELADR)
5511 if (cdb->byte2 & SSC_IMMED)
5514 starting_lba = scsi_4btoul(cdb->begin_lba);
5515 block_count = scsi_2btoul(cdb->lb_count);
5518 case SYNCHRONIZE_CACHE_16: {
5519 struct scsi_sync_cache_16 *cdb;
5520 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5522 if (cdb->byte2 & SSC_RELADR)
5525 if (cdb->byte2 & SSC_IMMED)
5528 starting_lba = scsi_8btou64(cdb->begin_lba);
5529 block_count = scsi_4btoul(cdb->lb_count);
5533 ctl_set_invalid_opcode(ctsio);
5534 ctl_done((union ctl_io *)ctsio);
5536 break; /* NOTREACHED */
5541 * We don't support the immediate bit. Since it's in the
5542 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5543 * commands, we can just return the same error in either
5546 ctl_set_invalid_field(ctsio,
5552 ctl_done((union ctl_io *)ctsio);
5558 * We don't support the reladr bit either. It can only be
5559 * used with linked commands, and we don't support linked
5560 * commands. Since the bit is in the same place for the
5561 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5562 * just return the same error in either case.
5564 ctl_set_invalid_field(ctsio,
5570 ctl_done((union ctl_io *)ctsio);
5575 * We check the LBA and length, but don't do anything with them.
5576 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5577 * get flushed. This check will just help satisfy anyone who wants
5578 * to see an error for an out of range LBA.
5580 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5581 ctl_set_lba_out_of_range(ctsio);
5582 ctl_done((union ctl_io *)ctsio);
5587 * If this LUN has no backend, we can't flush the cache anyway.
5589 if (lun->backend == NULL) {
5590 ctl_set_invalid_opcode(ctsio);
5591 ctl_done((union ctl_io *)ctsio);
5596 * Check to see whether we're configured to send the SYNCHRONIZE
5597 * CACHE command directly to the back end.
5599 mtx_lock(&ctl_softc->ctl_lock);
5600 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5601 && (++(lun->sync_count) >= lun->sync_interval)) {
5602 lun->sync_count = 0;
5603 mtx_unlock(&ctl_softc->ctl_lock);
5604 retval = lun->backend->config_write((union ctl_io *)ctsio);
5606 mtx_unlock(&ctl_softc->ctl_lock);
5607 ctl_set_success(ctsio);
5608 ctl_done((union ctl_io *)ctsio);
5617 ctl_format(struct ctl_scsiio *ctsio)
5619 struct scsi_format *cdb;
5620 struct ctl_lun *lun;
5621 struct ctl_softc *ctl_softc;
5622 int length, defect_list_len;
5624 CTL_DEBUG_PRINT(("ctl_format\n"));
5626 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5627 ctl_softc = control_softc;
5629 cdb = (struct scsi_format *)ctsio->cdb;
5632 if (cdb->byte2 & SF_FMTDATA) {
5633 if (cdb->byte2 & SF_LONGLIST)
5634 length = sizeof(struct scsi_format_header_long);
5636 length = sizeof(struct scsi_format_header_short);
5639 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5641 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5642 ctsio->kern_data_len = length;
5643 ctsio->kern_total_len = length;
5644 ctsio->kern_data_resid = 0;
5645 ctsio->kern_rel_offset = 0;
5646 ctsio->kern_sg_entries = 0;
5647 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5648 ctsio->be_move_done = ctl_config_move_done;
5649 ctl_datamove((union ctl_io *)ctsio);
5651 return (CTL_RETVAL_COMPLETE);
5654 defect_list_len = 0;
5656 if (cdb->byte2 & SF_FMTDATA) {
5657 if (cdb->byte2 & SF_LONGLIST) {
5658 struct scsi_format_header_long *header;
5660 header = (struct scsi_format_header_long *)
5661 ctsio->kern_data_ptr;
5663 defect_list_len = scsi_4btoul(header->defect_list_len);
5664 if (defect_list_len != 0) {
5665 ctl_set_invalid_field(ctsio,
5674 struct scsi_format_header_short *header;
5676 header = (struct scsi_format_header_short *)
5677 ctsio->kern_data_ptr;
5679 defect_list_len = scsi_2btoul(header->defect_list_len);
5680 if (defect_list_len != 0) {
5681 ctl_set_invalid_field(ctsio,
5693 * The format command will clear out the "Medium format corrupted"
5694 * status if set by the configuration code. That status is really
5695 * just a way to notify the host that we have lost the media, and
5696 * get them to issue a command that will basically make them think
5697 * they're blowing away the media.
5699 mtx_lock(&ctl_softc->ctl_lock);
5700 lun->flags &= ~CTL_LUN_INOPERABLE;
5701 mtx_unlock(&ctl_softc->ctl_lock);
5703 ctsio->scsi_status = SCSI_STATUS_OK;
5704 ctsio->io_hdr.status = CTL_SUCCESS;
5707 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5708 free(ctsio->kern_data_ptr, M_CTL);
5709 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5712 ctl_done((union ctl_io *)ctsio);
5713 return (CTL_RETVAL_COMPLETE);
5717 ctl_read_buffer(struct ctl_scsiio *ctsio)
5719 struct scsi_read_buffer *cdb;
5720 struct ctl_lun *lun;
5721 int buffer_offset, len;
5722 static uint8_t descr[4];
5723 static uint8_t echo_descr[4] = { 0 };
5725 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5727 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5728 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5730 if (lun->flags & CTL_LUN_PR_RESERVED) {
5734 * XXX KDM need a lock here.
5736 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5737 if ((lun->res_type == SPR_TYPE_EX_AC
5738 && residx != lun->pr_res_idx)
5739 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5740 || lun->res_type == SPR_TYPE_EX_AC_AR)
5741 && !lun->per_res[residx].registered)) {
5742 ctl_set_reservation_conflict(ctsio);
5743 ctl_done((union ctl_io *)ctsio);
5744 return (CTL_RETVAL_COMPLETE);
5748 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5749 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5750 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5751 ctl_set_invalid_field(ctsio,
5757 ctl_done((union ctl_io *)ctsio);
5758 return (CTL_RETVAL_COMPLETE);
5760 if (cdb->buffer_id != 0) {
5761 ctl_set_invalid_field(ctsio,
5767 ctl_done((union ctl_io *)ctsio);
5768 return (CTL_RETVAL_COMPLETE);
5771 len = scsi_3btoul(cdb->length);
5772 buffer_offset = scsi_3btoul(cdb->offset);
5774 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5775 ctl_set_invalid_field(ctsio,
5781 ctl_done((union ctl_io *)ctsio);
5782 return (CTL_RETVAL_COMPLETE);
5785 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5787 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5788 ctsio->kern_data_ptr = descr;
5789 len = min(len, sizeof(descr));
5790 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5791 ctsio->kern_data_ptr = echo_descr;
5792 len = min(len, sizeof(echo_descr));
5794 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5795 ctsio->kern_data_len = len;
5796 ctsio->kern_total_len = len;
5797 ctsio->kern_data_resid = 0;
5798 ctsio->kern_rel_offset = 0;
5799 ctsio->kern_sg_entries = 0;
5800 ctsio->be_move_done = ctl_config_move_done;
5801 ctl_datamove((union ctl_io *)ctsio);
5803 return (CTL_RETVAL_COMPLETE);
5807 ctl_write_buffer(struct ctl_scsiio *ctsio)
5809 struct scsi_write_buffer *cdb;
5810 struct ctl_lun *lun;
5811 int buffer_offset, len;
5813 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5815 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5816 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5818 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5819 ctl_set_invalid_field(ctsio,
5825 ctl_done((union ctl_io *)ctsio);
5826 return (CTL_RETVAL_COMPLETE);
5828 if (cdb->buffer_id != 0) {
5829 ctl_set_invalid_field(ctsio,
5835 ctl_done((union ctl_io *)ctsio);
5836 return (CTL_RETVAL_COMPLETE);
5839 len = scsi_3btoul(cdb->length);
5840 buffer_offset = scsi_3btoul(cdb->offset);
5842 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5843 ctl_set_invalid_field(ctsio,
5849 ctl_done((union ctl_io *)ctsio);
5850 return (CTL_RETVAL_COMPLETE);
5854 * If we've got a kernel request that hasn't been malloced yet,
5855 * malloc it and tell the caller the data buffer is here.
5857 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5858 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5859 ctsio->kern_data_len = len;
5860 ctsio->kern_total_len = len;
5861 ctsio->kern_data_resid = 0;
5862 ctsio->kern_rel_offset = 0;
5863 ctsio->kern_sg_entries = 0;
5864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5865 ctsio->be_move_done = ctl_config_move_done;
5866 ctl_datamove((union ctl_io *)ctsio);
5868 return (CTL_RETVAL_COMPLETE);
5871 ctl_done((union ctl_io *)ctsio);
5873 return (CTL_RETVAL_COMPLETE);
5877 ctl_write_same(struct ctl_scsiio *ctsio)
5879 struct ctl_lun *lun;
5880 struct ctl_lba_len_flags *lbalen;
5882 uint32_t num_blocks;
5886 retval = CTL_RETVAL_COMPLETE;
5888 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5890 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5892 switch (ctsio->cdb[0]) {
5893 case WRITE_SAME_10: {
5894 struct scsi_write_same_10 *cdb;
5896 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5898 lba = scsi_4btoul(cdb->addr);
5899 num_blocks = scsi_2btoul(cdb->length);
5903 case WRITE_SAME_16: {
5904 struct scsi_write_same_16 *cdb;
5906 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5908 lba = scsi_8btou64(cdb->addr);
5909 num_blocks = scsi_4btoul(cdb->length);
5915 * We got a command we don't support. This shouldn't
5916 * happen, commands should be filtered out above us.
5918 ctl_set_invalid_opcode(ctsio);
5919 ctl_done((union ctl_io *)ctsio);
5921 return (CTL_RETVAL_COMPLETE);
5922 break; /* NOTREACHED */
5926 * The first check is to make sure we're in bounds, the second
5927 * check is to catch wrap-around problems. If the lba + num blocks
5928 * is less than the lba, then we've wrapped around and the block
5929 * range is invalid anyway.
5931 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5932 || ((lba + num_blocks) < lba)) {
5933 ctl_set_lba_out_of_range(ctsio);
5934 ctl_done((union ctl_io *)ctsio);
5935 return (CTL_RETVAL_COMPLETE);
5938 /* Zero number of blocks means "to the last logical block" */
5939 if (num_blocks == 0) {
5940 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5941 ctl_set_invalid_field(ctsio,
5947 ctl_done((union ctl_io *)ctsio);
5948 return (CTL_RETVAL_COMPLETE);
5950 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5953 len = lun->be_lun->blocksize;
5956 * If we've got a kernel request that hasn't been malloced yet,
5957 * malloc it and tell the caller the data buffer is here.
5959 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5960 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5961 ctsio->kern_data_len = len;
5962 ctsio->kern_total_len = len;
5963 ctsio->kern_data_resid = 0;
5964 ctsio->kern_rel_offset = 0;
5965 ctsio->kern_sg_entries = 0;
5966 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5967 ctsio->be_move_done = ctl_config_move_done;
5968 ctl_datamove((union ctl_io *)ctsio);
5970 return (CTL_RETVAL_COMPLETE);
5973 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5975 lbalen->len = num_blocks;
5976 lbalen->flags = byte2;
5977 retval = lun->backend->config_write((union ctl_io *)ctsio);
5983 ctl_unmap(struct ctl_scsiio *ctsio)
5985 struct ctl_lun *lun;
5986 struct scsi_unmap *cdb;
5987 struct ctl_ptr_len_flags *ptrlen;
5988 struct scsi_unmap_header *hdr;
5989 struct scsi_unmap_desc *buf, *end;
5991 uint32_t num_blocks;
5995 retval = CTL_RETVAL_COMPLETE;
5997 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6000 cdb = (struct scsi_unmap *)ctsio->cdb;
6002 len = scsi_2btoul(cdb->length);
6006 * If we've got a kernel request that hasn't been malloced yet,
6007 * malloc it and tell the caller the data buffer is here.
6009 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6010 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6011 ctsio->kern_data_len = len;
6012 ctsio->kern_total_len = len;
6013 ctsio->kern_data_resid = 0;
6014 ctsio->kern_rel_offset = 0;
6015 ctsio->kern_sg_entries = 0;
6016 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6017 ctsio->be_move_done = ctl_config_move_done;
6018 ctl_datamove((union ctl_io *)ctsio);
6020 return (CTL_RETVAL_COMPLETE);
6023 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6024 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6025 if (len < sizeof (*hdr) ||
6026 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6027 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6028 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6029 ctl_set_invalid_field(ctsio,
6035 ctl_done((union ctl_io *)ctsio);
6036 return (CTL_RETVAL_COMPLETE);
6038 len = scsi_2btoul(hdr->desc_length);
6039 buf = (struct scsi_unmap_desc *)(hdr + 1);
6040 end = buf + len / sizeof(*buf);
6042 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6043 ptrlen->ptr = (void *)buf;
6045 ptrlen->flags = byte2;
6047 for (; buf < end; buf++) {
6048 lba = scsi_8btou64(buf->lba);
6049 num_blocks = scsi_4btoul(buf->length);
6050 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6051 || ((lba + num_blocks) < lba)) {
6052 ctl_set_lba_out_of_range(ctsio);
6053 ctl_done((union ctl_io *)ctsio);
6054 return (CTL_RETVAL_COMPLETE);
6058 retval = lun->backend->config_write((union ctl_io *)ctsio);
6064 * Note that this function currently doesn't actually do anything inside
6065 * CTL to enforce things if the DQue bit is turned on.
6067 * Also note that this function can't be used in the default case, because
6068 * the DQue bit isn't set in the changeable mask for the control mode page
6069 * anyway. This is just here as an example for how to implement a page
6070 * handler, and a placeholder in case we want to allow the user to turn
6071 * tagged queueing on and off.
6073 * The D_SENSE bit handling is functional, however, and will turn
6074 * descriptor sense on and off for a given LUN.
6077 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6078 struct ctl_page_index *page_index, uint8_t *page_ptr)
6080 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6081 struct ctl_lun *lun;
6082 struct ctl_softc *softc;
6086 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6087 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6090 user_cp = (struct scsi_control_page *)page_ptr;
6091 current_cp = (struct scsi_control_page *)
6092 (page_index->page_data + (page_index->page_len *
6094 saved_cp = (struct scsi_control_page *)
6095 (page_index->page_data + (page_index->page_len *
6098 softc = control_softc;
6100 mtx_lock(&softc->ctl_lock);
6101 if (((current_cp->rlec & SCP_DSENSE) == 0)
6102 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6104 * Descriptor sense is currently turned off and the user
6105 * wants to turn it on.
6107 current_cp->rlec |= SCP_DSENSE;
6108 saved_cp->rlec |= SCP_DSENSE;
6109 lun->flags |= CTL_LUN_SENSE_DESC;
6111 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6112 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6114 * Descriptor sense is currently turned on, and the user
6115 * wants to turn it off.
6117 current_cp->rlec &= ~SCP_DSENSE;
6118 saved_cp->rlec &= ~SCP_DSENSE;
6119 lun->flags &= ~CTL_LUN_SENSE_DESC;
6122 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6123 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6125 csevent_log(CSC_CTL | CSC_SHELF_SW |
6127 csevent_LogType_Trace,
6128 csevent_Severity_Information,
6129 csevent_AlertLevel_Green,
6130 csevent_FRU_Firmware,
6131 csevent_FRU_Unknown,
6132 "Received untagged to untagged transition");
6133 #endif /* NEEDTOPORT */
6136 csevent_log(CSC_CTL | CSC_SHELF_SW |
6138 csevent_LogType_ConfigChange,
6139 csevent_Severity_Information,
6140 csevent_AlertLevel_Green,
6141 csevent_FRU_Firmware,
6142 csevent_FRU_Unknown,
6143 "Received untagged to tagged "
6144 "queueing transition");
6145 #endif /* NEEDTOPORT */
6147 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6148 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6152 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6154 csevent_log(CSC_CTL | CSC_SHELF_SW |
6156 csevent_LogType_ConfigChange,
6157 csevent_Severity_Warning,
6158 csevent_AlertLevel_Yellow,
6159 csevent_FRU_Firmware,
6160 csevent_FRU_Unknown,
6161 "Received tagged queueing to untagged "
6163 #endif /* NEEDTOPORT */
6165 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6166 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6170 csevent_log(CSC_CTL | CSC_SHELF_SW |
6172 csevent_LogType_Trace,
6173 csevent_Severity_Information,
6174 csevent_AlertLevel_Green,
6175 csevent_FRU_Firmware,
6176 csevent_FRU_Unknown,
6177 "Received tagged queueing to tagged "
6178 "queueing transition");
6179 #endif /* NEEDTOPORT */
6185 * Let other initiators know that the mode
6186 * parameters for this LUN have changed.
6188 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6192 lun->pending_sense[i].ua_pending |=
6196 mtx_unlock(&softc->ctl_lock);
6202 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6203 struct ctl_page_index *page_index, uint8_t *page_ptr)
6209 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6210 struct ctl_page_index *page_index, int pc)
6212 struct copan_power_subpage *page;
6214 page = (struct copan_power_subpage *)page_index->page_data +
6215 (page_index->page_len * pc);
6218 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6220 * We don't update the changable bits for this page.
6223 case SMS_PAGE_CTRL_CURRENT >> 6:
6224 case SMS_PAGE_CTRL_DEFAULT >> 6:
6225 case SMS_PAGE_CTRL_SAVED >> 6:
6227 ctl_update_power_subpage(page);
6232 EPRINT(0, "Invalid PC %d!!", pc);
6241 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6242 struct ctl_page_index *page_index, uint8_t *page_ptr)
6244 struct copan_aps_subpage *user_sp;
6245 struct copan_aps_subpage *current_sp;
6246 union ctl_modepage_info *modepage_info;
6247 struct ctl_softc *softc;
6248 struct ctl_lun *lun;
6251 retval = CTL_RETVAL_COMPLETE;
6252 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6253 (page_index->page_len * CTL_PAGE_CURRENT));
6254 softc = control_softc;
6255 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6257 user_sp = (struct copan_aps_subpage *)page_ptr;
6259 modepage_info = (union ctl_modepage_info *)
6260 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6262 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6263 modepage_info->header.subpage = page_index->subpage;
6264 modepage_info->aps.lock_active = user_sp->lock_active;
6266 mtx_lock(&softc->ctl_lock);
6269 * If there is a request to lock the LUN and another LUN is locked
6270 * this is an error. If the requested LUN is already locked ignore
6271 * the request. If no LUN is locked attempt to lock it.
6272 * if there is a request to unlock the LUN and the LUN is currently
6273 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6274 * if another LUN is locked or no LUN is locked.
6276 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6277 if (softc->aps_locked_lun == lun->lun) {
6279 * This LUN is already locked, so we're done.
6281 retval = CTL_RETVAL_COMPLETE;
6282 } else if (softc->aps_locked_lun == 0) {
6284 * No one has the lock, pass the request to the
6287 retval = lun->backend->config_write(
6288 (union ctl_io *)ctsio);
6291 * Someone else has the lock, throw out the request.
6293 ctl_set_already_locked(ctsio);
6294 free(ctsio->kern_data_ptr, M_CTL);
6295 ctl_done((union ctl_io *)ctsio);
6298 * Set the return value so that ctl_do_mode_select()
6299 * won't try to complete the command. We already
6300 * completed it here.
6302 retval = CTL_RETVAL_ERROR;
6304 } else if (softc->aps_locked_lun == lun->lun) {
6306 * This LUN is locked, so pass the unlock request to the
6309 retval = lun->backend->config_write((union ctl_io *)ctsio);
6311 mtx_unlock(&softc->ctl_lock);
6317 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6318 struct ctl_page_index *page_index,
6324 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6329 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6330 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6331 printf("page data:");
6333 printf(" %.2x",page_ptr[i]);
6339 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6340 struct ctl_page_index *page_index,
6343 struct copan_debugconf_subpage *page;
6345 page = (struct copan_debugconf_subpage *)page_index->page_data +
6346 (page_index->page_len * pc);
6349 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6350 case SMS_PAGE_CTRL_DEFAULT >> 6:
6351 case SMS_PAGE_CTRL_SAVED >> 6:
6353 * We don't update the changable or default bits for this page.
6356 case SMS_PAGE_CTRL_CURRENT >> 6:
6357 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6358 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6362 EPRINT(0, "Invalid PC %d!!", pc);
6363 #endif /* NEEDTOPORT */
6371 ctl_do_mode_select(union ctl_io *io)
6373 struct scsi_mode_page_header *page_header;
6374 struct ctl_page_index *page_index;
6375 struct ctl_scsiio *ctsio;
6376 int control_dev, page_len;
6377 int page_len_offset, page_len_size;
6378 union ctl_modepage_info *modepage_info;
6379 struct ctl_lun *lun;
6380 int *len_left, *len_used;
6383 ctsio = &io->scsiio;
6386 retval = CTL_RETVAL_COMPLETE;
6388 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6390 if (lun->be_lun->lun_type != T_DIRECT)
6395 modepage_info = (union ctl_modepage_info *)
6396 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6397 len_left = &modepage_info->header.len_left;
6398 len_used = &modepage_info->header.len_used;
6402 page_header = (struct scsi_mode_page_header *)
6403 (ctsio->kern_data_ptr + *len_used);
6405 if (*len_left == 0) {
6406 free(ctsio->kern_data_ptr, M_CTL);
6407 ctl_set_success(ctsio);
6408 ctl_done((union ctl_io *)ctsio);
6409 return (CTL_RETVAL_COMPLETE);
6410 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6412 free(ctsio->kern_data_ptr, M_CTL);
6413 ctl_set_param_len_error(ctsio);
6414 ctl_done((union ctl_io *)ctsio);
6415 return (CTL_RETVAL_COMPLETE);
6417 } else if ((page_header->page_code & SMPH_SPF)
6418 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6420 free(ctsio->kern_data_ptr, M_CTL);
6421 ctl_set_param_len_error(ctsio);
6422 ctl_done((union ctl_io *)ctsio);
6423 return (CTL_RETVAL_COMPLETE);
6428 * XXX KDM should we do something with the block descriptor?
6430 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6432 if ((control_dev != 0)
6433 && (lun->mode_pages.index[i].page_flags &
6434 CTL_PAGE_FLAG_DISK_ONLY))
6437 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6438 (page_header->page_code & SMPH_PC_MASK))
6442 * If neither page has a subpage code, then we've got a
6445 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6446 && ((page_header->page_code & SMPH_SPF) == 0)) {
6447 page_index = &lun->mode_pages.index[i];
6448 page_len = page_header->page_length;
6453 * If both pages have subpages, then the subpage numbers
6456 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6457 && (page_header->page_code & SMPH_SPF)) {
6458 struct scsi_mode_page_header_sp *sph;
6460 sph = (struct scsi_mode_page_header_sp *)page_header;
6462 if (lun->mode_pages.index[i].subpage ==
6464 page_index = &lun->mode_pages.index[i];
6465 page_len = scsi_2btoul(sph->page_length);
6472 * If we couldn't find the page, or if we don't have a mode select
6473 * handler for it, send back an error to the user.
6475 if ((page_index == NULL)
6476 || (page_index->select_handler == NULL)) {
6477 ctl_set_invalid_field(ctsio,
6480 /*field*/ *len_used,
6483 free(ctsio->kern_data_ptr, M_CTL);
6484 ctl_done((union ctl_io *)ctsio);
6485 return (CTL_RETVAL_COMPLETE);
6488 if (page_index->page_code & SMPH_SPF) {
6489 page_len_offset = 2;
6493 page_len_offset = 1;
6497 * If the length the initiator gives us isn't the one we specify in
6498 * the mode page header, or if they didn't specify enough data in
6499 * the CDB to avoid truncating this page, kick out the request.
6501 if ((page_len != (page_index->page_len - page_len_offset -
6503 || (*len_left < page_index->page_len)) {
6506 ctl_set_invalid_field(ctsio,
6509 /*field*/ *len_used + page_len_offset,
6512 free(ctsio->kern_data_ptr, M_CTL);
6513 ctl_done((union ctl_io *)ctsio);
6514 return (CTL_RETVAL_COMPLETE);
6518 * Run through the mode page, checking to make sure that the bits
6519 * the user changed are actually legal for him to change.
6521 for (i = 0; i < page_index->page_len; i++) {
6522 uint8_t *user_byte, *change_mask, *current_byte;
6526 user_byte = (uint8_t *)page_header + i;
6527 change_mask = page_index->page_data +
6528 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6529 current_byte = page_index->page_data +
6530 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6533 * Check to see whether the user set any bits in this byte
6534 * that he is not allowed to set.
6536 if ((*user_byte & ~(*change_mask)) ==
6537 (*current_byte & ~(*change_mask)))
6541 * Go through bit by bit to determine which one is illegal.
6544 for (j = 7; j >= 0; j--) {
6545 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6546 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6551 ctl_set_invalid_field(ctsio,
6554 /*field*/ *len_used + i,
6557 free(ctsio->kern_data_ptr, M_CTL);
6558 ctl_done((union ctl_io *)ctsio);
6559 return (CTL_RETVAL_COMPLETE);
6563 * Decrement these before we call the page handler, since we may
6564 * end up getting called back one way or another before the handler
6565 * returns to this context.
6567 *len_left -= page_index->page_len;
6568 *len_used += page_index->page_len;
6570 retval = page_index->select_handler(ctsio, page_index,
6571 (uint8_t *)page_header);
6574 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6575 * wait until this queued command completes to finish processing
6576 * the mode page. If it returns anything other than
6577 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6578 * already set the sense information, freed the data pointer, and
6579 * completed the io for us.
6581 if (retval != CTL_RETVAL_COMPLETE)
6582 goto bailout_no_done;
6585 * If the initiator sent us more than one page, parse the next one.
6590 ctl_set_success(ctsio);
6591 free(ctsio->kern_data_ptr, M_CTL);
6592 ctl_done((union ctl_io *)ctsio);
6596 return (CTL_RETVAL_COMPLETE);
6601 ctl_mode_select(struct ctl_scsiio *ctsio)
6603 int param_len, pf, sp;
6604 int header_size, bd_len;
6605 int len_left, len_used;
6606 struct ctl_page_index *page_index;
6607 struct ctl_lun *lun;
6608 int control_dev, page_len;
6609 union ctl_modepage_info *modepage_info;
6621 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6623 if (lun->be_lun->lun_type != T_DIRECT)
6628 switch (ctsio->cdb[0]) {
6629 case MODE_SELECT_6: {
6630 struct scsi_mode_select_6 *cdb;
6632 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6634 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6635 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6637 param_len = cdb->length;
6638 header_size = sizeof(struct scsi_mode_header_6);
6641 case MODE_SELECT_10: {
6642 struct scsi_mode_select_10 *cdb;
6644 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6646 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6647 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6649 param_len = scsi_2btoul(cdb->length);
6650 header_size = sizeof(struct scsi_mode_header_10);
6654 ctl_set_invalid_opcode(ctsio);
6655 ctl_done((union ctl_io *)ctsio);
6656 return (CTL_RETVAL_COMPLETE);
6657 break; /* NOTREACHED */
6662 * "A parameter list length of zero indicates that the Data-Out Buffer
6663 * shall be empty. This condition shall not be considered as an error."
6665 if (param_len == 0) {
6666 ctl_set_success(ctsio);
6667 ctl_done((union ctl_io *)ctsio);
6668 return (CTL_RETVAL_COMPLETE);
6672 * Since we'll hit this the first time through, prior to
6673 * allocation, we don't need to free a data buffer here.
6675 if (param_len < header_size) {
6676 ctl_set_param_len_error(ctsio);
6677 ctl_done((union ctl_io *)ctsio);
6678 return (CTL_RETVAL_COMPLETE);
6682 * Allocate the data buffer and grab the user's data. In theory,
6683 * we shouldn't have to sanity check the parameter list length here
6684 * because the maximum size is 64K. We should be able to malloc
6685 * that much without too many problems.
6687 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6688 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6689 ctsio->kern_data_len = param_len;
6690 ctsio->kern_total_len = param_len;
6691 ctsio->kern_data_resid = 0;
6692 ctsio->kern_rel_offset = 0;
6693 ctsio->kern_sg_entries = 0;
6694 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6695 ctsio->be_move_done = ctl_config_move_done;
6696 ctl_datamove((union ctl_io *)ctsio);
6698 return (CTL_RETVAL_COMPLETE);
6701 switch (ctsio->cdb[0]) {
6702 case MODE_SELECT_6: {
6703 struct scsi_mode_header_6 *mh6;
6705 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6706 bd_len = mh6->blk_desc_len;
6709 case MODE_SELECT_10: {
6710 struct scsi_mode_header_10 *mh10;
6712 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6713 bd_len = scsi_2btoul(mh10->blk_desc_len);
6717 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6721 if (param_len < (header_size + bd_len)) {
6722 free(ctsio->kern_data_ptr, M_CTL);
6723 ctl_set_param_len_error(ctsio);
6724 ctl_done((union ctl_io *)ctsio);
6725 return (CTL_RETVAL_COMPLETE);
6729 * Set the IO_CONT flag, so that if this I/O gets passed to
6730 * ctl_config_write_done(), it'll get passed back to
6731 * ctl_do_mode_select() for further processing, or completion if
6734 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6735 ctsio->io_cont = ctl_do_mode_select;
6737 modepage_info = (union ctl_modepage_info *)
6738 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6740 memset(modepage_info, 0, sizeof(*modepage_info));
6742 len_left = param_len - header_size - bd_len;
6743 len_used = header_size + bd_len;
6745 modepage_info->header.len_left = len_left;
6746 modepage_info->header.len_used = len_used;
6748 return (ctl_do_mode_select((union ctl_io *)ctsio));
6752 ctl_mode_sense(struct ctl_scsiio *ctsio)
6754 struct ctl_lun *lun;
6755 int pc, page_code, dbd, llba, subpage;
6756 int alloc_len, page_len, header_len, total_len;
6757 struct scsi_mode_block_descr *block_desc;
6758 struct ctl_page_index *page_index;
6766 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6768 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6770 if (lun->be_lun->lun_type != T_DIRECT)
6775 if (lun->flags & CTL_LUN_PR_RESERVED) {
6779 * XXX KDM need a lock here.
6781 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6782 if ((lun->res_type == SPR_TYPE_EX_AC
6783 && residx != lun->pr_res_idx)
6784 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6785 || lun->res_type == SPR_TYPE_EX_AC_AR)
6786 && !lun->per_res[residx].registered)) {
6787 ctl_set_reservation_conflict(ctsio);
6788 ctl_done((union ctl_io *)ctsio);
6789 return (CTL_RETVAL_COMPLETE);
6793 switch (ctsio->cdb[0]) {
6794 case MODE_SENSE_6: {
6795 struct scsi_mode_sense_6 *cdb;
6797 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6799 header_len = sizeof(struct scsi_mode_hdr_6);
6800 if (cdb->byte2 & SMS_DBD)
6803 header_len += sizeof(struct scsi_mode_block_descr);
6805 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6806 page_code = cdb->page & SMS_PAGE_CODE;
6807 subpage = cdb->subpage;
6808 alloc_len = cdb->length;
6811 case MODE_SENSE_10: {
6812 struct scsi_mode_sense_10 *cdb;
6814 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6816 header_len = sizeof(struct scsi_mode_hdr_10);
6818 if (cdb->byte2 & SMS_DBD)
6821 header_len += sizeof(struct scsi_mode_block_descr);
6822 if (cdb->byte2 & SMS10_LLBAA)
6824 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6825 page_code = cdb->page & SMS_PAGE_CODE;
6826 subpage = cdb->subpage;
6827 alloc_len = scsi_2btoul(cdb->length);
6831 ctl_set_invalid_opcode(ctsio);
6832 ctl_done((union ctl_io *)ctsio);
6833 return (CTL_RETVAL_COMPLETE);
6834 break; /* NOTREACHED */
6838 * We have to make a first pass through to calculate the size of
6839 * the pages that match the user's query. Then we allocate enough
6840 * memory to hold it, and actually copy the data into the buffer.
6842 switch (page_code) {
6843 case SMS_ALL_PAGES_PAGE: {
6849 * At the moment, values other than 0 and 0xff here are
6850 * reserved according to SPC-3.
6852 if ((subpage != SMS_SUBPAGE_PAGE_0)
6853 && (subpage != SMS_SUBPAGE_ALL)) {
6854 ctl_set_invalid_field(ctsio,
6860 ctl_done((union ctl_io *)ctsio);
6861 return (CTL_RETVAL_COMPLETE);
6864 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6865 if ((control_dev != 0)
6866 && (lun->mode_pages.index[i].page_flags &
6867 CTL_PAGE_FLAG_DISK_ONLY))
6871 * We don't use this subpage if the user didn't
6872 * request all subpages.
6874 if ((lun->mode_pages.index[i].subpage != 0)
6875 && (subpage == SMS_SUBPAGE_PAGE_0))
6879 printf("found page %#x len %d\n",
6880 lun->mode_pages.index[i].page_code &
6882 lun->mode_pages.index[i].page_len);
6884 page_len += lun->mode_pages.index[i].page_len;
6893 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6894 /* Look for the right page code */
6895 if ((lun->mode_pages.index[i].page_code &
6896 SMPH_PC_MASK) != page_code)
6899 /* Look for the right subpage or the subpage wildcard*/
6900 if ((lun->mode_pages.index[i].subpage != subpage)
6901 && (subpage != SMS_SUBPAGE_ALL))
6904 /* Make sure the page is supported for this dev type */
6905 if ((control_dev != 0)
6906 && (lun->mode_pages.index[i].page_flags &
6907 CTL_PAGE_FLAG_DISK_ONLY))
6911 printf("found page %#x len %d\n",
6912 lun->mode_pages.index[i].page_code &
6914 lun->mode_pages.index[i].page_len);
6917 page_len += lun->mode_pages.index[i].page_len;
6920 if (page_len == 0) {
6921 ctl_set_invalid_field(ctsio,
6927 ctl_done((union ctl_io *)ctsio);
6928 return (CTL_RETVAL_COMPLETE);
6934 total_len = header_len + page_len;
6936 printf("header_len = %d, page_len = %d, total_len = %d\n",
6937 header_len, page_len, total_len);
6940 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6941 ctsio->kern_sg_entries = 0;
6942 ctsio->kern_data_resid = 0;
6943 ctsio->kern_rel_offset = 0;
6944 if (total_len < alloc_len) {
6945 ctsio->residual = alloc_len - total_len;
6946 ctsio->kern_data_len = total_len;
6947 ctsio->kern_total_len = total_len;
6949 ctsio->residual = 0;
6950 ctsio->kern_data_len = alloc_len;
6951 ctsio->kern_total_len = alloc_len;
6954 switch (ctsio->cdb[0]) {
6955 case MODE_SENSE_6: {
6956 struct scsi_mode_hdr_6 *header;
6958 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6960 header->datalen = ctl_min(total_len - 1, 254);
6963 header->block_descr_len = 0;
6965 header->block_descr_len =
6966 sizeof(struct scsi_mode_block_descr);
6967 block_desc = (struct scsi_mode_block_descr *)&header[1];
6970 case MODE_SENSE_10: {
6971 struct scsi_mode_hdr_10 *header;
6974 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6976 datalen = ctl_min(total_len - 2, 65533);
6977 scsi_ulto2b(datalen, header->datalen);
6979 scsi_ulto2b(0, header->block_descr_len);
6981 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6982 header->block_descr_len);
6983 block_desc = (struct scsi_mode_block_descr *)&header[1];
6987 panic("invalid CDB type %#x", ctsio->cdb[0]);
6988 break; /* NOTREACHED */
6992 * If we've got a disk, use its blocksize in the block
6993 * descriptor. Otherwise, just set it to 0.
6996 if (control_dev != 0)
6997 scsi_ulto3b(lun->be_lun->blocksize,
6998 block_desc->block_len);
7000 scsi_ulto3b(0, block_desc->block_len);
7003 switch (page_code) {
7004 case SMS_ALL_PAGES_PAGE: {
7007 data_used = header_len;
7008 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7009 struct ctl_page_index *page_index;
7011 page_index = &lun->mode_pages.index[i];
7013 if ((control_dev != 0)
7014 && (page_index->page_flags &
7015 CTL_PAGE_FLAG_DISK_ONLY))
7019 * We don't use this subpage if the user didn't
7020 * request all subpages. We already checked (above)
7021 * to make sure the user only specified a subpage
7022 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
7024 if ((page_index->subpage != 0)
7025 && (subpage == SMS_SUBPAGE_PAGE_0))
7029 * Call the handler, if it exists, to update the
7030 * page to the latest values.
7032 if (page_index->sense_handler != NULL)
7033 page_index->sense_handler(ctsio, page_index,pc);
7035 memcpy(ctsio->kern_data_ptr + data_used,
7036 page_index->page_data +
7037 (page_index->page_len * pc),
7038 page_index->page_len);
7039 data_used += page_index->page_len;
7046 data_used = header_len;
7048 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7049 struct ctl_page_index *page_index;
7051 page_index = &lun->mode_pages.index[i];
7053 /* Look for the right page code */
7054 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
7057 /* Look for the right subpage or the subpage wildcard*/
7058 if ((page_index->subpage != subpage)
7059 && (subpage != SMS_SUBPAGE_ALL))
7062 /* Make sure the page is supported for this dev type */
7063 if ((control_dev != 0)
7064 && (page_index->page_flags &
7065 CTL_PAGE_FLAG_DISK_ONLY))
7069 * Call the handler, if it exists, to update the
7070 * page to the latest values.
7072 if (page_index->sense_handler != NULL)
7073 page_index->sense_handler(ctsio, page_index,pc);
7075 memcpy(ctsio->kern_data_ptr + data_used,
7076 page_index->page_data +
7077 (page_index->page_len * pc),
7078 page_index->page_len);
7079 data_used += page_index->page_len;
7085 ctsio->scsi_status = SCSI_STATUS_OK;
7087 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7088 ctsio->be_move_done = ctl_config_move_done;
7089 ctl_datamove((union ctl_io *)ctsio);
7091 return (CTL_RETVAL_COMPLETE);
7095 ctl_read_capacity(struct ctl_scsiio *ctsio)
7097 struct scsi_read_capacity *cdb;
7098 struct scsi_read_capacity_data *data;
7099 struct ctl_lun *lun;
7102 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7104 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7106 lba = scsi_4btoul(cdb->addr);
7107 if (((cdb->pmi & SRC_PMI) == 0)
7109 ctl_set_invalid_field(/*ctsio*/ ctsio,
7115 ctl_done((union ctl_io *)ctsio);
7116 return (CTL_RETVAL_COMPLETE);
7119 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7121 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7122 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7123 ctsio->residual = 0;
7124 ctsio->kern_data_len = sizeof(*data);
7125 ctsio->kern_total_len = sizeof(*data);
7126 ctsio->kern_data_resid = 0;
7127 ctsio->kern_rel_offset = 0;
7128 ctsio->kern_sg_entries = 0;
7131 * If the maximum LBA is greater than 0xfffffffe, the user must
7132 * issue a SERVICE ACTION IN (16) command, with the read capacity
7133 * serivce action set.
7135 if (lun->be_lun->maxlba > 0xfffffffe)
7136 scsi_ulto4b(0xffffffff, data->addr);
7138 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7141 * XXX KDM this may not be 512 bytes...
7143 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7145 ctsio->scsi_status = SCSI_STATUS_OK;
7147 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7148 ctsio->be_move_done = ctl_config_move_done;
7149 ctl_datamove((union ctl_io *)ctsio);
7151 return (CTL_RETVAL_COMPLETE);
7155 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7157 struct scsi_read_capacity_16 *cdb;
7158 struct scsi_read_capacity_data_long *data;
7159 struct ctl_lun *lun;
7163 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7165 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7167 alloc_len = scsi_4btoul(cdb->alloc_len);
7168 lba = scsi_8btou64(cdb->addr);
7170 if ((cdb->reladr & SRC16_PMI)
7172 ctl_set_invalid_field(/*ctsio*/ ctsio,
7178 ctl_done((union ctl_io *)ctsio);
7179 return (CTL_RETVAL_COMPLETE);
7182 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7184 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7185 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7187 if (sizeof(*data) < alloc_len) {
7188 ctsio->residual = alloc_len - sizeof(*data);
7189 ctsio->kern_data_len = sizeof(*data);
7190 ctsio->kern_total_len = sizeof(*data);
7192 ctsio->residual = 0;
7193 ctsio->kern_data_len = alloc_len;
7194 ctsio->kern_total_len = alloc_len;
7196 ctsio->kern_data_resid = 0;
7197 ctsio->kern_rel_offset = 0;
7198 ctsio->kern_sg_entries = 0;
7200 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7201 /* XXX KDM this may not be 512 bytes... */
7202 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7203 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7204 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7205 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7206 data->lalba_lbp[0] |= SRC16_LBPME;
7208 ctsio->scsi_status = SCSI_STATUS_OK;
7210 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7211 ctsio->be_move_done = ctl_config_move_done;
7212 ctl_datamove((union ctl_io *)ctsio);
7214 return (CTL_RETVAL_COMPLETE);
7218 ctl_service_action_in(struct ctl_scsiio *ctsio)
7220 struct scsi_service_action_in *cdb;
7223 CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
7225 cdb = (struct scsi_service_action_in *)ctsio->cdb;
7227 retval = CTL_RETVAL_COMPLETE;
7229 switch (cdb->service_action) {
7230 case SRC16_SERVICE_ACTION:
7231 retval = ctl_read_capacity_16(ctsio);
7234 ctl_set_invalid_field(/*ctsio*/ ctsio,
7240 ctl_done((union ctl_io *)ctsio);
7248 ctl_maintenance_in(struct ctl_scsiio *ctsio)
7250 struct scsi_maintenance_in *cdb;
7252 int alloc_len, total_len = 0;
7253 int num_target_port_groups, single;
7254 struct ctl_lun *lun;
7255 struct ctl_softc *softc;
7256 struct scsi_target_group_data *rtg_ptr;
7257 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
7258 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
7259 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
7261 CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
7263 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7264 softc = control_softc;
7265 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7267 retval = CTL_RETVAL_COMPLETE;
7269 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
7270 ctl_set_invalid_field(/*ctsio*/ ctsio,
7276 ctl_done((union ctl_io *)ctsio);
7280 mtx_lock(&softc->ctl_lock);
7281 single = ctl_is_single;
7282 mtx_unlock(&softc->ctl_lock);
7285 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
7287 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7289 total_len = sizeof(struct scsi_target_group_data) +
7290 sizeof(struct scsi_target_port_group_descriptor) *
7291 num_target_port_groups +
7292 sizeof(struct scsi_target_port_descriptor) *
7293 NUM_PORTS_PER_GRP * num_target_port_groups;
7295 alloc_len = scsi_4btoul(cdb->length);
7297 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7299 ctsio->kern_sg_entries = 0;
7301 if (total_len < alloc_len) {
7302 ctsio->residual = alloc_len - total_len;
7303 ctsio->kern_data_len = total_len;
7304 ctsio->kern_total_len = total_len;
7306 ctsio->residual = 0;
7307 ctsio->kern_data_len = alloc_len;
7308 ctsio->kern_total_len = alloc_len;
7310 ctsio->kern_data_resid = 0;
7311 ctsio->kern_rel_offset = 0;
7313 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
7315 tpg_desc_ptr1 = &rtg_ptr->groups[0];
7316 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
7317 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7318 &tp_desc_ptr1_1->desc_list[0];
7321 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7322 &tp_desc_ptr1_2->desc_list[0];
7323 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7324 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7325 &tp_desc_ptr2_1->desc_list[0];
7327 tpg_desc_ptr2 = NULL;
7328 tp_desc_ptr2_1 = NULL;
7329 tp_desc_ptr2_2 = NULL;
7332 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7334 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7335 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7336 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7337 tpg_desc_ptr2->pref_state =
7338 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7340 tpg_desc_ptr1->pref_state =
7341 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7342 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7345 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7346 tpg_desc_ptr1->pref_state =
7347 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7348 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7350 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7351 tpg_desc_ptr2->pref_state =
7352 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7356 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7358 tpg_desc_ptr1->support = 0;
7359 tpg_desc_ptr1->target_port_group[1] = 1;
7360 tpg_desc_ptr1->status = TPG_IMPLICIT;
7361 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7364 tpg_desc_ptr2->support = 0;
7365 tpg_desc_ptr2->target_port_group[1] = 2;
7366 tpg_desc_ptr2->status = TPG_IMPLICIT;
7367 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7369 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7370 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7372 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7373 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7375 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7376 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7377 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7379 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7380 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7384 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7385 ctsio->be_move_done = ctl_config_move_done;
7387 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7388 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7389 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7390 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7391 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7393 ctl_datamove((union ctl_io *)ctsio);
7398 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7400 struct scsi_per_res_in *cdb;
7401 int alloc_len, total_len = 0;
7402 /* struct scsi_per_res_in_rsrv in_data; */
7403 struct ctl_lun *lun;
7404 struct ctl_softc *softc;
7406 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7408 softc = control_softc;
7410 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7412 alloc_len = scsi_2btoul(cdb->length);
7414 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7417 mtx_lock(&softc->ctl_lock);
7418 switch (cdb->action) {
7419 case SPRI_RK: /* read keys */
7420 total_len = sizeof(struct scsi_per_res_in_keys) +
7422 sizeof(struct scsi_per_res_key);
7424 case SPRI_RR: /* read reservation */
7425 if (lun->flags & CTL_LUN_PR_RESERVED)
7426 total_len = sizeof(struct scsi_per_res_in_rsrv);
7428 total_len = sizeof(struct scsi_per_res_in_header);
7430 case SPRI_RC: /* report capabilities */
7431 total_len = sizeof(struct scsi_per_res_cap);
7433 case SPRI_RS: /* read full status */
7435 mtx_unlock(&softc->ctl_lock);
7436 ctl_set_invalid_field(ctsio,
7442 ctl_done((union ctl_io *)ctsio);
7443 return (CTL_RETVAL_COMPLETE);
7444 break; /* NOTREACHED */
7446 mtx_unlock(&softc->ctl_lock);
7448 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7450 if (total_len < alloc_len) {
7451 ctsio->residual = alloc_len - total_len;
7452 ctsio->kern_data_len = total_len;
7453 ctsio->kern_total_len = total_len;
7455 ctsio->residual = 0;
7456 ctsio->kern_data_len = alloc_len;
7457 ctsio->kern_total_len = alloc_len;
7460 ctsio->kern_data_resid = 0;
7461 ctsio->kern_rel_offset = 0;
7462 ctsio->kern_sg_entries = 0;
7464 mtx_lock(&softc->ctl_lock);
7465 switch (cdb->action) {
7466 case SPRI_RK: { // read keys
7467 struct scsi_per_res_in_keys *res_keys;
7470 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7473 * We had to drop the lock to allocate our buffer, which
7474 * leaves time for someone to come in with another
7475 * persistent reservation. (That is unlikely, though,
7476 * since this should be the only persistent reservation
7477 * command active right now.)
7479 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7480 (lun->pr_key_count *
7481 sizeof(struct scsi_per_res_key)))){
7482 mtx_unlock(&softc->ctl_lock);
7483 free(ctsio->kern_data_ptr, M_CTL);
7484 printf("%s: reservation length changed, retrying\n",
7489 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7491 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7492 lun->pr_key_count, res_keys->header.length);
7494 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7495 if (!lun->per_res[i].registered)
7499 * We used lun->pr_key_count to calculate the
7500 * size to allocate. If it turns out the number of
7501 * initiators with the registered flag set is
7502 * larger than that (i.e. they haven't been kept in
7503 * sync), we've got a problem.
7505 if (key_count >= lun->pr_key_count) {
7507 csevent_log(CSC_CTL | CSC_SHELF_SW |
7509 csevent_LogType_Fault,
7510 csevent_AlertLevel_Yellow,
7511 csevent_FRU_ShelfController,
7512 csevent_FRU_Firmware,
7513 csevent_FRU_Unknown,
7514 "registered keys %d >= key "
7515 "count %d", key_count,
7521 memcpy(res_keys->keys[key_count].key,
7522 lun->per_res[i].res_key.key,
7523 ctl_min(sizeof(res_keys->keys[key_count].key),
7524 sizeof(lun->per_res[i].res_key)));
7529 case SPRI_RR: { // read reservation
7530 struct scsi_per_res_in_rsrv *res;
7531 int tmp_len, header_only;
7533 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7535 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7537 if (lun->flags & CTL_LUN_PR_RESERVED)
7539 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7540 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7541 res->header.length);
7544 tmp_len = sizeof(struct scsi_per_res_in_header);
7545 scsi_ulto4b(0, res->header.length);
7550 * We had to drop the lock to allocate our buffer, which
7551 * leaves time for someone to come in with another
7552 * persistent reservation. (That is unlikely, though,
7553 * since this should be the only persistent reservation
7554 * command active right now.)
7556 if (tmp_len != total_len) {
7557 mtx_unlock(&softc->ctl_lock);
7558 free(ctsio->kern_data_ptr, M_CTL);
7559 printf("%s: reservation status changed, retrying\n",
7565 * No reservation held, so we're done.
7567 if (header_only != 0)
7571 * If the registration is an All Registrants type, the key
7572 * is 0, since it doesn't really matter.
7574 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7575 memcpy(res->data.reservation,
7576 &lun->per_res[lun->pr_res_idx].res_key,
7577 sizeof(struct scsi_per_res_key));
7579 res->data.scopetype = lun->res_type;
7582 case SPRI_RC: //report capabilities
7584 struct scsi_per_res_cap *res_cap;
7587 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7588 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7589 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7590 type_mask = SPRI_TM_WR_EX_AR |
7596 scsi_ulto2b(type_mask, res_cap->type_mask);
7599 case SPRI_RS: //read full status
7602 * This is a bug, because we just checked for this above,
7603 * and should have returned an error.
7605 panic("Invalid PR type %x", cdb->action);
7606 break; /* NOTREACHED */
7608 mtx_unlock(&softc->ctl_lock);
7610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7611 ctsio->be_move_done = ctl_config_move_done;
7613 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7614 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7615 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7616 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7617 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7619 ctl_datamove((union ctl_io *)ctsio);
7621 return (CTL_RETVAL_COMPLETE);
7625 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7629 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7630 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7631 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7632 struct scsi_per_res_out_parms* param)
7634 union ctl_ha_msg persis_io;
7640 if (sa_res_key == 0) {
7641 mtx_lock(&softc->ctl_lock);
7642 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7643 /* validate scope and type */
7644 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7646 mtx_unlock(&softc->ctl_lock);
7647 ctl_set_invalid_field(/*ctsio*/ ctsio,
7653 ctl_done((union ctl_io *)ctsio);
7657 if (type>8 || type==2 || type==4 || type==0) {
7658 mtx_unlock(&softc->ctl_lock);
7659 ctl_set_invalid_field(/*ctsio*/ ctsio,
7665 ctl_done((union ctl_io *)ctsio);
7669 /* temporarily unregister this nexus */
7670 lun->per_res[residx].registered = 0;
7673 * Unregister everybody else and build UA for
7676 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7677 if (lun->per_res[i].registered == 0)
7681 && i <CTL_MAX_INITIATORS)
7682 lun->pending_sense[i].ua_pending |=
7684 else if (persis_offset
7685 && i >= persis_offset)
7686 lun->pending_sense[i-persis_offset
7689 lun->per_res[i].registered = 0;
7690 memset(&lun->per_res[i].res_key, 0,
7691 sizeof(struct scsi_per_res_key));
7693 lun->per_res[residx].registered = 1;
7694 lun->pr_key_count = 1;
7695 lun->res_type = type;
7696 if (lun->res_type != SPR_TYPE_WR_EX_AR
7697 && lun->res_type != SPR_TYPE_EX_AC_AR)
7698 lun->pr_res_idx = residx;
7700 mtx_unlock(&softc->ctl_lock);
7701 /* send msg to other side */
7702 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7703 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7704 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7705 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7706 persis_io.pr.pr_info.res_type = type;
7707 memcpy(persis_io.pr.pr_info.sa_res_key,
7708 param->serv_act_res_key,
7709 sizeof(param->serv_act_res_key));
7710 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7711 &persis_io, sizeof(persis_io), 0)) >
7712 CTL_HA_STATUS_SUCCESS) {
7713 printf("CTL:Persis Out error returned "
7714 "from ctl_ha_msg_send %d\n",
7718 /* not all registrants */
7719 mtx_unlock(&softc->ctl_lock);
7720 free(ctsio->kern_data_ptr, M_CTL);
7721 ctl_set_invalid_field(ctsio,
7727 ctl_done((union ctl_io *)ctsio);
7730 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7731 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7734 mtx_lock(&softc->ctl_lock);
7735 if (res_key == sa_res_key) {
7738 * The spec implies this is not good but doesn't
7739 * say what to do. There are two choices either
7740 * generate a res conflict or check condition
7741 * with illegal field in parameter data. Since
7742 * that is what is done when the sa_res_key is
7743 * zero I'll take that approach since this has
7744 * to do with the sa_res_key.
7746 mtx_unlock(&softc->ctl_lock);
7747 free(ctsio->kern_data_ptr, M_CTL);
7748 ctl_set_invalid_field(ctsio,
7754 ctl_done((union ctl_io *)ctsio);
7758 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7759 if (lun->per_res[i].registered
7760 && memcmp(param->serv_act_res_key,
7761 lun->per_res[i].res_key.key,
7762 sizeof(struct scsi_per_res_key)) != 0)
7766 lun->per_res[i].registered = 0;
7767 memset(&lun->per_res[i].res_key, 0,
7768 sizeof(struct scsi_per_res_key));
7769 lun->pr_key_count--;
7772 && i < CTL_MAX_INITIATORS)
7773 lun->pending_sense[i].ua_pending |=
7775 else if (persis_offset
7776 && i >= persis_offset)
7777 lun->pending_sense[i-persis_offset].ua_pending|=
7780 mtx_unlock(&softc->ctl_lock);
7782 free(ctsio->kern_data_ptr, M_CTL);
7783 ctl_set_reservation_conflict(ctsio);
7784 ctl_done((union ctl_io *)ctsio);
7785 return (CTL_RETVAL_COMPLETE);
7787 /* send msg to other side */
7788 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7789 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7790 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7791 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7792 persis_io.pr.pr_info.res_type = type;
7793 memcpy(persis_io.pr.pr_info.sa_res_key,
7794 param->serv_act_res_key,
7795 sizeof(param->serv_act_res_key));
7796 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7797 &persis_io, sizeof(persis_io), 0)) >
7798 CTL_HA_STATUS_SUCCESS) {
7799 printf("CTL:Persis Out error returned from "
7800 "ctl_ha_msg_send %d\n", isc_retval);
7803 /* Reserved but not all registrants */
7804 /* sa_res_key is res holder */
7805 if (memcmp(param->serv_act_res_key,
7806 lun->per_res[lun->pr_res_idx].res_key.key,
7807 sizeof(struct scsi_per_res_key)) == 0) {
7808 /* validate scope and type */
7809 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7811 ctl_set_invalid_field(/*ctsio*/ ctsio,
7817 ctl_done((union ctl_io *)ctsio);
7821 if (type>8 || type==2 || type==4 || type==0) {
7822 ctl_set_invalid_field(/*ctsio*/ ctsio,
7828 ctl_done((union ctl_io *)ctsio);
7834 * if sa_res_key != res_key remove all
7835 * registrants w/sa_res_key and generate UA
7836 * for these registrants(Registrations
7837 * Preempted) if it wasn't an exclusive
7838 * reservation generate UA(Reservations
7839 * Preempted) for all other registered nexuses
7840 * if the type has changed. Establish the new
7841 * reservation and holder. If res_key and
7842 * sa_res_key are the same do the above
7843 * except don't unregister the res holder.
7847 * Temporarily unregister so it won't get
7848 * removed or UA generated
7850 lun->per_res[residx].registered = 0;
7851 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7852 if (lun->per_res[i].registered == 0)
7855 if (memcmp(param->serv_act_res_key,
7856 lun->per_res[i].res_key.key,
7857 sizeof(struct scsi_per_res_key)) == 0) {
7858 lun->per_res[i].registered = 0;
7859 memset(&lun->per_res[i].res_key,
7861 sizeof(struct scsi_per_res_key));
7862 lun->pr_key_count--;
7865 && i < CTL_MAX_INITIATORS)
7866 lun->pending_sense[i
7869 else if (persis_offset
7870 && i >= persis_offset)
7872 i-persis_offset].ua_pending |=
7874 } else if (type != lun->res_type
7875 && (lun->res_type == SPR_TYPE_WR_EX_RO
7876 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7878 && i < CTL_MAX_INITIATORS)
7879 lun->pending_sense[i
7882 else if (persis_offset
7883 && i >= persis_offset)
7890 lun->per_res[residx].registered = 1;
7891 lun->res_type = type;
7892 if (lun->res_type != SPR_TYPE_WR_EX_AR
7893 && lun->res_type != SPR_TYPE_EX_AC_AR)
7894 lun->pr_res_idx = residx;
7897 CTL_PR_ALL_REGISTRANTS;
7899 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7900 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7901 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7902 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7903 persis_io.pr.pr_info.res_type = type;
7904 memcpy(persis_io.pr.pr_info.sa_res_key,
7905 param->serv_act_res_key,
7906 sizeof(param->serv_act_res_key));
7907 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7908 &persis_io, sizeof(persis_io), 0)) >
7909 CTL_HA_STATUS_SUCCESS) {
7910 printf("CTL:Persis Out error returned "
7911 "from ctl_ha_msg_send %d\n",
7916 * sa_res_key is not the res holder just
7917 * remove registrants
7920 mtx_lock(&softc->ctl_lock);
7922 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7923 if (memcmp(param->serv_act_res_key,
7924 lun->per_res[i].res_key.key,
7925 sizeof(struct scsi_per_res_key)) != 0)
7929 lun->per_res[i].registered = 0;
7930 memset(&lun->per_res[i].res_key, 0,
7931 sizeof(struct scsi_per_res_key));
7932 lun->pr_key_count--;
7935 && i < CTL_MAX_INITIATORS)
7936 lun->pending_sense[i].ua_pending |=
7938 else if (persis_offset
7939 && i >= persis_offset)
7941 i-persis_offset].ua_pending |=
7946 mtx_unlock(&softc->ctl_lock);
7947 free(ctsio->kern_data_ptr, M_CTL);
7948 ctl_set_reservation_conflict(ctsio);
7949 ctl_done((union ctl_io *)ctsio);
7952 mtx_unlock(&softc->ctl_lock);
7953 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7954 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7955 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7956 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7957 persis_io.pr.pr_info.res_type = type;
7958 memcpy(persis_io.pr.pr_info.sa_res_key,
7959 param->serv_act_res_key,
7960 sizeof(param->serv_act_res_key));
7961 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7962 &persis_io, sizeof(persis_io), 0)) >
7963 CTL_HA_STATUS_SUCCESS) {
7964 printf("CTL:Persis Out error returned "
7965 "from ctl_ha_msg_send %d\n",
7971 lun->PRGeneration++;
7977 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7981 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7982 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7983 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7984 msg->pr.pr_info.sa_res_key,
7985 sizeof(struct scsi_per_res_key)) != 0) {
7986 uint64_t sa_res_key;
7987 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7989 if (sa_res_key == 0) {
7990 /* temporarily unregister this nexus */
7991 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7994 * Unregister everybody else and build UA for
7997 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7998 if (lun->per_res[i].registered == 0)
8002 && i < CTL_MAX_INITIATORS)
8003 lun->pending_sense[i].ua_pending |=
8005 else if (persis_offset && i >= persis_offset)
8006 lun->pending_sense[i -
8007 persis_offset].ua_pending |=
8009 lun->per_res[i].registered = 0;
8010 memset(&lun->per_res[i].res_key, 0,
8011 sizeof(struct scsi_per_res_key));
8014 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8015 lun->pr_key_count = 1;
8016 lun->res_type = msg->pr.pr_info.res_type;
8017 if (lun->res_type != SPR_TYPE_WR_EX_AR
8018 && lun->res_type != SPR_TYPE_EX_AC_AR)
8019 lun->pr_res_idx = msg->pr.pr_info.residx;
8021 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8022 if (memcmp(msg->pr.pr_info.sa_res_key,
8023 lun->per_res[i].res_key.key,
8024 sizeof(struct scsi_per_res_key)) != 0)
8027 lun->per_res[i].registered = 0;
8028 memset(&lun->per_res[i].res_key, 0,
8029 sizeof(struct scsi_per_res_key));
8030 lun->pr_key_count--;
8033 && i < persis_offset)
8034 lun->pending_sense[i].ua_pending |=
8036 else if (persis_offset
8037 && i >= persis_offset)
8038 lun->pending_sense[i -
8039 persis_offset].ua_pending |=
8045 * Temporarily unregister so it won't get removed
8048 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8049 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8050 if (lun->per_res[i].registered == 0)
8053 if (memcmp(msg->pr.pr_info.sa_res_key,
8054 lun->per_res[i].res_key.key,
8055 sizeof(struct scsi_per_res_key)) == 0) {
8056 lun->per_res[i].registered = 0;
8057 memset(&lun->per_res[i].res_key, 0,
8058 sizeof(struct scsi_per_res_key));
8059 lun->pr_key_count--;
8061 && i < CTL_MAX_INITIATORS)
8062 lun->pending_sense[i].ua_pending |=
8064 else if (persis_offset
8065 && i >= persis_offset)
8066 lun->pending_sense[i -
8067 persis_offset].ua_pending |=
8069 } else if (msg->pr.pr_info.res_type != lun->res_type
8070 && (lun->res_type == SPR_TYPE_WR_EX_RO
8071 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8073 && i < persis_offset)
8074 lun->pending_sense[i
8077 else if (persis_offset
8078 && i >= persis_offset)
8079 lun->pending_sense[i -
8080 persis_offset].ua_pending |=
8084 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8085 lun->res_type = msg->pr.pr_info.res_type;
8086 if (lun->res_type != SPR_TYPE_WR_EX_AR
8087 && lun->res_type != SPR_TYPE_EX_AC_AR)
8088 lun->pr_res_idx = msg->pr.pr_info.residx;
8090 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8092 lun->PRGeneration++;
8098 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8102 u_int32_t param_len;
8103 struct scsi_per_res_out *cdb;
8104 struct ctl_lun *lun;
8105 struct scsi_per_res_out_parms* param;
8106 struct ctl_softc *softc;
8108 uint64_t res_key, sa_res_key;
8110 union ctl_ha_msg persis_io;
8113 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8115 retval = CTL_RETVAL_COMPLETE;
8117 softc = control_softc;
8119 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8120 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8123 * We only support whole-LUN scope. The scope & type are ignored for
8124 * register, register and ignore existing key and clear.
8125 * We sometimes ignore scope and type on preempts too!!
8126 * Verify reservation type here as well.
8128 type = cdb->scope_type & SPR_TYPE_MASK;
8129 if ((cdb->action == SPRO_RESERVE)
8130 || (cdb->action == SPRO_RELEASE)) {
8131 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8132 ctl_set_invalid_field(/*ctsio*/ ctsio,
8138 ctl_done((union ctl_io *)ctsio);
8139 return (CTL_RETVAL_COMPLETE);
8142 if (type>8 || type==2 || type==4 || type==0) {
8143 ctl_set_invalid_field(/*ctsio*/ ctsio,
8149 ctl_done((union ctl_io *)ctsio);
8150 return (CTL_RETVAL_COMPLETE);
8154 switch (cdb->action & SPRO_ACTION_MASK) {
8165 ctl_set_invalid_field(/*ctsio*/ ctsio,
8171 ctl_done((union ctl_io *)ctsio);
8172 return (CTL_RETVAL_COMPLETE);
8173 break; /* NOTREACHED */
8176 param_len = scsi_4btoul(cdb->length);
8178 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8179 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8180 ctsio->kern_data_len = param_len;
8181 ctsio->kern_total_len = param_len;
8182 ctsio->kern_data_resid = 0;
8183 ctsio->kern_rel_offset = 0;
8184 ctsio->kern_sg_entries = 0;
8185 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8186 ctsio->be_move_done = ctl_config_move_done;
8187 ctl_datamove((union ctl_io *)ctsio);
8189 return (CTL_RETVAL_COMPLETE);
8192 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8194 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8195 res_key = scsi_8btou64(param->res_key.key);
8196 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8199 * Validate the reservation key here except for SPRO_REG_IGNO
8200 * This must be done for all other service actions
8202 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8203 mtx_lock(&softc->ctl_lock);
8204 if (lun->per_res[residx].registered) {
8205 if (memcmp(param->res_key.key,
8206 lun->per_res[residx].res_key.key,
8207 ctl_min(sizeof(param->res_key),
8208 sizeof(lun->per_res[residx].res_key))) != 0) {
8210 * The current key passed in doesn't match
8211 * the one the initiator previously
8214 mtx_unlock(&softc->ctl_lock);
8215 free(ctsio->kern_data_ptr, M_CTL);
8216 ctl_set_reservation_conflict(ctsio);
8217 ctl_done((union ctl_io *)ctsio);
8218 return (CTL_RETVAL_COMPLETE);
8220 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8222 * We are not registered
8224 mtx_unlock(&softc->ctl_lock);
8225 free(ctsio->kern_data_ptr, M_CTL);
8226 ctl_set_reservation_conflict(ctsio);
8227 ctl_done((union ctl_io *)ctsio);
8228 return (CTL_RETVAL_COMPLETE);
8229 } else if (res_key != 0) {
8231 * We are not registered and trying to register but
8232 * the register key isn't zero.
8234 mtx_unlock(&softc->ctl_lock);
8235 free(ctsio->kern_data_ptr, M_CTL);
8236 ctl_set_reservation_conflict(ctsio);
8237 ctl_done((union ctl_io *)ctsio);
8238 return (CTL_RETVAL_COMPLETE);
8240 mtx_unlock(&softc->ctl_lock);
8243 switch (cdb->action & SPRO_ACTION_MASK) {
8245 case SPRO_REG_IGNO: {
8248 printf("Registration received\n");
8252 * We don't support any of these options, as we report in
8253 * the read capabilities request (see
8254 * ctl_persistent_reserve_in(), above).
8256 if ((param->flags & SPR_SPEC_I_PT)
8257 || (param->flags & SPR_ALL_TG_PT)
8258 || (param->flags & SPR_APTPL)) {
8261 if (param->flags & SPR_APTPL)
8263 else if (param->flags & SPR_ALL_TG_PT)
8265 else /* SPR_SPEC_I_PT */
8268 free(ctsio->kern_data_ptr, M_CTL);
8269 ctl_set_invalid_field(ctsio,
8275 ctl_done((union ctl_io *)ctsio);
8276 return (CTL_RETVAL_COMPLETE);
8279 mtx_lock(&softc->ctl_lock);
8282 * The initiator wants to clear the
8285 if (sa_res_key == 0) {
8287 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8288 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8289 && !lun->per_res[residx].registered)) {
8290 mtx_unlock(&softc->ctl_lock);
8294 lun->per_res[residx].registered = 0;
8295 memset(&lun->per_res[residx].res_key,
8296 0, sizeof(lun->per_res[residx].res_key));
8297 lun->pr_key_count--;
8299 if (residx == lun->pr_res_idx) {
8300 lun->flags &= ~CTL_LUN_PR_RESERVED;
8301 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8303 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8304 || lun->res_type == SPR_TYPE_EX_AC_RO)
8305 && lun->pr_key_count) {
8307 * If the reservation is a registrants
8308 * only type we need to generate a UA
8309 * for other registered inits. The
8310 * sense code should be RESERVATIONS
8314 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8316 i+persis_offset].registered
8319 lun->pending_sense[i
8325 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8326 if (lun->pr_key_count==0) {
8327 lun->flags &= ~CTL_LUN_PR_RESERVED;
8329 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8332 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8333 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8334 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8335 persis_io.pr.pr_info.residx = residx;
8336 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8337 &persis_io, sizeof(persis_io), 0 )) >
8338 CTL_HA_STATUS_SUCCESS) {
8339 printf("CTL:Persis Out error returned from "
8340 "ctl_ha_msg_send %d\n", isc_retval);
8342 mtx_unlock(&softc->ctl_lock);
8343 } else /* sa_res_key != 0 */ {
8346 * If we aren't registered currently then increment
8347 * the key count and set the registered flag.
8349 if (!lun->per_res[residx].registered) {
8350 lun->pr_key_count++;
8351 lun->per_res[residx].registered = 1;
8354 memcpy(&lun->per_res[residx].res_key,
8355 param->serv_act_res_key,
8356 ctl_min(sizeof(param->serv_act_res_key),
8357 sizeof(lun->per_res[residx].res_key)));
8359 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8360 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8361 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8362 persis_io.pr.pr_info.residx = residx;
8363 memcpy(persis_io.pr.pr_info.sa_res_key,
8364 param->serv_act_res_key,
8365 sizeof(param->serv_act_res_key));
8366 mtx_unlock(&softc->ctl_lock);
8367 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8368 &persis_io, sizeof(persis_io), 0)) >
8369 CTL_HA_STATUS_SUCCESS) {
8370 printf("CTL:Persis Out error returned from "
8371 "ctl_ha_msg_send %d\n", isc_retval);
8374 lun->PRGeneration++;
8380 printf("Reserve executed type %d\n", type);
8382 mtx_lock(&softc->ctl_lock);
8383 if (lun->flags & CTL_LUN_PR_RESERVED) {
8385 * if this isn't the reservation holder and it's
8386 * not a "all registrants" type or if the type is
8387 * different then we have a conflict
8389 if ((lun->pr_res_idx != residx
8390 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8391 || lun->res_type != type) {
8392 mtx_unlock(&softc->ctl_lock);
8393 free(ctsio->kern_data_ptr, M_CTL);
8394 ctl_set_reservation_conflict(ctsio);
8395 ctl_done((union ctl_io *)ctsio);
8396 return (CTL_RETVAL_COMPLETE);
8398 mtx_unlock(&softc->ctl_lock);
8399 } else /* create a reservation */ {
8401 * If it's not an "all registrants" type record
8402 * reservation holder
8404 if (type != SPR_TYPE_WR_EX_AR
8405 && type != SPR_TYPE_EX_AC_AR)
8406 lun->pr_res_idx = residx; /* Res holder */
8408 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8410 lun->flags |= CTL_LUN_PR_RESERVED;
8411 lun->res_type = type;
8413 mtx_unlock(&softc->ctl_lock);
8415 /* send msg to other side */
8416 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8417 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8418 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8419 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8420 persis_io.pr.pr_info.res_type = type;
8421 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8422 &persis_io, sizeof(persis_io), 0)) >
8423 CTL_HA_STATUS_SUCCESS) {
8424 printf("CTL:Persis Out error returned from "
8425 "ctl_ha_msg_send %d\n", isc_retval);
8431 mtx_lock(&softc->ctl_lock);
8432 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8433 /* No reservation exists return good status */
8434 mtx_unlock(&softc->ctl_lock);
8438 * Is this nexus a reservation holder?
8440 if (lun->pr_res_idx != residx
8441 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8443 * not a res holder return good status but
8446 mtx_unlock(&softc->ctl_lock);
8450 if (lun->res_type != type) {
8451 mtx_unlock(&softc->ctl_lock);
8452 free(ctsio->kern_data_ptr, M_CTL);
8453 ctl_set_illegal_pr_release(ctsio);
8454 ctl_done((union ctl_io *)ctsio);
8455 return (CTL_RETVAL_COMPLETE);
8458 /* okay to release */
8459 lun->flags &= ~CTL_LUN_PR_RESERVED;
8460 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8464 * if this isn't an exclusive access
8465 * res generate UA for all other
8468 if (type != SPR_TYPE_EX_AC
8469 && type != SPR_TYPE_WR_EX) {
8471 * temporarily unregister so we don't generate UA
8473 lun->per_res[residx].registered = 0;
8475 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8476 if (lun->per_res[i+persis_offset].registered
8479 lun->pending_sense[i].ua_pending |=
8483 lun->per_res[residx].registered = 1;
8485 mtx_unlock(&softc->ctl_lock);
8486 /* Send msg to other side */
8487 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8488 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8489 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8490 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8491 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8492 printf("CTL:Persis Out error returned from "
8493 "ctl_ha_msg_send %d\n", isc_retval);
8498 /* send msg to other side */
8500 mtx_lock(&softc->ctl_lock);
8501 lun->flags &= ~CTL_LUN_PR_RESERVED;
8503 lun->pr_key_count = 0;
8504 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8507 memset(&lun->per_res[residx].res_key,
8508 0, sizeof(lun->per_res[residx].res_key));
8509 lun->per_res[residx].registered = 0;
8511 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8512 if (lun->per_res[i].registered) {
8513 if (!persis_offset && i < CTL_MAX_INITIATORS)
8514 lun->pending_sense[i].ua_pending |=
8516 else if (persis_offset && i >= persis_offset)
8517 lun->pending_sense[i-persis_offset
8518 ].ua_pending |= CTL_UA_RES_PREEMPT;
8520 memset(&lun->per_res[i].res_key,
8521 0, sizeof(struct scsi_per_res_key));
8522 lun->per_res[i].registered = 0;
8524 lun->PRGeneration++;
8525 mtx_unlock(&softc->ctl_lock);
8526 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8527 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8528 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8529 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8530 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8531 printf("CTL:Persis Out error returned from "
8532 "ctl_ha_msg_send %d\n", isc_retval);
8536 case SPRO_PREEMPT: {
8539 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8540 residx, ctsio, cdb, param);
8542 return (CTL_RETVAL_COMPLETE);
8548 free(ctsio->kern_data_ptr, M_CTL);
8549 ctl_set_invalid_field(/*ctsio*/ ctsio,
8555 ctl_done((union ctl_io *)ctsio);
8556 return (CTL_RETVAL_COMPLETE);
8557 break; /* NOTREACHED */
8561 free(ctsio->kern_data_ptr, M_CTL);
8562 ctl_set_success(ctsio);
8563 ctl_done((union ctl_io *)ctsio);
8569 * This routine is for handling a message from the other SC pertaining to
8570 * persistent reserve out. All the error checking will have been done
8571 * so only perorming the action need be done here to keep the two
8575 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8577 struct ctl_lun *lun;
8578 struct ctl_softc *softc;
8582 softc = control_softc;
8584 mtx_lock(&softc->ctl_lock);
8586 targ_lun = msg->hdr.nexus.targ_lun;
8587 if (msg->hdr.nexus.lun_map_fn != NULL)
8588 targ_lun = msg->hdr.nexus.lun_map_fn(msg->hdr.nexus.lun_map_arg, targ_lun);
8589 lun = softc->ctl_luns[targ_lun];
8590 switch(msg->pr.pr_info.action) {
8591 case CTL_PR_REG_KEY:
8592 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8593 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8594 lun->pr_key_count++;
8596 lun->PRGeneration++;
8597 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8598 msg->pr.pr_info.sa_res_key,
8599 sizeof(struct scsi_per_res_key));
8602 case CTL_PR_UNREG_KEY:
8603 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8604 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8605 0, sizeof(struct scsi_per_res_key));
8606 lun->pr_key_count--;
8608 /* XXX Need to see if the reservation has been released */
8609 /* if so do we need to generate UA? */
8610 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8611 lun->flags &= ~CTL_LUN_PR_RESERVED;
8612 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8614 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8615 || lun->res_type == SPR_TYPE_EX_AC_RO)
8616 && lun->pr_key_count) {
8618 * If the reservation is a registrants
8619 * only type we need to generate a UA
8620 * for other registered inits. The
8621 * sense code should be RESERVATIONS
8625 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8627 persis_offset].registered == 0)
8630 lun->pending_sense[i
8636 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8637 if (lun->pr_key_count==0) {
8638 lun->flags &= ~CTL_LUN_PR_RESERVED;
8640 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8643 lun->PRGeneration++;
8646 case CTL_PR_RESERVE:
8647 lun->flags |= CTL_LUN_PR_RESERVED;
8648 lun->res_type = msg->pr.pr_info.res_type;
8649 lun->pr_res_idx = msg->pr.pr_info.residx;
8653 case CTL_PR_RELEASE:
8655 * if this isn't an exclusive access res generate UA for all
8656 * other registrants.
8658 if (lun->res_type != SPR_TYPE_EX_AC
8659 && lun->res_type != SPR_TYPE_WR_EX) {
8660 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8661 if (lun->per_res[i+persis_offset].registered)
8662 lun->pending_sense[i].ua_pending |=
8666 lun->flags &= ~CTL_LUN_PR_RESERVED;
8667 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8671 case CTL_PR_PREEMPT:
8672 ctl_pro_preempt_other(lun, msg);
8675 lun->flags &= ~CTL_LUN_PR_RESERVED;
8677 lun->pr_key_count = 0;
8678 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8680 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8681 if (lun->per_res[i].registered == 0)
8684 && i < CTL_MAX_INITIATORS)
8685 lun->pending_sense[i].ua_pending |=
8687 else if (persis_offset
8688 && i >= persis_offset)
8689 lun->pending_sense[i-persis_offset].ua_pending|=
8691 memset(&lun->per_res[i].res_key, 0,
8692 sizeof(struct scsi_per_res_key));
8693 lun->per_res[i].registered = 0;
8695 lun->PRGeneration++;
8699 mtx_unlock(&softc->ctl_lock);
8703 ctl_read_write(struct ctl_scsiio *ctsio)
8705 struct ctl_lun *lun;
8706 struct ctl_lba_len_flags *lbalen;
8708 uint32_t num_blocks;
8709 int reladdr, fua, dpo, ebp;
8713 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8715 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8722 retval = CTL_RETVAL_COMPLETE;
8724 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8725 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8726 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8730 * XXX KDM need a lock here.
8732 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8733 if ((lun->res_type == SPR_TYPE_EX_AC
8734 && residx != lun->pr_res_idx)
8735 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8736 || lun->res_type == SPR_TYPE_EX_AC_AR)
8737 && !lun->per_res[residx].registered)) {
8738 ctl_set_reservation_conflict(ctsio);
8739 ctl_done((union ctl_io *)ctsio);
8740 return (CTL_RETVAL_COMPLETE);
8744 switch (ctsio->cdb[0]) {
8747 struct scsi_rw_6 *cdb;
8749 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8751 lba = scsi_3btoul(cdb->addr);
8752 /* only 5 bits are valid in the most significant address byte */
8754 num_blocks = cdb->length;
8756 * This is correct according to SBC-2.
8758 if (num_blocks == 0)
8764 struct scsi_rw_10 *cdb;
8766 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8768 if (cdb->byte2 & SRW10_RELADDR)
8770 if (cdb->byte2 & SRW10_FUA)
8772 if (cdb->byte2 & SRW10_DPO)
8775 if ((cdb->opcode == WRITE_10)
8776 && (cdb->byte2 & SRW10_EBP))
8779 lba = scsi_4btoul(cdb->addr);
8780 num_blocks = scsi_2btoul(cdb->length);
8783 case WRITE_VERIFY_10: {
8784 struct scsi_write_verify_10 *cdb;
8786 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8789 * XXX KDM we should do actual write verify support at some
8790 * point. This is obviously fake, we're just translating
8791 * things to a write. So we don't even bother checking the
8792 * BYTCHK field, since we don't do any verification. If
8793 * the user asks for it, we'll just pretend we did it.
8795 if (cdb->byte2 & SWV_DPO)
8798 lba = scsi_4btoul(cdb->addr);
8799 num_blocks = scsi_2btoul(cdb->length);
8804 struct scsi_rw_12 *cdb;
8806 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8808 if (cdb->byte2 & SRW12_RELADDR)
8810 if (cdb->byte2 & SRW12_FUA)
8812 if (cdb->byte2 & SRW12_DPO)
8814 lba = scsi_4btoul(cdb->addr);
8815 num_blocks = scsi_4btoul(cdb->length);
8818 case WRITE_VERIFY_12: {
8819 struct scsi_write_verify_12 *cdb;
8821 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8823 if (cdb->byte2 & SWV_DPO)
8826 lba = scsi_4btoul(cdb->addr);
8827 num_blocks = scsi_4btoul(cdb->length);
8833 struct scsi_rw_16 *cdb;
8835 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8837 if (cdb->byte2 & SRW12_RELADDR)
8839 if (cdb->byte2 & SRW12_FUA)
8841 if (cdb->byte2 & SRW12_DPO)
8844 lba = scsi_8btou64(cdb->addr);
8845 num_blocks = scsi_4btoul(cdb->length);
8848 case WRITE_VERIFY_16: {
8849 struct scsi_write_verify_16 *cdb;
8851 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8853 if (cdb->byte2 & SWV_DPO)
8856 lba = scsi_8btou64(cdb->addr);
8857 num_blocks = scsi_4btoul(cdb->length);
8862 * We got a command we don't support. This shouldn't
8863 * happen, commands should be filtered out above us.
8865 ctl_set_invalid_opcode(ctsio);
8866 ctl_done((union ctl_io *)ctsio);
8868 return (CTL_RETVAL_COMPLETE);
8869 break; /* NOTREACHED */
8873 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8874 * interesting for us, but if RAIDCore is in write-back mode,
8875 * getting it to do write-through for a particular transaction may
8879 * We don't support relative addressing. That also requires
8880 * supporting linked commands, which we don't do.
8883 ctl_set_invalid_field(ctsio,
8889 ctl_done((union ctl_io *)ctsio);
8890 return (CTL_RETVAL_COMPLETE);
8894 * The first check is to make sure we're in bounds, the second
8895 * check is to catch wrap-around problems. If the lba + num blocks
8896 * is less than the lba, then we've wrapped around and the block
8897 * range is invalid anyway.
8899 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8900 || ((lba + num_blocks) < lba)) {
8901 ctl_set_lba_out_of_range(ctsio);
8902 ctl_done((union ctl_io *)ctsio);
8903 return (CTL_RETVAL_COMPLETE);
8907 * According to SBC-3, a transfer length of 0 is not an error.
8908 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8909 * translates to 256 blocks for those commands.
8911 if (num_blocks == 0) {
8912 ctl_set_success(ctsio);
8913 ctl_done((union ctl_io *)ctsio);
8914 return (CTL_RETVAL_COMPLETE);
8917 lbalen = (struct ctl_lba_len_flags *)
8918 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8920 lbalen->len = num_blocks;
8921 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
8923 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8924 ctsio->kern_rel_offset = 0;
8926 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8928 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8934 ctl_cnw_cont(union ctl_io *io)
8936 struct ctl_scsiio *ctsio;
8937 struct ctl_lun *lun;
8938 struct ctl_lba_len_flags *lbalen;
8941 ctsio = &io->scsiio;
8942 ctsio->io_hdr.status = CTL_STATUS_NONE;
8943 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8944 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8945 lbalen = (struct ctl_lba_len_flags *)
8946 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8947 lbalen->flags = CTL_LLF_WRITE;
8949 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8950 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8955 ctl_cnw(struct ctl_scsiio *ctsio)
8957 struct ctl_lun *lun;
8958 struct ctl_lba_len_flags *lbalen;
8960 uint32_t num_blocks;
8964 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8966 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8971 retval = CTL_RETVAL_COMPLETE;
8973 switch (ctsio->cdb[0]) {
8974 case COMPARE_AND_WRITE: {
8975 struct scsi_compare_and_write *cdb;
8977 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8979 if (cdb->byte2 & SRW10_FUA)
8981 if (cdb->byte2 & SRW10_DPO)
8983 lba = scsi_8btou64(cdb->addr);
8984 num_blocks = cdb->length;
8989 * We got a command we don't support. This shouldn't
8990 * happen, commands should be filtered out above us.
8992 ctl_set_invalid_opcode(ctsio);
8993 ctl_done((union ctl_io *)ctsio);
8995 return (CTL_RETVAL_COMPLETE);
8996 break; /* NOTREACHED */
9000 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
9001 * interesting for us, but if RAIDCore is in write-back mode,
9002 * getting it to do write-through for a particular transaction may
9007 * The first check is to make sure we're in bounds, the second
9008 * check is to catch wrap-around problems. If the lba + num blocks
9009 * is less than the lba, then we've wrapped around and the block
9010 * range is invalid anyway.
9012 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9013 || ((lba + num_blocks) < lba)) {
9014 ctl_set_lba_out_of_range(ctsio);
9015 ctl_done((union ctl_io *)ctsio);
9016 return (CTL_RETVAL_COMPLETE);
9020 * According to SBC-3, a transfer length of 0 is not an error.
9022 if (num_blocks == 0) {
9023 ctl_set_success(ctsio);
9024 ctl_done((union ctl_io *)ctsio);
9025 return (CTL_RETVAL_COMPLETE);
9028 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9029 ctsio->kern_rel_offset = 0;
9032 * Set the IO_CONT flag, so that if this I/O gets passed to
9033 * ctl_data_submit_done(), it'll get passed back to
9034 * ctl_ctl_cnw_cont() for further processing.
9036 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9037 ctsio->io_cont = ctl_cnw_cont;
9039 lbalen = (struct ctl_lba_len_flags *)
9040 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9042 lbalen->len = num_blocks;
9043 lbalen->flags = CTL_LLF_COMPARE;
9045 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9046 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9051 ctl_verify(struct ctl_scsiio *ctsio)
9053 struct ctl_lun *lun;
9054 struct ctl_lba_len_flags *lbalen;
9056 uint32_t num_blocks;
9060 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9062 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9066 retval = CTL_RETVAL_COMPLETE;
9068 switch (ctsio->cdb[0]) {
9070 struct scsi_verify_10 *cdb;
9072 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9073 if (cdb->byte2 & SVFY_BYTCHK)
9075 if (cdb->byte2 & SVFY_DPO)
9077 lba = scsi_4btoul(cdb->addr);
9078 num_blocks = scsi_2btoul(cdb->length);
9082 struct scsi_verify_12 *cdb;
9084 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9085 if (cdb->byte2 & SVFY_BYTCHK)
9087 if (cdb->byte2 & SVFY_DPO)
9089 lba = scsi_4btoul(cdb->addr);
9090 num_blocks = scsi_4btoul(cdb->length);
9094 struct scsi_rw_16 *cdb;
9096 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9097 if (cdb->byte2 & SVFY_BYTCHK)
9099 if (cdb->byte2 & SVFY_DPO)
9101 lba = scsi_8btou64(cdb->addr);
9102 num_blocks = scsi_4btoul(cdb->length);
9107 * We got a command we don't support. This shouldn't
9108 * happen, commands should be filtered out above us.
9110 ctl_set_invalid_opcode(ctsio);
9111 ctl_done((union ctl_io *)ctsio);
9112 return (CTL_RETVAL_COMPLETE);
9116 * The first check is to make sure we're in bounds, the second
9117 * check is to catch wrap-around problems. If the lba + num blocks
9118 * is less than the lba, then we've wrapped around and the block
9119 * range is invalid anyway.
9121 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9122 || ((lba + num_blocks) < lba)) {
9123 ctl_set_lba_out_of_range(ctsio);
9124 ctl_done((union ctl_io *)ctsio);
9125 return (CTL_RETVAL_COMPLETE);
9129 * According to SBC-3, a transfer length of 0 is not an error.
9131 if (num_blocks == 0) {
9132 ctl_set_success(ctsio);
9133 ctl_done((union ctl_io *)ctsio);
9134 return (CTL_RETVAL_COMPLETE);
9137 lbalen = (struct ctl_lba_len_flags *)
9138 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9140 lbalen->len = num_blocks;
9142 lbalen->flags = CTL_LLF_COMPARE;
9143 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9145 lbalen->flags = CTL_LLF_VERIFY;
9146 ctsio->kern_total_len = 0;
9148 ctsio->kern_rel_offset = 0;
9150 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9151 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9156 ctl_report_luns(struct ctl_scsiio *ctsio)
9158 struct scsi_report_luns *cdb;
9159 struct scsi_report_luns_data *lun_data;
9160 struct ctl_lun *lun, *request_lun;
9161 int num_luns, retval;
9162 uint32_t alloc_len, lun_datalen;
9163 int num_filled, well_known;
9164 uint32_t initidx, targ_lun_id, lun_id;
9166 retval = CTL_RETVAL_COMPLETE;
9169 cdb = (struct scsi_report_luns *)ctsio->cdb;
9171 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9173 mtx_lock(&control_softc->ctl_lock);
9174 num_luns = control_softc->num_luns;
9175 mtx_unlock(&control_softc->ctl_lock);
9177 switch (cdb->select_report) {
9178 case RPL_REPORT_DEFAULT:
9179 case RPL_REPORT_ALL:
9181 case RPL_REPORT_WELLKNOWN:
9186 ctl_set_invalid_field(ctsio,
9192 ctl_done((union ctl_io *)ctsio);
9194 break; /* NOTREACHED */
9197 alloc_len = scsi_4btoul(cdb->length);
9199 * The initiator has to allocate at least 16 bytes for this request,
9200 * so he can at least get the header and the first LUN. Otherwise
9201 * we reject the request (per SPC-3 rev 14, section 6.21).
9203 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9204 sizeof(struct scsi_report_luns_lundata))) {
9205 ctl_set_invalid_field(ctsio,
9211 ctl_done((union ctl_io *)ctsio);
9215 request_lun = (struct ctl_lun *)
9216 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9218 lun_datalen = sizeof(*lun_data) +
9219 (num_luns * sizeof(struct scsi_report_luns_lundata));
9221 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9222 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9223 ctsio->kern_sg_entries = 0;
9225 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9227 mtx_lock(&control_softc->ctl_lock);
9228 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9229 lun_id = targ_lun_id;
9230 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
9231 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
9232 if (lun_id >= CTL_MAX_LUNS)
9234 lun = control_softc->ctl_luns[lun_id];
9238 if (targ_lun_id <= 0xff) {
9240 * Peripheral addressing method, bus number 0.
9242 lun_data->luns[num_filled].lundata[0] =
9243 RPL_LUNDATA_ATYP_PERIPH;
9244 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9246 } else if (targ_lun_id <= 0x3fff) {
9248 * Flat addressing method.
9250 lun_data->luns[num_filled].lundata[0] =
9251 RPL_LUNDATA_ATYP_FLAT |
9252 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9253 #ifdef OLDCTLHEADERS
9254 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9255 (targ_lun_id & SRLD_BUS_LUN_MASK);
9257 lun_data->luns[num_filled].lundata[1] =
9258 #ifdef OLDCTLHEADERS
9259 targ_lun_id >> SRLD_BUS_LUN_BITS;
9261 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9264 printf("ctl_report_luns: bogus LUN number %jd, "
9265 "skipping\n", (intmax_t)targ_lun_id);
9268 * According to SPC-3, rev 14 section 6.21:
9270 * "The execution of a REPORT LUNS command to any valid and
9271 * installed logical unit shall clear the REPORTED LUNS DATA
9272 * HAS CHANGED unit attention condition for all logical
9273 * units of that target with respect to the requesting
9274 * initiator. A valid and installed logical unit is one
9275 * having a PERIPHERAL QUALIFIER of 000b in the standard
9276 * INQUIRY data (see 6.4.2)."
9278 * If request_lun is NULL, the LUN this report luns command
9279 * was issued to is either disabled or doesn't exist. In that
9280 * case, we shouldn't clear any pending lun change unit
9283 if (request_lun != NULL)
9284 lun->pending_sense[initidx].ua_pending &=
9287 mtx_unlock(&control_softc->ctl_lock);
9290 * It's quite possible that we've returned fewer LUNs than we allocated
9291 * space for. Trim it.
9293 lun_datalen = sizeof(*lun_data) +
9294 (num_filled * sizeof(struct scsi_report_luns_lundata));
9296 if (lun_datalen < alloc_len) {
9297 ctsio->residual = alloc_len - lun_datalen;
9298 ctsio->kern_data_len = lun_datalen;
9299 ctsio->kern_total_len = lun_datalen;
9301 ctsio->residual = 0;
9302 ctsio->kern_data_len = alloc_len;
9303 ctsio->kern_total_len = alloc_len;
9305 ctsio->kern_data_resid = 0;
9306 ctsio->kern_rel_offset = 0;
9307 ctsio->kern_sg_entries = 0;
9310 * We set this to the actual data length, regardless of how much
9311 * space we actually have to return results. If the user looks at
9312 * this value, he'll know whether or not he allocated enough space
9313 * and reissue the command if necessary. We don't support well
9314 * known logical units, so if the user asks for that, return none.
9316 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9319 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9322 ctsio->scsi_status = SCSI_STATUS_OK;
9324 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9325 ctsio->be_move_done = ctl_config_move_done;
9326 ctl_datamove((union ctl_io *)ctsio);
9332 ctl_request_sense(struct ctl_scsiio *ctsio)
9334 struct scsi_request_sense *cdb;
9335 struct scsi_sense_data *sense_ptr;
9336 struct ctl_lun *lun;
9339 scsi_sense_data_type sense_format;
9341 cdb = (struct scsi_request_sense *)ctsio->cdb;
9343 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9345 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9348 * Determine which sense format the user wants.
9350 if (cdb->byte2 & SRS_DESC)
9351 sense_format = SSD_TYPE_DESC;
9353 sense_format = SSD_TYPE_FIXED;
9355 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9356 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9357 ctsio->kern_sg_entries = 0;
9360 * struct scsi_sense_data, which is currently set to 256 bytes, is
9361 * larger than the largest allowed value for the length field in the
9362 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9364 ctsio->residual = 0;
9365 ctsio->kern_data_len = cdb->length;
9366 ctsio->kern_total_len = cdb->length;
9368 ctsio->kern_data_resid = 0;
9369 ctsio->kern_rel_offset = 0;
9370 ctsio->kern_sg_entries = 0;
9373 * If we don't have a LUN, we don't have any pending sense.
9379 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9381 * Check for pending sense, and then for pending unit attentions.
9382 * Pending sense gets returned first, then pending unit attentions.
9384 mtx_lock(&lun->ctl_softc->ctl_lock);
9385 if (ctl_is_set(lun->have_ca, initidx)) {
9386 scsi_sense_data_type stored_format;
9389 * Check to see which sense format was used for the stored
9392 stored_format = scsi_sense_type(
9393 &lun->pending_sense[initidx].sense);
9396 * If the user requested a different sense format than the
9397 * one we stored, then we need to convert it to the other
9398 * format. If we're going from descriptor to fixed format
9399 * sense data, we may lose things in translation, depending
9400 * on what options were used.
9402 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9403 * for some reason we'll just copy it out as-is.
9405 if ((stored_format == SSD_TYPE_FIXED)
9406 && (sense_format == SSD_TYPE_DESC))
9407 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9408 &lun->pending_sense[initidx].sense,
9409 (struct scsi_sense_data_desc *)sense_ptr);
9410 else if ((stored_format == SSD_TYPE_DESC)
9411 && (sense_format == SSD_TYPE_FIXED))
9412 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9413 &lun->pending_sense[initidx].sense,
9414 (struct scsi_sense_data_fixed *)sense_ptr);
9416 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9417 ctl_min(sizeof(*sense_ptr),
9418 sizeof(lun->pending_sense[initidx].sense)));
9420 ctl_clear_mask(lun->have_ca, initidx);
9422 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9423 ctl_ua_type ua_type;
9425 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9426 sense_ptr, sense_format);
9427 if (ua_type != CTL_UA_NONE) {
9429 /* We're reporting this UA, so clear it */
9430 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9433 mtx_unlock(&lun->ctl_softc->ctl_lock);
9436 * We already have a pending error, return it.
9438 if (have_error != 0) {
9440 * We report the SCSI status as OK, since the status of the
9441 * request sense command itself is OK.
9443 ctsio->scsi_status = SCSI_STATUS_OK;
9446 * We report 0 for the sense length, because we aren't doing
9447 * autosense in this case. We're reporting sense as
9450 ctsio->sense_len = 0;
9451 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9452 ctsio->be_move_done = ctl_config_move_done;
9453 ctl_datamove((union ctl_io *)ctsio);
9455 return (CTL_RETVAL_COMPLETE);
9461 * No sense information to report, so we report that everything is
9464 ctl_set_sense_data(sense_ptr,
9467 /*current_error*/ 1,
9468 /*sense_key*/ SSD_KEY_NO_SENSE,
9473 ctsio->scsi_status = SCSI_STATUS_OK;
9476 * We report 0 for the sense length, because we aren't doing
9477 * autosense in this case. We're reporting sense as parameter data.
9479 ctsio->sense_len = 0;
9480 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9481 ctsio->be_move_done = ctl_config_move_done;
9482 ctl_datamove((union ctl_io *)ctsio);
9484 return (CTL_RETVAL_COMPLETE);
9488 ctl_tur(struct ctl_scsiio *ctsio)
9490 struct ctl_lun *lun;
9492 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9494 CTL_DEBUG_PRINT(("ctl_tur\n"));
9499 ctsio->scsi_status = SCSI_STATUS_OK;
9500 ctsio->io_hdr.status = CTL_SUCCESS;
9502 ctl_done((union ctl_io *)ctsio);
9504 return (CTL_RETVAL_COMPLETE);
9509 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9516 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9518 struct scsi_vpd_supported_pages *pages;
9520 struct ctl_lun *lun;
9522 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9524 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9525 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9526 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9527 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9528 ctsio->kern_sg_entries = 0;
9530 if (sup_page_size < alloc_len) {
9531 ctsio->residual = alloc_len - sup_page_size;
9532 ctsio->kern_data_len = sup_page_size;
9533 ctsio->kern_total_len = sup_page_size;
9535 ctsio->residual = 0;
9536 ctsio->kern_data_len = alloc_len;
9537 ctsio->kern_total_len = alloc_len;
9539 ctsio->kern_data_resid = 0;
9540 ctsio->kern_rel_offset = 0;
9541 ctsio->kern_sg_entries = 0;
9544 * The control device is always connected. The disk device, on the
9545 * other hand, may not be online all the time. Need to change this
9546 * to figure out whether the disk device is actually online or not.
9549 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9550 lun->be_lun->lun_type;
9552 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9554 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9555 /* Supported VPD pages */
9556 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9558 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9559 /* Device Identification */
9560 pages->page_list[2] = SVPD_DEVICE_ID;
9562 pages->page_list[3] = SVPD_BLOCK_LIMITS;
9563 /* Logical Block Provisioning */
9564 pages->page_list[4] = SVPD_LBP;
9566 ctsio->scsi_status = SCSI_STATUS_OK;
9568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9569 ctsio->be_move_done = ctl_config_move_done;
9570 ctl_datamove((union ctl_io *)ctsio);
9572 return (CTL_RETVAL_COMPLETE);
9576 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9578 struct scsi_vpd_unit_serial_number *sn_ptr;
9579 struct ctl_lun *lun;
9580 #ifndef CTL_USE_BACKEND_SN
9584 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9586 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9587 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9588 ctsio->kern_sg_entries = 0;
9590 if (sizeof(*sn_ptr) < alloc_len) {
9591 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9592 ctsio->kern_data_len = sizeof(*sn_ptr);
9593 ctsio->kern_total_len = sizeof(*sn_ptr);
9595 ctsio->residual = 0;
9596 ctsio->kern_data_len = alloc_len;
9597 ctsio->kern_total_len = alloc_len;
9599 ctsio->kern_data_resid = 0;
9600 ctsio->kern_rel_offset = 0;
9601 ctsio->kern_sg_entries = 0;
9604 * The control device is always connected. The disk device, on the
9605 * other hand, may not be online all the time. Need to change this
9606 * to figure out whether the disk device is actually online or not.
9609 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9610 lun->be_lun->lun_type;
9612 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9614 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9615 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9616 #ifdef CTL_USE_BACKEND_SN
9618 * If we don't have a LUN, we just leave the serial number as
9621 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9623 strncpy((char *)sn_ptr->serial_num,
9624 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9628 * Note that we're using a non-unique serial number here,
9630 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9631 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9632 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9633 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9635 ctsio->scsi_status = SCSI_STATUS_OK;
9637 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9638 ctsio->be_move_done = ctl_config_move_done;
9639 ctl_datamove((union ctl_io *)ctsio);
9641 return (CTL_RETVAL_COMPLETE);
9646 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9648 struct scsi_vpd_device_id *devid_ptr;
9649 struct scsi_vpd_id_descriptor *desc, *desc1;
9650 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9651 struct scsi_vpd_id_t10 *t10id;
9652 struct ctl_softc *ctl_softc;
9653 struct ctl_lun *lun;
9654 struct ctl_frontend *fe;
9656 #ifndef CTL_USE_BACKEND_SN
9658 #endif /* CTL_USE_BACKEND_SN */
9661 ctl_softc = control_softc;
9663 mtx_lock(&ctl_softc->ctl_lock);
9664 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9665 mtx_unlock(&ctl_softc->ctl_lock);
9667 if (fe->devid != NULL)
9668 return ((fe->devid)(ctsio, alloc_len));
9670 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9672 devid_len = sizeof(struct scsi_vpd_device_id) +
9673 sizeof(struct scsi_vpd_id_descriptor) +
9674 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9675 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9676 sizeof(struct scsi_vpd_id_descriptor) +
9677 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9678 sizeof(struct scsi_vpd_id_descriptor) +
9679 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9681 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
9682 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9683 ctsio->kern_sg_entries = 0;
9685 if (devid_len < alloc_len) {
9686 ctsio->residual = alloc_len - devid_len;
9687 ctsio->kern_data_len = devid_len;
9688 ctsio->kern_total_len = devid_len;
9690 ctsio->residual = 0;
9691 ctsio->kern_data_len = alloc_len;
9692 ctsio->kern_total_len = alloc_len;
9694 ctsio->kern_data_resid = 0;
9695 ctsio->kern_rel_offset = 0;
9696 ctsio->kern_sg_entries = 0;
9698 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9699 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9700 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9701 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9702 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9704 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9705 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9708 * The control device is always connected. The disk device, on the
9709 * other hand, may not be online all the time.
9712 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9713 lun->be_lun->lun_type;
9715 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9717 devid_ptr->page_code = SVPD_DEVICE_ID;
9719 scsi_ulto2b(devid_len - 4, devid_ptr->length);
9721 mtx_lock(&ctl_softc->ctl_lock);
9724 * For Fibre channel,
9726 if (fe->port_type == CTL_PORT_FC)
9728 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9729 SVPD_ID_CODESET_ASCII;
9730 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9731 SVPD_ID_CODESET_BINARY;
9735 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9736 SVPD_ID_CODESET_ASCII;
9737 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9738 SVPD_ID_CODESET_BINARY;
9740 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9741 mtx_unlock(&ctl_softc->ctl_lock);
9744 * We're using a LUN association here. i.e., this device ID is a
9745 * per-LUN identifier.
9747 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9748 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9749 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
9750 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9752 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
9753 strncpy(t10id->vendor, val,
9754 min(sizeof(t10id->vendor), strlen(val)));
9758 * desc1 is for the WWPN which is a port asscociation.
9760 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9761 desc1->length = CTL_WWPN_LEN;
9762 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9763 /* For testing just create the WWPN */
9765 ddb_GetWWNN((char *)desc1->identifier);
9767 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9768 /* This is so Copancontrol will return something sane */
9769 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9770 ctsio->io_hdr.nexus.targ_port!=8)
9771 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9773 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9776 be64enc(desc1->identifier, fe->wwpn);
9779 * desc2 is for the Relative Target Port(type 4h) identifier
9781 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9782 | SVPD_ID_TYPE_RELTARG;
9785 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9786 /* This is so Copancontrol will return something sane */
9787 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9788 ctsio->io_hdr.nexus.targ_port!=8)
9789 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9791 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9795 * desc3 is for the Target Port Group(type 5h) identifier
9797 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9798 | SVPD_ID_TYPE_TPORTGRP;
9800 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9801 desc3->identifier[3] = 1;
9803 desc3->identifier[3] = 2;
9805 #ifdef CTL_USE_BACKEND_SN
9807 * If we've actually got a backend, copy the device id from the
9808 * per-LUN data. Otherwise, set it to all spaces.
9812 * Copy the backend's LUN ID.
9814 strncpy((char *)t10id->vendor_spec_id,
9815 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9818 * No backend, set this to spaces.
9820 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9823 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9824 (lun != NULL) ? (int)lun->lun : 0);
9825 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9829 ctsio->scsi_status = SCSI_STATUS_OK;
9831 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9832 ctsio->be_move_done = ctl_config_move_done;
9833 ctl_datamove((union ctl_io *)ctsio);
9835 return (CTL_RETVAL_COMPLETE);
9839 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9841 struct scsi_vpd_block_limits *bl_ptr;
9842 struct ctl_lun *lun;
9845 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9846 bs = lun->be_lun->blocksize;
9848 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9849 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9850 ctsio->kern_sg_entries = 0;
9852 if (sizeof(*bl_ptr) < alloc_len) {
9853 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9854 ctsio->kern_data_len = sizeof(*bl_ptr);
9855 ctsio->kern_total_len = sizeof(*bl_ptr);
9857 ctsio->residual = 0;
9858 ctsio->kern_data_len = alloc_len;
9859 ctsio->kern_total_len = alloc_len;
9861 ctsio->kern_data_resid = 0;
9862 ctsio->kern_rel_offset = 0;
9863 ctsio->kern_sg_entries = 0;
9866 * The control device is always connected. The disk device, on the
9867 * other hand, may not be online all the time. Need to change this
9868 * to figure out whether the disk device is actually online or not.
9871 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9872 lun->be_lun->lun_type;
9874 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9876 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9877 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9878 bl_ptr->max_cmp_write_len = 0xff;
9879 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9880 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9881 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9882 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9883 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9885 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9887 ctsio->scsi_status = SCSI_STATUS_OK;
9888 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9889 ctsio->be_move_done = ctl_config_move_done;
9890 ctl_datamove((union ctl_io *)ctsio);
9892 return (CTL_RETVAL_COMPLETE);
9896 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9898 struct scsi_vpd_logical_block_prov *lbp_ptr;
9899 struct ctl_lun *lun;
9902 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9903 bs = lun->be_lun->blocksize;
9905 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9906 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9907 ctsio->kern_sg_entries = 0;
9909 if (sizeof(*lbp_ptr) < alloc_len) {
9910 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9911 ctsio->kern_data_len = sizeof(*lbp_ptr);
9912 ctsio->kern_total_len = sizeof(*lbp_ptr);
9914 ctsio->residual = 0;
9915 ctsio->kern_data_len = alloc_len;
9916 ctsio->kern_total_len = alloc_len;
9918 ctsio->kern_data_resid = 0;
9919 ctsio->kern_rel_offset = 0;
9920 ctsio->kern_sg_entries = 0;
9923 * The control device is always connected. The disk device, on the
9924 * other hand, may not be online all the time. Need to change this
9925 * to figure out whether the disk device is actually online or not.
9928 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9929 lun->be_lun->lun_type;
9931 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9933 lbp_ptr->page_code = SVPD_LBP;
9934 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9935 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9937 ctsio->scsi_status = SCSI_STATUS_OK;
9938 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9939 ctsio->be_move_done = ctl_config_move_done;
9940 ctl_datamove((union ctl_io *)ctsio);
9942 return (CTL_RETVAL_COMPLETE);
9946 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9948 struct scsi_inquiry *cdb;
9949 struct ctl_lun *lun;
9950 int alloc_len, retval;
9952 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9953 cdb = (struct scsi_inquiry *)ctsio->cdb;
9955 retval = CTL_RETVAL_COMPLETE;
9957 alloc_len = scsi_2btoul(cdb->length);
9959 switch (cdb->page_code) {
9960 case SVPD_SUPPORTED_PAGES:
9961 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9963 case SVPD_UNIT_SERIAL_NUMBER:
9964 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9966 case SVPD_DEVICE_ID:
9967 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9969 case SVPD_BLOCK_LIMITS:
9970 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9973 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9976 ctl_set_invalid_field(ctsio,
9982 ctl_done((union ctl_io *)ctsio);
9983 retval = CTL_RETVAL_COMPLETE;
9991 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9993 struct scsi_inquiry_data *inq_ptr;
9994 struct scsi_inquiry *cdb;
9995 struct ctl_softc *ctl_softc;
9996 struct ctl_lun *lun;
10001 ctl_softc = control_softc;
10004 * Figure out whether we're talking to a Fibre Channel port or not.
10005 * We treat the ioctl front end, and any SCSI adapters, as packetized
10008 mtx_lock(&ctl_softc->ctl_lock);
10009 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
10014 mtx_unlock(&ctl_softc->ctl_lock);
10016 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10017 cdb = (struct scsi_inquiry *)ctsio->cdb;
10018 alloc_len = scsi_2btoul(cdb->length);
10021 * We malloc the full inquiry data size here and fill it
10022 * in. If the user only asks for less, we'll give him
10025 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
10026 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10027 ctsio->kern_sg_entries = 0;
10028 ctsio->kern_data_resid = 0;
10029 ctsio->kern_rel_offset = 0;
10031 if (sizeof(*inq_ptr) < alloc_len) {
10032 ctsio->residual = alloc_len - sizeof(*inq_ptr);
10033 ctsio->kern_data_len = sizeof(*inq_ptr);
10034 ctsio->kern_total_len = sizeof(*inq_ptr);
10036 ctsio->residual = 0;
10037 ctsio->kern_data_len = alloc_len;
10038 ctsio->kern_total_len = alloc_len;
10042 * If we have a LUN configured, report it as connected. Otherwise,
10043 * report that it is offline or no device is supported, depending
10044 * on the value of inquiry_pq_no_lun.
10046 * According to the spec (SPC-4 r34), the peripheral qualifier
10047 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10049 * "A peripheral device having the specified peripheral device type
10050 * is not connected to this logical unit. However, the device
10051 * server is capable of supporting the specified peripheral device
10052 * type on this logical unit."
10054 * According to the same spec, the peripheral qualifier
10055 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10057 * "The device server is not capable of supporting a peripheral
10058 * device on this logical unit. For this peripheral qualifier the
10059 * peripheral device type shall be set to 1Fh. All other peripheral
10060 * device type values are reserved for this peripheral qualifier."
10062 * Given the text, it would seem that we probably want to report that
10063 * the LUN is offline here. There is no LUN connected, but we can
10064 * support a LUN at the given LUN number.
10066 * In the real world, though, it sounds like things are a little
10069 * - Linux, when presented with a LUN with the offline peripheral
10070 * qualifier, will create an sg driver instance for it. So when
10071 * you attach it to CTL, you wind up with a ton of sg driver
10072 * instances. (One for every LUN that Linux bothered to probe.)
10073 * Linux does this despite the fact that it issues a REPORT LUNs
10074 * to LUN 0 to get the inventory of supported LUNs.
10076 * - There is other anecdotal evidence (from Emulex folks) about
10077 * arrays that use the offline peripheral qualifier for LUNs that
10078 * are on the "passive" path in an active/passive array.
10080 * So the solution is provide a hopefully reasonable default
10081 * (return bad/no LUN) and allow the user to change the behavior
10082 * with a tunable/sysctl variable.
10085 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10086 lun->be_lun->lun_type;
10087 else if (ctl_softc->inquiry_pq_no_lun == 0)
10088 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10090 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10092 /* RMB in byte 2 is 0 */
10093 inq_ptr->version = SCSI_REV_SPC3;
10096 * According to SAM-3, even if a device only supports a single
10097 * level of LUN addressing, it should still set the HISUP bit:
10099 * 4.9.1 Logical unit numbers overview
10101 * All logical unit number formats described in this standard are
10102 * hierarchical in structure even when only a single level in that
10103 * hierarchy is used. The HISUP bit shall be set to one in the
10104 * standard INQUIRY data (see SPC-2) when any logical unit number
10105 * format described in this standard is used. Non-hierarchical
10106 * formats are outside the scope of this standard.
10108 * Therefore we set the HiSup bit here.
10110 * The reponse format is 2, per SPC-3.
10112 inq_ptr->response_format = SID_HiSup | 2;
10114 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
10115 CTL_DEBUG_PRINT(("additional_length = %d\n",
10116 inq_ptr->additional_length));
10118 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
10119 /* 16 bit addressing */
10121 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10122 /* XXX set the SID_MultiP bit here if we're actually going to
10123 respond on multiple ports */
10124 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10126 /* 16 bit data bus, synchronous transfers */
10127 /* XXX these flags don't apply for FC */
10129 inq_ptr->flags = SID_WBus16 | SID_Sync;
10131 * XXX KDM do we want to support tagged queueing on the control
10135 || (lun->be_lun->lun_type != T_PROCESSOR))
10136 inq_ptr->flags |= SID_CmdQue;
10138 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10139 * We have 8 bytes for the vendor name, and 16 bytes for the device
10140 * name and 4 bytes for the revision.
10142 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
10143 strcpy(inq_ptr->vendor, CTL_VENDOR);
10145 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10146 strncpy(inq_ptr->vendor, val,
10147 min(sizeof(inq_ptr->vendor), strlen(val)));
10150 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10151 } else if ((val = ctl_get_opt(lun->be_lun, "product")) == NULL) {
10152 switch (lun->be_lun->lun_type) {
10154 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10157 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
10160 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
10164 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10165 strncpy(inq_ptr->product, val,
10166 min(sizeof(inq_ptr->product), strlen(val)));
10170 * XXX make this a macro somewhere so it automatically gets
10171 * incremented when we make changes.
10173 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "revision")) == NULL) {
10174 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10176 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10177 strncpy(inq_ptr->revision, val,
10178 min(sizeof(inq_ptr->revision), strlen(val)));
10182 * For parallel SCSI, we support double transition and single
10183 * transition clocking. We also support QAS (Quick Arbitration
10184 * and Selection) and Information Unit transfers on both the
10185 * control and array devices.
10188 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10192 scsi_ulto2b(0x0060, inq_ptr->version1);
10193 /* SPC-3 (no version claimed) XXX should we claim a version? */
10194 scsi_ulto2b(0x0300, inq_ptr->version2);
10196 /* FCP-2 ANSI INCITS.350:2003 */
10197 scsi_ulto2b(0x0917, inq_ptr->version3);
10199 /* SPI-4 ANSI INCITS.362:200x */
10200 scsi_ulto2b(0x0B56, inq_ptr->version3);
10204 /* SBC-2 (no version claimed) XXX should we claim a version? */
10205 scsi_ulto2b(0x0320, inq_ptr->version4);
10207 switch (lun->be_lun->lun_type) {
10210 * SBC-2 (no version claimed) XXX should we claim a
10213 scsi_ulto2b(0x0320, inq_ptr->version4);
10221 ctsio->scsi_status = SCSI_STATUS_OK;
10222 if (ctsio->kern_data_len > 0) {
10223 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10224 ctsio->be_move_done = ctl_config_move_done;
10225 ctl_datamove((union ctl_io *)ctsio);
10227 ctsio->io_hdr.status = CTL_SUCCESS;
10228 ctl_done((union ctl_io *)ctsio);
10231 return (CTL_RETVAL_COMPLETE);
10235 ctl_inquiry(struct ctl_scsiio *ctsio)
10237 struct scsi_inquiry *cdb;
10240 cdb = (struct scsi_inquiry *)ctsio->cdb;
10244 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10247 * Right now, we don't support the CmdDt inquiry information.
10248 * This would be nice to support in the future. When we do
10249 * support it, we should change this test so that it checks to make
10250 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10253 if (((cdb->byte2 & SI_EVPD)
10254 && (cdb->byte2 & SI_CMDDT)))
10256 if (cdb->byte2 & SI_CMDDT) {
10258 * Point to the SI_CMDDT bit. We might change this
10259 * when we support SI_CMDDT, but since both bits would be
10260 * "wrong", this should probably just stay as-is then.
10262 ctl_set_invalid_field(ctsio,
10268 ctl_done((union ctl_io *)ctsio);
10269 return (CTL_RETVAL_COMPLETE);
10271 if (cdb->byte2 & SI_EVPD)
10272 retval = ctl_inquiry_evpd(ctsio);
10274 else if (cdb->byte2 & SI_CMDDT)
10275 retval = ctl_inquiry_cmddt(ctsio);
10278 retval = ctl_inquiry_std(ctsio);
10284 * For known CDB types, parse the LBA and length.
10287 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10289 if (io->io_hdr.io_type != CTL_IO_SCSI)
10292 switch (io->scsiio.cdb[0]) {
10293 case COMPARE_AND_WRITE: {
10294 struct scsi_compare_and_write *cdb;
10296 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10298 *lba = scsi_8btou64(cdb->addr);
10299 *len = cdb->length;
10304 struct scsi_rw_6 *cdb;
10306 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10308 *lba = scsi_3btoul(cdb->addr);
10309 /* only 5 bits are valid in the most significant address byte */
10311 *len = cdb->length;
10316 struct scsi_rw_10 *cdb;
10318 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10320 *lba = scsi_4btoul(cdb->addr);
10321 *len = scsi_2btoul(cdb->length);
10324 case WRITE_VERIFY_10: {
10325 struct scsi_write_verify_10 *cdb;
10327 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10329 *lba = scsi_4btoul(cdb->addr);
10330 *len = scsi_2btoul(cdb->length);
10335 struct scsi_rw_12 *cdb;
10337 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10339 *lba = scsi_4btoul(cdb->addr);
10340 *len = scsi_4btoul(cdb->length);
10343 case WRITE_VERIFY_12: {
10344 struct scsi_write_verify_12 *cdb;
10346 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10348 *lba = scsi_4btoul(cdb->addr);
10349 *len = scsi_4btoul(cdb->length);
10354 struct scsi_rw_16 *cdb;
10356 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10358 *lba = scsi_8btou64(cdb->addr);
10359 *len = scsi_4btoul(cdb->length);
10362 case WRITE_VERIFY_16: {
10363 struct scsi_write_verify_16 *cdb;
10365 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10368 *lba = scsi_8btou64(cdb->addr);
10369 *len = scsi_4btoul(cdb->length);
10372 case WRITE_SAME_10: {
10373 struct scsi_write_same_10 *cdb;
10375 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10377 *lba = scsi_4btoul(cdb->addr);
10378 *len = scsi_2btoul(cdb->length);
10381 case WRITE_SAME_16: {
10382 struct scsi_write_same_16 *cdb;
10384 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10386 *lba = scsi_8btou64(cdb->addr);
10387 *len = scsi_4btoul(cdb->length);
10391 struct scsi_verify_10 *cdb;
10393 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10395 *lba = scsi_4btoul(cdb->addr);
10396 *len = scsi_2btoul(cdb->length);
10400 struct scsi_verify_12 *cdb;
10402 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10404 *lba = scsi_4btoul(cdb->addr);
10405 *len = scsi_4btoul(cdb->length);
10409 struct scsi_verify_16 *cdb;
10411 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10413 *lba = scsi_8btou64(cdb->addr);
10414 *len = scsi_4btoul(cdb->length);
10419 break; /* NOTREACHED */
10426 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10428 uint64_t endlba1, endlba2;
10430 endlba1 = lba1 + len1 - 1;
10431 endlba2 = lba2 + len2 - 1;
10433 if ((endlba1 < lba2)
10434 || (endlba2 < lba1))
10435 return (CTL_ACTION_PASS);
10437 return (CTL_ACTION_BLOCK);
10441 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10443 uint64_t lba1, lba2;
10444 uint32_t len1, len2;
10447 retval = ctl_get_lba_len(io1, &lba1, &len1);
10449 return (CTL_ACTION_ERROR);
10451 retval = ctl_get_lba_len(io2, &lba2, &len2);
10453 return (CTL_ACTION_ERROR);
10455 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10459 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10461 struct ctl_cmd_entry *pending_entry, *ooa_entry;
10462 ctl_serialize_action *serialize_row;
10465 * The initiator attempted multiple untagged commands at the same
10466 * time. Can't do that.
10468 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10469 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10470 && ((pending_io->io_hdr.nexus.targ_port ==
10471 ooa_io->io_hdr.nexus.targ_port)
10472 && (pending_io->io_hdr.nexus.initid.id ==
10473 ooa_io->io_hdr.nexus.initid.id))
10474 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10475 return (CTL_ACTION_OVERLAP);
10478 * The initiator attempted to send multiple tagged commands with
10479 * the same ID. (It's fine if different initiators have the same
10482 * Even if all of those conditions are true, we don't kill the I/O
10483 * if the command ahead of us has been aborted. We won't end up
10484 * sending it to the FETD, and it's perfectly legal to resend a
10485 * command with the same tag number as long as the previous
10486 * instance of this tag number has been aborted somehow.
10488 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10489 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10490 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10491 && ((pending_io->io_hdr.nexus.targ_port ==
10492 ooa_io->io_hdr.nexus.targ_port)
10493 && (pending_io->io_hdr.nexus.initid.id ==
10494 ooa_io->io_hdr.nexus.initid.id))
10495 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10496 return (CTL_ACTION_OVERLAP_TAG);
10499 * If we get a head of queue tag, SAM-3 says that we should
10500 * immediately execute it.
10502 * What happens if this command would normally block for some other
10503 * reason? e.g. a request sense with a head of queue tag
10504 * immediately after a write. Normally that would block, but this
10505 * will result in its getting executed immediately...
10507 * We currently return "pass" instead of "skip", so we'll end up
10508 * going through the rest of the queue to check for overlapped tags.
10510 * XXX KDM check for other types of blockage first??
10512 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10513 return (CTL_ACTION_PASS);
10516 * Ordered tags have to block until all items ahead of them
10517 * have completed. If we get called with an ordered tag, we always
10518 * block, if something else is ahead of us in the queue.
10520 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10521 return (CTL_ACTION_BLOCK);
10524 * Simple tags get blocked until all head of queue and ordered tags
10525 * ahead of them have completed. I'm lumping untagged commands in
10526 * with simple tags here. XXX KDM is that the right thing to do?
10528 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10529 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10530 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10531 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10532 return (CTL_ACTION_BLOCK);
10534 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
10535 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
10537 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10539 switch (serialize_row[pending_entry->seridx]) {
10540 case CTL_SER_BLOCK:
10541 return (CTL_ACTION_BLOCK);
10542 break; /* NOTREACHED */
10543 case CTL_SER_EXTENT:
10544 return (ctl_extent_check(pending_io, ooa_io));
10545 break; /* NOTREACHED */
10547 return (CTL_ACTION_PASS);
10548 break; /* NOTREACHED */
10550 return (CTL_ACTION_SKIP);
10553 panic("invalid serialization value %d",
10554 serialize_row[pending_entry->seridx]);
10555 break; /* NOTREACHED */
10558 return (CTL_ACTION_ERROR);
10562 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10564 * - pending_io is generally either incoming, or on the blocked queue
10565 * - starting I/O is the I/O we want to start the check with.
10568 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10569 union ctl_io *starting_io)
10571 union ctl_io *ooa_io;
10574 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
10577 * Run back along the OOA queue, starting with the current
10578 * blocked I/O and going through every I/O before it on the
10579 * queue. If starting_io is NULL, we'll just end up returning
10582 for (ooa_io = starting_io; ooa_io != NULL;
10583 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10587 * This routine just checks to see whether
10588 * cur_blocked is blocked by ooa_io, which is ahead
10589 * of it in the queue. It doesn't queue/dequeue
10592 action = ctl_check_for_blockage(pending_io, ooa_io);
10594 case CTL_ACTION_BLOCK:
10595 case CTL_ACTION_OVERLAP:
10596 case CTL_ACTION_OVERLAP_TAG:
10597 case CTL_ACTION_SKIP:
10598 case CTL_ACTION_ERROR:
10600 break; /* NOTREACHED */
10601 case CTL_ACTION_PASS:
10604 panic("invalid action %d", action);
10605 break; /* NOTREACHED */
10609 return (CTL_ACTION_PASS);
10614 * - An I/O has just completed, and has been removed from the per-LUN OOA
10615 * queue, so some items on the blocked queue may now be unblocked.
10618 ctl_check_blocked(struct ctl_lun *lun)
10620 union ctl_io *cur_blocked, *next_blocked;
10622 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
10625 * Run forward from the head of the blocked queue, checking each
10626 * entry against the I/Os prior to it on the OOA queue to see if
10627 * there is still any blockage.
10629 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10630 * with our removing a variable on it while it is traversing the
10633 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10634 cur_blocked != NULL; cur_blocked = next_blocked) {
10635 union ctl_io *prev_ooa;
10638 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10641 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10642 ctl_ooaq, ooa_links);
10645 * If cur_blocked happens to be the first item in the OOA
10646 * queue now, prev_ooa will be NULL, and the action
10647 * returned will just be CTL_ACTION_PASS.
10649 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10652 case CTL_ACTION_BLOCK:
10653 /* Nothing to do here, still blocked */
10655 case CTL_ACTION_OVERLAP:
10656 case CTL_ACTION_OVERLAP_TAG:
10658 * This shouldn't happen! In theory we've already
10659 * checked this command for overlap...
10662 case CTL_ACTION_PASS:
10663 case CTL_ACTION_SKIP: {
10664 struct ctl_softc *softc;
10665 struct ctl_cmd_entry *entry;
10671 * The skip case shouldn't happen, this transaction
10672 * should have never made it onto the blocked queue.
10675 * This I/O is no longer blocked, we can remove it
10676 * from the blocked queue. Since this is a TAILQ
10677 * (doubly linked list), we can do O(1) removals
10678 * from any place on the list.
10680 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10682 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10684 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10686 * Need to send IO back to original side to
10689 union ctl_ha_msg msg_info;
10691 msg_info.hdr.original_sc =
10692 cur_blocked->io_hdr.original_sc;
10693 msg_info.hdr.serializing_sc = cur_blocked;
10694 msg_info.hdr.msg_type = CTL_MSG_R2R;
10695 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10696 &msg_info, sizeof(msg_info), 0)) >
10697 CTL_HA_STATUS_SUCCESS) {
10698 printf("CTL:Check Blocked error from "
10699 "ctl_ha_msg_send %d\n",
10704 opcode = cur_blocked->scsiio.cdb[0];
10705 entry = &ctl_cmd_table[opcode];
10706 softc = control_softc;
10708 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10711 * Check this I/O for LUN state changes that may
10712 * have happened while this command was blocked.
10713 * The LUN state may have been changed by a command
10714 * ahead of us in the queue, so we need to re-check
10715 * for any states that can be caused by SCSI
10718 if (ctl_scsiio_lun_check(softc, lun, entry,
10719 &cur_blocked->scsiio) == 0) {
10720 cur_blocked->io_hdr.flags |=
10721 CTL_FLAG_IS_WAS_ON_RTR;
10722 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
10723 &cur_blocked->io_hdr, links);
10725 * In the non CTL_DONE_THREAD case, we need
10726 * to wake up the work thread here. When
10727 * we're processing completed requests from
10728 * the work thread context, we'll pop back
10729 * around and end up pulling things off the
10730 * RtR queue. When we aren't processing
10731 * things from the work thread context,
10732 * though, we won't ever check the RtR queue.
10733 * So we need to wake up the thread to clear
10734 * things off the queue. Otherwise this
10735 * transaction will just sit on the RtR queue
10736 * until a new I/O comes in. (Which may or
10737 * may not happen...)
10739 #ifndef CTL_DONE_THREAD
10740 ctl_wakeup_thread();
10743 ctl_done_lock(cur_blocked, /*have_lock*/ 1);
10748 * This probably shouldn't happen -- we shouldn't
10749 * get CTL_ACTION_ERROR, or anything else.
10755 return (CTL_RETVAL_COMPLETE);
10759 * This routine (with one exception) checks LUN flags that can be set by
10760 * commands ahead of us in the OOA queue. These flags have to be checked
10761 * when a command initially comes in, and when we pull a command off the
10762 * blocked queue and are preparing to execute it. The reason we have to
10763 * check these flags for commands on the blocked queue is that the LUN
10764 * state may have been changed by a command ahead of us while we're on the
10767 * Ordering is somewhat important with these checks, so please pay
10768 * careful attention to the placement of any new checks.
10771 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10772 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10779 * If this shelf is a secondary shelf controller, we have to reject
10780 * any media access commands.
10783 /* No longer needed for HA */
10784 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10785 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10786 ctl_set_lun_standby(ctsio);
10793 * Check for a reservation conflict. If this command isn't allowed
10794 * even on reserved LUNs, and if this initiator isn't the one who
10795 * reserved us, reject the command with a reservation conflict.
10797 if ((lun->flags & CTL_LUN_RESERVED)
10798 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10799 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10800 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10801 || (ctsio->io_hdr.nexus.targ_target.id !=
10802 lun->rsv_nexus.targ_target.id)) {
10803 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10804 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10810 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10811 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10814 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10816 * if we aren't registered or it's a res holder type
10817 * reservation and this isn't the res holder then set a
10819 * NOTE: Commands which might be allowed on write exclusive
10820 * type reservations are checked in the particular command
10821 * for a conflict. Read and SSU are the only ones.
10823 if (!lun->per_res[residx].registered
10824 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10825 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10826 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10833 if ((lun->flags & CTL_LUN_OFFLINE)
10834 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10835 ctl_set_lun_not_ready(ctsio);
10841 * If the LUN is stopped, see if this particular command is allowed
10842 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10844 if ((lun->flags & CTL_LUN_STOPPED)
10845 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10846 /* "Logical unit not ready, initializing cmd. required" */
10847 ctl_set_lun_stopped(ctsio);
10852 if ((lun->flags & CTL_LUN_INOPERABLE)
10853 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10854 /* "Medium format corrupted" */
10855 ctl_set_medium_format_corrupted(ctsio);
10866 ctl_failover_io(union ctl_io *io, int have_lock)
10868 ctl_set_busy(&io->scsiio);
10869 ctl_done_lock(io, have_lock);
10875 struct ctl_lun *lun;
10876 struct ctl_softc *ctl_softc;
10877 union ctl_io *next_io, *pending_io;
10882 ctl_softc = control_softc;
10884 mtx_lock(&ctl_softc->ctl_lock);
10886 * Remove any cmds from the other SC from the rtr queue. These
10887 * will obviously only be for LUNs for which we're the primary.
10888 * We can't send status or get/send data for these commands.
10889 * Since they haven't been executed yet, we can just remove them.
10890 * We'll either abort them or delete them below, depending on
10891 * which HA mode we're in.
10893 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10894 io != NULL; io = next_io) {
10895 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10896 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10897 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10898 ctl_io_hdr, links);
10901 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10902 lun = ctl_softc->ctl_luns[lun_idx];
10907 * Processor LUNs are primary on both sides.
10908 * XXX will this always be true?
10910 if (lun->be_lun->lun_type == T_PROCESSOR)
10913 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10914 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10915 printf("FAILOVER: primary lun %d\n", lun_idx);
10917 * Remove all commands from the other SC. First from the
10918 * blocked queue then from the ooa queue. Once we have
10919 * removed them. Call ctl_check_blocked to see if there
10920 * is anything that can run.
10922 for (io = (union ctl_io *)TAILQ_FIRST(
10923 &lun->blocked_queue); io != NULL; io = next_io) {
10925 next_io = (union ctl_io *)TAILQ_NEXT(
10926 &io->io_hdr, blocked_links);
10928 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10929 TAILQ_REMOVE(&lun->blocked_queue,
10930 &io->io_hdr,blocked_links);
10931 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10932 TAILQ_REMOVE(&lun->ooa_queue,
10933 &io->io_hdr, ooa_links);
10939 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10940 io != NULL; io = next_io) {
10942 next_io = (union ctl_io *)TAILQ_NEXT(
10943 &io->io_hdr, ooa_links);
10945 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10947 TAILQ_REMOVE(&lun->ooa_queue,
10954 ctl_check_blocked(lun);
10955 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10956 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10958 printf("FAILOVER: primary lun %d\n", lun_idx);
10960 * Abort all commands from the other SC. We can't
10961 * send status back for them now. These should get
10962 * cleaned up when they are completed or come out
10963 * for a datamove operation.
10965 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10966 io != NULL; io = next_io) {
10967 next_io = (union ctl_io *)TAILQ_NEXT(
10968 &io->io_hdr, ooa_links);
10970 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10971 io->io_hdr.flags |= CTL_FLAG_ABORT;
10973 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10974 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10976 printf("FAILOVER: secondary lun %d\n", lun_idx);
10978 lun->flags |= CTL_LUN_PRIMARY_SC;
10981 * We send all I/O that was sent to this controller
10982 * and redirected to the other side back with
10983 * busy status, and have the initiator retry it.
10984 * Figuring out how much data has been transferred,
10985 * etc. and picking up where we left off would be
10988 * XXX KDM need to remove I/O from the blocked
10991 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10992 &lun->ooa_queue); pending_io != NULL;
10993 pending_io = next_io) {
10995 next_io = (union ctl_io *)TAILQ_NEXT(
10996 &pending_io->io_hdr, ooa_links);
10998 pending_io->io_hdr.flags &=
10999 ~CTL_FLAG_SENT_2OTHER_SC;
11001 if (pending_io->io_hdr.flags &
11002 CTL_FLAG_IO_ACTIVE) {
11003 pending_io->io_hdr.flags |=
11006 ctl_set_busy(&pending_io->scsiio);
11007 ctl_done_lock(pending_io,
11013 * Build Unit Attention
11015 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11016 lun->pending_sense[i].ua_pending |=
11017 CTL_UA_ASYM_ACC_CHANGE;
11019 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11020 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11021 printf("FAILOVER: secondary lun %d\n", lun_idx);
11023 * if the first io on the OOA is not on the RtR queue
11026 lun->flags |= CTL_LUN_PRIMARY_SC;
11028 pending_io = (union ctl_io *)TAILQ_FIRST(
11030 if (pending_io==NULL) {
11031 printf("Nothing on OOA queue\n");
11035 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11036 if ((pending_io->io_hdr.flags &
11037 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11038 pending_io->io_hdr.flags |=
11039 CTL_FLAG_IS_WAS_ON_RTR;
11040 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11041 &pending_io->io_hdr, links);
11046 printf("Tag 0x%04x is running\n",
11047 pending_io->scsiio.tag_num);
11051 next_io = (union ctl_io *)TAILQ_NEXT(
11052 &pending_io->io_hdr, ooa_links);
11053 for (pending_io=next_io; pending_io != NULL;
11054 pending_io = next_io) {
11055 pending_io->io_hdr.flags &=
11056 ~CTL_FLAG_SENT_2OTHER_SC;
11057 next_io = (union ctl_io *)TAILQ_NEXT(
11058 &pending_io->io_hdr, ooa_links);
11059 if (pending_io->io_hdr.flags &
11060 CTL_FLAG_IS_WAS_ON_RTR) {
11062 printf("Tag 0x%04x is running\n",
11063 pending_io->scsiio.tag_num);
11068 switch (ctl_check_ooa(lun, pending_io,
11069 (union ctl_io *)TAILQ_PREV(
11070 &pending_io->io_hdr, ctl_ooaq,
11073 case CTL_ACTION_BLOCK:
11074 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11075 &pending_io->io_hdr,
11077 pending_io->io_hdr.flags |=
11080 case CTL_ACTION_PASS:
11081 case CTL_ACTION_SKIP:
11082 pending_io->io_hdr.flags |=
11083 CTL_FLAG_IS_WAS_ON_RTR;
11084 STAILQ_INSERT_TAIL(
11085 &ctl_softc->rtr_queue,
11086 &pending_io->io_hdr, links);
11088 case CTL_ACTION_OVERLAP:
11089 ctl_set_overlapped_cmd(
11090 (struct ctl_scsiio *)pending_io);
11091 ctl_done_lock(pending_io,
11094 case CTL_ACTION_OVERLAP_TAG:
11095 ctl_set_overlapped_tag(
11096 (struct ctl_scsiio *)pending_io,
11097 pending_io->scsiio.tag_num & 0xff);
11098 ctl_done_lock(pending_io,
11101 case CTL_ACTION_ERROR:
11103 ctl_set_internal_failure(
11104 (struct ctl_scsiio *)pending_io,
11107 ctl_done_lock(pending_io,
11114 * Build Unit Attention
11116 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11117 lun->pending_sense[i].ua_pending |=
11118 CTL_UA_ASYM_ACC_CHANGE;
11121 panic("Unhandled HA mode failover, LUN flags = %#x, "
11122 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11126 mtx_unlock(&ctl_softc->ctl_lock);
11130 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11132 struct ctl_lun *lun;
11133 struct ctl_cmd_entry *entry;
11135 uint32_t initidx, targ_lun;
11142 opcode = ctsio->cdb[0];
11144 mtx_lock(&ctl_softc->ctl_lock);
11146 targ_lun = ctsio->io_hdr.nexus.targ_lun;
11147 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
11148 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
11149 if ((targ_lun < CTL_MAX_LUNS)
11150 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11151 lun = ctl_softc->ctl_luns[targ_lun];
11153 * If the LUN is invalid, pretend that it doesn't exist.
11154 * It will go away as soon as all pending I/O has been
11157 if (lun->flags & CTL_LUN_DISABLED) {
11160 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11161 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11163 if (lun->be_lun->lun_type == T_PROCESSOR) {
11164 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11168 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11169 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11172 entry = &ctl_cmd_table[opcode];
11174 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11175 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11178 * Check to see whether we can send this command to LUNs that don't
11179 * exist. This should pretty much only be the case for inquiry
11180 * and request sense. Further checks, below, really require having
11181 * a LUN, so we can't really check the command anymore. Just put
11182 * it on the rtr queue.
11185 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
11188 ctl_set_unsupported_lun(ctsio);
11189 mtx_unlock(&ctl_softc->ctl_lock);
11190 ctl_done((union ctl_io *)ctsio);
11191 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11195 * Every I/O goes into the OOA queue for a particular LUN, and
11196 * stays there until completion.
11198 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11201 * Make sure we support this particular command on this LUN.
11202 * e.g., we don't support writes to the control LUN.
11204 switch (lun->be_lun->lun_type) {
11206 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11207 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
11209 ctl_set_invalid_opcode(ctsio);
11210 mtx_unlock(&ctl_softc->ctl_lock);
11211 ctl_done((union ctl_io *)ctsio);
11216 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
11217 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
11219 ctl_set_invalid_opcode(ctsio);
11220 mtx_unlock(&ctl_softc->ctl_lock);
11221 ctl_done((union ctl_io *)ctsio);
11226 printf("Unsupported CTL LUN type %d\n",
11227 lun->be_lun->lun_type);
11228 panic("Unsupported CTL LUN type %d\n",
11229 lun->be_lun->lun_type);
11230 break; /* NOTREACHED */
11234 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11237 * If we've got a request sense, it'll clear the contingent
11238 * allegiance condition. Otherwise, if we have a CA condition for
11239 * this initiator, clear it, because it sent down a command other
11240 * than request sense.
11242 if ((opcode != REQUEST_SENSE)
11243 && (ctl_is_set(lun->have_ca, initidx)))
11244 ctl_clear_mask(lun->have_ca, initidx);
11247 * If the command has this flag set, it handles its own unit
11248 * attention reporting, we shouldn't do anything. Otherwise we
11249 * check for any pending unit attentions, and send them back to the
11250 * initiator. We only do this when a command initially comes in,
11251 * not when we pull it off the blocked queue.
11253 * According to SAM-3, section 5.3.2, the order that things get
11254 * presented back to the host is basically unit attentions caused
11255 * by some sort of reset event, busy status, reservation conflicts
11256 * or task set full, and finally any other status.
11258 * One issue here is that some of the unit attentions we report
11259 * don't fall into the "reset" category (e.g. "reported luns data
11260 * has changed"). So reporting it here, before the reservation
11261 * check, may be technically wrong. I guess the only thing to do
11262 * would be to check for and report the reset events here, and then
11263 * check for the other unit attention types after we check for a
11264 * reservation conflict.
11266 * XXX KDM need to fix this
11268 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11269 ctl_ua_type ua_type;
11271 ua_type = lun->pending_sense[initidx].ua_pending;
11272 if (ua_type != CTL_UA_NONE) {
11273 scsi_sense_data_type sense_format;
11276 sense_format = (lun->flags &
11277 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11280 sense_format = SSD_TYPE_FIXED;
11282 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11284 if (ua_type != CTL_UA_NONE) {
11285 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11286 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11288 ctsio->sense_len = SSD_FULL_SIZE;
11289 lun->pending_sense[initidx].ua_pending &=
11291 mtx_unlock(&ctl_softc->ctl_lock);
11292 ctl_done((union ctl_io *)ctsio);
11299 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11300 mtx_unlock(&ctl_softc->ctl_lock);
11301 ctl_done((union ctl_io *)ctsio);
11306 * XXX CHD this is where we want to send IO to other side if
11307 * this LUN is secondary on this SC. We will need to make a copy
11308 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11309 * the copy we send as FROM_OTHER.
11310 * We also need to stuff the address of the original IO so we can
11311 * find it easily. Something similar will need be done on the other
11312 * side so when we are done we can find the copy.
11314 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11315 union ctl_ha_msg msg_info;
11318 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11320 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11321 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11323 printf("1. ctsio %p\n", ctsio);
11325 msg_info.hdr.serializing_sc = NULL;
11326 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11327 msg_info.scsi.tag_num = ctsio->tag_num;
11328 msg_info.scsi.tag_type = ctsio->tag_type;
11329 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11331 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11333 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11334 (void *)&msg_info, sizeof(msg_info), 0)) >
11335 CTL_HA_STATUS_SUCCESS) {
11336 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11338 printf("CTL:opcode is %x\n",opcode);
11341 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11346 * XXX KDM this I/O is off the incoming queue, but hasn't
11347 * been inserted on any other queue. We may need to come
11348 * up with a holding queue while we wait for serialization
11349 * so that we have an idea of what we're waiting for from
11352 goto bailout_unlock;
11355 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11356 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11357 ctl_ooaq, ooa_links))) {
11358 case CTL_ACTION_BLOCK:
11359 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11360 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11362 goto bailout_unlock;
11363 break; /* NOTREACHED */
11364 case CTL_ACTION_PASS:
11365 case CTL_ACTION_SKIP:
11367 break; /* NOTREACHED */
11368 case CTL_ACTION_OVERLAP:
11369 ctl_set_overlapped_cmd(ctsio);
11370 mtx_unlock(&ctl_softc->ctl_lock);
11371 ctl_done((union ctl_io *)ctsio);
11373 break; /* NOTREACHED */
11374 case CTL_ACTION_OVERLAP_TAG:
11375 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11376 mtx_unlock(&ctl_softc->ctl_lock);
11377 ctl_done((union ctl_io *)ctsio);
11379 break; /* NOTREACHED */
11380 case CTL_ACTION_ERROR:
11382 ctl_set_internal_failure(ctsio,
11384 /*retry_count*/ 0);
11385 mtx_unlock(&ctl_softc->ctl_lock);
11386 ctl_done((union ctl_io *)ctsio);
11388 break; /* NOTREACHED */
11391 goto bailout_unlock;
11394 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11395 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
11398 mtx_unlock(&ctl_softc->ctl_lock);
11405 ctl_scsiio(struct ctl_scsiio *ctsio)
11408 struct ctl_cmd_entry *entry;
11410 retval = CTL_RETVAL_COMPLETE;
11412 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11414 entry = &ctl_cmd_table[ctsio->cdb[0]];
11417 * If this I/O has been aborted, just send it straight to
11418 * ctl_done() without executing it.
11420 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11421 ctl_done((union ctl_io *)ctsio);
11426 * All the checks should have been handled by ctl_scsiio_precheck().
11427 * We should be clear now to just execute the I/O.
11429 retval = entry->execute(ctsio);
11436 * Since we only implement one target right now, a bus reset simply resets
11437 * our single target.
11440 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11442 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11446 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11447 ctl_ua_type ua_type)
11449 struct ctl_lun *lun;
11452 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11453 union ctl_ha_msg msg_info;
11455 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11456 msg_info.hdr.nexus = io->io_hdr.nexus;
11457 if (ua_type==CTL_UA_TARG_RESET)
11458 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11460 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11461 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11462 msg_info.hdr.original_sc = NULL;
11463 msg_info.hdr.serializing_sc = NULL;
11464 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11465 (void *)&msg_info, sizeof(msg_info), 0)) {
11470 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11471 retval += ctl_lun_reset(lun, io, ua_type);
11477 * The LUN should always be set. The I/O is optional, and is used to
11478 * distinguish between I/Os sent by this initiator, and by other
11479 * initiators. We set unit attention for initiators other than this one.
11480 * SAM-3 is vague on this point. It does say that a unit attention should
11481 * be established for other initiators when a LUN is reset (see section
11482 * 5.7.3), but it doesn't specifically say that the unit attention should
11483 * be established for this particular initiator when a LUN is reset. Here
11484 * is the relevant text, from SAM-3 rev 8:
11486 * 5.7.2 When a SCSI initiator port aborts its own tasks
11488 * When a SCSI initiator port causes its own task(s) to be aborted, no
11489 * notification that the task(s) have been aborted shall be returned to
11490 * the SCSI initiator port other than the completion response for the
11491 * command or task management function action that caused the task(s) to
11492 * be aborted and notification(s) associated with related effects of the
11493 * action (e.g., a reset unit attention condition).
11495 * XXX KDM for now, we're setting unit attention for all initiators.
11498 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11502 uint32_t initindex;
11507 * Run through the OOA queue and abort each I/O.
11510 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11512 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11513 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11514 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11518 * This version sets unit attention for every
11521 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11522 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11523 if (initindex == i)
11525 lun->pending_sense[i].ua_pending |= ua_type;
11530 * A reset (any kind, really) clears reservations established with
11531 * RESERVE/RELEASE. It does not clear reservations established
11532 * with PERSISTENT RESERVE OUT, but we don't support that at the
11533 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11534 * reservations made with the RESERVE/RELEASE commands, because
11535 * those commands are obsolete in SPC-3.
11537 lun->flags &= ~CTL_LUN_RESERVED;
11539 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11540 ctl_clear_mask(lun->have_ca, i);
11541 lun->pending_sense[i].ua_pending |= ua_type;
11548 ctl_abort_task(union ctl_io *io)
11551 struct ctl_lun *lun;
11552 struct ctl_softc *ctl_softc;
11555 char printbuf[128];
11560 ctl_softc = control_softc;
11566 targ_lun = io->io_hdr.nexus.targ_lun;
11567 if (io->io_hdr.nexus.lun_map_fn != NULL)
11568 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11569 if ((targ_lun < CTL_MAX_LUNS)
11570 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11571 lun = ctl_softc->ctl_luns[targ_lun];
11576 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11577 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11581 * Run through the OOA queue and attempt to find the given I/O.
11582 * The target port, initiator ID, tag type and tag number have to
11583 * match the values that we got from the initiator. If we have an
11584 * untagged command to abort, simply abort the first untagged command
11585 * we come to. We only allow one untagged command at a time of course.
11588 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11590 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11591 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11593 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11595 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11596 lun->lun, xio->scsiio.tag_num,
11597 xio->scsiio.tag_type,
11598 (xio->io_hdr.blocked_links.tqe_prev
11599 == NULL) ? "" : " BLOCKED",
11600 (xio->io_hdr.flags &
11601 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11602 (xio->io_hdr.flags &
11603 CTL_FLAG_ABORT) ? " ABORT" : "",
11604 (xio->io_hdr.flags &
11605 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11606 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11608 printf("%s\n", sbuf_data(&sb));
11611 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11612 && (xio->io_hdr.nexus.initid.id ==
11613 io->io_hdr.nexus.initid.id)) {
11615 * If the abort says that the task is untagged, the
11616 * task in the queue must be untagged. Otherwise,
11617 * we just check to see whether the tag numbers
11618 * match. This is because the QLogic firmware
11619 * doesn't pass back the tag type in an abort
11623 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11624 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11625 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11628 * XXX KDM we've got problems with FC, because it
11629 * doesn't send down a tag type with aborts. So we
11630 * can only really go by the tag number...
11631 * This may cause problems with parallel SCSI.
11632 * Need to figure that out!!
11634 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11635 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11637 if ((io->io_hdr.flags &
11638 CTL_FLAG_FROM_OTHER_SC) == 0 &&
11639 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11640 union ctl_ha_msg msg_info;
11642 io->io_hdr.flags |=
11643 CTL_FLAG_SENT_2OTHER_SC;
11644 msg_info.hdr.nexus = io->io_hdr.nexus;
11645 msg_info.task.task_action =
11646 CTL_TASK_ABORT_TASK;
11647 msg_info.task.tag_num =
11648 io->taskio.tag_num;
11649 msg_info.task.tag_type =
11650 io->taskio.tag_type;
11651 msg_info.hdr.msg_type =
11652 CTL_MSG_MANAGE_TASKS;
11653 msg_info.hdr.original_sc = NULL;
11654 msg_info.hdr.serializing_sc = NULL;
11656 printf("Sent Abort to other side\n");
11658 if (CTL_HA_STATUS_SUCCESS !=
11659 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11661 sizeof(msg_info), 0)) {
11665 printf("ctl_abort_task: found I/O to abort\n");
11676 * This isn't really an error. It's entirely possible for
11677 * the abort and command completion to cross on the wire.
11678 * This is more of an informative/diagnostic error.
11681 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11682 "%d:%d:%d:%d tag %d type %d\n",
11683 io->io_hdr.nexus.initid.id,
11684 io->io_hdr.nexus.targ_port,
11685 io->io_hdr.nexus.targ_target.id,
11686 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11687 io->taskio.tag_type);
11695 * This routine cannot block! It must be callable from an interrupt
11696 * handler as well as from the work thread.
11699 ctl_run_task_queue(struct ctl_softc *ctl_softc)
11701 union ctl_io *io, *next_io;
11703 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
11705 CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
11707 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
11708 io != NULL; io = next_io) {
11710 const char *task_desc;
11712 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11716 switch (io->io_hdr.io_type) {
11717 case CTL_IO_TASK: {
11718 task_desc = ctl_scsi_task_string(&io->taskio);
11719 if (task_desc != NULL) {
11721 csevent_log(CSC_CTL | CSC_SHELF_SW |
11723 csevent_LogType_Trace,
11724 csevent_Severity_Information,
11725 csevent_AlertLevel_Green,
11726 csevent_FRU_Firmware,
11727 csevent_FRU_Unknown,
11728 "CTL: received task: %s",task_desc);
11732 csevent_log(CSC_CTL | CSC_SHELF_SW |
11734 csevent_LogType_Trace,
11735 csevent_Severity_Information,
11736 csevent_AlertLevel_Green,
11737 csevent_FRU_Firmware,
11738 csevent_FRU_Unknown,
11739 "CTL: received unknown task "
11741 io->taskio.task_action,
11742 io->taskio.task_action);
11745 switch (io->taskio.task_action) {
11746 case CTL_TASK_ABORT_TASK:
11747 retval = ctl_abort_task(io);
11749 case CTL_TASK_ABORT_TASK_SET:
11751 case CTL_TASK_CLEAR_ACA:
11753 case CTL_TASK_CLEAR_TASK_SET:
11755 case CTL_TASK_LUN_RESET: {
11756 struct ctl_lun *lun;
11760 targ_lun = io->io_hdr.nexus.targ_lun;
11761 if (io->io_hdr.nexus.lun_map_fn != NULL)
11762 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11764 if ((targ_lun < CTL_MAX_LUNS)
11765 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11766 lun = ctl_softc->ctl_luns[targ_lun];
11772 if (!(io->io_hdr.flags &
11773 CTL_FLAG_FROM_OTHER_SC)) {
11774 union ctl_ha_msg msg_info;
11776 io->io_hdr.flags |=
11777 CTL_FLAG_SENT_2OTHER_SC;
11778 msg_info.hdr.msg_type =
11779 CTL_MSG_MANAGE_TASKS;
11780 msg_info.hdr.nexus = io->io_hdr.nexus;
11781 msg_info.task.task_action =
11782 CTL_TASK_LUN_RESET;
11783 msg_info.hdr.original_sc = NULL;
11784 msg_info.hdr.serializing_sc = NULL;
11785 if (CTL_HA_STATUS_SUCCESS !=
11786 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11788 sizeof(msg_info), 0)) {
11792 retval = ctl_lun_reset(lun, io,
11796 case CTL_TASK_TARGET_RESET:
11797 retval = ctl_target_reset(ctl_softc, io,
11798 CTL_UA_TARG_RESET);
11800 case CTL_TASK_BUS_RESET:
11801 retval = ctl_bus_reset(ctl_softc, io);
11803 case CTL_TASK_PORT_LOGIN:
11805 case CTL_TASK_PORT_LOGOUT:
11808 printf("ctl_run_task_queue: got unknown task "
11809 "management event %d\n",
11810 io->taskio.task_action);
11814 io->io_hdr.status = CTL_SUCCESS;
11816 io->io_hdr.status = CTL_ERROR;
11818 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11819 ctl_io_hdr, links);
11821 * This will queue this I/O to the done queue, but the
11822 * work thread won't be able to process it until we
11823 * return and the lock is released.
11825 ctl_done_lock(io, /*have_lock*/ 1);
11830 printf("%s: invalid I/O type %d msg %d cdb %x"
11831 " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11832 __func__, io->io_hdr.io_type,
11833 io->io_hdr.msg_type, io->scsiio.cdb[0],
11834 (uintmax_t)io->io_hdr.nexus.initid.id,
11835 io->io_hdr.nexus.targ_port,
11836 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11837 io->io_hdr.nexus.targ_lun /* XXX */,
11838 (io->io_hdr.io_type == CTL_IO_TASK) ?
11839 io->taskio.tag_num : io->scsiio.tag_num);
11840 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11841 ctl_io_hdr, links);
11848 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11852 * For HA operation. Handle commands that come in from the other
11856 ctl_handle_isc(union ctl_io *io)
11859 struct ctl_lun *lun;
11860 struct ctl_softc *ctl_softc;
11863 ctl_softc = control_softc;
11865 targ_lun = io->io_hdr.nexus.targ_lun;
11866 if (io->io_hdr.nexus.lun_map_fn != NULL)
11867 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11868 lun = ctl_softc->ctl_luns[targ_lun];
11870 switch (io->io_hdr.msg_type) {
11871 case CTL_MSG_SERIALIZE:
11872 free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11875 case CTL_MSG_R2R: {
11877 struct ctl_cmd_entry *entry;
11880 * This is only used in SER_ONLY mode.
11883 opcode = io->scsiio.cdb[0];
11884 entry = &ctl_cmd_table[opcode];
11885 mtx_lock(&ctl_softc->ctl_lock);
11886 if (ctl_scsiio_lun_check(ctl_softc, lun,
11887 entry, (struct ctl_scsiio *)io) != 0) {
11888 ctl_done_lock(io, /*have_lock*/ 1);
11889 mtx_unlock(&ctl_softc->ctl_lock);
11892 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11893 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11894 &io->io_hdr, links);
11895 mtx_unlock(&ctl_softc->ctl_lock);
11898 case CTL_MSG_FINISH_IO:
11899 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11901 ctl_done_lock(io, /*have_lock*/ 0);
11904 mtx_lock(&ctl_softc->ctl_lock);
11905 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11907 STAILQ_REMOVE(&ctl_softc->task_queue,
11908 &io->io_hdr, ctl_io_hdr, links);
11909 ctl_check_blocked(lun);
11910 mtx_unlock(&ctl_softc->ctl_lock);
11913 case CTL_MSG_PERS_ACTION:
11914 ctl_hndl_per_res_out_on_other_sc(
11915 (union ctl_ha_msg *)&io->presio.pr_msg);
11918 case CTL_MSG_BAD_JUJU:
11920 ctl_done_lock(io, /*have_lock*/ 0);
11922 case CTL_MSG_DATAMOVE:
11923 /* Only used in XFER mode */
11925 ctl_datamove_remote(io);
11927 case CTL_MSG_DATAMOVE_DONE:
11928 /* Only used in XFER mode */
11930 io->scsiio.be_move_done(io);
11934 printf("%s: Invalid message type %d\n",
11935 __func__, io->io_hdr.msg_type);
11945 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11946 * there is no match.
11948 static ctl_lun_error_pattern
11949 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11951 struct ctl_cmd_entry *entry;
11952 ctl_lun_error_pattern filtered_pattern, pattern;
11955 pattern = desc->error_pattern;
11958 * XXX KDM we need more data passed into this function to match a
11959 * custom pattern, and we actually need to implement custom pattern
11962 if (pattern & CTL_LUN_PAT_CMD)
11963 return (CTL_LUN_PAT_CMD);
11965 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11966 return (CTL_LUN_PAT_ANY);
11968 opcode = ctsio->cdb[0];
11969 entry = &ctl_cmd_table[opcode];
11971 filtered_pattern = entry->pattern & pattern;
11974 * If the user requested specific flags in the pattern (e.g.
11975 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11978 * If the user did not specify any flags, it doesn't matter whether
11979 * or not the command supports the flags.
11981 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11982 (pattern & ~CTL_LUN_PAT_MASK))
11983 return (CTL_LUN_PAT_NONE);
11986 * If the user asked for a range check, see if the requested LBA
11987 * range overlaps with this command's LBA range.
11989 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11995 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11997 return (CTL_LUN_PAT_NONE);
11999 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12000 desc->lba_range.len);
12002 * A "pass" means that the LBA ranges don't overlap, so
12003 * this doesn't match the user's range criteria.
12005 if (action == CTL_ACTION_PASS)
12006 return (CTL_LUN_PAT_NONE);
12009 return (filtered_pattern);
12013 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12015 struct ctl_error_desc *desc, *desc2;
12017 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
12019 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12020 ctl_lun_error_pattern pattern;
12022 * Check to see whether this particular command matches
12023 * the pattern in the descriptor.
12025 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12026 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12029 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12030 case CTL_LUN_INJ_ABORTED:
12031 ctl_set_aborted(&io->scsiio);
12033 case CTL_LUN_INJ_MEDIUM_ERR:
12034 ctl_set_medium_error(&io->scsiio);
12036 case CTL_LUN_INJ_UA:
12037 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12039 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12041 case CTL_LUN_INJ_CUSTOM:
12043 * We're assuming the user knows what he is doing.
12044 * Just copy the sense information without doing
12047 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12048 ctl_min(sizeof(desc->custom_sense),
12049 sizeof(io->scsiio.sense_data)));
12050 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12051 io->scsiio.sense_len = SSD_FULL_SIZE;
12052 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12054 case CTL_LUN_INJ_NONE:
12057 * If this is an error injection type we don't know
12058 * about, clear the continuous flag (if it is set)
12059 * so it will get deleted below.
12061 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12065 * By default, each error injection action is a one-shot
12067 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12070 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12076 #ifdef CTL_IO_DELAY
12078 ctl_datamove_timer_wakeup(void *arg)
12082 io = (union ctl_io *)arg;
12086 #endif /* CTL_IO_DELAY */
12089 ctl_datamove(union ctl_io *io)
12091 void (*fe_datamove)(union ctl_io *io);
12093 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12095 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12098 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12103 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12104 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12106 sbuf_cat(&sb, path_str);
12107 switch (io->io_hdr.io_type) {
12109 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12110 sbuf_printf(&sb, "\n");
12111 sbuf_cat(&sb, path_str);
12112 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12113 io->scsiio.tag_num, io->scsiio.tag_type);
12116 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12117 "Tag Type: %d\n", io->taskio.task_action,
12118 io->taskio.tag_num, io->taskio.tag_type);
12121 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12122 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12125 sbuf_cat(&sb, path_str);
12126 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12127 (intmax_t)time_uptime - io->io_hdr.start_time);
12129 printf("%s", sbuf_data(&sb));
12131 #endif /* CTL_TIME_IO */
12133 mtx_lock(&control_softc->ctl_lock);
12134 #ifdef CTL_IO_DELAY
12135 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12136 struct ctl_lun *lun;
12138 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12140 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12142 struct ctl_lun *lun;
12144 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12146 && (lun->delay_info.datamove_delay > 0)) {
12147 struct callout *callout;
12149 callout = (struct callout *)&io->io_hdr.timer_bytes;
12150 callout_init(callout, /*mpsafe*/ 1);
12151 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12152 callout_reset(callout,
12153 lun->delay_info.datamove_delay * hz,
12154 ctl_datamove_timer_wakeup, io);
12155 if (lun->delay_info.datamove_type ==
12156 CTL_DELAY_TYPE_ONESHOT)
12157 lun->delay_info.datamove_delay = 0;
12158 mtx_unlock(&control_softc->ctl_lock);
12164 * If we have any pending task management commands, process them
12165 * first. This is necessary to eliminate a race condition with the
12168 * - FETD submits a task management command, like an abort.
12169 * - Back end calls fe_datamove() to move the data for the aborted
12170 * command. The FETD can't really accept it, but if it did, it
12171 * would end up transmitting data for a command that the initiator
12172 * told us to abort.
12174 * We close the race by processing all pending task management
12175 * commands here (we can't block!), and then check this I/O to see
12176 * if it has been aborted. If so, return it to the back end with
12177 * bad status, so the back end can say return an error to the back end
12178 * and then when the back end returns an error, we can return the
12179 * aborted command to the FETD, so it can clean up its resources.
12181 if (control_softc->flags & CTL_FLAG_TASK_PENDING)
12182 ctl_run_task_queue(control_softc);
12185 * This command has been aborted. Set the port status, so we fail
12188 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12189 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12190 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12191 io->io_hdr.nexus.targ_port,
12192 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12193 io->io_hdr.nexus.targ_lun);
12194 io->io_hdr.status = CTL_CMD_ABORTED;
12195 io->io_hdr.port_status = 31337;
12196 mtx_unlock(&control_softc->ctl_lock);
12198 * Note that the backend, in this case, will get the
12199 * callback in its context. In other cases it may get
12200 * called in the frontend's interrupt thread context.
12202 io->scsiio.be_move_done(io);
12207 * If we're in XFER mode and this I/O is from the other shelf
12208 * controller, we need to send the DMA to the other side to
12209 * actually transfer the data to/from the host. In serialize only
12210 * mode the transfer happens below CTL and ctl_datamove() is only
12211 * called on the machine that originally received the I/O.
12213 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12214 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12215 union ctl_ha_msg msg;
12216 uint32_t sg_entries_sent;
12220 memset(&msg, 0, sizeof(msg));
12221 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12222 msg.hdr.original_sc = io->io_hdr.original_sc;
12223 msg.hdr.serializing_sc = io;
12224 msg.hdr.nexus = io->io_hdr.nexus;
12225 msg.dt.flags = io->io_hdr.flags;
12227 * We convert everything into a S/G list here. We can't
12228 * pass by reference, only by value between controllers.
12229 * So we can't pass a pointer to the S/G list, only as many
12230 * S/G entries as we can fit in here. If it's possible for
12231 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12232 * then we need to break this up into multiple transfers.
12234 if (io->scsiio.kern_sg_entries == 0) {
12235 msg.dt.kern_sg_entries = 1;
12237 * If this is in cached memory, flush the cache
12238 * before we send the DMA request to the other
12239 * controller. We want to do this in either the
12240 * read or the write case. The read case is
12241 * straightforward. In the write case, we want to
12242 * make sure nothing is in the local cache that
12243 * could overwrite the DMAed data.
12245 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12247 * XXX KDM use bus_dmamap_sync() here.
12252 * Convert to a physical address if this is a
12255 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12256 msg.dt.sg_list[0].addr =
12257 io->scsiio.kern_data_ptr;
12260 * XXX KDM use busdma here!
12263 msg.dt.sg_list[0].addr = (void *)
12264 vtophys(io->scsiio.kern_data_ptr);
12268 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12271 struct ctl_sg_entry *sgl;
12274 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12275 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12276 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12278 * XXX KDM use bus_dmamap_sync() here.
12283 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12284 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12285 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12286 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12287 msg.dt.sg_sequence = 0;
12290 * Loop until we've sent all of the S/G entries. On the
12291 * other end, we'll recompose these S/G entries into one
12292 * contiguous list before passing it to the
12294 for (sg_entries_sent = 0; sg_entries_sent <
12295 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12296 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12297 sizeof(msg.dt.sg_list[0])),
12298 msg.dt.kern_sg_entries - sg_entries_sent);
12300 if (do_sg_copy != 0) {
12301 struct ctl_sg_entry *sgl;
12304 sgl = (struct ctl_sg_entry *)
12305 io->scsiio.kern_data_ptr;
12307 * If this is in cached memory, flush the cache
12308 * before we send the DMA request to the other
12309 * controller. We want to do this in either
12310 * the * read or the write case. The read
12311 * case is straightforward. In the write
12312 * case, we want to make sure nothing is
12313 * in the local cache that could overwrite
12317 for (i = sg_entries_sent, j = 0;
12318 i < msg.dt.cur_sg_entries; i++, j++) {
12319 if ((io->io_hdr.flags &
12320 CTL_FLAG_NO_DATASYNC) == 0) {
12322 * XXX KDM use bus_dmamap_sync()
12325 if ((io->io_hdr.flags &
12326 CTL_FLAG_BUS_ADDR) == 0) {
12328 * XXX KDM use busdma.
12331 msg.dt.sg_list[j].addr =(void *)
12332 vtophys(sgl[i].addr);
12335 msg.dt.sg_list[j].addr =
12338 msg.dt.sg_list[j].len = sgl[i].len;
12342 sg_entries_sent += msg.dt.cur_sg_entries;
12343 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12344 msg.dt.sg_last = 1;
12346 msg.dt.sg_last = 0;
12349 * XXX KDM drop and reacquire the lock here?
12351 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12352 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12354 * XXX do something here.
12358 msg.dt.sent_sg_entries = sg_entries_sent;
12360 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12361 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12362 ctl_failover_io(io, /*have_lock*/ 1);
12367 * Lookup the fe_datamove() function for this particular
12371 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12372 mtx_unlock(&control_softc->ctl_lock);
12379 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12381 union ctl_ha_msg msg;
12384 memset(&msg, 0, sizeof(msg));
12386 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12387 msg.hdr.original_sc = io;
12388 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12389 msg.hdr.nexus = io->io_hdr.nexus;
12390 msg.hdr.status = io->io_hdr.status;
12391 msg.scsi.tag_num = io->scsiio.tag_num;
12392 msg.scsi.tag_type = io->scsiio.tag_type;
12393 msg.scsi.scsi_status = io->scsiio.scsi_status;
12394 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12395 sizeof(io->scsiio.sense_data));
12396 msg.scsi.sense_len = io->scsiio.sense_len;
12397 msg.scsi.sense_residual = io->scsiio.sense_residual;
12398 msg.scsi.fetd_status = io->io_hdr.port_status;
12399 msg.scsi.residual = io->scsiio.residual;
12400 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12402 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12403 ctl_failover_io(io, /*have_lock*/ have_lock);
12407 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12408 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12409 /* XXX do something if this fails */
12415 * The DMA to the remote side is done, now we need to tell the other side
12416 * we're done so it can continue with its data movement.
12419 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12425 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12426 printf("%s: ISC DMA write failed with error %d", __func__,
12428 ctl_set_internal_failure(&io->scsiio,
12430 /*retry_count*/ rq->ret);
12433 ctl_dt_req_free(rq);
12436 * In this case, we had to malloc the memory locally. Free it.
12438 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12440 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12441 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12444 * The data is in local and remote memory, so now we need to send
12445 * status (good or back) back to the other side.
12447 ctl_send_datamove_done(io, /*have_lock*/ 0);
12451 * We've moved the data from the host/controller into local memory. Now we
12452 * need to push it over to the remote controller's memory.
12455 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12461 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12462 ctl_datamove_remote_write_cb);
12468 ctl_datamove_remote_write(union ctl_io *io)
12471 void (*fe_datamove)(union ctl_io *io);
12474 * - Get the data from the host/HBA into local memory.
12475 * - DMA memory from the local controller to the remote controller.
12476 * - Send status back to the remote controller.
12479 retval = ctl_datamove_remote_sgl_setup(io);
12483 /* Switch the pointer over so the FETD knows what to do */
12484 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12487 * Use a custom move done callback, since we need to send completion
12488 * back to the other controller, not to the backend on this side.
12490 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12492 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12501 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12510 * In this case, we had to malloc the memory locally. Free it.
12512 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12514 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12515 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12519 scsi_path_string(io, path_str, sizeof(path_str));
12520 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12521 sbuf_cat(&sb, path_str);
12522 scsi_command_string(&io->scsiio, NULL, &sb);
12523 sbuf_printf(&sb, "\n");
12524 sbuf_cat(&sb, path_str);
12525 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12526 io->scsiio.tag_num, io->scsiio.tag_type);
12527 sbuf_cat(&sb, path_str);
12528 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12529 io->io_hdr.flags, io->io_hdr.status);
12531 printk("%s", sbuf_data(&sb));
12536 * The read is done, now we need to send status (good or bad) back
12537 * to the other side.
12539 ctl_send_datamove_done(io, /*have_lock*/ 0);
12545 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12548 void (*fe_datamove)(union ctl_io *io);
12552 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12553 printf("%s: ISC DMA read failed with error %d", __func__,
12555 ctl_set_internal_failure(&io->scsiio,
12557 /*retry_count*/ rq->ret);
12560 ctl_dt_req_free(rq);
12562 /* Switch the pointer over so the FETD knows what to do */
12563 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12566 * Use a custom move done callback, since we need to send completion
12567 * back to the other controller, not to the backend on this side.
12569 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12571 /* XXX KDM add checks like the ones in ctl_datamove? */
12573 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12579 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12581 struct ctl_sg_entry *local_sglist, *remote_sglist;
12582 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12583 struct ctl_softc *softc;
12588 softc = control_softc;
12590 local_sglist = io->io_hdr.local_sglist;
12591 local_dma_sglist = io->io_hdr.local_dma_sglist;
12592 remote_sglist = io->io_hdr.remote_sglist;
12593 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12595 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12596 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12597 local_sglist[i].len = remote_sglist[i].len;
12600 * XXX Detect the situation where the RS-level I/O
12601 * redirector on the other side has already read the
12602 * data off of the AOR RS on this side, and
12603 * transferred it to remote (mirror) memory on the
12604 * other side. Since we already have the data in
12605 * memory here, we just need to use it.
12607 * XXX KDM this can probably be removed once we
12608 * get the cache device code in and take the
12609 * current AOR implementation out.
12612 if ((remote_sglist[i].addr >=
12613 (void *)vtophys(softc->mirr->addr))
12614 && (remote_sglist[i].addr <
12615 ((void *)vtophys(softc->mirr->addr) +
12616 CacheMirrorOffset))) {
12617 local_sglist[i].addr = remote_sglist[i].addr -
12619 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12621 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12623 local_sglist[i].addr = remote_sglist[i].addr +
12628 printf("%s: local %p, remote %p, len %d\n",
12629 __func__, local_sglist[i].addr,
12630 remote_sglist[i].addr, local_sglist[i].len);
12634 uint32_t len_to_go;
12637 * In this case, we don't have automatically allocated
12638 * memory for this I/O on this controller. This typically
12639 * happens with internal CTL I/O -- e.g. inquiry, mode
12640 * sense, etc. Anything coming from RAIDCore will have
12641 * a mirror area available.
12643 len_to_go = io->scsiio.kern_data_len;
12646 * Clear the no datasync flag, we have to use malloced
12649 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12652 * The difficult thing here is that the size of the various
12653 * S/G segments may be different than the size from the
12654 * remote controller. That'll make it harder when DMAing
12655 * the data back to the other side.
12657 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12658 sizeof(io->io_hdr.remote_sglist[0])) &&
12659 (len_to_go > 0); i++) {
12660 local_sglist[i].len = ctl_min(len_to_go, 131072);
12661 CTL_SIZE_8B(local_dma_sglist[i].len,
12662 local_sglist[i].len);
12663 local_sglist[i].addr =
12664 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12666 local_dma_sglist[i].addr = local_sglist[i].addr;
12668 if (local_sglist[i].addr == NULL) {
12671 printf("malloc failed for %zd bytes!",
12672 local_dma_sglist[i].len);
12673 for (j = 0; j < i; j++) {
12674 free(local_sglist[j].addr, M_CTL);
12676 ctl_set_internal_failure(&io->scsiio,
12678 /*retry_count*/ 4857);
12680 goto bailout_error;
12683 /* XXX KDM do we need a sync here? */
12685 len_to_go -= local_sglist[i].len;
12688 * Reset the number of S/G entries accordingly. The
12689 * original number of S/G entries is available in
12692 io->scsiio.kern_sg_entries = i;
12695 printf("%s: kern_sg_entries = %d\n", __func__,
12696 io->scsiio.kern_sg_entries);
12697 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12698 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12699 local_sglist[i].addr, local_sglist[i].len,
12700 local_dma_sglist[i].len);
12709 ctl_send_datamove_done(io, /*have_lock*/ 0);
12715 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12716 ctl_ha_dt_cb callback)
12718 struct ctl_ha_dt_req *rq;
12719 struct ctl_sg_entry *remote_sglist, *local_sglist;
12720 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12721 uint32_t local_used, remote_used, total_used;
12727 rq = ctl_dt_req_alloc();
12730 * If we failed to allocate the request, and if the DMA didn't fail
12731 * anyway, set busy status. This is just a resource allocation
12735 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12736 ctl_set_busy(&io->scsiio);
12738 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12741 ctl_dt_req_free(rq);
12744 * The data move failed. We need to return status back
12745 * to the other controller. No point in trying to DMA
12746 * data to the remote controller.
12749 ctl_send_datamove_done(io, /*have_lock*/ 0);
12756 local_sglist = io->io_hdr.local_sglist;
12757 local_dma_sglist = io->io_hdr.local_dma_sglist;
12758 remote_sglist = io->io_hdr.remote_sglist;
12759 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12764 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12765 rq->ret = CTL_HA_STATUS_SUCCESS;
12772 * Pull/push the data over the wire from/to the other controller.
12773 * This takes into account the possibility that the local and
12774 * remote sglists may not be identical in terms of the size of
12775 * the elements and the number of elements.
12777 * One fundamental assumption here is that the length allocated for
12778 * both the local and remote sglists is identical. Otherwise, we've
12779 * essentially got a coding error of some sort.
12781 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12783 uint32_t cur_len, dma_length;
12786 rq->id = CTL_HA_DATA_CTL;
12787 rq->command = command;
12791 * Both pointers should be aligned. But it is possible
12792 * that the allocation length is not. They should both
12793 * also have enough slack left over at the end, though,
12794 * to round up to the next 8 byte boundary.
12796 cur_len = ctl_min(local_sglist[i].len - local_used,
12797 remote_sglist[j].len - remote_used);
12800 * In this case, we have a size issue and need to decrease
12801 * the size, except in the case where we actually have less
12802 * than 8 bytes left. In that case, we need to increase
12803 * the DMA length to get the last bit.
12805 if ((cur_len & 0x7) != 0) {
12806 if (cur_len > 0x7) {
12807 cur_len = cur_len - (cur_len & 0x7);
12808 dma_length = cur_len;
12810 CTL_SIZE_8B(dma_length, cur_len);
12814 dma_length = cur_len;
12817 * If we had to allocate memory for this I/O, instead of using
12818 * the non-cached mirror memory, we'll need to flush the cache
12819 * before trying to DMA to the other controller.
12821 * We could end up doing this multiple times for the same
12822 * segment if we have a larger local segment than remote
12823 * segment. That shouldn't be an issue.
12825 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12827 * XXX KDM use bus_dmamap_sync() here.
12831 rq->size = dma_length;
12833 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12834 tmp_ptr += local_used;
12836 /* Use physical addresses when talking to ISC hardware */
12837 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12838 /* XXX KDM use busdma */
12840 rq->local = vtophys(tmp_ptr);
12843 rq->local = tmp_ptr;
12845 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12846 tmp_ptr += remote_used;
12847 rq->remote = tmp_ptr;
12849 rq->callback = NULL;
12851 local_used += cur_len;
12852 if (local_used >= local_sglist[i].len) {
12857 remote_used += cur_len;
12858 if (remote_used >= remote_sglist[j].len) {
12862 total_used += cur_len;
12864 if (total_used >= io->scsiio.kern_data_len)
12865 rq->callback = callback;
12867 if ((rq->size & 0x7) != 0) {
12868 printf("%s: warning: size %d is not on 8b boundary\n",
12869 __func__, rq->size);
12871 if (((uintptr_t)rq->local & 0x7) != 0) {
12872 printf("%s: warning: local %p not on 8b boundary\n",
12873 __func__, rq->local);
12875 if (((uintptr_t)rq->remote & 0x7) != 0) {
12876 printf("%s: warning: remote %p not on 8b boundary\n",
12877 __func__, rq->local);
12880 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12881 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12882 rq->local, rq->remote, rq->size);
12885 isc_ret = ctl_dt_single(rq);
12886 if (isc_ret == CTL_HA_STATUS_WAIT)
12889 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12890 rq->ret = CTL_HA_STATUS_SUCCESS;
12904 ctl_datamove_remote_read(union ctl_io *io)
12910 * This will send an error to the other controller in the case of a
12913 retval = ctl_datamove_remote_sgl_setup(io);
12917 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12918 ctl_datamove_remote_read_cb);
12920 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12922 * Make sure we free memory if there was an error.. The
12923 * ctl_datamove_remote_xfer() function will send the
12924 * datamove done message, or call the callback with an
12925 * error if there is a problem.
12927 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12928 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12935 * Process a datamove request from the other controller. This is used for
12936 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12937 * first. Once that is complete, the data gets DMAed into the remote
12938 * controller's memory. For reads, we DMA from the remote controller's
12939 * memory into our memory first, and then move it out to the FETD.
12942 ctl_datamove_remote(union ctl_io *io)
12944 struct ctl_softc *softc;
12946 softc = control_softc;
12948 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12951 * Note that we look for an aborted I/O here, but don't do some of
12952 * the other checks that ctl_datamove() normally does. We don't
12953 * need to run the task queue, because this I/O is on the ISC
12954 * queue, which is executed by the work thread after the task queue.
12955 * We don't need to run the datamove delay code, since that should
12956 * have been done if need be on the other controller.
12958 mtx_lock(&softc->ctl_lock);
12960 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12962 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12963 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12964 io->io_hdr.nexus.targ_port,
12965 io->io_hdr.nexus.targ_target.id,
12966 io->io_hdr.nexus.targ_lun);
12967 io->io_hdr.status = CTL_CMD_ABORTED;
12968 io->io_hdr.port_status = 31338;
12970 mtx_unlock(&softc->ctl_lock);
12972 ctl_send_datamove_done(io, /*have_lock*/ 0);
12977 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12978 mtx_unlock(&softc->ctl_lock);
12979 ctl_datamove_remote_write(io);
12980 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12981 mtx_unlock(&softc->ctl_lock);
12982 ctl_datamove_remote_read(io);
12984 union ctl_ha_msg msg;
12985 struct scsi_sense_data *sense;
12989 memset(&msg, 0, sizeof(msg));
12991 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12992 msg.hdr.status = CTL_SCSI_ERROR;
12993 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12995 retry_count = 4243;
12997 sense = &msg.scsi.sense_data;
12998 sks[0] = SSD_SCS_VALID;
12999 sks[1] = (retry_count >> 8) & 0xff;
13000 sks[2] = retry_count & 0xff;
13002 /* "Internal target failure" */
13003 scsi_set_sense_data(sense,
13004 /*sense_format*/ SSD_TYPE_NONE,
13005 /*current_error*/ 1,
13006 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13009 /*type*/ SSD_ELEM_SKS,
13010 /*size*/ sizeof(sks),
13014 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13015 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13016 ctl_failover_io(io, /*have_lock*/ 1);
13017 mtx_unlock(&softc->ctl_lock);
13021 mtx_unlock(&softc->ctl_lock);
13023 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13024 CTL_HA_STATUS_SUCCESS) {
13025 /* XXX KDM what to do if this fails? */
13033 ctl_process_done(union ctl_io *io, int have_lock)
13035 struct ctl_lun *lun;
13036 struct ctl_softc *ctl_softc;
13037 void (*fe_done)(union ctl_io *io);
13038 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13040 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13043 control_softc->ctl_ports[targ_port]->fe_done;
13046 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13051 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13052 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13054 sbuf_cat(&sb, path_str);
13055 switch (io->io_hdr.io_type) {
13057 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13058 sbuf_printf(&sb, "\n");
13059 sbuf_cat(&sb, path_str);
13060 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13061 io->scsiio.tag_num, io->scsiio.tag_type);
13064 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13065 "Tag Type: %d\n", io->taskio.task_action,
13066 io->taskio.tag_num, io->taskio.tag_type);
13069 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13070 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13073 sbuf_cat(&sb, path_str);
13074 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13075 (intmax_t)time_uptime - io->io_hdr.start_time);
13077 printf("%s", sbuf_data(&sb));
13079 #endif /* CTL_TIME_IO */
13081 switch (io->io_hdr.io_type) {
13085 if (bootverbose || verbose > 0)
13086 ctl_io_error_print(io, NULL);
13087 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13091 return (CTL_RETVAL_COMPLETE);
13094 printf("ctl_process_done: invalid io type %d\n",
13095 io->io_hdr.io_type);
13096 panic("ctl_process_done: invalid io type %d\n",
13097 io->io_hdr.io_type);
13098 break; /* NOTREACHED */
13101 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13103 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13104 io->io_hdr.nexus.targ_lun));
13108 ctl_softc = lun->ctl_softc;
13111 * Remove this from the OOA queue.
13113 if (have_lock == 0)
13114 mtx_lock(&ctl_softc->ctl_lock);
13117 * Check to see if we have any errors to inject here. We only
13118 * inject errors for commands that don't already have errors set.
13120 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13121 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13122 ctl_inject_error(lun, io);
13125 * XXX KDM how do we treat commands that aren't completed
13128 * XXX KDM should we also track I/O latency?
13130 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13131 io->io_hdr.io_type == CTL_IO_SCSI) {
13133 struct bintime cur_bt;
13137 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13139 type = CTL_STATS_READ;
13140 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13142 type = CTL_STATS_WRITE;
13144 type = CTL_STATS_NO_IO;
13146 lun->stats.ports[targ_port].bytes[type] +=
13147 io->scsiio.kern_total_len;
13148 lun->stats.ports[targ_port].operations[type]++;
13150 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13151 &io->io_hdr.dma_bt);
13152 lun->stats.ports[targ_port].num_dmas[type] +=
13153 io->io_hdr.num_dmas;
13154 getbintime(&cur_bt);
13155 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13156 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13160 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13163 * Run through the blocked queue on this LUN and see if anything
13164 * has become unblocked, now that this transaction is done.
13166 ctl_check_blocked(lun);
13169 * If the LUN has been invalidated, free it if there is nothing
13170 * left on its OOA queue.
13172 if ((lun->flags & CTL_LUN_INVALID)
13173 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
13177 * If this command has been aborted, make sure we set the status
13178 * properly. The FETD is responsible for freeing the I/O and doing
13179 * whatever it needs to do to clean up its state.
13181 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13182 io->io_hdr.status = CTL_CMD_ABORTED;
13185 * We print out status for every task management command. For SCSI
13186 * commands, we filter out any unit attention errors; they happen
13187 * on every boot, and would clutter up the log. Note: task
13188 * management commands aren't printed here, they are printed above,
13189 * since they should never even make it down here.
13191 switch (io->io_hdr.io_type) {
13192 case CTL_IO_SCSI: {
13193 int error_code, sense_key, asc, ascq;
13197 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13198 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13200 * Since this is just for printing, no need to
13201 * show errors here.
13203 scsi_extract_sense_len(&io->scsiio.sense_data,
13204 io->scsiio.sense_len,
13209 /*show_errors*/ 0);
13212 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13213 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13214 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13215 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13217 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13218 ctl_softc->skipped_prints++;
13219 if (have_lock == 0)
13220 mtx_unlock(&ctl_softc->ctl_lock);
13222 uint32_t skipped_prints;
13224 skipped_prints = ctl_softc->skipped_prints;
13226 ctl_softc->skipped_prints = 0;
13227 ctl_softc->last_print_jiffies = time_uptime;
13229 if (have_lock == 0)
13230 mtx_unlock(&ctl_softc->ctl_lock);
13231 if (skipped_prints > 0) {
13233 csevent_log(CSC_CTL | CSC_SHELF_SW |
13235 csevent_LogType_Trace,
13236 csevent_Severity_Information,
13237 csevent_AlertLevel_Green,
13238 csevent_FRU_Firmware,
13239 csevent_FRU_Unknown,
13240 "High CTL error volume, %d prints "
13241 "skipped", skipped_prints);
13244 if (bootverbose || verbose > 0)
13245 ctl_io_error_print(io, NULL);
13248 if (have_lock == 0)
13249 mtx_unlock(&ctl_softc->ctl_lock);
13254 if (have_lock == 0)
13255 mtx_unlock(&ctl_softc->ctl_lock);
13256 if (bootverbose || verbose > 0)
13257 ctl_io_error_print(io, NULL);
13260 if (have_lock == 0)
13261 mtx_unlock(&ctl_softc->ctl_lock);
13266 * Tell the FETD or the other shelf controller we're done with this
13267 * command. Note that only SCSI commands get to this point. Task
13268 * management commands are completed above.
13270 * We only send status to the other controller if we're in XFER
13271 * mode. In SER_ONLY mode, the I/O is done on the controller that
13272 * received the I/O (from CTL's perspective), and so the status is
13275 * XXX KDM if we hold the lock here, we could cause a deadlock
13276 * if the frontend comes back in in this context to queue
13279 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13280 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13281 union ctl_ha_msg msg;
13283 memset(&msg, 0, sizeof(msg));
13284 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13285 msg.hdr.original_sc = io->io_hdr.original_sc;
13286 msg.hdr.nexus = io->io_hdr.nexus;
13287 msg.hdr.status = io->io_hdr.status;
13288 msg.scsi.scsi_status = io->scsiio.scsi_status;
13289 msg.scsi.tag_num = io->scsiio.tag_num;
13290 msg.scsi.tag_type = io->scsiio.tag_type;
13291 msg.scsi.sense_len = io->scsiio.sense_len;
13292 msg.scsi.sense_residual = io->scsiio.sense_residual;
13293 msg.scsi.residual = io->scsiio.residual;
13294 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13295 sizeof(io->scsiio.sense_data));
13297 * We copy this whether or not this is an I/O-related
13298 * command. Otherwise, we'd have to go and check to see
13299 * whether it's a read/write command, and it really isn't
13302 memcpy(&msg.scsi.lbalen,
13303 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13304 sizeof(msg.scsi.lbalen));
13306 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13307 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13308 /* XXX do something here */
13317 return (CTL_RETVAL_COMPLETE);
13321 * Front end should call this if it doesn't do autosense. When the request
13322 * sense comes back in from the initiator, we'll dequeue this and send it.
13325 ctl_queue_sense(union ctl_io *io)
13327 struct ctl_lun *lun;
13328 struct ctl_softc *ctl_softc;
13329 uint32_t initidx, targ_lun;
13331 ctl_softc = control_softc;
13333 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13336 * LUN lookup will likely move to the ctl_work_thread() once we
13337 * have our new queueing infrastructure (that doesn't put things on
13338 * a per-LUN queue initially). That is so that we can handle
13339 * things like an INQUIRY to a LUN that we don't have enabled. We
13340 * can't deal with that right now.
13342 mtx_lock(&ctl_softc->ctl_lock);
13345 * If we don't have a LUN for this, just toss the sense
13348 targ_lun = io->io_hdr.nexus.targ_lun;
13349 if (io->io_hdr.nexus.lun_map_fn != NULL)
13350 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13351 if ((targ_lun < CTL_MAX_LUNS)
13352 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13353 lun = ctl_softc->ctl_luns[targ_lun];
13357 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13360 * Already have CA set for this LUN...toss the sense information.
13362 if (ctl_is_set(lun->have_ca, initidx))
13365 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13366 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13367 sizeof(io->scsiio.sense_data)));
13368 ctl_set_mask(lun->have_ca, initidx);
13371 mtx_unlock(&ctl_softc->ctl_lock);
13375 return (CTL_RETVAL_COMPLETE);
13379 * Primary command inlet from frontend ports. All SCSI and task I/O
13380 * requests must go through this function.
13383 ctl_queue(union ctl_io *io)
13385 struct ctl_softc *ctl_softc;
13387 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13389 ctl_softc = control_softc;
13392 io->io_hdr.start_time = time_uptime;
13393 getbintime(&io->io_hdr.start_bt);
13394 #endif /* CTL_TIME_IO */
13396 mtx_lock(&ctl_softc->ctl_lock);
13398 switch (io->io_hdr.io_type) {
13400 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
13404 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
13406 * Set the task pending flag. This is necessary to close a
13407 * race condition with the FETD:
13409 * - FETD submits a task management command, like an abort.
13410 * - Back end calls fe_datamove() to move the data for the
13411 * aborted command. The FETD can't really accept it, but
13412 * if it did, it would end up transmitting data for a
13413 * command that the initiator told us to abort.
13415 * We close the race condition by setting the flag here,
13416 * and checking it in ctl_datamove(), before calling the
13417 * FETD's fe_datamove routine. If we've got a task
13418 * pending, we run the task queue and then check to see
13419 * whether our particular I/O has been aborted.
13421 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
13424 mtx_unlock(&ctl_softc->ctl_lock);
13425 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13427 break; /* NOTREACHED */
13429 mtx_unlock(&ctl_softc->ctl_lock);
13431 ctl_wakeup_thread();
13433 return (CTL_RETVAL_COMPLETE);
13436 #ifdef CTL_IO_DELAY
13438 ctl_done_timer_wakeup(void *arg)
13442 io = (union ctl_io *)arg;
13443 ctl_done_lock(io, /*have_lock*/ 0);
13445 #endif /* CTL_IO_DELAY */
13448 ctl_done_lock(union ctl_io *io, int have_lock)
13450 struct ctl_softc *ctl_softc;
13451 #ifndef CTL_DONE_THREAD
13453 #endif /* !CTL_DONE_THREAD */
13455 ctl_softc = control_softc;
13457 if (have_lock == 0)
13458 mtx_lock(&ctl_softc->ctl_lock);
13461 * Enable this to catch duplicate completion issues.
13464 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13465 printf("%s: type %d msg %d cdb %x iptl: "
13466 "%d:%d:%d:%d tag 0x%04x "
13467 "flag %#x status %x\n",
13469 io->io_hdr.io_type,
13470 io->io_hdr.msg_type,
13472 io->io_hdr.nexus.initid.id,
13473 io->io_hdr.nexus.targ_port,
13474 io->io_hdr.nexus.targ_target.id,
13475 io->io_hdr.nexus.targ_lun,
13476 (io->io_hdr.io_type ==
13478 io->taskio.tag_num :
13479 io->scsiio.tag_num,
13481 io->io_hdr.status);
13483 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13487 * This is an internal copy of an I/O, and should not go through
13488 * the normal done processing logic.
13490 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
13491 if (have_lock == 0)
13492 mtx_unlock(&ctl_softc->ctl_lock);
13497 * We need to send a msg to the serializing shelf to finish the IO
13498 * as well. We don't send a finish message to the other shelf if
13499 * this is a task management command. Task management commands
13500 * aren't serialized in the OOA queue, but rather just executed on
13501 * both shelf controllers for commands that originated on that
13504 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13505 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13506 union ctl_ha_msg msg_io;
13508 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13509 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13510 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13511 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13513 /* continue on to finish IO */
13515 #ifdef CTL_IO_DELAY
13516 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13517 struct ctl_lun *lun;
13519 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13521 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13523 struct ctl_lun *lun;
13525 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13528 && (lun->delay_info.done_delay > 0)) {
13529 struct callout *callout;
13531 callout = (struct callout *)&io->io_hdr.timer_bytes;
13532 callout_init(callout, /*mpsafe*/ 1);
13533 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13534 callout_reset(callout,
13535 lun->delay_info.done_delay * hz,
13536 ctl_done_timer_wakeup, io);
13537 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13538 lun->delay_info.done_delay = 0;
13539 if (have_lock == 0)
13540 mtx_unlock(&ctl_softc->ctl_lock);
13544 #endif /* CTL_IO_DELAY */
13546 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
13548 #ifdef CTL_DONE_THREAD
13549 if (have_lock == 0)
13550 mtx_unlock(&ctl_softc->ctl_lock);
13552 ctl_wakeup_thread();
13553 #else /* CTL_DONE_THREAD */
13554 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
13556 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
13558 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
13560 ctl_process_done(xio, /*have_lock*/ 1);
13562 if (have_lock == 0)
13563 mtx_unlock(&ctl_softc->ctl_lock);
13564 #endif /* CTL_DONE_THREAD */
13568 ctl_done(union ctl_io *io)
13570 ctl_done_lock(io, /*have_lock*/ 0);
13574 ctl_isc(struct ctl_scsiio *ctsio)
13576 struct ctl_lun *lun;
13579 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13581 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13583 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13585 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13592 ctl_work_thread(void *arg)
13594 struct ctl_softc *softc;
13596 struct ctl_be_lun *be_lun;
13599 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13601 softc = (struct ctl_softc *)arg;
13605 mtx_lock(&softc->ctl_lock);
13610 * We handle the queues in this order:
13611 * - task management
13613 * - done queue (to free up resources, unblock other commands)
13617 * If those queues are empty, we break out of the loop and
13620 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
13622 ctl_run_task_queue(softc);
13625 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
13627 STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
13628 ctl_handle_isc(io);
13631 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
13633 STAILQ_REMOVE_HEAD(&softc->done_queue, links);
13634 /* clear any blocked commands, call fe_done */
13635 mtx_unlock(&softc->ctl_lock);
13638 * Call this without a lock for now. This will
13639 * depend on whether there is any way the FETD can
13640 * sleep or deadlock if called with the CTL lock
13643 retval = ctl_process_done(io, /*have_lock*/ 0);
13644 mtx_lock(&softc->ctl_lock);
13647 if (!ctl_pause_rtr) {
13648 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
13650 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
13651 mtx_unlock(&softc->ctl_lock);
13652 retval = ctl_scsiio(&io->scsiio);
13653 if (retval != CTL_RETVAL_COMPLETE)
13654 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13655 mtx_lock(&softc->ctl_lock);
13659 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
13661 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
13662 mtx_unlock(&softc->ctl_lock);
13663 ctl_scsiio_precheck(softc, &io->scsiio);
13664 mtx_lock(&softc->ctl_lock);
13668 * We might want to move this to a separate thread, so that
13669 * configuration requests (in this case LUN creations)
13670 * won't impact the I/O path.
13672 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13673 if (be_lun != NULL) {
13674 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13675 mtx_unlock(&softc->ctl_lock);
13676 ctl_create_lun(be_lun);
13677 mtx_lock(&softc->ctl_lock);
13681 /* XXX KDM use the PDROP flag?? */
13682 /* Sleep until we have something to do. */
13683 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "-", 0);
13685 /* Back to the top of the loop to see what woke us up. */
13691 ctl_wakeup_thread()
13693 struct ctl_softc *softc;
13695 softc = control_softc;
13700 /* Initialization and failover */
13703 ctl_init_isc_msg(void)
13705 printf("CTL: Still calling this thing\n");
13710 * Initializes component into configuration defined by bootMode
13712 * returns hasc_Status:
13714 * ERROR - fatal error
13716 static ctl_ha_comp_status
13717 ctl_isc_init(struct ctl_ha_component *c)
13719 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13726 * Starts component in state requested. If component starts successfully,
13727 * it must set its own state to the requestrd state
13728 * When requested state is HASC_STATE_HA, the component may refine it
13729 * by adding _SLAVE or _MASTER flags.
13730 * Currently allowed state transitions are:
13731 * UNKNOWN->HA - initial startup
13732 * UNKNOWN->SINGLE - initial startup when no parter detected
13733 * HA->SINGLE - failover
13734 * returns ctl_ha_comp_status:
13735 * OK - component successfully started in requested state
13736 * FAILED - could not start the requested state, failover may
13738 * ERROR - fatal error detected, no future startup possible
13740 static ctl_ha_comp_status
13741 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13743 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13745 printf("%s: go\n", __func__);
13747 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13748 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13750 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13751 != CTL_HA_STATUS_SUCCESS) {
13752 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13753 ret = CTL_HA_COMP_STATUS_ERROR;
13755 } else if (CTL_HA_STATE_IS_HA(c->state)
13756 && CTL_HA_STATE_IS_SINGLE(state)){
13757 // HA->SINGLE transition
13761 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13763 ret = CTL_HA_COMP_STATUS_ERROR;
13765 if (CTL_HA_STATE_IS_SINGLE(state))
13774 * Quiesce component
13775 * The component must clear any error conditions (set status to OK) and
13776 * prepare itself to another Start call
13777 * returns ctl_ha_comp_status:
13781 static ctl_ha_comp_status
13782 ctl_isc_quiesce(struct ctl_ha_component *c)
13784 int ret = CTL_HA_COMP_STATUS_OK;
13791 struct ctl_ha_component ctl_ha_component_ctlisc =
13794 .state = CTL_HA_STATE_UNKNOWN,
13795 .init = ctl_isc_init,
13796 .start = ctl_isc_start,
13797 .quiesce = ctl_isc_quiesce