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;
966 int i, error, retval;
973 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
975 softc = control_softc;
977 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
980 softc->dev->si_drv1 = softc;
983 * By default, return a "bad LUN" peripheral qualifier for unknown
984 * LUNs. The user can override this default using the tunable or
985 * sysctl. See the comment in ctl_inquiry_std() for more details.
987 softc->inquiry_pq_no_lun = 1;
988 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
989 &softc->inquiry_pq_no_lun);
990 sysctl_ctx_init(&softc->sysctl_ctx);
991 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
992 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
993 CTLFLAG_RD, 0, "CAM Target Layer");
995 if (softc->sysctl_tree == NULL) {
996 printf("%s: unable to allocate sysctl tree\n", __func__);
997 destroy_dev(softc->dev);
998 free(control_softc, M_DEVBUF);
999 control_softc = NULL;
1003 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1004 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1005 "inquiry_pq_no_lun", CTLFLAG_RW,
1006 &softc->inquiry_pq_no_lun, 0,
1007 "Report no lun possible for invalid LUNs");
1009 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1010 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
1011 softc->open_count = 0;
1014 * Default to actually sending a SYNCHRONIZE CACHE command down to
1017 softc->flags = CTL_FLAG_REAL_SYNC;
1020 * In Copan's HA scheme, the "master" and "slave" roles are
1021 * figured out through the slot the controller is in. Although it
1022 * is an active/active system, someone has to be in charge.
1025 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1029 softc->flags |= CTL_FLAG_MASTER_SHELF;
1032 persis_offset = CTL_MAX_INITIATORS;
1035 * XXX KDM need to figure out where we want to get our target ID
1036 * and WWID. Is it different on each port?
1038 softc->target.id = 0;
1039 softc->target.wwid[0] = 0x12345678;
1040 softc->target.wwid[1] = 0x87654321;
1041 STAILQ_INIT(&softc->lun_list);
1042 STAILQ_INIT(&softc->pending_lun_queue);
1043 STAILQ_INIT(&softc->task_queue);
1044 STAILQ_INIT(&softc->incoming_queue);
1045 STAILQ_INIT(&softc->rtr_queue);
1046 STAILQ_INIT(&softc->done_queue);
1047 STAILQ_INIT(&softc->isc_queue);
1048 STAILQ_INIT(&softc->fe_list);
1049 STAILQ_INIT(&softc->be_list);
1050 STAILQ_INIT(&softc->io_pools);
1055 * We don't bother calling these with ctl_lock held here, because,
1056 * in theory, no one else can try to do anything while we're in our
1057 * module init routine.
1059 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1060 &internal_pool)!= 0){
1061 printf("ctl: can't allocate %d entry internal pool, "
1062 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1066 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1067 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1068 printf("ctl: can't allocate %d entry emergency pool, "
1069 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1070 ctl_pool_free(internal_pool);
1074 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1077 printf("ctl: can't allocate %d entry other SC pool, "
1078 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1079 ctl_pool_free(internal_pool);
1080 ctl_pool_free(emergency_pool);
1084 softc->internal_pool = internal_pool;
1085 softc->emergency_pool = emergency_pool;
1086 softc->othersc_pool = other_pool;
1089 * We used to allocate a processor LUN here. The new scheme is to
1090 * just let the user allocate LUNs as he sees fit.
1093 mtx_lock(&softc->ctl_lock);
1094 ctl_alloc_lun(softc, lun, /*be_lun*/NULL, /*target*/softc->target);
1095 mtx_unlock(&softc->ctl_lock);
1098 if (worker_threads > MAXCPU || worker_threads == 0) {
1099 printf("invalid kern.cam.ctl.worker_threads value; "
1102 } else if (worker_threads < 0) {
1105 * Using more than two worker threads actually hurts
1106 * performance due to lock contention.
1114 for (i = 0; i < worker_threads; i++) {
1115 error = kproc_kthread_add(ctl_work_thread, softc,
1116 &softc->work_thread, NULL, 0, 0, "ctl", "work%d", i);
1118 printf("error creating CTL work thread!\n");
1119 mtx_lock(&softc->ctl_lock);
1121 mtx_unlock(&softc->ctl_lock);
1122 ctl_pool_free(internal_pool);
1123 ctl_pool_free(emergency_pool);
1124 ctl_pool_free(other_pool);
1129 printf("ctl: CAM Target Layer loaded\n");
1132 * Initialize the initiator and portname mappings
1134 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1137 * Initialize the ioctl front end.
1139 fe = &softc->ioctl_info.fe;
1140 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1141 fe->port_type = CTL_PORT_IOCTL;
1142 fe->num_requested_ctl_io = 100;
1143 fe->port_name = softc->ioctl_info.port_name;
1144 fe->port_online = ctl_ioctl_online;
1145 fe->port_offline = ctl_ioctl_offline;
1146 fe->onoff_arg = &softc->ioctl_info;
1147 fe->targ_enable = ctl_ioctl_targ_enable;
1148 fe->targ_disable = ctl_ioctl_targ_disable;
1149 fe->lun_enable = ctl_ioctl_lun_enable;
1150 fe->lun_disable = ctl_ioctl_lun_disable;
1151 fe->targ_lun_arg = &softc->ioctl_info;
1152 fe->fe_datamove = ctl_ioctl_datamove;
1153 fe->fe_done = ctl_ioctl_done;
1154 fe->max_targets = 15;
1155 fe->max_target_id = 15;
1157 if (ctl_frontend_register(&softc->ioctl_info.fe,
1158 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1159 printf("ctl: ioctl front end registration failed, will "
1160 "continue anyway\n");
1164 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1165 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1166 sizeof(struct callout), CTL_TIMER_BYTES);
1169 #endif /* CTL_IO_DELAY */
1177 struct ctl_softc *softc;
1178 struct ctl_lun *lun, *next_lun;
1179 struct ctl_io_pool *pool;
1181 softc = (struct ctl_softc *)control_softc;
1183 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1184 printf("ctl: ioctl front end deregistration failed\n");
1186 mtx_lock(&softc->ctl_lock);
1191 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1192 next_lun = STAILQ_NEXT(lun, links);
1196 mtx_unlock(&softc->ctl_lock);
1199 * This will rip the rug out from under any FETDs or anyone else
1200 * that has a pool allocated. Since we increment our module
1201 * refcount any time someone outside the main CTL module allocates
1202 * a pool, we shouldn't have any problems here. The user won't be
1203 * able to unload the CTL module until client modules have
1204 * successfully unloaded.
1206 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1207 ctl_pool_free(pool);
1210 ctl_shutdown_thread(softc->work_thread);
1213 mtx_destroy(&softc->pool_lock);
1214 mtx_destroy(&softc->ctl_lock);
1216 destroy_dev(softc->dev);
1218 sysctl_ctx_free(&softc->sysctl_ctx);
1220 free(control_softc, M_DEVBUF);
1221 control_softc = NULL;
1224 printf("ctl: CAM Target Layer unloaded\n");
1228 ctl_module_event_handler(module_t mod, int what, void *arg)
1233 return (ctl_init());
1237 return (EOPNOTSUPP);
1242 * XXX KDM should we do some access checks here? Bump a reference count to
1243 * prevent a CTL module from being unloaded while someone has it open?
1246 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1252 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1258 ctl_port_enable(ctl_port_type port_type)
1260 struct ctl_softc *softc;
1261 struct ctl_frontend *fe;
1263 if (ctl_is_single == 0) {
1264 union ctl_ha_msg msg_info;
1268 printf("%s: HA mode, synchronizing frontend enable\n",
1271 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1272 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1273 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1274 printf("Sync msg send error retval %d\n", isc_retval);
1276 if (!rcv_sync_msg) {
1277 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1278 sizeof(msg_info), 1);
1281 printf("CTL:Frontend Enable\n");
1283 printf("%s: single mode, skipping frontend synchronization\n",
1288 softc = control_softc;
1290 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1291 if (port_type & fe->port_type)
1294 printf("port %d\n", fe->targ_port);
1296 ctl_frontend_online(fe);
1304 ctl_port_disable(ctl_port_type port_type)
1306 struct ctl_softc *softc;
1307 struct ctl_frontend *fe;
1309 softc = control_softc;
1311 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1312 if (port_type & fe->port_type)
1313 ctl_frontend_offline(fe);
1320 * Returns 0 for success, 1 for failure.
1321 * Currently the only failure mode is if there aren't enough entries
1322 * allocated. So, in case of a failure, look at num_entries_dropped,
1323 * reallocate and try again.
1326 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1327 int *num_entries_filled, int *num_entries_dropped,
1328 ctl_port_type port_type, int no_virtual)
1330 struct ctl_softc *softc;
1331 struct ctl_frontend *fe;
1332 int entries_dropped, entries_filled;
1336 softc = control_softc;
1340 entries_dropped = 0;
1343 mtx_lock(&softc->ctl_lock);
1344 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1345 struct ctl_port_entry *entry;
1347 if ((fe->port_type & port_type) == 0)
1350 if ((no_virtual != 0)
1351 && (fe->virtual_port != 0))
1354 if (entries_filled >= num_entries_alloced) {
1358 entry = &entries[i];
1360 entry->port_type = fe->port_type;
1361 strlcpy(entry->port_name, fe->port_name,
1362 sizeof(entry->port_name));
1363 entry->physical_port = fe->physical_port;
1364 entry->virtual_port = fe->virtual_port;
1365 entry->wwnn = fe->wwnn;
1366 entry->wwpn = fe->wwpn;
1372 mtx_unlock(&softc->ctl_lock);
1374 if (entries_dropped > 0)
1377 *num_entries_dropped = entries_dropped;
1378 *num_entries_filled = entries_filled;
1384 ctl_ioctl_online(void *arg)
1386 struct ctl_ioctl_info *ioctl_info;
1388 ioctl_info = (struct ctl_ioctl_info *)arg;
1390 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1394 ctl_ioctl_offline(void *arg)
1396 struct ctl_ioctl_info *ioctl_info;
1398 ioctl_info = (struct ctl_ioctl_info *)arg;
1400 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1404 * Remove an initiator by port number and initiator ID.
1405 * Returns 0 for success, 1 for failure.
1408 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1410 struct ctl_softc *softc;
1412 softc = control_softc;
1414 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1417 || (targ_port > CTL_MAX_PORTS)) {
1418 printf("%s: invalid port number %d\n", __func__, targ_port);
1421 if (iid > CTL_MAX_INIT_PER_PORT) {
1422 printf("%s: initiator ID %u > maximun %u!\n",
1423 __func__, iid, CTL_MAX_INIT_PER_PORT);
1427 mtx_lock(&softc->ctl_lock);
1429 softc->wwpn_iid[targ_port][iid].in_use = 0;
1431 mtx_unlock(&softc->ctl_lock);
1437 * Add an initiator to the initiator map.
1438 * Returns 0 for success, 1 for failure.
1441 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1443 struct ctl_softc *softc;
1446 softc = control_softc;
1448 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1453 || (targ_port > CTL_MAX_PORTS)) {
1454 printf("%s: invalid port number %d\n", __func__, targ_port);
1457 if (iid > CTL_MAX_INIT_PER_PORT) {
1458 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1459 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1463 mtx_lock(&softc->ctl_lock);
1465 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1467 * We don't treat this as an error.
1469 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1470 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1471 __func__, targ_port, iid, (uintmax_t)wwpn);
1476 * This is an error, but what do we do about it? The
1477 * driver is telling us we have a new WWPN for this
1478 * initiator ID, so we pretty much need to use it.
1480 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1481 "still at that address\n", __func__, targ_port, iid,
1483 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1486 * XXX KDM clear have_ca and ua_pending on each LUN for
1490 softc->wwpn_iid[targ_port][iid].in_use = 1;
1491 softc->wwpn_iid[targ_port][iid].iid = iid;
1492 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1493 softc->wwpn_iid[targ_port][iid].port = targ_port;
1497 mtx_unlock(&softc->ctl_lock);
1503 * XXX KDM should we pretend to do something in the target/lun
1504 * enable/disable functions?
1507 ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1513 ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1519 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1525 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1531 * Data movement routine for the CTL ioctl frontend port.
1534 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1536 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1537 struct ctl_sg_entry ext_entry, kern_entry;
1538 int ext_sglen, ext_sg_entries, kern_sg_entries;
1539 int ext_sg_start, ext_offset;
1540 int len_to_copy, len_copied;
1541 int kern_watermark, ext_watermark;
1542 int ext_sglist_malloced;
1545 ext_sglist_malloced = 0;
1549 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1552 * If this flag is set, fake the data transfer.
1554 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1555 ctsio->ext_data_filled = ctsio->ext_data_len;
1560 * To simplify things here, if we have a single buffer, stick it in
1561 * a S/G entry and just make it a single entry S/G list.
1563 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1566 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1568 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1570 ext_sglist_malloced = 1;
1571 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1573 ctl_set_internal_failure(ctsio,
1578 ext_sg_entries = ctsio->ext_sg_entries;
1580 for (i = 0; i < ext_sg_entries; i++) {
1581 if ((len_seen + ext_sglist[i].len) >=
1582 ctsio->ext_data_filled) {
1584 ext_offset = ctsio->ext_data_filled - len_seen;
1587 len_seen += ext_sglist[i].len;
1590 ext_sglist = &ext_entry;
1591 ext_sglist->addr = ctsio->ext_data_ptr;
1592 ext_sglist->len = ctsio->ext_data_len;
1595 ext_offset = ctsio->ext_data_filled;
1598 if (ctsio->kern_sg_entries > 0) {
1599 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1600 kern_sg_entries = ctsio->kern_sg_entries;
1602 kern_sglist = &kern_entry;
1603 kern_sglist->addr = ctsio->kern_data_ptr;
1604 kern_sglist->len = ctsio->kern_data_len;
1605 kern_sg_entries = 1;
1610 ext_watermark = ext_offset;
1612 for (i = ext_sg_start, j = 0;
1613 i < ext_sg_entries && j < kern_sg_entries;) {
1614 uint8_t *ext_ptr, *kern_ptr;
1616 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1617 kern_sglist[j].len - kern_watermark);
1619 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1620 ext_ptr = ext_ptr + ext_watermark;
1621 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1625 panic("need to implement bus address support");
1627 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1630 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1631 kern_ptr = kern_ptr + kern_watermark;
1633 kern_watermark += len_to_copy;
1634 ext_watermark += len_to_copy;
1636 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1638 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1639 "bytes to user\n", len_to_copy));
1640 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1641 "to %p\n", kern_ptr, ext_ptr));
1642 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1643 ctl_set_internal_failure(ctsio,
1649 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1650 "bytes from user\n", len_to_copy));
1651 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1652 "to %p\n", ext_ptr, kern_ptr));
1653 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1654 ctl_set_internal_failure(ctsio,
1661 len_copied += len_to_copy;
1663 if (ext_sglist[i].len == ext_watermark) {
1668 if (kern_sglist[j].len == kern_watermark) {
1674 ctsio->ext_data_filled += len_copied;
1676 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1677 "kern_sg_entries: %d\n", ext_sg_entries,
1679 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1680 "kern_data_len = %d\n", ctsio->ext_data_len,
1681 ctsio->kern_data_len));
1684 /* XXX KDM set residual?? */
1687 if (ext_sglist_malloced != 0)
1688 free(ext_sglist, M_CTL);
1690 return (CTL_RETVAL_COMPLETE);
1694 * Serialize a command that went down the "wrong" side, and so was sent to
1695 * this controller for execution. The logic is a little different than the
1696 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1697 * sent back to the other side, but in the success case, we execute the
1698 * command on this side (XFER mode) or tell the other side to execute it
1702 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1704 struct ctl_softc *ctl_softc;
1705 union ctl_ha_msg msg_info;
1706 struct ctl_lun *lun;
1710 ctl_softc = control_softc;
1712 mtx_lock(&ctl_softc->ctl_lock);
1714 targ_lun = ctsio->io_hdr.nexus.targ_lun;
1715 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
1716 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
1717 lun = ctl_softc->ctl_luns[targ_lun];
1721 * Why isn't LUN defined? The other side wouldn't
1722 * send a cmd if the LUN is undefined.
1724 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1726 /* "Logical unit not supported" */
1727 ctl_set_sense_data(&msg_info.scsi.sense_data,
1729 /*sense_format*/SSD_TYPE_NONE,
1730 /*current_error*/ 1,
1731 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1736 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1737 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1738 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1739 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1740 msg_info.hdr.serializing_sc = NULL;
1741 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1742 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1743 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1746 mtx_unlock(&ctl_softc->ctl_lock);
1751 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1753 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1754 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1756 case CTL_ACTION_BLOCK:
1757 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1758 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1761 case CTL_ACTION_PASS:
1762 case CTL_ACTION_SKIP:
1763 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1764 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1765 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1766 &ctsio->io_hdr, links);
1769 /* send msg back to other side */
1770 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1771 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1772 msg_info.hdr.msg_type = CTL_MSG_R2R;
1774 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1776 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1777 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1781 case CTL_ACTION_OVERLAP:
1782 /* OVERLAPPED COMMANDS ATTEMPTED */
1783 ctl_set_sense_data(&msg_info.scsi.sense_data,
1785 /*sense_format*/SSD_TYPE_NONE,
1786 /*current_error*/ 1,
1787 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1792 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1793 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1794 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1795 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1796 msg_info.hdr.serializing_sc = NULL;
1797 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1799 printf("BAD JUJU:Major Bummer Overlap\n");
1801 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1803 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1804 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1807 case CTL_ACTION_OVERLAP_TAG:
1808 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1809 ctl_set_sense_data(&msg_info.scsi.sense_data,
1811 /*sense_format*/SSD_TYPE_NONE,
1812 /*current_error*/ 1,
1813 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1815 /*ascq*/ ctsio->tag_num & 0xff,
1818 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1819 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1820 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1821 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1822 msg_info.hdr.serializing_sc = NULL;
1823 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1825 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1827 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1829 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1830 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1833 case CTL_ACTION_ERROR:
1835 /* "Internal target failure" */
1836 ctl_set_sense_data(&msg_info.scsi.sense_data,
1838 /*sense_format*/SSD_TYPE_NONE,
1839 /*current_error*/ 1,
1840 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1845 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1846 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1847 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1848 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1849 msg_info.hdr.serializing_sc = NULL;
1850 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1852 printf("BAD JUJU:Major Bummer HW Error\n");
1854 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1856 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1857 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1862 mtx_unlock(&ctl_softc->ctl_lock);
1867 ctl_ioctl_submit_wait(union ctl_io *io)
1869 struct ctl_fe_ioctl_params params;
1870 ctl_fe_ioctl_state last_state;
1875 bzero(¶ms, sizeof(params));
1877 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1878 cv_init(¶ms.sem, "ctlioccv");
1879 params.state = CTL_IOCTL_INPROG;
1880 last_state = params.state;
1882 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1884 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1886 /* This shouldn't happen */
1887 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1893 mtx_lock(¶ms.ioctl_mtx);
1895 * Check the state here, and don't sleep if the state has
1896 * already changed (i.e. wakeup has already occured, but we
1897 * weren't waiting yet).
1899 if (params.state == last_state) {
1900 /* XXX KDM cv_wait_sig instead? */
1901 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1903 last_state = params.state;
1905 switch (params.state) {
1906 case CTL_IOCTL_INPROG:
1907 /* Why did we wake up? */
1908 /* XXX KDM error here? */
1909 mtx_unlock(¶ms.ioctl_mtx);
1911 case CTL_IOCTL_DATAMOVE:
1912 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1915 * change last_state back to INPROG to avoid
1916 * deadlock on subsequent data moves.
1918 params.state = last_state = CTL_IOCTL_INPROG;
1920 mtx_unlock(¶ms.ioctl_mtx);
1921 ctl_ioctl_do_datamove(&io->scsiio);
1923 * Note that in some cases, most notably writes,
1924 * this will queue the I/O and call us back later.
1925 * In other cases, generally reads, this routine
1926 * will immediately call back and wake us up,
1927 * probably using our own context.
1929 io->scsiio.be_move_done(io);
1931 case CTL_IOCTL_DONE:
1932 mtx_unlock(¶ms.ioctl_mtx);
1933 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1937 mtx_unlock(¶ms.ioctl_mtx);
1938 /* XXX KDM error here? */
1941 } while (done == 0);
1943 mtx_destroy(¶ms.ioctl_mtx);
1944 cv_destroy(¶ms.sem);
1946 return (CTL_RETVAL_COMPLETE);
1950 ctl_ioctl_datamove(union ctl_io *io)
1952 struct ctl_fe_ioctl_params *params;
1954 params = (struct ctl_fe_ioctl_params *)
1955 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1957 mtx_lock(¶ms->ioctl_mtx);
1958 params->state = CTL_IOCTL_DATAMOVE;
1959 cv_broadcast(¶ms->sem);
1960 mtx_unlock(¶ms->ioctl_mtx);
1964 ctl_ioctl_done(union ctl_io *io)
1966 struct ctl_fe_ioctl_params *params;
1968 params = (struct ctl_fe_ioctl_params *)
1969 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1971 mtx_lock(¶ms->ioctl_mtx);
1972 params->state = CTL_IOCTL_DONE;
1973 cv_broadcast(¶ms->sem);
1974 mtx_unlock(¶ms->ioctl_mtx);
1978 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1980 struct ctl_fe_ioctl_startstop_info *sd_info;
1982 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1984 sd_info->hs_info.status = metatask->status;
1985 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1986 sd_info->hs_info.luns_complete =
1987 metatask->taskinfo.startstop.luns_complete;
1988 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1990 cv_broadcast(&sd_info->sem);
1994 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1996 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1998 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2000 mtx_lock(fe_bbr_info->lock);
2001 fe_bbr_info->bbr_info->status = metatask->status;
2002 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2003 fe_bbr_info->wakeup_done = 1;
2004 mtx_unlock(fe_bbr_info->lock);
2006 cv_broadcast(&fe_bbr_info->sem);
2010 * Returns 0 for success, errno for failure.
2013 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2014 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2021 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
2023 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2024 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2026 struct ctl_ooa_entry *entry;
2029 * If we've got more than we can fit, just count the
2030 * remaining entries.
2032 if (*cur_fill_num >= ooa_hdr->alloc_num)
2035 entry = &kern_entries[*cur_fill_num];
2037 entry->tag_num = io->scsiio.tag_num;
2038 entry->lun_num = lun->lun;
2040 entry->start_bt = io->io_hdr.start_bt;
2042 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2043 entry->cdb_len = io->scsiio.cdb_len;
2044 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2045 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2047 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2048 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2050 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2051 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2053 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2054 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2056 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2057 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2064 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2065 size_t error_str_len)
2069 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2071 if (copyin(user_addr, kptr, len) != 0) {
2072 snprintf(error_str, error_str_len, "Error copying %d bytes "
2073 "from user address %p to kernel address %p", len,
2083 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2087 if (be_args == NULL)
2090 for (i = 0; i < num_be_args; i++) {
2091 free(be_args[i].kname, M_CTL);
2092 free(be_args[i].kvalue, M_CTL);
2095 free(be_args, M_CTL);
2098 static struct ctl_be_arg *
2099 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2100 char *error_str, size_t error_str_len)
2102 struct ctl_be_arg *args;
2105 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2106 error_str, error_str_len);
2111 for (i = 0; i < num_be_args; i++) {
2112 args[i].kname = NULL;
2113 args[i].kvalue = NULL;
2116 for (i = 0; i < num_be_args; i++) {
2119 args[i].kname = ctl_copyin_alloc(args[i].name,
2120 args[i].namelen, error_str, error_str_len);
2121 if (args[i].kname == NULL)
2124 if (args[i].kname[args[i].namelen - 1] != '\0') {
2125 snprintf(error_str, error_str_len, "Argument %d "
2126 "name is not NUL-terminated", i);
2130 args[i].kvalue = NULL;
2132 tmpptr = ctl_copyin_alloc(args[i].value,
2133 args[i].vallen, error_str, error_str_len);
2137 args[i].kvalue = tmpptr;
2139 if ((args[i].flags & CTL_BEARG_ASCII)
2140 && (tmpptr[args[i].vallen - 1] != '\0')) {
2141 snprintf(error_str, error_str_len, "Argument %d "
2142 "value is not NUL-terminated", i);
2150 ctl_free_args(num_be_args, args);
2156 * Escape characters that are illegal or not recommended in XML.
2159 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2165 for (; *str; str++) {
2168 retval = sbuf_printf(sb, "&");
2171 retval = sbuf_printf(sb, ">");
2174 retval = sbuf_printf(sb, "<");
2177 retval = sbuf_putc(sb, *str);
2190 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2193 struct ctl_softc *softc;
2196 softc = control_softc;
2206 * If we haven't been "enabled", don't allow any SCSI I/O
2209 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2214 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2216 printf("ctl_ioctl: can't allocate ctl_io!\n");
2222 * Need to save the pool reference so it doesn't get
2223 * spammed by the user's ctl_io.
2225 pool_tmp = io->io_hdr.pool;
2227 memcpy(io, (void *)addr, sizeof(*io));
2229 io->io_hdr.pool = pool_tmp;
2231 * No status yet, so make sure the status is set properly.
2233 io->io_hdr.status = CTL_STATUS_NONE;
2236 * The user sets the initiator ID, target and LUN IDs.
2238 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2239 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2240 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2241 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2242 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2244 retval = ctl_ioctl_submit_wait(io);
2251 memcpy((void *)addr, io, sizeof(*io));
2253 /* return this to our pool */
2258 case CTL_ENABLE_PORT:
2259 case CTL_DISABLE_PORT:
2260 case CTL_SET_PORT_WWNS: {
2261 struct ctl_frontend *fe;
2262 struct ctl_port_entry *entry;
2264 entry = (struct ctl_port_entry *)addr;
2266 mtx_lock(&softc->ctl_lock);
2267 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2273 if ((entry->port_type == CTL_PORT_NONE)
2274 && (entry->targ_port == fe->targ_port)) {
2276 * If the user only wants to enable or
2277 * disable or set WWNs on a specific port,
2278 * do the operation and we're done.
2282 } else if (entry->port_type & fe->port_type) {
2284 * Compare the user's type mask with the
2285 * particular frontend type to see if we
2292 * Make sure the user isn't trying to set
2293 * WWNs on multiple ports at the same time.
2295 if (cmd == CTL_SET_PORT_WWNS) {
2296 printf("%s: Can't set WWNs on "
2297 "multiple ports\n", __func__);
2304 * XXX KDM we have to drop the lock here,
2305 * because the online/offline operations
2306 * can potentially block. We need to
2307 * reference count the frontends so they
2310 mtx_unlock(&softc->ctl_lock);
2312 if (cmd == CTL_ENABLE_PORT) {
2313 struct ctl_lun *lun;
2315 STAILQ_FOREACH(lun, &softc->lun_list,
2317 fe->lun_enable(fe->targ_lun_arg,
2322 ctl_frontend_online(fe);
2323 } else if (cmd == CTL_DISABLE_PORT) {
2324 struct ctl_lun *lun;
2326 ctl_frontend_offline(fe);
2328 STAILQ_FOREACH(lun, &softc->lun_list,
2337 mtx_lock(&softc->ctl_lock);
2339 if (cmd == CTL_SET_PORT_WWNS)
2340 ctl_frontend_set_wwns(fe,
2341 (entry->flags & CTL_PORT_WWNN_VALID) ?
2343 (entry->flags & CTL_PORT_WWPN_VALID) ?
2344 1 : 0, entry->wwpn);
2349 mtx_unlock(&softc->ctl_lock);
2352 case CTL_GET_PORT_LIST: {
2353 struct ctl_frontend *fe;
2354 struct ctl_port_list *list;
2357 list = (struct ctl_port_list *)addr;
2359 if (list->alloc_len != (list->alloc_num *
2360 sizeof(struct ctl_port_entry))) {
2361 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2362 "alloc_num %u * sizeof(struct ctl_port_entry) "
2363 "%zu\n", __func__, list->alloc_len,
2364 list->alloc_num, sizeof(struct ctl_port_entry));
2370 list->dropped_num = 0;
2372 mtx_lock(&softc->ctl_lock);
2373 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2374 struct ctl_port_entry entry, *list_entry;
2376 if (list->fill_num >= list->alloc_num) {
2377 list->dropped_num++;
2381 entry.port_type = fe->port_type;
2382 strlcpy(entry.port_name, fe->port_name,
2383 sizeof(entry.port_name));
2384 entry.targ_port = fe->targ_port;
2385 entry.physical_port = fe->physical_port;
2386 entry.virtual_port = fe->virtual_port;
2387 entry.wwnn = fe->wwnn;
2388 entry.wwpn = fe->wwpn;
2389 if (fe->status & CTL_PORT_STATUS_ONLINE)
2394 list_entry = &list->entries[i];
2396 retval = copyout(&entry, list_entry, sizeof(entry));
2398 printf("%s: CTL_GET_PORT_LIST: copyout "
2399 "returned %d\n", __func__, retval);
2404 list->fill_len += sizeof(entry);
2406 mtx_unlock(&softc->ctl_lock);
2409 * If this is non-zero, we had a copyout fault, so there's
2410 * probably no point in attempting to set the status inside
2416 if (list->dropped_num > 0)
2417 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2419 list->status = CTL_PORT_LIST_OK;
2422 case CTL_DUMP_OOA: {
2423 struct ctl_lun *lun;
2428 mtx_lock(&softc->ctl_lock);
2429 printf("Dumping OOA queues:\n");
2430 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2431 for (io = (union ctl_io *)TAILQ_FIRST(
2432 &lun->ooa_queue); io != NULL;
2433 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2435 sbuf_new(&sb, printbuf, sizeof(printbuf),
2437 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2441 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2443 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2445 CTL_FLAG_ABORT) ? " ABORT" : "",
2447 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2448 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2450 printf("%s\n", sbuf_data(&sb));
2453 printf("OOA queues dump done\n");
2454 mtx_unlock(&softc->ctl_lock);
2458 struct ctl_lun *lun;
2459 struct ctl_ooa *ooa_hdr;
2460 struct ctl_ooa_entry *entries;
2461 uint32_t cur_fill_num;
2463 ooa_hdr = (struct ctl_ooa *)addr;
2465 if ((ooa_hdr->alloc_len == 0)
2466 || (ooa_hdr->alloc_num == 0)) {
2467 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2468 "must be non-zero\n", __func__,
2469 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2474 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2475 sizeof(struct ctl_ooa_entry))) {
2476 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2477 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2478 __func__, ooa_hdr->alloc_len,
2479 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2484 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2485 if (entries == NULL) {
2486 printf("%s: could not allocate %d bytes for OOA "
2487 "dump\n", __func__, ooa_hdr->alloc_len);
2492 mtx_lock(&softc->ctl_lock);
2493 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2494 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2495 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2496 mtx_unlock(&softc->ctl_lock);
2497 free(entries, M_CTL);
2498 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2499 __func__, (uintmax_t)ooa_hdr->lun_num);
2506 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2507 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2508 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2514 mtx_unlock(&softc->ctl_lock);
2515 free(entries, M_CTL);
2519 lun = softc->ctl_luns[ooa_hdr->lun_num];
2521 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2524 mtx_unlock(&softc->ctl_lock);
2526 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2527 ooa_hdr->fill_len = ooa_hdr->fill_num *
2528 sizeof(struct ctl_ooa_entry);
2529 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2531 printf("%s: error copying out %d bytes for OOA dump\n",
2532 __func__, ooa_hdr->fill_len);
2535 getbintime(&ooa_hdr->cur_bt);
2537 if (cur_fill_num > ooa_hdr->alloc_num) {
2538 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2539 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2541 ooa_hdr->dropped_num = 0;
2542 ooa_hdr->status = CTL_OOA_OK;
2545 free(entries, M_CTL);
2548 case CTL_CHECK_OOA: {
2550 struct ctl_lun *lun;
2551 struct ctl_ooa_info *ooa_info;
2554 ooa_info = (struct ctl_ooa_info *)addr;
2556 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2557 ooa_info->status = CTL_OOA_INVALID_LUN;
2560 mtx_lock(&softc->ctl_lock);
2561 lun = softc->ctl_luns[ooa_info->lun_id];
2563 mtx_unlock(&softc->ctl_lock);
2564 ooa_info->status = CTL_OOA_INVALID_LUN;
2568 ooa_info->num_entries = 0;
2569 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2570 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2571 &io->io_hdr, ooa_links)) {
2572 ooa_info->num_entries++;
2575 mtx_unlock(&softc->ctl_lock);
2576 ooa_info->status = CTL_OOA_SUCCESS;
2580 case CTL_HARD_START:
2581 case CTL_HARD_STOP: {
2582 struct ctl_fe_ioctl_startstop_info ss_info;
2583 struct cfi_metatask *metatask;
2586 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2588 cv_init(&ss_info.sem, "hard start/stop cv" );
2590 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2591 if (metatask == NULL) {
2593 mtx_destroy(&hs_mtx);
2597 if (cmd == CTL_HARD_START)
2598 metatask->tasktype = CFI_TASK_STARTUP;
2600 metatask->tasktype = CFI_TASK_SHUTDOWN;
2602 metatask->callback = ctl_ioctl_hard_startstop_callback;
2603 metatask->callback_arg = &ss_info;
2605 cfi_action(metatask);
2607 /* Wait for the callback */
2609 cv_wait_sig(&ss_info.sem, &hs_mtx);
2610 mtx_unlock(&hs_mtx);
2613 * All information has been copied from the metatask by the
2614 * time cv_broadcast() is called, so we free the metatask here.
2616 cfi_free_metatask(metatask);
2618 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2620 mtx_destroy(&hs_mtx);
2624 struct ctl_bbrread_info *bbr_info;
2625 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2627 struct cfi_metatask *metatask;
2629 bbr_info = (struct ctl_bbrread_info *)addr;
2631 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2633 bzero(&bbr_mtx, sizeof(bbr_mtx));
2634 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2636 fe_bbr_info.bbr_info = bbr_info;
2637 fe_bbr_info.lock = &bbr_mtx;
2639 cv_init(&fe_bbr_info.sem, "BBR read cv");
2640 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2642 if (metatask == NULL) {
2643 mtx_destroy(&bbr_mtx);
2644 cv_destroy(&fe_bbr_info.sem);
2648 metatask->tasktype = CFI_TASK_BBRREAD;
2649 metatask->callback = ctl_ioctl_bbrread_callback;
2650 metatask->callback_arg = &fe_bbr_info;
2651 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2652 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2653 metatask->taskinfo.bbrread.len = bbr_info->len;
2655 cfi_action(metatask);
2658 while (fe_bbr_info.wakeup_done == 0)
2659 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2660 mtx_unlock(&bbr_mtx);
2662 bbr_info->status = metatask->status;
2663 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2664 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2665 memcpy(&bbr_info->sense_data,
2666 &metatask->taskinfo.bbrread.sense_data,
2667 ctl_min(sizeof(bbr_info->sense_data),
2668 sizeof(metatask->taskinfo.bbrread.sense_data)));
2670 cfi_free_metatask(metatask);
2672 mtx_destroy(&bbr_mtx);
2673 cv_destroy(&fe_bbr_info.sem);
2677 case CTL_DELAY_IO: {
2678 struct ctl_io_delay_info *delay_info;
2680 struct ctl_lun *lun;
2681 #endif /* CTL_IO_DELAY */
2683 delay_info = (struct ctl_io_delay_info *)addr;
2686 mtx_lock(&softc->ctl_lock);
2688 if ((delay_info->lun_id > CTL_MAX_LUNS)
2689 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2690 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2692 lun = softc->ctl_luns[delay_info->lun_id];
2694 delay_info->status = CTL_DELAY_STATUS_OK;
2696 switch (delay_info->delay_type) {
2697 case CTL_DELAY_TYPE_CONT:
2699 case CTL_DELAY_TYPE_ONESHOT:
2702 delay_info->status =
2703 CTL_DELAY_STATUS_INVALID_TYPE;
2707 switch (delay_info->delay_loc) {
2708 case CTL_DELAY_LOC_DATAMOVE:
2709 lun->delay_info.datamove_type =
2710 delay_info->delay_type;
2711 lun->delay_info.datamove_delay =
2712 delay_info->delay_secs;
2714 case CTL_DELAY_LOC_DONE:
2715 lun->delay_info.done_type =
2716 delay_info->delay_type;
2717 lun->delay_info.done_delay =
2718 delay_info->delay_secs;
2721 delay_info->status =
2722 CTL_DELAY_STATUS_INVALID_LOC;
2727 mtx_unlock(&softc->ctl_lock);
2729 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2730 #endif /* CTL_IO_DELAY */
2733 case CTL_REALSYNC_SET: {
2736 syncstate = (int *)addr;
2738 mtx_lock(&softc->ctl_lock);
2739 switch (*syncstate) {
2741 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2744 softc->flags |= CTL_FLAG_REAL_SYNC;
2750 mtx_unlock(&softc->ctl_lock);
2753 case CTL_REALSYNC_GET: {
2756 syncstate = (int*)addr;
2758 mtx_lock(&softc->ctl_lock);
2759 if (softc->flags & CTL_FLAG_REAL_SYNC)
2763 mtx_unlock(&softc->ctl_lock);
2769 struct ctl_sync_info *sync_info;
2770 struct ctl_lun *lun;
2772 sync_info = (struct ctl_sync_info *)addr;
2774 mtx_lock(&softc->ctl_lock);
2775 lun = softc->ctl_luns[sync_info->lun_id];
2777 mtx_unlock(&softc->ctl_lock);
2778 sync_info->status = CTL_GS_SYNC_NO_LUN;
2781 * Get or set the sync interval. We're not bounds checking
2782 * in the set case, hopefully the user won't do something
2785 if (cmd == CTL_GETSYNC)
2786 sync_info->sync_interval = lun->sync_interval;
2788 lun->sync_interval = sync_info->sync_interval;
2790 mtx_unlock(&softc->ctl_lock);
2792 sync_info->status = CTL_GS_SYNC_OK;
2796 case CTL_GETSTATS: {
2797 struct ctl_stats *stats;
2798 struct ctl_lun *lun;
2801 stats = (struct ctl_stats *)addr;
2803 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2805 stats->status = CTL_SS_NEED_MORE_SPACE;
2806 stats->num_luns = softc->num_luns;
2810 * XXX KDM no locking here. If the LUN list changes,
2811 * things can blow up.
2813 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2814 i++, lun = STAILQ_NEXT(lun, links)) {
2815 retval = copyout(&lun->stats, &stats->lun_stats[i],
2816 sizeof(lun->stats));
2820 stats->num_luns = softc->num_luns;
2821 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2823 stats->status = CTL_SS_OK;
2825 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2827 stats->flags = CTL_STATS_FLAG_NONE;
2829 getnanouptime(&stats->timestamp);
2832 case CTL_ERROR_INJECT: {
2833 struct ctl_error_desc *err_desc, *new_err_desc;
2834 struct ctl_lun *lun;
2836 err_desc = (struct ctl_error_desc *)addr;
2838 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2840 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2842 mtx_lock(&softc->ctl_lock);
2843 lun = softc->ctl_luns[err_desc->lun_id];
2845 mtx_unlock(&softc->ctl_lock);
2846 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2847 __func__, (uintmax_t)err_desc->lun_id);
2853 * We could do some checking here to verify the validity
2854 * of the request, but given the complexity of error
2855 * injection requests, the checking logic would be fairly
2858 * For now, if the request is invalid, it just won't get
2859 * executed and might get deleted.
2861 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2864 * XXX KDM check to make sure the serial number is unique,
2865 * in case we somehow manage to wrap. That shouldn't
2866 * happen for a very long time, but it's the right thing to
2869 new_err_desc->serial = lun->error_serial;
2870 err_desc->serial = lun->error_serial;
2871 lun->error_serial++;
2873 mtx_unlock(&softc->ctl_lock);
2876 case CTL_ERROR_INJECT_DELETE: {
2877 struct ctl_error_desc *delete_desc, *desc, *desc2;
2878 struct ctl_lun *lun;
2881 delete_desc = (struct ctl_error_desc *)addr;
2884 mtx_lock(&softc->ctl_lock);
2885 lun = softc->ctl_luns[delete_desc->lun_id];
2887 mtx_unlock(&softc->ctl_lock);
2888 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2889 __func__, (uintmax_t)delete_desc->lun_id);
2893 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2894 if (desc->serial != delete_desc->serial)
2897 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2902 mtx_unlock(&softc->ctl_lock);
2903 if (delete_done == 0) {
2904 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2905 "error serial %ju on LUN %u\n", __func__,
2906 delete_desc->serial, delete_desc->lun_id);
2912 case CTL_DUMP_STRUCTS: {
2914 struct ctl_frontend *fe;
2916 printf("CTL IID to WWPN map start:\n");
2917 for (i = 0; i < CTL_MAX_PORTS; i++) {
2918 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2919 if (softc->wwpn_iid[i][j].in_use == 0)
2922 printf("port %d iid %u WWPN %#jx\n",
2923 softc->wwpn_iid[i][j].port,
2924 softc->wwpn_iid[i][j].iid,
2925 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2928 printf("CTL IID to WWPN map end\n");
2929 printf("CTL Persistent Reservation information start:\n");
2930 for (i = 0; i < CTL_MAX_LUNS; i++) {
2931 struct ctl_lun *lun;
2933 lun = softc->ctl_luns[i];
2936 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2939 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2940 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2941 if (lun->per_res[j+k].registered == 0)
2943 printf("LUN %d port %d iid %d key "
2945 (uintmax_t)scsi_8btou64(
2946 lun->per_res[j+k].res_key.key));
2950 printf("CTL Persistent Reservation information end\n");
2951 printf("CTL Frontends:\n");
2953 * XXX KDM calling this without a lock. We'd likely want
2954 * to drop the lock before calling the frontend's dump
2957 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2958 printf("Frontend %s Type %u pport %d vport %d WWNN "
2959 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2960 fe->physical_port, fe->virtual_port,
2961 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2964 * Frontends are not required to support the dump
2967 if (fe->fe_dump == NULL)
2972 printf("CTL Frontend information end\n");
2976 struct ctl_lun_req *lun_req;
2977 struct ctl_backend_driver *backend;
2979 lun_req = (struct ctl_lun_req *)addr;
2981 backend = ctl_backend_find(lun_req->backend);
2982 if (backend == NULL) {
2983 lun_req->status = CTL_LUN_ERROR;
2984 snprintf(lun_req->error_str,
2985 sizeof(lun_req->error_str),
2986 "Backend \"%s\" not found.",
2990 if (lun_req->num_be_args > 0) {
2991 lun_req->kern_be_args = ctl_copyin_args(
2992 lun_req->num_be_args,
2995 sizeof(lun_req->error_str));
2996 if (lun_req->kern_be_args == NULL) {
2997 lun_req->status = CTL_LUN_ERROR;
3002 retval = backend->ioctl(dev, cmd, addr, flag, td);
3004 if (lun_req->num_be_args > 0) {
3005 ctl_free_args(lun_req->num_be_args,
3006 lun_req->kern_be_args);
3010 case CTL_LUN_LIST: {
3012 struct ctl_lun *lun;
3013 struct ctl_lun_list *list;
3014 struct ctl_be_lun_option *opt;
3016 list = (struct ctl_lun_list *)addr;
3019 * Allocate a fixed length sbuf here, based on the length
3020 * of the user's buffer. We could allocate an auto-extending
3021 * buffer, and then tell the user how much larger our
3022 * amount of data is than his buffer, but that presents
3025 * 1. The sbuf(9) routines use a blocking malloc, and so
3026 * we can't hold a lock while calling them with an
3027 * auto-extending buffer.
3029 * 2. There is not currently a LUN reference counting
3030 * mechanism, outside of outstanding transactions on
3031 * the LUN's OOA queue. So a LUN could go away on us
3032 * while we're getting the LUN number, backend-specific
3033 * information, etc. Thus, given the way things
3034 * currently work, we need to hold the CTL lock while
3035 * grabbing LUN information.
3037 * So, from the user's standpoint, the best thing to do is
3038 * allocate what he thinks is a reasonable buffer length,
3039 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3040 * double the buffer length and try again. (And repeat
3041 * that until he succeeds.)
3043 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3045 list->status = CTL_LUN_LIST_ERROR;
3046 snprintf(list->error_str, sizeof(list->error_str),
3047 "Unable to allocate %d bytes for LUN list",
3052 sbuf_printf(sb, "<ctllunlist>\n");
3054 mtx_lock(&softc->ctl_lock);
3056 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3057 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3058 (uintmax_t)lun->lun);
3061 * Bail out as soon as we see that we've overfilled
3067 retval = sbuf_printf(sb, "<backend_type>%s"
3068 "</backend_type>\n",
3069 (lun->backend == NULL) ? "none" :
3070 lun->backend->name);
3075 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3076 lun->be_lun->lun_type);
3081 if (lun->backend == NULL) {
3082 retval = sbuf_printf(sb, "</lun>\n");
3088 retval = sbuf_printf(sb, "<size>%ju</size>\n",
3089 (lun->be_lun->maxlba > 0) ?
3090 lun->be_lun->maxlba + 1 : 0);
3095 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3096 lun->be_lun->blocksize);
3101 retval = sbuf_printf(sb, "<serial_number>");
3106 retval = ctl_sbuf_printf_esc(sb,
3107 lun->be_lun->serial_num);
3112 retval = sbuf_printf(sb, "</serial_number>\n");
3117 retval = sbuf_printf(sb, "<device_id>");
3122 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3127 retval = sbuf_printf(sb, "</device_id>\n");
3132 if (lun->backend->lun_info != NULL) {
3133 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3137 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3138 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name);
3143 retval = sbuf_printf(sb, "</lun>\n");
3148 mtx_unlock(&softc->ctl_lock);
3151 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3154 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3155 snprintf(list->error_str, sizeof(list->error_str),
3156 "Out of space, %d bytes is too small",
3163 retval = copyout(sbuf_data(sb), list->lun_xml,
3166 list->fill_len = sbuf_len(sb) + 1;
3167 list->status = CTL_LUN_LIST_OK;
3172 struct ctl_iscsi *ci;
3173 struct ctl_frontend *fe;
3175 ci = (struct ctl_iscsi *)addr;
3177 mtx_lock(&softc->ctl_lock);
3178 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3179 if (strcmp(fe->port_name, "iscsi") == 0)
3182 mtx_unlock(&softc->ctl_lock);
3185 ci->status = CTL_ISCSI_ERROR;
3186 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found.");
3190 retval = fe->ioctl(dev, cmd, addr, flag, td);
3194 /* XXX KDM should we fix this? */
3196 struct ctl_backend_driver *backend;
3203 * We encode the backend type as the ioctl type for backend
3204 * ioctls. So parse it out here, and then search for a
3205 * backend of this type.
3207 type = _IOC_TYPE(cmd);
3209 STAILQ_FOREACH(backend, &softc->be_list, links) {
3210 if (backend->type == type) {
3216 printf("ctl: unknown ioctl command %#lx or backend "
3221 retval = backend->ioctl(dev, cmd, addr, flag, td);
3231 ctl_get_initindex(struct ctl_nexus *nexus)
3233 if (nexus->targ_port < CTL_MAX_PORTS)
3234 return (nexus->initid.id +
3235 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3237 return (nexus->initid.id +
3238 ((nexus->targ_port - CTL_MAX_PORTS) *
3239 CTL_MAX_INIT_PER_PORT));
3243 ctl_get_resindex(struct ctl_nexus *nexus)
3245 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3249 ctl_port_idx(int port_num)
3251 if (port_num < CTL_MAX_PORTS)
3254 return(port_num - CTL_MAX_PORTS);
3258 * Note: This only works for bitmask sizes that are at least 32 bits, and
3259 * that are a power of 2.
3262 ctl_ffz(uint32_t *mask, uint32_t size)
3264 uint32_t num_chunks, num_pieces;
3267 num_chunks = (size >> 5);
3268 if (num_chunks == 0)
3270 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3272 for (i = 0; i < num_chunks; i++) {
3273 for (j = 0; j < num_pieces; j++) {
3274 if ((mask[i] & (1 << j)) == 0)
3275 return ((i << 5) + j);
3283 ctl_set_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_clear_mask(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)
3309 mask[chunk] &= ~(1 << piece);
3315 ctl_is_set(uint32_t *mask, uint32_t bit)
3317 uint32_t chunk, piece;
3320 piece = bit % (sizeof(uint32_t) * 8);
3322 if ((mask[chunk] & (1 << piece)) == 0)
3330 * The bus, target and lun are optional, they can be filled in later.
3331 * can_wait is used to determine whether we can wait on the malloc or not.
3334 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3335 uint32_t targ_lun, int can_wait)
3340 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3342 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3345 io->io_hdr.io_type = io_type;
3346 io->io_hdr.targ_port = targ_port;
3348 * XXX KDM this needs to change/go away. We need to move
3349 * to a preallocated pool of ctl_scsiio structures.
3351 io->io_hdr.nexus.targ_target.id = targ_target;
3352 io->io_hdr.nexus.targ_lun = targ_lun;
3359 ctl_kfree_io(union ctl_io *io)
3366 * ctl_softc, pool_type, total_ctl_io are passed in.
3367 * npool is passed out.
3370 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3371 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3374 union ctl_io *cur_io, *next_io;
3375 struct ctl_io_pool *pool;
3380 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3387 pool->type = pool_type;
3388 pool->ctl_softc = ctl_softc;
3390 mtx_lock(&ctl_softc->pool_lock);
3391 pool->id = ctl_softc->cur_pool_id++;
3392 mtx_unlock(&ctl_softc->pool_lock);
3394 pool->flags = CTL_POOL_FLAG_NONE;
3395 pool->refcount = 1; /* Reference for validity. */
3396 STAILQ_INIT(&pool->free_queue);
3399 * XXX KDM other options here:
3400 * - allocate a page at a time
3401 * - allocate one big chunk of memory.
3402 * Page allocation might work well, but would take a little more
3405 for (i = 0; i < total_ctl_io; i++) {
3406 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3408 if (cur_io == NULL) {
3412 cur_io->io_hdr.pool = pool;
3413 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3414 pool->total_ctl_io++;
3415 pool->free_ctl_io++;
3419 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3420 cur_io != NULL; cur_io = next_io) {
3421 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3423 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3425 free(cur_io, M_CTL);
3431 mtx_lock(&ctl_softc->pool_lock);
3432 ctl_softc->num_pools++;
3433 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3435 * Increment our usage count if this is an external consumer, so we
3436 * can't get unloaded until the external consumer (most likely a
3437 * FETD) unloads and frees his pool.
3439 * XXX KDM will this increment the caller's module use count, or
3443 if ((pool_type != CTL_POOL_EMERGENCY)
3444 && (pool_type != CTL_POOL_INTERNAL)
3445 && (pool_type != CTL_POOL_IOCTL)
3446 && (pool_type != CTL_POOL_4OTHERSC))
3450 mtx_unlock(&ctl_softc->pool_lock);
3460 ctl_pool_acquire(struct ctl_io_pool *pool)
3463 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3465 if (pool->flags & CTL_POOL_FLAG_INVALID)
3474 ctl_pool_release(struct ctl_io_pool *pool)
3476 struct ctl_softc *ctl_softc = pool->ctl_softc;
3479 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3481 if (--pool->refcount != 0)
3484 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3485 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3490 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3491 ctl_softc->num_pools--;
3494 * XXX KDM will this decrement the caller's usage count or mine?
3497 if ((pool->type != CTL_POOL_EMERGENCY)
3498 && (pool->type != CTL_POOL_INTERNAL)
3499 && (pool->type != CTL_POOL_IOCTL))
3507 ctl_pool_free(struct ctl_io_pool *pool)
3509 struct ctl_softc *ctl_softc;
3514 ctl_softc = pool->ctl_softc;
3515 mtx_lock(&ctl_softc->pool_lock);
3516 pool->flags |= CTL_POOL_FLAG_INVALID;
3517 ctl_pool_release(pool);
3518 mtx_unlock(&ctl_softc->pool_lock);
3522 * This routine does not block (except for spinlocks of course).
3523 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3527 ctl_alloc_io(void *pool_ref)
3530 struct ctl_softc *ctl_softc;
3531 struct ctl_io_pool *pool, *npool;
3532 struct ctl_io_pool *emergency_pool;
3534 pool = (struct ctl_io_pool *)pool_ref;
3537 printf("%s: pool is NULL\n", __func__);
3541 emergency_pool = NULL;
3543 ctl_softc = pool->ctl_softc;
3545 mtx_lock(&ctl_softc->pool_lock);
3547 * First, try to get the io structure from the user's pool.
3549 if (ctl_pool_acquire(pool) == 0) {
3550 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3552 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3553 pool->total_allocated++;
3554 pool->free_ctl_io--;
3555 mtx_unlock(&ctl_softc->pool_lock);
3558 ctl_pool_release(pool);
3561 * If he doesn't have any io structures left, search for an
3562 * emergency pool and grab one from there.
3564 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3565 if (npool->type != CTL_POOL_EMERGENCY)
3568 if (ctl_pool_acquire(npool) != 0)
3571 emergency_pool = npool;
3573 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3575 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3576 npool->total_allocated++;
3577 npool->free_ctl_io--;
3578 mtx_unlock(&ctl_softc->pool_lock);
3581 ctl_pool_release(npool);
3584 /* Drop the spinlock before we malloc */
3585 mtx_unlock(&ctl_softc->pool_lock);
3588 * The emergency pool (if it exists) didn't have one, so try an
3589 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3591 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3594 * If the emergency pool exists but is empty, add this
3595 * ctl_io to its list when it gets freed.
3597 if (emergency_pool != NULL) {
3598 mtx_lock(&ctl_softc->pool_lock);
3599 if (ctl_pool_acquire(emergency_pool) == 0) {
3600 io->io_hdr.pool = emergency_pool;
3601 emergency_pool->total_ctl_io++;
3603 * Need to bump this, otherwise
3604 * total_allocated and total_freed won't
3605 * match when we no longer have anything
3608 emergency_pool->total_allocated++;
3610 mtx_unlock(&ctl_softc->pool_lock);
3612 io->io_hdr.pool = NULL;
3619 ctl_free_io(union ctl_io *io)
3625 * If this ctl_io has a pool, return it to that pool.
3627 if (io->io_hdr.pool != NULL) {
3628 struct ctl_io_pool *pool;
3630 struct ctl_softc *ctl_softc;
3631 union ctl_io *tmp_io;
3632 unsigned long xflags;
3635 ctl_softc = control_softc;
3638 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3640 mtx_lock(&pool->ctl_softc->pool_lock);
3644 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3645 &ctl_softc->task_queue); tmp_io != NULL; i++,
3646 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3649 printf("%s: %p is still on the task queue!\n",
3651 printf("%s: (%d): type %d "
3652 "msg %d cdb %x iptl: "
3653 "%d:%d:%d:%d tag 0x%04x "
3656 tmp_io->io_hdr.io_type,
3657 tmp_io->io_hdr.msg_type,
3658 tmp_io->scsiio.cdb[0],
3659 tmp_io->io_hdr.nexus.initid.id,
3660 tmp_io->io_hdr.nexus.targ_port,
3661 tmp_io->io_hdr.nexus.targ_target.id,
3662 tmp_io->io_hdr.nexus.targ_lun,
3663 (tmp_io->io_hdr.io_type ==
3665 tmp_io->taskio.tag_num :
3666 tmp_io->scsiio.tag_num,
3668 panic("I/O still on the task queue!");
3672 io->io_hdr.io_type = 0xff;
3673 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3674 pool->total_freed++;
3675 pool->free_ctl_io++;
3676 ctl_pool_release(pool);
3677 mtx_unlock(&pool->ctl_softc->pool_lock);
3680 * Otherwise, just free it. We probably malloced it and
3681 * the emergency pool wasn't available.
3689 ctl_zero_io(union ctl_io *io)
3697 * May need to preserve linked list pointers at some point too.
3699 pool_ref = io->io_hdr.pool;
3701 memset(io, 0, sizeof(*io));
3703 io->io_hdr.pool = pool_ref;
3707 * This routine is currently used for internal copies of ctl_ios that need
3708 * to persist for some reason after we've already returned status to the
3709 * FETD. (Thus the flag set.)
3712 * Note that this makes a blind copy of all fields in the ctl_io, except
3713 * for the pool reference. This includes any memory that has been
3714 * allocated! That memory will no longer be valid after done has been
3715 * called, so this would be VERY DANGEROUS for command that actually does
3716 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3717 * start and stop commands, which don't transfer any data, so this is not a
3718 * problem. If it is used for anything else, the caller would also need to
3719 * allocate data buffer space and this routine would need to be modified to
3720 * copy the data buffer(s) as well.
3723 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3732 * May need to preserve linked list pointers at some point too.
3734 pool_ref = dest->io_hdr.pool;
3736 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3738 dest->io_hdr.pool = pool_ref;
3740 * We need to know that this is an internal copy, and doesn't need
3741 * to get passed back to the FETD that allocated it.
3743 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3748 ctl_update_power_subpage(struct copan_power_subpage *page)
3750 int num_luns, num_partitions, config_type;
3751 struct ctl_softc *softc;
3752 cs_BOOL_t aor_present, shelf_50pct_power;
3753 cs_raidset_personality_t rs_type;
3754 int max_active_luns;
3756 softc = control_softc;
3758 /* subtract out the processor LUN */
3759 num_luns = softc->num_luns - 1;
3761 * Default to 7 LUNs active, which was the only number we allowed
3764 max_active_luns = 7;
3766 num_partitions = config_GetRsPartitionInfo();
3767 config_type = config_GetConfigType();
3768 shelf_50pct_power = config_GetShelfPowerMode();
3769 aor_present = config_IsAorRsPresent();
3771 rs_type = ddb_GetRsRaidType(1);
3772 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3773 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3774 EPRINT(0, "Unsupported RS type %d!", rs_type);
3778 page->total_luns = num_luns;
3780 switch (config_type) {
3783 * In a 40 drive configuration, it doesn't matter what DC
3784 * cards we have, whether we have AOR enabled or not,
3785 * partitioning or not, or what type of RAIDset we have.
3786 * In that scenario, we can power up every LUN we present
3789 max_active_luns = num_luns;
3793 if (shelf_50pct_power == CS_FALSE) {
3795 if (aor_present == CS_TRUE) {
3797 CS_RAIDSET_PERSONALITY_RAID5) {
3798 max_active_luns = 7;
3799 } else if (rs_type ==
3800 CS_RAIDSET_PERSONALITY_RAID1){
3801 max_active_luns = 14;
3803 /* XXX KDM now what?? */
3807 CS_RAIDSET_PERSONALITY_RAID5) {
3808 max_active_luns = 8;
3809 } else if (rs_type ==
3810 CS_RAIDSET_PERSONALITY_RAID1){
3811 max_active_luns = 16;
3813 /* XXX KDM now what?? */
3819 * With 50% power in a 64 drive configuration, we
3820 * can power all LUNs we present.
3822 max_active_luns = num_luns;
3826 if (shelf_50pct_power == CS_FALSE) {
3828 if (aor_present == CS_TRUE) {
3830 CS_RAIDSET_PERSONALITY_RAID5) {
3831 max_active_luns = 7;
3832 } else if (rs_type ==
3833 CS_RAIDSET_PERSONALITY_RAID1){
3834 max_active_luns = 14;
3836 /* XXX KDM now what?? */
3840 CS_RAIDSET_PERSONALITY_RAID5) {
3841 max_active_luns = 8;
3842 } else if (rs_type ==
3843 CS_RAIDSET_PERSONALITY_RAID1){
3844 max_active_luns = 16;
3846 /* XXX KDM now what?? */
3851 if (aor_present == CS_TRUE) {
3853 CS_RAIDSET_PERSONALITY_RAID5) {
3854 max_active_luns = 14;
3855 } else if (rs_type ==
3856 CS_RAIDSET_PERSONALITY_RAID1){
3858 * We're assuming here that disk
3859 * caching is enabled, and so we're
3860 * able to power up half of each
3861 * LUN, and cache all writes.
3863 max_active_luns = num_luns;
3865 /* XXX KDM now what?? */
3869 CS_RAIDSET_PERSONALITY_RAID5) {
3870 max_active_luns = 15;
3871 } else if (rs_type ==
3872 CS_RAIDSET_PERSONALITY_RAID1){
3873 max_active_luns = 30;
3875 /* XXX KDM now what?? */
3882 * In this case, we have an unknown configuration, so we
3883 * just use the default from above.
3888 page->max_active_luns = max_active_luns;
3890 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3891 page->total_luns, page->max_active_luns);
3894 #endif /* NEEDTOPORT */
3897 * This routine could be used in the future to load default and/or saved
3898 * mode page parameters for a particuar lun.
3901 ctl_init_page_index(struct ctl_lun *lun)
3904 struct ctl_page_index *page_index;
3905 struct ctl_softc *softc;
3907 memcpy(&lun->mode_pages.index, page_index_template,
3908 sizeof(page_index_template));
3910 softc = lun->ctl_softc;
3912 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3914 page_index = &lun->mode_pages.index[i];
3916 * If this is a disk-only mode page, there's no point in
3917 * setting it up. For some pages, we have to have some
3918 * basic information about the disk in order to calculate the
3921 if ((lun->be_lun->lun_type != T_DIRECT)
3922 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3925 switch (page_index->page_code & SMPH_PC_MASK) {
3926 case SMS_FORMAT_DEVICE_PAGE: {
3927 struct scsi_format_page *format_page;
3929 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3930 panic("subpage is incorrect!");
3933 * Sectors per track are set above. Bytes per
3934 * sector need to be set here on a per-LUN basis.
3936 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3937 &format_page_default,
3938 sizeof(format_page_default));
3939 memcpy(&lun->mode_pages.format_page[
3940 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3941 sizeof(format_page_changeable));
3942 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3943 &format_page_default,
3944 sizeof(format_page_default));
3945 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3946 &format_page_default,
3947 sizeof(format_page_default));
3949 format_page = &lun->mode_pages.format_page[
3951 scsi_ulto2b(lun->be_lun->blocksize,
3952 format_page->bytes_per_sector);
3954 format_page = &lun->mode_pages.format_page[
3956 scsi_ulto2b(lun->be_lun->blocksize,
3957 format_page->bytes_per_sector);
3959 format_page = &lun->mode_pages.format_page[
3961 scsi_ulto2b(lun->be_lun->blocksize,
3962 format_page->bytes_per_sector);
3964 page_index->page_data =
3965 (uint8_t *)lun->mode_pages.format_page;
3968 case SMS_RIGID_DISK_PAGE: {
3969 struct scsi_rigid_disk_page *rigid_disk_page;
3970 uint32_t sectors_per_cylinder;
3974 #endif /* !__XSCALE__ */
3976 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3977 panic("invalid subpage value %d",
3978 page_index->subpage);
3981 * Rotation rate and sectors per track are set
3982 * above. We calculate the cylinders here based on
3983 * capacity. Due to the number of heads and
3984 * sectors per track we're using, smaller arrays
3985 * may turn out to have 0 cylinders. Linux and
3986 * FreeBSD don't pay attention to these mode pages
3987 * to figure out capacity, but Solaris does. It
3988 * seems to deal with 0 cylinders just fine, and
3989 * works out a fake geometry based on the capacity.
3991 memcpy(&lun->mode_pages.rigid_disk_page[
3992 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3993 sizeof(rigid_disk_page_default));
3994 memcpy(&lun->mode_pages.rigid_disk_page[
3995 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3996 sizeof(rigid_disk_page_changeable));
3997 memcpy(&lun->mode_pages.rigid_disk_page[
3998 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3999 sizeof(rigid_disk_page_default));
4000 memcpy(&lun->mode_pages.rigid_disk_page[
4001 CTL_PAGE_SAVED], &rigid_disk_page_default,
4002 sizeof(rigid_disk_page_default));
4004 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4008 * The divide method here will be more accurate,
4009 * probably, but results in floating point being
4010 * used in the kernel on i386 (__udivdi3()). On the
4011 * XScale, though, __udivdi3() is implemented in
4014 * The shift method for cylinder calculation is
4015 * accurate if sectors_per_cylinder is a power of
4016 * 2. Otherwise it might be slightly off -- you
4017 * might have a bit of a truncation problem.
4020 cylinders = (lun->be_lun->maxlba + 1) /
4021 sectors_per_cylinder;
4023 for (shift = 31; shift > 0; shift--) {
4024 if (sectors_per_cylinder & (1 << shift))
4027 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4031 * We've basically got 3 bytes, or 24 bits for the
4032 * cylinder size in the mode page. If we're over,
4033 * just round down to 2^24.
4035 if (cylinders > 0xffffff)
4036 cylinders = 0xffffff;
4038 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4040 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4042 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4044 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4046 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4048 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4050 page_index->page_data =
4051 (uint8_t *)lun->mode_pages.rigid_disk_page;
4054 case SMS_CACHING_PAGE: {
4056 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4057 panic("invalid subpage value %d",
4058 page_index->subpage);
4060 * Defaults should be okay here, no calculations
4063 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4064 &caching_page_default,
4065 sizeof(caching_page_default));
4066 memcpy(&lun->mode_pages.caching_page[
4067 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4068 sizeof(caching_page_changeable));
4069 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4070 &caching_page_default,
4071 sizeof(caching_page_default));
4072 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4073 &caching_page_default,
4074 sizeof(caching_page_default));
4075 page_index->page_data =
4076 (uint8_t *)lun->mode_pages.caching_page;
4079 case SMS_CONTROL_MODE_PAGE: {
4081 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4082 panic("invalid subpage value %d",
4083 page_index->subpage);
4086 * Defaults should be okay here, no calculations
4089 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4090 &control_page_default,
4091 sizeof(control_page_default));
4092 memcpy(&lun->mode_pages.control_page[
4093 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4094 sizeof(control_page_changeable));
4095 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4096 &control_page_default,
4097 sizeof(control_page_default));
4098 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4099 &control_page_default,
4100 sizeof(control_page_default));
4101 page_index->page_data =
4102 (uint8_t *)lun->mode_pages.control_page;
4106 case SMS_VENDOR_SPECIFIC_PAGE:{
4107 switch (page_index->subpage) {
4108 case PWR_SUBPAGE_CODE: {
4109 struct copan_power_subpage *current_page,
4112 memcpy(&lun->mode_pages.power_subpage[
4114 &power_page_default,
4115 sizeof(power_page_default));
4116 memcpy(&lun->mode_pages.power_subpage[
4117 CTL_PAGE_CHANGEABLE],
4118 &power_page_changeable,
4119 sizeof(power_page_changeable));
4120 memcpy(&lun->mode_pages.power_subpage[
4122 &power_page_default,
4123 sizeof(power_page_default));
4124 memcpy(&lun->mode_pages.power_subpage[
4126 &power_page_default,
4127 sizeof(power_page_default));
4128 page_index->page_data =
4129 (uint8_t *)lun->mode_pages.power_subpage;
4131 current_page = (struct copan_power_subpage *)
4132 (page_index->page_data +
4133 (page_index->page_len *
4135 saved_page = (struct copan_power_subpage *)
4136 (page_index->page_data +
4137 (page_index->page_len *
4141 case APS_SUBPAGE_CODE: {
4142 struct copan_aps_subpage *current_page,
4145 // This gets set multiple times but
4146 // it should always be the same. It's
4147 // only done during init so who cares.
4148 index_to_aps_page = i;
4150 memcpy(&lun->mode_pages.aps_subpage[
4153 sizeof(aps_page_default));
4154 memcpy(&lun->mode_pages.aps_subpage[
4155 CTL_PAGE_CHANGEABLE],
4156 &aps_page_changeable,
4157 sizeof(aps_page_changeable));
4158 memcpy(&lun->mode_pages.aps_subpage[
4161 sizeof(aps_page_default));
4162 memcpy(&lun->mode_pages.aps_subpage[
4165 sizeof(aps_page_default));
4166 page_index->page_data =
4167 (uint8_t *)lun->mode_pages.aps_subpage;
4169 current_page = (struct copan_aps_subpage *)
4170 (page_index->page_data +
4171 (page_index->page_len *
4173 saved_page = (struct copan_aps_subpage *)
4174 (page_index->page_data +
4175 (page_index->page_len *
4179 case DBGCNF_SUBPAGE_CODE: {
4180 struct copan_debugconf_subpage *current_page,
4183 memcpy(&lun->mode_pages.debugconf_subpage[
4185 &debugconf_page_default,
4186 sizeof(debugconf_page_default));
4187 memcpy(&lun->mode_pages.debugconf_subpage[
4188 CTL_PAGE_CHANGEABLE],
4189 &debugconf_page_changeable,
4190 sizeof(debugconf_page_changeable));
4191 memcpy(&lun->mode_pages.debugconf_subpage[
4193 &debugconf_page_default,
4194 sizeof(debugconf_page_default));
4195 memcpy(&lun->mode_pages.debugconf_subpage[
4197 &debugconf_page_default,
4198 sizeof(debugconf_page_default));
4199 page_index->page_data =
4200 (uint8_t *)lun->mode_pages.debugconf_subpage;
4202 current_page = (struct copan_debugconf_subpage *)
4203 (page_index->page_data +
4204 (page_index->page_len *
4206 saved_page = (struct copan_debugconf_subpage *)
4207 (page_index->page_data +
4208 (page_index->page_len *
4213 panic("invalid subpage value %d",
4214 page_index->subpage);
4220 panic("invalid page value %d",
4221 page_index->page_code & SMPH_PC_MASK);
4226 return (CTL_RETVAL_COMPLETE);
4233 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4234 * wants us to allocate the LUN and he can block.
4235 * - ctl_softc is always set
4236 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4238 * Returns 0 for success, non-zero (errno) for failure.
4241 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4242 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4244 struct ctl_lun *nlun, *lun;
4245 struct ctl_frontend *fe;
4246 int lun_number, i, lun_malloced;
4252 * We currently only support Direct Access or Processor LUN types.
4254 switch (be_lun->lun_type) {
4262 be_lun->lun_config_status(be_lun->be_lun,
4263 CTL_LUN_CONFIG_FAILURE);
4266 if (ctl_lun == NULL) {
4267 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4274 memset(lun, 0, sizeof(*lun));
4276 lun->flags = CTL_LUN_MALLOCED;
4278 mtx_lock(&ctl_softc->ctl_lock);
4280 * See if the caller requested a particular LUN number. If so, see
4281 * if it is available. Otherwise, allocate the first available LUN.
4283 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4284 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4285 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4286 mtx_unlock(&ctl_softc->ctl_lock);
4287 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4288 printf("ctl: requested LUN ID %d is higher "
4289 "than CTL_MAX_LUNS - 1 (%d)\n",
4290 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4293 * XXX KDM return an error, or just assign
4294 * another LUN ID in this case??
4296 printf("ctl: requested LUN ID %d is already "
4297 "in use\n", be_lun->req_lun_id);
4299 if (lun->flags & CTL_LUN_MALLOCED)
4301 be_lun->lun_config_status(be_lun->be_lun,
4302 CTL_LUN_CONFIG_FAILURE);
4305 lun_number = be_lun->req_lun_id;
4307 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4308 if (lun_number == -1) {
4309 mtx_unlock(&ctl_softc->ctl_lock);
4310 printf("ctl: can't allocate LUN on target %ju, out of "
4311 "LUNs\n", (uintmax_t)target_id.id);
4312 if (lun->flags & CTL_LUN_MALLOCED)
4314 be_lun->lun_config_status(be_lun->be_lun,
4315 CTL_LUN_CONFIG_FAILURE);
4319 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4321 lun->target = target_id;
4322 lun->lun = lun_number;
4323 lun->be_lun = be_lun;
4325 * The processor LUN is always enabled. Disk LUNs come on line
4326 * disabled, and must be enabled by the backend.
4328 lun->flags |= CTL_LUN_DISABLED;
4329 lun->backend = be_lun->be;
4330 be_lun->ctl_lun = lun;
4331 be_lun->lun_id = lun_number;
4332 atomic_add_int(&be_lun->be->num_luns, 1);
4333 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4334 lun->flags |= CTL_LUN_STOPPED;
4336 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4337 lun->flags |= CTL_LUN_INOPERABLE;
4339 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4340 lun->flags |= CTL_LUN_PRIMARY_SC;
4342 lun->ctl_softc = ctl_softc;
4343 TAILQ_INIT(&lun->ooa_queue);
4344 TAILQ_INIT(&lun->blocked_queue);
4345 STAILQ_INIT(&lun->error_list);
4348 * Initialize the mode page index.
4350 ctl_init_page_index(lun);
4353 * Set the poweron UA for all initiators on this LUN only.
4355 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4356 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4359 * Now, before we insert this lun on the lun list, set the lun
4360 * inventory changed UA for all other luns.
4362 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4363 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4364 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4368 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4370 ctl_softc->ctl_luns[lun_number] = lun;
4372 ctl_softc->num_luns++;
4374 /* Setup statistics gathering */
4375 lun->stats.device_type = be_lun->lun_type;
4376 lun->stats.lun_number = lun_number;
4377 if (lun->stats.device_type == T_DIRECT)
4378 lun->stats.blocksize = be_lun->blocksize;
4380 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4381 for (i = 0;i < CTL_MAX_PORTS;i++)
4382 lun->stats.ports[i].targ_port = i;
4384 mtx_unlock(&ctl_softc->ctl_lock);
4386 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4389 * Run through each registered FETD and bring it online if it isn't
4390 * already. Enable the target ID if it hasn't been enabled, and
4391 * enable this particular LUN.
4393 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4397 * XXX KDM this only works for ONE TARGET ID. We'll need
4398 * to do things differently if we go to a multiple target
4401 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4403 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4405 printf("ctl_alloc_lun: FETD %s port %d "
4406 "returned error %d for targ_enable on "
4407 "target %ju\n", fe->port_name,
4408 fe->targ_port, retval,
4409 (uintmax_t)target_id.id);
4411 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4414 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4416 printf("ctl_alloc_lun: FETD %s port %d returned error "
4417 "%d for lun_enable on target %ju lun %d\n",
4418 fe->port_name, fe->targ_port, retval,
4419 (uintmax_t)target_id.id, lun_number);
4421 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4429 * - LUN has already been marked invalid and any pending I/O has been taken
4433 ctl_free_lun(struct ctl_lun *lun)
4435 struct ctl_softc *softc;
4437 struct ctl_frontend *fe;
4439 struct ctl_lun *nlun;
4440 union ctl_io *io, *next_io;
4443 softc = lun->ctl_softc;
4445 mtx_assert(&softc->ctl_lock, MA_OWNED);
4447 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4449 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4451 softc->ctl_luns[lun->lun] = NULL;
4453 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4454 printf("ctl_free_lun: aieee!! freeing a LUN with "
4455 "outstanding I/O!!\n");
4459 * If we have anything pending on the RtR queue, remove it.
4461 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4465 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4466 targ_lun = io->io_hdr.nexus.targ_lun;
4467 if (io->io_hdr.nexus.lun_map_fn != NULL)
4468 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
4469 if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4470 && (targ_lun == lun->lun))
4471 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4476 * Then remove everything from the blocked queue.
4478 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4480 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4481 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4482 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4486 * Now clear out the OOA queue, and free all the I/O.
4487 * XXX KDM should we notify the FETD here? We probably need to
4488 * quiesce the LUN before deleting it.
4490 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4492 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4493 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4500 * XXX KDM this scheme only works for a single target/multiple LUN
4501 * setup. It needs to be revamped for a multiple target scheme.
4503 * XXX KDM this results in fe->lun_disable() getting called twice,
4504 * once when ctl_disable_lun() is called, and a second time here.
4505 * We really need to re-think the LUN disable semantics. There
4506 * should probably be several steps/levels to LUN removal:
4511 * Right now we only have a disable method when communicating to
4512 * the front end ports, at least for individual LUNs.
4515 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4518 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4521 printf("ctl_free_lun: FETD %s port %d returned error "
4522 "%d for lun_disable on target %ju lun %jd\n",
4523 fe->port_name, fe->targ_port, retval,
4524 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4527 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4528 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4530 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4532 printf("ctl_free_lun: FETD %s port %d "
4533 "returned error %d for targ_disable on "
4534 "target %ju\n", fe->port_name,
4535 fe->targ_port, retval,
4536 (uintmax_t)lun->target.id);
4538 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4540 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4544 fe->port_offline(fe->onoff_arg);
4545 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4552 * Tell the backend to free resources, if this LUN has a backend.
4554 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4555 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4557 if (lun->flags & CTL_LUN_MALLOCED)
4560 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4561 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4562 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4570 ctl_create_lun(struct ctl_be_lun *be_lun)
4572 struct ctl_softc *ctl_softc;
4574 ctl_softc = control_softc;
4577 * ctl_alloc_lun() should handle all potential failure cases.
4579 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4583 ctl_add_lun(struct ctl_be_lun *be_lun)
4585 struct ctl_softc *ctl_softc;
4587 ctl_softc = control_softc;
4589 mtx_lock(&ctl_softc->ctl_lock);
4590 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4591 mtx_unlock(&ctl_softc->ctl_lock);
4593 ctl_wakeup_thread();
4599 ctl_enable_lun(struct ctl_be_lun *be_lun)
4601 struct ctl_softc *ctl_softc;
4602 struct ctl_frontend *fe, *nfe;
4603 struct ctl_lun *lun;
4606 ctl_softc = control_softc;
4608 lun = (struct ctl_lun *)be_lun->ctl_lun;
4610 mtx_lock(&ctl_softc->ctl_lock);
4611 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4613 * eh? Why did we get called if the LUN is already
4616 mtx_unlock(&ctl_softc->ctl_lock);
4619 lun->flags &= ~CTL_LUN_DISABLED;
4621 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4622 nfe = STAILQ_NEXT(fe, links);
4625 * Drop the lock while we call the FETD's enable routine.
4626 * This can lead to a callback into CTL (at least in the
4627 * case of the internal initiator frontend.
4629 mtx_unlock(&ctl_softc->ctl_lock);
4630 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4631 mtx_lock(&ctl_softc->ctl_lock);
4633 printf("%s: FETD %s port %d returned error "
4634 "%d for lun_enable on target %ju lun %jd\n",
4635 __func__, fe->port_name, fe->targ_port, retval,
4636 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4640 /* NOTE: TODO: why does lun enable affect port status? */
4641 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4646 mtx_unlock(&ctl_softc->ctl_lock);
4652 ctl_disable_lun(struct ctl_be_lun *be_lun)
4654 struct ctl_softc *ctl_softc;
4655 struct ctl_frontend *fe;
4656 struct ctl_lun *lun;
4659 ctl_softc = control_softc;
4661 lun = (struct ctl_lun *)be_lun->ctl_lun;
4663 mtx_lock(&ctl_softc->ctl_lock);
4665 if (lun->flags & CTL_LUN_DISABLED) {
4666 mtx_unlock(&ctl_softc->ctl_lock);
4669 lun->flags |= CTL_LUN_DISABLED;
4671 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4672 mtx_unlock(&ctl_softc->ctl_lock);
4674 * Drop the lock before we call the frontend's disable
4675 * routine, to avoid lock order reversals.
4677 * XXX KDM what happens if the frontend list changes while
4678 * we're traversing it? It's unlikely, but should be handled.
4680 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4682 mtx_lock(&ctl_softc->ctl_lock);
4684 printf("ctl_alloc_lun: FETD %s port %d returned error "
4685 "%d for lun_disable on target %ju lun %jd\n",
4686 fe->port_name, fe->targ_port, retval,
4687 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4691 mtx_unlock(&ctl_softc->ctl_lock);
4697 ctl_start_lun(struct ctl_be_lun *be_lun)
4699 struct ctl_softc *ctl_softc;
4700 struct ctl_lun *lun;
4702 ctl_softc = control_softc;
4704 lun = (struct ctl_lun *)be_lun->ctl_lun;
4706 mtx_lock(&ctl_softc->ctl_lock);
4707 lun->flags &= ~CTL_LUN_STOPPED;
4708 mtx_unlock(&ctl_softc->ctl_lock);
4714 ctl_stop_lun(struct ctl_be_lun *be_lun)
4716 struct ctl_softc *ctl_softc;
4717 struct ctl_lun *lun;
4719 ctl_softc = control_softc;
4721 lun = (struct ctl_lun *)be_lun->ctl_lun;
4723 mtx_lock(&ctl_softc->ctl_lock);
4724 lun->flags |= CTL_LUN_STOPPED;
4725 mtx_unlock(&ctl_softc->ctl_lock);
4731 ctl_lun_offline(struct ctl_be_lun *be_lun)
4733 struct ctl_softc *ctl_softc;
4734 struct ctl_lun *lun;
4736 ctl_softc = control_softc;
4738 lun = (struct ctl_lun *)be_lun->ctl_lun;
4740 mtx_lock(&ctl_softc->ctl_lock);
4741 lun->flags |= CTL_LUN_OFFLINE;
4742 mtx_unlock(&ctl_softc->ctl_lock);
4748 ctl_lun_online(struct ctl_be_lun *be_lun)
4750 struct ctl_softc *ctl_softc;
4751 struct ctl_lun *lun;
4753 ctl_softc = control_softc;
4755 lun = (struct ctl_lun *)be_lun->ctl_lun;
4757 mtx_lock(&ctl_softc->ctl_lock);
4758 lun->flags &= ~CTL_LUN_OFFLINE;
4759 mtx_unlock(&ctl_softc->ctl_lock);
4765 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4767 struct ctl_softc *ctl_softc;
4768 struct ctl_lun *lun;
4770 ctl_softc = control_softc;
4772 lun = (struct ctl_lun *)be_lun->ctl_lun;
4774 mtx_lock(&ctl_softc->ctl_lock);
4777 * The LUN needs to be disabled before it can be marked invalid.
4779 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4780 mtx_unlock(&ctl_softc->ctl_lock);
4784 * Mark the LUN invalid.
4786 lun->flags |= CTL_LUN_INVALID;
4789 * If there is nothing in the OOA queue, go ahead and free the LUN.
4790 * If we have something in the OOA queue, we'll free it when the
4791 * last I/O completes.
4793 if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4795 mtx_unlock(&ctl_softc->ctl_lock);
4801 ctl_lun_inoperable(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_operable(struct ctl_be_lun *be_lun)
4819 struct ctl_softc *ctl_softc;
4820 struct ctl_lun *lun;
4822 ctl_softc = control_softc;
4823 lun = (struct ctl_lun *)be_lun->ctl_lun;
4825 mtx_lock(&ctl_softc->ctl_lock);
4826 lun->flags &= ~CTL_LUN_INOPERABLE;
4827 mtx_unlock(&ctl_softc->ctl_lock);
4833 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4836 struct ctl_softc *softc;
4837 struct ctl_lun *lun;
4838 struct copan_aps_subpage *current_sp;
4839 struct ctl_page_index *page_index;
4842 softc = control_softc;
4844 mtx_lock(&softc->ctl_lock);
4846 lun = (struct ctl_lun *)be_lun->ctl_lun;
4849 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4850 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4854 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4856 page_index = &lun->mode_pages.index[i];
4859 if (page_index == NULL) {
4860 mtx_unlock(&softc->ctl_lock);
4861 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4862 (uintmax_t)lun->lun);
4866 if ((softc->aps_locked_lun != 0)
4867 && (softc->aps_locked_lun != lun->lun)) {
4868 printf("%s: attempt to lock LUN %llu when %llu is already "
4870 mtx_unlock(&softc->ctl_lock);
4875 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4876 (page_index->page_len * CTL_PAGE_CURRENT));
4879 current_sp->lock_active = APS_LOCK_ACTIVE;
4880 softc->aps_locked_lun = lun->lun;
4882 current_sp->lock_active = 0;
4883 softc->aps_locked_lun = 0;
4888 * If we're in HA mode, try to send the lock message to the other
4891 if (ctl_is_single == 0) {
4893 union ctl_ha_msg lock_msg;
4895 lock_msg.hdr.nexus = *nexus;
4896 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4898 lock_msg.aps.lock_flag = 1;
4900 lock_msg.aps.lock_flag = 0;
4901 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4902 sizeof(lock_msg), 0);
4903 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4904 printf("%s: APS (lock=%d) error returned from "
4905 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4906 mtx_unlock(&softc->ctl_lock);
4911 mtx_unlock(&softc->ctl_lock);
4917 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4919 struct ctl_lun *lun;
4920 struct ctl_softc *softc;
4923 softc = control_softc;
4925 mtx_lock(&softc->ctl_lock);
4927 lun = (struct ctl_lun *)be_lun->ctl_lun;
4929 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4930 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4932 mtx_unlock(&softc->ctl_lock);
4936 * Backend "memory move is complete" callback for requests that never
4937 * make it down to say RAIDCore's configuration code.
4940 ctl_config_move_done(union ctl_io *io)
4944 retval = CTL_RETVAL_COMPLETE;
4947 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4949 * XXX KDM this shouldn't happen, but what if it does?
4951 if (io->io_hdr.io_type != CTL_IO_SCSI)
4952 panic("I/O type isn't CTL_IO_SCSI!");
4954 if ((io->io_hdr.port_status == 0)
4955 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4956 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4957 io->io_hdr.status = CTL_SUCCESS;
4958 else if ((io->io_hdr.port_status != 0)
4959 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4960 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4962 * For hardware error sense keys, the sense key
4963 * specific value is defined to be a retry count,
4964 * but we use it to pass back an internal FETD
4965 * error code. XXX KDM Hopefully the FETD is only
4966 * using 16 bits for an error code, since that's
4967 * all the space we have in the sks field.
4969 ctl_set_internal_failure(&io->scsiio,
4972 io->io_hdr.port_status);
4973 free(io->scsiio.kern_data_ptr, M_CTL);
4978 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4979 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4980 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4982 * XXX KDM just assuming a single pointer here, and not a
4983 * S/G list. If we start using S/G lists for config data,
4984 * we'll need to know how to clean them up here as well.
4986 free(io->scsiio.kern_data_ptr, M_CTL);
4987 /* Hopefully the user has already set the status... */
4991 * XXX KDM now we need to continue data movement. Some
4993 * - call ctl_scsiio() again? We don't do this for data
4994 * writes, because for those at least we know ahead of
4995 * time where the write will go and how long it is. For
4996 * config writes, though, that information is largely
4997 * contained within the write itself, thus we need to
4998 * parse out the data again.
5000 * - Call some other function once the data is in?
5004 * XXX KDM call ctl_scsiio() again for now, and check flag
5005 * bits to see whether we're allocated or not.
5007 retval = ctl_scsiio(&io->scsiio);
5014 * This gets called by a backend driver when it is done with a
5015 * configuration write.
5018 ctl_config_write_done(union ctl_io *io)
5021 * If the IO_CONT flag is set, we need to call the supplied
5022 * function to continue processing the I/O, instead of completing
5025 * If there is an error, though, we don't want to keep processing.
5026 * Instead, just send status back to the initiator.
5028 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5029 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5030 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5031 io->scsiio.io_cont(io);
5035 * Since a configuration write can be done for commands that actually
5036 * have data allocated, like write buffer, and commands that have
5037 * no data, like start/stop unit, we need to check here.
5039 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5040 free(io->scsiio.kern_data_ptr, M_CTL);
5045 * SCSI release command.
5048 ctl_scsi_release(struct ctl_scsiio *ctsio)
5050 int length, longid, thirdparty_id, resv_id;
5051 struct ctl_softc *ctl_softc;
5052 struct ctl_lun *lun;
5057 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5059 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5060 ctl_softc = control_softc;
5062 switch (ctsio->cdb[0]) {
5064 struct scsi_release *cdb;
5066 cdb = (struct scsi_release *)ctsio->cdb;
5067 if ((cdb->byte2 & 0x1f) != 0) {
5068 ctl_set_invalid_field(ctsio,
5074 ctl_done((union ctl_io *)ctsio);
5075 return (CTL_RETVAL_COMPLETE);
5080 struct scsi_release_10 *cdb;
5082 cdb = (struct scsi_release_10 *)ctsio->cdb;
5084 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5085 ctl_set_invalid_field(ctsio,
5091 ctl_done((union ctl_io *)ctsio);
5092 return (CTL_RETVAL_COMPLETE);
5096 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5097 ctl_set_invalid_field(ctsio,
5103 ctl_done((union ctl_io *)ctsio);
5104 return (CTL_RETVAL_COMPLETE);
5107 if (cdb->byte2 & SR10_LONGID)
5110 thirdparty_id = cdb->thirdparty_id;
5112 resv_id = cdb->resv_id;
5113 length = scsi_2btoul(cdb->length);
5120 * XXX KDM right now, we only support LUN reservation. We don't
5121 * support 3rd party reservations, or extent reservations, which
5122 * might actually need the parameter list. If we've gotten this
5123 * far, we've got a LUN reservation. Anything else got kicked out
5124 * above. So, according to SPC, ignore the length.
5128 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5130 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5131 ctsio->kern_data_len = length;
5132 ctsio->kern_total_len = length;
5133 ctsio->kern_data_resid = 0;
5134 ctsio->kern_rel_offset = 0;
5135 ctsio->kern_sg_entries = 0;
5136 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5137 ctsio->be_move_done = ctl_config_move_done;
5138 ctl_datamove((union ctl_io *)ctsio);
5140 return (CTL_RETVAL_COMPLETE);
5144 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5146 mtx_lock(&ctl_softc->ctl_lock);
5149 * According to SPC, it is not an error for an intiator to attempt
5150 * to release a reservation on a LUN that isn't reserved, or that
5151 * is reserved by another initiator. The reservation can only be
5152 * released, though, by the initiator who made it or by one of
5153 * several reset type events.
5155 if (lun->flags & CTL_LUN_RESERVED) {
5156 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5157 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5158 && (ctsio->io_hdr.nexus.targ_target.id ==
5159 lun->rsv_nexus.targ_target.id)) {
5160 lun->flags &= ~CTL_LUN_RESERVED;
5164 ctsio->scsi_status = SCSI_STATUS_OK;
5165 ctsio->io_hdr.status = CTL_SUCCESS;
5167 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5168 free(ctsio->kern_data_ptr, M_CTL);
5169 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5172 mtx_unlock(&ctl_softc->ctl_lock);
5174 ctl_done((union ctl_io *)ctsio);
5175 return (CTL_RETVAL_COMPLETE);
5179 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5181 int extent, thirdparty, longid;
5182 int resv_id, length;
5183 uint64_t thirdparty_id;
5184 struct ctl_softc *ctl_softc;
5185 struct ctl_lun *lun;
5194 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5196 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5197 ctl_softc = control_softc;
5199 switch (ctsio->cdb[0]) {
5201 struct scsi_reserve *cdb;
5203 cdb = (struct scsi_reserve *)ctsio->cdb;
5204 if ((cdb->byte2 & 0x1f) != 0) {
5205 ctl_set_invalid_field(ctsio,
5211 ctl_done((union ctl_io *)ctsio);
5212 return (CTL_RETVAL_COMPLETE);
5214 resv_id = cdb->resv_id;
5215 length = scsi_2btoul(cdb->length);
5219 struct scsi_reserve_10 *cdb;
5221 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5223 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5224 ctl_set_invalid_field(ctsio,
5230 ctl_done((union ctl_io *)ctsio);
5231 return (CTL_RETVAL_COMPLETE);
5233 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5234 ctl_set_invalid_field(ctsio,
5240 ctl_done((union ctl_io *)ctsio);
5241 return (CTL_RETVAL_COMPLETE);
5243 if (cdb->byte2 & SR10_LONGID)
5246 thirdparty_id = cdb->thirdparty_id;
5248 resv_id = cdb->resv_id;
5249 length = scsi_2btoul(cdb->length);
5255 * XXX KDM right now, we only support LUN reservation. We don't
5256 * support 3rd party reservations, or extent reservations, which
5257 * might actually need the parameter list. If we've gotten this
5258 * far, we've got a LUN reservation. Anything else got kicked out
5259 * above. So, according to SPC, ignore the length.
5263 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5265 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5266 ctsio->kern_data_len = length;
5267 ctsio->kern_total_len = length;
5268 ctsio->kern_data_resid = 0;
5269 ctsio->kern_rel_offset = 0;
5270 ctsio->kern_sg_entries = 0;
5271 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5272 ctsio->be_move_done = ctl_config_move_done;
5273 ctl_datamove((union ctl_io *)ctsio);
5275 return (CTL_RETVAL_COMPLETE);
5279 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5281 mtx_lock(&ctl_softc->ctl_lock);
5282 if (lun->flags & CTL_LUN_RESERVED) {
5283 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5284 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5285 || (ctsio->io_hdr.nexus.targ_target.id !=
5286 lun->rsv_nexus.targ_target.id)) {
5287 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5288 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5293 lun->flags |= CTL_LUN_RESERVED;
5294 lun->rsv_nexus = ctsio->io_hdr.nexus;
5296 ctsio->scsi_status = SCSI_STATUS_OK;
5297 ctsio->io_hdr.status = CTL_SUCCESS;
5300 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5301 free(ctsio->kern_data_ptr, M_CTL);
5302 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5305 mtx_unlock(&ctl_softc->ctl_lock);
5307 ctl_done((union ctl_io *)ctsio);
5308 return (CTL_RETVAL_COMPLETE);
5312 ctl_start_stop(struct ctl_scsiio *ctsio)
5314 struct scsi_start_stop_unit *cdb;
5315 struct ctl_lun *lun;
5316 struct ctl_softc *ctl_softc;
5319 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5321 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5322 ctl_softc = control_softc;
5325 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5329 * We don't support the immediate bit on a stop unit. In order to
5330 * do that, we would need to code up a way to know that a stop is
5331 * pending, and hold off any new commands until it completes, one
5332 * way or another. Then we could accept or reject those commands
5333 * depending on its status. We would almost need to do the reverse
5334 * of what we do below for an immediate start -- return the copy of
5335 * the ctl_io to the FETD with status to send to the host (and to
5336 * free the copy!) and then free the original I/O once the stop
5337 * actually completes. That way, the OOA queue mechanism can work
5338 * to block commands that shouldn't proceed. Another alternative
5339 * would be to put the copy in the queue in place of the original,
5340 * and return the original back to the caller. That could be
5343 if ((cdb->byte2 & SSS_IMMED)
5344 && ((cdb->how & SSS_START) == 0)) {
5345 ctl_set_invalid_field(ctsio,
5351 ctl_done((union ctl_io *)ctsio);
5352 return (CTL_RETVAL_COMPLETE);
5356 * We don't support the power conditions field. We need to check
5357 * this prior to checking the load/eject and start/stop bits.
5359 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5360 ctl_set_invalid_field(ctsio,
5366 ctl_done((union ctl_io *)ctsio);
5367 return (CTL_RETVAL_COMPLETE);
5371 * Media isn't removable, so we can't load or eject it.
5373 if ((cdb->how & SSS_LOEJ) != 0) {
5374 ctl_set_invalid_field(ctsio,
5380 ctl_done((union ctl_io *)ctsio);
5381 return (CTL_RETVAL_COMPLETE);
5384 if ((lun->flags & CTL_LUN_PR_RESERVED)
5385 && ((cdb->how & SSS_START)==0)) {
5388 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5389 if (!lun->per_res[residx].registered
5390 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5392 ctl_set_reservation_conflict(ctsio);
5393 ctl_done((union ctl_io *)ctsio);
5394 return (CTL_RETVAL_COMPLETE);
5399 * If there is no backend on this device, we can't start or stop
5400 * it. In theory we shouldn't get any start/stop commands in the
5401 * first place at this level if the LUN doesn't have a backend.
5402 * That should get stopped by the command decode code.
5404 if (lun->backend == NULL) {
5405 ctl_set_invalid_opcode(ctsio);
5406 ctl_done((union ctl_io *)ctsio);
5407 return (CTL_RETVAL_COMPLETE);
5411 * XXX KDM Copan-specific offline behavior.
5412 * Figure out a reasonable way to port this?
5415 mtx_lock(&ctl_softc->ctl_lock);
5417 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5418 && (lun->flags & CTL_LUN_OFFLINE)) {
5420 * If the LUN is offline, and the on/offline bit isn't set,
5421 * reject the start or stop. Otherwise, let it through.
5423 mtx_unlock(&ctl_softc->ctl_lock);
5424 ctl_set_lun_not_ready(ctsio);
5425 ctl_done((union ctl_io *)ctsio);
5427 mtx_unlock(&ctl_softc->ctl_lock);
5428 #endif /* NEEDTOPORT */
5430 * This could be a start or a stop when we're online,
5431 * or a stop/offline or start/online. A start or stop when
5432 * we're offline is covered in the case above.
5435 * In the non-immediate case, we send the request to
5436 * the backend and return status to the user when
5439 * In the immediate case, we allocate a new ctl_io
5440 * to hold a copy of the request, and send that to
5441 * the backend. We then set good status on the
5442 * user's request and return it immediately.
5444 if (cdb->byte2 & SSS_IMMED) {
5445 union ctl_io *new_io;
5447 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5448 if (new_io == NULL) {
5449 ctl_set_busy(ctsio);
5450 ctl_done((union ctl_io *)ctsio);
5452 ctl_copy_io((union ctl_io *)ctsio,
5454 retval = lun->backend->config_write(new_io);
5455 ctl_set_success(ctsio);
5456 ctl_done((union ctl_io *)ctsio);
5459 retval = lun->backend->config_write(
5460 (union ctl_io *)ctsio);
5469 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5470 * we don't really do anything with the LBA and length fields if the user
5471 * passes them in. Instead we'll just flush out the cache for the entire
5475 ctl_sync_cache(struct ctl_scsiio *ctsio)
5477 struct ctl_lun *lun;
5478 struct ctl_softc *ctl_softc;
5479 uint64_t starting_lba;
5480 uint32_t block_count;
5484 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5486 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5487 ctl_softc = control_softc;
5492 switch (ctsio->cdb[0]) {
5493 case SYNCHRONIZE_CACHE: {
5494 struct scsi_sync_cache *cdb;
5495 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5497 if (cdb->byte2 & SSC_RELADR)
5500 if (cdb->byte2 & SSC_IMMED)
5503 starting_lba = scsi_4btoul(cdb->begin_lba);
5504 block_count = scsi_2btoul(cdb->lb_count);
5507 case SYNCHRONIZE_CACHE_16: {
5508 struct scsi_sync_cache_16 *cdb;
5509 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5511 if (cdb->byte2 & SSC_RELADR)
5514 if (cdb->byte2 & SSC_IMMED)
5517 starting_lba = scsi_8btou64(cdb->begin_lba);
5518 block_count = scsi_4btoul(cdb->lb_count);
5522 ctl_set_invalid_opcode(ctsio);
5523 ctl_done((union ctl_io *)ctsio);
5525 break; /* NOTREACHED */
5530 * We don't support the immediate bit. Since it's in the
5531 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5532 * commands, we can just return the same error in either
5535 ctl_set_invalid_field(ctsio,
5541 ctl_done((union ctl_io *)ctsio);
5547 * We don't support the reladr bit either. It can only be
5548 * used with linked commands, and we don't support linked
5549 * commands. Since the bit is in the same place for the
5550 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5551 * just return the same error in either case.
5553 ctl_set_invalid_field(ctsio,
5559 ctl_done((union ctl_io *)ctsio);
5564 * We check the LBA and length, but don't do anything with them.
5565 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5566 * get flushed. This check will just help satisfy anyone who wants
5567 * to see an error for an out of range LBA.
5569 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5570 ctl_set_lba_out_of_range(ctsio);
5571 ctl_done((union ctl_io *)ctsio);
5576 * If this LUN has no backend, we can't flush the cache anyway.
5578 if (lun->backend == NULL) {
5579 ctl_set_invalid_opcode(ctsio);
5580 ctl_done((union ctl_io *)ctsio);
5585 * Check to see whether we're configured to send the SYNCHRONIZE
5586 * CACHE command directly to the back end.
5588 mtx_lock(&ctl_softc->ctl_lock);
5589 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5590 && (++(lun->sync_count) >= lun->sync_interval)) {
5591 lun->sync_count = 0;
5592 mtx_unlock(&ctl_softc->ctl_lock);
5593 retval = lun->backend->config_write((union ctl_io *)ctsio);
5595 mtx_unlock(&ctl_softc->ctl_lock);
5596 ctl_set_success(ctsio);
5597 ctl_done((union ctl_io *)ctsio);
5606 ctl_format(struct ctl_scsiio *ctsio)
5608 struct scsi_format *cdb;
5609 struct ctl_lun *lun;
5610 struct ctl_softc *ctl_softc;
5611 int length, defect_list_len;
5613 CTL_DEBUG_PRINT(("ctl_format\n"));
5615 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5616 ctl_softc = control_softc;
5618 cdb = (struct scsi_format *)ctsio->cdb;
5621 if (cdb->byte2 & SF_FMTDATA) {
5622 if (cdb->byte2 & SF_LONGLIST)
5623 length = sizeof(struct scsi_format_header_long);
5625 length = sizeof(struct scsi_format_header_short);
5628 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5630 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5631 ctsio->kern_data_len = length;
5632 ctsio->kern_total_len = length;
5633 ctsio->kern_data_resid = 0;
5634 ctsio->kern_rel_offset = 0;
5635 ctsio->kern_sg_entries = 0;
5636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5637 ctsio->be_move_done = ctl_config_move_done;
5638 ctl_datamove((union ctl_io *)ctsio);
5640 return (CTL_RETVAL_COMPLETE);
5643 defect_list_len = 0;
5645 if (cdb->byte2 & SF_FMTDATA) {
5646 if (cdb->byte2 & SF_LONGLIST) {
5647 struct scsi_format_header_long *header;
5649 header = (struct scsi_format_header_long *)
5650 ctsio->kern_data_ptr;
5652 defect_list_len = scsi_4btoul(header->defect_list_len);
5653 if (defect_list_len != 0) {
5654 ctl_set_invalid_field(ctsio,
5663 struct scsi_format_header_short *header;
5665 header = (struct scsi_format_header_short *)
5666 ctsio->kern_data_ptr;
5668 defect_list_len = scsi_2btoul(header->defect_list_len);
5669 if (defect_list_len != 0) {
5670 ctl_set_invalid_field(ctsio,
5682 * The format command will clear out the "Medium format corrupted"
5683 * status if set by the configuration code. That status is really
5684 * just a way to notify the host that we have lost the media, and
5685 * get them to issue a command that will basically make them think
5686 * they're blowing away the media.
5688 mtx_lock(&ctl_softc->ctl_lock);
5689 lun->flags &= ~CTL_LUN_INOPERABLE;
5690 mtx_unlock(&ctl_softc->ctl_lock);
5692 ctsio->scsi_status = SCSI_STATUS_OK;
5693 ctsio->io_hdr.status = CTL_SUCCESS;
5696 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5697 free(ctsio->kern_data_ptr, M_CTL);
5698 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5701 ctl_done((union ctl_io *)ctsio);
5702 return (CTL_RETVAL_COMPLETE);
5706 ctl_write_buffer(struct ctl_scsiio *ctsio)
5708 struct scsi_write_buffer *cdb;
5709 struct copan_page_header *header;
5710 struct ctl_lun *lun;
5711 struct ctl_softc *ctl_softc;
5712 int buffer_offset, len;
5717 retval = CTL_RETVAL_COMPLETE;
5719 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5721 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5722 ctl_softc = control_softc;
5723 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5725 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5726 ctl_set_invalid_field(ctsio,
5732 ctl_done((union ctl_io *)ctsio);
5733 return (CTL_RETVAL_COMPLETE);
5735 if (cdb->buffer_id != 0) {
5736 ctl_set_invalid_field(ctsio,
5742 ctl_done((union ctl_io *)ctsio);
5743 return (CTL_RETVAL_COMPLETE);
5746 len = scsi_3btoul(cdb->length);
5747 buffer_offset = scsi_3btoul(cdb->offset);
5749 if (len > sizeof(lun->write_buffer)) {
5750 ctl_set_invalid_field(ctsio,
5756 ctl_done((union ctl_io *)ctsio);
5757 return (CTL_RETVAL_COMPLETE);
5760 if (buffer_offset != 0) {
5761 ctl_set_invalid_field(ctsio,
5767 ctl_done((union ctl_io *)ctsio);
5768 return (CTL_RETVAL_COMPLETE);
5772 * If we've got a kernel request that hasn't been malloced yet,
5773 * malloc it and tell the caller the data buffer is here.
5775 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5776 ctsio->kern_data_ptr = lun->write_buffer;
5777 ctsio->kern_data_len = len;
5778 ctsio->kern_total_len = len;
5779 ctsio->kern_data_resid = 0;
5780 ctsio->kern_rel_offset = 0;
5781 ctsio->kern_sg_entries = 0;
5782 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5783 ctsio->be_move_done = ctl_config_move_done;
5784 ctl_datamove((union ctl_io *)ctsio);
5786 return (CTL_RETVAL_COMPLETE);
5789 ctl_done((union ctl_io *)ctsio);
5791 return (CTL_RETVAL_COMPLETE);
5795 ctl_write_same(struct ctl_scsiio *ctsio)
5797 struct ctl_lun *lun;
5798 struct ctl_lba_len_flags lbalen;
5800 uint32_t num_blocks;
5804 retval = CTL_RETVAL_COMPLETE;
5806 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5808 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5810 switch (ctsio->cdb[0]) {
5811 case WRITE_SAME_10: {
5812 struct scsi_write_same_10 *cdb;
5814 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5816 lba = scsi_4btoul(cdb->addr);
5817 num_blocks = scsi_2btoul(cdb->length);
5821 case WRITE_SAME_16: {
5822 struct scsi_write_same_16 *cdb;
5824 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5826 lba = scsi_8btou64(cdb->addr);
5827 num_blocks = scsi_4btoul(cdb->length);
5833 * We got a command we don't support. This shouldn't
5834 * happen, commands should be filtered out above us.
5836 ctl_set_invalid_opcode(ctsio);
5837 ctl_done((union ctl_io *)ctsio);
5839 return (CTL_RETVAL_COMPLETE);
5840 break; /* NOTREACHED */
5844 * The first check is to make sure we're in bounds, the second
5845 * check is to catch wrap-around problems. If the lba + num blocks
5846 * is less than the lba, then we've wrapped around and the block
5847 * range is invalid anyway.
5849 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5850 || ((lba + num_blocks) < lba)) {
5851 ctl_set_lba_out_of_range(ctsio);
5852 ctl_done((union ctl_io *)ctsio);
5853 return (CTL_RETVAL_COMPLETE);
5856 /* Zero number of blocks means "to the last logical block" */
5857 if (num_blocks == 0) {
5858 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5859 ctl_set_invalid_field(ctsio,
5865 ctl_done((union ctl_io *)ctsio);
5866 return (CTL_RETVAL_COMPLETE);
5868 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5871 len = lun->be_lun->blocksize;
5874 * If we've got a kernel request that hasn't been malloced yet,
5875 * malloc it and tell the caller the data buffer is here.
5877 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5878 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5879 ctsio->kern_data_len = len;
5880 ctsio->kern_total_len = len;
5881 ctsio->kern_data_resid = 0;
5882 ctsio->kern_rel_offset = 0;
5883 ctsio->kern_sg_entries = 0;
5884 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5885 ctsio->be_move_done = ctl_config_move_done;
5886 ctl_datamove((union ctl_io *)ctsio);
5888 return (CTL_RETVAL_COMPLETE);
5892 lbalen.len = num_blocks;
5893 lbalen.flags = byte2;
5894 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
5896 retval = lun->backend->config_write((union ctl_io *)ctsio);
5902 ctl_unmap(struct ctl_scsiio *ctsio)
5904 struct ctl_lun *lun;
5905 struct scsi_unmap *cdb;
5906 struct ctl_ptr_len_flags ptrlen;
5907 struct scsi_unmap_header *hdr;
5908 struct scsi_unmap_desc *buf, *end;
5910 uint32_t num_blocks;
5914 retval = CTL_RETVAL_COMPLETE;
5916 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5918 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5919 cdb = (struct scsi_unmap *)ctsio->cdb;
5921 len = scsi_2btoul(cdb->length);
5925 * If we've got a kernel request that hasn't been malloced yet,
5926 * malloc it and tell the caller the data buffer is here.
5928 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5929 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5930 ctsio->kern_data_len = len;
5931 ctsio->kern_total_len = len;
5932 ctsio->kern_data_resid = 0;
5933 ctsio->kern_rel_offset = 0;
5934 ctsio->kern_sg_entries = 0;
5935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5936 ctsio->be_move_done = ctl_config_move_done;
5937 ctl_datamove((union ctl_io *)ctsio);
5939 return (CTL_RETVAL_COMPLETE);
5942 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5943 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5944 if (len < sizeof (*hdr) ||
5945 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5946 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5947 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5948 ctl_set_invalid_field(ctsio,
5954 ctl_done((union ctl_io *)ctsio);
5955 return (CTL_RETVAL_COMPLETE);
5957 len = scsi_2btoul(hdr->desc_length);
5958 buf = (struct scsi_unmap_desc *)(hdr + 1);
5959 end = buf + len / sizeof(*buf);
5961 ptrlen.ptr = (void *)buf;
5963 ptrlen.flags = byte2;
5964 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &ptrlen,
5967 for (; buf < end; buf++) {
5968 lba = scsi_8btou64(buf->lba);
5969 num_blocks = scsi_4btoul(buf->length);
5970 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5971 || ((lba + num_blocks) < lba)) {
5972 ctl_set_lba_out_of_range(ctsio);
5973 ctl_done((union ctl_io *)ctsio);
5974 return (CTL_RETVAL_COMPLETE);
5978 retval = lun->backend->config_write((union ctl_io *)ctsio);
5984 * Note that this function currently doesn't actually do anything inside
5985 * CTL to enforce things if the DQue bit is turned on.
5987 * Also note that this function can't be used in the default case, because
5988 * the DQue bit isn't set in the changeable mask for the control mode page
5989 * anyway. This is just here as an example for how to implement a page
5990 * handler, and a placeholder in case we want to allow the user to turn
5991 * tagged queueing on and off.
5993 * The D_SENSE bit handling is functional, however, and will turn
5994 * descriptor sense on and off for a given LUN.
5997 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5998 struct ctl_page_index *page_index, uint8_t *page_ptr)
6000 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6001 struct ctl_lun *lun;
6002 struct ctl_softc *softc;
6006 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6007 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6010 user_cp = (struct scsi_control_page *)page_ptr;
6011 current_cp = (struct scsi_control_page *)
6012 (page_index->page_data + (page_index->page_len *
6014 saved_cp = (struct scsi_control_page *)
6015 (page_index->page_data + (page_index->page_len *
6018 softc = control_softc;
6020 mtx_lock(&softc->ctl_lock);
6021 if (((current_cp->rlec & SCP_DSENSE) == 0)
6022 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6024 * Descriptor sense is currently turned off and the user
6025 * wants to turn it on.
6027 current_cp->rlec |= SCP_DSENSE;
6028 saved_cp->rlec |= SCP_DSENSE;
6029 lun->flags |= CTL_LUN_SENSE_DESC;
6031 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6032 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6034 * Descriptor sense is currently turned on, and the user
6035 * wants to turn it off.
6037 current_cp->rlec &= ~SCP_DSENSE;
6038 saved_cp->rlec &= ~SCP_DSENSE;
6039 lun->flags &= ~CTL_LUN_SENSE_DESC;
6042 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6043 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6045 csevent_log(CSC_CTL | CSC_SHELF_SW |
6047 csevent_LogType_Trace,
6048 csevent_Severity_Information,
6049 csevent_AlertLevel_Green,
6050 csevent_FRU_Firmware,
6051 csevent_FRU_Unknown,
6052 "Received untagged to untagged transition");
6053 #endif /* NEEDTOPORT */
6056 csevent_log(CSC_CTL | CSC_SHELF_SW |
6058 csevent_LogType_ConfigChange,
6059 csevent_Severity_Information,
6060 csevent_AlertLevel_Green,
6061 csevent_FRU_Firmware,
6062 csevent_FRU_Unknown,
6063 "Received untagged to tagged "
6064 "queueing transition");
6065 #endif /* NEEDTOPORT */
6067 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6068 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6072 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6074 csevent_log(CSC_CTL | CSC_SHELF_SW |
6076 csevent_LogType_ConfigChange,
6077 csevent_Severity_Warning,
6078 csevent_AlertLevel_Yellow,
6079 csevent_FRU_Firmware,
6080 csevent_FRU_Unknown,
6081 "Received tagged queueing to untagged "
6083 #endif /* NEEDTOPORT */
6085 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6086 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6090 csevent_log(CSC_CTL | CSC_SHELF_SW |
6092 csevent_LogType_Trace,
6093 csevent_Severity_Information,
6094 csevent_AlertLevel_Green,
6095 csevent_FRU_Firmware,
6096 csevent_FRU_Unknown,
6097 "Received tagged queueing to tagged "
6098 "queueing transition");
6099 #endif /* NEEDTOPORT */
6105 * Let other initiators know that the mode
6106 * parameters for this LUN have changed.
6108 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6112 lun->pending_sense[i].ua_pending |=
6116 mtx_unlock(&softc->ctl_lock);
6122 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6123 struct ctl_page_index *page_index, uint8_t *page_ptr)
6129 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6130 struct ctl_page_index *page_index, int pc)
6132 struct copan_power_subpage *page;
6134 page = (struct copan_power_subpage *)page_index->page_data +
6135 (page_index->page_len * pc);
6138 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6140 * We don't update the changable bits for this page.
6143 case SMS_PAGE_CTRL_CURRENT >> 6:
6144 case SMS_PAGE_CTRL_DEFAULT >> 6:
6145 case SMS_PAGE_CTRL_SAVED >> 6:
6147 ctl_update_power_subpage(page);
6152 EPRINT(0, "Invalid PC %d!!", pc);
6161 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6162 struct ctl_page_index *page_index, uint8_t *page_ptr)
6164 struct copan_aps_subpage *user_sp;
6165 struct copan_aps_subpage *current_sp;
6166 union ctl_modepage_info *modepage_info;
6167 struct ctl_softc *softc;
6168 struct ctl_lun *lun;
6171 retval = CTL_RETVAL_COMPLETE;
6172 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6173 (page_index->page_len * CTL_PAGE_CURRENT));
6174 softc = control_softc;
6175 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6177 user_sp = (struct copan_aps_subpage *)page_ptr;
6179 modepage_info = (union ctl_modepage_info *)
6180 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6182 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6183 modepage_info->header.subpage = page_index->subpage;
6184 modepage_info->aps.lock_active = user_sp->lock_active;
6186 mtx_lock(&softc->ctl_lock);
6189 * If there is a request to lock the LUN and another LUN is locked
6190 * this is an error. If the requested LUN is already locked ignore
6191 * the request. If no LUN is locked attempt to lock it.
6192 * if there is a request to unlock the LUN and the LUN is currently
6193 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6194 * if another LUN is locked or no LUN is locked.
6196 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6197 if (softc->aps_locked_lun == lun->lun) {
6199 * This LUN is already locked, so we're done.
6201 retval = CTL_RETVAL_COMPLETE;
6202 } else if (softc->aps_locked_lun == 0) {
6204 * No one has the lock, pass the request to the
6207 retval = lun->backend->config_write(
6208 (union ctl_io *)ctsio);
6211 * Someone else has the lock, throw out the request.
6213 ctl_set_already_locked(ctsio);
6214 free(ctsio->kern_data_ptr, M_CTL);
6215 ctl_done((union ctl_io *)ctsio);
6218 * Set the return value so that ctl_do_mode_select()
6219 * won't try to complete the command. We already
6220 * completed it here.
6222 retval = CTL_RETVAL_ERROR;
6224 } else if (softc->aps_locked_lun == lun->lun) {
6226 * This LUN is locked, so pass the unlock request to the
6229 retval = lun->backend->config_write((union ctl_io *)ctsio);
6231 mtx_unlock(&softc->ctl_lock);
6237 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6238 struct ctl_page_index *page_index,
6244 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6249 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6250 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6251 printf("page data:");
6253 printf(" %.2x",page_ptr[i]);
6259 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6260 struct ctl_page_index *page_index,
6263 struct copan_debugconf_subpage *page;
6265 page = (struct copan_debugconf_subpage *)page_index->page_data +
6266 (page_index->page_len * pc);
6269 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6270 case SMS_PAGE_CTRL_DEFAULT >> 6:
6271 case SMS_PAGE_CTRL_SAVED >> 6:
6273 * We don't update the changable or default bits for this page.
6276 case SMS_PAGE_CTRL_CURRENT >> 6:
6277 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6278 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6282 EPRINT(0, "Invalid PC %d!!", pc);
6283 #endif /* NEEDTOPORT */
6291 ctl_do_mode_select(union ctl_io *io)
6293 struct scsi_mode_page_header *page_header;
6294 struct ctl_page_index *page_index;
6295 struct ctl_scsiio *ctsio;
6296 int control_dev, page_len;
6297 int page_len_offset, page_len_size;
6298 union ctl_modepage_info *modepage_info;
6299 struct ctl_lun *lun;
6300 int *len_left, *len_used;
6303 ctsio = &io->scsiio;
6306 retval = CTL_RETVAL_COMPLETE;
6308 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6310 if (lun->be_lun->lun_type != T_DIRECT)
6315 modepage_info = (union ctl_modepage_info *)
6316 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6317 len_left = &modepage_info->header.len_left;
6318 len_used = &modepage_info->header.len_used;
6322 page_header = (struct scsi_mode_page_header *)
6323 (ctsio->kern_data_ptr + *len_used);
6325 if (*len_left == 0) {
6326 free(ctsio->kern_data_ptr, M_CTL);
6327 ctl_set_success(ctsio);
6328 ctl_done((union ctl_io *)ctsio);
6329 return (CTL_RETVAL_COMPLETE);
6330 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6332 free(ctsio->kern_data_ptr, M_CTL);
6333 ctl_set_param_len_error(ctsio);
6334 ctl_done((union ctl_io *)ctsio);
6335 return (CTL_RETVAL_COMPLETE);
6337 } else if ((page_header->page_code & SMPH_SPF)
6338 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6340 free(ctsio->kern_data_ptr, M_CTL);
6341 ctl_set_param_len_error(ctsio);
6342 ctl_done((union ctl_io *)ctsio);
6343 return (CTL_RETVAL_COMPLETE);
6348 * XXX KDM should we do something with the block descriptor?
6350 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6352 if ((control_dev != 0)
6353 && (lun->mode_pages.index[i].page_flags &
6354 CTL_PAGE_FLAG_DISK_ONLY))
6357 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6358 (page_header->page_code & SMPH_PC_MASK))
6362 * If neither page has a subpage code, then we've got a
6365 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6366 && ((page_header->page_code & SMPH_SPF) == 0)) {
6367 page_index = &lun->mode_pages.index[i];
6368 page_len = page_header->page_length;
6373 * If both pages have subpages, then the subpage numbers
6376 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6377 && (page_header->page_code & SMPH_SPF)) {
6378 struct scsi_mode_page_header_sp *sph;
6380 sph = (struct scsi_mode_page_header_sp *)page_header;
6382 if (lun->mode_pages.index[i].subpage ==
6384 page_index = &lun->mode_pages.index[i];
6385 page_len = scsi_2btoul(sph->page_length);
6392 * If we couldn't find the page, or if we don't have a mode select
6393 * handler for it, send back an error to the user.
6395 if ((page_index == NULL)
6396 || (page_index->select_handler == NULL)) {
6397 ctl_set_invalid_field(ctsio,
6400 /*field*/ *len_used,
6403 free(ctsio->kern_data_ptr, M_CTL);
6404 ctl_done((union ctl_io *)ctsio);
6405 return (CTL_RETVAL_COMPLETE);
6408 if (page_index->page_code & SMPH_SPF) {
6409 page_len_offset = 2;
6413 page_len_offset = 1;
6417 * If the length the initiator gives us isn't the one we specify in
6418 * the mode page header, or if they didn't specify enough data in
6419 * the CDB to avoid truncating this page, kick out the request.
6421 if ((page_len != (page_index->page_len - page_len_offset -
6423 || (*len_left < page_index->page_len)) {
6426 ctl_set_invalid_field(ctsio,
6429 /*field*/ *len_used + page_len_offset,
6432 free(ctsio->kern_data_ptr, M_CTL);
6433 ctl_done((union ctl_io *)ctsio);
6434 return (CTL_RETVAL_COMPLETE);
6438 * Run through the mode page, checking to make sure that the bits
6439 * the user changed are actually legal for him to change.
6441 for (i = 0; i < page_index->page_len; i++) {
6442 uint8_t *user_byte, *change_mask, *current_byte;
6446 user_byte = (uint8_t *)page_header + i;
6447 change_mask = page_index->page_data +
6448 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6449 current_byte = page_index->page_data +
6450 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6453 * Check to see whether the user set any bits in this byte
6454 * that he is not allowed to set.
6456 if ((*user_byte & ~(*change_mask)) ==
6457 (*current_byte & ~(*change_mask)))
6461 * Go through bit by bit to determine which one is illegal.
6464 for (j = 7; j >= 0; j--) {
6465 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6466 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6471 ctl_set_invalid_field(ctsio,
6474 /*field*/ *len_used + i,
6477 free(ctsio->kern_data_ptr, M_CTL);
6478 ctl_done((union ctl_io *)ctsio);
6479 return (CTL_RETVAL_COMPLETE);
6483 * Decrement these before we call the page handler, since we may
6484 * end up getting called back one way or another before the handler
6485 * returns to this context.
6487 *len_left -= page_index->page_len;
6488 *len_used += page_index->page_len;
6490 retval = page_index->select_handler(ctsio, page_index,
6491 (uint8_t *)page_header);
6494 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6495 * wait until this queued command completes to finish processing
6496 * the mode page. If it returns anything other than
6497 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6498 * already set the sense information, freed the data pointer, and
6499 * completed the io for us.
6501 if (retval != CTL_RETVAL_COMPLETE)
6502 goto bailout_no_done;
6505 * If the initiator sent us more than one page, parse the next one.
6510 ctl_set_success(ctsio);
6511 free(ctsio->kern_data_ptr, M_CTL);
6512 ctl_done((union ctl_io *)ctsio);
6516 return (CTL_RETVAL_COMPLETE);
6521 ctl_mode_select(struct ctl_scsiio *ctsio)
6523 int param_len, pf, sp;
6524 int header_size, bd_len;
6525 int len_left, len_used;
6526 struct ctl_page_index *page_index;
6527 struct ctl_lun *lun;
6528 int control_dev, page_len;
6529 union ctl_modepage_info *modepage_info;
6541 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6543 if (lun->be_lun->lun_type != T_DIRECT)
6548 switch (ctsio->cdb[0]) {
6549 case MODE_SELECT_6: {
6550 struct scsi_mode_select_6 *cdb;
6552 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6554 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6555 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6557 param_len = cdb->length;
6558 header_size = sizeof(struct scsi_mode_header_6);
6561 case MODE_SELECT_10: {
6562 struct scsi_mode_select_10 *cdb;
6564 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6566 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6567 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6569 param_len = scsi_2btoul(cdb->length);
6570 header_size = sizeof(struct scsi_mode_header_10);
6574 ctl_set_invalid_opcode(ctsio);
6575 ctl_done((union ctl_io *)ctsio);
6576 return (CTL_RETVAL_COMPLETE);
6577 break; /* NOTREACHED */
6582 * "A parameter list length of zero indicates that the Data-Out Buffer
6583 * shall be empty. This condition shall not be considered as an error."
6585 if (param_len == 0) {
6586 ctl_set_success(ctsio);
6587 ctl_done((union ctl_io *)ctsio);
6588 return (CTL_RETVAL_COMPLETE);
6592 * Since we'll hit this the first time through, prior to
6593 * allocation, we don't need to free a data buffer here.
6595 if (param_len < header_size) {
6596 ctl_set_param_len_error(ctsio);
6597 ctl_done((union ctl_io *)ctsio);
6598 return (CTL_RETVAL_COMPLETE);
6602 * Allocate the data buffer and grab the user's data. In theory,
6603 * we shouldn't have to sanity check the parameter list length here
6604 * because the maximum size is 64K. We should be able to malloc
6605 * that much without too many problems.
6607 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6608 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6609 ctsio->kern_data_len = param_len;
6610 ctsio->kern_total_len = param_len;
6611 ctsio->kern_data_resid = 0;
6612 ctsio->kern_rel_offset = 0;
6613 ctsio->kern_sg_entries = 0;
6614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6615 ctsio->be_move_done = ctl_config_move_done;
6616 ctl_datamove((union ctl_io *)ctsio);
6618 return (CTL_RETVAL_COMPLETE);
6621 switch (ctsio->cdb[0]) {
6622 case MODE_SELECT_6: {
6623 struct scsi_mode_header_6 *mh6;
6625 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6626 bd_len = mh6->blk_desc_len;
6629 case MODE_SELECT_10: {
6630 struct scsi_mode_header_10 *mh10;
6632 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6633 bd_len = scsi_2btoul(mh10->blk_desc_len);
6637 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6641 if (param_len < (header_size + bd_len)) {
6642 free(ctsio->kern_data_ptr, M_CTL);
6643 ctl_set_param_len_error(ctsio);
6644 ctl_done((union ctl_io *)ctsio);
6645 return (CTL_RETVAL_COMPLETE);
6649 * Set the IO_CONT flag, so that if this I/O gets passed to
6650 * ctl_config_write_done(), it'll get passed back to
6651 * ctl_do_mode_select() for further processing, or completion if
6654 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6655 ctsio->io_cont = ctl_do_mode_select;
6657 modepage_info = (union ctl_modepage_info *)
6658 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6660 memset(modepage_info, 0, sizeof(*modepage_info));
6662 len_left = param_len - header_size - bd_len;
6663 len_used = header_size + bd_len;
6665 modepage_info->header.len_left = len_left;
6666 modepage_info->header.len_used = len_used;
6668 return (ctl_do_mode_select((union ctl_io *)ctsio));
6672 ctl_mode_sense(struct ctl_scsiio *ctsio)
6674 struct ctl_lun *lun;
6675 int pc, page_code, dbd, llba, subpage;
6676 int alloc_len, page_len, header_len, total_len;
6677 struct scsi_mode_block_descr *block_desc;
6678 struct ctl_page_index *page_index;
6686 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6688 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6690 if (lun->be_lun->lun_type != T_DIRECT)
6695 switch (ctsio->cdb[0]) {
6696 case MODE_SENSE_6: {
6697 struct scsi_mode_sense_6 *cdb;
6699 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6701 header_len = sizeof(struct scsi_mode_hdr_6);
6702 if (cdb->byte2 & SMS_DBD)
6705 header_len += sizeof(struct scsi_mode_block_descr);
6707 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6708 page_code = cdb->page & SMS_PAGE_CODE;
6709 subpage = cdb->subpage;
6710 alloc_len = cdb->length;
6713 case MODE_SENSE_10: {
6714 struct scsi_mode_sense_10 *cdb;
6716 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6718 header_len = sizeof(struct scsi_mode_hdr_10);
6720 if (cdb->byte2 & SMS_DBD)
6723 header_len += sizeof(struct scsi_mode_block_descr);
6724 if (cdb->byte2 & SMS10_LLBAA)
6726 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6727 page_code = cdb->page & SMS_PAGE_CODE;
6728 subpage = cdb->subpage;
6729 alloc_len = scsi_2btoul(cdb->length);
6733 ctl_set_invalid_opcode(ctsio);
6734 ctl_done((union ctl_io *)ctsio);
6735 return (CTL_RETVAL_COMPLETE);
6736 break; /* NOTREACHED */
6740 * We have to make a first pass through to calculate the size of
6741 * the pages that match the user's query. Then we allocate enough
6742 * memory to hold it, and actually copy the data into the buffer.
6744 switch (page_code) {
6745 case SMS_ALL_PAGES_PAGE: {
6751 * At the moment, values other than 0 and 0xff here are
6752 * reserved according to SPC-3.
6754 if ((subpage != SMS_SUBPAGE_PAGE_0)
6755 && (subpage != SMS_SUBPAGE_ALL)) {
6756 ctl_set_invalid_field(ctsio,
6762 ctl_done((union ctl_io *)ctsio);
6763 return (CTL_RETVAL_COMPLETE);
6766 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6767 if ((control_dev != 0)
6768 && (lun->mode_pages.index[i].page_flags &
6769 CTL_PAGE_FLAG_DISK_ONLY))
6773 * We don't use this subpage if the user didn't
6774 * request all subpages.
6776 if ((lun->mode_pages.index[i].subpage != 0)
6777 && (subpage == SMS_SUBPAGE_PAGE_0))
6781 printf("found page %#x len %d\n",
6782 lun->mode_pages.index[i].page_code &
6784 lun->mode_pages.index[i].page_len);
6786 page_len += lun->mode_pages.index[i].page_len;
6795 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6796 /* Look for the right page code */
6797 if ((lun->mode_pages.index[i].page_code &
6798 SMPH_PC_MASK) != page_code)
6801 /* Look for the right subpage or the subpage wildcard*/
6802 if ((lun->mode_pages.index[i].subpage != subpage)
6803 && (subpage != SMS_SUBPAGE_ALL))
6806 /* Make sure the page is supported for this dev type */
6807 if ((control_dev != 0)
6808 && (lun->mode_pages.index[i].page_flags &
6809 CTL_PAGE_FLAG_DISK_ONLY))
6813 printf("found page %#x len %d\n",
6814 lun->mode_pages.index[i].page_code &
6816 lun->mode_pages.index[i].page_len);
6819 page_len += lun->mode_pages.index[i].page_len;
6822 if (page_len == 0) {
6823 ctl_set_invalid_field(ctsio,
6829 ctl_done((union ctl_io *)ctsio);
6830 return (CTL_RETVAL_COMPLETE);
6836 total_len = header_len + page_len;
6838 printf("header_len = %d, page_len = %d, total_len = %d\n",
6839 header_len, page_len, total_len);
6842 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6843 ctsio->kern_sg_entries = 0;
6844 ctsio->kern_data_resid = 0;
6845 ctsio->kern_rel_offset = 0;
6846 if (total_len < alloc_len) {
6847 ctsio->residual = alloc_len - total_len;
6848 ctsio->kern_data_len = total_len;
6849 ctsio->kern_total_len = total_len;
6851 ctsio->residual = 0;
6852 ctsio->kern_data_len = alloc_len;
6853 ctsio->kern_total_len = alloc_len;
6856 switch (ctsio->cdb[0]) {
6857 case MODE_SENSE_6: {
6858 struct scsi_mode_hdr_6 *header;
6860 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6862 header->datalen = ctl_min(total_len - 1, 254);
6865 header->block_descr_len = 0;
6867 header->block_descr_len =
6868 sizeof(struct scsi_mode_block_descr);
6869 block_desc = (struct scsi_mode_block_descr *)&header[1];
6872 case MODE_SENSE_10: {
6873 struct scsi_mode_hdr_10 *header;
6876 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6878 datalen = ctl_min(total_len - 2, 65533);
6879 scsi_ulto2b(datalen, header->datalen);
6881 scsi_ulto2b(0, header->block_descr_len);
6883 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6884 header->block_descr_len);
6885 block_desc = (struct scsi_mode_block_descr *)&header[1];
6889 panic("invalid CDB type %#x", ctsio->cdb[0]);
6890 break; /* NOTREACHED */
6894 * If we've got a disk, use its blocksize in the block
6895 * descriptor. Otherwise, just set it to 0.
6898 if (control_dev != 0)
6899 scsi_ulto3b(lun->be_lun->blocksize,
6900 block_desc->block_len);
6902 scsi_ulto3b(0, block_desc->block_len);
6905 switch (page_code) {
6906 case SMS_ALL_PAGES_PAGE: {
6909 data_used = header_len;
6910 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6911 struct ctl_page_index *page_index;
6913 page_index = &lun->mode_pages.index[i];
6915 if ((control_dev != 0)
6916 && (page_index->page_flags &
6917 CTL_PAGE_FLAG_DISK_ONLY))
6921 * We don't use this subpage if the user didn't
6922 * request all subpages. We already checked (above)
6923 * to make sure the user only specified a subpage
6924 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6926 if ((page_index->subpage != 0)
6927 && (subpage == SMS_SUBPAGE_PAGE_0))
6931 * Call the handler, if it exists, to update the
6932 * page to the latest values.
6934 if (page_index->sense_handler != NULL)
6935 page_index->sense_handler(ctsio, page_index,pc);
6937 memcpy(ctsio->kern_data_ptr + data_used,
6938 page_index->page_data +
6939 (page_index->page_len * pc),
6940 page_index->page_len);
6941 data_used += page_index->page_len;
6948 data_used = header_len;
6950 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6951 struct ctl_page_index *page_index;
6953 page_index = &lun->mode_pages.index[i];
6955 /* Look for the right page code */
6956 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6959 /* Look for the right subpage or the subpage wildcard*/
6960 if ((page_index->subpage != subpage)
6961 && (subpage != SMS_SUBPAGE_ALL))
6964 /* Make sure the page is supported for this dev type */
6965 if ((control_dev != 0)
6966 && (page_index->page_flags &
6967 CTL_PAGE_FLAG_DISK_ONLY))
6971 * Call the handler, if it exists, to update the
6972 * page to the latest values.
6974 if (page_index->sense_handler != NULL)
6975 page_index->sense_handler(ctsio, page_index,pc);
6977 memcpy(ctsio->kern_data_ptr + data_used,
6978 page_index->page_data +
6979 (page_index->page_len * pc),
6980 page_index->page_len);
6981 data_used += page_index->page_len;
6987 ctsio->scsi_status = SCSI_STATUS_OK;
6989 ctsio->be_move_done = ctl_config_move_done;
6990 ctl_datamove((union ctl_io *)ctsio);
6992 return (CTL_RETVAL_COMPLETE);
6996 ctl_read_capacity(struct ctl_scsiio *ctsio)
6998 struct scsi_read_capacity *cdb;
6999 struct scsi_read_capacity_data *data;
7000 struct ctl_lun *lun;
7003 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7005 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7007 lba = scsi_4btoul(cdb->addr);
7008 if (((cdb->pmi & SRC_PMI) == 0)
7010 ctl_set_invalid_field(/*ctsio*/ ctsio,
7016 ctl_done((union ctl_io *)ctsio);
7017 return (CTL_RETVAL_COMPLETE);
7020 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7022 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7023 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7024 ctsio->residual = 0;
7025 ctsio->kern_data_len = sizeof(*data);
7026 ctsio->kern_total_len = sizeof(*data);
7027 ctsio->kern_data_resid = 0;
7028 ctsio->kern_rel_offset = 0;
7029 ctsio->kern_sg_entries = 0;
7032 * If the maximum LBA is greater than 0xfffffffe, the user must
7033 * issue a SERVICE ACTION IN (16) command, with the read capacity
7034 * serivce action set.
7036 if (lun->be_lun->maxlba > 0xfffffffe)
7037 scsi_ulto4b(0xffffffff, data->addr);
7039 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7042 * XXX KDM this may not be 512 bytes...
7044 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7046 ctsio->scsi_status = SCSI_STATUS_OK;
7048 ctsio->be_move_done = ctl_config_move_done;
7049 ctl_datamove((union ctl_io *)ctsio);
7051 return (CTL_RETVAL_COMPLETE);
7055 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7057 struct scsi_read_capacity_16 *cdb;
7058 struct scsi_read_capacity_data_long *data;
7059 struct ctl_lun *lun;
7063 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7065 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7067 alloc_len = scsi_4btoul(cdb->alloc_len);
7068 lba = scsi_8btou64(cdb->addr);
7070 if ((cdb->reladr & SRC16_PMI)
7072 ctl_set_invalid_field(/*ctsio*/ ctsio,
7078 ctl_done((union ctl_io *)ctsio);
7079 return (CTL_RETVAL_COMPLETE);
7082 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7084 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7085 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7087 if (sizeof(*data) < alloc_len) {
7088 ctsio->residual = alloc_len - sizeof(*data);
7089 ctsio->kern_data_len = sizeof(*data);
7090 ctsio->kern_total_len = sizeof(*data);
7092 ctsio->residual = 0;
7093 ctsio->kern_data_len = alloc_len;
7094 ctsio->kern_total_len = alloc_len;
7096 ctsio->kern_data_resid = 0;
7097 ctsio->kern_rel_offset = 0;
7098 ctsio->kern_sg_entries = 0;
7100 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7101 /* XXX KDM this may not be 512 bytes... */
7102 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7103 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7104 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7105 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7106 data->lalba_lbp[0] |= SRC16_LBPME;
7108 ctsio->scsi_status = SCSI_STATUS_OK;
7110 ctsio->be_move_done = ctl_config_move_done;
7111 ctl_datamove((union ctl_io *)ctsio);
7113 return (CTL_RETVAL_COMPLETE);
7117 ctl_service_action_in(struct ctl_scsiio *ctsio)
7119 struct scsi_service_action_in *cdb;
7122 CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
7124 cdb = (struct scsi_service_action_in *)ctsio->cdb;
7126 retval = CTL_RETVAL_COMPLETE;
7128 switch (cdb->service_action) {
7129 case SRC16_SERVICE_ACTION:
7130 retval = ctl_read_capacity_16(ctsio);
7133 ctl_set_invalid_field(/*ctsio*/ ctsio,
7139 ctl_done((union ctl_io *)ctsio);
7147 ctl_maintenance_in(struct ctl_scsiio *ctsio)
7149 struct scsi_maintenance_in *cdb;
7151 int alloc_len, total_len = 0;
7152 int num_target_port_groups, single;
7153 struct ctl_lun *lun;
7154 struct ctl_softc *softc;
7155 struct scsi_target_group_data *rtg_ptr;
7156 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
7157 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
7158 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
7160 CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
7162 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7163 softc = control_softc;
7164 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7166 retval = CTL_RETVAL_COMPLETE;
7168 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
7169 ctl_set_invalid_field(/*ctsio*/ ctsio,
7175 ctl_done((union ctl_io *)ctsio);
7179 mtx_lock(&softc->ctl_lock);
7180 single = ctl_is_single;
7181 mtx_unlock(&softc->ctl_lock);
7184 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
7186 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7188 total_len = sizeof(struct scsi_target_group_data) +
7189 sizeof(struct scsi_target_port_group_descriptor) *
7190 num_target_port_groups +
7191 sizeof(struct scsi_target_port_descriptor) *
7192 NUM_PORTS_PER_GRP * num_target_port_groups;
7194 alloc_len = scsi_4btoul(cdb->length);
7196 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7198 ctsio->kern_sg_entries = 0;
7200 if (total_len < alloc_len) {
7201 ctsio->residual = alloc_len - total_len;
7202 ctsio->kern_data_len = total_len;
7203 ctsio->kern_total_len = total_len;
7205 ctsio->residual = 0;
7206 ctsio->kern_data_len = alloc_len;
7207 ctsio->kern_total_len = alloc_len;
7209 ctsio->kern_data_resid = 0;
7210 ctsio->kern_rel_offset = 0;
7212 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
7214 tpg_desc_ptr1 = &rtg_ptr->groups[0];
7215 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
7216 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7217 &tp_desc_ptr1_1->desc_list[0];
7220 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7221 &tp_desc_ptr1_2->desc_list[0];
7222 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7223 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7224 &tp_desc_ptr2_1->desc_list[0];
7226 tpg_desc_ptr2 = NULL;
7227 tp_desc_ptr2_1 = NULL;
7228 tp_desc_ptr2_2 = NULL;
7231 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7233 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7234 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7235 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7236 tpg_desc_ptr2->pref_state =
7237 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7239 tpg_desc_ptr1->pref_state =
7240 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7241 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7244 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7245 tpg_desc_ptr1->pref_state =
7246 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7247 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7249 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7250 tpg_desc_ptr2->pref_state =
7251 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7255 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7257 tpg_desc_ptr1->support = 0;
7258 tpg_desc_ptr1->target_port_group[1] = 1;
7259 tpg_desc_ptr1->status = TPG_IMPLICIT;
7260 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7263 tpg_desc_ptr2->support = 0;
7264 tpg_desc_ptr2->target_port_group[1] = 2;
7265 tpg_desc_ptr2->status = TPG_IMPLICIT;
7266 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7268 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7269 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7271 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7272 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7274 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7275 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7276 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7278 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7279 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7283 ctsio->be_move_done = ctl_config_move_done;
7285 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7286 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7287 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7288 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7289 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7291 ctl_datamove((union ctl_io *)ctsio);
7296 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7298 struct scsi_per_res_in *cdb;
7299 int alloc_len, total_len = 0;
7300 /* struct scsi_per_res_in_rsrv in_data; */
7301 struct ctl_lun *lun;
7302 struct ctl_softc *softc;
7304 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7306 softc = control_softc;
7308 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7310 alloc_len = scsi_2btoul(cdb->length);
7312 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7315 mtx_lock(&softc->ctl_lock);
7316 switch (cdb->action) {
7317 case SPRI_RK: /* read keys */
7318 total_len = sizeof(struct scsi_per_res_in_keys) +
7320 sizeof(struct scsi_per_res_key);
7322 case SPRI_RR: /* read reservation */
7323 if (lun->flags & CTL_LUN_PR_RESERVED)
7324 total_len = sizeof(struct scsi_per_res_in_rsrv);
7326 total_len = sizeof(struct scsi_per_res_in_header);
7328 case SPRI_RC: /* report capabilities */
7329 total_len = sizeof(struct scsi_per_res_cap);
7331 case SPRI_RS: /* read full status */
7333 mtx_unlock(&softc->ctl_lock);
7334 ctl_set_invalid_field(ctsio,
7340 ctl_done((union ctl_io *)ctsio);
7341 return (CTL_RETVAL_COMPLETE);
7342 break; /* NOTREACHED */
7344 mtx_unlock(&softc->ctl_lock);
7346 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7348 if (total_len < alloc_len) {
7349 ctsio->residual = alloc_len - total_len;
7350 ctsio->kern_data_len = total_len;
7351 ctsio->kern_total_len = total_len;
7353 ctsio->residual = 0;
7354 ctsio->kern_data_len = alloc_len;
7355 ctsio->kern_total_len = alloc_len;
7358 ctsio->kern_data_resid = 0;
7359 ctsio->kern_rel_offset = 0;
7360 ctsio->kern_sg_entries = 0;
7362 mtx_lock(&softc->ctl_lock);
7363 switch (cdb->action) {
7364 case SPRI_RK: { // read keys
7365 struct scsi_per_res_in_keys *res_keys;
7368 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7371 * We had to drop the lock to allocate our buffer, which
7372 * leaves time for someone to come in with another
7373 * persistent reservation. (That is unlikely, though,
7374 * since this should be the only persistent reservation
7375 * command active right now.)
7377 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7378 (lun->pr_key_count *
7379 sizeof(struct scsi_per_res_key)))){
7380 mtx_unlock(&softc->ctl_lock);
7381 free(ctsio->kern_data_ptr, M_CTL);
7382 printf("%s: reservation length changed, retrying\n",
7387 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7389 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7390 lun->pr_key_count, res_keys->header.length);
7392 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7393 if (!lun->per_res[i].registered)
7397 * We used lun->pr_key_count to calculate the
7398 * size to allocate. If it turns out the number of
7399 * initiators with the registered flag set is
7400 * larger than that (i.e. they haven't been kept in
7401 * sync), we've got a problem.
7403 if (key_count >= lun->pr_key_count) {
7405 csevent_log(CSC_CTL | CSC_SHELF_SW |
7407 csevent_LogType_Fault,
7408 csevent_AlertLevel_Yellow,
7409 csevent_FRU_ShelfController,
7410 csevent_FRU_Firmware,
7411 csevent_FRU_Unknown,
7412 "registered keys %d >= key "
7413 "count %d", key_count,
7419 memcpy(res_keys->keys[key_count].key,
7420 lun->per_res[i].res_key.key,
7421 ctl_min(sizeof(res_keys->keys[key_count].key),
7422 sizeof(lun->per_res[i].res_key)));
7427 case SPRI_RR: { // read reservation
7428 struct scsi_per_res_in_rsrv *res;
7429 int tmp_len, header_only;
7431 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7433 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7435 if (lun->flags & CTL_LUN_PR_RESERVED)
7437 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7438 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7439 res->header.length);
7442 tmp_len = sizeof(struct scsi_per_res_in_header);
7443 scsi_ulto4b(0, res->header.length);
7448 * We had to drop the lock to allocate our buffer, which
7449 * leaves time for someone to come in with another
7450 * persistent reservation. (That is unlikely, though,
7451 * since this should be the only persistent reservation
7452 * command active right now.)
7454 if (tmp_len != total_len) {
7455 mtx_unlock(&softc->ctl_lock);
7456 free(ctsio->kern_data_ptr, M_CTL);
7457 printf("%s: reservation status changed, retrying\n",
7463 * No reservation held, so we're done.
7465 if (header_only != 0)
7469 * If the registration is an All Registrants type, the key
7470 * is 0, since it doesn't really matter.
7472 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7473 memcpy(res->data.reservation,
7474 &lun->per_res[lun->pr_res_idx].res_key,
7475 sizeof(struct scsi_per_res_key));
7477 res->data.scopetype = lun->res_type;
7480 case SPRI_RC: //report capabilities
7482 struct scsi_per_res_cap *res_cap;
7485 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7486 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7487 res_cap->flags2 |= SPRI_TMV;
7488 type_mask = SPRI_TM_WR_EX_AR |
7494 scsi_ulto2b(type_mask, res_cap->type_mask);
7497 case SPRI_RS: //read full status
7500 * This is a bug, because we just checked for this above,
7501 * and should have returned an error.
7503 panic("Invalid PR type %x", cdb->action);
7504 break; /* NOTREACHED */
7506 mtx_unlock(&softc->ctl_lock);
7508 ctsio->be_move_done = ctl_config_move_done;
7510 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7511 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7512 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7513 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7514 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7516 ctl_datamove((union ctl_io *)ctsio);
7518 return (CTL_RETVAL_COMPLETE);
7522 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7526 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7527 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7528 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7529 struct scsi_per_res_out_parms* param)
7531 union ctl_ha_msg persis_io;
7537 if (sa_res_key == 0) {
7538 mtx_lock(&softc->ctl_lock);
7539 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7540 /* validate scope and type */
7541 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7543 mtx_unlock(&softc->ctl_lock);
7544 ctl_set_invalid_field(/*ctsio*/ ctsio,
7550 ctl_done((union ctl_io *)ctsio);
7554 if (type>8 || type==2 || type==4 || type==0) {
7555 mtx_unlock(&softc->ctl_lock);
7556 ctl_set_invalid_field(/*ctsio*/ ctsio,
7562 ctl_done((union ctl_io *)ctsio);
7566 /* temporarily unregister this nexus */
7567 lun->per_res[residx].registered = 0;
7570 * Unregister everybody else and build UA for
7573 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7574 if (lun->per_res[i].registered == 0)
7578 && i <CTL_MAX_INITIATORS)
7579 lun->pending_sense[i].ua_pending |=
7581 else if (persis_offset
7582 && i >= persis_offset)
7583 lun->pending_sense[i-persis_offset
7586 lun->per_res[i].registered = 0;
7587 memset(&lun->per_res[i].res_key, 0,
7588 sizeof(struct scsi_per_res_key));
7590 lun->per_res[residx].registered = 1;
7591 lun->pr_key_count = 1;
7592 lun->res_type = type;
7593 if (lun->res_type != SPR_TYPE_WR_EX_AR
7594 && lun->res_type != SPR_TYPE_EX_AC_AR)
7595 lun->pr_res_idx = residx;
7597 mtx_unlock(&softc->ctl_lock);
7598 /* send msg to other side */
7599 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7600 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7601 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7602 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7603 persis_io.pr.pr_info.res_type = type;
7604 memcpy(persis_io.pr.pr_info.sa_res_key,
7605 param->serv_act_res_key,
7606 sizeof(param->serv_act_res_key));
7607 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7608 &persis_io, sizeof(persis_io), 0)) >
7609 CTL_HA_STATUS_SUCCESS) {
7610 printf("CTL:Persis Out error returned "
7611 "from ctl_ha_msg_send %d\n",
7615 /* not all registrants */
7616 mtx_unlock(&softc->ctl_lock);
7617 free(ctsio->kern_data_ptr, M_CTL);
7618 ctl_set_invalid_field(ctsio,
7624 ctl_done((union ctl_io *)ctsio);
7627 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7628 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7631 mtx_lock(&softc->ctl_lock);
7632 if (res_key == sa_res_key) {
7635 * The spec implies this is not good but doesn't
7636 * say what to do. There are two choices either
7637 * generate a res conflict or check condition
7638 * with illegal field in parameter data. Since
7639 * that is what is done when the sa_res_key is
7640 * zero I'll take that approach since this has
7641 * to do with the sa_res_key.
7643 mtx_unlock(&softc->ctl_lock);
7644 free(ctsio->kern_data_ptr, M_CTL);
7645 ctl_set_invalid_field(ctsio,
7651 ctl_done((union ctl_io *)ctsio);
7655 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7656 if (lun->per_res[i].registered
7657 && memcmp(param->serv_act_res_key,
7658 lun->per_res[i].res_key.key,
7659 sizeof(struct scsi_per_res_key)) != 0)
7663 lun->per_res[i].registered = 0;
7664 memset(&lun->per_res[i].res_key, 0,
7665 sizeof(struct scsi_per_res_key));
7666 lun->pr_key_count--;
7669 && i < CTL_MAX_INITIATORS)
7670 lun->pending_sense[i].ua_pending |=
7672 else if (persis_offset
7673 && i >= persis_offset)
7674 lun->pending_sense[i-persis_offset].ua_pending|=
7677 mtx_unlock(&softc->ctl_lock);
7679 free(ctsio->kern_data_ptr, M_CTL);
7680 ctl_set_reservation_conflict(ctsio);
7681 ctl_done((union ctl_io *)ctsio);
7682 return (CTL_RETVAL_COMPLETE);
7684 /* send msg to other side */
7685 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7686 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7687 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7688 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7689 persis_io.pr.pr_info.res_type = type;
7690 memcpy(persis_io.pr.pr_info.sa_res_key,
7691 param->serv_act_res_key,
7692 sizeof(param->serv_act_res_key));
7693 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7694 &persis_io, sizeof(persis_io), 0)) >
7695 CTL_HA_STATUS_SUCCESS) {
7696 printf("CTL:Persis Out error returned from "
7697 "ctl_ha_msg_send %d\n", isc_retval);
7700 /* Reserved but not all registrants */
7701 /* sa_res_key is res holder */
7702 if (memcmp(param->serv_act_res_key,
7703 lun->per_res[lun->pr_res_idx].res_key.key,
7704 sizeof(struct scsi_per_res_key)) == 0) {
7705 /* validate scope and type */
7706 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7708 ctl_set_invalid_field(/*ctsio*/ ctsio,
7714 ctl_done((union ctl_io *)ctsio);
7718 if (type>8 || type==2 || type==4 || type==0) {
7719 ctl_set_invalid_field(/*ctsio*/ ctsio,
7725 ctl_done((union ctl_io *)ctsio);
7731 * if sa_res_key != res_key remove all
7732 * registrants w/sa_res_key and generate UA
7733 * for these registrants(Registrations
7734 * Preempted) if it wasn't an exclusive
7735 * reservation generate UA(Reservations
7736 * Preempted) for all other registered nexuses
7737 * if the type has changed. Establish the new
7738 * reservation and holder. If res_key and
7739 * sa_res_key are the same do the above
7740 * except don't unregister the res holder.
7744 * Temporarily unregister so it won't get
7745 * removed or UA generated
7747 lun->per_res[residx].registered = 0;
7748 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7749 if (lun->per_res[i].registered == 0)
7752 if (memcmp(param->serv_act_res_key,
7753 lun->per_res[i].res_key.key,
7754 sizeof(struct scsi_per_res_key)) == 0) {
7755 lun->per_res[i].registered = 0;
7756 memset(&lun->per_res[i].res_key,
7758 sizeof(struct scsi_per_res_key));
7759 lun->pr_key_count--;
7762 && i < CTL_MAX_INITIATORS)
7763 lun->pending_sense[i
7766 else if (persis_offset
7767 && i >= persis_offset)
7769 i-persis_offset].ua_pending |=
7771 } else if (type != lun->res_type
7772 && (lun->res_type == SPR_TYPE_WR_EX_RO
7773 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7775 && i < CTL_MAX_INITIATORS)
7776 lun->pending_sense[i
7779 else if (persis_offset
7780 && i >= persis_offset)
7787 lun->per_res[residx].registered = 1;
7788 lun->res_type = type;
7789 if (lun->res_type != SPR_TYPE_WR_EX_AR
7790 && lun->res_type != SPR_TYPE_EX_AC_AR)
7791 lun->pr_res_idx = residx;
7794 CTL_PR_ALL_REGISTRANTS;
7796 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7797 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7798 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7799 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7800 persis_io.pr.pr_info.res_type = type;
7801 memcpy(persis_io.pr.pr_info.sa_res_key,
7802 param->serv_act_res_key,
7803 sizeof(param->serv_act_res_key));
7804 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7805 &persis_io, sizeof(persis_io), 0)) >
7806 CTL_HA_STATUS_SUCCESS) {
7807 printf("CTL:Persis Out error returned "
7808 "from ctl_ha_msg_send %d\n",
7813 * sa_res_key is not the res holder just
7814 * remove registrants
7817 mtx_lock(&softc->ctl_lock);
7819 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7820 if (memcmp(param->serv_act_res_key,
7821 lun->per_res[i].res_key.key,
7822 sizeof(struct scsi_per_res_key)) != 0)
7826 lun->per_res[i].registered = 0;
7827 memset(&lun->per_res[i].res_key, 0,
7828 sizeof(struct scsi_per_res_key));
7829 lun->pr_key_count--;
7832 && i < CTL_MAX_INITIATORS)
7833 lun->pending_sense[i].ua_pending |=
7835 else if (persis_offset
7836 && i >= persis_offset)
7838 i-persis_offset].ua_pending |=
7843 mtx_unlock(&softc->ctl_lock);
7844 free(ctsio->kern_data_ptr, M_CTL);
7845 ctl_set_reservation_conflict(ctsio);
7846 ctl_done((union ctl_io *)ctsio);
7849 mtx_unlock(&softc->ctl_lock);
7850 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7851 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7852 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7853 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7854 persis_io.pr.pr_info.res_type = type;
7855 memcpy(persis_io.pr.pr_info.sa_res_key,
7856 param->serv_act_res_key,
7857 sizeof(param->serv_act_res_key));
7858 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7859 &persis_io, sizeof(persis_io), 0)) >
7860 CTL_HA_STATUS_SUCCESS) {
7861 printf("CTL:Persis Out error returned "
7862 "from ctl_ha_msg_send %d\n",
7868 lun->PRGeneration++;
7874 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7878 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7879 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7880 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7881 msg->pr.pr_info.sa_res_key,
7882 sizeof(struct scsi_per_res_key)) != 0) {
7883 uint64_t sa_res_key;
7884 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7886 if (sa_res_key == 0) {
7887 /* temporarily unregister this nexus */
7888 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7891 * Unregister everybody else and build UA for
7894 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7895 if (lun->per_res[i].registered == 0)
7899 && i < CTL_MAX_INITIATORS)
7900 lun->pending_sense[i].ua_pending |=
7902 else if (persis_offset && i >= persis_offset)
7903 lun->pending_sense[i -
7904 persis_offset].ua_pending |=
7906 lun->per_res[i].registered = 0;
7907 memset(&lun->per_res[i].res_key, 0,
7908 sizeof(struct scsi_per_res_key));
7911 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7912 lun->pr_key_count = 1;
7913 lun->res_type = msg->pr.pr_info.res_type;
7914 if (lun->res_type != SPR_TYPE_WR_EX_AR
7915 && lun->res_type != SPR_TYPE_EX_AC_AR)
7916 lun->pr_res_idx = msg->pr.pr_info.residx;
7918 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7919 if (memcmp(msg->pr.pr_info.sa_res_key,
7920 lun->per_res[i].res_key.key,
7921 sizeof(struct scsi_per_res_key)) != 0)
7924 lun->per_res[i].registered = 0;
7925 memset(&lun->per_res[i].res_key, 0,
7926 sizeof(struct scsi_per_res_key));
7927 lun->pr_key_count--;
7930 && i < persis_offset)
7931 lun->pending_sense[i].ua_pending |=
7933 else if (persis_offset
7934 && i >= persis_offset)
7935 lun->pending_sense[i -
7936 persis_offset].ua_pending |=
7942 * Temporarily unregister so it won't get removed
7945 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7946 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7947 if (lun->per_res[i].registered == 0)
7950 if (memcmp(msg->pr.pr_info.sa_res_key,
7951 lun->per_res[i].res_key.key,
7952 sizeof(struct scsi_per_res_key)) == 0) {
7953 lun->per_res[i].registered = 0;
7954 memset(&lun->per_res[i].res_key, 0,
7955 sizeof(struct scsi_per_res_key));
7956 lun->pr_key_count--;
7958 && i < CTL_MAX_INITIATORS)
7959 lun->pending_sense[i].ua_pending |=
7961 else if (persis_offset
7962 && i >= persis_offset)
7963 lun->pending_sense[i -
7964 persis_offset].ua_pending |=
7966 } else if (msg->pr.pr_info.res_type != lun->res_type
7967 && (lun->res_type == SPR_TYPE_WR_EX_RO
7968 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7970 && i < persis_offset)
7971 lun->pending_sense[i
7974 else if (persis_offset
7975 && i >= persis_offset)
7976 lun->pending_sense[i -
7977 persis_offset].ua_pending |=
7981 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7982 lun->res_type = msg->pr.pr_info.res_type;
7983 if (lun->res_type != SPR_TYPE_WR_EX_AR
7984 && lun->res_type != SPR_TYPE_EX_AC_AR)
7985 lun->pr_res_idx = msg->pr.pr_info.residx;
7987 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7989 lun->PRGeneration++;
7995 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
7999 u_int32_t param_len;
8000 struct scsi_per_res_out *cdb;
8001 struct ctl_lun *lun;
8002 struct scsi_per_res_out_parms* param;
8003 struct ctl_softc *softc;
8005 uint64_t res_key, sa_res_key;
8007 union ctl_ha_msg persis_io;
8010 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8012 retval = CTL_RETVAL_COMPLETE;
8014 softc = control_softc;
8016 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8017 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8020 * We only support whole-LUN scope. The scope & type are ignored for
8021 * register, register and ignore existing key and clear.
8022 * We sometimes ignore scope and type on preempts too!!
8023 * Verify reservation type here as well.
8025 type = cdb->scope_type & SPR_TYPE_MASK;
8026 if ((cdb->action == SPRO_RESERVE)
8027 || (cdb->action == SPRO_RELEASE)) {
8028 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8029 ctl_set_invalid_field(/*ctsio*/ ctsio,
8035 ctl_done((union ctl_io *)ctsio);
8036 return (CTL_RETVAL_COMPLETE);
8039 if (type>8 || type==2 || type==4 || type==0) {
8040 ctl_set_invalid_field(/*ctsio*/ ctsio,
8046 ctl_done((union ctl_io *)ctsio);
8047 return (CTL_RETVAL_COMPLETE);
8051 switch (cdb->action & SPRO_ACTION_MASK) {
8062 ctl_set_invalid_field(/*ctsio*/ ctsio,
8068 ctl_done((union ctl_io *)ctsio);
8069 return (CTL_RETVAL_COMPLETE);
8070 break; /* NOTREACHED */
8073 param_len = scsi_4btoul(cdb->length);
8075 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8076 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8077 ctsio->kern_data_len = param_len;
8078 ctsio->kern_total_len = param_len;
8079 ctsio->kern_data_resid = 0;
8080 ctsio->kern_rel_offset = 0;
8081 ctsio->kern_sg_entries = 0;
8082 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8083 ctsio->be_move_done = ctl_config_move_done;
8084 ctl_datamove((union ctl_io *)ctsio);
8086 return (CTL_RETVAL_COMPLETE);
8089 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8091 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8092 res_key = scsi_8btou64(param->res_key.key);
8093 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8096 * Validate the reservation key here except for SPRO_REG_IGNO
8097 * This must be done for all other service actions
8099 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8100 mtx_lock(&softc->ctl_lock);
8101 if (lun->per_res[residx].registered) {
8102 if (memcmp(param->res_key.key,
8103 lun->per_res[residx].res_key.key,
8104 ctl_min(sizeof(param->res_key),
8105 sizeof(lun->per_res[residx].res_key))) != 0) {
8107 * The current key passed in doesn't match
8108 * the one the initiator previously
8111 mtx_unlock(&softc->ctl_lock);
8112 free(ctsio->kern_data_ptr, M_CTL);
8113 ctl_set_reservation_conflict(ctsio);
8114 ctl_done((union ctl_io *)ctsio);
8115 return (CTL_RETVAL_COMPLETE);
8117 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8119 * We are not registered
8121 mtx_unlock(&softc->ctl_lock);
8122 free(ctsio->kern_data_ptr, M_CTL);
8123 ctl_set_reservation_conflict(ctsio);
8124 ctl_done((union ctl_io *)ctsio);
8125 return (CTL_RETVAL_COMPLETE);
8126 } else if (res_key != 0) {
8128 * We are not registered and trying to register but
8129 * the register key isn't zero.
8131 mtx_unlock(&softc->ctl_lock);
8132 free(ctsio->kern_data_ptr, M_CTL);
8133 ctl_set_reservation_conflict(ctsio);
8134 ctl_done((union ctl_io *)ctsio);
8135 return (CTL_RETVAL_COMPLETE);
8137 mtx_unlock(&softc->ctl_lock);
8140 switch (cdb->action & SPRO_ACTION_MASK) {
8142 case SPRO_REG_IGNO: {
8145 printf("Registration received\n");
8149 * We don't support any of these options, as we report in
8150 * the read capabilities request (see
8151 * ctl_persistent_reserve_in(), above).
8153 if ((param->flags & SPR_SPEC_I_PT)
8154 || (param->flags & SPR_ALL_TG_PT)
8155 || (param->flags & SPR_APTPL)) {
8158 if (param->flags & SPR_APTPL)
8160 else if (param->flags & SPR_ALL_TG_PT)
8162 else /* SPR_SPEC_I_PT */
8165 free(ctsio->kern_data_ptr, M_CTL);
8166 ctl_set_invalid_field(ctsio,
8172 ctl_done((union ctl_io *)ctsio);
8173 return (CTL_RETVAL_COMPLETE);
8176 mtx_lock(&softc->ctl_lock);
8179 * The initiator wants to clear the
8182 if (sa_res_key == 0) {
8184 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8185 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8186 && !lun->per_res[residx].registered)) {
8187 mtx_unlock(&softc->ctl_lock);
8191 lun->per_res[residx].registered = 0;
8192 memset(&lun->per_res[residx].res_key,
8193 0, sizeof(lun->per_res[residx].res_key));
8194 lun->pr_key_count--;
8196 if (residx == lun->pr_res_idx) {
8197 lun->flags &= ~CTL_LUN_PR_RESERVED;
8198 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8200 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8201 || lun->res_type == SPR_TYPE_EX_AC_RO)
8202 && lun->pr_key_count) {
8204 * If the reservation is a registrants
8205 * only type we need to generate a UA
8206 * for other registered inits. The
8207 * sense code should be RESERVATIONS
8211 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8213 i+persis_offset].registered
8216 lun->pending_sense[i
8222 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8223 if (lun->pr_key_count==0) {
8224 lun->flags &= ~CTL_LUN_PR_RESERVED;
8226 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8229 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8230 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8231 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8232 persis_io.pr.pr_info.residx = residx;
8233 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8234 &persis_io, sizeof(persis_io), 0 )) >
8235 CTL_HA_STATUS_SUCCESS) {
8236 printf("CTL:Persis Out error returned from "
8237 "ctl_ha_msg_send %d\n", isc_retval);
8239 mtx_unlock(&softc->ctl_lock);
8240 } else /* sa_res_key != 0 */ {
8243 * If we aren't registered currently then increment
8244 * the key count and set the registered flag.
8246 if (!lun->per_res[residx].registered) {
8247 lun->pr_key_count++;
8248 lun->per_res[residx].registered = 1;
8251 memcpy(&lun->per_res[residx].res_key,
8252 param->serv_act_res_key,
8253 ctl_min(sizeof(param->serv_act_res_key),
8254 sizeof(lun->per_res[residx].res_key)));
8256 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8257 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8258 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8259 persis_io.pr.pr_info.residx = residx;
8260 memcpy(persis_io.pr.pr_info.sa_res_key,
8261 param->serv_act_res_key,
8262 sizeof(param->serv_act_res_key));
8263 mtx_unlock(&softc->ctl_lock);
8264 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8265 &persis_io, sizeof(persis_io), 0)) >
8266 CTL_HA_STATUS_SUCCESS) {
8267 printf("CTL:Persis Out error returned from "
8268 "ctl_ha_msg_send %d\n", isc_retval);
8271 lun->PRGeneration++;
8277 printf("Reserve executed type %d\n", type);
8279 mtx_lock(&softc->ctl_lock);
8280 if (lun->flags & CTL_LUN_PR_RESERVED) {
8282 * if this isn't the reservation holder and it's
8283 * not a "all registrants" type or if the type is
8284 * different then we have a conflict
8286 if ((lun->pr_res_idx != residx
8287 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8288 || lun->res_type != type) {
8289 mtx_unlock(&softc->ctl_lock);
8290 free(ctsio->kern_data_ptr, M_CTL);
8291 ctl_set_reservation_conflict(ctsio);
8292 ctl_done((union ctl_io *)ctsio);
8293 return (CTL_RETVAL_COMPLETE);
8295 mtx_unlock(&softc->ctl_lock);
8296 } else /* create a reservation */ {
8298 * If it's not an "all registrants" type record
8299 * reservation holder
8301 if (type != SPR_TYPE_WR_EX_AR
8302 && type != SPR_TYPE_EX_AC_AR)
8303 lun->pr_res_idx = residx; /* Res holder */
8305 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8307 lun->flags |= CTL_LUN_PR_RESERVED;
8308 lun->res_type = type;
8310 mtx_unlock(&softc->ctl_lock);
8312 /* send msg to other side */
8313 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8314 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8315 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8316 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8317 persis_io.pr.pr_info.res_type = type;
8318 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8319 &persis_io, sizeof(persis_io), 0)) >
8320 CTL_HA_STATUS_SUCCESS) {
8321 printf("CTL:Persis Out error returned from "
8322 "ctl_ha_msg_send %d\n", isc_retval);
8328 mtx_lock(&softc->ctl_lock);
8329 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8330 /* No reservation exists return good status */
8331 mtx_unlock(&softc->ctl_lock);
8335 * Is this nexus a reservation holder?
8337 if (lun->pr_res_idx != residx
8338 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8340 * not a res holder return good status but
8343 mtx_unlock(&softc->ctl_lock);
8347 if (lun->res_type != type) {
8348 mtx_unlock(&softc->ctl_lock);
8349 free(ctsio->kern_data_ptr, M_CTL);
8350 ctl_set_illegal_pr_release(ctsio);
8351 ctl_done((union ctl_io *)ctsio);
8352 return (CTL_RETVAL_COMPLETE);
8355 /* okay to release */
8356 lun->flags &= ~CTL_LUN_PR_RESERVED;
8357 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8361 * if this isn't an exclusive access
8362 * res generate UA for all other
8365 if (type != SPR_TYPE_EX_AC
8366 && type != SPR_TYPE_WR_EX) {
8368 * temporarily unregister so we don't generate UA
8370 lun->per_res[residx].registered = 0;
8372 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8373 if (lun->per_res[i+persis_offset].registered
8376 lun->pending_sense[i].ua_pending |=
8380 lun->per_res[residx].registered = 1;
8382 mtx_unlock(&softc->ctl_lock);
8383 /* Send msg to other side */
8384 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8385 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8386 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8387 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8388 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8389 printf("CTL:Persis Out error returned from "
8390 "ctl_ha_msg_send %d\n", isc_retval);
8395 /* send msg to other side */
8397 mtx_lock(&softc->ctl_lock);
8398 lun->flags &= ~CTL_LUN_PR_RESERVED;
8400 lun->pr_key_count = 0;
8401 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8404 memset(&lun->per_res[residx].res_key,
8405 0, sizeof(lun->per_res[residx].res_key));
8406 lun->per_res[residx].registered = 0;
8408 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8409 if (lun->per_res[i].registered) {
8410 if (!persis_offset && i < CTL_MAX_INITIATORS)
8411 lun->pending_sense[i].ua_pending |=
8413 else if (persis_offset && i >= persis_offset)
8414 lun->pending_sense[i-persis_offset
8415 ].ua_pending |= CTL_UA_RES_PREEMPT;
8417 memset(&lun->per_res[i].res_key,
8418 0, sizeof(struct scsi_per_res_key));
8419 lun->per_res[i].registered = 0;
8421 lun->PRGeneration++;
8422 mtx_unlock(&softc->ctl_lock);
8423 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8424 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8425 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8426 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8427 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8428 printf("CTL:Persis Out error returned from "
8429 "ctl_ha_msg_send %d\n", isc_retval);
8433 case SPRO_PREEMPT: {
8436 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8437 residx, ctsio, cdb, param);
8439 return (CTL_RETVAL_COMPLETE);
8445 free(ctsio->kern_data_ptr, M_CTL);
8446 ctl_set_invalid_field(/*ctsio*/ ctsio,
8452 ctl_done((union ctl_io *)ctsio);
8453 return (CTL_RETVAL_COMPLETE);
8454 break; /* NOTREACHED */
8458 free(ctsio->kern_data_ptr, M_CTL);
8459 ctl_set_success(ctsio);
8460 ctl_done((union ctl_io *)ctsio);
8466 * This routine is for handling a message from the other SC pertaining to
8467 * persistent reserve out. All the error checking will have been done
8468 * so only perorming the action need be done here to keep the two
8472 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8474 struct ctl_lun *lun;
8475 struct ctl_softc *softc;
8479 softc = control_softc;
8481 mtx_lock(&softc->ctl_lock);
8483 targ_lun = msg->hdr.nexus.targ_lun;
8484 if (msg->hdr.nexus.lun_map_fn != NULL)
8485 targ_lun = msg->hdr.nexus.lun_map_fn(msg->hdr.nexus.lun_map_arg, targ_lun);
8486 lun = softc->ctl_luns[targ_lun];
8487 switch(msg->pr.pr_info.action) {
8488 case CTL_PR_REG_KEY:
8489 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8490 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8491 lun->pr_key_count++;
8493 lun->PRGeneration++;
8494 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8495 msg->pr.pr_info.sa_res_key,
8496 sizeof(struct scsi_per_res_key));
8499 case CTL_PR_UNREG_KEY:
8500 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8501 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8502 0, sizeof(struct scsi_per_res_key));
8503 lun->pr_key_count--;
8505 /* XXX Need to see if the reservation has been released */
8506 /* if so do we need to generate UA? */
8507 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8508 lun->flags &= ~CTL_LUN_PR_RESERVED;
8509 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8511 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8512 || lun->res_type == SPR_TYPE_EX_AC_RO)
8513 && lun->pr_key_count) {
8515 * If the reservation is a registrants
8516 * only type we need to generate a UA
8517 * for other registered inits. The
8518 * sense code should be RESERVATIONS
8522 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8524 persis_offset].registered == 0)
8527 lun->pending_sense[i
8533 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8534 if (lun->pr_key_count==0) {
8535 lun->flags &= ~CTL_LUN_PR_RESERVED;
8537 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8540 lun->PRGeneration++;
8543 case CTL_PR_RESERVE:
8544 lun->flags |= CTL_LUN_PR_RESERVED;
8545 lun->res_type = msg->pr.pr_info.res_type;
8546 lun->pr_res_idx = msg->pr.pr_info.residx;
8550 case CTL_PR_RELEASE:
8552 * if this isn't an exclusive access res generate UA for all
8553 * other registrants.
8555 if (lun->res_type != SPR_TYPE_EX_AC
8556 && lun->res_type != SPR_TYPE_WR_EX) {
8557 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8558 if (lun->per_res[i+persis_offset].registered)
8559 lun->pending_sense[i].ua_pending |=
8563 lun->flags &= ~CTL_LUN_PR_RESERVED;
8564 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8568 case CTL_PR_PREEMPT:
8569 ctl_pro_preempt_other(lun, msg);
8572 lun->flags &= ~CTL_LUN_PR_RESERVED;
8574 lun->pr_key_count = 0;
8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8577 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8578 if (lun->per_res[i].registered == 0)
8581 && i < CTL_MAX_INITIATORS)
8582 lun->pending_sense[i].ua_pending |=
8584 else if (persis_offset
8585 && i >= persis_offset)
8586 lun->pending_sense[i-persis_offset].ua_pending|=
8588 memset(&lun->per_res[i].res_key, 0,
8589 sizeof(struct scsi_per_res_key));
8590 lun->per_res[i].registered = 0;
8592 lun->PRGeneration++;
8596 mtx_unlock(&softc->ctl_lock);
8600 ctl_read_write(struct ctl_scsiio *ctsio)
8602 struct ctl_lun *lun;
8603 struct ctl_lba_len lbalen;
8605 uint32_t num_blocks;
8606 int reladdr, fua, dpo, ebp;
8610 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8612 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8619 retval = CTL_RETVAL_COMPLETE;
8621 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8622 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8623 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8627 * XXX KDM need a lock here.
8629 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8630 if ((lun->res_type == SPR_TYPE_EX_AC
8631 && residx != lun->pr_res_idx)
8632 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8633 || lun->res_type == SPR_TYPE_EX_AC_AR)
8634 && !lun->per_res[residx].registered)) {
8635 ctl_set_reservation_conflict(ctsio);
8636 ctl_done((union ctl_io *)ctsio);
8637 return (CTL_RETVAL_COMPLETE);
8641 switch (ctsio->cdb[0]) {
8644 struct scsi_rw_6 *cdb;
8646 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8648 lba = scsi_3btoul(cdb->addr);
8649 /* only 5 bits are valid in the most significant address byte */
8651 num_blocks = cdb->length;
8653 * This is correct according to SBC-2.
8655 if (num_blocks == 0)
8661 struct scsi_rw_10 *cdb;
8663 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8665 if (cdb->byte2 & SRW10_RELADDR)
8667 if (cdb->byte2 & SRW10_FUA)
8669 if (cdb->byte2 & SRW10_DPO)
8672 if ((cdb->opcode == WRITE_10)
8673 && (cdb->byte2 & SRW10_EBP))
8676 lba = scsi_4btoul(cdb->addr);
8677 num_blocks = scsi_2btoul(cdb->length);
8680 case WRITE_VERIFY_10: {
8681 struct scsi_write_verify_10 *cdb;
8683 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8686 * XXX KDM we should do actual write verify support at some
8687 * point. This is obviously fake, we're just translating
8688 * things to a write. So we don't even bother checking the
8689 * BYTCHK field, since we don't do any verification. If
8690 * the user asks for it, we'll just pretend we did it.
8692 if (cdb->byte2 & SWV_DPO)
8695 lba = scsi_4btoul(cdb->addr);
8696 num_blocks = scsi_2btoul(cdb->length);
8701 struct scsi_rw_12 *cdb;
8703 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8705 if (cdb->byte2 & SRW12_RELADDR)
8707 if (cdb->byte2 & SRW12_FUA)
8709 if (cdb->byte2 & SRW12_DPO)
8711 lba = scsi_4btoul(cdb->addr);
8712 num_blocks = scsi_4btoul(cdb->length);
8715 case WRITE_VERIFY_12: {
8716 struct scsi_write_verify_12 *cdb;
8718 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8720 if (cdb->byte2 & SWV_DPO)
8723 lba = scsi_4btoul(cdb->addr);
8724 num_blocks = scsi_4btoul(cdb->length);
8730 struct scsi_rw_16 *cdb;
8732 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8734 if (cdb->byte2 & SRW12_RELADDR)
8736 if (cdb->byte2 & SRW12_FUA)
8738 if (cdb->byte2 & SRW12_DPO)
8741 lba = scsi_8btou64(cdb->addr);
8742 num_blocks = scsi_4btoul(cdb->length);
8745 case WRITE_VERIFY_16: {
8746 struct scsi_write_verify_16 *cdb;
8748 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8750 if (cdb->byte2 & SWV_DPO)
8753 lba = scsi_8btou64(cdb->addr);
8754 num_blocks = scsi_4btoul(cdb->length);
8759 * We got a command we don't support. This shouldn't
8760 * happen, commands should be filtered out above us.
8762 ctl_set_invalid_opcode(ctsio);
8763 ctl_done((union ctl_io *)ctsio);
8765 return (CTL_RETVAL_COMPLETE);
8766 break; /* NOTREACHED */
8770 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8771 * interesting for us, but if RAIDCore is in write-back mode,
8772 * getting it to do write-through for a particular transaction may
8776 * We don't support relative addressing. That also requires
8777 * supporting linked commands, which we don't do.
8780 ctl_set_invalid_field(ctsio,
8786 ctl_done((union ctl_io *)ctsio);
8787 return (CTL_RETVAL_COMPLETE);
8791 * The first check is to make sure we're in bounds, the second
8792 * check is to catch wrap-around problems. If the lba + num blocks
8793 * is less than the lba, then we've wrapped around and the block
8794 * range is invalid anyway.
8796 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8797 || ((lba + num_blocks) < lba)) {
8798 ctl_set_lba_out_of_range(ctsio);
8799 ctl_done((union ctl_io *)ctsio);
8800 return (CTL_RETVAL_COMPLETE);
8804 * According to SBC-3, a transfer length of 0 is not an error.
8805 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8806 * translates to 256 blocks for those commands.
8808 if (num_blocks == 0) {
8809 ctl_set_success(ctsio);
8810 ctl_done((union ctl_io *)ctsio);
8811 return (CTL_RETVAL_COMPLETE);
8815 lbalen.len = num_blocks;
8816 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
8819 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8821 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8827 ctl_report_luns(struct ctl_scsiio *ctsio)
8829 struct scsi_report_luns *cdb;
8830 struct scsi_report_luns_data *lun_data;
8831 struct ctl_lun *lun, *request_lun;
8832 int num_luns, retval;
8833 uint32_t alloc_len, lun_datalen;
8834 int num_filled, well_known;
8835 uint32_t initidx, targ_lun_id, lun_id;
8837 retval = CTL_RETVAL_COMPLETE;
8840 cdb = (struct scsi_report_luns *)ctsio->cdb;
8842 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8844 mtx_lock(&control_softc->ctl_lock);
8845 num_luns = control_softc->num_luns;
8846 mtx_unlock(&control_softc->ctl_lock);
8848 switch (cdb->select_report) {
8849 case RPL_REPORT_DEFAULT:
8850 case RPL_REPORT_ALL:
8852 case RPL_REPORT_WELLKNOWN:
8857 ctl_set_invalid_field(ctsio,
8863 ctl_done((union ctl_io *)ctsio);
8865 break; /* NOTREACHED */
8868 alloc_len = scsi_4btoul(cdb->length);
8870 * The initiator has to allocate at least 16 bytes for this request,
8871 * so he can at least get the header and the first LUN. Otherwise
8872 * we reject the request (per SPC-3 rev 14, section 6.21).
8874 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
8875 sizeof(struct scsi_report_luns_lundata))) {
8876 ctl_set_invalid_field(ctsio,
8882 ctl_done((union ctl_io *)ctsio);
8886 request_lun = (struct ctl_lun *)
8887 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8889 lun_datalen = sizeof(*lun_data) +
8890 (num_luns * sizeof(struct scsi_report_luns_lundata));
8892 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
8893 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
8894 ctsio->kern_sg_entries = 0;
8896 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8898 mtx_lock(&control_softc->ctl_lock);
8899 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
8900 lun_id = targ_lun_id;
8901 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
8902 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
8903 if (lun_id >= CTL_MAX_LUNS)
8905 lun = control_softc->ctl_luns[lun_id];
8909 if (targ_lun_id <= 0xff) {
8911 * Peripheral addressing method, bus number 0.
8913 lun_data->luns[num_filled].lundata[0] =
8914 RPL_LUNDATA_ATYP_PERIPH;
8915 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
8917 } else if (targ_lun_id <= 0x3fff) {
8919 * Flat addressing method.
8921 lun_data->luns[num_filled].lundata[0] =
8922 RPL_LUNDATA_ATYP_FLAT |
8923 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
8924 #ifdef OLDCTLHEADERS
8925 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
8926 (targ_lun_id & SRLD_BUS_LUN_MASK);
8928 lun_data->luns[num_filled].lundata[1] =
8929 #ifdef OLDCTLHEADERS
8930 targ_lun_id >> SRLD_BUS_LUN_BITS;
8932 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
8935 printf("ctl_report_luns: bogus LUN number %jd, "
8936 "skipping\n", (intmax_t)targ_lun_id);
8939 * According to SPC-3, rev 14 section 6.21:
8941 * "The execution of a REPORT LUNS command to any valid and
8942 * installed logical unit shall clear the REPORTED LUNS DATA
8943 * HAS CHANGED unit attention condition for all logical
8944 * units of that target with respect to the requesting
8945 * initiator. A valid and installed logical unit is one
8946 * having a PERIPHERAL QUALIFIER of 000b in the standard
8947 * INQUIRY data (see 6.4.2)."
8949 * If request_lun is NULL, the LUN this report luns command
8950 * was issued to is either disabled or doesn't exist. In that
8951 * case, we shouldn't clear any pending lun change unit
8954 if (request_lun != NULL)
8955 lun->pending_sense[initidx].ua_pending &=
8958 mtx_unlock(&control_softc->ctl_lock);
8961 * It's quite possible that we've returned fewer LUNs than we allocated
8962 * space for. Trim it.
8964 lun_datalen = sizeof(*lun_data) +
8965 (num_filled * sizeof(struct scsi_report_luns_lundata));
8967 if (lun_datalen < alloc_len) {
8968 ctsio->residual = alloc_len - lun_datalen;
8969 ctsio->kern_data_len = lun_datalen;
8970 ctsio->kern_total_len = lun_datalen;
8972 ctsio->residual = 0;
8973 ctsio->kern_data_len = alloc_len;
8974 ctsio->kern_total_len = alloc_len;
8976 ctsio->kern_data_resid = 0;
8977 ctsio->kern_rel_offset = 0;
8978 ctsio->kern_sg_entries = 0;
8981 * We set this to the actual data length, regardless of how much
8982 * space we actually have to return results. If the user looks at
8983 * this value, he'll know whether or not he allocated enough space
8984 * and reissue the command if necessary. We don't support well
8985 * known logical units, so if the user asks for that, return none.
8987 scsi_ulto4b(lun_datalen - 8, lun_data->length);
8990 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
8993 ctsio->scsi_status = SCSI_STATUS_OK;
8995 ctsio->be_move_done = ctl_config_move_done;
8996 ctl_datamove((union ctl_io *)ctsio);
9002 ctl_request_sense(struct ctl_scsiio *ctsio)
9004 struct scsi_request_sense *cdb;
9005 struct scsi_sense_data *sense_ptr;
9006 struct ctl_lun *lun;
9009 scsi_sense_data_type sense_format;
9011 cdb = (struct scsi_request_sense *)ctsio->cdb;
9013 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9015 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9018 * Determine which sense format the user wants.
9020 if (cdb->byte2 & SRS_DESC)
9021 sense_format = SSD_TYPE_DESC;
9023 sense_format = SSD_TYPE_FIXED;
9025 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9026 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9027 ctsio->kern_sg_entries = 0;
9030 * struct scsi_sense_data, which is currently set to 256 bytes, is
9031 * larger than the largest allowed value for the length field in the
9032 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9034 ctsio->residual = 0;
9035 ctsio->kern_data_len = cdb->length;
9036 ctsio->kern_total_len = cdb->length;
9038 ctsio->kern_data_resid = 0;
9039 ctsio->kern_rel_offset = 0;
9040 ctsio->kern_sg_entries = 0;
9043 * If we don't have a LUN, we don't have any pending sense.
9049 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9051 * Check for pending sense, and then for pending unit attentions.
9052 * Pending sense gets returned first, then pending unit attentions.
9054 mtx_lock(&lun->ctl_softc->ctl_lock);
9055 if (ctl_is_set(lun->have_ca, initidx)) {
9056 scsi_sense_data_type stored_format;
9059 * Check to see which sense format was used for the stored
9062 stored_format = scsi_sense_type(
9063 &lun->pending_sense[initidx].sense);
9066 * If the user requested a different sense format than the
9067 * one we stored, then we need to convert it to the other
9068 * format. If we're going from descriptor to fixed format
9069 * sense data, we may lose things in translation, depending
9070 * on what options were used.
9072 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9073 * for some reason we'll just copy it out as-is.
9075 if ((stored_format == SSD_TYPE_FIXED)
9076 && (sense_format == SSD_TYPE_DESC))
9077 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9078 &lun->pending_sense[initidx].sense,
9079 (struct scsi_sense_data_desc *)sense_ptr);
9080 else if ((stored_format == SSD_TYPE_DESC)
9081 && (sense_format == SSD_TYPE_FIXED))
9082 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9083 &lun->pending_sense[initidx].sense,
9084 (struct scsi_sense_data_fixed *)sense_ptr);
9086 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9087 ctl_min(sizeof(*sense_ptr),
9088 sizeof(lun->pending_sense[initidx].sense)));
9090 ctl_clear_mask(lun->have_ca, initidx);
9092 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9093 ctl_ua_type ua_type;
9095 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9096 sense_ptr, sense_format);
9097 if (ua_type != CTL_UA_NONE) {
9099 /* We're reporting this UA, so clear it */
9100 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9103 mtx_unlock(&lun->ctl_softc->ctl_lock);
9106 * We already have a pending error, return it.
9108 if (have_error != 0) {
9110 * We report the SCSI status as OK, since the status of the
9111 * request sense command itself is OK.
9113 ctsio->scsi_status = SCSI_STATUS_OK;
9116 * We report 0 for the sense length, because we aren't doing
9117 * autosense in this case. We're reporting sense as
9120 ctsio->sense_len = 0;
9122 ctsio->be_move_done = ctl_config_move_done;
9123 ctl_datamove((union ctl_io *)ctsio);
9125 return (CTL_RETVAL_COMPLETE);
9131 * No sense information to report, so we report that everything is
9134 ctl_set_sense_data(sense_ptr,
9137 /*current_error*/ 1,
9138 /*sense_key*/ SSD_KEY_NO_SENSE,
9143 ctsio->scsi_status = SCSI_STATUS_OK;
9146 * We report 0 for the sense length, because we aren't doing
9147 * autosense in this case. We're reporting sense as parameter data.
9149 ctsio->sense_len = 0;
9150 ctsio->be_move_done = ctl_config_move_done;
9151 ctl_datamove((union ctl_io *)ctsio);
9153 return (CTL_RETVAL_COMPLETE);
9157 ctl_tur(struct ctl_scsiio *ctsio)
9159 struct ctl_lun *lun;
9161 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9163 CTL_DEBUG_PRINT(("ctl_tur\n"));
9168 ctsio->scsi_status = SCSI_STATUS_OK;
9169 ctsio->io_hdr.status = CTL_SUCCESS;
9171 ctl_done((union ctl_io *)ctsio);
9173 return (CTL_RETVAL_COMPLETE);
9178 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9185 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9187 struct scsi_vpd_supported_pages *pages;
9189 struct ctl_lun *lun;
9191 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9193 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9194 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9195 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9196 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9197 ctsio->kern_sg_entries = 0;
9199 if (sup_page_size < alloc_len) {
9200 ctsio->residual = alloc_len - sup_page_size;
9201 ctsio->kern_data_len = sup_page_size;
9202 ctsio->kern_total_len = sup_page_size;
9204 ctsio->residual = 0;
9205 ctsio->kern_data_len = alloc_len;
9206 ctsio->kern_total_len = alloc_len;
9208 ctsio->kern_data_resid = 0;
9209 ctsio->kern_rel_offset = 0;
9210 ctsio->kern_sg_entries = 0;
9213 * The control device is always connected. The disk device, on the
9214 * other hand, may not be online all the time. Need to change this
9215 * to figure out whether the disk device is actually online or not.
9218 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9219 lun->be_lun->lun_type;
9221 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9223 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9224 /* Supported VPD pages */
9225 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9227 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9228 /* Device Identification */
9229 pages->page_list[2] = SVPD_DEVICE_ID;
9231 pages->page_list[3] = SVPD_BLOCK_LIMITS;
9232 /* Logical Block Provisioning */
9233 pages->page_list[4] = SVPD_LBP;
9235 ctsio->scsi_status = SCSI_STATUS_OK;
9237 ctsio->be_move_done = ctl_config_move_done;
9238 ctl_datamove((union ctl_io *)ctsio);
9240 return (CTL_RETVAL_COMPLETE);
9244 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9246 struct scsi_vpd_unit_serial_number *sn_ptr;
9247 struct ctl_lun *lun;
9248 #ifndef CTL_USE_BACKEND_SN
9252 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9254 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9255 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9256 ctsio->kern_sg_entries = 0;
9258 if (sizeof(*sn_ptr) < alloc_len) {
9259 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9260 ctsio->kern_data_len = sizeof(*sn_ptr);
9261 ctsio->kern_total_len = sizeof(*sn_ptr);
9263 ctsio->residual = 0;
9264 ctsio->kern_data_len = alloc_len;
9265 ctsio->kern_total_len = alloc_len;
9267 ctsio->kern_data_resid = 0;
9268 ctsio->kern_rel_offset = 0;
9269 ctsio->kern_sg_entries = 0;
9272 * The control device is always connected. The disk device, on the
9273 * other hand, may not be online all the time. Need to change this
9274 * to figure out whether the disk device is actually online or not.
9277 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9278 lun->be_lun->lun_type;
9280 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9282 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9283 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9284 #ifdef CTL_USE_BACKEND_SN
9286 * If we don't have a LUN, we just leave the serial number as
9289 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9291 strncpy((char *)sn_ptr->serial_num,
9292 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9296 * Note that we're using a non-unique serial number here,
9298 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9299 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9300 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9301 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9303 ctsio->scsi_status = SCSI_STATUS_OK;
9305 ctsio->be_move_done = ctl_config_move_done;
9306 ctl_datamove((union ctl_io *)ctsio);
9308 return (CTL_RETVAL_COMPLETE);
9313 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9315 struct scsi_vpd_device_id *devid_ptr;
9316 struct scsi_vpd_id_descriptor *desc, *desc1;
9317 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9318 struct scsi_vpd_id_t10 *t10id;
9319 struct ctl_softc *ctl_softc;
9320 struct ctl_lun *lun;
9321 struct ctl_frontend *fe;
9322 #ifndef CTL_USE_BACKEND_SN
9324 #endif /* CTL_USE_BACKEND_SN */
9327 ctl_softc = control_softc;
9329 mtx_lock(&ctl_softc->ctl_lock);
9330 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9331 mtx_unlock(&ctl_softc->ctl_lock);
9333 if (fe->devid != NULL)
9334 return ((fe->devid)(ctsio, alloc_len));
9336 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9338 devid_len = sizeof(struct scsi_vpd_device_id) +
9339 sizeof(struct scsi_vpd_id_descriptor) +
9340 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9341 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9342 sizeof(struct scsi_vpd_id_descriptor) +
9343 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9344 sizeof(struct scsi_vpd_id_descriptor) +
9345 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9347 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
9348 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9349 ctsio->kern_sg_entries = 0;
9351 if (devid_len < alloc_len) {
9352 ctsio->residual = alloc_len - devid_len;
9353 ctsio->kern_data_len = devid_len;
9354 ctsio->kern_total_len = devid_len;
9356 ctsio->residual = 0;
9357 ctsio->kern_data_len = alloc_len;
9358 ctsio->kern_total_len = alloc_len;
9360 ctsio->kern_data_resid = 0;
9361 ctsio->kern_rel_offset = 0;
9362 ctsio->kern_sg_entries = 0;
9364 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9365 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9366 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9367 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9368 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9370 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9371 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9374 * The control device is always connected. The disk device, on the
9375 * other hand, may not be online all the time.
9378 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9379 lun->be_lun->lun_type;
9381 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9383 devid_ptr->page_code = SVPD_DEVICE_ID;
9385 scsi_ulto2b(devid_len - 4, devid_ptr->length);
9387 mtx_lock(&ctl_softc->ctl_lock);
9390 * For Fibre channel,
9392 if (fe->port_type == CTL_PORT_FC)
9394 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9395 SVPD_ID_CODESET_ASCII;
9396 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9397 SVPD_ID_CODESET_BINARY;
9401 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9402 SVPD_ID_CODESET_ASCII;
9403 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9404 SVPD_ID_CODESET_BINARY;
9406 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9407 mtx_unlock(&ctl_softc->ctl_lock);
9410 * We're using a LUN association here. i.e., this device ID is a
9411 * per-LUN identifier.
9413 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9414 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9415 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9418 * desc1 is for the WWPN which is a port asscociation.
9420 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9421 desc1->length = CTL_WWPN_LEN;
9422 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9423 /* For testing just create the WWPN */
9425 ddb_GetWWNN((char *)desc1->identifier);
9427 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9428 /* This is so Copancontrol will return something sane */
9429 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9430 ctsio->io_hdr.nexus.targ_port!=8)
9431 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9433 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9436 be64enc(desc1->identifier, fe->wwpn);
9439 * desc2 is for the Relative Target Port(type 4h) identifier
9441 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9442 | SVPD_ID_TYPE_RELTARG;
9445 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9446 /* This is so Copancontrol will return something sane */
9447 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9448 ctsio->io_hdr.nexus.targ_port!=8)
9449 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9451 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9455 * desc3 is for the Target Port Group(type 5h) identifier
9457 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9458 | SVPD_ID_TYPE_TPORTGRP;
9460 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9461 desc3->identifier[3] = 1;
9463 desc3->identifier[3] = 2;
9465 #ifdef CTL_USE_BACKEND_SN
9467 * If we've actually got a backend, copy the device id from the
9468 * per-LUN data. Otherwise, set it to all spaces.
9472 * Copy the backend's LUN ID.
9474 strncpy((char *)t10id->vendor_spec_id,
9475 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9478 * No backend, set this to spaces.
9480 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9483 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9484 (lun != NULL) ? (int)lun->lun : 0);
9485 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9489 ctsio->scsi_status = SCSI_STATUS_OK;
9491 ctsio->be_move_done = ctl_config_move_done;
9492 ctl_datamove((union ctl_io *)ctsio);
9494 return (CTL_RETVAL_COMPLETE);
9498 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9500 struct scsi_vpd_block_limits *bl_ptr;
9501 struct ctl_lun *lun;
9504 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9505 bs = lun->be_lun->blocksize;
9507 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9508 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9509 ctsio->kern_sg_entries = 0;
9511 if (sizeof(*bl_ptr) < alloc_len) {
9512 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9513 ctsio->kern_data_len = sizeof(*bl_ptr);
9514 ctsio->kern_total_len = sizeof(*bl_ptr);
9516 ctsio->residual = 0;
9517 ctsio->kern_data_len = alloc_len;
9518 ctsio->kern_total_len = alloc_len;
9520 ctsio->kern_data_resid = 0;
9521 ctsio->kern_rel_offset = 0;
9522 ctsio->kern_sg_entries = 0;
9525 * The control device is always connected. The disk device, on the
9526 * other hand, may not be online all the time. Need to change this
9527 * to figure out whether the disk device is actually online or not.
9530 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9531 lun->be_lun->lun_type;
9533 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9535 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9536 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9537 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9538 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9539 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9540 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9541 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9543 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9545 ctsio->scsi_status = SCSI_STATUS_OK;
9546 ctsio->be_move_done = ctl_config_move_done;
9547 ctl_datamove((union ctl_io *)ctsio);
9549 return (CTL_RETVAL_COMPLETE);
9553 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9555 struct scsi_vpd_logical_block_prov *lbp_ptr;
9556 struct ctl_lun *lun;
9559 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9560 bs = lun->be_lun->blocksize;
9562 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9563 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9564 ctsio->kern_sg_entries = 0;
9566 if (sizeof(*lbp_ptr) < alloc_len) {
9567 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9568 ctsio->kern_data_len = sizeof(*lbp_ptr);
9569 ctsio->kern_total_len = sizeof(*lbp_ptr);
9571 ctsio->residual = 0;
9572 ctsio->kern_data_len = alloc_len;
9573 ctsio->kern_total_len = alloc_len;
9575 ctsio->kern_data_resid = 0;
9576 ctsio->kern_rel_offset = 0;
9577 ctsio->kern_sg_entries = 0;
9580 * The control device is always connected. The disk device, on the
9581 * other hand, may not be online all the time. Need to change this
9582 * to figure out whether the disk device is actually online or not.
9585 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9586 lun->be_lun->lun_type;
9588 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9590 lbp_ptr->page_code = SVPD_LBP;
9591 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9592 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9594 ctsio->scsi_status = SCSI_STATUS_OK;
9595 ctsio->be_move_done = ctl_config_move_done;
9596 ctl_datamove((union ctl_io *)ctsio);
9598 return (CTL_RETVAL_COMPLETE);
9602 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9604 struct scsi_inquiry *cdb;
9605 struct ctl_lun *lun;
9606 int alloc_len, retval;
9608 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9609 cdb = (struct scsi_inquiry *)ctsio->cdb;
9611 retval = CTL_RETVAL_COMPLETE;
9613 alloc_len = scsi_2btoul(cdb->length);
9615 switch (cdb->page_code) {
9616 case SVPD_SUPPORTED_PAGES:
9617 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9619 case SVPD_UNIT_SERIAL_NUMBER:
9620 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9622 case SVPD_DEVICE_ID:
9623 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9625 case SVPD_BLOCK_LIMITS:
9626 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9629 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9632 ctl_set_invalid_field(ctsio,
9638 ctl_done((union ctl_io *)ctsio);
9639 retval = CTL_RETVAL_COMPLETE;
9647 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9649 struct scsi_inquiry_data *inq_ptr;
9650 struct scsi_inquiry *cdb;
9651 struct ctl_softc *ctl_softc;
9652 struct ctl_lun *lun;
9656 ctl_softc = control_softc;
9659 * Figure out whether we're talking to a Fibre Channel port or not.
9660 * We treat the ioctl front end, and any SCSI adapters, as packetized
9663 mtx_lock(&ctl_softc->ctl_lock);
9664 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9669 mtx_unlock(&ctl_softc->ctl_lock);
9671 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9672 cdb = (struct scsi_inquiry *)ctsio->cdb;
9673 alloc_len = scsi_2btoul(cdb->length);
9676 * We malloc the full inquiry data size here and fill it
9677 * in. If the user only asks for less, we'll give him
9680 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9681 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9682 ctsio->kern_sg_entries = 0;
9683 ctsio->kern_data_resid = 0;
9684 ctsio->kern_rel_offset = 0;
9686 if (sizeof(*inq_ptr) < alloc_len) {
9687 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9688 ctsio->kern_data_len = sizeof(*inq_ptr);
9689 ctsio->kern_total_len = sizeof(*inq_ptr);
9691 ctsio->residual = 0;
9692 ctsio->kern_data_len = alloc_len;
9693 ctsio->kern_total_len = alloc_len;
9697 * If we have a LUN configured, report it as connected. Otherwise,
9698 * report that it is offline or no device is supported, depending
9699 * on the value of inquiry_pq_no_lun.
9701 * According to the spec (SPC-4 r34), the peripheral qualifier
9702 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9704 * "A peripheral device having the specified peripheral device type
9705 * is not connected to this logical unit. However, the device
9706 * server is capable of supporting the specified peripheral device
9707 * type on this logical unit."
9709 * According to the same spec, the peripheral qualifier
9710 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9712 * "The device server is not capable of supporting a peripheral
9713 * device on this logical unit. For this peripheral qualifier the
9714 * peripheral device type shall be set to 1Fh. All other peripheral
9715 * device type values are reserved for this peripheral qualifier."
9717 * Given the text, it would seem that we probably want to report that
9718 * the LUN is offline here. There is no LUN connected, but we can
9719 * support a LUN at the given LUN number.
9721 * In the real world, though, it sounds like things are a little
9724 * - Linux, when presented with a LUN with the offline peripheral
9725 * qualifier, will create an sg driver instance for it. So when
9726 * you attach it to CTL, you wind up with a ton of sg driver
9727 * instances. (One for every LUN that Linux bothered to probe.)
9728 * Linux does this despite the fact that it issues a REPORT LUNs
9729 * to LUN 0 to get the inventory of supported LUNs.
9731 * - There is other anecdotal evidence (from Emulex folks) about
9732 * arrays that use the offline peripheral qualifier for LUNs that
9733 * are on the "passive" path in an active/passive array.
9735 * So the solution is provide a hopefully reasonable default
9736 * (return bad/no LUN) and allow the user to change the behavior
9737 * with a tunable/sysctl variable.
9740 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9741 lun->be_lun->lun_type;
9742 else if (ctl_softc->inquiry_pq_no_lun == 0)
9743 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9745 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9747 /* RMB in byte 2 is 0 */
9748 inq_ptr->version = SCSI_REV_SPC3;
9751 * According to SAM-3, even if a device only supports a single
9752 * level of LUN addressing, it should still set the HISUP bit:
9754 * 4.9.1 Logical unit numbers overview
9756 * All logical unit number formats described in this standard are
9757 * hierarchical in structure even when only a single level in that
9758 * hierarchy is used. The HISUP bit shall be set to one in the
9759 * standard INQUIRY data (see SPC-2) when any logical unit number
9760 * format described in this standard is used. Non-hierarchical
9761 * formats are outside the scope of this standard.
9763 * Therefore we set the HiSup bit here.
9765 * The reponse format is 2, per SPC-3.
9767 inq_ptr->response_format = SID_HiSup | 2;
9769 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9770 CTL_DEBUG_PRINT(("additional_length = %d\n",
9771 inq_ptr->additional_length));
9773 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9774 /* 16 bit addressing */
9776 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9777 /* XXX set the SID_MultiP bit here if we're actually going to
9778 respond on multiple ports */
9779 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9781 /* 16 bit data bus, synchronous transfers */
9782 /* XXX these flags don't apply for FC */
9784 inq_ptr->flags = SID_WBus16 | SID_Sync;
9786 * XXX KDM do we want to support tagged queueing on the control
9790 || (lun->be_lun->lun_type != T_PROCESSOR))
9791 inq_ptr->flags |= SID_CmdQue;
9793 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9794 * We have 8 bytes for the vendor name, and 16 bytes for the device
9795 * name and 4 bytes for the revision.
9797 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
9799 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9801 switch (lun->be_lun->lun_type) {
9803 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9806 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
9809 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
9815 * XXX make this a macro somewhere so it automatically gets
9816 * incremented when we make changes.
9818 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
9821 * For parallel SCSI, we support double transition and single
9822 * transition clocking. We also support QAS (Quick Arbitration
9823 * and Selection) and Information Unit transfers on both the
9824 * control and array devices.
9827 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
9831 scsi_ulto2b(0x0060, inq_ptr->version1);
9832 /* SPC-3 (no version claimed) XXX should we claim a version? */
9833 scsi_ulto2b(0x0300, inq_ptr->version2);
9835 /* FCP-2 ANSI INCITS.350:2003 */
9836 scsi_ulto2b(0x0917, inq_ptr->version3);
9838 /* SPI-4 ANSI INCITS.362:200x */
9839 scsi_ulto2b(0x0B56, inq_ptr->version3);
9843 /* SBC-2 (no version claimed) XXX should we claim a version? */
9844 scsi_ulto2b(0x0320, inq_ptr->version4);
9846 switch (lun->be_lun->lun_type) {
9849 * SBC-2 (no version claimed) XXX should we claim a
9852 scsi_ulto2b(0x0320, inq_ptr->version4);
9860 ctsio->scsi_status = SCSI_STATUS_OK;
9861 if (ctsio->kern_data_len > 0) {
9862 ctsio->be_move_done = ctl_config_move_done;
9863 ctl_datamove((union ctl_io *)ctsio);
9865 ctsio->io_hdr.status = CTL_SUCCESS;
9866 ctl_done((union ctl_io *)ctsio);
9869 return (CTL_RETVAL_COMPLETE);
9873 ctl_inquiry(struct ctl_scsiio *ctsio)
9875 struct scsi_inquiry *cdb;
9878 cdb = (struct scsi_inquiry *)ctsio->cdb;
9882 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
9885 * Right now, we don't support the CmdDt inquiry information.
9886 * This would be nice to support in the future. When we do
9887 * support it, we should change this test so that it checks to make
9888 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
9891 if (((cdb->byte2 & SI_EVPD)
9892 && (cdb->byte2 & SI_CMDDT)))
9894 if (cdb->byte2 & SI_CMDDT) {
9896 * Point to the SI_CMDDT bit. We might change this
9897 * when we support SI_CMDDT, but since both bits would be
9898 * "wrong", this should probably just stay as-is then.
9900 ctl_set_invalid_field(ctsio,
9906 ctl_done((union ctl_io *)ctsio);
9907 return (CTL_RETVAL_COMPLETE);
9909 if (cdb->byte2 & SI_EVPD)
9910 retval = ctl_inquiry_evpd(ctsio);
9912 else if (cdb->byte2 & SI_CMDDT)
9913 retval = ctl_inquiry_cmddt(ctsio);
9916 retval = ctl_inquiry_std(ctsio);
9922 * For known CDB types, parse the LBA and length.
9925 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
9927 if (io->io_hdr.io_type != CTL_IO_SCSI)
9930 switch (io->scsiio.cdb[0]) {
9933 struct scsi_rw_6 *cdb;
9935 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
9937 *lba = scsi_3btoul(cdb->addr);
9938 /* only 5 bits are valid in the most significant address byte */
9945 struct scsi_rw_10 *cdb;
9947 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
9949 *lba = scsi_4btoul(cdb->addr);
9950 *len = scsi_2btoul(cdb->length);
9953 case WRITE_VERIFY_10: {
9954 struct scsi_write_verify_10 *cdb;
9956 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
9958 *lba = scsi_4btoul(cdb->addr);
9959 *len = scsi_2btoul(cdb->length);
9964 struct scsi_rw_12 *cdb;
9966 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
9968 *lba = scsi_4btoul(cdb->addr);
9969 *len = scsi_4btoul(cdb->length);
9972 case WRITE_VERIFY_12: {
9973 struct scsi_write_verify_12 *cdb;
9975 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
9977 *lba = scsi_4btoul(cdb->addr);
9978 *len = scsi_4btoul(cdb->length);
9983 struct scsi_rw_16 *cdb;
9985 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
9987 *lba = scsi_8btou64(cdb->addr);
9988 *len = scsi_4btoul(cdb->length);
9991 case WRITE_VERIFY_16: {
9992 struct scsi_write_verify_16 *cdb;
9994 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
9997 *lba = scsi_8btou64(cdb->addr);
9998 *len = scsi_4btoul(cdb->length);
10001 case WRITE_SAME_10: {
10002 struct scsi_write_same_10 *cdb;
10004 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10006 *lba = scsi_4btoul(cdb->addr);
10007 *len = scsi_2btoul(cdb->length);
10010 case WRITE_SAME_16: {
10011 struct scsi_write_same_16 *cdb;
10013 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10015 *lba = scsi_8btou64(cdb->addr);
10016 *len = scsi_4btoul(cdb->length);
10021 break; /* NOTREACHED */
10028 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10030 uint64_t endlba1, endlba2;
10032 endlba1 = lba1 + len1 - 1;
10033 endlba2 = lba2 + len2 - 1;
10035 if ((endlba1 < lba2)
10036 || (endlba2 < lba1))
10037 return (CTL_ACTION_PASS);
10039 return (CTL_ACTION_BLOCK);
10043 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10045 uint64_t lba1, lba2;
10046 uint32_t len1, len2;
10049 retval = ctl_get_lba_len(io1, &lba1, &len1);
10051 return (CTL_ACTION_ERROR);
10053 retval = ctl_get_lba_len(io2, &lba2, &len2);
10055 return (CTL_ACTION_ERROR);
10057 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10061 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10063 struct ctl_cmd_entry *pending_entry, *ooa_entry;
10064 ctl_serialize_action *serialize_row;
10067 * The initiator attempted multiple untagged commands at the same
10068 * time. Can't do that.
10070 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10071 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10072 && ((pending_io->io_hdr.nexus.targ_port ==
10073 ooa_io->io_hdr.nexus.targ_port)
10074 && (pending_io->io_hdr.nexus.initid.id ==
10075 ooa_io->io_hdr.nexus.initid.id))
10076 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10077 return (CTL_ACTION_OVERLAP);
10080 * The initiator attempted to send multiple tagged commands with
10081 * the same ID. (It's fine if different initiators have the same
10084 * Even if all of those conditions are true, we don't kill the I/O
10085 * if the command ahead of us has been aborted. We won't end up
10086 * sending it to the FETD, and it's perfectly legal to resend a
10087 * command with the same tag number as long as the previous
10088 * instance of this tag number has been aborted somehow.
10090 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10091 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10092 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10093 && ((pending_io->io_hdr.nexus.targ_port ==
10094 ooa_io->io_hdr.nexus.targ_port)
10095 && (pending_io->io_hdr.nexus.initid.id ==
10096 ooa_io->io_hdr.nexus.initid.id))
10097 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10098 return (CTL_ACTION_OVERLAP_TAG);
10101 * If we get a head of queue tag, SAM-3 says that we should
10102 * immediately execute it.
10104 * What happens if this command would normally block for some other
10105 * reason? e.g. a request sense with a head of queue tag
10106 * immediately after a write. Normally that would block, but this
10107 * will result in its getting executed immediately...
10109 * We currently return "pass" instead of "skip", so we'll end up
10110 * going through the rest of the queue to check for overlapped tags.
10112 * XXX KDM check for other types of blockage first??
10114 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10115 return (CTL_ACTION_PASS);
10118 * Ordered tags have to block until all items ahead of them
10119 * have completed. If we get called with an ordered tag, we always
10120 * block, if something else is ahead of us in the queue.
10122 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10123 return (CTL_ACTION_BLOCK);
10126 * Simple tags get blocked until all head of queue and ordered tags
10127 * ahead of them have completed. I'm lumping untagged commands in
10128 * with simple tags here. XXX KDM is that the right thing to do?
10130 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10131 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10132 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10133 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10134 return (CTL_ACTION_BLOCK);
10136 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
10137 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
10139 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10141 switch (serialize_row[pending_entry->seridx]) {
10142 case CTL_SER_BLOCK:
10143 return (CTL_ACTION_BLOCK);
10144 break; /* NOTREACHED */
10145 case CTL_SER_EXTENT:
10146 return (ctl_extent_check(pending_io, ooa_io));
10147 break; /* NOTREACHED */
10149 return (CTL_ACTION_PASS);
10150 break; /* NOTREACHED */
10152 return (CTL_ACTION_SKIP);
10155 panic("invalid serialization value %d",
10156 serialize_row[pending_entry->seridx]);
10157 break; /* NOTREACHED */
10160 return (CTL_ACTION_ERROR);
10164 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10166 * - pending_io is generally either incoming, or on the blocked queue
10167 * - starting I/O is the I/O we want to start the check with.
10170 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10171 union ctl_io *starting_io)
10173 union ctl_io *ooa_io;
10176 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
10179 * Run back along the OOA queue, starting with the current
10180 * blocked I/O and going through every I/O before it on the
10181 * queue. If starting_io is NULL, we'll just end up returning
10184 for (ooa_io = starting_io; ooa_io != NULL;
10185 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10189 * This routine just checks to see whether
10190 * cur_blocked is blocked by ooa_io, which is ahead
10191 * of it in the queue. It doesn't queue/dequeue
10194 action = ctl_check_for_blockage(pending_io, ooa_io);
10196 case CTL_ACTION_BLOCK:
10197 case CTL_ACTION_OVERLAP:
10198 case CTL_ACTION_OVERLAP_TAG:
10199 case CTL_ACTION_SKIP:
10200 case CTL_ACTION_ERROR:
10202 break; /* NOTREACHED */
10203 case CTL_ACTION_PASS:
10206 panic("invalid action %d", action);
10207 break; /* NOTREACHED */
10211 return (CTL_ACTION_PASS);
10216 * - An I/O has just completed, and has been removed from the per-LUN OOA
10217 * queue, so some items on the blocked queue may now be unblocked.
10220 ctl_check_blocked(struct ctl_lun *lun)
10222 union ctl_io *cur_blocked, *next_blocked;
10224 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
10227 * Run forward from the head of the blocked queue, checking each
10228 * entry against the I/Os prior to it on the OOA queue to see if
10229 * there is still any blockage.
10231 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10232 * with our removing a variable on it while it is traversing the
10235 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10236 cur_blocked != NULL; cur_blocked = next_blocked) {
10237 union ctl_io *prev_ooa;
10240 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10243 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10244 ctl_ooaq, ooa_links);
10247 * If cur_blocked happens to be the first item in the OOA
10248 * queue now, prev_ooa will be NULL, and the action
10249 * returned will just be CTL_ACTION_PASS.
10251 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10254 case CTL_ACTION_BLOCK:
10255 /* Nothing to do here, still blocked */
10257 case CTL_ACTION_OVERLAP:
10258 case CTL_ACTION_OVERLAP_TAG:
10260 * This shouldn't happen! In theory we've already
10261 * checked this command for overlap...
10264 case CTL_ACTION_PASS:
10265 case CTL_ACTION_SKIP: {
10266 struct ctl_softc *softc;
10267 struct ctl_cmd_entry *entry;
10273 * The skip case shouldn't happen, this transaction
10274 * should have never made it onto the blocked queue.
10277 * This I/O is no longer blocked, we can remove it
10278 * from the blocked queue. Since this is a TAILQ
10279 * (doubly linked list), we can do O(1) removals
10280 * from any place on the list.
10282 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10284 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10286 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10288 * Need to send IO back to original side to
10291 union ctl_ha_msg msg_info;
10293 msg_info.hdr.original_sc =
10294 cur_blocked->io_hdr.original_sc;
10295 msg_info.hdr.serializing_sc = cur_blocked;
10296 msg_info.hdr.msg_type = CTL_MSG_R2R;
10297 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10298 &msg_info, sizeof(msg_info), 0)) >
10299 CTL_HA_STATUS_SUCCESS) {
10300 printf("CTL:Check Blocked error from "
10301 "ctl_ha_msg_send %d\n",
10306 opcode = cur_blocked->scsiio.cdb[0];
10307 entry = &ctl_cmd_table[opcode];
10308 softc = control_softc;
10310 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10313 * Check this I/O for LUN state changes that may
10314 * have happened while this command was blocked.
10315 * The LUN state may have been changed by a command
10316 * ahead of us in the queue, so we need to re-check
10317 * for any states that can be caused by SCSI
10320 if (ctl_scsiio_lun_check(softc, lun, entry,
10321 &cur_blocked->scsiio) == 0) {
10322 cur_blocked->io_hdr.flags |=
10323 CTL_FLAG_IS_WAS_ON_RTR;
10324 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
10325 &cur_blocked->io_hdr, links);
10327 * In the non CTL_DONE_THREAD case, we need
10328 * to wake up the work thread here. When
10329 * we're processing completed requests from
10330 * the work thread context, we'll pop back
10331 * around and end up pulling things off the
10332 * RtR queue. When we aren't processing
10333 * things from the work thread context,
10334 * though, we won't ever check the RtR queue.
10335 * So we need to wake up the thread to clear
10336 * things off the queue. Otherwise this
10337 * transaction will just sit on the RtR queue
10338 * until a new I/O comes in. (Which may or
10339 * may not happen...)
10341 #ifndef CTL_DONE_THREAD
10342 ctl_wakeup_thread();
10345 ctl_done_lock(cur_blocked, /*have_lock*/ 1);
10350 * This probably shouldn't happen -- we shouldn't
10351 * get CTL_ACTION_ERROR, or anything else.
10357 return (CTL_RETVAL_COMPLETE);
10361 * This routine (with one exception) checks LUN flags that can be set by
10362 * commands ahead of us in the OOA queue. These flags have to be checked
10363 * when a command initially comes in, and when we pull a command off the
10364 * blocked queue and are preparing to execute it. The reason we have to
10365 * check these flags for commands on the blocked queue is that the LUN
10366 * state may have been changed by a command ahead of us while we're on the
10369 * Ordering is somewhat important with these checks, so please pay
10370 * careful attention to the placement of any new checks.
10373 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10374 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10381 * If this shelf is a secondary shelf controller, we have to reject
10382 * any media access commands.
10385 /* No longer needed for HA */
10386 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10387 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10388 ctl_set_lun_standby(ctsio);
10395 * Check for a reservation conflict. If this command isn't allowed
10396 * even on reserved LUNs, and if this initiator isn't the one who
10397 * reserved us, reject the command with a reservation conflict.
10399 if ((lun->flags & CTL_LUN_RESERVED)
10400 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10401 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10402 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10403 || (ctsio->io_hdr.nexus.targ_target.id !=
10404 lun->rsv_nexus.targ_target.id)) {
10405 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10406 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10412 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10413 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10416 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10418 * if we aren't registered or it's a res holder type
10419 * reservation and this isn't the res holder then set a
10421 * NOTE: Commands which might be allowed on write exclusive
10422 * type reservations are checked in the particular command
10423 * for a conflict. Read and SSU are the only ones.
10425 if (!lun->per_res[residx].registered
10426 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10427 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10428 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10435 if ((lun->flags & CTL_LUN_OFFLINE)
10436 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10437 ctl_set_lun_not_ready(ctsio);
10443 * If the LUN is stopped, see if this particular command is allowed
10444 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10446 if ((lun->flags & CTL_LUN_STOPPED)
10447 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10448 /* "Logical unit not ready, initializing cmd. required" */
10449 ctl_set_lun_stopped(ctsio);
10454 if ((lun->flags & CTL_LUN_INOPERABLE)
10455 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10456 /* "Medium format corrupted" */
10457 ctl_set_medium_format_corrupted(ctsio);
10468 ctl_failover_io(union ctl_io *io, int have_lock)
10470 ctl_set_busy(&io->scsiio);
10471 ctl_done_lock(io, have_lock);
10477 struct ctl_lun *lun;
10478 struct ctl_softc *ctl_softc;
10479 union ctl_io *next_io, *pending_io;
10484 ctl_softc = control_softc;
10486 mtx_lock(&ctl_softc->ctl_lock);
10488 * Remove any cmds from the other SC from the rtr queue. These
10489 * will obviously only be for LUNs for which we're the primary.
10490 * We can't send status or get/send data for these commands.
10491 * Since they haven't been executed yet, we can just remove them.
10492 * We'll either abort them or delete them below, depending on
10493 * which HA mode we're in.
10495 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10496 io != NULL; io = next_io) {
10497 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10498 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10499 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10500 ctl_io_hdr, links);
10503 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10504 lun = ctl_softc->ctl_luns[lun_idx];
10509 * Processor LUNs are primary on both sides.
10510 * XXX will this always be true?
10512 if (lun->be_lun->lun_type == T_PROCESSOR)
10515 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10516 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10517 printf("FAILOVER: primary lun %d\n", lun_idx);
10519 * Remove all commands from the other SC. First from the
10520 * blocked queue then from the ooa queue. Once we have
10521 * removed them. Call ctl_check_blocked to see if there
10522 * is anything that can run.
10524 for (io = (union ctl_io *)TAILQ_FIRST(
10525 &lun->blocked_queue); io != NULL; io = next_io) {
10527 next_io = (union ctl_io *)TAILQ_NEXT(
10528 &io->io_hdr, blocked_links);
10530 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10531 TAILQ_REMOVE(&lun->blocked_queue,
10532 &io->io_hdr,blocked_links);
10533 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10534 TAILQ_REMOVE(&lun->ooa_queue,
10535 &io->io_hdr, ooa_links);
10541 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10542 io != NULL; io = next_io) {
10544 next_io = (union ctl_io *)TAILQ_NEXT(
10545 &io->io_hdr, ooa_links);
10547 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10549 TAILQ_REMOVE(&lun->ooa_queue,
10556 ctl_check_blocked(lun);
10557 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10558 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10560 printf("FAILOVER: primary lun %d\n", lun_idx);
10562 * Abort all commands from the other SC. We can't
10563 * send status back for them now. These should get
10564 * cleaned up when they are completed or come out
10565 * for a datamove operation.
10567 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10568 io != NULL; io = next_io) {
10569 next_io = (union ctl_io *)TAILQ_NEXT(
10570 &io->io_hdr, ooa_links);
10572 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10573 io->io_hdr.flags |= CTL_FLAG_ABORT;
10575 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10576 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10578 printf("FAILOVER: secondary lun %d\n", lun_idx);
10580 lun->flags |= CTL_LUN_PRIMARY_SC;
10583 * We send all I/O that was sent to this controller
10584 * and redirected to the other side back with
10585 * busy status, and have the initiator retry it.
10586 * Figuring out how much data has been transferred,
10587 * etc. and picking up where we left off would be
10590 * XXX KDM need to remove I/O from the blocked
10593 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10594 &lun->ooa_queue); pending_io != NULL;
10595 pending_io = next_io) {
10597 next_io = (union ctl_io *)TAILQ_NEXT(
10598 &pending_io->io_hdr, ooa_links);
10600 pending_io->io_hdr.flags &=
10601 ~CTL_FLAG_SENT_2OTHER_SC;
10603 if (pending_io->io_hdr.flags &
10604 CTL_FLAG_IO_ACTIVE) {
10605 pending_io->io_hdr.flags |=
10608 ctl_set_busy(&pending_io->scsiio);
10609 ctl_done_lock(pending_io,
10615 * Build Unit Attention
10617 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10618 lun->pending_sense[i].ua_pending |=
10619 CTL_UA_ASYM_ACC_CHANGE;
10621 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10622 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10623 printf("FAILOVER: secondary lun %d\n", lun_idx);
10625 * if the first io on the OOA is not on the RtR queue
10628 lun->flags |= CTL_LUN_PRIMARY_SC;
10630 pending_io = (union ctl_io *)TAILQ_FIRST(
10632 if (pending_io==NULL) {
10633 printf("Nothing on OOA queue\n");
10637 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10638 if ((pending_io->io_hdr.flags &
10639 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10640 pending_io->io_hdr.flags |=
10641 CTL_FLAG_IS_WAS_ON_RTR;
10642 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
10643 &pending_io->io_hdr, links);
10648 printf("Tag 0x%04x is running\n",
10649 pending_io->scsiio.tag_num);
10653 next_io = (union ctl_io *)TAILQ_NEXT(
10654 &pending_io->io_hdr, ooa_links);
10655 for (pending_io=next_io; pending_io != NULL;
10656 pending_io = next_io) {
10657 pending_io->io_hdr.flags &=
10658 ~CTL_FLAG_SENT_2OTHER_SC;
10659 next_io = (union ctl_io *)TAILQ_NEXT(
10660 &pending_io->io_hdr, ooa_links);
10661 if (pending_io->io_hdr.flags &
10662 CTL_FLAG_IS_WAS_ON_RTR) {
10664 printf("Tag 0x%04x is running\n",
10665 pending_io->scsiio.tag_num);
10670 switch (ctl_check_ooa(lun, pending_io,
10671 (union ctl_io *)TAILQ_PREV(
10672 &pending_io->io_hdr, ctl_ooaq,
10675 case CTL_ACTION_BLOCK:
10676 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10677 &pending_io->io_hdr,
10679 pending_io->io_hdr.flags |=
10682 case CTL_ACTION_PASS:
10683 case CTL_ACTION_SKIP:
10684 pending_io->io_hdr.flags |=
10685 CTL_FLAG_IS_WAS_ON_RTR;
10686 STAILQ_INSERT_TAIL(
10687 &ctl_softc->rtr_queue,
10688 &pending_io->io_hdr, links);
10690 case CTL_ACTION_OVERLAP:
10691 ctl_set_overlapped_cmd(
10692 (struct ctl_scsiio *)pending_io);
10693 ctl_done_lock(pending_io,
10696 case CTL_ACTION_OVERLAP_TAG:
10697 ctl_set_overlapped_tag(
10698 (struct ctl_scsiio *)pending_io,
10699 pending_io->scsiio.tag_num & 0xff);
10700 ctl_done_lock(pending_io,
10703 case CTL_ACTION_ERROR:
10705 ctl_set_internal_failure(
10706 (struct ctl_scsiio *)pending_io,
10709 ctl_done_lock(pending_io,
10716 * Build Unit Attention
10718 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10719 lun->pending_sense[i].ua_pending |=
10720 CTL_UA_ASYM_ACC_CHANGE;
10723 panic("Unhandled HA mode failover, LUN flags = %#x, "
10724 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10728 mtx_unlock(&ctl_softc->ctl_lock);
10732 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10734 struct ctl_lun *lun;
10735 struct ctl_cmd_entry *entry;
10737 uint32_t initidx, targ_lun;
10744 opcode = ctsio->cdb[0];
10746 mtx_lock(&ctl_softc->ctl_lock);
10748 targ_lun = ctsio->io_hdr.nexus.targ_lun;
10749 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
10750 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
10751 if ((targ_lun < CTL_MAX_LUNS)
10752 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
10753 lun = ctl_softc->ctl_luns[targ_lun];
10755 * If the LUN is invalid, pretend that it doesn't exist.
10756 * It will go away as soon as all pending I/O has been
10759 if (lun->flags & CTL_LUN_DISABLED) {
10762 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10763 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10765 if (lun->be_lun->lun_type == T_PROCESSOR) {
10766 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10770 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10771 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
10774 entry = &ctl_cmd_table[opcode];
10776 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
10777 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
10780 * Check to see whether we can send this command to LUNs that don't
10781 * exist. This should pretty much only be the case for inquiry
10782 * and request sense. Further checks, below, really require having
10783 * a LUN, so we can't really check the command anymore. Just put
10784 * it on the rtr queue.
10787 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10790 ctl_set_unsupported_lun(ctsio);
10791 mtx_unlock(&ctl_softc->ctl_lock);
10792 ctl_done((union ctl_io *)ctsio);
10793 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
10797 * Every I/O goes into the OOA queue for a particular LUN, and
10798 * stays there until completion.
10800 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
10803 * Make sure we support this particular command on this LUN.
10804 * e.g., we don't support writes to the control LUN.
10806 switch (lun->be_lun->lun_type) {
10808 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
10809 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10811 ctl_set_invalid_opcode(ctsio);
10812 mtx_unlock(&ctl_softc->ctl_lock);
10813 ctl_done((union ctl_io *)ctsio);
10818 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
10819 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10821 ctl_set_invalid_opcode(ctsio);
10822 mtx_unlock(&ctl_softc->ctl_lock);
10823 ctl_done((union ctl_io *)ctsio);
10828 printf("Unsupported CTL LUN type %d\n",
10829 lun->be_lun->lun_type);
10830 panic("Unsupported CTL LUN type %d\n",
10831 lun->be_lun->lun_type);
10832 break; /* NOTREACHED */
10836 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10839 * If we've got a request sense, it'll clear the contingent
10840 * allegiance condition. Otherwise, if we have a CA condition for
10841 * this initiator, clear it, because it sent down a command other
10842 * than request sense.
10844 if ((opcode != REQUEST_SENSE)
10845 && (ctl_is_set(lun->have_ca, initidx)))
10846 ctl_clear_mask(lun->have_ca, initidx);
10849 * If the command has this flag set, it handles its own unit
10850 * attention reporting, we shouldn't do anything. Otherwise we
10851 * check for any pending unit attentions, and send them back to the
10852 * initiator. We only do this when a command initially comes in,
10853 * not when we pull it off the blocked queue.
10855 * According to SAM-3, section 5.3.2, the order that things get
10856 * presented back to the host is basically unit attentions caused
10857 * by some sort of reset event, busy status, reservation conflicts
10858 * or task set full, and finally any other status.
10860 * One issue here is that some of the unit attentions we report
10861 * don't fall into the "reset" category (e.g. "reported luns data
10862 * has changed"). So reporting it here, before the reservation
10863 * check, may be technically wrong. I guess the only thing to do
10864 * would be to check for and report the reset events here, and then
10865 * check for the other unit attention types after we check for a
10866 * reservation conflict.
10868 * XXX KDM need to fix this
10870 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
10871 ctl_ua_type ua_type;
10873 ua_type = lun->pending_sense[initidx].ua_pending;
10874 if (ua_type != CTL_UA_NONE) {
10875 scsi_sense_data_type sense_format;
10878 sense_format = (lun->flags &
10879 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
10882 sense_format = SSD_TYPE_FIXED;
10884 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
10886 if (ua_type != CTL_UA_NONE) {
10887 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
10888 ctsio->io_hdr.status = CTL_SCSI_ERROR |
10890 ctsio->sense_len = SSD_FULL_SIZE;
10891 lun->pending_sense[initidx].ua_pending &=
10893 mtx_unlock(&ctl_softc->ctl_lock);
10894 ctl_done((union ctl_io *)ctsio);
10901 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
10902 mtx_unlock(&ctl_softc->ctl_lock);
10903 ctl_done((union ctl_io *)ctsio);
10908 * XXX CHD this is where we want to send IO to other side if
10909 * this LUN is secondary on this SC. We will need to make a copy
10910 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
10911 * the copy we send as FROM_OTHER.
10912 * We also need to stuff the address of the original IO so we can
10913 * find it easily. Something similar will need be done on the other
10914 * side so when we are done we can find the copy.
10916 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10917 union ctl_ha_msg msg_info;
10920 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10922 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
10923 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
10925 printf("1. ctsio %p\n", ctsio);
10927 msg_info.hdr.serializing_sc = NULL;
10928 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
10929 msg_info.scsi.tag_num = ctsio->tag_num;
10930 msg_info.scsi.tag_type = ctsio->tag_type;
10931 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
10933 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10935 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10936 (void *)&msg_info, sizeof(msg_info), 0)) >
10937 CTL_HA_STATUS_SUCCESS) {
10938 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
10940 printf("CTL:opcode is %x\n",opcode);
10943 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
10948 * XXX KDM this I/O is off the incoming queue, but hasn't
10949 * been inserted on any other queue. We may need to come
10950 * up with a holding queue while we wait for serialization
10951 * so that we have an idea of what we're waiting for from
10954 goto bailout_unlock;
10957 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
10958 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
10959 ctl_ooaq, ooa_links))) {
10960 case CTL_ACTION_BLOCK:
10961 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
10962 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
10964 goto bailout_unlock;
10965 break; /* NOTREACHED */
10966 case CTL_ACTION_PASS:
10967 case CTL_ACTION_SKIP:
10969 break; /* NOTREACHED */
10970 case CTL_ACTION_OVERLAP:
10971 ctl_set_overlapped_cmd(ctsio);
10972 mtx_unlock(&ctl_softc->ctl_lock);
10973 ctl_done((union ctl_io *)ctsio);
10975 break; /* NOTREACHED */
10976 case CTL_ACTION_OVERLAP_TAG:
10977 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
10978 mtx_unlock(&ctl_softc->ctl_lock);
10979 ctl_done((union ctl_io *)ctsio);
10981 break; /* NOTREACHED */
10982 case CTL_ACTION_ERROR:
10984 ctl_set_internal_failure(ctsio,
10986 /*retry_count*/ 0);
10987 mtx_unlock(&ctl_softc->ctl_lock);
10988 ctl_done((union ctl_io *)ctsio);
10990 break; /* NOTREACHED */
10993 goto bailout_unlock;
10996 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
10997 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
11000 mtx_unlock(&ctl_softc->ctl_lock);
11007 ctl_scsiio(struct ctl_scsiio *ctsio)
11010 struct ctl_cmd_entry *entry;
11012 retval = CTL_RETVAL_COMPLETE;
11014 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11016 entry = &ctl_cmd_table[ctsio->cdb[0]];
11019 * If this I/O has been aborted, just send it straight to
11020 * ctl_done() without executing it.
11022 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11023 ctl_done((union ctl_io *)ctsio);
11028 * All the checks should have been handled by ctl_scsiio_precheck().
11029 * We should be clear now to just execute the I/O.
11031 retval = entry->execute(ctsio);
11038 * Since we only implement one target right now, a bus reset simply resets
11039 * our single target.
11042 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11044 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11048 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11049 ctl_ua_type ua_type)
11051 struct ctl_lun *lun;
11054 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11055 union ctl_ha_msg msg_info;
11057 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11058 msg_info.hdr.nexus = io->io_hdr.nexus;
11059 if (ua_type==CTL_UA_TARG_RESET)
11060 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11062 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11063 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11064 msg_info.hdr.original_sc = NULL;
11065 msg_info.hdr.serializing_sc = NULL;
11066 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11067 (void *)&msg_info, sizeof(msg_info), 0)) {
11072 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11073 retval += ctl_lun_reset(lun, io, ua_type);
11079 * The LUN should always be set. The I/O is optional, and is used to
11080 * distinguish between I/Os sent by this initiator, and by other
11081 * initiators. We set unit attention for initiators other than this one.
11082 * SAM-3 is vague on this point. It does say that a unit attention should
11083 * be established for other initiators when a LUN is reset (see section
11084 * 5.7.3), but it doesn't specifically say that the unit attention should
11085 * be established for this particular initiator when a LUN is reset. Here
11086 * is the relevant text, from SAM-3 rev 8:
11088 * 5.7.2 When a SCSI initiator port aborts its own tasks
11090 * When a SCSI initiator port causes its own task(s) to be aborted, no
11091 * notification that the task(s) have been aborted shall be returned to
11092 * the SCSI initiator port other than the completion response for the
11093 * command or task management function action that caused the task(s) to
11094 * be aborted and notification(s) associated with related effects of the
11095 * action (e.g., a reset unit attention condition).
11097 * XXX KDM for now, we're setting unit attention for all initiators.
11100 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11104 uint32_t initindex;
11109 * Run through the OOA queue and abort each I/O.
11112 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11114 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11115 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11116 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11120 * This version sets unit attention for every
11123 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11124 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11125 if (initindex == i)
11127 lun->pending_sense[i].ua_pending |= ua_type;
11132 * A reset (any kind, really) clears reservations established with
11133 * RESERVE/RELEASE. It does not clear reservations established
11134 * with PERSISTENT RESERVE OUT, but we don't support that at the
11135 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11136 * reservations made with the RESERVE/RELEASE commands, because
11137 * those commands are obsolete in SPC-3.
11139 lun->flags &= ~CTL_LUN_RESERVED;
11141 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11142 ctl_clear_mask(lun->have_ca, i);
11143 lun->pending_sense[i].ua_pending |= ua_type;
11150 ctl_abort_task(union ctl_io *io)
11153 struct ctl_lun *lun;
11154 struct ctl_softc *ctl_softc;
11157 char printbuf[128];
11162 ctl_softc = control_softc;
11168 targ_lun = io->io_hdr.nexus.targ_lun;
11169 if (io->io_hdr.nexus.lun_map_fn != NULL)
11170 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11171 if ((targ_lun < CTL_MAX_LUNS)
11172 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11173 lun = ctl_softc->ctl_luns[targ_lun];
11178 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11179 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11183 * Run through the OOA queue and attempt to find the given I/O.
11184 * The target port, initiator ID, tag type and tag number have to
11185 * match the values that we got from the initiator. If we have an
11186 * untagged command to abort, simply abort the first untagged command
11187 * we come to. We only allow one untagged command at a time of course.
11190 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11192 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11193 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11195 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11197 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11198 lun->lun, xio->scsiio.tag_num,
11199 xio->scsiio.tag_type,
11200 (xio->io_hdr.blocked_links.tqe_prev
11201 == NULL) ? "" : " BLOCKED",
11202 (xio->io_hdr.flags &
11203 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11204 (xio->io_hdr.flags &
11205 CTL_FLAG_ABORT) ? " ABORT" : "",
11206 (xio->io_hdr.flags &
11207 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11208 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11210 printf("%s\n", sbuf_data(&sb));
11213 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11214 && (xio->io_hdr.nexus.initid.id ==
11215 io->io_hdr.nexus.initid.id)) {
11217 * If the abort says that the task is untagged, the
11218 * task in the queue must be untagged. Otherwise,
11219 * we just check to see whether the tag numbers
11220 * match. This is because the QLogic firmware
11221 * doesn't pass back the tag type in an abort
11225 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11226 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11227 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11230 * XXX KDM we've got problems with FC, because it
11231 * doesn't send down a tag type with aborts. So we
11232 * can only really go by the tag number...
11233 * This may cause problems with parallel SCSI.
11234 * Need to figure that out!!
11236 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11237 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11239 if ((io->io_hdr.flags &
11240 CTL_FLAG_FROM_OTHER_SC) == 0 &&
11241 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11242 union ctl_ha_msg msg_info;
11244 io->io_hdr.flags |=
11245 CTL_FLAG_SENT_2OTHER_SC;
11246 msg_info.hdr.nexus = io->io_hdr.nexus;
11247 msg_info.task.task_action =
11248 CTL_TASK_ABORT_TASK;
11249 msg_info.task.tag_num =
11250 io->taskio.tag_num;
11251 msg_info.task.tag_type =
11252 io->taskio.tag_type;
11253 msg_info.hdr.msg_type =
11254 CTL_MSG_MANAGE_TASKS;
11255 msg_info.hdr.original_sc = NULL;
11256 msg_info.hdr.serializing_sc = NULL;
11258 printf("Sent Abort to other side\n");
11260 if (CTL_HA_STATUS_SUCCESS !=
11261 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11263 sizeof(msg_info), 0)) {
11267 printf("ctl_abort_task: found I/O to abort\n");
11278 * This isn't really an error. It's entirely possible for
11279 * the abort and command completion to cross on the wire.
11280 * This is more of an informative/diagnostic error.
11283 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11284 "%d:%d:%d:%d tag %d type %d\n",
11285 io->io_hdr.nexus.initid.id,
11286 io->io_hdr.nexus.targ_port,
11287 io->io_hdr.nexus.targ_target.id,
11288 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11289 io->taskio.tag_type);
11297 * This routine cannot block! It must be callable from an interrupt
11298 * handler as well as from the work thread.
11301 ctl_run_task_queue(struct ctl_softc *ctl_softc)
11303 union ctl_io *io, *next_io;
11305 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
11307 CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
11309 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
11310 io != NULL; io = next_io) {
11312 const char *task_desc;
11314 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11318 switch (io->io_hdr.io_type) {
11319 case CTL_IO_TASK: {
11320 task_desc = ctl_scsi_task_string(&io->taskio);
11321 if (task_desc != NULL) {
11323 csevent_log(CSC_CTL | CSC_SHELF_SW |
11325 csevent_LogType_Trace,
11326 csevent_Severity_Information,
11327 csevent_AlertLevel_Green,
11328 csevent_FRU_Firmware,
11329 csevent_FRU_Unknown,
11330 "CTL: received task: %s",task_desc);
11334 csevent_log(CSC_CTL | CSC_SHELF_SW |
11336 csevent_LogType_Trace,
11337 csevent_Severity_Information,
11338 csevent_AlertLevel_Green,
11339 csevent_FRU_Firmware,
11340 csevent_FRU_Unknown,
11341 "CTL: received unknown task "
11343 io->taskio.task_action,
11344 io->taskio.task_action);
11347 switch (io->taskio.task_action) {
11348 case CTL_TASK_ABORT_TASK:
11349 retval = ctl_abort_task(io);
11351 case CTL_TASK_ABORT_TASK_SET:
11353 case CTL_TASK_CLEAR_ACA:
11355 case CTL_TASK_CLEAR_TASK_SET:
11357 case CTL_TASK_LUN_RESET: {
11358 struct ctl_lun *lun;
11362 targ_lun = io->io_hdr.nexus.targ_lun;
11363 if (io->io_hdr.nexus.lun_map_fn != NULL)
11364 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11366 if ((targ_lun < CTL_MAX_LUNS)
11367 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11368 lun = ctl_softc->ctl_luns[targ_lun];
11374 if (!(io->io_hdr.flags &
11375 CTL_FLAG_FROM_OTHER_SC)) {
11376 union ctl_ha_msg msg_info;
11378 io->io_hdr.flags |=
11379 CTL_FLAG_SENT_2OTHER_SC;
11380 msg_info.hdr.msg_type =
11381 CTL_MSG_MANAGE_TASKS;
11382 msg_info.hdr.nexus = io->io_hdr.nexus;
11383 msg_info.task.task_action =
11384 CTL_TASK_LUN_RESET;
11385 msg_info.hdr.original_sc = NULL;
11386 msg_info.hdr.serializing_sc = NULL;
11387 if (CTL_HA_STATUS_SUCCESS !=
11388 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11390 sizeof(msg_info), 0)) {
11394 retval = ctl_lun_reset(lun, io,
11398 case CTL_TASK_TARGET_RESET:
11399 retval = ctl_target_reset(ctl_softc, io,
11400 CTL_UA_TARG_RESET);
11402 case CTL_TASK_BUS_RESET:
11403 retval = ctl_bus_reset(ctl_softc, io);
11405 case CTL_TASK_PORT_LOGIN:
11407 case CTL_TASK_PORT_LOGOUT:
11410 printf("ctl_run_task_queue: got unknown task "
11411 "management event %d\n",
11412 io->taskio.task_action);
11416 io->io_hdr.status = CTL_SUCCESS;
11418 io->io_hdr.status = CTL_ERROR;
11420 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11421 ctl_io_hdr, links);
11423 * This will queue this I/O to the done queue, but the
11424 * work thread won't be able to process it until we
11425 * return and the lock is released.
11427 ctl_done_lock(io, /*have_lock*/ 1);
11432 printf("%s: invalid I/O type %d msg %d cdb %x"
11433 " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11434 __func__, io->io_hdr.io_type,
11435 io->io_hdr.msg_type, io->scsiio.cdb[0],
11436 (uintmax_t)io->io_hdr.nexus.initid.id,
11437 io->io_hdr.nexus.targ_port,
11438 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11439 io->io_hdr.nexus.targ_lun /* XXX */,
11440 (io->io_hdr.io_type == CTL_IO_TASK) ?
11441 io->taskio.tag_num : io->scsiio.tag_num);
11442 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11443 ctl_io_hdr, links);
11450 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11454 * For HA operation. Handle commands that come in from the other
11458 ctl_handle_isc(union ctl_io *io)
11461 struct ctl_lun *lun;
11462 struct ctl_softc *ctl_softc;
11465 ctl_softc = control_softc;
11467 targ_lun = io->io_hdr.nexus.targ_lun;
11468 if (io->io_hdr.nexus.lun_map_fn != NULL)
11469 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11470 lun = ctl_softc->ctl_luns[targ_lun];
11472 switch (io->io_hdr.msg_type) {
11473 case CTL_MSG_SERIALIZE:
11474 free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11477 case CTL_MSG_R2R: {
11479 struct ctl_cmd_entry *entry;
11482 * This is only used in SER_ONLY mode.
11485 opcode = io->scsiio.cdb[0];
11486 entry = &ctl_cmd_table[opcode];
11487 mtx_lock(&ctl_softc->ctl_lock);
11488 if (ctl_scsiio_lun_check(ctl_softc, lun,
11489 entry, (struct ctl_scsiio *)io) != 0) {
11490 ctl_done_lock(io, /*have_lock*/ 1);
11491 mtx_unlock(&ctl_softc->ctl_lock);
11494 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11495 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11496 &io->io_hdr, links);
11497 mtx_unlock(&ctl_softc->ctl_lock);
11500 case CTL_MSG_FINISH_IO:
11501 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11503 ctl_done_lock(io, /*have_lock*/ 0);
11506 mtx_lock(&ctl_softc->ctl_lock);
11507 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11509 STAILQ_REMOVE(&ctl_softc->task_queue,
11510 &io->io_hdr, ctl_io_hdr, links);
11511 ctl_check_blocked(lun);
11512 mtx_unlock(&ctl_softc->ctl_lock);
11515 case CTL_MSG_PERS_ACTION:
11516 ctl_hndl_per_res_out_on_other_sc(
11517 (union ctl_ha_msg *)&io->presio.pr_msg);
11520 case CTL_MSG_BAD_JUJU:
11522 ctl_done_lock(io, /*have_lock*/ 0);
11524 case CTL_MSG_DATAMOVE:
11525 /* Only used in XFER mode */
11527 ctl_datamove_remote(io);
11529 case CTL_MSG_DATAMOVE_DONE:
11530 /* Only used in XFER mode */
11532 io->scsiio.be_move_done(io);
11536 printf("%s: Invalid message type %d\n",
11537 __func__, io->io_hdr.msg_type);
11547 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11548 * there is no match.
11550 static ctl_lun_error_pattern
11551 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11553 struct ctl_cmd_entry *entry;
11554 ctl_lun_error_pattern filtered_pattern, pattern;
11557 pattern = desc->error_pattern;
11560 * XXX KDM we need more data passed into this function to match a
11561 * custom pattern, and we actually need to implement custom pattern
11564 if (pattern & CTL_LUN_PAT_CMD)
11565 return (CTL_LUN_PAT_CMD);
11567 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11568 return (CTL_LUN_PAT_ANY);
11570 opcode = ctsio->cdb[0];
11571 entry = &ctl_cmd_table[opcode];
11573 filtered_pattern = entry->pattern & pattern;
11576 * If the user requested specific flags in the pattern (e.g.
11577 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11580 * If the user did not specify any flags, it doesn't matter whether
11581 * or not the command supports the flags.
11583 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11584 (pattern & ~CTL_LUN_PAT_MASK))
11585 return (CTL_LUN_PAT_NONE);
11588 * If the user asked for a range check, see if the requested LBA
11589 * range overlaps with this command's LBA range.
11591 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11597 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11599 return (CTL_LUN_PAT_NONE);
11601 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11602 desc->lba_range.len);
11604 * A "pass" means that the LBA ranges don't overlap, so
11605 * this doesn't match the user's range criteria.
11607 if (action == CTL_ACTION_PASS)
11608 return (CTL_LUN_PAT_NONE);
11611 return (filtered_pattern);
11615 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11617 struct ctl_error_desc *desc, *desc2;
11619 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
11621 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11622 ctl_lun_error_pattern pattern;
11624 * Check to see whether this particular command matches
11625 * the pattern in the descriptor.
11627 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11628 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11631 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11632 case CTL_LUN_INJ_ABORTED:
11633 ctl_set_aborted(&io->scsiio);
11635 case CTL_LUN_INJ_MEDIUM_ERR:
11636 ctl_set_medium_error(&io->scsiio);
11638 case CTL_LUN_INJ_UA:
11639 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11641 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11643 case CTL_LUN_INJ_CUSTOM:
11645 * We're assuming the user knows what he is doing.
11646 * Just copy the sense information without doing
11649 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11650 ctl_min(sizeof(desc->custom_sense),
11651 sizeof(io->scsiio.sense_data)));
11652 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11653 io->scsiio.sense_len = SSD_FULL_SIZE;
11654 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11656 case CTL_LUN_INJ_NONE:
11659 * If this is an error injection type we don't know
11660 * about, clear the continuous flag (if it is set)
11661 * so it will get deleted below.
11663 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11667 * By default, each error injection action is a one-shot
11669 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11672 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11678 #ifdef CTL_IO_DELAY
11680 ctl_datamove_timer_wakeup(void *arg)
11684 io = (union ctl_io *)arg;
11688 #endif /* CTL_IO_DELAY */
11691 ctl_datamove(union ctl_io *io)
11693 void (*fe_datamove)(union ctl_io *io);
11695 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11697 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11700 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11705 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11706 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11708 sbuf_cat(&sb, path_str);
11709 switch (io->io_hdr.io_type) {
11711 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11712 sbuf_printf(&sb, "\n");
11713 sbuf_cat(&sb, path_str);
11714 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11715 io->scsiio.tag_num, io->scsiio.tag_type);
11718 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11719 "Tag Type: %d\n", io->taskio.task_action,
11720 io->taskio.tag_num, io->taskio.tag_type);
11723 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11724 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11727 sbuf_cat(&sb, path_str);
11728 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11729 (intmax_t)time_uptime - io->io_hdr.start_time);
11731 printf("%s", sbuf_data(&sb));
11733 #endif /* CTL_TIME_IO */
11735 mtx_lock(&control_softc->ctl_lock);
11736 #ifdef CTL_IO_DELAY
11737 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11738 struct ctl_lun *lun;
11740 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11742 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11744 struct ctl_lun *lun;
11746 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11748 && (lun->delay_info.datamove_delay > 0)) {
11749 struct callout *callout;
11751 callout = (struct callout *)&io->io_hdr.timer_bytes;
11752 callout_init(callout, /*mpsafe*/ 1);
11753 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11754 callout_reset(callout,
11755 lun->delay_info.datamove_delay * hz,
11756 ctl_datamove_timer_wakeup, io);
11757 if (lun->delay_info.datamove_type ==
11758 CTL_DELAY_TYPE_ONESHOT)
11759 lun->delay_info.datamove_delay = 0;
11760 mtx_unlock(&control_softc->ctl_lock);
11766 * If we have any pending task management commands, process them
11767 * first. This is necessary to eliminate a race condition with the
11770 * - FETD submits a task management command, like an abort.
11771 * - Back end calls fe_datamove() to move the data for the aborted
11772 * command. The FETD can't really accept it, but if it did, it
11773 * would end up transmitting data for a command that the initiator
11774 * told us to abort.
11776 * We close the race by processing all pending task management
11777 * commands here (we can't block!), and then check this I/O to see
11778 * if it has been aborted. If so, return it to the back end with
11779 * bad status, so the back end can say return an error to the back end
11780 * and then when the back end returns an error, we can return the
11781 * aborted command to the FETD, so it can clean up its resources.
11783 if (control_softc->flags & CTL_FLAG_TASK_PENDING)
11784 ctl_run_task_queue(control_softc);
11787 * This command has been aborted. Set the port status, so we fail
11790 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
11791 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
11792 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
11793 io->io_hdr.nexus.targ_port,
11794 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11795 io->io_hdr.nexus.targ_lun);
11796 io->io_hdr.status = CTL_CMD_ABORTED;
11797 io->io_hdr.port_status = 31337;
11798 mtx_unlock(&control_softc->ctl_lock);
11800 * Note that the backend, in this case, will get the
11801 * callback in its context. In other cases it may get
11802 * called in the frontend's interrupt thread context.
11804 io->scsiio.be_move_done(io);
11809 * If we're in XFER mode and this I/O is from the other shelf
11810 * controller, we need to send the DMA to the other side to
11811 * actually transfer the data to/from the host. In serialize only
11812 * mode the transfer happens below CTL and ctl_datamove() is only
11813 * called on the machine that originally received the I/O.
11815 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
11816 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11817 union ctl_ha_msg msg;
11818 uint32_t sg_entries_sent;
11822 memset(&msg, 0, sizeof(msg));
11823 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
11824 msg.hdr.original_sc = io->io_hdr.original_sc;
11825 msg.hdr.serializing_sc = io;
11826 msg.hdr.nexus = io->io_hdr.nexus;
11827 msg.dt.flags = io->io_hdr.flags;
11829 * We convert everything into a S/G list here. We can't
11830 * pass by reference, only by value between controllers.
11831 * So we can't pass a pointer to the S/G list, only as many
11832 * S/G entries as we can fit in here. If it's possible for
11833 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
11834 * then we need to break this up into multiple transfers.
11836 if (io->scsiio.kern_sg_entries == 0) {
11837 msg.dt.kern_sg_entries = 1;
11839 * If this is in cached memory, flush the cache
11840 * before we send the DMA request to the other
11841 * controller. We want to do this in either the
11842 * read or the write case. The read case is
11843 * straightforward. In the write case, we want to
11844 * make sure nothing is in the local cache that
11845 * could overwrite the DMAed data.
11847 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11849 * XXX KDM use bus_dmamap_sync() here.
11854 * Convert to a physical address if this is a
11857 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
11858 msg.dt.sg_list[0].addr =
11859 io->scsiio.kern_data_ptr;
11862 * XXX KDM use busdma here!
11865 msg.dt.sg_list[0].addr = (void *)
11866 vtophys(io->scsiio.kern_data_ptr);
11870 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
11873 struct ctl_sg_entry *sgl;
11876 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
11877 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
11878 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11880 * XXX KDM use bus_dmamap_sync() here.
11885 msg.dt.kern_data_len = io->scsiio.kern_data_len;
11886 msg.dt.kern_total_len = io->scsiio.kern_total_len;
11887 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
11888 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
11889 msg.dt.sg_sequence = 0;
11892 * Loop until we've sent all of the S/G entries. On the
11893 * other end, we'll recompose these S/G entries into one
11894 * contiguous list before passing it to the
11896 for (sg_entries_sent = 0; sg_entries_sent <
11897 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
11898 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
11899 sizeof(msg.dt.sg_list[0])),
11900 msg.dt.kern_sg_entries - sg_entries_sent);
11902 if (do_sg_copy != 0) {
11903 struct ctl_sg_entry *sgl;
11906 sgl = (struct ctl_sg_entry *)
11907 io->scsiio.kern_data_ptr;
11909 * If this is in cached memory, flush the cache
11910 * before we send the DMA request to the other
11911 * controller. We want to do this in either
11912 * the * read or the write case. The read
11913 * case is straightforward. In the write
11914 * case, we want to make sure nothing is
11915 * in the local cache that could overwrite
11919 for (i = sg_entries_sent, j = 0;
11920 i < msg.dt.cur_sg_entries; i++, j++) {
11921 if ((io->io_hdr.flags &
11922 CTL_FLAG_NO_DATASYNC) == 0) {
11924 * XXX KDM use bus_dmamap_sync()
11927 if ((io->io_hdr.flags &
11928 CTL_FLAG_BUS_ADDR) == 0) {
11930 * XXX KDM use busdma.
11933 msg.dt.sg_list[j].addr =(void *)
11934 vtophys(sgl[i].addr);
11937 msg.dt.sg_list[j].addr =
11940 msg.dt.sg_list[j].len = sgl[i].len;
11944 sg_entries_sent += msg.dt.cur_sg_entries;
11945 if (sg_entries_sent >= msg.dt.kern_sg_entries)
11946 msg.dt.sg_last = 1;
11948 msg.dt.sg_last = 0;
11951 * XXX KDM drop and reacquire the lock here?
11953 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
11954 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
11956 * XXX do something here.
11960 msg.dt.sent_sg_entries = sg_entries_sent;
11962 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11963 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
11964 ctl_failover_io(io, /*have_lock*/ 1);
11969 * Lookup the fe_datamove() function for this particular
11973 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11974 mtx_unlock(&control_softc->ctl_lock);
11981 ctl_send_datamove_done(union ctl_io *io, int have_lock)
11983 union ctl_ha_msg msg;
11986 memset(&msg, 0, sizeof(msg));
11988 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
11989 msg.hdr.original_sc = io;
11990 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
11991 msg.hdr.nexus = io->io_hdr.nexus;
11992 msg.hdr.status = io->io_hdr.status;
11993 msg.scsi.tag_num = io->scsiio.tag_num;
11994 msg.scsi.tag_type = io->scsiio.tag_type;
11995 msg.scsi.scsi_status = io->scsiio.scsi_status;
11996 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
11997 sizeof(io->scsiio.sense_data));
11998 msg.scsi.sense_len = io->scsiio.sense_len;
11999 msg.scsi.sense_residual = io->scsiio.sense_residual;
12000 msg.scsi.fetd_status = io->io_hdr.port_status;
12001 msg.scsi.residual = io->scsiio.residual;
12002 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12004 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12005 ctl_failover_io(io, /*have_lock*/ have_lock);
12009 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12010 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12011 /* XXX do something if this fails */
12017 * The DMA to the remote side is done, now we need to tell the other side
12018 * we're done so it can continue with its data movement.
12021 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12027 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12028 printf("%s: ISC DMA write failed with error %d", __func__,
12030 ctl_set_internal_failure(&io->scsiio,
12032 /*retry_count*/ rq->ret);
12035 ctl_dt_req_free(rq);
12038 * In this case, we had to malloc the memory locally. Free it.
12040 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12042 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12043 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12046 * The data is in local and remote memory, so now we need to send
12047 * status (good or back) back to the other side.
12049 ctl_send_datamove_done(io, /*have_lock*/ 0);
12053 * We've moved the data from the host/controller into local memory. Now we
12054 * need to push it over to the remote controller's memory.
12057 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12063 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12064 ctl_datamove_remote_write_cb);
12070 ctl_datamove_remote_write(union ctl_io *io)
12073 void (*fe_datamove)(union ctl_io *io);
12076 * - Get the data from the host/HBA into local memory.
12077 * - DMA memory from the local controller to the remote controller.
12078 * - Send status back to the remote controller.
12081 retval = ctl_datamove_remote_sgl_setup(io);
12085 /* Switch the pointer over so the FETD knows what to do */
12086 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12089 * Use a custom move done callback, since we need to send completion
12090 * back to the other controller, not to the backend on this side.
12092 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12094 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12103 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12112 * In this case, we had to malloc the memory locally. Free it.
12114 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12116 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12117 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12121 scsi_path_string(io, path_str, sizeof(path_str));
12122 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12123 sbuf_cat(&sb, path_str);
12124 scsi_command_string(&io->scsiio, NULL, &sb);
12125 sbuf_printf(&sb, "\n");
12126 sbuf_cat(&sb, path_str);
12127 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12128 io->scsiio.tag_num, io->scsiio.tag_type);
12129 sbuf_cat(&sb, path_str);
12130 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12131 io->io_hdr.flags, io->io_hdr.status);
12133 printk("%s", sbuf_data(&sb));
12138 * The read is done, now we need to send status (good or bad) back
12139 * to the other side.
12141 ctl_send_datamove_done(io, /*have_lock*/ 0);
12147 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12150 void (*fe_datamove)(union ctl_io *io);
12154 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12155 printf("%s: ISC DMA read failed with error %d", __func__,
12157 ctl_set_internal_failure(&io->scsiio,
12159 /*retry_count*/ rq->ret);
12162 ctl_dt_req_free(rq);
12164 /* Switch the pointer over so the FETD knows what to do */
12165 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12168 * Use a custom move done callback, since we need to send completion
12169 * back to the other controller, not to the backend on this side.
12171 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12173 /* XXX KDM add checks like the ones in ctl_datamove? */
12175 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12181 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12183 struct ctl_sg_entry *local_sglist, *remote_sglist;
12184 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12185 struct ctl_softc *softc;
12190 softc = control_softc;
12192 local_sglist = io->io_hdr.local_sglist;
12193 local_dma_sglist = io->io_hdr.local_dma_sglist;
12194 remote_sglist = io->io_hdr.remote_sglist;
12195 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12197 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12198 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12199 local_sglist[i].len = remote_sglist[i].len;
12202 * XXX Detect the situation where the RS-level I/O
12203 * redirector on the other side has already read the
12204 * data off of the AOR RS on this side, and
12205 * transferred it to remote (mirror) memory on the
12206 * other side. Since we already have the data in
12207 * memory here, we just need to use it.
12209 * XXX KDM this can probably be removed once we
12210 * get the cache device code in and take the
12211 * current AOR implementation out.
12214 if ((remote_sglist[i].addr >=
12215 (void *)vtophys(softc->mirr->addr))
12216 && (remote_sglist[i].addr <
12217 ((void *)vtophys(softc->mirr->addr) +
12218 CacheMirrorOffset))) {
12219 local_sglist[i].addr = remote_sglist[i].addr -
12221 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12223 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12225 local_sglist[i].addr = remote_sglist[i].addr +
12230 printf("%s: local %p, remote %p, len %d\n",
12231 __func__, local_sglist[i].addr,
12232 remote_sglist[i].addr, local_sglist[i].len);
12236 uint32_t len_to_go;
12239 * In this case, we don't have automatically allocated
12240 * memory for this I/O on this controller. This typically
12241 * happens with internal CTL I/O -- e.g. inquiry, mode
12242 * sense, etc. Anything coming from RAIDCore will have
12243 * a mirror area available.
12245 len_to_go = io->scsiio.kern_data_len;
12248 * Clear the no datasync flag, we have to use malloced
12251 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12254 * The difficult thing here is that the size of the various
12255 * S/G segments may be different than the size from the
12256 * remote controller. That'll make it harder when DMAing
12257 * the data back to the other side.
12259 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12260 sizeof(io->io_hdr.remote_sglist[0])) &&
12261 (len_to_go > 0); i++) {
12262 local_sglist[i].len = ctl_min(len_to_go, 131072);
12263 CTL_SIZE_8B(local_dma_sglist[i].len,
12264 local_sglist[i].len);
12265 local_sglist[i].addr =
12266 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12268 local_dma_sglist[i].addr = local_sglist[i].addr;
12270 if (local_sglist[i].addr == NULL) {
12273 printf("malloc failed for %zd bytes!",
12274 local_dma_sglist[i].len);
12275 for (j = 0; j < i; j++) {
12276 free(local_sglist[j].addr, M_CTL);
12278 ctl_set_internal_failure(&io->scsiio,
12280 /*retry_count*/ 4857);
12282 goto bailout_error;
12285 /* XXX KDM do we need a sync here? */
12287 len_to_go -= local_sglist[i].len;
12290 * Reset the number of S/G entries accordingly. The
12291 * original number of S/G entries is available in
12294 io->scsiio.kern_sg_entries = i;
12297 printf("%s: kern_sg_entries = %d\n", __func__,
12298 io->scsiio.kern_sg_entries);
12299 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12300 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12301 local_sglist[i].addr, local_sglist[i].len,
12302 local_dma_sglist[i].len);
12311 ctl_send_datamove_done(io, /*have_lock*/ 0);
12317 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12318 ctl_ha_dt_cb callback)
12320 struct ctl_ha_dt_req *rq;
12321 struct ctl_sg_entry *remote_sglist, *local_sglist;
12322 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12323 uint32_t local_used, remote_used, total_used;
12329 rq = ctl_dt_req_alloc();
12332 * If we failed to allocate the request, and if the DMA didn't fail
12333 * anyway, set busy status. This is just a resource allocation
12337 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12338 ctl_set_busy(&io->scsiio);
12340 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12343 ctl_dt_req_free(rq);
12346 * The data move failed. We need to return status back
12347 * to the other controller. No point in trying to DMA
12348 * data to the remote controller.
12351 ctl_send_datamove_done(io, /*have_lock*/ 0);
12358 local_sglist = io->io_hdr.local_sglist;
12359 local_dma_sglist = io->io_hdr.local_dma_sglist;
12360 remote_sglist = io->io_hdr.remote_sglist;
12361 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12366 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12367 rq->ret = CTL_HA_STATUS_SUCCESS;
12374 * Pull/push the data over the wire from/to the other controller.
12375 * This takes into account the possibility that the local and
12376 * remote sglists may not be identical in terms of the size of
12377 * the elements and the number of elements.
12379 * One fundamental assumption here is that the length allocated for
12380 * both the local and remote sglists is identical. Otherwise, we've
12381 * essentially got a coding error of some sort.
12383 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12385 uint32_t cur_len, dma_length;
12388 rq->id = CTL_HA_DATA_CTL;
12389 rq->command = command;
12393 * Both pointers should be aligned. But it is possible
12394 * that the allocation length is not. They should both
12395 * also have enough slack left over at the end, though,
12396 * to round up to the next 8 byte boundary.
12398 cur_len = ctl_min(local_sglist[i].len - local_used,
12399 remote_sglist[j].len - remote_used);
12402 * In this case, we have a size issue and need to decrease
12403 * the size, except in the case where we actually have less
12404 * than 8 bytes left. In that case, we need to increase
12405 * the DMA length to get the last bit.
12407 if ((cur_len & 0x7) != 0) {
12408 if (cur_len > 0x7) {
12409 cur_len = cur_len - (cur_len & 0x7);
12410 dma_length = cur_len;
12412 CTL_SIZE_8B(dma_length, cur_len);
12416 dma_length = cur_len;
12419 * If we had to allocate memory for this I/O, instead of using
12420 * the non-cached mirror memory, we'll need to flush the cache
12421 * before trying to DMA to the other controller.
12423 * We could end up doing this multiple times for the same
12424 * segment if we have a larger local segment than remote
12425 * segment. That shouldn't be an issue.
12427 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12429 * XXX KDM use bus_dmamap_sync() here.
12433 rq->size = dma_length;
12435 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12436 tmp_ptr += local_used;
12438 /* Use physical addresses when talking to ISC hardware */
12439 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12440 /* XXX KDM use busdma */
12442 rq->local = vtophys(tmp_ptr);
12445 rq->local = tmp_ptr;
12447 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12448 tmp_ptr += remote_used;
12449 rq->remote = tmp_ptr;
12451 rq->callback = NULL;
12453 local_used += cur_len;
12454 if (local_used >= local_sglist[i].len) {
12459 remote_used += cur_len;
12460 if (remote_used >= remote_sglist[j].len) {
12464 total_used += cur_len;
12466 if (total_used >= io->scsiio.kern_data_len)
12467 rq->callback = callback;
12469 if ((rq->size & 0x7) != 0) {
12470 printf("%s: warning: size %d is not on 8b boundary\n",
12471 __func__, rq->size);
12473 if (((uintptr_t)rq->local & 0x7) != 0) {
12474 printf("%s: warning: local %p not on 8b boundary\n",
12475 __func__, rq->local);
12477 if (((uintptr_t)rq->remote & 0x7) != 0) {
12478 printf("%s: warning: remote %p not on 8b boundary\n",
12479 __func__, rq->local);
12482 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12483 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12484 rq->local, rq->remote, rq->size);
12487 isc_ret = ctl_dt_single(rq);
12488 if (isc_ret == CTL_HA_STATUS_WAIT)
12491 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12492 rq->ret = CTL_HA_STATUS_SUCCESS;
12506 ctl_datamove_remote_read(union ctl_io *io)
12512 * This will send an error to the other controller in the case of a
12515 retval = ctl_datamove_remote_sgl_setup(io);
12519 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12520 ctl_datamove_remote_read_cb);
12522 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12524 * Make sure we free memory if there was an error.. The
12525 * ctl_datamove_remote_xfer() function will send the
12526 * datamove done message, or call the callback with an
12527 * error if there is a problem.
12529 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12530 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12537 * Process a datamove request from the other controller. This is used for
12538 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12539 * first. Once that is complete, the data gets DMAed into the remote
12540 * controller's memory. For reads, we DMA from the remote controller's
12541 * memory into our memory first, and then move it out to the FETD.
12544 ctl_datamove_remote(union ctl_io *io)
12546 struct ctl_softc *softc;
12548 softc = control_softc;
12550 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12553 * Note that we look for an aborted I/O here, but don't do some of
12554 * the other checks that ctl_datamove() normally does. We don't
12555 * need to run the task queue, because this I/O is on the ISC
12556 * queue, which is executed by the work thread after the task queue.
12557 * We don't need to run the datamove delay code, since that should
12558 * have been done if need be on the other controller.
12560 mtx_lock(&softc->ctl_lock);
12562 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12564 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12565 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12566 io->io_hdr.nexus.targ_port,
12567 io->io_hdr.nexus.targ_target.id,
12568 io->io_hdr.nexus.targ_lun);
12569 io->io_hdr.status = CTL_CMD_ABORTED;
12570 io->io_hdr.port_status = 31338;
12572 mtx_unlock(&softc->ctl_lock);
12574 ctl_send_datamove_done(io, /*have_lock*/ 0);
12579 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12580 mtx_unlock(&softc->ctl_lock);
12581 ctl_datamove_remote_write(io);
12582 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12583 mtx_unlock(&softc->ctl_lock);
12584 ctl_datamove_remote_read(io);
12586 union ctl_ha_msg msg;
12587 struct scsi_sense_data *sense;
12591 memset(&msg, 0, sizeof(msg));
12593 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12594 msg.hdr.status = CTL_SCSI_ERROR;
12595 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12597 retry_count = 4243;
12599 sense = &msg.scsi.sense_data;
12600 sks[0] = SSD_SCS_VALID;
12601 sks[1] = (retry_count >> 8) & 0xff;
12602 sks[2] = retry_count & 0xff;
12604 /* "Internal target failure" */
12605 scsi_set_sense_data(sense,
12606 /*sense_format*/ SSD_TYPE_NONE,
12607 /*current_error*/ 1,
12608 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12611 /*type*/ SSD_ELEM_SKS,
12612 /*size*/ sizeof(sks),
12616 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12617 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12618 ctl_failover_io(io, /*have_lock*/ 1);
12619 mtx_unlock(&softc->ctl_lock);
12623 mtx_unlock(&softc->ctl_lock);
12625 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12626 CTL_HA_STATUS_SUCCESS) {
12627 /* XXX KDM what to do if this fails? */
12635 ctl_process_done(union ctl_io *io, int have_lock)
12637 struct ctl_lun *lun;
12638 struct ctl_softc *ctl_softc;
12639 void (*fe_done)(union ctl_io *io);
12640 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12642 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12645 control_softc->ctl_ports[targ_port]->fe_done;
12648 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12653 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12654 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12656 sbuf_cat(&sb, path_str);
12657 switch (io->io_hdr.io_type) {
12659 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12660 sbuf_printf(&sb, "\n");
12661 sbuf_cat(&sb, path_str);
12662 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12663 io->scsiio.tag_num, io->scsiio.tag_type);
12666 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12667 "Tag Type: %d\n", io->taskio.task_action,
12668 io->taskio.tag_num, io->taskio.tag_type);
12671 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12672 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12675 sbuf_cat(&sb, path_str);
12676 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12677 (intmax_t)time_uptime - io->io_hdr.start_time);
12679 printf("%s", sbuf_data(&sb));
12681 #endif /* CTL_TIME_IO */
12683 switch (io->io_hdr.io_type) {
12687 if (bootverbose || verbose > 0)
12688 ctl_io_error_print(io, NULL);
12689 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12693 return (CTL_RETVAL_COMPLETE);
12696 printf("ctl_process_done: invalid io type %d\n",
12697 io->io_hdr.io_type);
12698 panic("ctl_process_done: invalid io type %d\n",
12699 io->io_hdr.io_type);
12700 break; /* NOTREACHED */
12703 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12705 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12706 io->io_hdr.nexus.targ_lun));
12710 ctl_softc = lun->ctl_softc;
12713 * Remove this from the OOA queue.
12715 if (have_lock == 0)
12716 mtx_lock(&ctl_softc->ctl_lock);
12719 * Check to see if we have any errors to inject here. We only
12720 * inject errors for commands that don't already have errors set.
12722 if ((STAILQ_FIRST(&lun->error_list) != NULL)
12723 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12724 ctl_inject_error(lun, io);
12727 * XXX KDM how do we treat commands that aren't completed
12730 * XXX KDM should we also track I/O latency?
12732 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
12733 uint32_t blocksize;
12735 struct bintime cur_bt;
12738 if ((lun->be_lun != NULL)
12739 && (lun->be_lun->blocksize != 0))
12740 blocksize = lun->be_lun->blocksize;
12744 switch (io->io_hdr.io_type) {
12745 case CTL_IO_SCSI: {
12747 struct ctl_lba_len lbalen;
12750 switch (io->scsiio.cdb[0]) {
12761 case WRITE_VERIFY_10:
12762 case WRITE_VERIFY_12:
12763 case WRITE_VERIFY_16:
12764 memcpy(&lbalen, io->io_hdr.ctl_private[
12765 CTL_PRIV_LBA_LEN].bytes, sizeof(lbalen));
12768 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
12769 lbalen.len * blocksize;
12770 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
12774 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
12775 &io->io_hdr.dma_bt);
12776 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
12777 io->io_hdr.num_dmas;
12778 getbintime(&cur_bt);
12779 bintime_sub(&cur_bt,
12780 &io->io_hdr.start_bt);
12783 &lun->stats.ports[targ_port].time[CTL_STATS_READ],
12787 cs_prof_gettime(&cur_ticks);
12788 lun->stats.time[CTL_STATS_READ] +=
12790 io->io_hdr.start_ticks;
12793 lun->stats.time[CTL_STATS_READ] +=
12794 jiffies - io->io_hdr.start_time;
12796 #endif /* CTL_TIME_IO */
12798 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
12799 lbalen.len * blocksize;
12800 lun->stats.ports[targ_port].operations[
12801 CTL_STATS_WRITE]++;
12805 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
12806 &io->io_hdr.dma_bt);
12807 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
12808 io->io_hdr.num_dmas;
12809 getbintime(&cur_bt);
12810 bintime_sub(&cur_bt,
12811 &io->io_hdr.start_bt);
12814 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
12817 cs_prof_gettime(&cur_ticks);
12818 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12820 io->io_hdr.start_ticks;
12821 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12822 jiffies - io->io_hdr.start_time;
12824 #endif /* CTL_TIME_IO */
12828 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
12832 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
12833 &io->io_hdr.dma_bt);
12834 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
12835 io->io_hdr.num_dmas;
12836 getbintime(&cur_bt);
12837 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12839 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
12843 cs_prof_gettime(&cur_ticks);
12844 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12846 io->io_hdr.start_ticks;
12847 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12848 jiffies - io->io_hdr.start_time;
12850 #endif /* CTL_TIME_IO */
12860 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12863 * Run through the blocked queue on this LUN and see if anything
12864 * has become unblocked, now that this transaction is done.
12866 ctl_check_blocked(lun);
12869 * If the LUN has been invalidated, free it if there is nothing
12870 * left on its OOA queue.
12872 if ((lun->flags & CTL_LUN_INVALID)
12873 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
12877 * If this command has been aborted, make sure we set the status
12878 * properly. The FETD is responsible for freeing the I/O and doing
12879 * whatever it needs to do to clean up its state.
12881 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12882 io->io_hdr.status = CTL_CMD_ABORTED;
12885 * We print out status for every task management command. For SCSI
12886 * commands, we filter out any unit attention errors; they happen
12887 * on every boot, and would clutter up the log. Note: task
12888 * management commands aren't printed here, they are printed above,
12889 * since they should never even make it down here.
12891 switch (io->io_hdr.io_type) {
12892 case CTL_IO_SCSI: {
12893 int error_code, sense_key, asc, ascq;
12897 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
12898 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
12900 * Since this is just for printing, no need to
12901 * show errors here.
12903 scsi_extract_sense_len(&io->scsiio.sense_data,
12904 io->scsiio.sense_len,
12909 /*show_errors*/ 0);
12912 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
12913 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
12914 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
12915 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
12917 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
12918 ctl_softc->skipped_prints++;
12919 if (have_lock == 0)
12920 mtx_unlock(&ctl_softc->ctl_lock);
12922 uint32_t skipped_prints;
12924 skipped_prints = ctl_softc->skipped_prints;
12926 ctl_softc->skipped_prints = 0;
12927 ctl_softc->last_print_jiffies = time_uptime;
12929 if (have_lock == 0)
12930 mtx_unlock(&ctl_softc->ctl_lock);
12931 if (skipped_prints > 0) {
12933 csevent_log(CSC_CTL | CSC_SHELF_SW |
12935 csevent_LogType_Trace,
12936 csevent_Severity_Information,
12937 csevent_AlertLevel_Green,
12938 csevent_FRU_Firmware,
12939 csevent_FRU_Unknown,
12940 "High CTL error volume, %d prints "
12941 "skipped", skipped_prints);
12944 if (bootverbose || verbose > 0)
12945 ctl_io_error_print(io, NULL);
12948 if (have_lock == 0)
12949 mtx_unlock(&ctl_softc->ctl_lock);
12954 if (have_lock == 0)
12955 mtx_unlock(&ctl_softc->ctl_lock);
12956 if (bootverbose || verbose > 0)
12957 ctl_io_error_print(io, NULL);
12960 if (have_lock == 0)
12961 mtx_unlock(&ctl_softc->ctl_lock);
12966 * Tell the FETD or the other shelf controller we're done with this
12967 * command. Note that only SCSI commands get to this point. Task
12968 * management commands are completed above.
12970 * We only send status to the other controller if we're in XFER
12971 * mode. In SER_ONLY mode, the I/O is done on the controller that
12972 * received the I/O (from CTL's perspective), and so the status is
12975 * XXX KDM if we hold the lock here, we could cause a deadlock
12976 * if the frontend comes back in in this context to queue
12979 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
12980 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12981 union ctl_ha_msg msg;
12983 memset(&msg, 0, sizeof(msg));
12984 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12985 msg.hdr.original_sc = io->io_hdr.original_sc;
12986 msg.hdr.nexus = io->io_hdr.nexus;
12987 msg.hdr.status = io->io_hdr.status;
12988 msg.scsi.scsi_status = io->scsiio.scsi_status;
12989 msg.scsi.tag_num = io->scsiio.tag_num;
12990 msg.scsi.tag_type = io->scsiio.tag_type;
12991 msg.scsi.sense_len = io->scsiio.sense_len;
12992 msg.scsi.sense_residual = io->scsiio.sense_residual;
12993 msg.scsi.residual = io->scsiio.residual;
12994 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12995 sizeof(io->scsiio.sense_data));
12997 * We copy this whether or not this is an I/O-related
12998 * command. Otherwise, we'd have to go and check to see
12999 * whether it's a read/write command, and it really isn't
13002 memcpy(&msg.scsi.lbalen,
13003 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13004 sizeof(msg.scsi.lbalen));
13006 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13007 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13008 /* XXX do something here */
13017 return (CTL_RETVAL_COMPLETE);
13021 * Front end should call this if it doesn't do autosense. When the request
13022 * sense comes back in from the initiator, we'll dequeue this and send it.
13025 ctl_queue_sense(union ctl_io *io)
13027 struct ctl_lun *lun;
13028 struct ctl_softc *ctl_softc;
13029 uint32_t initidx, targ_lun;
13031 ctl_softc = control_softc;
13033 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13036 * LUN lookup will likely move to the ctl_work_thread() once we
13037 * have our new queueing infrastructure (that doesn't put things on
13038 * a per-LUN queue initially). That is so that we can handle
13039 * things like an INQUIRY to a LUN that we don't have enabled. We
13040 * can't deal with that right now.
13042 mtx_lock(&ctl_softc->ctl_lock);
13045 * If we don't have a LUN for this, just toss the sense
13048 targ_lun = io->io_hdr.nexus.targ_lun;
13049 if (io->io_hdr.nexus.lun_map_fn != NULL)
13050 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13051 if ((targ_lun < CTL_MAX_LUNS)
13052 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13053 lun = ctl_softc->ctl_luns[targ_lun];
13057 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13060 * Already have CA set for this LUN...toss the sense information.
13062 if (ctl_is_set(lun->have_ca, initidx))
13065 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13066 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13067 sizeof(io->scsiio.sense_data)));
13068 ctl_set_mask(lun->have_ca, initidx);
13071 mtx_unlock(&ctl_softc->ctl_lock);
13075 return (CTL_RETVAL_COMPLETE);
13079 * Primary command inlet from frontend ports. All SCSI and task I/O
13080 * requests must go through this function.
13083 ctl_queue(union ctl_io *io)
13085 struct ctl_softc *ctl_softc;
13087 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13089 ctl_softc = control_softc;
13092 io->io_hdr.start_time = time_uptime;
13093 getbintime(&io->io_hdr.start_bt);
13094 #endif /* CTL_TIME_IO */
13096 mtx_lock(&ctl_softc->ctl_lock);
13098 switch (io->io_hdr.io_type) {
13100 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
13104 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
13106 * Set the task pending flag. This is necessary to close a
13107 * race condition with the FETD:
13109 * - FETD submits a task management command, like an abort.
13110 * - Back end calls fe_datamove() to move the data for the
13111 * aborted command. The FETD can't really accept it, but
13112 * if it did, it would end up transmitting data for a
13113 * command that the initiator told us to abort.
13115 * We close the race condition by setting the flag here,
13116 * and checking it in ctl_datamove(), before calling the
13117 * FETD's fe_datamove routine. If we've got a task
13118 * pending, we run the task queue and then check to see
13119 * whether our particular I/O has been aborted.
13121 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
13124 mtx_unlock(&ctl_softc->ctl_lock);
13125 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13127 break; /* NOTREACHED */
13129 mtx_unlock(&ctl_softc->ctl_lock);
13131 ctl_wakeup_thread();
13133 return (CTL_RETVAL_COMPLETE);
13136 #ifdef CTL_IO_DELAY
13138 ctl_done_timer_wakeup(void *arg)
13142 io = (union ctl_io *)arg;
13143 ctl_done_lock(io, /*have_lock*/ 0);
13145 #endif /* CTL_IO_DELAY */
13148 ctl_done_lock(union ctl_io *io, int have_lock)
13150 struct ctl_softc *ctl_softc;
13151 #ifndef CTL_DONE_THREAD
13153 #endif /* !CTL_DONE_THREAD */
13155 ctl_softc = control_softc;
13157 if (have_lock == 0)
13158 mtx_lock(&ctl_softc->ctl_lock);
13161 * Enable this to catch duplicate completion issues.
13164 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13165 printf("%s: type %d msg %d cdb %x iptl: "
13166 "%d:%d:%d:%d tag 0x%04x "
13167 "flag %#x status %x\n",
13169 io->io_hdr.io_type,
13170 io->io_hdr.msg_type,
13172 io->io_hdr.nexus.initid.id,
13173 io->io_hdr.nexus.targ_port,
13174 io->io_hdr.nexus.targ_target.id,
13175 io->io_hdr.nexus.targ_lun,
13176 (io->io_hdr.io_type ==
13178 io->taskio.tag_num :
13179 io->scsiio.tag_num,
13181 io->io_hdr.status);
13183 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13187 * This is an internal copy of an I/O, and should not go through
13188 * the normal done processing logic.
13190 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
13191 if (have_lock == 0)
13192 mtx_unlock(&ctl_softc->ctl_lock);
13197 * We need to send a msg to the serializing shelf to finish the IO
13198 * as well. We don't send a finish message to the other shelf if
13199 * this is a task management command. Task management commands
13200 * aren't serialized in the OOA queue, but rather just executed on
13201 * both shelf controllers for commands that originated on that
13204 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13205 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13206 union ctl_ha_msg msg_io;
13208 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13209 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13210 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13211 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13213 /* continue on to finish IO */
13215 #ifdef CTL_IO_DELAY
13216 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13217 struct ctl_lun *lun;
13219 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13221 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13223 struct ctl_lun *lun;
13225 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13228 && (lun->delay_info.done_delay > 0)) {
13229 struct callout *callout;
13231 callout = (struct callout *)&io->io_hdr.timer_bytes;
13232 callout_init(callout, /*mpsafe*/ 1);
13233 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13234 callout_reset(callout,
13235 lun->delay_info.done_delay * hz,
13236 ctl_done_timer_wakeup, io);
13237 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13238 lun->delay_info.done_delay = 0;
13239 if (have_lock == 0)
13240 mtx_unlock(&ctl_softc->ctl_lock);
13244 #endif /* CTL_IO_DELAY */
13246 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
13248 #ifdef CTL_DONE_THREAD
13249 if (have_lock == 0)
13250 mtx_unlock(&ctl_softc->ctl_lock);
13252 ctl_wakeup_thread();
13253 #else /* CTL_DONE_THREAD */
13254 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
13256 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
13258 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
13260 ctl_process_done(xio, /*have_lock*/ 1);
13262 if (have_lock == 0)
13263 mtx_unlock(&ctl_softc->ctl_lock);
13264 #endif /* CTL_DONE_THREAD */
13268 ctl_done(union ctl_io *io)
13270 ctl_done_lock(io, /*have_lock*/ 0);
13274 ctl_isc(struct ctl_scsiio *ctsio)
13276 struct ctl_lun *lun;
13279 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13281 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13283 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13285 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13292 ctl_work_thread(void *arg)
13294 struct ctl_softc *softc;
13296 struct ctl_be_lun *be_lun;
13299 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13301 softc = (struct ctl_softc *)arg;
13305 mtx_lock(&softc->ctl_lock);
13310 * We handle the queues in this order:
13311 * - task management
13313 * - done queue (to free up resources, unblock other commands)
13317 * If those queues are empty, we break out of the loop and
13320 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
13322 ctl_run_task_queue(softc);
13325 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
13327 STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
13328 ctl_handle_isc(io);
13331 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
13333 STAILQ_REMOVE_HEAD(&softc->done_queue, links);
13334 /* clear any blocked commands, call fe_done */
13335 mtx_unlock(&softc->ctl_lock);
13338 * Call this without a lock for now. This will
13339 * depend on whether there is any way the FETD can
13340 * sleep or deadlock if called with the CTL lock
13343 retval = ctl_process_done(io, /*have_lock*/ 0);
13344 mtx_lock(&softc->ctl_lock);
13347 if (!ctl_pause_rtr) {
13348 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
13350 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
13351 mtx_unlock(&softc->ctl_lock);
13352 retval = ctl_scsiio(&io->scsiio);
13353 if (retval != CTL_RETVAL_COMPLETE)
13354 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13355 mtx_lock(&softc->ctl_lock);
13359 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
13361 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
13362 mtx_unlock(&softc->ctl_lock);
13363 ctl_scsiio_precheck(softc, &io->scsiio);
13364 mtx_lock(&softc->ctl_lock);
13368 * We might want to move this to a separate thread, so that
13369 * configuration requests (in this case LUN creations)
13370 * won't impact the I/O path.
13372 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13373 if (be_lun != NULL) {
13374 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13375 mtx_unlock(&softc->ctl_lock);
13376 ctl_create_lun(be_lun);
13377 mtx_lock(&softc->ctl_lock);
13381 /* XXX KDM use the PDROP flag?? */
13382 /* Sleep until we have something to do. */
13383 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "-", 0);
13385 /* Back to the top of the loop to see what woke us up. */
13391 ctl_wakeup_thread()
13393 struct ctl_softc *softc;
13395 softc = control_softc;
13400 /* Initialization and failover */
13403 ctl_init_isc_msg(void)
13405 printf("CTL: Still calling this thing\n");
13410 * Initializes component into configuration defined by bootMode
13412 * returns hasc_Status:
13414 * ERROR - fatal error
13416 static ctl_ha_comp_status
13417 ctl_isc_init(struct ctl_ha_component *c)
13419 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13426 * Starts component in state requested. If component starts successfully,
13427 * it must set its own state to the requestrd state
13428 * When requested state is HASC_STATE_HA, the component may refine it
13429 * by adding _SLAVE or _MASTER flags.
13430 * Currently allowed state transitions are:
13431 * UNKNOWN->HA - initial startup
13432 * UNKNOWN->SINGLE - initial startup when no parter detected
13433 * HA->SINGLE - failover
13434 * returns ctl_ha_comp_status:
13435 * OK - component successfully started in requested state
13436 * FAILED - could not start the requested state, failover may
13438 * ERROR - fatal error detected, no future startup possible
13440 static ctl_ha_comp_status
13441 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13443 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13445 printf("%s: go\n", __func__);
13447 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13448 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13450 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13451 != CTL_HA_STATUS_SUCCESS) {
13452 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13453 ret = CTL_HA_COMP_STATUS_ERROR;
13455 } else if (CTL_HA_STATE_IS_HA(c->state)
13456 && CTL_HA_STATE_IS_SINGLE(state)){
13457 // HA->SINGLE transition
13461 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13463 ret = CTL_HA_COMP_STATUS_ERROR;
13465 if (CTL_HA_STATE_IS_SINGLE(state))
13474 * Quiesce component
13475 * The component must clear any error conditions (set status to OK) and
13476 * prepare itself to another Start call
13477 * returns ctl_ha_comp_status:
13481 static ctl_ha_comp_status
13482 ctl_isc_quiesce(struct ctl_ha_component *c)
13484 int ret = CTL_HA_COMP_STATUS_OK;
13491 struct ctl_ha_component ctl_ha_component_ctlisc =
13494 .state = CTL_HA_STATE_UNKNOWN,
13495 .init = ctl_isc_init,
13496 .start = ctl_isc_start,
13497 .quiesce = ctl_isc_quiesce